I am trying to send data to a multicast group from an alias IP added previously to an interface. I am calling setsockopt
with IP_MULTICAST_IF
and the alias IP. But the data is always sent from the default IP of that interface. For explanation, I am providing some codes.
ip addr show
command gives the following output for ens33
interface:
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:4c:78:71 brd ff:ff:ff:ff:ff:ff
inet 192.168.190.183/24 brd 192.168.190.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.190.50/24 scope global secondary ens33
valid_lft forever preferred_lft forever
Now setsockopt
is used as:
struct in_addr localInterface;
localInterface.s_addr = inet_addr("192.168.190.50");
if(setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, (char *)&localInterface, sizeof(localInterface)) < 0)
{
perror("Setting local interface error");
exit(1);
}
else
{
printf("Setting the local interface...OK\n");
}
But, it send multicast packets always from 192.168.190.183
and no error is thrown during setsockopt
.
Can anyone solve this?