1.简介
whois:用于查询域名是否被注册,以及注册域名的详细信息
a.例如查百度的域名信息
-
cong@msi:/work/busybox$ ./_install/busybox whois -h whois.dns.com.cn www.baidu.com
-
-
Whois Server Version 2.0
-
-
Domain names in the .com and .net domains can now be registered
-
with many different competing registrars. Go to http://www.internic.net
-
for detailed information.
-
-
Server Name: WWW.BAIDU.COM.CN
-
Registrar: XIN NET TECHNOLOGY CORPORATION -->Registrar说明己被注册
-
Whois Server: whois.paycenter.com.cn
-
Referral URL: http://www.xinnet.com
-
-
>>> Last update of whois database: Thu, 16 Jul 2015 02:53:39 GMT <<<
-
...一堆NOTICE信息,不关注
b.再随便查一个
-
cong@msi:/work/test/busytest/4whois$ ./whois www.tnnd.com
-
whois.c:str2sockaddr[34]: inaddr=119.254.72.20
-
whois.c:str2sockaddr[36]: inaddr=119.254.72.20
-
whois.c:main[89]: inaddr=119.254.72.20
-
-
Whois Server Version 2.0
-
-
Domain names in the .com and .net domains can now be registered
-
with many different competing registrars. Go to http://www.internic.net
-
for detailed information.
-
-
No match for "". -->no match 说明没有注册
2. 源码
whois的原理就是向域名服务器发送一个命令,然后将回应的信息打印出来。
a.下面的代码将域名服务器写死了,固定为
"whois.dns.com.cn"
b. 向域名服务器发送的命令就是要查的域名,比如查百度,就将"\r\n"发送即可
c. 最后将收到的信息打印
-
cong@msi:/work/test/busytest/4whois$ cat whois.c
-
#include "utils.h"
-
-
int str2sockaddr(const char *host, struct sockaddr_in* servaddr)
-
{
-
struct in_addr inaddr;
-
struct addrinfo hint, *result, *used_res;
-
int ret;
-
//1.convert numbers-and-dots notation into binary
-
if(inet_aton(host, &inaddr) != 0) //a.把点分十进制字符串转为socket的格式
-
{
-
//servaddr->sin_addr = inaddr;
-
memcpy(&servaddr->sin_addr, &inaddr, sizeof(inaddr));
-
dbmsg("inaddr=%s", inet_ntoa(servaddr->sin_addr));
-
return 0;
-
}
-
//2.convert www.baidu.com to binary
-
memset(&hint, 0, sizeof(hint));
-
hint.ai_family = AF_UNSPEC;
-
hint.ai_socktype = SOCK_STREAM;
-
hint.ai_flags = AI_CANONNAME; //b.如果上述a不成功,则把字符串域名串转为socket的格式
-
ret = getaddrinfo(host, NULL, &hint, &result);
-
if(ret || !result)
-
{
-
dbmsg("%s",strerror(errno));
-
return -1;
-
}
-
used_res = result;
-
-
do{
-
if(used_res->ai_family == AF_INET)
-
break;
-
}while( (used_res= used_res->ai_next) != NULL);
-
-
dbmsg("inaddr=%s", inet_ntoa(((struct sockaddr_in*)used_res->ai_addr)->sin_addr));
-
memcpy(servaddr, used_res->ai_addr, used_res->ai_addrlen);
-
dbmsg("inaddr=%s", inet_ntoa(servaddr->sin_addr));
-
-
freeaddrinfo(result);
-
return 0;
-
}
-
-
int pipe_out(int fd)
-
{
-
FILE *fp;
-
char buf[1024];
-
fp = fdopen(fd, "r");
-
if(!fp)
-
{
-
dbmsg("%s",strerror(errno));
-
return -1;
-
}
-
-
while (fgets(buf, sizeof(buf), fp)) {
-
char *p = strpbrk(buf, "\r\n");
-
if (p)
-
*p = '\0';
-
puts(buf);
-
}
-
return 0;
-
}
-
-
int main ( int argc, char *argv[] )
-
{
-
int sockfd;
-
int port = 43;
-
int ret;
-
const char* host = "whois.dns.com.cn";
-
char cmdbuf[1024];
-
//const char* host = "119.254.72.20";
-
struct sockaddr_in servaddr;
-
if(argc < 2)
-
{
-
dbmsg("usage: arping ");
-
dbmsg("eg: ./arping ");
-
return -1;
-
}
-
//1. create socket
-
if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
-
{
-
dbmsg("%s",strerror(errno));
-
return -1;
-
}
-
//2. set reuse addr
-
ret = 1;
-
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &ret, sizeof(int));
-
-
//3.1 turn string host to socket host
-
str2sockaddr(host, &servaddr);
-
dbmsg("inaddr=%s", inet_ntoa(servaddr.sin_addr));
-
//3.2 connect to server
-
servaddr.sin_family =AF_INET;
-
servaddr.sin_port = htons(port);
-
ret = connect(sockfd, (const struct sockaddr*)&servaddr, sizeof(servaddr));
-
if(ret<0)
-
{
-
dbmsg("ret=%d,err=%s", ret, strerror(errno));
-
return -1;
-
}
-
//4.send cmd to server
-
snprintf(cmdbuf, sizeof(cmdbuf), "%s\r\n", argv[1]);
-
write(sockfd, cmdbuf, strlen(cmdbuf)+1);
-
-
//5.get info from server
-
pipe_out(sockfd);
-
return EXIT_SUCCESS;
-
}
注: str2sockaddr即可把点分十进制字符串
119
.254
.72
.20转为sockaddr_in
也可以将
whois.dns.com.cn这将的转为sockaddr_in
3.运行结果
-
cong@msi:/work/test/busytest/4whois$ ./whois www.baidu.com
-
whois.c:str2sockaddr[34]: inaddr=119.254.72.20
-
whois.c:str2sockaddr[36]: inaddr=119.254.72.20
-
whois.c:main[89]: inaddr=119.254.72.20
-
-
Whois Server Version 2.0
-
-
Domain names in the .com and .net domains can now be registered
-
with many different competing registrars. Go to http://www.internic.net
-
for detailed information.
-
-
Server Name: WWW.BAIDU.COM.CN
-
Registrar: XIN NET TECHNOLOGY CORPORATION
-
Whois Server: whois.paycenter.com.cn
-
Referral URL: http://www.xinnet.com
-
-
>>> Last update of whois database: Thu, 16 Jul 2015 08:52:26 GMT <<<
-
...一堆NOTICE信息,不关注
4. 代码包
4whois.rar (下载后改名为4whois.tar.gz)
阅读(1323) | 评论(0) | 转发(0) |