0

Please help me! I need to translate (or convert) domain names (for example google.com) to IP address. For that purpose I have found code on interet, which work perfectly, but I don't understand, why there is called function inet_ntop() two times. Please help me. Here is code:

/* 
 * getaddrinfo.c - Simple example of using getaddrinfo(3) function.
 * 
 * Michal Ludvig <michal@logix.cz> (c) 2002, 2003
 * http://www.logix.cz/michal/devel/
 *
 * License: public domain.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

int
lookup_host (const char *host)
{
  struct addrinfo hints, *res;
  int errcode;
  char addrstr[100];
  void *ptr;

  memset (&hints, 0, sizeof (hints));
  hints.ai_family = PF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags |= AI_CANONNAME;

  errcode = getaddrinfo (host, NULL, &hints, &res);
  if (errcode != 0)
    {
      perror ("getaddrinfo");
      return -1;
    }

  printf ("Host: %s\n", host);
  while (res)
    {
      inet_ntop (res->ai_family, res->ai_addr->sa_data, addrstr, 100);

      switch (res->ai_family)
        {
        case AF_INET:
          ptr = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
          break;
        case AF_INET6:
          ptr = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
          break;
        }
      inet_ntop (res->ai_family, ptr, addrstr, 100);
      printf ("IPv%d address: %s (%s)\n", res->ai_family == PF_INET6 ? 6 : 4,
              addrstr, res->ai_canonname);
      res = res->ai_next;
    }

  return 0;
}

int
main (int argc, char *argv[])
{
  if (argc < 2)
    exit (1);
  return lookup_host (argv[1]);
}

My question is - why there is function inet_ntop() used twice? I thought, that the first calling give you IP address...but why you need the second calling? Thanks!

Petr Marek
  • 59
  • 1
  • 7
  • I would avoid `inet_ntop()` and call `getnameinfo(res->ai_addr, res->ai_addrlen, addrstr, sizeof(addrstr), NULL, 0, NI_NUMERICHOST)`. It avoids the need for the `switch`-statement. – G. Sliepen Oct 07 '19 at 20:13

1 Answers1

1

The first call to inet_ntop isn't doing anything useful here. What it writes to addrstr is overwritten on the following call.

The author either didn't understand why they were calling it twice or left in test code they shouldn't have.

dbush
  • 205,898
  • 23
  • 218
  • 273