networking · advanced · ~20 min
Per-byte big-endian decoding of a real protocol header.
Decode an IGMPv2 membership-query packet from a fixed byte buffer — the wire format hosts use to join IPv4 multicast groups, without any real multicast.
Implement int parse_igmp_query(const unsigned char *buf, int len, int *out_max_resp_ds, unsigned *out_group). The grader supplies a fixed byte buffer.
The 8-byte packet layout:
byte 0: type (0x11 = membership query)
byte 1: max response time (in 1/10 of a second)
byte 2-3: checksum (ignored here)
byte 4-7: group address (4 bytes, big-endian; all zeros = general query)
buf: the packet bytes.len: number of bytes available in buf.out_max_resp_ds: receives byte 1 (max response time, in deciseconds).out_group: receives the group address as a host-order unsigned (big-endian on the wire).Return 1 when len >= 8 and buf[0] == 0x11; set *out_max_resp_ds and *out_group. Otherwise return 0 and leave nothing useful in the outputs.
{0x11, 100, 0xEE, 0xAB, 0,0,0,0} -> 1, max_resp=100, group=0x00000000 (general query)
{0x11, 50, 0,0, 0xE0,0,0,0x01} -> 1, max_resp=50, group=0xE0000001 (224.0.0.1)
len = 7 -> 0 (too short)
{0x12, ...} -> 0 (type not 0x11)
len < 8 returns 0.0x11 returns 0.(buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7].IGMP is how IPv4 hosts join multicast groups. Decoding one packet from bytes teaches the wire format without needing real multicast.
buf, the packet bytes; len, bytes available; out_max_resp_ds and out_group, output pointers.
1 with both outputs set when len>=8 and buf[0]==0x11; else 0.
Bound-check len>=8 before reading; decode the group address big-endian.
#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.