M
Muhammad Farooq-i-Azam
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_protoaddr and arp.dst_protoaddr
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_protoaddr.
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_format; /* Same as Ether Type field */
u_int8_t hwaddr_length; /* Lenght of hardware address
*/
u_int8_t protoaddr_length; /* 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_header_t));
printf("sizeof(arp) = %d\n", sizeof(arp));
printf("sizeof(arp.hwaddr_format) = %d\n",
sizeof(arp.hwaddr_format));
printf("sizeof(arp.protoaddr_format) = %d\n",
sizeof(arp.protoaddr_format));
printf("sizeof(arp.hwaddr_length) = %d\n",
sizeof(arp.hwaddr_length));
printf("sizeof(arp.protoaddr_length) = %d\n",
sizeof(arp.protoaddr_length));
printf("sizeof(arp.opcode) = %d\n", sizeof(arp.opcode));
printf("sizeof(arp.src_hwaddr) = %d\n", sizeof(arp.src_hwaddr));
printf("sizeof(arp.src_protoaddr) = %d\n",
sizeof(arp.src_protoaddr));
printf("sizeof(arp.dst_hwaddr) = %d\n", sizeof(arp.dst_hwaddr));
printf("sizeof(arp.dst_protoaddr) = %d\n",
sizeof(arp.dst_protoaddr));
printf("\n");
printf("&arp.hwaddr_format = 0x%x\n", &arp.hwaddr_format);
printf("&arp.protoaddr_format = 0x%x\n", &arp.protoaddr_format);
printf("&arp.hwaddr_length = 0x%x\n", &arp.hwaddr_length);
printf("&arp.protoaddr_length = 0x%x\n", &arp.protoaddr_length);
printf("&arp.opcode = 0x%x\n", &arp.opcode);
printf("&arp.src_hwaddr = 0x%x\n", &arp.src_hwaddr);
printf("&arp.src_protoaddr = 0x%x\n", &arp.src_protoaddr);
printf("&arp.dst_hwaddr = 0x%x\n", &arp.dst_hwaddr);
printf("&arp.dst_protoaddr = 0x%x\n", &arp.dst_protoaddr);
return 0;
}
Here is a sample output from the code:
sizeof(u_int32_t) = 4
sizeof(arp_header_t) = 32
sizeof(arp) = 32
sizeof(arp.hwaddr_format) = 2
sizeof(arp.protoaddr_format) = 2
sizeof(arp.hwaddr_length) = 1
sizeof(arp.protoaddr_length) = 1
sizeof(arp.opcode) = 2
sizeof(arp.src_hwaddr) = 6
sizeof(arp.src_protoaddr) = 4
sizeof(arp.dst_hwaddr) = 6
sizeof(arp.dst_protoaddr) = 4
&arp.hwaddr_format = 0xbfffe840
&arp.protoaddr_format = 0xbfffe842
&arp.hwaddr_length = 0xbfffe844
&arp.protoaddr_length = 0xbfffe845
&arp.opcode = 0xbfffe846
&arp.src_hwaddr = 0xbfffe848
&arp.src_protoaddr = 0xbfffe850 [PROBLEM HERE]
&arp.dst_hwaddr = 0xbfffe854 [PROBLEM HERE]
&arp.dst_protoaddr = 0xbfffe85c [PROBLEM HERE]
Thanks in advance for any help.
Muhammad Farooq-i-Azam
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_protoaddr and arp.dst_protoaddr
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_protoaddr.
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_format; /* Same as Ether Type field */
u_int8_t hwaddr_length; /* Lenght of hardware address
*/
u_int8_t protoaddr_length; /* 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_header_t));
printf("sizeof(arp) = %d\n", sizeof(arp));
printf("sizeof(arp.hwaddr_format) = %d\n",
sizeof(arp.hwaddr_format));
printf("sizeof(arp.protoaddr_format) = %d\n",
sizeof(arp.protoaddr_format));
printf("sizeof(arp.hwaddr_length) = %d\n",
sizeof(arp.hwaddr_length));
printf("sizeof(arp.protoaddr_length) = %d\n",
sizeof(arp.protoaddr_length));
printf("sizeof(arp.opcode) = %d\n", sizeof(arp.opcode));
printf("sizeof(arp.src_hwaddr) = %d\n", sizeof(arp.src_hwaddr));
printf("sizeof(arp.src_protoaddr) = %d\n",
sizeof(arp.src_protoaddr));
printf("sizeof(arp.dst_hwaddr) = %d\n", sizeof(arp.dst_hwaddr));
printf("sizeof(arp.dst_protoaddr) = %d\n",
sizeof(arp.dst_protoaddr));
printf("\n");
printf("&arp.hwaddr_format = 0x%x\n", &arp.hwaddr_format);
printf("&arp.protoaddr_format = 0x%x\n", &arp.protoaddr_format);
printf("&arp.hwaddr_length = 0x%x\n", &arp.hwaddr_length);
printf("&arp.protoaddr_length = 0x%x\n", &arp.protoaddr_length);
printf("&arp.opcode = 0x%x\n", &arp.opcode);
printf("&arp.src_hwaddr = 0x%x\n", &arp.src_hwaddr);
printf("&arp.src_protoaddr = 0x%x\n", &arp.src_protoaddr);
printf("&arp.dst_hwaddr = 0x%x\n", &arp.dst_hwaddr);
printf("&arp.dst_protoaddr = 0x%x\n", &arp.dst_protoaddr);
return 0;
}
Here is a sample output from the code:
sizeof(u_int32_t) = 4
sizeof(arp_header_t) = 32
sizeof(arp) = 32
sizeof(arp.hwaddr_format) = 2
sizeof(arp.protoaddr_format) = 2
sizeof(arp.hwaddr_length) = 1
sizeof(arp.protoaddr_length) = 1
sizeof(arp.opcode) = 2
sizeof(arp.src_hwaddr) = 6
sizeof(arp.src_protoaddr) = 4
sizeof(arp.dst_hwaddr) = 6
sizeof(arp.dst_protoaddr) = 4
&arp.hwaddr_format = 0xbfffe840
&arp.protoaddr_format = 0xbfffe842
&arp.hwaddr_length = 0xbfffe844
&arp.protoaddr_length = 0xbfffe845
&arp.opcode = 0xbfffe846
&arp.src_hwaddr = 0xbfffe848
&arp.src_protoaddr = 0xbfffe850 [PROBLEM HERE]
&arp.dst_hwaddr = 0xbfffe854 [PROBLEM HERE]
&arp.dst_protoaddr = 0xbfffe85c [PROBLEM HERE]
Thanks in advance for any help.
Muhammad Farooq-i-Azam