sending and receiving ipv6 multicasts

K

Kai Timmer

I am trying to send and receive packages via an ipv6 multicast. But I
can't get it working. What I thought was, that on the listener site, I
just need to bind my socket to the interfaces ipv6 local link address
and on the sender site, to the multicast address (in my case ff02::).
That doesn't work :)

I am using this: http://code.activestate.com/recipes/442490/ as an
example code, but I do not understand to which ipv6 address the socket
must be bind, to get this working on my link local network.

To make clear what I want to do: Every node in my ff80:: network
should receive a UDP package and answer with something. The sending
node then receives all the answers and puts them in some kind of data
structure. Can't be to hard to do this, but I am just lost in ipv6 ;)
 
M

Martin v. Löwis

Kai said:
I am trying to send and receive packages via an ipv6 multicast. But I
can't get it working. What I thought was, that on the listener site, I
just need to bind my socket to the interfaces ipv6 local link address
and on the sender site, to the multicast address (in my case ff02::).
That doesn't work :)

On the receiving side, you also need to set the IPV6_JOIN_GROUP
socket option - else your kernel doesn't know you are interested in
packets for that address. You need to bind to the multicast port,
and optionally to the multicast address.

On the sending side, you just use sendto, passing the multicast
address as the recipient.

HTH,
Martin
 
K

Kai Timmer

On the receiving side, you also need to set the IPV6_JOIN_GROUP
socket option - else your kernel doesn't know you are interested in
packets for that address. You need to bind to the multicast port,
and optionally to the multicast address.

If I do the following
s , ifn = createSocket("eth0")
maddr = ("ff02::1", 10101)

maddr = socket.getaddrinfo(maddr[0], maddr[1], socket.AF_INET6,
socket.SOCK_DGRAM)[0][-1]
group = socket.inet_pton(socket.AF_INET6, maddr[0]) + ifn #ifn is the
interface index
s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, group)
s.bind(maddr)

def createSocket(if=""):
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)

sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if hasattr(socket, "SO_REUSEPORT"):
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)

sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP,
1)
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS,
224)

ifn = if_nametoindex(if)
ifn = struct.pack("I", ifn)
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF,
ifn)

return sock, ifn

I get the following error: "socket.error: [Errno 22] Invalid
argument". So it complains about the multicast address.
 
M

Martin v. Löwis

I get the following error: "socket.error: [Errno 22] Invalid
argument". So it complains about the multicast address.

The fragment

py> import socket
py> s = socket.inet_pton(socket.AF_INET6, "ff02::1")
py> sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
py> sock.bind(('', 9090))
py> sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP,
socket.inet_pton(socket.AF_INET6, "ff02::1")+'\0'*4)

works fine for me.

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top