0

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?

dbush
  • 205,898
  • 23
  • 218
  • 273
JatiA
  • 73
  • 1
  • 4
  • 12

2 Answers2

1

IP_MULTICAST_IF is about receiving. It's about the NIC via which your join and leave messages are sent, which in turn determines who you will receive multicasts from.

If you want to send via a specific NIC or IP address, use bind().

user207421
  • 305,947
  • 44
  • 307
  • 483
  • My concept was wrong. Now, it is working properly. Thanks a lot. – JatiA Sep 03 '14 at 12:39
  • In order to control which interface multicast datagrams will be sent on, the API provides the IP_MULTICAST_IF socket option. This option can be used to set the interface for sending outbound multicast datagrams from the sockets application. Multicast datagrams can be transmitted on only one interface at a time. – Leroy Scandal Mar 08 '23 at 22:43
0

From the Linux Multicast Howto: https://tldp.org/HOWTO/Multicast-HOWTO-6.html#ss6.3

6.3 IP_MULTICAST_IF.

Usually, the system administrator specifies the default interface multicast datagrams should be sent from. The programmer can override this and choose a concrete outgoing interface for a given socket with this option.

struct in_addr interface_addr;
setsockopt (socket, IPPROTO_IP, IP_MULTICAST_IF, &interface_addr, sizeof(interface_addr));

From now on, all multicast traffic generated in this socket will be output from the interface chosen. To revert to the original behavior and let the kernel choose the outgoing interface based on the system administrator's configuration, it is enough to call setsockopt() with this same option and INADDR_ANY in the interface field.

In determining or selecting outgoing interfaces, the following ioctls might be useful: SIOCGIFADDR (to get an interface's address), SIOCGIFCONF (to get the list of all the interfaces) and SIOCGIFFLAGS (to get an interface's flags and, thus, determine whether the interface is multicast capable or not -the IFF_MULTICAST flag-).

If the host has more than one interface and the IP_MULTICAST_IF option is not set, multicast transmissions are sent from the default interface, although the remaining interfaces might be used for multicast forwarding if the host is acting as a multicast router.

Leroy Scandal
  • 329
  • 1
  • 4