raspberrypi / pico-examples

BSD 3-Clause "New" or "Revised" License
2.79k stars 810 forks source link

picow_access_point example cannot be changed to other subnet #383

Open eamars opened 1 year ago

eamars commented 1 year ago

The example code from https://github.com/raspberrypi/pico-examples/blob/master/pico_w/wifi/access_point/picow_access_point.c#L293 shows the default IP address for the DHCP server is 192.168.4.1. When I changed the subnet to, for example, 192.168.3.1, my TCP client could not connect to the pico w anymore.

Wireshark shows the DHCP server is still offering 192.168.4.1 as the router.

Unfortuantely I'm not professional in lwip implementation therefore I could not figure out where the IP address is hard coded.

peterharperuk commented 1 year ago

I can't think what would need to change - probably a bug. Is there any reason why are you using the tcp_server example rather than the access point example?

eamars commented 1 year ago

Hi @peterharperuk Thanks for your reply. I just updated the title and description for the issue. The cause was I changed the IP address for the host from 192.168.4.1 to 192.168.3.1. For some reason the DHCP server is still offering the 192.168.4.1 for the router address, according to the wireshark, therefore the host could not be resolved.

peterharperuk commented 1 year ago

You changed the gateway address to 192.168.3.1 ad it still offers 192.168.4.1?

IP4_ADDR(ip_2_ip4(&state->gw), 192, 168, 4, 1); IP4_ADDR(ip_2_ip4(&mask), 255, 255, 255, 0);

// Start the dhcp server dhcp_server_t dhcp_server; dhcp_server_init(&dhcp_server, &state->gw, &mask);

As far as I'm aware the dhcp server uses its own address and just changes the last number.

Do you have two netifs? If you have an AP and are also connected via STA mode - be aware of this bug https://github.com/raspberrypi/pico-examples/issues/382

eamars commented 1 year ago
Screen Shot 2023-05-18 at 12 01 05 AM

The new DHCP starts from packet 481. The 192.168.4.1 was offering the IP address while the pi has 192.168.3.1 IP address. In the end the 192.168.3.16 was assigned to the client.

eamars commented 1 year ago

@peterharperuk The only change I made was to change the subnet from 192.168.4.1 to 192.168.3.1.

Only the AP mode is activated at the time of running the TCP server. Also please find my screenshot above regarding the DHCP packet.

peterharperuk commented 1 year ago

If you changed the gateway address I don't see how it's possible for it to offer the wrong address.

eamars commented 1 year ago

@peterharperuk I finally trace back to the source of the 192.168.4.1 https://github.com/georgerobotics/cyw43-driver/blob/9bfca61173a94432839cd39210f1d1afdf602c42/src/cyw43_lwip.c#L182

For which when DHCP sends the offer, it uses 192.168.4.1 as the source. Unfortuantely due to the lack of knowledge I'm not sure why the system could not behave as normal when the IP address is changed to 192.168.3.1.

Is this coincidence for pico-example to also choose the 192.168.4.1 as the default subnet for example in the AP mode?

peterharperuk commented 1 year ago

Ah right. Well done - I'd completely forgotten about that! The latest driver (which should make it into the next release, has a macro CYW43_DEFAULT_IP_AP_GATEWAY to set this. I guess the dhcp server should use that.

peterharperuk commented 1 year ago

Is this coincidence for pico-example to also choose the 192.168.4.1 as the default subnet for example in the AP mode?

I think they have to match, right? The cyw43-driver comes originally from Micropython and I "borrowed" this DHCP server from Micropython, so I suspect that's why they match.

eamars commented 1 year ago

Thank you very much!