zoozoc
zoozoc

Reputation: 105

Python 3 - Cannot receive IPv6 packets (UDP - linux)

I have a script that is trying to receive IPv6 packets, but it fails to receive any.

First off, here is my ethernet configuration from ifconfig.

eth1      Link encap:Ethernet  HWaddr f8:b1:56:9a:cf:ef  
      inet addr:192.168.1.90  Bcast:192.168.1.255  Mask:255.255.255.0
      inet6 addr: fe80::fab1:56ff:fe9a:cfef/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:751359199 errors:38 dropped:10874 overruns:0 frame:35
      TX packets:23407 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
      RX bytes:1033523557150 (1.0 TB)  TX bytes:2002869 (2.0 MB)
      Interrupt:20 Memory:ef400000-ef420000 

I have two network cards, but am using one for internet and one for testing. The second card is connect to a device that sends ethernet packets. I am configuring that device to send IPv6 packets to address fe80::fab1:56ff:fe9a:cfef and port 46780 (however, I can configure it to send to any IPv6 address and any port). I wrote a python script to receive these packets, but I either get an error, or my script doesn't find the packets. I confirmed these packets through wireshark, and through using a raw python socket.

Here is a list of things I have tried and the various errors/problems I encounter.

  1. If I bind to address "::1", I am able to bind to the address. However, I never receive any IPv6 packets.
  2. I tried using socket.getaddrinfo() and then use the returned information and bind to that, however when I try to do so I get the error "Invalid argument"

    info = socket.getaddrinfo(host_ipv6_addr, port_num, socket.AF_INET6, socket.SOCK_DGRAM, 0, socket.AI_PASSIVE) rtp_socket.bind(info[0][4])

socket.getaddrinfo returns [(10, 2, 17, '', ('fe80::fab1:56ff:fe9a:cfef', 46780, 0, 0))]

  1. If I try to bind directly to my IPv6 address, I also received "Invalid argument". However, when I changed the scope from 0 to 5, I instead received the error "Cannot assign request address". rtp_socket.bind( (host_ipv6_addr, port_num, 0, 5))

Any insight would be greatly appreciated. I'm guessing at this point that I don't have my ethernet card setup properly or something.

UPDATE: Using Michael Hampton's answer, I solved my problem by using the information from socket.getaddrinfo with the IP address being "fe80::fab1:56ff:fe9a:cfef%eth1" and sticking the results into rtp_socket.bind(). The scope ID went from 0 to 3.

Upvotes: 1

Views: 578

Answers (1)

Michael Hampton
Michael Hampton

Reputation: 9980

You're trying to bind to a link-local address but you have forgotten to include the scope ID (in this case, %eth1).

So you should be binding to address fe80::fab1:56ff:fe9a:cfef%eth1.

Upvotes: 2

Related Questions