Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1922695
  • 博文数量: 505
  • 博客积分: 1552
  • 博客等级: 上尉
  • 技术积分: 2524
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-23 18:24
文章分类

全部博文(505)

文章存档

2019年(12)

2018年(15)

2017年(1)

2016年(17)

2015年(14)

2014年(93)

2013年(233)

2012年(108)

2011年(1)

2009年(11)

分类: LINUX

2015-07-21 21:11:50

参考链接:

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

代码:


点击(此处)折叠或打开

  1. /* ip_to_hostname ip */
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netdb.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.         if (argc != 2)
  13.         {
  14.                 fprintf(stderr, "Usage: %s hostname\n", argv[0]);
  15.                 exit(EXIT_FAILURE);
  16.         }
  17.         struct addrinfo hints;
  18.         struct addrinfo *result, *result_pointer;
  19.         int ret;
  20.         /* obtaining address matching host */
  21.         memset(&hints, 0, sizeof(struct addrinfo));
  22.         hints.ai_family = AF_UNSPEC;
  23.         hints.ai_socktype = SOCK_STREAM;
  24.         hints.ai_flags = AI_CANONNAME | AI_NUMERICHOST;
  25.         hints.ai_protocol = 0; /* any protocol */
  26.  
  27. // ret = getaddrinfo(argv[1], NULL, &hints, &result);
  28.         ret = getaddrinfo(argv[1], NULL, &hints, &result);
  29.         if (ret != 0)
  30.         {
  31.                 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
  32.                 exit(EXIT_FAILURE);
  33.         }
  34.         /* traverse the returned list and output the ip addresses */
  35.         for (result_pointer = result; result_pointer != NULL; result_pointer = result_pointer->ai_next)
  36.         {
  37.                 char hostname[1025] = "";
  38.                 ret = getnameinfo(result_pointer->ai_addr, result_pointer->ai_addrlen, hostname, sizeof(hostname), NULL, 0, NI_NAMEREQD);
  39.                 if (ret != 0)
  40.                 {
  41.                         fprintf(stderr, "error in getnameinfo: %s \n", gai_strerror(ret));
  42.                 }
  43.                 else
  44.                 {
  45.                         printf("hostname: %s \n", hostname);
  46.                 }
  47. // printf("hostname: %s \n", result_pointer->ai_canonname);
  48.         }
  49.         freeaddrinfo(result);
  50.         exit(EXIT_SUCCESS);
  51. }


点击(此处)折叠或打开

  1. /* hostname_to_ip hostname */
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netdb.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.         if (argc != 2)
  13.         {
  14.                 fprintf(stderr, "Usage: %s hostname\n", argv[0]);
  15.                 exit(EXIT_FAILURE);
  16.         }
  17.         struct addrinfo hints;
  18.         struct addrinfo *result, *result_pointer;
  19.         int ret;
  20.         /* obtaining address matching host */
  21.         memset(&hints, 0, sizeof(struct addrinfo));
  22.         hints.ai_family = AF_UNSPEC;
  23.         hints.ai_socktype = SOCK_STREAM;
  24.         hints.ai_flags = AI_CANONNAME;
  25.         hints.ai_protocol = 0; /* any protocol */
  26.  
  27. // ret = getaddrinfo(argv[1], NULL, &hints, &result);
  28.         ret = getaddrinfo(argv[1], NULL, &hints, &result);
  29.         if (ret != 0)
  30.         {
  31.                 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
  32.                 exit(EXIT_FAILURE);
  33.         }
  34.         /* traverse the returned list and output the ip addresses */
  35.         for (result_pointer = result; result_pointer != NULL; result_pointer = result_pointer->ai_next)
  36.         {
  37.                 char hostname[1025] = "";
  38.                 ret = getnameinfo(result_pointer->ai_addr, result_pointer->ai_addrlen, hostname, sizeof(hostname), NULL, 0, NI_NUMERICHOST);
  39.                 if (ret != 0)
  40.                 {
  41.                         fprintf(stderr, "error in getnameinfo: %s \n", gai_strerror(ret));
  42.                         continue;
  43.                 }
  44.                 else
  45.                 {
  46.                         printf("IP: %s \n", hostname);
  47.                 }
  48.         }
  49.         freeaddrinfo(result);
  50.         exit(EXIT_SUCCESS);
  51. }

结果:

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

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.com

阅读(1208) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册