esp-rs / esp-wifi-sys

Wi-Fi and BT drivers packaged for integration into bare-metal esp-wifi.
Apache License 2.0
401 stars 94 forks source link

`next_local_port()` always return the same port after resets / panic #383

Closed AnthonyGrondin closed 9 months ago

AnthonyGrondin commented 11 months ago

next_local_port(), which is used as a local port when connecting outside isn't random and instead uses the same value after resets or panic. This causes issues when trying to connect to a server, which already had a previous connection opened with that port, because it might be stuck in a previous case.

With the current implementation, it always uses 41001 when opening a new connection. Since esp32 supports hardware RNG, I think it should be used as a source of entropy for selecting a port.

https://github.com/esp-rs/esp-wifi/blob/6f438e33bbac2d37abe522470b3afd648e858573/esp-wifi/src/wifi_interface.rs#L435-L443

Reference: https://github.com/smoltcp-rs/smoltcp/issues/850

EDIT: There is a similar issue when using async + embassy_net as the seed used determines the port, but we hardcode the seed in the examples to 1234 https://github.com/esp-rs/esp-wifi/blob/6f438e33bbac2d37abe522470b3afd648e858573/esp-wifi/examples/embassy_dhcp.rs#L58-L66

Reference: https://github.com/embassy-rs/embassy/blob/14f41a71b6ea9dedb4ee5b9c741fe10575772c7d/embassy-net/src/lib.rs#L314

MabezDev commented 9 months ago

Closes via #414