2
votes

The Linux provided header file "/usr/include/linux/socket.h" contains definitions for Supported address families and Protocol families:

/* Supported address families. */
#define AF_UNSPEC       0
....

/* Protocol families, same as address families. */
#define PF_UNSPEC       AF_UNSPEC
...

But why does it not define socket types?

I can find it's definition in "/usr/include/bits/socket.h" as

enum sock_type {
    SOCK_DGRAM  = 1,
    SOCK_STREAM = 2,
    SOCK_RAW    = 3,
    SOCK_RDM    = 4,
    SOCK_SEQPACKET  = 5,
    SOCK_DCCP   = 6,
    SOCK_PACKET = 10,
};

I wonder why these are not defined in the header file provided by Linux?

1
how do you compiling just gcc filename.c not enoughNagaraju Badaeni
I am calling Linux system calls directly. I noticed that SOCK_DGRAM is not defined in Linux provided header file. Wanted to know why is it not defined in Linux header files.Dew Kumar
As Alnitak says, by POSIX the header is <sys/socket.h> and if you #include that you will have access to the correct defines. DO NOT just look them up and use the numbers.CodeClown42
That's correct. Macro name may or may not be same as POSIX defined macros. However, those numbers for socket type should be defined in any header files shipped with Linux. Isn't it?Dew Kumar
If you look at "/usr/src/kernels/<Linux version>/include/linux/net.h" file, you'll see the definition for SOCK_DGRAM etc. I wonder why is it not defined in the header files shipped with Linux?Dew Kumar

1 Answers

5
votes

User space programs should be using:

#include <sys/socket.h>

NB: sys, not linux.

This will then #include the appropriate low level header files.

The fact that some definitions are in <bits/socket.h> and some in <linux/socket.h> is just an implementation detail.