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");
}
}
}
CodePudding user response:
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.