Page 1 of 1

UDP packet stops going out over ethernet

Posted: Mon Sep 18, 2023 11:34 am
by asierdo
Hi everybody,

I'm using ESP32-WROVER-2 module.

Interfaces that are configured:
Ethernet.
Wifi AP or Wifi client.

Protocols started:
Socket UDP.
Webserver with some websockets.


On one hand, I have UDP packets going out over ethernet to a local PC.

At the same time, I started a web server with some websockets. I can configure the wifi of the ESP32 as AP mode or as Client mode to serve a http page.

When I start wifi as AP mode, everything works fine. UDP packets goes out over ethernet and web server serves the page over wifi as expected. Instead, if I start Wifi as client mode, as soon as the router assigns IP to esp32, UDP packets stop leaving ethernet.

Is any special socket configuration necessary for it to work when wifi is enabled as a client?

Thank you so much,
Asier.

Re: UDP packet stops going out over ethernet

Posted: Tue Sep 19, 2023 4:54 pm
by MicroController
asierdo wrote:
Mon Sep 18, 2023 11:34 am
Is any special socket configuration necessary for it to work when wifi is enabled as a client?
You can try to explicitly bind the UDP socket to the ethernet interface via setsockopt(..., SO_BINDTODEVICE,...), like in this example.

Re: UDP packet stops going out over ethernet

Posted: Wed Sep 20, 2023 8:36 am
by asierdo
I bind sock to address but no way.

Furthermore, I do:
int err = sendto(sock, &udp_packet, sizeof(udp_packet), 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));

where address is explicitly passed to the function.

¿Why udp packet are going out when Wifi is in AP mode or when it is just disabled and not when it is client?

Thank you,

ASier.

Re: UDP packet stops going out over ethernet

Posted: Thu Sep 21, 2023 6:19 pm
by MicroController
¿Why udp packet are going out when Wifi is in AP mode or when it is just disabled and not when it is client?
I'd guess that either a) the UDP socket doesn't survive the reconfiguration of the network environment, or b) the UDP packets still "go out", but via WiFi once WiFi is connected.

Re: UDP packet stops going out over ethernet

Posted: Fri Sep 22, 2023 8:59 am
by asierdo
It seems to be otion b) that you said, because only when the router assigns an IP does it start to fail. Is there any way or example to redirect those udp packets to the ethernet interface ?

I've tried to bind but it does not work:

esp_netif_ip_info_t ip;
memset(&ip, 0, sizeof(esp_netif_ip_info_t));
ESP_ERROR_CHECK(esp_netif_get_ip_info(eth_handle, &ip));
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = ip.ip.addr;
int retb = bind(sock, (struct sockaddr *)&addr, sizeof(addr));

Best regards,
ASier.

Re: UDP packet stops going out over ethernet

Posted: Fri Sep 22, 2023 6:03 pm
by MicroController
Is there any way or example to redirect those udp packets to the ethernet interface ?
You do read the answers people write to your questions, right?
MicroController wrote:
Tue Sep 19, 2023 4:54 pm
You can try to explicitly bind the UDP socket to the ethernet interface via setsockopt(..., SO_BINDTODEVICE,...), like in this example.

Re: UDP packet stops going out over ethernet

Posted: Mon Sep 25, 2023 11:16 am
by asierdo
The issue was here:

esp_netif_ip_info_t ip;
memset(&ip, 0, sizeof(esp_netif_ip_info_t));
ESP_ERROR_CHECK(esp_netif_get_ip_info(eth_netif, &ip));
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = ip.ip.addr;
int retb = bind(sock, (struct sockaddr *)&addr, sizeof(addr));

I was passing wrong netif, so I was binding an incorrect address.

Thank you,
ASier.

Re: UDP packet stops going out over ethernet

Posted: Mon Sep 25, 2023 5:33 pm
by MicroController
Glad you got it working :)