Why does `struct sockaddr` contain an address family field?

Why struct sockaddrdoes it contain an address family field? Is the address family already associated with the call socket()?

+5
source share
4 answers

sockaddris used in more places than just connectand bind, including places in which you do not have external knowledge of the corresponding address family getaddrinfo.

In addition, while I do not believe that the following is equivalent to practice anywhere, I see that it was in the eyes of the one who originally created this material: a call socket()defines a family of protocols. sockaddrcontains the address family. In practice, I believe that they are always the same, but theoretically you can have a protocol that can support two different types of addresses.

EDIT: Another way to use a parameter. If you are using datagram sockets (UDP) and you have a socket in the “connected” state with the default destination address, you can clear this address by calling connect()with sockaddrwith sa_familyset to AF_UNSPEC.

+4
source

getaddrinfo, ( ) sockaddr, , , .

, struct sockaddr , .

, sa_family - getsockname getpeername. , , , , .

+2

If you look at the network code 4.2BSD , where the socket interface came from, you will see what sockaddris passed to the network interface drivers, but the fd socket is not.

+1
source

The sa_family field is used to indicate what type of address will be in the sa_data field. In many applications, the address family is considered IPV4. However, many applications also support IPV6.

0
source

All Articles