struct addrinfo
is returned by getaddrinfo()
, and contains, on success, a linked list of such struct
s for a specified hostname and/or service.
The ai_addr
member isn’t actually a struct sockaddr
, because that struct
is merely a generic one that contains common members for all the others, and is used in order to determine what type of struct you actually have. Depending upon what you pass to getaddrinfo()
, and what that function found out, ai_addr
might actually be a pointer to struct sockaddr_in
, or struct sockaddr_in6
, or whatever else, depending upon what is appropriate for that particular address entry. This is one good reason why they’re kept “separate”, because that member might point to one of a bunch of different types of struct
s, which it couldn’t do if you tried to hardcode all the members into struct addrinfo
, because those different struct
s have different members.
This is probably the easiest way to get this information if you have a hostname, but it’s not the only way. For an IPv4 connection, you can just populate a struct sockaddr_in
structure yourself, if you want to and you have the data to do so, and avoid going through the rigamarole of calling getaddrinfo()
, which you might have to wait for if it needs to go out into the internet to collect the information for you. You don’t have to use struct addrinfo
at all.