Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2150583
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2015-07-20 10:12:46

1.简介
whois:用于查询域名是否被注册,以及注册域名的详细信息
a.例如查百度的域名信息
  1. cong@msi:/work/busybox$ ./_install/busybox whois -h whois.dns.com.cn www.baidu.com

  2. Whois Server Version 2.0

  3. Domain names in the .com and .net domains can now be registered
  4. with many different competing registrars. Go to http://www.internic.net
  5. for detailed information.

  6.    Server Name: WWW.BAIDU.COM.CN
  7.    Registrar: XIN NET TECHNOLOGY CORPORATION     -->Registrar说明己被注册
  8.    Whois Server: whois.paycenter.com.cn
  9.    Referral URL: http://www.xinnet.com

  10. >>> Last update of whois database: Thu, 16 Jul 2015 02:53:39 GMT <<<
  11. ...一堆NOTICE信息,不关注
b.再随便查一个
  1. cong@msi:/work/test/busytest/4whois$ ./whois www.tnnd.com
  2. whois.c:str2sockaddr[34]: inaddr=119.254.72.20
  3. whois.c:str2sockaddr[36]: inaddr=119.254.72.20
  4. whois.c:main[89]: inaddr=119.254.72.20

  5. Whois Server Version 2.0

  6. Domain names in the .com and .net domains can now be registered
  7. with many different competing registrars. Go to http://www.internic.net
  8. for detailed information.

  9. No match for "".                -->no match 说明没有注册
2. 源码
whois的原理就是向域名服务器发送一个命令,然后将回应的信息打印出来。
a.下面的代码将域名服务器写死了,固定为"whois.dns.com.cn"
b. 向域名服务器发送的命令就是要查的域名,比如查百度,就将"\r\n"发送即可
c. 最后将收到的信息打印
  1. cong@msi:/work/test/busytest/4whois$ cat whois.c
  2. #include "utils.h"

  3. int str2sockaddr(const char *host, struct sockaddr_in* servaddr)
  4. {
  5.     struct in_addr inaddr;
  6.     struct addrinfo hint, *result, *used_res;
  7.     int ret;
  8.     //1.convert numbers-and-dots notation into binary
  9.     if(inet_aton(host, &inaddr) != 0)    //a.把点分十进制字符串转为socket的格式
  10.     {
  11.         //servaddr->sin_addr = inaddr;
  12.         memcpy(&servaddr->sin_addr, &inaddr, sizeof(inaddr));
  13.         dbmsg("inaddr=%s", inet_ntoa(servaddr->sin_addr));
  14.         return 0;
  15.     }
  16.     //2.convert www.baidu.com to binary
  17.     memset(&hint, 0, sizeof(hint));
  18.     hint.ai_family = AF_UNSPEC;
  19.     hint.ai_socktype = SOCK_STREAM;
  20.     hint.ai_flags = AI_CANONNAME;   //b.如果上述a不成功,则把字符串域名串转为socket的格式
  21.     ret = getaddrinfo(host, NULL, &hint, &result);  
  22.     if(ret || !result)
  23.     {
  24.         dbmsg("%s",strerror(errno));
  25.         return -1;
  26.     }
  27.     used_res = result;
  28.     
  29.     do{
  30.         if(used_res->ai_family == AF_INET)
  31.             break;
  32.     }while( (used_res= used_res->ai_next) != NULL);

  33.     dbmsg("inaddr=%s", inet_ntoa(((struct sockaddr_in*)used_res->ai_addr)->sin_addr));
  34.     memcpy(servaddr, used_res->ai_addr, used_res->ai_addrlen);
  35.     dbmsg("inaddr=%s", inet_ntoa(servaddr->sin_addr));

  36.     freeaddrinfo(result);
  37.     return 0;
  38. }

  39. int pipe_out(int fd)
  40. {
  41.     FILE *fp;
  42.     char buf[1024];
  43.     fp = fdopen(fd, "r");
  44.     if(!fp)
  45.     {
  46.         dbmsg("%s",strerror(errno));
  47.         return -1;
  48.     }

  49.     while (fgets(buf, sizeof(buf), fp)) {
  50.         char *p = strpbrk(buf, "\r\n");
  51.         if (p)
  52.             *p = '\0';
  53.         puts(buf);
  54.     }
  55.     return 0;
  56. }

  57. int main ( int argc, char *argv[] )
  58. {
  59.     int sockfd;
  60.     int port = 43;
  61.     int ret;
  62.     const char* host = "whois.dns.com.cn";
  63.     char cmdbuf[1024];
  64.     //const char* host = "119.254.72.20";
  65.     struct sockaddr_in servaddr;
  66.     if(argc < 2)
  67.     {
  68.         dbmsg("usage: arping ");
  69.         dbmsg("eg: ./arping ");
  70.         return -1;
  71.     }
  72.     //1. create socket
  73.     if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  74.     {
  75.         dbmsg("%s",strerror(errno));
  76.         return -1;
  77.     }
  78.     //2. set reuse addr
  79.     ret = 1;
  80.     setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &ret, sizeof(int));

  81.     //3.1 turn string host to socket host
  82.     str2sockaddr(host, &servaddr);
  83.     dbmsg("inaddr=%s", inet_ntoa(servaddr.sin_addr));
  84.     //3.2 connect to server
  85.     servaddr.sin_family =AF_INET;
  86.     servaddr.sin_port = htons(port);
  87.     ret = connect(sockfd, (const struct sockaddr*)&servaddr, sizeof(servaddr));
  88.     if(ret<0)
  89.     {
  90.         dbmsg("ret=%d,err=%s", ret, strerror(errno));
  91.         return -1;
  92.     }
  93.     //4.send cmd to server
  94.     snprintf(cmdbuf, sizeof(cmdbuf), "%s\r\n", argv[1]);
  95.     write(sockfd, cmdbuf, strlen(cmdbuf)+1);

  96.     //5.get info from server
  97.     pipe_out(sockfd);
  98.     return EXIT_SUCCESS;
  99. }
注: str2sockaddr即可把点分十进制字符串119.254.72.20转为sockaddr_in
也可以将whois.dns.com.cn这将的转为sockaddr_in
3.运行结果
  1. cong@msi:/work/test/busytest/4whois$ ./whois www.baidu.com
  2. whois.c:str2sockaddr[34]: inaddr=119.254.72.20
  3. whois.c:str2sockaddr[36]: inaddr=119.254.72.20
  4. whois.c:main[89]: inaddr=119.254.72.20

  5. Whois Server Version 2.0

  6. Domain names in the .com and .net domains can now be registered
  7. with many different competing registrars. Go to http://www.internic.net
  8. for detailed information.

  9.    Server Name: WWW.BAIDU.COM.CN
  10.    Registrar: XIN NET TECHNOLOGY CORPORATION
  11.    Whois Server: whois.paycenter.com.cn
  12.    Referral URL: http://www.xinnet.com

  13. >>> Last update of whois database: Thu, 16 Jul 2015 08:52:26 GMT <<<
  14. ...一堆NOTICE信息,不关注
4. 代码包
4whois.rar (下载后改名为4whois.tar.gz)


阅读(1313) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~