Error Sending UDP Packet

cpitman
Posts: 3
Joined: Sun Jan 21, 2024 11:34 pm

Error Sending UDP Packet

Postby cpitman » Sun Jan 21, 2024 11:47 pm

Hi all,

Been debugging this routine that sends UDP packets for the last couple hours, and not sure what to look at next. This is the ESP32-S3, as the only node making up an ESP Mesh Lite. It has connected to the router, it manages to get an IP address over DHCP and I can ping it.

When I send a UDP datagram, it fails with an errno of 118, which I believe means we're not connected to the network? But it definitely is. In the debug logs, I can see DHCP traffic continuing to arrive at the ESP32-S3.

Here's a snippet of the UDP code:
  1. int create_udp_socket() {
  2.     int sock = socket(AF_INET, SOCK_DGRAM, 0);
  3.     if (sock < 0) {
  4.         ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
  5.         return -1;
  6.     }
  7.  
  8.     struct sockaddr_in addr = {
  9.         .sin_family = AF_INET,
  10.         .sin_port = htons(5000),
  11.         .sin_addr.s_addr = inet_addr("192.168.1.3"),
  12.     };
  13.  
  14.     if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  15.         ESP_LOGE(TAG, "Failed to bind socket");
  16.         return -1;
  17.     }
  18.  
  19.     return sock;
  20. }
  21.  
  22. ...
  23.  
  24. struct sockaddr_in dest_addr = {
  25.                 .sin_family = AF_INET,
  26.                 .sin_port = htons(5000),
  27.                 .sin_addr.s_addr = inet_addr("192.168.1.3"),
  28. };
  29. if (-1 == sendto(udp_socket, rx_buffer, data_length, 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr))) {
  30.                 ESP_LOGE(TAG, "Failed to send data to udp socket: %d", errno);
  31. }
And here is an example of the log output, including both a DHCP packet that is working and the outgoing packet that is not (starting at 12396):
  1. D (11416) WifiBridge: Waiting for interrupt
  2. D (12006) lwip: lwip_sendto(54, data=0x3fca467c, short_size=183, flags=0x0 to=
  3. D (12006) lwip: 255.255.255.255
  4. D (12006) lwip:  port=6364
  5.  
  6. D (12006) lwip: udp_send
  7.  
  8. D (12006) lwip: udp_send: added header in given pbuf 0x3fcb7c7c
  9.  
  10. D (12016) lwip: udp_send: sending datagram of length 191
  11.  
  12. D (12016) lwip: udp_send: UDP packet length 191
  13.  
  14. D (12026) lwip: udp_send: UDP checksum 0xbbff
  15.  
  16. D (12026) lwip: udp_send: ip_output_if (,,,,0x11,)
  17.  
  18. D (12036) lwip: ip4_output_if: st3
  19.  
  20. D (12036) lwip: IP header:
  21.  
  22. D (12036) lwip: +-------------------------------+
  23.  
  24. D (12046) lwip: | 4 | 5 |  0x00 |       211     | (v, hl, tos, len)
  25.  
  26. D (12046) lwip: +-------------------------------+
  27.  
  28. D (12056) lwip: |        7      |000|       0   | (id, flags, offset)
  29.  
  30. D (12066) lwip: +-------------------------------+
  31.  
  32. D (12066) lwip: |  255  |   17  |    0xf8c0     | (ttl, proto, chksum)
  33.  
  34. D (12076) lwip: +-------------------------------+
  35.  
  36. D (12076) lwip: |  192  |  168  |    1  |  170  | (src)
  37.  
  38. D (12086) lwip: +-------------------------------+
  39.  
  40. D (12086) lwip: |  255  |  255  |  255  |  255  | (dest)
  41.  
  42. D (12096) lwip: +-------------------------------+
  43.  
  44. D (12096) lwip: ip4_output_if: call netif->output()
  45.  
  46. D (12306) lwip: ip4_input: UDP packet to DHCP client port 5353
  47.  
  48. D (12316) lwip: ip4_input: packet not for us.
  49.  
  50. D (12396) WifiBridge: Interrupt triggered
  51. D (12396) WifiBridge: Received 34 bytes from mcu
  52. D (12396) lwip: lwip_sendto(58, data=0x3fc9dc74, short_size=34, flags=0x0 to=
  53. D (12396) lwip: 192.168.1.3
  54. D (12396) lwip:  port=5000
  55.  
  56. D (12406) lwip: udp_send
  57.  
  58. E (12406) WifiBridge: Failed to send data to udp socket: 118
Any hints would be appreciated!

cpitman
Posts: 3
Joined: Sun Jan 21, 2024 11:34 pm

Re: Error Sending UDP Packet

Postby cpitman » Mon Jan 22, 2024 2:38 am

Turned on some more debug logs:
  1. D (11987) WifiBridge: Waiting for interrupt
  2. D (12947) WifiBridge: Interrupt triggered
  3. D (12957) WifiBridge: Received 34 bytes from mcu
  4. D (12957) lwip: lwip_sendto(58, data=0x3fc9dc74, short_size=34, flags=0x0 to=
  5. D (12957) lwip: 192.168.1.3
  6. D (12957) lwip:  port=5000
  7.  
  8. D (12957) lwip: pbuf_alloc(length=34)
  9.  
  10. D (12967) lwip: pbuf_alloc(length=34) == 0x3fcb82d4
  11.  
  12. D (12967) lwip: netconn_send: sending 34 bytes
  13.  
  14. D (12977) lwip: udp_send
  15.  
  16. D (12977) lwip: pbuf_free(0x3fcb82d4)
  17.  
  18. D (12977) lwip: pbuf_free: deallocating 0x3fcb82d4
  19.  
  20. E (12987) WifiBridge: Failed to send data to udp socket: 118
So, at a minimum, the execution is throwing an error sometime after netconn_send.

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

Re: Error Sending UDP Packet

Postby MicroController » Mon Jan 22, 2024 1:17 pm

You're binding the socket to the same IP address you're trying to send data to ("192.168.1.3")?
This is a) likely not what you want, unless you want the ESP to send data to itself, and b) not going to work unless "192.168.1.3" is actually the IP address of the ESP.

cpitman
Posts: 3
Joined: Sun Jan 21, 2024 11:34 pm

Re: Error Sending UDP Packet

Postby cpitman » Mon Jan 22, 2024 2:27 pm

That was exactly the issue, thanks!

Who is online

Users browsing this forum: No registered users and 82 guests