networking · advanced · ~20 min
Per-byte big-endian decoding of a real protocol header.
An IGMPv2 membership query packet has this 8-byte format:
byte 0: type (0x11 = membership query)
byte 1: max response time (in 1/10 of a second)
byte 2: checksum hi
byte 3: checksum lo
byte 4..7: group address (4 bytes, big-endian; all zeros = general query)
Implement int parse_igmp_query(const unsigned char *buf, int len, int *out_max_resp_ds, unsigned *out_group).
Return 1 if len >= 8 and byte 0 == 0x11; set *out_max_resp_ds (deciseconds)
and *out_group (host-order uint32). Else return 0.
IGMP is how IPv4 hosts join multicast groups. Decoding one packet from bytes teaches the wire format without needing real multicast.
Byte buffer + length.
0/1 + two outputs.
Bound-check every read.
#include <stddef.h>
int parse_igmp_query(const unsigned char *buf, int len, int *out_max_resp_ds, unsigned *out_group) { /* TODO */ (void)buf; (void)len; (void)out_max_resp_ds; (void)out_group; return 0; }
Decoding the group address in little-endian.
len < 8. type != 0x11. General query (group == 0).
O(1).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.