How to get UDP header information

Posts: 1
Joined: Fri Oct 19, 2018 3:39 am

How to get UDP header information

Postby aneto » Fri Feb 01, 2019 9:15 pm


I'm trying to answer a UDP request, but I'm not being able to locate the sender information (IP and port). I can broadcast the message to circumvent the necessity of the sender IP, but I can't do the same with the port. And, while the port I'm listening to is fixed, the port I'm supposed to send the message back changes with every message (it is on the UDP header specification, but apparently, the SDK doesn't give me easy access to that).

Bellow are the relevant parts of the code:

Code: Select all

static void ICACHE_FLASH_ATTR udp_recv(void *arg, char *pusrdata, unsigned short length) {
   struct espconn *pespconn = arg;

   os_printf("client %d.%d.%d.%d:%d -> ", pespconn->proto.udp->remote_ip[0],
           pespconn->proto.udp->remote_ip[1], pespconn->proto.udp->remote_ip[2],
           pespconn->proto.udp->remote_ip[3], pespconn->proto.udp->remote_port);
   os_printf("received %d bytes of data\n", length);
   os_printf("server %d.%d.%d.%d:%d\n", pespconn->proto.udp->local_ip[0],
           pespconn->proto.udp->local_ip[1], pespconn->proto.udp->local_ip[2],
           pespconn->proto.udp->local_ip[3], pespconn->proto.udp->local_port);

   uint16_t i;
   for(i = 0; i < length; ++i) {
      os_printf("%02x ", pusrdata[i]); // the IP and port are not in the user data. It should be in the connection

   sint8_t errcode = espconn_send(pespconn, response, sizeof(response)); //It's being sent to the wrong place because I don'r know the right IP/port pair.
   if(errcode != 0) {
      os_printf("%s\n", toURL(question));
      os_printf("UDP send: ");
      switch(errcode) {
         //case 0: os_printf("ok\n"); break;
         case ESPCONN_ISCONN: os_printf("already connected\n"); break;
         case ESPCONN_MEM: os_printf("lack of memory\n"); break;
         case ESPCONN_IF: os_printf("fail\n"); break;
         default: os_printf("unknown error (%d)\n", errcode);

Code: Select all

void ICACHE_FLASH_ATTR server_start(void) {
   struct espconn *conn = (struct espconn *) os_zalloc(sizeof(struct espconn));

   if (conn == NULL) return;

   conn->type = ESPCONN_UDP;
   conn->proto.udp = (esp_udp *) os_zalloc(sizeof(esp_udp));
   conn->proto.udp->local_port = XX; // port my devices listens to
   conn->proto.udp->remote_port = YYYY; // (????) port I should be sending te messages back

   char tcpserverip[16];
   os_sprintf(tcpserverip, "%s", "\0"); // I broadcasting, but I should use the right IP address
   uint32_t ip = ipaddr_addr(tcpserverip);
   ip = ipaddr_addr(tcpserverip);
   os_memcpy(conn->proto.udp->remote_ip, &ip, 4);

   espconn_regist_recvcb(conn, udp_recv);

   sint8_t errcode = espconn_create(conn);
   conn->proto.udp->remote_ip[2], conn->proto.udp->remote_ip[3]);
   os_printf("UDP connection: ");
   switch(errcode) {
      case 0: os_printf("created\n"); break;
      case ESPCONN_ISCONN: os_printf("already connected\n"); break;
      case ESPCONN_MEM: os_printf("lack of memory\n"); break;
      case ESPCONN_ARG: os_printf("invalid arguments\n"); break;
      default: os_printf("unknown error (%d)\n", errcode);

I've tried to set all fields in the connection (local IP, local port, remote IP and remote port), just the local fields (I was expecting the device would fill in the remote fields by itself as it should), just the remote fields (I don't even get the message, as expected) and a lot of different combinations. I've also tried to change the command to espconn_sendto instead of espconn_send, but it makes no difference and somewhere else in this forum someone said espconn_sendto has a bug.

How do I the remote information? Does anyone has any idea?

Thanks in advance for any suggestion.

Her Mary
Posts: 390
Joined: Mon Oct 27, 2014 11:09 am

Re: How to get UDP header information

Postby Her Mary » Tue Feb 26, 2019 5:37 pm

Maybe the ESP8266 RTOS SDK is a better choice. ... /protocols

Who is online

Users browsing this forum: No registered users and 5 guests