markqvist / Reticulum

The cryptography-based networking stack for building unstoppable networks with LoRa, Packet Radio, WiFi and everything in between.
https://reticulum.network
MIT License
2k stars 124 forks source link

Use SO_EXCLUSIVEADDRUSE instead of SO_REUSEADDR on Windows #278

Closed VioletEternity closed 1 year ago

VioletEternity commented 1 year ago

On Linux, SO_REUSEADDR is used so that a socket in TIME-WAIT state can be rebound after a listening process is restarted. It does not allow two processes to listen on the exact same (addr, port) combination. However, on Windows, it does, and SO_EXCLUSIVEADDRUSE is required to reproduce the Linux behavior.

Reticulum relies on an error being returned by bind() that reuses the same (addr, port) combination as another process to detect whether there is a shared instance already running. Setting SO_EXCLUSIVEADDRUSE makes this detection process work on Windows as well.

VioletEternity commented 1 year ago

The combination of SO_REUSEADDR and SO_EXCLUSIVEADDRUSE on Windows has WSABind return a WSAEINVAL error. Ideally, Reticulum would probe for a running shared instance by attempting to connect to it rather than attempting to bind, but this PR is a much simpler change that fixes the immediate issue of not being able to use most of the functionality of RNS with RNodes (which relies on being able to use a shared instance).

markqvist commented 1 year ago

Thank you very much for this contribution!