-
Notifications
You must be signed in to change notification settings - Fork 1
/
dns_main.c
381 lines (287 loc) · 10 KB
/
dns_main.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include <math.h>
#include <ctype.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/resource.h>
#include <stdint.h>
#include "dns1.c"
#define MAX 3 // number of client to connected
#define TRUE 1
#define FALSE 0
#define T_A 1 //Ipv4 address
#define T_NS 2 //Nameserver
#define T_CNAME 5 // canonical name
#define T_SOA 6 /* start of authoority zone */
#define T_PTR 12 /* domain name pointer */
#define T_MX 15 //Mail server
//global var
char dns_servers[10][100];
unsigned char host_cl_1[2000];
char dns_servers1[10][100];
int dns_server_count = 0;
//methods
//void getcon(char *argv[]);
void get_ip(char *host_cl);
void get_from_external(unsigned char* host_cl);
//void ChangetoDnsNameFormat(unsigned char*,unsigned char*);
void gettime();
void get_dns_servers();
void my_get_host_by_name(unsigned char *host , int query_type);
u_char* ReadName(unsigned char* reader,unsigned char* buffer,int* count);
void writetofile(unsigned char* host_cl,char *buffer2);
void ChangetoDnsNameFormat(unsigned char* dns,unsigned char* host_cl);
// connect to socket, Thread
//send recv ans
//get servers
//change to dot format
//change to dns format
//caching
// DNS server -> dns db
//dns ipv4 query
// must frw query which can not be ans no failure given
//various domain still can do it
//backend server should interact with DNS serve
//2 DNS server use same backend datbase
//manually edit backend data
int main(int argc , char *argv[])
{
int opt = TRUE;
int master_sock , new_sock , client_sock[MAX] , max_client = MAX , act, cl ,addrlen , valread , sd;
int max_sd;
fd_set sock_desc;
struct sockaddr_in servaddr;
char buffer1[2000]; //data buffer1
//now set the socket descriptors
//here initialise all client_sock[] to 0
for (cl = 0; cl < max_client; cl++)
{
client_sock[cl] = 0;
}
//create a master socket
if( (master_sock = socket(AF_INET , SOCK_STREAM , 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
//set master socket to allow multiple connections
if( setsockopt(master_sock, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0 )
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
//type of socket created
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htons(atoi("130.65.25.1"));
servaddr.sin_port = htons(atoi(argv[1]) );
//bind the socket to localhost port specified
if (bind(master_sock, (struct sockaddr *)&servaddr, sizeof(servaddr))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
printf("here Listener on port %s \n", argv[1]);
//try to specify maximum of 3 pending connections for the master socket
if (listen(master_sock, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
//accept the incoming connection
addrlen = sizeof(servaddr);
puts("Now Waiting for connections ...");
while(TRUE)
{
//clear the socket set
FD_ZERO(&sock_desc);
//add master socket to set
FD_SET(master_sock, &sock_desc);
max_sd = master_sock;
//add child sockets to set
for ( cl = 0 ; cl < max_client ; cl++)
{
//socket descriptor
sd = client_sock[cl];
//if valid socket descriptor then add to read list
if(sd > 0)
FD_SET( sd , &sock_desc);
//highest file descriptor number, need it for the select function
if(sd > max_sd)
max_sd = sd;
}
//wait for an act on one of the sockets , timeout is NULL , so wait indefinitely
act = select( max_sd + 1 , &sock_desc , NULL , NULL , NULL);
if ((act < 0) && (errno!=EINTR))
{
printf("select error here \n");
}
// an incoming connection
if (FD_ISSET(master_sock, &sock_desc))
{
if ((new_sock = accept(master_sock, (struct sockaddr *)&servaddr, (socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
//inform user of socket number - used in send and receive commands
printf("New connection , socket fd is : %d , ip is : %s , Port : %d \n" , new_sock , inet_ntoa(servaddr.sin_addr) , ntohs(servaddr.sin_port));
//add new socket to array of sockets
for (cl = 0; cl < max_client; cl++)
{
//if position is empty
if( client_sock[cl] == 0 )
{
client_sock[cl] = new_sock;
printf("Adding the list of sockets here: %d\n" ,cl);
break;
}
}
}
//else its some IO operation on some other socket
for (cl = 0; cl < max_client; cl++)
{
sd = client_sock[cl];
if (FD_ISSET( sd , &sock_desc))
{
//Check if it was for closing , and also read the incoming message
if ((valread = read(sd,buffer1, 2024)) == 0)
{
//Somebody disconnected , get his details and print
getpeername(sd , (struct sockaddr*)&servaddr , (socklen_t*)&addrlen);
printf("Host disconnected here sorry ip %s , port %d \n" , inet_ntoa(servaddr.sin_addr) , ntohs(servaddr.sin_port));
//Close the socket and mark as 0 in list for reuse
close( sd );
client_sock[cl] = 0;
}
//Echo back the message that came in
else
{
//set the string terminating NULL byte on the end of the data read
buffer1[valread] = '\0';
//puts(buffer1); host name recived checked.
//printf("%s", );
get_ip(buffer1);
send(sd ,dns_servers[0] , strlen(dns_servers[0]) , 0 );
//send(sd , buffer1 , strlen(buffer1) , 0 );
// strcpy(dns_servers[0],(char *)'\0');
int i, j;
for (i = 0; i < 10; i++) {
for (j = 0; j < 100; j++) {
dns_servers[i][j] = '\0';
}
}// SETTING DNS server field to null
memset(dns_servers[0], 0, sizeof(dns_servers[0][100]));
// void gettime();
}
}
}
}
return 0;
}
void get_ip(char* host_cl)
{
FILE *fp;
char line[200] , *p;
if((fp = fopen("server_db.txt" , "r")) == NULL)
{
printf("Failed opening server_db.txt file \n");
}
FILE *fp1=fopen("server_db.txt" , "r");
int ch, number_of_lines = 0;
do
{
ch = fgetc(fp1);
if(ch == '\n')
number_of_lines++;
} while (ch != EOF);
// printf(" Number of lines here in this file %d",number_of_lines);
int cnt_loop=0;
while(fgets(line , 200 , fp))
{
// printf("%s",line);
cnt_loop++;
if(line[0] == '#')
{
continue;
}
if (strncmp(host_cl,"",strlen(host_cl))==0) {
// printf("sorry no input recived");
strcpy(dns_servers[0],"sorry no input recived");
// strcpy(,);
strncpy((char*)host_cl_1, host_cl, 30);
get_from_external(host_cl_1);
//return;
continue;
}
if(strncmp(line,host_cl,strlen(host_cl)) < 0 )
{
if (number_of_lines<cnt_loop)
{
//strcpy(dns_servers[0], "too small");
strncpy((char*)host_cl_1, host_cl, 30);
get_from_external(host_cl_1);
return;
}
else
continue;
}
if(strncmp(line,host_cl,strlen(host_cl)) > 0 )
{
if (number_of_lines<cnt_loop)
{
//strcpy(dns_servers[0], "too small");
// strcpy(host_cl_1,(unsigned char*)host_cl);
strncpy((char*)host_cl_1, host_cl, 30);
get_from_external(host_cl_1);
return;
}
else
continue;
}
if(strncmp(line,host_cl,strlen(host_cl)) == 0 )
{
// printf("equal");
strcpy(dns_servers[0], strtok(line, " "));
strcpy(dns_servers[0], strtok(NULL, "\n"));
//printf("%s \n",dns_servers[0]);
return;
// continue;
}
}
return;
}
void gettime()
{
time_t current_time;
char* c_time_string;
/* Obtain current time as seconds elapsed since the Epoch. */
current_time = time(NULL);
c_time_string = ctime(¤t_time);
printf("Current time is %s", c_time_string);
return;
}
void get_from_external(unsigned char* host_cl)
{
// strcpy(dns_servers[0], "Sorry not found");
//now get DNS server
get_dns_servers();
printf("%s", "I am here ");
// now get the ip of this hostname
//my_get_host_by_name(host_cl,T_A);
char *buffer2=ngethostbyname(host_cl,T_A);
printf("%s", buffer2);
// strcpy(dns_servers[0],buffer2);
//strcat(dns_servers[0],"The IP Address");
//ngethost_clbyname(host_cl , T_A);
return;
}