6
votes

(Excuse me if I am not able to put the question correctly. English in not my primary language.)

I am trying to parse SyncE ESMC packet. It is Ethernet slow protocol packet.

Approach 1: For parsing this packet, I used byte by byte approach similar to what has been done here.

Approach 2: Other way to parse the packet would be to define a 'structure' to represent the whole packet and access individual fields to retrieve the value at particular offset. However in this approach structure padding and alignment may come into picture(which I am not sure) but on Linux various packet headers are defined in form of structure, e.g. iphdr in ip.h. IP packets (buffer) can be type casted to 'iphdr' to retrieve ip header fields, so it must be working.

Which approach is better to parse a network packet in C?

Does structure padding and aligment make any difference while parsing a packet via approach 2? If yes, how did Linux headers overcome this problem?

1
All compilers have functionality to not pad structures or align member fields, and you could also use bitfields for fields shorter an a byte.Some programmer dude
@JoachimPileborg - which approach would be better, portabale and efficient?Abhirav
Portable between compilers, operating systems, hardware, or a combination thereof? More efficient as in fast, using less memory, or some other criteria? Both approaches have merits, and which one is "best" depends depends a lot on your goals.Some programmer dude
I am trying to write a protocol stack which can ported to different kind of OS(e.g. Linux, qnx, vxworks) so portability is must across OS and Compilers. In context of the above description what would you suggest?Abhirav

1 Answers

1
votes

Approach 1 is the best for portability. It allows you to safely avoid misaligned accesses, for example. In particular, if your machine is little-endian, this approach lets you take care of the byte-swapping very easily.

Approach 2 is sometimes convenient and often is faster code to write. If structure padding gets in your way, your compiler probably provides a flag or attribute (like __attribute__((__packed__)) or #pragma pack) to work around it. If you have a little-endian machine, however, you're still going to have to byteswap fields all over the place.