ANaiveProgrammer said:
I have made the following two structs and size is not according to
what is supposed to be, so please ponder over following and identify
if im wrong...
please also mention what would be the size of "ethernet_frame" struct
and why ?
typedef struct {
//This struct defines Arp--Request.
uint8_t haddr_type
uint8_t proto_type[4];
uint8_t hard_len;
uint8_t proto_len
uint16_t operation;
uint8_t sender_hard[6];
uint8_t sender_proto[4];
uint8_t target_ip[4];
}arp_request;
typedef struct{
//Ethernet Frame
uint8_t destip[4];
uint8_t sourceip[4];
uint8_t frametype[3];
arp_request *req;
} ethernet_frame;
int main(){
arp_request request;
ethernet_frame ef;
printf("Size of ARP---%d\n",sizeof(request));
printf("Size of Ethernet---%d\n",sizeof(ef));
}
--------
im compiling this code under Red Hat linux(PSyche)...
The size of both structures is determined by the compiler and may
depend on the hardware the program is compiled for. The compiler
is allowed to insert as many padding bytes into the structure as
it deems necessary, see the C-FAQ, section 2.12/13. For that
reason it is _not_ a good idea to try to assemble binary data
in a certain format by using structures. You have to use a "flat"
memory region and populate it by copying the elements into that
region using memcpy(). Only if you don't mind making your program
dependend on the compiler you use then you can use some compiler-
specific extensions to make sure the compiler does not insert
padding bytes (but even in that case you may still have to popu-
late the elements of the structure using memcpy() since on many
machines you may get a bus error when trying to access the
structure elements because the hardware allows only accesses to
e.g. even addresses).
My second question is that how will i initialize a uint8_t array, for
example
As i have defined a uint8_t frametype[3]in ethernet_frame struct...i
have defined this unsigned integer array in another function as
follows
uint8_t frame[3]={0x8,0x0,0x6};
for( k=0; k<3;k++)
ef->frametype[k] = frame[k];
//where 'ef' is a pointer to ethernet_frame struct
Well im very much sure that there would be some better way around than
above in terms of code optimization. please answer this question as
well???
Well, you can use memcpy() (or memmove() in case the data you want to
copy could overlap). If that is faster in your case can only be deter-
mined experimentally. The C language does not make any promises about
the execution speed of a program.
Last but not the least , i need to send the ethernet_frame struct over
a socket , is the following a legitimate way to do that
write(sockfd,(char *)&ef,10);//'ef' is the instance of ethernet_frame
struct
Please tell me how to send a struct over the socket, is above way
correct OR there is another around?
Well, now you rather left the field of what's topical in this group.
The C language has no built-in support for network operations and
also the write() function is not a standard C function but system
dependend. Since you seem to be using Linux the newsgroups
comp.os.linux.development.apps or comp.unix.programmer would seem
to be good places to ask instead.
But typically sending structures in binary form is a bad idea since
the machines on both ends may have quite different ideas about the
lay-out of the structure (e.g. because different amounts of padding
bytes are used) and since the way numbers are represented may differ
a lot between machines with different architectures (look up e.g. the
difference between "big-endian" and "little-endian" machines). It
will probably work when both machines have the same architecture
and the programs on both ends have been compiled with the same
compiler (using the same version of that specific compiler and the
same compiler flags), but otherwise it can fail miserably. It's
the same problem as with writing structures into a file in binary
format and then trying to read them back on a different machine.
One solution would be to convert the data into some common repre-
sentation (e.g. ASCII), send them over to the other side in that
form and then assemble them back into a structure on the other
machine.
Regards, Jens