Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Type - socklen_t, sa_family_t

I'm building a simple socket web server using the sys/socket.h lib, and I came across the socklen_t and sa_family_t data types and am a bit confused on what their actual purpose is.

Definition:

  • sa_family_t - unsigned integer type.
  • socklen_t - an unsigned opaque integer type of length of at least 32-bits.

Now I understand that the <sys/socket> lib declares three structures (sockaddr,msghdr,cmsghdr) which contain members that declare these data types.

  • sa_family_t sa_family address family
  • socklen_t msg_namelen size of address
  • socklen_t msg_controllen ancillary data buffer len
  • socklen_t cmsg_len data byte count, including the cmsghdr

But why create new data types, why not just use an unsigned int data type?

like image 789
Jordan Davis Avatar asked Sep 15 '15 17:09

Jordan Davis


People also ask

What type is Socklen_t?

DESCRIPTION. <sys/socket. h> makes available a type, socklen_t, which is an unsigned opaque integral type of length of at least 32 bits.

What is Sa_family_t?

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 */ };

Where is Socklen_t defined?

DESCRIPTION. The <sys/socket. h> header shall define the type socklen_t, which is an integer type of width of at least 32 bits; see APPLICATION USAGE.


1 Answers

By declaring specific types for these fields, it decouples them from a particular representation like unsigned int.

Different architectures can be free to define different sizes for these fields, and code that uses these specific types doesn't need to worry about how big an int is on a given machine.

like image 156
dbush Avatar answered Oct 19 '22 10:10

dbush