not seeing UDP multicast messages from another device

半城伤御伤魂 提交于 2019-12-24 12:00:23

问题


I have a Windows machine where I have two scripts that send and receive messages via UDP multicast (on the same machine). I have a C and Python3 implementation of this. The Python3 one looks like this:

sender.py

import socket

MCAST_GRP = '239.1.1.1'
MCAST_PORT = 1234

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
print("Sending")
sock.sendto(bytearray("str()", "utf-8"), (MCAST_GRP, MCAST_PORT))

data, address = sock.recvfrom(1024)
print('received %s bytes from %s' % (len(data), address))
print(data)

receiver.py

import socket
import struct
import sys

multicast_group = '239.1.1.1'
server_address = ('', 1234)

# Create the socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Bind to the server address
sock.bind(server_address)

# Tell the operating system to add the socket to the multicast group
# on all interfaces.
group = socket.inet_aton(multicast_group)
mreq = struct.pack('4sL', group, socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

# Receive/respond loop
while True:
    print('\nwaiting to receive message')
    data, address = sock.recvfrom(1024)

    print('received %s bytes from %s' % (len(data), address))
    print(data)

    print('sending acknowledgement to', address)
    sock.sendto(bytearray("ack", "utf-8"), address)

I have another physical device hooked up to the same machine but I cannot receive any messages from it with my programs. I can see that Wireshark is seeing the messages from the other physical device, these messages are coming over the eth0 interface to the same IP and Port. I know that the traffic generated by my scripts is on the VirtualBox Host-Only Network. I am not sure if that could cause the issue of me not seeing the UDP multicast messages from the external device.

I have tested it on a Linux machine as well (latest kali version) but could not receive any messages from the external device as well.

If I am missing some information, please let me know.

EDIT 1:

My setup is as follows: I am running a native Windows 10 machine. To this machine is a device connected that is running some OS I don't know. I am only able to send and receive messages from it. I can send Ethernet, TCP, and IPv4 packets over the physical ethernet port on my Windows 10 machine by specifying the software I am using for this to use eth0 and a defined IP(v4) address I assigned to that port in the network adapter settings (192.168.1.100)

The scripts are running on the same Windows 10 machine that is also connected to the device. They are sending on this interface VirtualBox Host-Only Network but I don't know why. I did not configure anything like this. I assume that the interface should not be a problem because that is how UDP Multicast works (I am not sure of that so if I am mistaken please let me know!)

A sample output of the sender looks like this:

Sending
received 3 bytes from ('192.168.56.1', 3000)
b'ack'

Process finished with exit code 0

and the receiver:

waiting to receive message
received 5 bytes from ('192.168.56.1', 55132)
b'robot'
sending acknowledgement to ('192.168.56.1', 55132)

waiting to receive message

I hope that clarifies the setup. If there is still information missing please let me know!


回答1:


As covered in https://www.tldp.org/HOWTO/Multicast-HOWTO-6.html, the sockets API requires that you identify the interface as well as the multicast address/port you want to use.

By not specifying this in your sample code, you have left this down to the OS to pick and it has picked a VirtualBox Host-Only Network. Unfortunately this type of network is limited to VMs running on the Windows machines.

To fix it, you need to identify the interface that you want to use for the multicast and pass that in to your sending and receving code. For example:

sender.py
sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(MCAST_IF_IP))
receiver.py
mreq = struct.pack('4s4s', group, socket.inet_aton(MCAST_IF_IP))

where MCAST_IF_IP is the IP address of the interface that you want to use.



来源:https://stackoverflow.com/questions/50469157/not-seeing-udp-multicast-messages-from-another-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!