Need IPv6 Multicast C code that works on iOS 9 - c

Apple now requires iOS 9 apps to be IPv6 compliant. We're mostly OK, except for a bit of code which sends a UDP broadcast - this now fails in iOS 9.
Everything I read tells me that UDP multicast is the right way to do this in IPv6. I've found some example code, but it doesn't work on any version of iOS or Mac OS X I've tried.
This code is being called from a C/C++ lib inside our program - difficult to make a callback into Swift, Obj-C, Java, etc. And this code will be shared by a Mac OS X and Android version of our app. One would think it's possible to do IPv6 multicast in C in any POSIX environment!
In the sample below, execution succeeds up to the final sendto() call, which actually sends the UDP message. That sendto() fails, with errno set to EBROKENPIPE (22) after the failure.
My best guess is that I'm missing some required setsockopt() call, or am using the wrong multicast address. Right now, I'm stumped.
Here's the function call I'm making (to multicast "Is anybody out there?" on UDP port 4031):
char *msg = "Is anybody out there?";
err = multicast_udp_msg ( "FF01::1111", 4031, msg, strlen(msg) );
Here's the code that is being called:
// Multicasts a message on a specific UDP port.
// myhost - IPv6 address on which to multicast the message (i.e., ourself)
// port - UDP port on which to broadcast the mssage
// msg - message contents to broadcast
// msgsize - length of message in bytes
// Return value is zero if successful, or nonzero on error.
int multicast_udp_msg ( char *myhost, short port, char *msg, size_t msgsize )
{
int sockfd, n;
char service[16] = { 0 };
int err = 0;
struct addrinfo hints = { 0 }, *res, *ressave;
struct sockaddr_storage addr = { 0 };
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_DGRAM;
sprintf ( service, "%hd", port );
n = getaddrinfo ( myhost, service, &hints, &res );
if ( n < 0 )
{
fprintf(stderr, "getaddrinfo error:: [%s]\n", gai_strerror(n));
return -1;
}
ressave = res;
sockfd = socket ( res->ai_family, res->ai_socktype, res->ai_protocol );
if ( sockfd >= 0 )
{
memcpy ( &addr, res->ai_addr, sizeof ( addr ) );
if ( joinGroup ( sockfd, 0, 8, &addr ) == 0 )
if ( bind ( sockfd, res->ai_addr, res->ai_addrlen ) == 0 )
if ( sendto ( sockfd, msg, msgsize, 0, (struct sockaddr *) &addr, sizeof ( addr ) ) < 0 )
err = errno;
close ( sockfd );
res = res->ai_next;
}
freeaddrinfo ( ressave );
return err;
}
int
joinGroup(int sockfd, int loopBack, int mcastTTL,
struct sockaddr_storage *addr)
{
int r1, r2, r3, retval;
retval=-1;
switch (addr->ss_family) {
case AF_INET: {
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr=
((struct sockaddr_in *)addr)->sin_addr.s_addr;
mreq.imr_interface.s_addr= INADDR_ANY;
r1= setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_LOOP,
&loopBack, sizeof(loopBack));
if (r1<0)
perror("joinGroup:: IP_MULTICAST_LOOP:: ");
r2= setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL,
&mcastTTL, sizeof(mcastTTL));
if (r2<0)
perror("joinGroup:: IP_MULTICAST_TTL:: ");
r3= setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(const void *)&mreq, sizeof(mreq));
if (r3<0)
perror("joinGroup:: IP_ADD_MEMBERSHIP:: ");
} break;
case AF_INET6: {
struct ipv6_mreq mreq6;
memcpy(&mreq6.ipv6mr_multiaddr,
&(((struct sockaddr_in6 *)addr)->sin6_addr),
sizeof(struct in6_addr));
mreq6.ipv6mr_interface= 0; // cualquier interfaz
r1= setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
&loopBack, sizeof(loopBack));
if (r1<0)
perror("joinGroup:: IPV6_MULTICAST_LOOP:: ");
r2= setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
&mcastTTL, sizeof(mcastTTL));
if (r2<0)
perror("joinGroup:: IPV6_MULTICAST_HOPS:: ");
r3= setsockopt(sockfd, IPPROTO_IPV6,
IPV6_JOIN_GROUP, &mreq6, sizeof(mreq6));
if (r3<0)
perror("joinGroup:: IPV6_ADD_MEMBERSHIP:: ");
} break;
default:
r1=r2=r3=-1;
}
if ((r1>=0) && (r2>=0) && (r3>=0))
retval=0;
return retval;
}
Thoughts welcome!
-Tim

