Raw Socket Udp Multicast In Ipv6
i receive data from multicast for my UDP sniffer, but only in IPv4. My code looks like this, try: s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_UDP) except
Solution 1:
this example gets a multicast on FF02::158 (IoTivity UDP CoAP) in Windows
import socket
import struct
address = ('', 5683)
interface_index = 0# default
sock = socket.socket(family=socket.AF_INET6, type=socket.SOCK_DGRAM)
sock.bind(address)
for group in ['ff02::158']: # multiple addresses can be specified
sock.setsockopt(
41, # socket.IPPROTO_IPV6 = 41 - not found in windows 10, bug python
socket.IPV6_JOIN_GROUP,
struct.pack(
'16si',
socket.inet_pton(socket.AF_INET6, group),
interface_index
)
)
whileTrue:
data, sender = sock.recvfrom(1500)
while data[-1:] == '\0': data = data[:-1]
print(str(sender) + ' ' + repr(data))
fuller answer https://stackoverflow.com/a/66943594/8625835
Solution 2:
You need to use the sockopt IPV6_ADD_MEMBERSHIP, as the API between IPv6 and IPv4 is slightly different. This is a good example.
Solution 3:
This is what I'm doing in my code:
mc_address = ipaddress.IPv6Address('ff02::1:2')
listen_port = 547
interface_index = socket.if_nametoindex('eth0')
mc_sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
mc_sock.bind((str(mc_address), listen_port, 0, interface_index))
mc_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP,
struct.pack('16sI', mc_address.packed, interface_index))
This is for a DHCPv6 server, but you'll get the idea.
If you also want to get multicast packets transmitted by yourself you have to add:
mc_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, 1)
Post a Comment for "Raw Socket Udp Multicast In Ipv6"