3

I am writing a small program that sends and receive multicast packets.I need to set the outgoing interface with its name (e.g. eth0) rather than its address. Therefore I have to use struct ip_mreqn (rather than struct in_addr) so that I could use its imr_ifindex field to set the interface index (which I can get using interface's name).

However for some reason it doesn't work. The call to setsockopt() works fine but the following call to sendto() returns "Invalid argument" error. Of course the error disappears if I replace ip_mreqn with in_addr and use interface's address instead.

Following is my code:

sd = socket(AF_INET, SOCK_DGRAM, 0);

struct ip_mreqn addr;
addr.imr_ifindex = if_nametoindex("eth0");
setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr);

struct sockaddr_in sock_addr;
memset((char *) &sock_addr, 0, sizeof(sock_addr));
sock_addr.sin_family = AF_INET;
sock_addr.sin_port = destination_port;
sock_addr.sin_addr.s_addr = destination_address;

char msg[] = "abc";

while (sendto(sd, msg, sizeof(msg), 0, reinterpret_cast<const sockaddr*>(&sock_addr),
  sizeof(sock_addr)) < 0)
    throw std::runtime_error(strerror(errno));

Is there any problem in using struct ip_mreqn when setting IP_MULTICAST_IF? Has anyone got any idea? Really appreciate help. Thanks.

Note that destination port and address are already in network byte order.

Jahanzeb Farooq
  • 1,948
  • 4
  • 27
  • 27
  • Are you confident about the requirement to use an interface name rather than IP address? An interface can have thousands of IP addresses... – sarnold Jun 12 '12 at 09:36
  • Could the fact that it has got more than one IP address cause this problem? In that case shouldn't the kernel just choose the first address? At least that's what I was assuming. – Jahanzeb Farooq Jun 12 '12 at 10:24
  • How do you know it works "fine"? Check the return value of `setsockopt`. Does `if_nametoindex` return sane value? – Nikolai Fetissov Jun 13 '12 at 16:40

2 Answers2

4

From tldp's multicast howto:

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));

Just an index to an interface doesn't suffice according to them.

Edit:
You're maybe confusing IP_MULTICAST_IF with IPV6_MULTICAST_IF, the latter takes an unsigned integer to denote the interface number (see man 7 ipv6).

Edit:
As it turns out the example given works as expected. Here's my full test case:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/socket.h>


int main()
{
    int sd = socket(AF_INET, SOCK_DGRAM, 0);
    struct sockaddr_in sa = {0};
    struct ip_mreqn addr = {{0}};
    char msg[] = "abc";

    addr.imr_ifindex = if_nametoindex("eth0");
    setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr));

    sa.sin_family = AF_INET;
    sa.sin_port = 8653;

    inet_pton(AF_INET, "224.255.255.5", &sa.sin_addr);
    if (sendto(sd, msg, sizeof(msg), 0, (void*)&sa, sizeof(sa)) < 0) {
        perror("bugger");
        return 1;
    }
    return 0;
}
hroptatyr
  • 4,702
  • 1
  • 35
  • 38
  • [man 7 ip](http://www.kernel.org/doc/man-pages/online/pages/man7/ip.7.html) says that setsockopt takes ip_mreqn as argument in the case of IP_MULTICAST_IF. And looking into kernel code [here](http://git.kernel.org/?p=linux/kernel/git/stable/linux-stable.git;a=blob_plain;f=net/ipv4/ip_sockglue.c;hb=HEAD) I can see that it should work fine even if I specify only the index, as it first tries to use index to retrieve the device. – Jahanzeb Farooq Jun 13 '12 at 14:38
  • 1
    Indeed, and now that I've actually run your example code, it works just the way you did it. – hroptatyr Jun 13 '12 at 14:54
  • Yeah, I'm surprised that your code snippet actually works. That means I have something else somewhere which is causing the error. Going to dig it, will be back. Thanks a lot. – Jahanzeb Farooq Jun 13 '12 at 16:10
  • Got it. It happened because I never initialized ip_mreqn which resulted in garbage in the rest of the two fields (imr_multiaddr and imr_address). And eventually down somewhere it encountered the error when it tried to process the fields as IP addresses and thus "invalid argument". Silly mistake, learned a lesson. Thanks a lot for your help. – Jahanzeb Farooq Jun 14 '12 at 07:52
  • It indirectly answered my question hence accepting it as answer. – Jahanzeb Farooq Jun 14 '12 at 07:55
0

In the code example of the answer, don't forget to htons() the destination port in .sin_port