After some back-and-forth with Apple, and some additional context, we have an answer. But it's not the answer to my original question. First, here's the Apple thread for context:
https://forums.developer.apple.com/message/71107
It turns out that IPv6 multicast was not actually what we needed to solve the real problem at hand - namely, finding a legacy embedded device on a local Wi-Fi network. We really had to use IPv4 UDP broadcast to do that. Our embedded device ignores IPv6 multicast packets like the Earth ignores neutrinos flying through it.
Apple gave us a setsockopt() call which enabled IPv4 UDP broadcast to work in iOS 9 on an infrastructure Wi-Fi network. That is the intended use case for this feature. And Apple also gave us a likely cause of failure when that broadcast failed to work in an Ad Hoc Wi-Fi network (that appears to be a known iOS 9 issue).
So, although my original question is not answered here, the underlying issue has been resolved.

Related

Reading UDP packets with several clients

I have an application installed locally (not developed by me), which broadcasts UDP packets every second.
Reading the packets from my application (developed in C++ in Windows) which also is locally installed, works fine.
WSADATA data;
WORD version = MAKEWORD(2, 2);
int wsOK = WSAStartup(version, &data);
SOCKET serverIn = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
sockaddr_in serverHint;
serverHint.sin_addr.S_un.S_addr = INADDR_ANY;
serverHint.sin_family = AF_INET;
serverHint.sin_port = htons(UDP_RECEIVE_PORT);
bind(serverIn, (sockaddr*)&serverHint, sizeof(serverHint));
sockaddr_in client;
int clientSize = sizeof(client);
int RECIEVE_BUFFER_SIZE = 65507;
char* recieveBuffer = new char[RECIEVE_BUFFER_SIZE];
while(updating)
{
int bytesIn = recvfrom(serverIn, recieveBuffer, RECIEVE_BUFFER_SIZE, 0, (sockaddr*)&client, &clientSize);
}
closesocket(serverIn);
WSACleanup();
But I recently noticed while I was testing some code, while my app was running, that the bind(...)
function returned an error code of 10048 (WSAEADDRINUSE). Hence, it seems the first client bound to listen for the UDP packets is the only one who can listen, and the other clients is unable to read the broadcasted UDP packets.
So then I added the SO_REUSEADDR option before calling the bind(...) function to be able to bind successfully to the socket:
BOOL bOptVal = TRUE;
int bOptLen = sizeof(BOOL);
setsockopt((SOCKET)serverIn, SOL_SOCKET, SO_REUSEADDR, (char*)&bOptVal, bOptLen);
That works, but the recvfrom(...) function then does not recieve any data at all! I guess it waits for the other client to close its socket.
Next solution is to initialize the socket with SOCK_RAW instead.
The above option SO_REUSEADDR is now not needed, and remove it:
SOCKET serverIn = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
This works, I can read the data now! Though, Windows now requires the adminstrator rights for my application. Also I do recieve the UDP information in the data which I do not need.
Is there any better method to do this without requiring administrator rights, any possibility to discard the header information in the buffer?
Below is a little program I wrote to demonstrate that IPv4 UDP broadcast can and does work as expected under Windows (i.e. without requiring raw-sockets or Administrator privileges).
Run it with the command line argument "server" and it will send out one broadcast UDP packet per second.
Then also run several more instances of the same program, with no command line arguments, to receive the UDP packets and print a line of text to stdout whenever they do. The expected behavior should look like this:
As for why it's not working for you -- one possible guess is that your UDP-packet-sending program is actually sending out unicast UDP packets rather than broadcast. If that's the case, then I would expect that only one client program would receive packets (even if multiple clients are bound to the same port). A network trace tool like Wireshark might be able to help you determine if the UDP packets being sent are broadcast or unicast.
Anyway, here's the code:
#include <stdio.h>
#include <ws2tcpip.h>
#pragma comment(lib,"WS2_32")
static int BindUDPSocket(SOCKET sock, unsigned short port, bool allowPortSharing)
{
if (sock == INVALID_SOCKET) return -1;
if (allowPortSharing)
{
const BOOL trueValue = true;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char *) &trueValue, sizeof(trueValue)) < 0) return -1;
}
struct sockaddr_in bindAddr; memset(&bindAddr, 0, sizeof(bindAddr));
bindAddr.sin_family = AF_INET;
bindAddr.sin_addr.s_addr = INADDR_ANY; // aka 0.0.0.0
bindAddr.sin_port = htons(port);
return bind(sock, (struct sockaddr *) &bindAddr, sizeof(bindAddr));
}
int main(int argc, char ** argv)
{
WSADATA data;
WORD version = MAKEWORD(2, 2);
(void) WSAStartup(version, &data);
const unsigned short TEST_PORT = 12345;
SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock<0) {printf("socket() failed\n"); exit(10);}
if ((argc > 1)&&(strcmp(argv[1], "server") == 0))
{
if (BindUDPSocket(sock, 0, false)<0) {printf("BindUDPSocket() failed for server\n"); exit(10);}
const BOOL allowBroadcast = true;
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (const char *) &allowBroadcast, sizeof(allowBroadcast)) < 0)
{
printf("setsockopt(SO_BROADCAST) failed\n");
exit(10);
}
const char buf[] = {0x01, 0x02, 0x03, 0x04}; // dummy data
struct sockaddr_in toAddr; memset(&toAddr, 0, sizeof(toAddr));
toAddr.sin_family = AF_INET;
toAddr.sin_addr.s_addr = INADDR_BROADCAST; // aka 255.255.255.255
toAddr.sin_port = htons(TEST_PORT);
printf("Sending outgoing broadcast UDP sockets on port %u, once per second\n", TEST_PORT);
while(true)
{
if (sendto(sock, buf, sizeof(buf), 0, (const sockaddr *) &toAddr, sizeof(toAddr)) == sizeof(buf))
{
printf("Sent %zu bytes of broadcast UDP data\n", sizeof(buf));
}
else printf("sendto() failed!\n");
Sleep(1000); // wait 1 second
}
}
else
{
if (BindUDPSocket(sock, TEST_PORT, true)<0) {printf("BindUDPSocket() failed for client\n"); exit(10);}
printf("Waiting to receive incoming broadcast UDP sockets on port %u\n", TEST_PORT);
while(true)
{
char buf[1024];
const int ret = recv(sock, buf, sizeof(buf), 0L);
printf("Received %i bytes of incoming UDP data\n", ret);
}
}
}

