networking · advanced · ~20 min

Decode IGMP membership-query bytes

Per-byte big-endian decoding of a real protocol header.

Challenge

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.

Why this matters

IGMP is how IPv4 hosts join multicast groups. Decoding one packet from bytes teaches the wire format without needing real multicast.

Input format

Byte buffer + length.

Output format

0/1 + two outputs.

Constraints

Bound-check every read.

Starter code

#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; }

Common mistakes

Decoding the group address in little-endian.

Edge cases to handle

len < 8. type != 0x11. General query (group == 0).

Complexity

O(1).

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.