How does getaddrinfo() do DNS lookup?
c, networking, unix
Solution
The short answer is "it asks the system", which in turn knows how to do DNS lookups and which servers to use.
`getaddrinfo()` is documented by the `getaddrinfo(3)` manual page, which means it's a C library function. It is also a POSIX function, so there is no canonical "source"; each standard C library of an operating system that conforms to POSIX will implement its own version. Either way the source to just that function is probably not too enlightening, since it would just call other functions and OS APIs, and you'd have to drill down pretty far to get to the actual DNS mechanism. You'd be better off reading documentation of the DNS protocol itself if you're interested in how that works.
Problem
`getaddrinfo()` is a function we need to use before creating a socket and connecting (i.e. call `socket()` and `connect()`). How does `getaddrinfo()` communicate with DNS server in the first place? Where can I see the complete source of `getaddrinfo()`?