473,779 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is the problem with my arp structure?

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

Nov 14 '05 #1
3 2402
Muhammad Farooq-i-Azam wrote on 17/12/04 :
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.


Don't indent to map a C structure to a byte pattern. Due to various
reasons (open your C-book), it simply doesn't work. Period.

Use a byte memory stream (array of unsigned char), and read/write the
data from/to it a the required place byte by byte.

Numerical representation format are specified. The hton()/ntoh()
functions can help. Note that these functions are not standard, but
they intend to be portable and available on platforms using a network
acces.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++

Nov 14 '05 #2
Emmanuel!

Real thanks for your elaboration. I found the following
link from the faq you pointed to quite helpful:

http://www.eskimo.com/~scs/C-faq/q2.13.html

Yes, when I use

u_int8_t src_protoaddr[4];

instead of

u_int32_t src_protoaddr;
the problem is resolved.

Muhammad Farooq-i-Azam

Nov 14 '05 #3
On 17 Dec 2004 08:26:46 -0800, Muhammad Farooq-i-Azam
<fa***********@ gmail.com> wrote:
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.
Alignment of variables is your problem.
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, what follows src_hwaddr is a 32 bit value (src_protoaddr) . It
seems that on whatever machine you are using 32 bit variables have to be
aligned on a 4-byte boundary, so a 'hole' is left to bring the address
to the correct boundary.

You may be able to get round this with a #pragma or a (non-standard)
attribute, or with a compiler flag (although you have to be careful with
that since it will affect every structure in that module so every other
module needs to use the same flag). Look in your compiler documentation
for terms like 'pack' and 'packed'. However, not only is this
nonstandard but it also isn't guaranteed, your processor may mot work
with misaligned data access (the i86 processors will access unaligned
data, but more slowly, but RISC ones will often fault if you try to
access a 32 bit value which crosses word boundaries).

The C standard deliberately doesn't define how or whether packing is
done. See section 6.7.2.1:

11 Each non-bit-field member of a structure or union object is
aligned in an implementation defined manner appropriate to its
type.

12 Within a structure object, the non-bit-field members and the
units in which bit-fields reside have addresses that increase in
the order in which they are declared. A pointer to a structure
object, suitably converted, points to its initial member (or if
that member is a bit-field, then to the unit in which it
resides), and vice versa. There may be unnamed padding within a
structure object, but not at its beginning.

Any amount of 'padding' can be within a structure, with the exception of
bit fields, so getting a structure which maps onto a specific byte
sequence is not defined. The only sure way to hadle it is to extract
the bytes from memory and reassemble them yourself into the specified
fields (that way you can also handle the 'endianness' in a portable
way).

When dealing with network structures I have written functions to extract
from and write to byte arrays 32 and 16 bit values. For instance:

unsigned long getData16(unsig ned char *data, int offset)
{
return (data[offset] << 8) | data[offset+1];
}

unsigned long getData32(unsig ned char *data, int offset)
{
return (getData16(data , offset) << 16) | getData16(data, offset+2);
}

void putData16(unsig ned char *data, int offset, unsigned long val)
{
data[offset] = (val >> 8) & 0xFF;
data[offset+1] = val & 0xFF;
}

void putData32(unsig ned char *data, int offset, unsigned long val)
{
putData16(data, offset, val >> 16);
putData16(data, offset+2, val);
}

Then I use the data buffer and explicit offsets to put and get the
fields and don't have to worry about endianness or alignment...

Chris C
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1972
by: Clarence | last post by:
Hi - I have a problem and here is the verbose version of what I am trying to do (better too much info than not enough). I am searching through about 4,700 XML files containing company contact details. In the company details are phone numbers. The phone numbers with the formats I need have the following structure. <xs:complexType name="PHONENO"> <xs:sequence maxOccurs="unbounded"> <xs:element name="COUNTRY" type="xs:string"/>
2
6467
by: yee young han | last post by:
I need a fast data structure and algorithm like below condition. (1) this data structure contain only 10,000 data entry. (2) data structure's one entry is like below typedef struct _DataEntry_ { char szInput; char szOutput; int iSum;
15
2781
by: damian birchler | last post by:
Hi I'm wondering of what type a structure is. Of course, it is a _structure_, but an array isn't an _array_ either. So of what type is a structure? I'd say a pointer, am I right?
2
5072
by: Tom | last post by:
I'm getting this error when I try to pass a structure to a dll. An unhandled exception of type 'System.ArgumentException' occured in Test1.exe Additional Information: Type could not be marshaled because the length of an embedded array instance does not match the declared length in the layout What does it mean?
39
3234
by: windandwaves | last post by:
Hi Folk I have to store up to eight boolean bits of information about an item in my database. e.g. with restaurant drive-through facility yellow windows
14
2282
by: teslar91 | last post by:
As a fairly new .NET coder, I would greatly appreciate some comments on any .NET classes that are known to be notoriously slow by comparison to direct API calls. I've already had a bad experience with System.IO.DirectoryInfo. My requirements were to recursively scan a folder, recording all filenames/dates/sizes/attribs. The target folder contained 88,000 files and 5,000 subfolders. I originally used System.IO.DirectoryInfo and found...
8
40471
by: cman | last post by:
What does this kind of typedef accomplish? typedef struct { unsigned long pte_low; } pte_t; typedef struct { unsigned long pgd; } pgd_t; typedef struct { unsigned long pgprot; } pgprot_t I am familiar with "typedef int NUMBER", but how does it work with structures. Tilak
14
1794
by: Mark S. | last post by:
Hello, I've written a high performance web app with C# and it completely relies on static hash tables (using sync) and classes. Under real world stress this app is handling 5 get requests per CPU per second. I've gone from a single-core/single-proc, to a dual-core/single-proc to a dual-core/dual-proc and as I suspected the number of requests per second per CPU does not increase it remains at 5 per CPU.
10
11357
by: giddy | last post by:
hi , I' know what the StructLayoutAttribute does. I've seen it being used. But what does the Pack field do!? This is what MSDN says: Controls the alignment of data fields of a class or structure in memory. This field indicates the packing size that should be used when the LayoutKind.Sequential value is specified. The value of Pack must be 0,
0
9632
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9471
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10302
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9925
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8958
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2867
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.