chrysn / aiocoap

The Python CoAP library
Other
264 stars 119 forks source link

Specifiy the network interface on windows #334

Closed sibertdeclercq closed 6 months ago

sibertdeclercq commented 9 months ago

I'm trying to use aiocoap as a CoAP client on windows. I'm trying to make requests over IPv6, which fails presumable because I cannot figure out how to tell aiocoap which network interface it should be using.

I've tried using aiocoap-client: aiocoap-client -m GET coap://[fe80::21c:e2ff:fe00:3ce2]/info which eventually times out

I can get it to work on Linux (WSL) by appending %eth1 to the address: aiocoap-client -m GET coap://[fe80::21c:e2ff:fe00:3ce2%eth1 ]/info but this obviously doesn't work on windows.

Is this functionality supported for windows somehow?

Enviroment:

Python version: 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)]
aiocoap version: 0.4.7
Modules missing for subsystems:
    dtls: missing DTLSSocket
    oscore: missing cbor2, cryptography, filelock, ge25519
    linkheader: everything there
    prettyprint: missing cbor2, termcolor, pygments
Python platform: win32
Default server transports:  tcpserver:tcpclient:tlsserver:tlsclient:simple6:simplesocketserver
Selected server transports: tcpserver:tcpclient:tlsserver:tlsclient:simple6:simplesocketserver
Default client transports:  tcpclient:tlsclient:simple6
Selected client transports: tcpclient:tlsclient:simple6
SO_REUSEPORT available (default, selected): False, False
chrysn commented 8 months ago

I have little usable knowledge of how Windows does interface selection; I just vaguely remember that Windows is using numeric interface identifiers (so maybe it's just a topic of doing [fe80::...%1] or some other small value).

I'm happy to fix anything that works on normal UDP sockets -- can you open a regular socket in Python to that host with any arguments?

a-w commented 6 months ago

An address beginning with fe80:: is a link-local address. This will work only if applied on the correct interface - both on Windows and Linux. You should have an IPv6 capable router in your network, which either provides you global unicast addresses (usually delegated from your internet provider) or a unique local address (ULA) which you can define yourself (in your router). If your network is configured properly, then any OS, including Windows, will route the request to the correct interface. Background information on IPv6 address types: https://www.ripe.net/media/documents/ipv6_reference_card.pdf

sibertdeclercq commented 6 months ago

I eventually found the solution.

aiocoap-client -m GET coap://[fe80::21c:e2ff:fe00:3ce2%<interface number>]/info

In contrast to WSL/Linux, the value after the % should be the interface number, not the interface name. The interface number can be found in ipconfig