Hello misc@!
My goal is send few multicast datagrams via "non-primary network interface"
at multihomed host without affecting system wide defaults. After reading
man 4 ip:
---
For hosts with multiple interfaces, each multicast transmission is sent
from the primary network interface. The IP_MULTICAST_IF option
overrides
the default for subsequent transmissions from a given socket:
struct in_addr addr;
setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr));
where addr is the local IP address of the desired interface or
INADDR_ANY
to specify the default interface. An interface's local IP address and
multicast capability can be obtained via the SIOCGIFCONF and
SIOCGIFFLAGS
ioctl(2)'s. Normal applications should not need to use this option.
---
I wrote this simple code:
---
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <err.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define IFACE_ADDR "192.168.2.1"
#define MCAST_ADDR "239.1.2.3"
#define MCAST_PORT 1234
int main(void)
{
struct sockaddr_in ifaddr, mcaddr;
int sock, on;
char msg[] = "test test test";
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
err(1, "socket");
on = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on) ==
-1)
err(1, "setsockopt(SO_REUSEADDR)");
ifaddr.sin_addr.s_addr = inet_addr(IFACE_ADDR);
if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF,
&ifaddr.sin_addr.s_addr, sizeof(ifaddr.sin_addr.s_addr)) == -1)
err(1, "setsockopt(IP_MULTICAST_IF)");
memset(&mcaddr, 0, sizeof mcaddr);
mcaddr.sin_family = AF_INET;
mcaddr.sin_addr.s_addr = inet_addr(MCAST_ADDR);
mcaddr.sin_port = (unsigned short)htons(MCAST_PORT);
if (sendto(sock, msg, sizeof msg, 0,
(struct sockaddr *)&mcaddr, sizeof mcaddr) == -1)
err(1, "sendto");
close(sock);
return (0);
}
--
But this code allways give "sendto: No route to host" error.
Bad code or I miss something or misunderstood manual page?
--
/unk