Page 1 of 1

Using the sockets API I get an error related to sockaddr_in

Posted: Sat Jul 13, 2024 6:31 pm
by jhsrennie
Mainly for fun I wanted to see if I could write a server using the good old BSD sockets API. This appears to be supported on the ESP32 but when I try to compile a simple program I get errors that I cannot understand.

The code is very short and simple:

Code: Select all

#include "lwip/sockets.h"

void setup() {
  Serial.begin(115200);

   int sock_srv = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  
  struct sockaddr addr_in;
  memset(&addr_in, 0, sizeof(addr_in));

  struct sockadder_in* p_addr_in = (struct sockadder_in*) &addr_in;
  p_addr_in->sin_family = PF_INET;
  p_addr_in->sin_port = htons(23);
  p_addr_in->sin_addr.s_addr = INADDR_ANY;
  bind(sock_srv, &addr_in, sizeof(addr_in));

  listen(sock_srv, 1);
}

void loop() {
  delay(100);
}
But when I compile it I get errors:

Code: Select all

sketch.ino:12:12: error: invalid use of incomplete type 'struct setup()::sockadder_in'
   12 |   p_addr_in->sin_family = PF_INET;
      |            ^~
sketch.ino:11:10: note: forward declaration of 'struct setup()::sockadder_in'
   11 |   struct sockadder_in* p_addr_in = (struct sockadder_in*) &addr_in;
      |          ^~~~~~~~~~~~
sketch.ino:13:12: error: invalid use of incomplete type 'struct setup()::sockadder_in'
   13 |   p_addr_in->sin_port = htons(23);
      |            ^~
sketch.ino:11:10: note: forward declaration of 'struct setup()::sockadder_in'
   11 |   struct sockadder_in* p_addr_in = (struct sockadder_in*) &addr_in;
      |          ^~~~~~~~~~~~
sketch.ino:14:12: error: invalid use of incomplete type 'struct setup()::sockadder_in'
   14 |   p_addr_in->sin_addr.s_addr = INADDR_ANY;
      |            ^~
sketch.ino:11:10: note: forward declaration of 'struct setup()::sockadder_in'
   11 |   struct sockadder_in* p_addr_in = (struct sockadder_in*) &addr_in;
      |          ^~~~~~~~~~~~
If you want to try for yourself I wrote it on Wokwi and this is the link:
https://wokwi.com/projects/403318967701557249

I get exactly the same errors on my ESP32-WROOM-32, but I used Wokwi to give anyone interested an easy way to try the code for themselves.

The error suggests that the header file is not defining the struct sockaddr_in but I had a look through the source code here:
https://github.com/espressif/esp-lwip/b ... /sockets.h

and it looks to me as if the struct is defined. I note the definition is wrapped in a #if statement:

Code: Select all

#if LWIP_IPV4
/* members are in network byte order */
struct sockaddr_in {
  u8_t            sin_len;
  sa_family_t     sin_family;
  in_port_t       sin_port;
  struct in_addr  sin_addr;
#define SIN_ZERO_LEN 8
  char            sin_zero[SIN_ZERO_LEN];
};
#endif /* LWIP_IPV4 */
and I tried adding

Code: Select all

#define LWIP_IPV4 1
above the #include

Code: Select all

#include "lwip/sockets.h"
but that made no difference.

If anyone can figure out what is causing the errors I would be most grateful. I have been bashing my head on this for a day now without getting anywhere.

Re: Using the sockets API I get an error related to sockaddr_in

Posted: Sun Jul 14, 2024 5:31 am
by ESP_Sprite
The proper struct is 'sockaddr_in' but you consistently write 'sockadder_in'. Note the extra 'e'.

Re: Using the sockets API I get an error related to sockaddr_in

Posted: Sun Jul 14, 2024 5:53 am
by jhsrennie
Oh no! Just a stupid typo!

A thousand thanks :-) I've been staring at that for hours and failed to see the typo.

Re: Using the sockets API I get an error related to sockaddr_in

Posted: Mon Jul 15, 2024 1:44 pm
by ESP_Sprite
No worries :) I know exactly how easy it is to look over things like that when you're in the thick of things.