Search code examples
cmulticastopenwrt

multicast receiver software running on openwrt router does not work


I have written a simple multicast receiver program, which is currently running in the OpenWRT router. But this software does not receive any data from the multicast group.

I have a working multicast environment in the router (my wireless devices can send and receive data from the same multicast group), but if I try to listen to that group from the router using my software, I receive nothing.

NOTE: The same software works fine if I run it on the Linux desktop machine.

I did tcpdump on the multicast; 11.11.11.15 and 11.11.11.32 are two known multicast senders in the network:

root@OpenWrt:/# tcpdump -n multicast
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on wlan0, link-type EN10MB (Ethernet), capture size 65535 bytes
14:03:19.478476 IP 11.11.11.15.60472 > 239.0.0.38.12345: UDP, length 50
14:03:24.480007 IP 11.11.11.15.60472 > 239.0.0.38.12345: UDP, length 50
14:03:29.483429 IP 11.11.11.15.60472 > 239.0.0.38.12345: UDP, length 50
14:03:34.479858 IP 11.11.11.15.60472 > 239.0.0.38.12345: UDP, length 50
14:03:39.484171 IP 11.11.11.15.60472 > 239.0.0.38.12345: UDP, length 50
14:03:44.483130 IP 11.11.11.15.60472 > 239.0.0.38.12345: UDP, length 50
14:03:49.486375 IP 11.11.11.15.60472 > 239.0.0.38.12345: UDP, length 50
14:03:54.492302 IP 11.11.11.15.60472 > 239.0.0.38.12345: UDP, length 50
14:03:59.493509 IP 11.11.11.15.60472 > 239.0.0.38.12345: UDP, length 50
14:04:04.495279 IP 11.11.11.15.60472 > 239.0.0.38.12345: UDP, length 50
14:04:08.432849 IP 11.11.11.32.48484 > 239.0.0.38.12345: UDP, length 26
14:04:08.432962 IP 11.11.11.32.48484 > 239.0.0.38.12345: UDP, length 26
14:04:09.423884 IP 11.11.11.32.48484 > 239.0.0.38.12345: UDP, length 26
14:04:09.424013 IP 11.11.11.32.48484 > 239.0.0.38.12345: UDP, length 26

Source code of the multicast receiver.

#define GROUP_PORT      12345
#define GROUP_ADDRESS   "239.0.0.38"

int main(int argc, char *argv[])
{
    struct sockaddr_in addr;
    struct ip_mreq mreq;
    int hndlSocket, nbytes,addrlen;
    char message[32];
    u_int allow=1;

    memset(&addr, 0, sizeof(addr));
    addr.sin_family         = AF_INET;
    addr.sin_addr.s_addr    = htonl(INADDR_ANY);
    addr.sin_port           = htons(GROUP_PORT);
    mreq.imr_multiaddr.s_addr   = inet_addr(GROUP_ADDRESS);
    mreq.imr_interface.s_addr   = htonl(INADDR_ANY);

    if ((hndlSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 
    {
        perror("socket");
    }
    else if (setsockopt(hndlSocket, SOL_SOCKET, SO_REUSEADDR, &allow, sizeof(allow)) < 0) 
    {
        perror("Reusing ADDR failed");
    }
    else if (bind(hndlSocket, (struct sockaddr *) &addr, sizeof(addr)) < 0) 
    {
        perror("bind");
    }
    else if (setsockopt(hndlSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) 
    {
        perror("setsockopt for multi membership");
    }
    else
    {
        /* now just enter a read-print loop */
        while (true)
        {
            addrlen = sizeof(addr);
            if ((nbytes=recvfrom(hndlSocket, message, sizeof(message), 0, (struct sockaddr *) &addr, &addrlen)) < 0) 
            {
                perror("recvfrom"); exit(1);
            }
            puts(message);
        }
    }
}

Solution

  • After adding the below route, it worked like a charm.

    route add -net 224.0.0.0 netmask 224.0.0.0 wlan0