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.
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;
}