Subversion Repositories Kolibri OS

Rev

Rev 8515 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 8515 Rev 8686
Line 2... Line 2...
2
WHOIS port for KolibriOS (Adapted by turbocat2001).
2
WHOIS port for KolibriOS (Adapted by turbocat2001).
3
The main code is taken from the site:
3
The main code is taken from the site:
4
https://www.binarytides.com/whois-client-code-in-c-with-linux-sockets/
4
https://www.binarytides.com/whois-client-code-in-c-with-linux-sockets/
5
*/
5
*/
Line -... Line 6...
-
 
6
 
-
 
7
#include 
-
 
8
int errno;
-
 
9
 
6
 
10
#include 
7
#include 
-
 
8
#include 	
11
#include 
9
#include 	
12
#include 	
10
#include 	
13
#include 	
11
#include 	
14
#include 	
12
#include 
-
 
13
 
-
 
Line 14... Line 15...
14
#define stdout (FILE*)0
15
#include 
Line 15... Line 16...
15
 
16
 
16
FILE *out=stdout;
17
FILE *out=stdout;
17
 
18
 
Line 18... Line 19...
18
#ifdef DEBUG
19
#ifdef DEBUG
19
FILE *out=stderr;
20
FILE *out=stderr;
20
#endif
-
 
21
 
-
 
22
#define EXIT_SUCCESS 0
-
 
23
#define EXIT_FAILURE 1
-
 
24
#define perror(STR) debug_printf(STR);
-
 
25
 
-
 
26
int unix_fprintf(FILE* file, const char* format, ...)
-
 
27
{
-
 
28
    va_list	arg;
-
 
29
    va_start (arg, format);
-
 
30
    if(file==stdout){
-
 
31
		return vprintf(format, arg);
-
 
32
	}else if(file==stderr){
-
 
33
		char log_board[300];
-
 
34
		vsnprintf(log_board, sizeof log_board, format, arg);
-
 
35
        debug_out_str(log_board);
-
 
36
		return 0;
-
 
Line 37... Line 21...
37
	}else{
21
#endif
38
		return vfprintf(file, format, arg);
22
 
39
	}
23
#define EXIT_SUCCESS 0
40
}
24
#define EXIT_FAILURE 1
Line 51... Line 35...
51
int hostname_to_ip(char * , char *);
35
int hostname_to_ip(char * , char *);
52
int whois_query(char * , char * , char **);
36
int whois_query(char * , char * , char **);
53
char *str_replace(char *search , char *replace , char *subject );
37
char *str_replace(char *search , char *replace , char *subject );
54
char* str_copy(char*);
38
char* str_copy(char*);
Line 55... Line -...
55
 
-
 
56
void start_console()
-
 
57
{
-
 
58
	con_init_console_dll();
-
 
59
	con_set_title("Whois");
-
 
Line 60... Line 39...
60
}
39
 
61
 
40
 
62
int main(int argc , char *argv[])
41
int main(int argc , char *argv[])
63
{	
42
{	
64
	networklib_init();
43
	networklib_init();
Line 65... Line 44...
65
	char *domain , *data = NULL;
44
	char *domain , *data = NULL;
66
	int f_flag=0;
-
 
67
 
45
	int f_flag=0;
68
	if(argc==2){
46
 
Line 69... Line 47...
69
		start_console();
47
	if(argc==2){
70
		domain=strdup(argv[1]);
48
		domain=strdup(argv[1]);
71
	}
49
	}
72
 
50
 
73
	else if(!strcmp(argv[2], "-f") && argc==4){
51
	else if(!strcmp(argv[2], "-f") && argc==4){
74
		domain=strdup(argv[1]);
52
		domain=strdup(argv[1]);
75
		if((out=fopen(argv[3],"w"))==NULL){
53
		if((out=fopen(argv[3],"w"))==NULL){
76
			printf("Error writing to file: '%s' !\n", argv[3]);
-
 
77
			exit(0);
54
			printf("Error writing to file: '%s' !\n", argv[3]);
78
		}
55
			exit(0);
79
	}else{
56
		}
-
 
57
	}else{
-
 
58
		show_help();
80
		start_console();
59
		exit(0);
81
		show_help();
60
	}
82
		exit(0);
61
	if(out==stdout){
83
	}
62
		con_set_title("Whois");
Line 84... Line 63...
84
 
63
	}
Line 98... Line 77...
98
	domain = str_replace("http://" , "" , domain);
77
	domain = str_replace("http://" , "" , domain);
99
	domain = str_replace("www." , "" , domain);
78
	domain = str_replace("www." , "" , domain);
Line 100... Line 79...
100
 
79
 
101
	//get the extension , com , org , edu
80
	//get the extension , com , org , edu
-
 
81
	dt = strdup(domain);
102
	dt = strdup(domain);
82
 
103
	if(dt == NULL){
83
	if(dt == NULL){
104
		unix_fprintf(out, "strdup failed");
84
		fprintf(out, "strdup failed");
105
	}
85
	}
106
	pch = (char*)strtok(dt , ".");
86
	pch = (char*)strtok(dt , ".");
107
	while(pch != NULL){
87
	while(pch != NULL){
108
		strcpy(ext , pch);
88
		strcpy(ext , pch);
109
		pch = strtok(NULL , ".");
89
		pch = strtok(NULL , ".");
110
	}
-
 
111
	
90
	}
112
	// This will tell the whois server for the particular TLD like com , org
91
	// This will tell the whois server for the particular TLD like com , org
113
	if( whois_query("whois.iana.org" , ext , &response) == EXIT_FAILURE){
92
	if( whois_query("whois.iana.org" , ext , &response) == EXIT_FAILURE){
114
		unix_fprintf(out, "Whois query failed");
93
		fprintf(out, "Whois query failed");
115
        return 1;
94
        return 1;
116
	}
95
	}
117
	unix_fprintf(out, "\n\nResponse is:\n\n");
96
	fprintf(out, "\n\nResponse is:\n\n");
Line 118... Line 97...
118
	unix_fprintf(out, "%s", response);
97
	fprintf(out, "%s", response);
119
	
98
 
120
	// Now analysze the response
99
	// Now analysze the response
121
	pch = strtok(response , "\n");
100
	pch = strtok(response , "\n");
Line 129... Line 108...
129
		// Next line please
108
		// Next line please
130
		pch = strtok(NULL , "\n");
109
		pch = strtok(NULL , "\n");
131
	}
110
	}
132
	// Now we have the TLD whois server in wch , query again
111
	// Now we have the TLD whois server in wch , query again
133
	//This will provide minimal whois information along with the parent whois server of the specific domain :)
112
	//This will provide minimal whois information along with the parent whois server of the specific domain :)
-
 
113
	wch = strdup(wch);
134
	free(response);
114
	free(response);
135
    //This should not be necessary , but segmentation fault without this , why ?
115
    //This should not be necessary , but segmentation fault without this , why ?
136
	response = NULL;
116
	response = NULL;
137
	if(wch != NULL){
117
	if(wch != NULL){
138
		unix_fprintf(out, "\nTLD Whois server is : %s" , wch);
118
		fprintf(out,"\nTLD Whois server is : %s" , wch);
139
        if( whois_query(wch , domain , &response) == EXIT_FAILURE){
119
		if( whois_query(wch , domain , &response) == EXIT_FAILURE){
140
			unix_fprintf(out, "Whois query failed\n");
120
			fprintf(out, "Whois query failed\n");
141
            return EXIT_FAILURE;
121
            return EXIT_FAILURE;
142
		}
122
		}
143
	}else{
123
	}else{
144
		unix_fprintf(out, "\nTLD whois server for %s not found\n" , ext);
124
		fprintf(out, "\nTLD whois server for %s not found\n" , ext);
145
		return EXIT_SUCCESS;
125
		return EXIT_SUCCESS;
146
	}
126
	}
Line 147... Line 127...
147
	
127
	
Line 164... Line 144...
164
    */
144
    */
165
	if(wch){
145
	if(wch){
166
		// Now we have the registrar whois server , this has the direct full information of the particular domain
146
		// Now we have the registrar whois server , this has the direct full information of the particular domain
167
		// so lets query again
147
		// so lets query again
Line 168... Line 148...
168
		
148
		
Line 169... Line 149...
169
		unix_fprintf(out, "\nRegistrar Whois server is : %s" , wch);
149
		fprintf(out, "\nRegistrar Whois server is : %s" , wch);
170
		
150
		
171
		if( whois_query(wch , domain , &response) == EXIT_FAILURE ){
151
		if( whois_query(wch , domain , &response) == EXIT_FAILURE ){
172
			unix_fprintf(out, "Whois query failed");
152
			fprintf(out, "Whois query failed");
173
		}else{
153
		}else{
174
            unix_fprintf(out, "\n%s" , response);
154
            fprintf(out, "\n%s" , response);
175
        }
155
        }
176
	}
156
	}
177
	/*
157
	/*
178
        otherwise echo the output from the previous whois result
158
        otherwise echo the output from the previous whois result
179
    */
159
    */
180
	else{
160
	else{
181
		unix_fprintf(out, "%s" , response_2);
161
		fprintf(out, "%s" , response_2);
182
	}	
162
	}	
Line 183... Line 163...
183
	return 0;
163
	return 0;
Line 196... Line 176...
196
	sock = socket(AF_INET4 , SOCK_STREAM , IPPROTO_TCP);
176
	sock = socket(AF_INET4 , SOCK_STREAM , IPPROTO_TCP);
Line 197... Line 177...
197
     
177
     
198
    //Prepare connection structures :)
178
    //Prepare connection structures :)
199
    memset( &dest , 0 , sizeof(dest) );
179
    memset(&dest , 0 , sizeof(dest) );
200
    dest.sin_family = AF_INET;
-
 
201
    
180
	dest.sin_family = AF_INET;
202
    server = str_copy(server);
-
 
Line -... Line 181...
-
 
181
    server = str_copy(server);
203
    server[strcspn(server, "\r\n")] = '\0';
182
	
204
    
183
    server[strcspn(server, "\r\n")] = '\0';
205
	unix_fprintf(out, "\nResolving: %s ...\n" , server);
184
	fprintf(out, "\nResolving: %s ...\n" , server);
206
	if( hostname_to_ip(server , ip) == EXIT_FAILURE ){
185
	if(hostname_to_ip(server , ip) == EXIT_FAILURE ){
207
        unix_fprintf(out, "Failed\n");
186
        fprintf(out, "Failed\n");
Line 208... Line 187...
208
        return EXIT_FAILURE;
187
        return EXIT_FAILURE;
209
	}
-
 
210
	
188
	}
211
	unix_fprintf(out, "Found ip: %s \n" , ip);    
189
	
Line 212... Line 190...
212
	
190
	fprintf(out, "Found ip: %s \n" , ip);    
213
    dest.sin_addr = inet_addr(ip);
191
    dest.sin_addr = inet_addr(ip);
214
	dest.sin_port = PORT(WHOIS_PORT);
192
	dest.sin_port = PORT(WHOIS_PORT);
-
 
193
 
215
 
194
;	//Now connect to remote server
216
	//Now connect to remote server
195
	if(connect(sock , (const struct sockaddr*) &dest , sizeof(dest)) < 0){
Line 217... Line 196...
217
	if(connect(sock , (const struct sockaddr*) &dest , sizeof(dest)) < 0){
196
		perror("connect failed");
218
		perror("connect failed");
197
		perror(strerror(errno));
219
        return EXIT_FAILURE;
198
        return EXIT_FAILURE;
220
	}
199
	}
221
	
200
 
222
	//Now send some data or message
201
	//Now send some data or message
223
	unix_fprintf(out, "\nQuerying for: %s ...\n" , query);
202
	fprintf(out, "\nQuerying for: %s ...\n" , query);
Line 224... Line 203...
224
	sprintf(message , "%s\r\n" , query);
203
	sprintf(message , "%s\r\n" , query);
225
	if( send(sock , message , strlen(message) , 0) < 0){
204
	if( send(sock , message , strlen(message) , 0) < 0){
226
		perror("send failed");
205
		perror("send failed");
227
        return EXIT_FAILURE;
206
        return EXIT_FAILURE;
228
	}
207
	}
229
	
208
	
230
	//Now receive the response
209
	//Now receive the response
231
	while((read_size = recv(sock, buffer, sizeof(buffer), 0))){
210
	while((read_size = recv(sock, buffer, sizeof(buffer), 0))){
232
		*response = realloc(*response , read_size + total_size);
211
		*response = realloc(*response , read_size + total_size);
233
		if(*response == NULL){
212
		if(*response == NULL){
Line 234... Line 213...
234
			unix_fprintf(out, "realloc failed");
213
			fprintf(out, "realloc failed");
235
            return EXIT_FAILURE;
-
 
Line 236... Line 214...
236
		}
214
            return EXIT_FAILURE;
237
		memcpy(*response + total_size , buffer , read_size);
215
		}
238
		total_size += read_size;
216
		memcpy(*response + total_size , buffer , read_size);
239
	}
217
		total_size += read_size;