简体   繁体   中英

Receiving UDPv6 multicast in Python

I have trouble with receiving UDPv6 packet using Python. Packet comes from SLIP interface using 6LBR and whole runs on Beagleboard, but rather that is not the case. Tcpdump captures the packet without any problem:

10:17:32.220009 IP6 (hlim 64, next-header UDP (17) payload length: 21)
fe80::200:0:0:3.3000 > ff02::1.3000: [udp sum ok] UDP, length 13
0x0000:  6000 0000 0015 1140 fe80 0000 0000 0000  `......@........
0x0010:  0200 0000 0000 0003 ff02 0000 0000 0000  ................
0x0020:  0000 0000 0000 0001 0bb8 0bb8 0015 f9fe  ................
0x0030:  4d65 7373 6167 6520 3537 3236 00         Message.5726.

However, I am not able to receive the packet using Python:

MYPORT = 3000
MYGROUP_6 = 'ff02::1%eth0'

import time
import struct
import socket
import sys

def main():

    addrinfo = socket.getaddrinfo(MYGROUP_6, None)[0]
    print(addrinfo)
    s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
    s.bind(('', MYPORT))
    while True:
        data, sender = s.recvfrom(150)
        print (str(sender) + '  ' + repr(data))

if __name__ == '__main__':
    main()

On the other hand, when I generate local multicast, the same code is able to receive packet. Tcpdump output of packet, which can be received:

10:29:37.301406 IP6 (hlim 3, next-header UDP (17) payload length: 30)
fe80::c03a:f9ff:fe3d:9b30.56963 > ff02::1.3000: [udp sum ok] UDP, length 22
    0x0000:  6000 0000 001e 1103 fe80 0000 0000 0000  `...............
    0x0010:  c03a f9ff fe3d 9b30 ff02 0000 0000 0000  .:...=.0........
    0x0020:  0000 0000 0000 0001 de83 0bb8 001e 20ca  ................
    0x0030:  3134 3336 3639 3639 3737 2e33 3030 3532  1436696977.30052
    0x0040:  3164 7570 6100                           1dupa.

Why? How can I capture this first packet using python?

ARM Linux ip configuration:

debian@arm:~$ sudo ifconfig
eth0      Link encap:Ethernet  HWaddr c2:3a:f9:3d:9b:30  
          inet addr:192.168.1.206  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::c03a:f9ff:fe3d:9b30/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST DYNAMIC  MTU:1500  Metric:1
          RX packets:261457 errors:0 dropped:0 overruns:0 frame:0
          TX packets:568962 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:28323386 (27.0 MiB)  TX bytes:71483396 (68.1 MiB)

Contiki node's IP configuration:

MAC 00:00:00:00:00:00:00:03 Contiki-2.7-7-g9698ca5 started. 
Node id is set to 3.
nullmac nullrdc, channel check rate 128 Hz, radio channel 26
Tentative link-local IPv6 address fe80:0000:0000:0000:0200:0000:0000:0003
Starting 'Unicast sender example process'
IPv6 addresses: bbbb::200:0:0:3
Sending unicast to ff02::1

You forgot to join the multicast group. Your process binds to the wildcard address on port 3000. Any packet that arrives on the with that destination port number is delivered to your process, but you're not actually asking the operating system to subscribe the process to the multicast group so there is no reason for the packet to get there (unless it's created locally).

To subscribe the system to a multicast dress you do:

# Get the interface index for eth0
interface_index = socket.if_nametoindex("eth0")

# Create the JOIN option data
mc_addr = ipaddress.IPv6Address('ff02::1')
join_data = struct.pack('16sI', mc_addr.packed, interface_index)

# And join the group
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, join_data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM