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
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); |
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); |
53 | printf ( "strdup failed" ); |
55 | pch = ( char *) strtok (dt , "." ); |
59 | pch = strtok (NULL , "." ); |
63 | if (whois_query( "whois.iana.org" , ext , &response)) |
65 | printf ( "Whois query failed" ); |
69 | pch = strtok (response , "\n" ); |
73 | wch = strstr (pch , "whois." ); |
80 | pch = strtok (NULL , "\n" ); |
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); |
107 | pch = strtok (response , "\n" ); |
111 | wch = strstr (pch , "whois." ); |
118 | pch = strtok (NULL , "\n" ); |
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); |
145 | printf ( "%s" , response_2); |
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); |
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 ); |
176 | if (connect( sock , ( const struct sockaddr*) &dest , sizeof (dest) ) < 0) |
178 | perror ( "connect failed" ); |
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" ); |
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' ; |
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++) |
233 | strcpy (ip , inet_ntoa(*addr_list[i]) ); |
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); |
251 | for (p = strstr (subject , search) ; p != NULL ; p = strstr (p + search_size , search)) |
257 | c = ( strlen (replace) - search_size )*c + strlen (subject); |
260 | new_subject = malloc ( c ); |
263 | strcpy (new_subject , "" ); |
268 | for (p = strstr (subject , search) ; p != NULL ; p = strstr (p + search_size , search)) |
271 | strncpy (new_subject + strlen (new_subject) , old , p - old); |
274 | strcpy (new_subject + strlen (new_subject) , replace); |
277 | old = p + search_size; |
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 |
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 |
74 | Tech Phone:+1.4158396885 |
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
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); |
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); |
53 | printf ( "strdup failed" ); |
55 | pch = ( char *) strtok (dt , "." ); |
59 | pch = strtok (NULL , "." ); |
63 | if (whois_query( "whois.iana.org" , ext , &response)) |
65 | printf ( "Whois query failed" ); |
69 | pch = strtok (response , "\n" ); |
73 | wch = strstr (pch , "whois." ); |
80 | pch = strtok (NULL , "\n" ); |
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); |
107 | pch = strtok (response , "\n" ); |
111 | wch = strstr (pch , "whois." ); |
118 | pch = strtok (NULL , "\n" ); |
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); |
145 | printf ( "%s" , response_2); |
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); |
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 ); |
176 | if (connect( sock , ( const struct sockaddr*) &dest , sizeof (dest) ) < 0) |
178 | perror ( "connect failed" ); |
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" ); |
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' ; |
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++) |
233 | strcpy (ip , inet_ntoa(*addr_list[i]) ); |
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); |
251 | for (p = strstr (subject , search) ; p != NULL ; p = strstr (p + search_size , search)) |
257 | c = ( strlen (replace) - search_size )*c + strlen (subject); |
260 | new_subject = malloc ( c ); |
263 | strcpy (new_subject , "" ); |
268 | for (p = strstr (subject , search) ; p != NULL ; p = strstr (p + search_size , search)) |
271 | strncpy (new_subject + strlen (new_subject) , old , p - old); |
274 | strcpy (new_subject + strlen (new_subject) , replace); |
277 | old = p + search_size; |
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 |
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 |
74 | Tech Phone:+1.4158396885 |
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 |