Send and receive IPv6 link-local multicast UDP datagrams in Python?

Matt Joiner picture Matt Joiner · Jul 6, 2010 · Viewed 8.2k times · Source

This following is a straightforward IPv4 UDP broadcast, followed by listening on all interfaces.

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True)
sock.bind(("", 1337))
sock.sendto("hello world", ("255.255.255.255", 1337))
while True:
    data, addr = sock.recvfrom(0x100)
    print "received from {0}: {1!r}".format(addr, data)

I want to adjust this to send and receive both IPv4 and IPv6.

I've poked around and read as much as I can and believe the following is roughly the route I need to take:

  1. Create an IPv6 socket.
  2. Add the socket to a link or site local multicast group.
  3. Send the UDP packets to the multicast address for the group in use.

Further info I have is that I may need to bind to several interfaces, and tell the socket using setsockopt() that it should also receive multicast packets. Lastly getaddrinfo() can be used across the board to gracefully "drop back" to IPv4 where IPv6 isn't available.

I have much of this implemented, but stumble primarily on the multicasting parts. A complete code example in Python, or vivid description of the constants and addresses needed are preferred.

Answer

Nikolai Fetissov picture Nikolai Fetissov · Aug 13, 2010

Here's a link to python mcast demo, does both IPv4 and IPv6.