多网卡的情况下收udp组播数据的坑

多网卡指定其中一个网卡收udp组播的三个关键(缺一不可):

  1. 接收udp组播的网卡的ip,必须设置成抓包看到的组播发送ip的同一网段的ip.
  2. 代码里本地监听地址ip必须是0.0.0.0, 端口是组播端口.(如下代码搜搜"关键")
  3. 代码加入组播的set里需要指定收组播的网卡ip. (如下代码搜搜"关键")
    此外也需要看iptables和防火墙有没有限制。。。

1.看防火墙有没有关闭:
$ sudo ufw status verbose
Status: inactive # <- inactive说明防火墙关了。good。

2. 看系统有没有过滤组播包:
2.1 看接受组播的网卡是否过滤了:
cat /proc/sys/net/ipv4/conf/en4/rp_filter
如果是0, good。
2.2 看all网卡是否过滤了:
cat /proc/sys/net/ipv4/conf/all/rp_filter
如果是0, good。
这两个值都必须是0,才行!如果不是0,这样修改:
2.3 临时修改取消过滤:
sudo sysctl -w net.ipv4.conf.en4.rp_filter=0
sudo sysctl -w net.ipv4.conf.all.rp_filter=0
2.4 永久修改取消过滤(重启亦有效):
sudo vi /etc/sysctl.conf
改为:
net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.all.rp_filter=0

收组播的C代码:

// filename: recver.cc
// it's a demo to receive multicast udp data with the one of multi net interfaces....
 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
using namespace std;
 
int main(void)
{
    const char* group_ip = "230.13.0.2"; // 组播地址
    int group_port = 3456;              // 组播的端口
 
    char recmsg[256];
 
    int n;
    int socket_fd;
    struct sockaddr_in group_addr;      //group address
    struct sockaddr_in local_addr;      //local address
    struct ip_mreq mreq;
    socklen_t addr_len = sizeof(group_addr);
    u_int yes;
 
    socket_fd=socket(AF_INET,SOCK_DGRAM,0);
    if(socket_fd < 0)
    {
        perror("socket multicast!");
        exit(1);
    }
 
    /*set up the local address*/
    memset(&local_addr,0,sizeof(local_addr));
    local_addr.sin_family = AF_INET;
    local_addr.sin_addr.s_addr = inet_addr("0.0.0.0");   // 设定本地监听必须是0.0.0.0 这里是关键!
    local_addr.sin_port = htons(group_port);             //this port must be the group port
 
    // 设置端口复用
    if (setsockopt(socket_fd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0)
    {
        perror("Reusing ADDR failed");
        exit(1);
    }
 
    /*bind local address*/
    if(bind(socket_fd,(struct sockaddr *)&local_addr,sizeof(local_addr)) == -1)
    {
        perror("Binding the multicast!");
        exit(1);
    }
    
    /*use the setsocketopt() to request joining the multicast group*/
    mreq.imr_multiaddr.s_addr=inet_addr(group_ip);
    //mreq.imr_interface.s_addr=htonl(INADDR_ANY);
    mreq.imr_interface.s_addr=inet_addr("180.0.0.109"); // 设定用新网卡来接收组播. 这里是关键!
    if (setsockopt(socket_fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq)) < 0)
    {   /* fuck boost: udp_socket.set_option(boost::asio::ip::multicast::join_group(boost::asio::ip::address_v4::from_string(group_ip),boost::asio::ip::address_v4::from_string("180.0.0.109"))); */
        perror("setsockopt multicast!");
        exit(1);
    }
 
    /*set up the destination address*/
    memset(&group_addr,0,sizeof(struct sockaddr_in));
    group_addr.sin_family = AF_INET;
    group_addr.sin_port = htons(group_port);
    group_addr.sin_addr.s_addr = inet_addr(group_ip);
 
    /*loop to send or recieve*/
    while(1)
    {
 
       bzero(recmsg, sizeof(recmsg)-1);
       n = recvfrom(socket_fd, recmsg, sizeof(recmsg)-1, 0,
               (struct sockaddr *) &group_addr, &addr_len);
       if (n < 0) {
           printf("recvfrom err in udptalk!\n");
           exit(4);
       } else {
           /* success recieve the information */
           recmsg[n] = 0;
           printf("peer:%s \n", recmsg);
       }
 
    }
 
    return 0;
}

抓包命令:

tcpdump -i en4 host 230.13.0.2 and port 3456 and udp -w ./udp.cap
用wireshark打开udp.cap,可以看到组播数据。或者直接在控制台观看:
tcpdump -vv -i en4 host 230.13.0.2
但是这样控制台可能看不到src的ip,所以还是建议抓包存下来用wireshark打开,如下图
多网卡的情况下收udp组播数据的坑_第1张图片
修改网卡的ip(改为抓包看到的src的ip同网段):

  1. (ubuntu16.04)修改sudo vi /etc/network/interfaces
    auto en4
    iface en4 inet static
    address 180.0.0.109
    netmask 255.255.255.0

  2. 重启这个网卡:
    sudo ifdown en4
    sudo ifup en4

  3. 如果ip没改过来,再:
    sudo ifconfig en4 180.0.0.109

udp_socket.set_option(boost::asio::ip::multicast::join_group(multicast_interface, network_interface));

你可能感兴趣的:(udp,网络协议,网络)