简体   繁体   中英

Receiving multicast data on RHEL 7 using c++

I am trying to receive multicast UDP data on a network interface on RHEL 7.2

So, about my setup: NIC: Intel X540 IP: 192.168.42.100 Distro: RHEL 7.2 Multicast Address: 224.5.6.7 Port: 2002 Interface name: ens4f1

I have 2 interfaces open, the 1 Gbit on the Mobo and one of the 10 Gbit on the intel card.

Like many other posts, I have data coming in and visible on both wireshark and tcpdump but my recvfrom call just hangs. My code is a copy of a similar problem described here , which appears to be working for the OP.

Notes:

1) I run my code as root

2) I have tried to change the rp_filter in /proc/sys/net/ipv4/conf/ens4f1/rp_filter to 0. No change

3) Disabling SELinux did not change anything

4) Wireshark and tcpdump shows the data just fine. Dump shown below

[@localhost ~]$ sudo tcpdump -c 5 -i ens4f1 -v
tcpdump: listening on ens4f1, link-type EN10MB (Ethernet), capture size 65535 bytes
15:43:57.368470 IP (tos 0x0, ttl 255, id 6526, offset 0, flags [DF], proto UDP (17), length 7996)
    192.168.42.44.62111 > 224.5.6.7.globe: UDP, length 7968
15:43:57.368477 IP (tos 0x0, ttl 255, id 6526, offset 0, flags [DF], proto UDP (17), length 316)
    192.168.42.44.62111 > 224.5.6.7.globe: UDP, length 288
15:43:57.368869 IP (tos 0x0, ttl 255, id 6526, offset 0, flags [DF], proto UDP (17), length 7996)
    192.168.42.44.62111 > 224.5.6.7.globe: UDP, length 7968
15:43:57.368878 IP (tos 0x0, ttl 255, id 6526, offset 0, flags [DF], proto UDP (17), length 316)
    192.168.42.44.62111 > 224.5.6.7.globe: UDP, length 288
15:43:57.369264 IP (tos 0x0, ttl 255, id 6526, offset 0, flags [DF], proto UDP (17), length 7996)
    192.168.42.44.62111 > 224.5.6.7.globe: UDP, length 7968
5 packets captured
46 packets received by filter
9 packets dropped by kernel

Copy of code

#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdio.h>

#include <iostream>
#include <string>

using namespace std;

#define HELLO_PORT 2002
#define HELLO_GROUP "224.5.6.7"
#define MSGBUFSIZE 10000

int main(int argc, char *argv[])
{
    string source_iface("192.168.42.100");
    string group(HELLO_GROUP);
    int port(HELLO_PORT);

    cout << "group: " << group << " port: " << port << " source_iface: " << source_iface << endl;

    int fd;
    if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket");
        exit(1);
    }

    u_int yes = 1;
    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
    {
        perror("Reusing ADDR failed");
        exit(1);
    }

    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));

    addr.sin_family = AF_INET;

    addr.sin_port = htons(port);
    addr.sin_addr.s_addr =  (group.empty() ?
                            htonl(INADDR_ANY) :
                            inet_addr(group.c_str()));

    if (bind(fd,(struct sockaddr *)&addr, sizeof(addr)) < 0)
    {
        perror("bind");
        exit(1);
    }

    struct ip_mreq mreq;
    mreq.imr_multiaddr.s_addr = inet_addr(group.c_str());
    mreq.imr_interface.s_addr = (source_iface.empty() ? htonl(INADDR_ANY) : inet_addr(source_iface.c_str()));

    if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
    {
        perror("setsockopt");
        exit(1);
    }

    socklen_t addrlen;
    int nbytes;
    char msgbuf[MSGBUFSIZE];

    while (1)
    {
        memset(&msgbuf, 0, MSGBUFSIZE);

        addrlen = sizeof(addr);
        if ((nbytes = recvfrom(fd, msgbuf, MSGBUFSIZE, 0, (struct sockaddr *)&addr, &addrlen)) < 0)
        {
            perror("recvfrom");
            exit(1);
        }
        cout.write(msgbuf, nbytes);
        cout.flush();
    }

    return 0;
}

All help and suggestions are most welcome

Thanks

Henrik

It was an issue with the firewalld in RHEL 7. Creating a rule described here https://access.redhat.com/solutions/1587673 solved it for one interface, but not both. Will repost another question for that if I don't solve it.

EDIT: The culprit for getting both running at the same time was setting the rp_filter value to 2 for all interfaces. Setting it for only 1 does not do anything if the 'all' category is still 1.

    net.ipv4.conf.all.rp_filter=2

This can be done by creating a file at /etc/sysctl.d/multicast.conf or using a "sysctl -w" call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM