Johanne Irish
Johanne Irish

Reputation: 1063

What is sa_family_t

I'm following Beej's Guide to Network Programming, and I'm using VC++ 2010, but when I copy paste the structs into my program, some of the types come up as incorrect identifiers. For example:

u_int32_t came up as that, and after some searching I found out those are old types from the C language circa 1999. I could have just included stdint.h, but that would require me to remember what they meant. Instead I used the standard int, which is 32 bits long (4 bytes), and for the other ones which are 64 bits long (8 bytes), I used long long int.

Anyways, I'm narrowing down to my last syntax error and it says sa_family_t is an invalid indentifier. I don't have a clue what its supposed to be and searching has turned up nothing. That's my problem, I don't know what I should specify for a type identifier for that.

Another thing that's bothering me is this: char __ss_pad1[_SS_PAD1SIZE]; The SS_PAD1SIZE thing comes up in red as invalid too.

Upvotes: 5

Views: 22392

Answers (2)

Ryan
Ryan

Reputation: 2065

In case anyone arrives at this answer and is looking for sa_family_t; under Linux, it is defined here:

https://github.com/torvalds/linux/blob/1a5304fecee523060f26e2778d9d8e33c0562df3/include/linux/socket.h#L28

typedef __kernel_sa_family_t    sa_family_t;

In turn, __kernel_sa_family_t is defined here:

https://github.com/torvalds/linux/blob/1a5304fecee523060f26e2778d9d8e33c0562df3/include/uapi/linux/socket.h#L10

typedef unsigned short __kernel_sa_family_t;

So ultimately, sa_family_t is an unsigned short, i.e. 2 bytes. This brings the entire struct sockaddr to a neat 16 bytes:

struct sockaddr {
    sa_family_t sa_family;  // unsigned short
    char        sa_data[14];

Upvotes: 5

Carey Gregory
Carey Gregory

Reputation: 6846

sa_family_t should be an unsigned integer. The Windows header files don't conform to that standard. Winsock.h defines the sockaddr struct as follows:

struct sockaddr {
        u_short sa_family;              /* address family */
        char    sa_data[14];            /* up to 14 bytes of direct address */
};

So to compile your code you're going to need to typedef sa_family_t yourself.

Upvotes: 8

Related Questions