域名,ip相互轉換(Linux,getaddrinfo, getnameinfo)

參考連接:socket

http://en.wikipedia.org/wiki/Getaddrinfo.net

代碼:code

 

/* ip_to_hostname ip */
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
        if (argc != 2)
        {
                fprintf(stderr, "Usage: %s hostname\n", argv[0]);
                exit(EXIT_FAILURE);
        }
        struct addrinfo hints;
        struct addrinfo *result, *result_pointer;
        int ret;
        /* obtaining address matching host */
        memset(&hints, 0, sizeof(struct addrinfo));
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_flags = AI_CANONNAME | AI_NUMERICHOST;
        hints.ai_protocol = 0;  /* any protocol */

//      ret = getaddrinfo(argv[1], NULL, &hints, &result);                                                                                                             
        ret = getaddrinfo(argv[1], NULL, &hints, &result);
        if (ret != 0)
        {
                fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
                exit(EXIT_FAILURE);
        }
        /* traverse the returned list and output the ip addresses */
        for (result_pointer = result; result_pointer != NULL; result_pointer = result_pointer->ai_next)
        {
                char hostname[1025] = "";
                ret = getnameinfo(result_pointer->ai_addr, result_pointer->ai_addrlen, hostname, sizeof(hostname), NULL, 0, NI_NAMEREQD);
                if (ret != 0)
                {
                        fprintf(stderr, "error in getnameinfo: %s \n", gai_strerror(ret));
                }
                else
                {
                        printf("hostname: %s \n", hostname);
                }
//              printf("hostname: %s \n", result_pointer->ai_canonname);                                                                                               
        }
        freeaddrinfo(result);
        exit(EXIT_SUCCESS);
}

 

 

/* hostname_to_ip hostname */
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
        if (argc != 2)
        {
                fprintf(stderr, "Usage: %s hostname\n", argv[0]);
                exit(EXIT_FAILURE);
        }
        struct addrinfo hints;
        struct addrinfo *result, *result_pointer;
        int ret;
        /* obtaining address matching host */
        memset(&hints, 0, sizeof(struct addrinfo));
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_flags = AI_CANONNAME;
        hints.ai_protocol = 0;  /* any protocol */

//      ret = getaddrinfo(argv[1], NULL, &hints, &result);                                                                                                             
        ret = getaddrinfo(argv[1], NULL, &hints, &result);
        if (ret != 0)
        {
                fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
                exit(EXIT_FAILURE);
        }
        /* traverse the returned list and output the ip addresses */
        for (result_pointer = result; result_pointer != NULL; result_pointer = result_pointer->ai_next)
        {
                char hostname[1025] = "";
                ret = getnameinfo(result_pointer->ai_addr, result_pointer->ai_addrlen, hostname, sizeof(hostname), NULL, 0, NI_NUMERICHOST);
                if (ret != 0)
                {
                        fprintf(stderr, "error in getnameinfo: %s \n", gai_strerror(ret));
                        continue;
                }
                else
                {
                        printf("IP: %s \n", hostname);
                }
        }
        freeaddrinfo(result);
        exit(EXIT_SUCCESS);
}

 

結果:ip

root@localhost :/home/James/mypro/Linux-Pro/Network# ./hostname_to_ip baidu.com
IP: 123.125.114.144
IP: 220.181.111.85
IP: 220.181.111.86
root@localhost :/home/James/mypro/Linux-Pro/Network# ./ip_to_hostname 220.181.111.86
error in getnameinfo: Name or service not known
root@localhost :/home/James/mypro/Linux-Pro/Network# ./ip_to_hostname 10.0.0.78
hostname: localhost get

root@localhost:/home/James/mypro/Linux-Pro/Network# ./ip_to_hostname 59.66.137.62
hostname: th137062.ip.tsinghua.edu.cn
root@localhost:/home/James/mypro/Linux-Pro/Network# ./ip_to_hostname 202.106.182.229
hostname: mail182-229.sinamail.sina.com.cn
root@localhost:/home/James/mypro/Linux-Pro/Network# ./ip_to_hostname 211.147.4.7
hostname: mail3.douban.comstring

相關文章
相關標籤/搜索