Hi,
I am trying to define an arp structure but having problem
doing so. I think I have define the correct arp structure but
I find myself in a strange problem. The size of structure that
I have defined is 28 bytes, but it is reported to be 32 bytes by
the sizeof() call.
Interestingly, sum of individuals members of the sturcture is 28.
However, the sizeof() the entire structure is returned to be 32.
I tried to display the addresses of various structure members
and found that addresses of arp.src_protoad dr and arp.dst_protoad dr
which are both (4 bytes) are not correct boundaries.
Size of arp.src_hwaddr is 6 bytes. Its address is 0xbfffe848.
Therefore, the size of the next structure member should be
0xbfffe848+6 = 0xbfffe84e. However, the actual address that I get
from my code below is 0xbfffe850 which is larger by 2 bytes.
Same is the problem with arp.dst_protoad dr.
Here is the code listing that I am using:
/*************** *************** *************** *************** **
* Why is the structure size returned 32 ?
*************** *************** *************** *************** **/
#include <sys/types.h>
int main(void)
{
typedef struct arp_header
{
u_int16_t hwaddr_format; /* Ethernet, Token Ring, etc */
u_int16_t protoaddr_forma t; /* Same as Ether Type field */
u_int8_t hwaddr_length; /* Lenght of hardware address
*/
u_int8_t protoaddr_lengt h; /* Length of protocol address
*/
u_int16_t opcode; /* Request or Response */
u_int8_t src_hwaddr[6]; /* Source Hardware Address */
u_int32_t src_protoaddr; /* Source Protocol Address */
u_int8_t dst_hwaddr[6]; /* Destination Hardware Address
*/
u_int32_t dst_protoaddr; /* Destination Protocol Address
*/
} arp_header_t;
arp_header_t arp;
printf("sizeof( u_int32_t) = %d\n", sizeof(u_int32_ t));
printf("sizeof( arp_header_t) = %d\n", sizeof(arp_head er_t));
printf("sizeof( arp) = %d\n", sizeof(arp));
printf("sizeof( arp.hwaddr_form at) = %d\n",
sizeof(arp.hwad dr_format));
printf("sizeof( arp.protoaddr_f ormat) = %d\n",
sizeof(arp.prot oaddr_format));
printf("sizeof( arp.hwaddr_leng th) = %d\n",
sizeof(arp.hwad dr_length));
printf("sizeof( arp.protoaddr_l ength) = %d\n",
sizeof(arp.prot oaddr_length));
printf("sizeof( arp.opcode) = %d\n", sizeof(arp.opco de));
printf("sizeof( arp.src_hwaddr) = %d\n", sizeof(arp.src_ hwaddr));
printf("sizeof( arp.src_protoad dr) = %d\n",
sizeof(arp.src_ protoaddr));
printf("sizeof( arp.dst_hwaddr) = %d\n", sizeof(arp.dst_ hwaddr));
printf("sizeof( arp.dst_protoad dr) = %d\n",
sizeof(arp.dst_ protoaddr));
printf("\n");
printf("&arp.hw addr_format = 0x%x\n", &arp.hwaddr_for mat);
printf("&arp.pr otoaddr_format = 0x%x\n", &arp.protoaddr_ format);
printf("&arp.hw addr_length = 0x%x\n", &arp.hwaddr_len gth);
printf("&arp.pr otoaddr_length = 0x%x\n", &arp.protoaddr_ length);
printf("&arp.op code = 0x%x\n", &arp.opcode) ;
printf("&arp.sr c_hwaddr = 0x%x\n", &arp.src_hwaddr );
printf("&arp.sr c_protoaddr = 0x%x\n", &arp.src_protoa ddr);
printf("&arp.ds t_hwaddr = 0x%x\n", &arp.dst_hwaddr );
printf("&arp.ds t_protoaddr = 0x%x\n", &arp.dst_protoa ddr);
return 0;
}
Here is a sample output from the code:
sizeof(u_int32_ t) = 4
sizeof(arp_head er_t) = 32
sizeof(arp) = 32
sizeof(arp.hwad dr_format) = 2
sizeof(arp.prot oaddr_format) = 2
sizeof(arp.hwad dr_length) = 1
sizeof(arp.prot oaddr_length) = 1
sizeof(arp.opco de) = 2
sizeof(arp.src_ hwaddr) = 6
sizeof(arp.src_ protoaddr) = 4
sizeof(arp.dst_ hwaddr) = 6
sizeof(arp.dst_ protoaddr) = 4
&arp.hwaddr_for mat = 0xbfffe840
&arp.protoaddr_ format = 0xbfffe842
&arp.hwaddr_len gth = 0xbfffe844
&arp.protoaddr_ length = 0xbfffe845
&arp.opcode = 0xbfffe846
&arp.src_hwa ddr = 0xbfffe848
&arp.src_protoa ddr = 0xbfffe850 [PROBLEM HERE]
&arp.dst_hwa ddr = 0xbfffe854 [PROBLEM HERE]
&arp.dst_protoa ddr = 0xbfffe85c [PROBLEM HERE]
Thanks in advance for any help.
Muhammad Farooq-i-Azam