I have been following along Beej's Guide to Network Programming and am having trouble understanding why we must fill in some of the fields of the struct addrinfo before calling getaddrinfo()? Also, why does it return a pointer to a list of multiple addrinfo structs? Since there is only one host, why are there multiple addresses?
-
Regarding the return part: it's a func that should work in any conditions so it should be general. A host might have multiple _NIC_ s. But even if it only has one, there may be multiple entries. Try `ipconfig -all` (under _Win_), or `ifconfig -a` (under _Linux_); each of the output records corresponds to one of `addrinfo` struct (so you'll almost always have more than 1). Regarding the 1st art you could read the `getaddrinfo` manuals: [Ux](http://man7.org/linux/man-pages/man3/getaddrinfo.3.html) or [Win](https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx). – CristiFati Sep 17 '16 at 22:59
-
" Since there is only one host" why do you say that? – xaxxon Sep 18 '16 at 02:27
1 Answers
why we must fill in some of the fields of the struct addrinfo before calling getaddrinfo()?
I assume you're asking about the hints
parameter. This parameter can be used to specify the type of response you want to get from getaddrinfo()
. For instance, whether you'd like an IPv4 only DNS lookup (AF_INET
) or an IPv6 one (AF_INET6
) From the man page:
The hints argument points to an addrinfo structure that specifies criteria for selecting the socket address structures returned in the list pointed to by res. If hints is not NULL it points to an addrinfo structure whose ai_family, ai_socktype, and ai_protocol specify criteria that limit the set of socket addresses returned by getaddrinfo(), as follows:
ai_family This field specifies the desired address family for the returned addresses. Valid values for this field include AF_INET and AF_INET6. The value AF_UNSPEC indicates that getaddrinfo() should return socket addresses for any address family (either IPv4 or IPv6, for example) that can be used with node and service.
ai_socktype This field specifies the preferred socket type, for example SOCK_STREAM or SOCK_DGRAM. Specifying 0 in this field indicates that socket addresses of any type can be returned by getaddrinfo().
ai_protocol This field specifies the protocol for the returned socket addresses. Specifying 0 in this field indicates that socket addresses with any protocol can be returned by getaddrinfo().
ai_flags This field specifies additional options, described below. Multiple flags are specified by bitwise OR-ing them together.
All the other fields in the structure pointed to by hints must contain either 0 or a null pointer, as appropriate.
why does it return a pointer to a list of multiple addrinfo structs? Since there is only one host, why are there multiple addresses?
Domain names are often resolved to more than one IP address.
For instance, for me, stackoverflow.com
, at the moment resolves to the following addresses:
$ nslookup stackoverflow.com
Non-authoritative answer:
Server: UnKnown
Address: 10.0.0.138
Name: stackoverflow.com
Addresses: 151.101.65.69
151.101.129.69
151.101.1.69
151.101.193.69
All of these addresses are stackoverflow.com