sendto() does not generate error if destination does not exist

I am using sendto() function in C. I have set the destination address and dest port. While sending UDP frames I can see the frames in Wireshark and the number of packet Wireshark shows are exactly as I have defined in my program.
The problem is even though the destination address is not reachable the frames are being sent and I can see it in Wireshark.
Should not the sendto() function generates a error if the destination IP is not existing?
if (sendto(sockfd, &buffer[i], UDP_FRAME, 0,
(const struct sockaddr*)&server, sizeof(server)) < 0)
{
fprintf(stderr, "Error in sendto()\n");
//return EXIT_FAILURE;
}
Dest. IP: 234.168.0.1
Dest port: 80 or 9 (discard protocol)
#define PORT (80)
#define FRAMES (20000)
#define UDP_FRAME (1442)
#define SERVERADDRESS "234.168.0.1"
#define BUFFER_SIZE (FRAMES * UDP_FRAME)
char buffer[BUFFER_SIZE];
int main(int argc, char **argv)
{
struct timespec start, end, loop_start, loop_end;
int sockfd, count_frame = 0, frames_total, i = UDP_FRAME, n=1;
struct sockaddr_in server;
printf("Build Data...\n");
build(buffer, sizeof(buffer));
printf("Configure socket...\n");
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
{
fprintf(stderr, "Error opening socket");
return EXIT_FAILURE;
}
/*----------------------------------------------------*/
/*--- Initialize address protocol ---*/
/*----------------------------------------------------*/
bzero((char*)&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(SERVERADDRESS);
server.sin_port = htons(PORT);
/*---------------------------------------------------*/
/*--- S E N D I N G D A T A --*/
/*---------------------------------------------------*/
printf("\nSend UDP data...\n\n");
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
clock_gettime(CLOCK_MONOTONIC_RAW, &loop_start);
frames_total = 0;
for (int i = 0; i < BUFFER_SIZE; i += UDP_FRAME) {
//while(1) {
if (sendto(sockfd, &buffer[i], UDP_FRAME, 0,
(const struct sockaddr*)&server, sizeof(server)) < 0)
{
fprintf(stderr, "Error in sendto()\n");
//return EXIT_FAILURE;
}
count_frame += 1;
clock_gettime(CLOCK_MONOTONIC_RAW, &loop_end);
if ((loop_end.tv_nsec - loop_start.tv_nsec) > 5000000) {
printf("\nCount [%d] ... ", n);
printf("Fames sent: %d\n", count_frame);
frames_total += count_frame;
n+=1;
count_frame = 0;
clock_gettime(CLOCK_MONOTONIC_RAW, &loop_start);
}
}
printf("Total successful counted frames: %d \n", frames_total);
return EXIT_SUCCESS;
}
UDP is an unreliable protocol. A call to sendto is successful once the packet leaves the interface. After that, whether it gets to its destination or not is up to the network.
Even if the network supports ICMP messages stating that the host or port is not reachable, it won't matter in your particular case because you're sending to a multicast address. If you have at least one multicast-capable interface, the system will pick one to send the packet over. It could be received by multiple (or no) hosts. So it doesn't make sense to say that the destination is not reachable.
sendto() will give you an error if the host doesn't know a route to the host (which is almost never the case, since your host will have a default gateway). Otherwise, you might (or might not) receive an ICMP destination unreachable message if your packet did not reach the targeted application, but this is unreliable and won't be communicated by the call to sendto().
What you can do is to query the socket with
struct sock_extended_err err;
socklen_t errlen = sizeof(err);
getsockopt(fd, SOL_IP, IP_RECVERR, &err, &errlen);
for received errors, which will give you detailed information about received errors on the socket (i.e. ICMP port unreachable, ICMP host unreachable, etc. pp). This can help, but as I said, it is not realiable, since ICMP messages are often strictly rate limited, filtered on the way or not sent at all, if your packet is blocked by a packet filter (firewall).

getaddrinfo returns 2 results with localhost

I am trying to use the function getaddrinfo with a passive socket/listener instead of filling directly the old sockaddr structure.
My purpose is to open a socket and bind it to a port. Essentially I am building a server.
I don't know why but getaddrinfo returns 2 results; because of this in the loop below the second time I call bind it exit with an error saying "address already in use".
Do you know how to fix this and if it is correct to have it in a loop ?
struct addrinfo addr;
memset(&addr,0,sizeof(addrinfo));
addr.ai_family = AF_INET;
addr.ai_socktype = SOCK_STREAM;
addr.ai_protocol = 0;
addr.ai_flags = AI_PASSIVE;
struct addrinfo * rp,* result;
getaddrinfo( "localhost","59001",&addr,&result );
for( rp = result; rp != NULL; rp = rp->ai_next ){
int sd = socket( AF_INET, SOCK_STREAM, 0 );
if(-1==sd ){
printf( "error creating the socket %m");
return -1;
}
if( -1==bind( sd, result->ai_addr, result->ai_addrlen ) ){
printf( "error binding %m")
return -1;
}
if( -1== listen(sd, MAX_BACKLOG ) ){
std::cerr << "listen didn't work" << std::endl;
return -1;
}
}
There are a couple of things going on here. For the multiple results from getaddrinfo(), that's because it returns an IPv4 address and a IPv6 address (you have both). As for the "address already in use", you need to set the SO_REUSEADDR option. Put this between your calls to socket() and bind():
int opval = 1;
setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
Is this on a redhat machine perchance? There is a wellknown bug on those, that because /etc/hosts/ lists localhost twice, when you request specifically AF_INET you actually get the same result twice; once for IPv4 and once for IPv6 translated a bit.
See also https://bugzilla.redhat.com/show_bug.cgi?id=496300

UDP Broadcast in Windows 7 - does it work?

I'm trying to write some code under Windows 7 to broadcast across my local network and can't get the following code to work. I come from a Linux background so apologies for style - the full code compiles etc and works and if I use an address of:
unsigned long broadcastAddr = inet_addr("192.168.10.0") | ~(inet_addr("255.255.240.0"));
Then that works fine, I just would really like to use the preferred INADDR_BROADCAST/255.255.255.255 method.
<snip>
SOCKET sockfd;
int broadcast = 1;
WSADATA wsaData; // Windows socket
// Initialize Winsock
if (WSAStartup(MAKEWORD(2,2), &wsaData) == SOCKET_ERROR) {
perror("WinSock Error");
getc(stdin);
exit(EXIT_FAILURE);
}
if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
perror("Socket Error");
getc(stdin);
exit(1);
}
if ((setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast, sizeof(broadcast))) == SOCKET_ERROR) {
perror("Setsockopt - SOL_SOCKET");
getc(stdin);
exit(1);
}
struct sockaddr_in recvaddr;
recvaddr.sin_family = AF_INET;
recvaddr.sin_port = htons(PORT);
recvaddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
memset(recvaddr.sin_zero,'\0', sizeof(recvaddr.sin_zero));
int numbytes = 0;
while ((numbytes = sendto(sockfd, greet, strlen(greet) , MSG_DONTROUTE, (struct sockaddr *)&recvaddr, sizeof(struct sockaddr_in))) != -1) {
printf("Sent a packet %d\n", numbytes);
Sleep(100);
}
There is a huge bug in Windows 7 for UDP broadcast which makes broadcasting on 255.255.255.255 not work on most windows 7 install: https://serverfault.com/questions/72112/how-to-alter-the-global-broadcast-address-255-255-255-255-behavior-on-windows
Basically it will send the broadcast only on a single network interface, which could be anything, even something like a VM network interface or bluetooth one, which can end up not broadcasting to any device.
Unless my bit maths is out, inet_addr("192.168.10.0") | ~(inet_addr("255.255.240.0")) is the same as inet_addr("192.168.15.255") which is the broadcast address for that subnet.
It looks to me like the most likely possibility is not that the sending code is wrong but that the receiving code is wrong. What address have you bound the receiving socket to? What subnet is it on?

