UDP packet stops going out over ethernet

asierdo
Posts: 11
Joined: Mon Sep 18, 2023 10:37 am

UDP packet stops going out over ethernet

Postby asierdo » Mon Sep 18, 2023 11:34 am

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.

MicroController
Posts: 1725
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: UDP packet stops going out over ethernet

Postby MicroController » Tue Sep 19, 2023 4:54 pm

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.

asierdo
Posts: 11
Joined: Mon Sep 18, 2023 10:37 am

Re: UDP packet stops going out over ethernet

Postby asierdo » Wed Sep 20, 2023 8:36 am

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.

MicroController
Posts: 1725
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: UDP packet stops going out over ethernet

Postby MicroController » Thu Sep 21, 2023 6:19 pm

¿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.

asierdo
Posts: 11
Joined: Mon Sep 18, 2023 10:37 am

Re: UDP packet stops going out over ethernet

Postby asierdo » Fri Sep 22, 2023 8:59 am

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.

MicroController
Posts: 1725
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: UDP packet stops going out over ethernet

Postby MicroController » Fri Sep 22, 2023 6:03 pm

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.

asierdo
Posts: 11
Joined: Mon Sep 18, 2023 10:37 am

Re: UDP packet stops going out over ethernet

Postby asierdo » Mon Sep 25, 2023 11:16 am

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.

MicroController
Posts: 1725
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: UDP packet stops going out over ethernet

Postby MicroController » Mon Sep 25, 2023 5:33 pm

Glad you got it working :)

Who is online

Users browsing this forum: No registered users and 271 guests