JatiA
JatiA

Reputation: 81

How to send multicast data from a particular IP?

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?

Upvotes: 0

Views: 371

Answers (2)

user9710374
user9710374

Reputation:

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.

Upvotes: 0

user207421
user207421

Reputation: 310884

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().

Upvotes: 1

Related Questions