Error "No such device" in call setsockopt when joining multicast group

I have a code in which send multicast datagrams.
A critical piece of code:
uint32_t port;
int sockfd, err_ip;
const uint32_t sizebuff = 65535 - (20 + 8);
unsigned char *buff = (unsigned char *) malloc(sizebuff);
struct sockaddr_in servaddr, cliaddr;
struct in_addr serv_in_addr;
struct ip_mreq req;
port = str2uint16(cmdsrv->ipport);
bzero(buff, (size_t)sizebuff);
bzero(&servaddr, sizeof(servaddr));
bzero(&serv_in_addr, sizeof(serv_in_addr));
err_ip = inet_aton(cmdsrv->ipaddr, &serv_in_addr);
if(( err_ip != 0 ) && ( port != 0 )) {
servaddr.sin_family = AF_INET;
servaddr.sin_addr = serv_in_addr;
servaddr.sin_port = htons(port);
memcpy(&req.imr_multiaddr,&serv_in_addr,sizeof(req.imr_multiaddr));
req.imr_interface.s_addr = INADDR_ANY;
sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if( sockfd == -1 ) {
int outerror = errno;
char *retstr = "Couldn't open socket\n";
pthread_exit(retstr);
}
else {
struct in_addr ifaddr;
ifaddr.s_addr = INADDR_ANY;
int optres3 =
setsockopt( sockfd, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr,
sizeof( ifaddr ));
if( optres3 == -1 ) {
int perrno = errno;
char *retstr = "Can't set IP_MULTICAST_IF for socket\n";
printf( "Error setsockopt: ERRNO = %s\n", strerror( perrno ));
printf( "%s",retstr );
pthread_exit(retstr);
}
unsigned char ttl = 32;
int optres2 =
setsockopt( sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
sizeof( ttl ));
if( optres2 == -1 ) {
int perrno = errno;
char *retstr = "Can't set IP_MULTICAST_TTL for socket\n";
printf("Error setsockopt: ERRNO = %s\n",strerror(perrno));
printf("%s",retstr);
pthread_exit(retstr);
}
int optres =
setsockopt( sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &req,
sizeof( req ));
if( optres == -1 ) {
int perrno = errno;
char *retstr = "Can't join to multicast-group\n";
printf("Error setsockopt: ERRNO = %s\n",strerror(perrno));
printf("%s",retstr);
pthread_exit(retstr);
}
// Bind port with socket
uint16_t cliport;
cliaddr.sin_family = AF_INET;
cliaddr.sin_addr.s_addr = INADDR_ANY;
if( strcmp( cmdsrv->ipport, "16011" ) == 0 ) {
cliport = str2uint16("16003");
cliaddr.sin_port = htons(cliport);
}
else if( strcmp( cmdsrv->ipport, "16012" ) == 0 ) {
cliport = str2uint16("16004");
cliaddr.sin_port = htons(cliport);
}
else {
printf("Device hasn't such port");
pthread_exit(NULL);
}
int bindres =
bind( sockfd, (struct sockaddr*)&cliaddr, sizeof( cliaddr ));
if( bindres == -1 ) {
int perrno = errno;
perror("Error in bind\n");
}
// ADD 1 BYTE
data rawdata;
rawdata.desc = 23;
printf( "SIZEOF = %d\n", sizeof( *( cmdsrv->cmd )));
memcpy( &rawdata.cmd, cmdsrv->cmd, sizeof( *( cmdsrv->cmd )));
printf( "RAWDATA: desc = %d, cmd = %d\n", rawdata.desc, rawdata.cmd );
int outerror = 0;
printf( "Send command to IP:\n addr = %s, port = %d\n",
inet_ntoa( servaddr.sin_addr ), ntohs( servaddr.sin_port ));
int size = sendto( sockfd, &rawdata, sizeof( rawdata ), 0,
(struct sockaddr*)&servaddr, sizeof( servaddr ));
if( size == -1 ) {
perror("Can't send command to socket");
}
...
Sometimes program executes successfully (at this moment I have IP - 192.168.80.122). I can capture my multicast datagram by wireshark. That's all OK.
But if I change my IP to 192.168.1.2, I get error when is called
int optres =
setsockopt( sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &req,
sizeof( req ));
And I can't even capture my multicast packet. Nothing is sent.
Where's bug?
If it works for one IP but not for another, maybe this can help.
What does "IP_ADD_MEMBERSHIP: No such device" mean?
It means that the tool is trying to use multicast but the network interface doesn't support it There are two likely causes:
Your machine doesn't have multicast support enabled. For example, on Linux and FreeBSD it is possible to compile a kernel which doesn't support multicast.
You don't have a route for multicast traffic. Some systems don't add this by default, and you need to run. route add -net 224.0.0.0 netmask 224.0.0.0 eth0 (or similar). If you wish to use RAT in unicast mode only, it is possible to add the multicast route on the loopback interface.
IP_ADD_MEMBERSHIP and bind() are only required for receiving multicast, use IP_MULTICAST_IF instead for effectively a "send-only membership" of a multicast group.
IP_MULTICAST_IF sets the kernel to send multicast packets for a given group on a given interface, it is effectively "send-only" as you will not be able to receive traffic on that group after setting. This varies by platform: Posix platforms generally function this way as an optimisation, whilst Win32 will perform software level routing to propagate locally generated packets.
You might not use the interface address as INADDR_ANY.
req.imr_interface.s_addr = INADDR_ANY;
check where the loopback is running using ifconfig.
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
You need to use interface address as inet addr i.e. 127.0.0.1
Check your loopback interface address and use that address only.

Resources