I'm new to C and socket programming, still struggling in sockaddr_in
structure:
struct sockaddr_in {
short sin_family; /* Protocol family (always AF_INET) */
unsigned short sin_port; /* Port number in network byte order */
struct in_addr sin_addr; /* IP address in network byte order */
unsigned char sin_zero[8]; /* Pad to sizeof(struct sockaddr) */
};
and
struct in_addr {
uint32_t s_addr; /* Address in network byte order (big-endian) */
};
I'm not sure about the need of sin_zero
other than alignment requirement, below is my assumption, please correct me if I was wrong:
Assumption 1-since sin_addr
is 32 bit for ipv4 address, if we need a ipv6 address, the the first 4 bytes of sin_zero
will be assigned and combined with sin_addr
to form a 64 bit ipv6 address.
Assumption 2-If my assumption is correct, then is the reason why we need to specific the length of a socket address such as:
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
is because the possibility of ipv6 addresses? so if ipv4 is used, then addrlen
should be 8, and if ipv6 is used, then addrlen
should be 12?