int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
The actual structure passed to addr will depend on the address family. The sockaddr structure is defined as something like:
struct sockaddr { sa_family_t sa_family; char sa_data[14]; }
So, for the IPv4 address (AF_INET), the actual structure to be transmitted is this:
struct sockaddr_in { sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; }; struct in_addr { uint32_t s_addr; };
Does the binding code check the value of sockaddr.sa_family and depending on the value found, it then turns the sockaddr structure into an appropriate structure, such as sockaddr_in ?
Why is sa_data set to 14 characters? If I understand correctly, the sa_data field is just a field that will have a large enough space to store all types of address families? Presumably, the original designers expected 14 characters to be wide enough to fit all future types.
source share