http://www.binarytides.com/c-code-to-perform-ip-whois/
http://www.oschina.net/code/snippet_12_9390
/*
* @brief
* Whois client program
*
* @details
* This program shall fetch whois data for a IPv4 address.
*
* @author Silver Moon ( m00n.silv3r@gmail.com )
* */
#include<stdio.h> //scanf , printf
#include<string.h> //strtok
#include<stdlib.h> //realloc
#include<sys/socket.h> //socket
#include<netinet/in.h> //sockaddr_in
#include<arpa/inet.h> //getsockname
#include<netdb.h> //hostent
#include<unistd.h> //close
int
main(
int
argc ,
char
*argv[])
{
char
ip[100] , *data = NULL;
printf
(
"Enter ip address to whois : "
);
scanf
(
"%s"
, ip);
get_whois(ip , &data);
printf
(
"\n\n"
);
puts
(data);
free
(data);
return
0;
}
/**
Get the whois content of an ip
by selecting the correct server
*/
void
get_whois(
char
*ip ,
char
**data)
{
char
*wch = NULL, *pch , *response = NULL;
if
(whois_query(
"whois.iana.org"
, ip , &response))
{
printf
(
"Whois query failed"
);
}
pch =
strtok
(response ,
"\n"
);
while
(pch != NULL)
{
//Check if whois line
wch =
strstr
(pch ,
"whois."
);
if
(wch != NULL)
{
break
;
}
//Next line please
pch =
strtok
(NULL ,
"\n"
);
}
if
(wch != NULL)
{
printf
(
"\nWhois server is : %s"
, wch);
whois_query(wch , ip , data);
}
else
{
*data =
malloc
(100);
strcpy
(*data ,
"No whois data"
);
}
return
;
}
/*
* Perform a whois query to a server and record the response
* */
int
whois_query(
char
*server ,
char
*query ,
char
**response)
{
char
ip[32] , message[100] , buffer[1500];
int
sock , read_size , total_size = 0;
struct
sockaddr_in dest;
sock = socket(AF_INET , SOCK_STREAM , IPPROTO_TCP);
//Prepare connection structures :)
memset
( &dest , 0 ,
sizeof
(dest) );
dest.sin_family = AF_INET;
printf
(
"\nResolving %s..."
, server);
if
(hostname_to_ip(server , ip))
{
printf
(
"Failed"
);
return
1;
}
printf
(
"%s"
, ip);
dest.sin_addr.s_addr = inet_addr( ip );
dest.sin_port = htons( 43 );
//Now connect to remote server
if
(connect( sock , (
const
struct
sockaddr*) &dest ,
sizeof
(dest) ) < 0)
{
perror
(
"connect failed"
);
}
//Now send some data or message
printf
(
"\nQuerying for ... %s ..."
, query);
sprintf
(message ,
"%s\r\n"
, query);
if
( send(sock , message ,
strlen
(message) , 0) < 0)
{
perror
(
"send failed"
);
}
//Now receive the response
while
( (read_size = recv(sock , buffer ,
sizeof
(buffer) , 0) ) )
{
*response =
realloc
(*response , read_size + total_size);
if
(*response == NULL)
{
printf
(
"realloc failed"
);
}
memcpy
(*response + total_size , buffer , read_size);
total_size += read_size;
}
printf
(
"Done"
);
fflush
(stdout);
*response =
realloc
(*response , total_size + 1);
*(*response + total_size) =
'\0'
;
close(sock);
return
0;
}
/*
* @brief
* Get the ip address of a given hostname
*
* */
int
hostname_to_ip(
char
* hostname ,
char
* ip)
{
struct
hostent *he;
struct
in_addr **addr_list;
int
i;
if
( (he = gethostbyname( hostname ) ) == NULL)
{
// get the host info
herror(
"gethostbyname"
);
return
1;
}
addr_list = (
struct
in_addr **) he->h_addr_list;
for
(i = 0; addr_list[i] != NULL; i++)
{
//Return the first one;
strcpy
(ip , inet_ntoa(*addr_list[i]) );
return
0;
}
return
0;
}
相关推荐
全球 WHOIS 查询 是一网页接口的域名及 IP 地址 WHOIS 信息查询工具。... 域名信息, 网名信息, 网址信息, whois 信息查询, .tw 域名查询, .hk 域名查询, .cn 域名查询, .com 域名查询, IDN 域名查询, 多语言域名查询
whoiscl是windows下的whois查询小工具; whoiscl是windows下的whois查询小工具
在线WHOIS查询工具简介 1、采用BootStrap V3框架 2、能够查询COM、ORG、NET常规域名后缀WHOIS信息 3、适合用于个人网站常用工具 在线域名WHOIS查询工具(PHP版)页面展示 相关阅读 同类推荐:站长常用源码
C# 实现域名WhoIs 查询,WinForm实现
whois查询经典源码,不用多说哦,整理得不错的!
whois 域名信息查询 whois域名信息查询 whois域名信息查询
快速查询网站whois 信息,网站备案信息,网站域名状态信息实时查询
whois,whois查询PHP源码,简单实用
使用AJAX进行WhoIs查询
今天整理了这个比较简单的PHP域名WHOIS查询小工具,源码比较简单,顺带放出来以便同样学习脚本的朋友一起研究,以及我们也可以把该工具上传到自己的网站中,作为工具集的一部分。使用方法很简单:1、上传WHOIS文件夹...
php的whois查询,以及相应的数据处理
C# 网站Whois查询源码.rar CrAcK8_编程小组[K.8] QQ吻
下载解压后,将解压出来的whois.exe放置到 C:/Windows/system32/ 目录下即可(windows vista、7、8 在向这个目录放置文件时,需要管理员权限,确认即可)。以后凡是查询域名,只要打开CMD命令行,输入:whois xxx....
ASP网站整站程序PHP源码_PHP+AJAX的whois查询程序_whois整站打包程序_毕设源码实例.zipASP网站整站程序PHP源码_PHP+AJAX的whois查询程序_whois整站打包程序_毕设源码实例.zipASP网站整站程序PHP源码_PHP+AJAX的whois...
自己写的Whois查询工具,可以添加服务器,查询IP地址和域名信息
PHP+AJAX的whois查询程序_whoisPHP+AJAX的whois查询程序_whoisPHP+AJAX的whois查询程序_whoisPHP+AJAX的whois查询程序_whoisPHP+AJAX的whois查询程序_whoisPHP+AJAX的whois查询程序_whoisPHP+AJAX的whois查询程序_...
ASP源码—想念查询网IP地址Whois查询接口.zip
WHOIS查询检索,域名信息查询工具软件
qqnc.cc域名WHOIS查询系统 后期版本展望: 记录网友最新查询的10个域名
PHP脚本搭建的在线WHOIS域名查询工具。具备以下几个优势: 第一,没有使用数据库。因为作为个人站长来说,数据库的工具会导致空间增加,一般站长的数据库都比较小,简单的工具不需要这么复杂。 第二,安装容易。就...