Send UDP packet to link-local IPv6 address?

Grant Edwards invalid at invalid.invalid
Wed Oct 29 18:36:30 CET 2014


On 2014-10-29, Alain Ketterlin <alain at dpt-info.u-strasbg.fr> wrote:
> Grant Edwards <invalid at invalid.invalid> writes:
>
> [...]
>> With link-local addresses you also need to specify which interface to
>> use. The normal way of doing this on Linux with command-line utilities
>> is append %<ifname> to the address/hostname (e.g. ping6 ff02::1%net1).
>>
>> That doesn't work:
>>
>> s.sendto(data, ("ff02::1%net1",port))
>> s.sendto(data, ("fe80::2c0:4eff:fe40:5f%net1",port))
>>
>> The "%net1" appears to be ignored, and the packet will go out _some_
>> interface, but I can't figure out how it decides which one (it's not
>> always the same one).
>
> The only way I've found is to use socket.getaddrinfo(), which does
> accept "%net1" after the IPv6 address, and returns scope_id (actually a
> complete sockaddr). I can't access my files now, by I think
>
>     socket.getaddrinfo("fe80::2c0:4eff:fe40:5f%net1",port,...)
>
> should work for you (family, socktype etc should be passed also to avoid
> searching the results of getaddrinfo()).

Doh!, of course that works.  Using getaddrinfo() is how you're always
supposed to do things these days.  Creating an address tuple by hand
is a bad habit left over from too many years spent doing network stuff
in the old days before getaddrinfo et al.  I looked at the
documentation for getaddrinfo(), and I would have sworn I had tried it
and couldn't get it to work.  But it does.

> Hope this helps,

Definitely!

Thanks!

-- 
Grant Edwards               grant.b.edwards        Yow! FOOLED you!  Absorb
                                  at               EGO SHATTERING impulse
                              gmail.com            rays, polyester poltroon!!



More information about the Python-list mailing list