Search code examples
cmulticast

Understand the received packet length from a multicast group address


https://github.com/dashersw/docker-cloud-multicast/blob/master/mcreceive.c

$ ./mcreceive X.X.X.X  60001

where X.X.X.X is a multicast address

Received 28 bytes from X.X.X.X: Ϭ(
Received 28 bytes from X.X.X.X: Ϭ(
Received 28 bytes from X.X.X.X: Ϭ(
Received 28 bytes from X.X.X.X: Ϭ(
Received 56 bytes from X.X.X.X: Ϭ(
Received 28 bytes from X.X.X.X: (
Received 28 bytes from X.X.X.X: Ϭ(
Received 56 bytes from X.X.X.X: Ϭ(
Received 168 bytes from X.X.X.X: Ϭ(
Received 28 bytes from X.X.X.X: %Ϭ(
Received 28 bytes from X.X.X.X: &Ϭ(
Received 28 bytes from X.X.X.X: 'Ϭ(

Ethernet II: 18 bytes
IP: Minimum 20 bytes (can be more with options)
UDP: 8 bytes

Related source code:

/* create socket to join multicast group on */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  perror("socket() failed");
  exit(1);
}

...

/* block waiting to receive a packet */
if ((recv_len = recvfrom(sock, recv_str, MAX_LEN, 0,
     (struct sockaddr*)&from_addr, &from_len)) < 0) {
  perror("recvfrom() failed");
  exit(1);
}

/* output received string */
printf("Received %d bytes from %s: ", recv_len,
       inet_ntoa(from_addr.sin_addr));           

Question> As you can see, I have received a few packets with a length of 28. How could I receive a UDP packet with length less than 18+20+8?


Solution

  • You created a UDP socket, so recvfrom only returns the UDP payload, not the UDP or lower layer headers. So the length being return is that of just the payload.

    A raw socket would give you access to the Ethernet, IP, and UDP headers.