How to confirm multicast packets flow to specified network interface with python socket?

24 Views Asked by At

My Os is Linux, CentOS7.

I want to write a multicast sender that sends UDP packets periodly, and want to confirm the UDP packets flow to specified network interface. In my system, I have 6 interface, 4 of them are normal electric interfaces, the other 2 are optical interfaces.

I write the following sender code,


import socket
import time

ANY = '0.0.0.0'
SENDERPORT=15011
MCAST_ADDR = '239.255.255.250'
MCAST_PORT = 12345

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.bind((ANY,SENDERPORT))
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255) 
while True:
    time.sleep(1)
    sock.sendto('Hello World!', (MCAST_ADDR,MCAST_PORT) );

With the following receiver code, I can receive all the packets that sender sent.

import socket
import time

ANY = '0.0.0.0'
MCAST_ADDR = '239.255.255.250'
MCAST_PORT = 12345

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
sock.bind((ANY,MCAST_PORT))
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255) 
status = sock.setsockopt(socket.IPPROTO_IP,  
    socket.IP_ADD_MEMBERSHIP,
    socket.inet_aton(MCAST_ADDR) + socket.inet_aton(ANY));

sock.setblocking(0)
while True:
    try:
        data, addr = sock.recvfrom(1024)
    except socket.error, e:
        pass
    else:
        print("DATA: ", data)

But when I receive data using kernel-bypass method(like solarflare efvi, Join Membership is OK), no data captured.

With the same kernel-bypass method(program), and I am sure there are multicast packets in the specified network interface(optical), I can capture all the multicast packets. (The interface cannot communicate with other interface. No route.)

How can I set options in my sender Python code that the multicast packets will flow on to the desired interface definitely?

0

There are 0 best solutions below