Skip to content
Advertisement

Receive foreign UDP Broadcast with Python

I have a device in the network at 192.168.123.204 which broadcasts UDP datagrams(Artnet) as 2.168.123.204 to 2.255.255.255:6454. (The network address is 192.168.123.204 but the datagram is sent with 2.168.123.204 as source.) The address 2.255.255.255 can’t be changed (no setting for that).

My Python script runs on the device 192.168.123.148. I can receive the datagrams there with wireshark: but a Python socket bound to 0.0.0.0:6454 can’t receive them. Binding it to 2.168.123.204 or 2.255.255.255 does not work. The script is working, since I can receive Packets from 127.0.0.1.

If it can’t be solved with Python can I redirect the UDP broadcast with iptables (linux)?

Network:
        Router 192.168.123.1
          /   
Broadcaster: 192.168.123.204Script: 192.168.123.148

basic script:

JavaScript

full script:

JavaScript

interfaces:

JavaScript

Wireshark Protocol

Advertisement

Answer

I misread your question, you are dealing with broadcast not multicast addresses. Your problem is that you do not have the SO_BROADCAST flag sent

IPv4 addresses are divided into unicast, broadcast and multicast addresses. Unicast addresses specify a single interface of a host, broadcast addresses specify all hosts on a network and multicast addresses address all hosts in a multicast group. Datagrams to broadcast addresses can be only sent or received when the SO_BROADCAST socket flag is set. In the current implementation, connection-oriented sockets are only allowed to use unicast addresses.

There is an explicit example here. The most important part being:

JavaScript

You will also need to add an address on that subnet to the interface. This is very simple just enter:

JavaScript

This assumes that the broadcast is on 2.0.0.0/8, the interface name is eth1 and that 2.255.255.254 is not taken by another host.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement