A whois client is a program that will simply fetch the whois information for a domain/ip address from the whois servers. The code over here works according to the algorithm discussed here.
Code
6 |
* This program shall perform whois for a domain and get you the whois data of that domain
|
8 |
* @author Silver Moon ( m00n.silv3r@gmail.com )
|
11 |
#include<stdio.h> //scanf , printf |
12 |
#include<string.h> //strtok |
13 |
#include<stdlib.h> //realloc |
14 |
#include<sys/socket.h> //socket |
15 |
#include<netinet/in.h> //sockaddr_in |
16 |
#include<arpa/inet.h> //getsockname |
17 |
#include<netdb.h> //hostent |
18 |
#include<unistd.h> //close |
20 |
int get_whois_data( char * , char **);
|
21 |
int hostname_to_ip( char * , char *);
|
22 |
int whois_query( char * , char * , char **);
|
23 |
char *str_replace( char *search , char *replace , char *subject );
|
25 |
int main( int argc , char *argv[])
|
27 |
char domain[100] , *data = NULL;
|
29 |
printf ( "Enter domain name to whois : " );
|
32 |
get_whois_data(domain , &data);
|
39 |
* Get the whois data of a domain
|
41 |
int get_whois_data( char *domain , char **data)
|
43 |
char ext[1024] , *pch , *response = NULL , *response_2 = NULL , *wch , *dt;
|
46 |
domain = str_replace( "http://" , "" , domain);
|
47 |
domain = str_replace( "www." , "" , domain);
|
49 |
//get the extension , com , org , edu
|
53 |
printf ( "strdup failed" );
|
55 |
pch = ( char *) strtok (dt , "." );
|
59 |
pch = strtok (NULL , "." );
|
62 |
//This will tell the whois server for the particular TLD like com , org
|
63 |
if (whois_query( "whois.iana.org" , ext , &response))
|
65 |
printf ( "Whois query failed" );
|
68 |
//Now analysze the response :)
|
69 |
pch = strtok (response , "\n" );
|
73 |
wch = strstr (pch , "whois." );
|
80 |
pch = strtok (NULL , "\n" );
|
85 |
//Now we have the TLD whois server in wch , query again
|
86 |
//This will provide minimal whois information along with the parent whois server of the specific domain :)
|
88 |
//This should not be necessary , but segmentation fault without this , why ?
|
92 |
printf ( "\nTLD Whois server is : %s" , wch);
|
93 |
if (whois_query(wch , domain , &response))
|
95 |
printf ( "Whois query failed" );
|
100 |
printf ( "\nTLD whois server for %s not found" , ext);
|
104 |
response_2 = strdup(response);
|
106 |
//Again search for a whois server in this response. :)
|
107 |
pch = strtok (response , "\n" );
|
110 |
//Check if whois line
|
111 |
wch = strstr (pch , "whois." );
|
118 |
pch = strtok (NULL , "\n" );
|
123 |
* If a registrar whois server is found then query it
|
127 |
//Now we have the registrar whois server , this has the direct full information of the particular domain
|
128 |
//so lets query again
|
130 |
printf ( "\nRegistrar Whois server is : %s" , wch);
|
132 |
if (whois_query(wch , domain , &response))
|
134 |
printf ( "Whois query failed" );
|
137 |
printf ( "\n%s" , response);
|
141 |
* otherwise echo the output from the previous whois result
|
145 |
printf ( "%s" , response_2);
|
151 |
* Perform a whois query to a server and record the response
|
153 |
int whois_query( char *server , char *query , char **response)
|
155 |
char ip[32] , message[100] , buffer[1500];
|
156 |
int sock , read_size , total_size = 0;
|
157 |
struct sockaddr_in dest;
|
159 |
sock = socket(AF_INET , SOCK_STREAM , IPPROTO_TCP);
|
161 |
//Prepare connection structures :)
|
162 |
memset ( &dest , 0 , sizeof (dest) );
|
163 |
dest.sin_family = AF_INET;
|
165 |
printf ( "\nResolving %s..." , server);
|
166 |
if (hostname_to_ip(server , ip))
|
172 |
dest.sin_addr.s_addr = inet_addr( ip );
|
173 |
dest.sin_port = htons( 43 );
|
175 |
//Now connect to remote server
|
176 |
if (connect( sock , ( const struct sockaddr*) &dest , sizeof (dest) ) < 0)
|
178 |
perror ( "connect failed" );
|
181 |
//Now send some data or message
|
182 |
printf ( "\nQuerying for ... %s ..." , query);
|
183 |
sprintf (message , "%s\r\n" , query);
|
184 |
if ( send(sock , message , strlen (message) , 0) < 0)
|
186 |
perror ( "send failed" );
|
189 |
//Now receive the response
|
190 |
while ( (read_size = recv(sock , buffer , sizeof (buffer) , 0) ) )
|
192 |
*response = realloc (*response , read_size + total_size);
|
193 |
if (*response == NULL)
|
195 |
printf ( "realloc failed" );
|
197 |
memcpy (*response + total_size , buffer , read_size);
|
198 |
total_size += read_size;
|
203 |
*response = realloc (*response , total_size + 1);
|
204 |
*(*response + total_size) = '\0' ;
|
212 |
* Get the ip address of a given hostname
|
215 |
int hostname_to_ip( char * hostname , char * ip)
|
218 |
struct in_addr **addr_list;
|
221 |
if ( (he = gethostbyname( hostname ) ) == NULL)
|
224 |
herror( "gethostbyname" );
|
228 |
addr_list = ( struct in_addr **) he->h_addr_list;
|
230 |
for (i = 0; addr_list[i] != NULL; i++)
|
232 |
//Return the first one;
|
233 |
strcpy (ip , inet_ntoa(*addr_list[i]) );
|
241 |
* Search and replace a string with another string , in a string
|
243 |
char *str_replace( char *search , char *replace , char *subject)
|
245 |
char *p = NULL , *old = NULL , *new_subject = NULL ;
|
246 |
int c = 0 , search_size;
|
248 |
search_size = strlen (search);
|
250 |
//Count how many occurences
|
251 |
for (p = strstr (subject , search) ; p != NULL ; p = strstr (p + search_size , search))
|
257 |
c = ( strlen (replace) - search_size )*c + strlen (subject);
|
259 |
//New subject with new size
|
260 |
new_subject = malloc ( c );
|
263 |
strcpy (new_subject , "" );
|
268 |
for (p = strstr (subject , search) ; p != NULL ; p = strstr (p + search_size , search))
|
270 |
//move ahead and copy some text from original subject , from a certain position
|
271 |
strncpy (new_subject + strlen (new_subject) , old , p - old);
|
273 |
//move ahead and copy the replacement text
|
274 |
strcpy (new_subject + strlen (new_subject) , replace);
|
276 |
//The new start position after this search match
|
277 |
old = p + search_size;
|
280 |
//Copy the part after the last search match
|
281 |
strcpy (new_subject + strlen (new_subject) , old);
|
hostname_to_ip – This is a simple function to get an IP of a domain.
str_replace – This is a generic string processing function that is used to search for a string in another big string and replace it with another string.
Output
1 |
Enter domain name to whois : www.wikipedia.org |
3 |
Resolving whois.iana.org...192.0.47.59 |
4 |
Querying for ... org ...Done
|
5 |
TLD Whois server is : whois.pir.org |
6 |
Resolving whois.pir.org...149.17.192.7 |
7 |
Querying for ... wikipedia.org ...DoneAccess to .ORG WHOIS information is provided to assist persons in
|
8 |
determining the contents of a domain name registration record in the
|
9 |
Public Interest Registry registry database. The data in this record is provided by
|
10 |
Public Interest Registry for informational purposes only, and Public Interest Registry does not
|
11 |
guarantee its accuracy. This service is intended only for query-based
|
12 |
access. You agree that you will use this data only for lawful purposes
|
13 |
and that, under no circumstances will you use this data to: (a) allow, |
14 |
enable , or otherwise support the transmission by e-mail, telephone, or
|
15 |
facsimile of mass unsolicited, commercial advertising or solicitations |
16 |
to entities other than the data recipient's own existing customers; or |
17 |
(b) enable high volume, automated, electronic processes that send
|
18 |
queries or data to the systems of Registry Operator, a Registrar, or |
19 |
Afilias except as reasonably necessary to register domain names or |
20 |
modify existing registrations. All rights reserved. Public Interest Registry reserves |
21 |
the right to modify these terms at any time . By submitting this query,
|
22 |
you agree to abide by this policy. |
24 |
Domain ID:D51687756-LROR |
25 |
Domain Name:WIKIPEDIA.ORG |
26 |
Created On:13-Jan-2001 00:12:14 UTC |
27 |
Last Updated On:02-Dec-2009 20:57:17 UTC |
28 |
Expiration Date:13-Jan-2015 00:12:14 UTC |
29 |
Sponsoring Registrar:GoDaddy.com, Inc. (R91-LROR) |
30 |
Status:CLIENT DELETE PROHIBITED |
31 |
Status:CLIENT RENEW PROHIBITED |
32 |
Status:CLIENT TRANSFER PROHIBITED |
33 |
Status:CLIENT UPDATE PROHIBITED |
34 |
Registrant ID:CR31094073 |
35 |
Registrant Name:DNS Admin |
36 |
Registrant Organization:Wikimedia Foundation, Inc. |
37 |
Registrant Street1:149 New Montgomery Street |
38 |
Registrant Street2:Third Floor |
40 |
Registrant City:San Francisco |
41 |
Registrant State/Province:California |
42 |
Registrant Postal Code:94105 |
44 |
Registrant Phone:+1.4158396885 |
45 |
Registrant Phone Ext.: |
46 |
Registrant FAX:+1.4158820495 |
48 |
Registrant Email:dns-admin@wikimedia.org |
51 |
Admin Organization:Wikimedia Foundation, Inc. |
52 |
Admin Street1:149 New Montgomery Street |
53 |
Admin Street2:Third Floor |
55 |
Admin City:San Francisco |
56 |
Admin State/Province:California |
57 |
Admin Postal Code:94105 |
59 |
Admin Phone:+1.4158396885 |
61 |
Admin FAX:+1.4158820495 |
63 |
Admin Email:dns-admin@wikimedia.org |
66 |
Tech Organization:Wikimedia Foundation, Inc. |
67 |
Tech Street1:149 New Montgomery Street |
68 |
Tech Street2:Third Floor |
70 |
Tech City:San Francisco |
71 |
Tech State/Province:California |
72 |
Tech Postal Code:94105 |
74 |
Tech Phone:+1.4158396885 |
76 |
Tech FAX:+1.4158820495 |
78 |
Tech Email:dns-admin@wikimedia.org |
79 |
Name Server:NS0.WIKIMEDIA.ORG |
80 |
Name Server:NS1.WIKIMEDIA.ORG |
81 |
Name Server:NS2.WIKIMEDIA.ORG |
A whois client is a program that will simply fetch the whois information for a domain/ip address from the whois servers. The code over here works according to the algorithm discussed here.
Code
6 |
* This program shall perform whois for a domain and get you the whois data of that domain
|
8 |
* @author Silver Moon ( m00n.silv3r@gmail.com )
|
11 |
#include<stdio.h> //scanf , printf |
12 |
#include<string.h> //strtok |
13 |
#include<stdlib.h> //realloc |
14 |
#include<sys/socket.h> //socket |
15 |
#include<netinet/in.h> //sockaddr_in |
16 |
#include<arpa/inet.h> //getsockname |
17 |
#include<netdb.h> //hostent |
18 |
#include<unistd.h> //close |
20 |
int get_whois_data( char * , char **);
|
21 |
int hostname_to_ip( char * , char *);
|
22 |
int whois_query( char * , char * , char **);
|
23 |
char *str_replace( char *search , char *replace , char *subject );
|
25 |
int main( int argc , char *argv[])
|
27 |
char domain[100] , *data = NULL;
|
29 |
printf ( "Enter domain name to whois : " );
|
32 |
get_whois_data(domain , &data);
|
39 |
* Get the whois data of a domain
|
41 |
int get_whois_data( char *domain , char **data)
|
43 |
char ext[1024] , *pch , *response = NULL , *response_2 = NULL , *wch , *dt;
|
46 |
domain = str_replace( "http://" , "" , domain);
|
47 |
domain = str_replace( "www." , "" , domain);
|
49 |
//get the extension , com , org , edu
|
53 |
printf ( "strdup failed" );
|
55 |
pch = ( char *) strtok (dt , "." );
|
59 |
pch = strtok (NULL , "." );
|
62 |
//This will tell the whois server for the particular TLD like com , org
|
63 |
if (whois_query( "whois.iana.org" , ext , &response))
|
65 |
printf ( "Whois query failed" );
|
68 |
//Now analysze the response :)
|
69 |
pch = strtok (response , "\n" );
|
73 |
wch = strstr (pch , "whois." );
|
80 |
pch = strtok (NULL , "\n" );
|
85 |
//Now we have the TLD whois server in wch , query again
|
86 |
//This will provide minimal whois information along with the parent whois server of the specific domain :)
|
88 |
//This should not be necessary , but segmentation fault without this , why ?
|
92 |
printf ( "\nTLD Whois server is : %s" , wch);
|
93 |
if (whois_query(wch , domain , &response))
|
95 |
printf ( "Whois query failed" );
|
100 |
printf ( "\nTLD whois server for %s not found" , ext);
|
104 |
response_2 = strdup(response);
|
106 |
//Again search for a whois server in this response. :)
|
107 |
pch = strtok (response , "\n" );
|
110 |
//Check if whois line
|
111 |
wch = strstr (pch , "whois." );
|
118 |
pch = strtok (NULL , "\n" );
|
123 |
* If a registrar whois server is found then query it
|
127 |
//Now we have the registrar whois server , this has the direct full information of the particular domain
|
128 |
//so lets query again
|
130 |
printf ( "\nRegistrar Whois server is : %s" , wch);
|
132 |
if (whois_query(wch , domain , &response))
|
134 |
printf ( "Whois query failed" );
|
137 |
printf ( "\n%s" , response);
|
141 |
* otherwise echo the output from the previous whois result
|
145 |
printf ( "%s" , response_2);
|
151 |
* Perform a whois query to a server and record the response
|
153 |
int whois_query( char *server , char *query , char **response)
|
155 |
char ip[32] , message[100] , buffer[1500];
|
156 |
int sock , read_size , total_size = 0;
|
157 |
struct sockaddr_in dest;
|
159 |
sock = socket(AF_INET , SOCK_STREAM , IPPROTO_TCP);
|
161 |
//Prepare connection structures :)
|
162 |
memset ( &dest , 0 , sizeof (dest) );
|
163 |
dest.sin_family = AF_INET;
|
165 |
printf ( "\nResolving %s..." , server);
|
166 |
if (hostname_to_ip(server , ip))
|
172 |
dest.sin_addr.s_addr = inet_addr( ip );
|
173 |
dest.sin_port = htons( 43 );
|
175 |
//Now connect to remote server
|
176 |
if (connect( sock , ( const struct sockaddr*) &dest , sizeof (dest) ) < 0)
|
178 |
perror ( "connect failed" );
|
181 |
//Now send some data or message
|
182 |
printf ( "\nQuerying for ... %s ..." , query);
|
183 |
sprintf (message , "%s\r\n" , query);
|
184 |
if ( send(sock , message , strlen (message) , 0) < 0)
|
186 |
perror ( "send failed" );
|
189 |
//Now receive the response
|
190 |
while ( (read_size = recv(sock , buffer , sizeof (buffer) , 0) ) )
|
192 |
*response = realloc (*response , read_size + total_size);
|
193 |
if (*response == NULL)
|
195 |
printf ( "realloc failed" );
|
197 |
memcpy (*response + total_size , buffer , read_size);
|
198 |
total_size += read_size;
|
203 |
*response = realloc (*response , total_size + 1);
|
204 |
*(*response + total_size) = '\0' ;
|
212 |
* Get the ip address of a given hostname
|
215 |
int hostname_to_ip( char * hostname , char * ip)
|
218 |
struct in_addr **addr_list;
|
221 |
if ( (he = gethostbyname( hostname ) ) == NULL)
|
224 |
herror( "gethostbyname" );
|
228 |
addr_list = ( struct in_addr **) he->h_addr_list;
|
230 |
for (i = 0; addr_list[i] != NULL; i++)
|
232 |
//Return the first one;
|
233 |
strcpy (ip , inet_ntoa(*addr_list[i]) );
|
241 |
* Search and replace a string with another string , in a string
|
243 |
char *str_replace( char *search , char *replace , char *subject)
|
245 |
char *p = NULL , *old = NULL , *new_subject = NULL ;
|
246 |
int c = 0 , search_size;
|
248 |
search_size = strlen (search);
|
250 |
//Count how many occurences
|
251 |
for (p = strstr (subject , search) ; p != NULL ; p = strstr (p + search_size , search))
|
257 |
c = ( strlen (replace) - search_size )*c + strlen (subject);
|
259 |
//New subject with new size
|
260 |
new_subject = malloc ( c );
|
263 |
strcpy (new_subject , "" );
|
268 |
for (p = strstr (subject , search) ; p != NULL ; p = strstr (p + search_size , search))
|
270 |
//move ahead and copy some text from original subject , from a certain position
|
271 |
strncpy (new_subject + strlen (new_subject) , old , p - old);
|
273 |
//move ahead and copy the replacement text
|
274 |
strcpy (new_subject + strlen (new_subject) , replace);
|
276 |
//The new start position after this search match
|
277 |
old = p + search_size;
|
280 |
//Copy the part after the last search match
|
281 |
strcpy (new_subject + strlen (new_subject) , old);
|
hostname_to_ip – This is a simple function to get an IP of a domain.
str_replace – This is a generic string processing function that is used to search for a string in another big string and replace it with another string.
Output
1 |
Enter domain name to whois : www.wikipedia.org |
3 |
Resolving whois.iana.org...192.0.47.59 |
4 |
Querying for ... org ...Done
|
5 |
TLD Whois server is : whois.pir.org |
6 |
Resolving whois.pir.org...149.17.192.7 |
7 |
Querying for ... wikipedia.org ...DoneAccess to .ORG WHOIS information is provided to assist persons in
|
8 |
determining the contents of a domain name registration record in the
|
9 |
Public Interest Registry registry database. The data in this record is provided by
|
10 |
Public Interest Registry for informational purposes only, and Public Interest Registry does not
|
11 |
guarantee its accuracy. This service is intended only for query-based
|
12 |
access. You agree that you will use this data only for lawful purposes
|
13 |
and that, under no circumstances will you use this data to: (a) allow, |
14 |
enable , or otherwise support the transmission by e-mail, telephone, or
|
15 |
facsimile of mass unsolicited, commercial advertising or solicitations |
16 |
to entities other than the data recipient's own existing customers; or |
17 |
(b) enable high volume, automated, electronic processes that send
|
18 |
queries or data to the systems of Registry Operator, a Registrar, or |
19 |
Afilias except as reasonably necessary to register domain names or |
20 |
modify existing registrations. All rights reserved. Public Interest Registry reserves |
21 |
the right to modify these terms at any time . By submitting this query,
|
22 |
you agree to abide by this policy. |
24 |
Domain ID:D51687756-LROR |
25 |
Domain Name:WIKIPEDIA.ORG |
26 |
Created On:13-Jan-2001 00:12:14 UTC |
27 |
Last Updated On:02-Dec-2009 20:57:17 UTC |
28 |
Expiration Date:13-Jan-2015 00:12:14 UTC |
29 |
Sponsoring Registrar:GoDaddy.com, Inc. (R91-LROR) |
30 |
Status:CLIENT DELETE PROHIBITED |
31 |
Status:CLIENT RENEW PROHIBITED |
32 |
Status:CLIENT TRANSFER PROHIBITED |
33 |
Status:CLIENT UPDATE PROHIBITED |
34 |
Registrant ID:CR31094073 |
35 |
Registrant Name:DNS Admin |
36 |
Registrant Organization:Wikimedia Foundation, Inc. |
37 |
Registrant Street1:149 New Montgomery Street |
38 |
Registrant Street2:Third Floor |
40 |
Registrant City:San Francisco |
41 |
Registrant State/Province:California |
42 |
Registrant Postal Code:94105 |
44 |
Registrant Phone:+1.4158396885 |
45 |
Registrant Phone Ext.: |
46 |
Registrant FAX:+1.4158820495 |
48 |
Registrant Email:dns-admin@wikimedia.org |
51 |
Admin Organization:Wikimedia Foundation, Inc. |
52 |
Admin Street1:149 New Montgomery Street |
53 |
Admin Street2:Third Floor |
55 |
Admin City:San Francisco |
56 |
Admin State/Province:California |
57 |
Admin Postal Code:94105 |
59 |
Admin Phone:+1.4158396885 |
61 |
Admin FAX:+1.4158820495 |
63 |
Admin Email:dns-admin@wikimedia.org |
66 |
Tech Organization:Wikimedia Foundation, Inc. |
67 |
Tech Street1:149 New Montgomery Street |
68 |
Tech Street2:Third Floor |
70 |
Tech City:San Francisco |
71 |
Tech State/Province:California |
72 |
Tech Postal Code:94105 |
74 |
Tech Phone:+1.4158396885 |
76 |
Tech FAX:+1.4158820495 |
78 |
Tech Email:dns-admin@wikimedia.org |
79 |
Name Server:NS0.WIKIMEDIA.ORG |
80 |
Name Server:NS1.WIKIMEDIA.ORG |
81 |
Name Server:NS2.WIKIMEDIA.ORG |
时间: 2024-10-26 15:10:01