0

There are similar questions for that but I can't find the answer for which I'm looking.

#include <netdb.h>

int getaddrinfo (const char *hostname, 
                 const char *service, 
                 const struct addrinfo *hints, 
                 struct addrinfo **result) ;

Returns: 0 if OK, nonzero on error 

What I wonder is that why we do need the iteration over the linked-list pointed to by result even if we define hints instead of it being NULL. We already know the IP(hostname) and the PORT(service). Don't we?

If the iteration is needed, could you exemplify simply by considering a guy not having full-filled network knowledge?

For example, I define hints as following.

struct addrinfo  hints;

        memset(&hints, 0, sizeof hints);
        hints.ai_family = AF_UNSPEC;        /* IPv4 or IPv6 */
        hints.ai_socktype = SOCK_STREAM;    /* TCP */
        hints.ai_flags = 0;
        hints.ai_protocol = 0;
  • 4
    `AF_UNSPEC` means you can get at least two results back; one for IPv4, and one for IPv6. Also, each interface could have more than one IP address (multiple A records), then you could get even more records back. – jww May 13 '19 at 19:41
  • You can avoid iterating if you're satisfied with the first result, but you should not neglect to check that result before accepting it. – John Bollinger May 13 '19 at 19:56
  • @jww for the latter case, do we assume _hostname_ parameter is alphabetical-name(e.g. google.com) instead of numerical-IP-address? For example, on terminal _dig A www.google.com_ yields one A record but yahoo.com 6 A records. **But let's assume we have already passed the numerical IP address instead of alphabetical(e.g. yahoo.com) to _getaddr()_ function, doesn't it limit itself with the IP address?** I still don't get why iterating through though for the example case. Or do we need the iteration at all for this example? – Soner from The Ottoman Empire May 13 '19 at 22:21
  • @snr, if you already had the numerical address, then why would you call `getaddrinfo`? It is a *name resolution* interface. – John Bollinger May 13 '19 at 22:37
  • @John Bollingerr , sir, isn't the function is to both usage? name and numeric. Moreover, Kerrisk says it is the contemproray approach for sockets. ?? – Soner from The Ottoman Empire May 13 '19 at 22:45
  • @snr, it is likely that you can get a useful result from feeding a numeric address to `getaddrinfo()`, but if you know in advance that the address is numeric then it is more efficient to avoid involving the resolver library. And if you don't know in advance, then the question is moot, because you cannot then distinguish separate numeric and non-numeric cases. – John Bollinger May 14 '19 at 00:19
  • @JohnBollinger even if you already know the IP, the function is still useful for parsing the IP and preparing the necessary `sockaddr` struct for you. – Remy Lebeau May 14 '19 at 03:08

1 Answers1

0

In the old days, prior to IPv6 becoming a thing you could assume that a hostname could reasonablly be resolved to a single IP address.

Nowadays that is simply not the case, getaddrinfo will return both ipv4 and ipv6 addresses and you really need to try both of them. If you are sure your users will be on good networks or your users can tolerate long delays it may be sufficient to try "connect" on them in sequence, but if you need to establish connections rapidly on potentially broken networks then it may prove nessacery to implement a more elaborate process (search for "happy eyeballs").

plugwash
  • 9,724
  • 2
  • 38
  • 51