3

I have this code to send multicast messages to a group. There are no errors while running the program but when I monitor packets in Wireshark the ethernet destination of my packets are of my default gateway instead of something like 01-00-5e-xx-xx-xx

The code:


#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>

void main(int argc, char **argv){
    int sockfd;
    struct in_addr interface;
    struct sockaddr_in group;
    char readbuf[1024];

    if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0){
        perror("socket error");
    }

    memset(&group,0,sizeof(group));
    group.sin_family=AF_INET;
    group.sin_addr.s_addr=inet_addr("244.244.244.1");
    group.sin_port=htons(5555);



    char loop=0;
    if(setsockopt(sockfd,IPPROTO_IP,IP_MULTICAST_LOOP,&loop,sizeof(loop))<0){
        perror("setsockopt error(IP_MULTICAST_LOOP)");
    }

    interface.s_addr=inet_addr("192.168.1.69");
    if(setsockopt(sockfd,IPPROTO_IP,IP_MULTICAST_IF,&interface,sizeof(interface))<0){
        perror("setsockopt error(IP_MULTICAST_IF)");
    }

    for(;;){
        fgets(readbuf,sizeof(readbuf),stdin);
        if(sendto(sockfd,readbuf,sizeof(readbuf),0,(struct sockaddr *)&group,sizeof(struct sockaddr))==-1){
            perror("sendto error");
        }
    }

}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
mojo
  • 61
  • 4
  • For 10K+: https://stackoverflow.com/questions/71405670/multicast-datagram-being-sent-like-unicast – dbush Mar 10 '22 at 14:10
  • You are not using a multicast address. You are using a Reserved address that most devices recognize as an invalid IP address and refuse. You should be using a multicast address in the multicast Organization-Local scope (`239.0.0.0` to `239.255.255.255`). – Ron Maupin Mar 10 '22 at 14:18

1 Answers1

3

244.244.244.1 is not a valid multicast address.

Multicast address are in the range of 224.0.0.1 - 239.255.255.255. The address you're sending to is not in that range. So the outgoing MAC address is not a multicast MAC.

Change the destination IP to be in the range of multicast IP addresses and you'll see a proper multicast MAC address.

dbush
  • 205,898
  • 23
  • 218
  • 273