openwrt 路由器上的多播接收器软件 运行 不工作

multicast receiver software running on openwrt router does not work

我写了一个简单的多播接收程序,目前运行正在OpenWRT路由器中。但是这个软件收不到多播组的任何数据。

我在路由器中有一个有效的多播环境(我的无线设备可以从同一个多播组发送和接收数据),但是如果我尝试使用我的软件从路由器收听该组,我什么也收不到。

注意:如果我 运行 在 Linux 桌面计算机上使用相同的软件,它可以正常工作。

我在多播上做了tcpdump; 11.11.11.15 和 11.11.11.32 是网络中两个已知的多播发送者:

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

多播接收器的源代码。

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

添加以下路由后,效果非常好。

route add -net 224.0.0.0 netmask 224.0.0.0 wlan0