networking · advanced · ~20 min

Decode IGMP membership-query bytes

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

Challenge

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.

Task

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)

Input

  • 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).

Output

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.

Example

{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)

Edge cases

  • len < 8 returns 0.
  • A type byte other than 0x11 returns 0.
  • A general query has group address 0.

Rules

  • The group address is big-endian: (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7].

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

buf, the packet bytes; len, bytes available; out_max_resp_ds and out_group, output pointers.

Output format

1 with both outputs set when len>=8 and buf[0]==0x11; else 0.

Constraints

Bound-check len>=8 before reading; decode the group address big-endian.

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.