gethostbyname, getaddrinfo 관련 한 재미있는 소스이다.

 

 

cat gethostbyname.c


#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

int main(int argc, char **argv)
{
    struct hostent     *myent;
    struct in_addr  myen;
    long int *add;

    printf ("%s\n", argv[1]);

    myent = gethostbyname(argv[1]);
    if (myent == NULL)
    {
        printf("ERROR : ");
        exit(0);
    }

    printf("%s\n", myent->h_name);

    while(*myent->h_addr_list != NULL)
    {
        add = (long int *)*myent->h_addr_list;
        myen.s_addr = *add;
        printf("%s\n", inet_ntoa(myen));
        myent->h_addr_list++;
    }
    return 0;
}
 

 

 

cat getaddr.c

#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>

int main(int argc, char **argv)
{
    struct addrinfo *res;
    char hostname[NI_MAXHOST];
    char addrstr[100];
    void *ptr;
    struct addrinfo hints;
    int error;

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


    printf("%s\n", argv[1]);
    if (error = getaddrinfo(argv[1], NULL, &hints, &res))
    //if (error = getaddrinfo(argv[1], NULL, NULL, &res))
    {
        printf("error using getaddrinfo: %s\n", gai_strerror(error));
    }

    if (res) {
        if (error = getnameinfo(res->ai_addr, sizeof(struct sockaddr), hostname, sizeof(hostname), NULL,0,0)) {
            printf("error using getnameinfo: %s\n", gai_strerror(error));
            return 0;
        }
    }


    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 ("getaddinfo test : IPv%d address: %s (%s)\n", res->ai_family == PF_INET6 ? 6 : 4, addrstr, res->ai_canonname);
      printf("%s(%s)\n", addrstr, res->ai_canonname);
      res = res->ai_next;
    }

    freeaddrinfo(res);

}

 

 

 

 

Posted by '김용환'
,