[C-prog-lang-l] extra task: IP header parsing

Vladimír Kotal vlada at kotalovi.cz
Mon Mar 20 22:46:29 CET 2023


Hi all,

here's extra task for practicing programming with pointers described at the end of today's lecture:

Consider this IPv4 packet (no options/padding, just a plain IPv4 packet):
`0000   45 00 00 34 65 6e 40 00 40 06 0a 15 0a 00 03 5d
0010   03 7e ba 66`

The first column is offset (in hex), not the actual packet data.
Initialize an array with the individual bytes from the packet and then use pointer arithmetics, in particular pointer dereference and postfix increment or decrement, and print the source and destination IP address to standard output in the common dot notation form (a.b.c.d).

See RFC 791 <https://www.rfc-editor.org/rfc/rfc791> for the packet structure.

Attached is the packet dump in PCAP format, you can open it in your favourite network analyzer, such as Wireshark, tshark, tcpdump etc. This will help you to verify that you got the IP octets printing right.

As I mentioned today, there is bigger potential for this task, to parse more fields of the packet, however we have not properly introduced bitwise operators (needed for some of the fields with multi-byte values or flags) yet.

Have fun,


V. Kotal
-------------- next part --------------
HTML attachment scrubbed and removed
-------------- next part --------------
A non-text attachment was scrubbed...
Name: single-packet.pcap
Type: application/octet-stream
Size: 730 bytes
Desc: not available
URL: <http://mbox.ms.mff.cuni.cz/listserv/archive/c-prog-lang-l/attachments/20230320/754f9aa1/attachment.obj>


More information about the c-prog-lang-l mailing list