12

I have a piece of code that used to work in some environment a long time ago. I'm pretty sure it was a FreeBSD machine so I got FreeBSD 8.3 and I'm trying to make this file but it's not working.

When I try to compile it it complains with:

f.c: In function 'tcp'>
f.c:24: error: storage size of 'socket_stru' isn't known
f.c:29: error: 'IPPROTO_TCP' undeclared (first use in this function)
...

I've been looking around and I see these are all specified in the sys/socket.h file. This is my actual file:

#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>


#include "f.h"

int tcp4 (in_addr_t ip, int port, int qsize )
{   
    struct sockaddr_in socket_stru; // line 24
    socket_stru.sin_family = AF_INET;
    socket_stru.sin_port = htons(port);
    socket_stru.sin_addr.s_addr = ip;

    int actual_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); // line 29
...

I feel like my code somehow doesn't "read" the sys/socket.h file so it doesn't know about socket_stru and IPPROTO_TCP, but I'm just really lost.

Any ideas?

selbie
  • 100,020
  • 15
  • 103
  • 173
coconut
  • 1,074
  • 4
  • 12
  • 30

5 Answers5

12

None of the other answers worked for me. After taking a look inside the sys/socket.h file, I didn't even see a definition for struct sockaddr_in.

What worked for me was to #include one of the following files when using the corresponding struct sockaddr_* type:

  • if you're using struct sockaddr_in, #include <netinet/in.h>
  • if you're using struct sockaddr_un, #include <sys/un.h>
  • if you're using struct sockaddr_ns, #include <netns/ns.h>
  • if you're using struct sockaddr_ndd, #include <sys/ndd_var.h>

More information on the header files for socket programming can be found here.

aanrv
  • 2,159
  • 5
  • 25
  • 37
8

I cut and paste your code into a file (removing only the #include f.h and closed off the function call.) It compiles just fine on Linux.

I suspect there may be header files differences on BSD. For socket programming, I typically include ALL these header files. And I know my socket code compiles on BSD as well. I suspect one of these header files brings in the definition for sockaddr_in. I recall when I ported by socket code to BSD, I had to explicitly add a few of these.

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>
#include <memory.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <stdarg.h>
/* the next two includes probably aren't relevant for you, but I typically use them all anyway */
#include <math.h>
#include <sys/termios.h>

Hope this helps

selbie
  • 100,020
  • 15
  • 103
  • 173
  • Wow. It compiles with all these headers! Now off to take them out one by one until I know which one it is. Thanks!! – coconut Apr 22 '13 at 19:11
  • 2
    It's `#include `. More details are given here: http://www.cas.mcmaster.ca/~qiao/courses/cs3mh3/tutorials/socket.html – bhakku Apr 10 '17 at 02:49
5

I had the same problem, but the following include fixed the issue for me

#include <arpa/inet.h>
slmyers
  • 767
  • 1
  • 8
  • 21
4

Just add #include <resolv.h> to your source and you are good to go.

jetman
  • 41
  • 1
2

According to freebsd developer's handbook you need

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
pmg
  • 106,608
  • 13
  • 126
  • 198