forked from millken/c-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip-whois.c
166 lines (140 loc) · 3.17 KB
/
ip-whois.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* @brief
* Whois client program
*
* @details
* This program shall fetch whois data for a IPv4 address.
*
* @author Silver Moon ( [email protected] )
* http://www.binarytides.com/c-code-to-perform-ip-whois/
* */
#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;
}