6

(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?

4
  • All compilers have functionality to not pad structures or align member fields, and you could also use bitfields for fields shorter an a byte. Commented Jan 31, 2013 at 20:01
  • @JoachimPileborg - which approach would be better, portabale and efficient? Commented Jan 31, 2013 at 20:14
  • 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. Commented Jan 31, 2013 at 21:14
  • 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? Commented Feb 1, 2013 at 17:40

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.