-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgestor_pedidos.c
440 lines (325 loc) · 9.49 KB
/
gestor_pedidos.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "time.h"
#include "auxiliar.h"
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <signal.h>
/*Daniel Azevedo nº 2014200607
Nuno Afonso Santos nº 2014226541*/
void insere_lpedidos(Lpedido lista,struct QUERY dominio,unsigned short id,int sockfd, struct sockaddr_in dest){
Lpedido no;
no = (Lpedido) malloc (sizeof (Lpedido_no));
no->Query_name=malloc(sizeof(struct QUERY*));
if (no != NULL){
while(lista->next!=NULL)
lista=lista->next;
memcpy(no->Query_name,&dominio,sizeof(struct QUERY));
no->dest=dest;
no->id=id;
no->sockfd=sockfd;
no->next=NULL;
lista->next=no;
}
}
char* verifica_ip(char* query,char* linha){
int i=0;
int a=0;
char *ip=malloc(sizeof(char));
if(strstr(linha,query)!=NULL){
while(linha[i]!=' '){
i++;
}
i++;
while(linha[i]!='\0'){
ip[a]=linha[i];
a++;
i++;
}
ip[a-1]='\0';
return ip;
}
return NULL;
}
char* resolve_ip(char* t, char* local){
char linha[30];
char x=local[0];
int p=0;
int aux=0;
while(x!='\0'){
while(x!='\n'){
x=local[p];
linha[p-aux]=local[p];
//printf("%c",x);
p++;
}
linha[p-aux]='\0';
if(verifica_ip(t,linha)!=NULL){
const char *v=malloc(sizeof(char));
v=verifica_ip(t,linha);
return (char*)v;
}
linha[0]='\0';
aux=p;
x=local[p];
}
return NULL;
}
int verifica_validade(char tabela[][30], char* dominio){
int i=0;
while(strcmp(tabela[i],"\0")!=0){
if(strstr(dominio,tabela[i])!=NULL){
return 1;
}
i++;
}
return 0;
}
void sendReply(unsigned short id, unsigned char* query, int ip_addr, int sockfd, struct sockaddr_in dest) {
unsigned char bufReply[65536], *rname;
char *rip;
struct R_DATA *rinfo = NULL;
//Set the DNS structure to reply (according to the RFC)
struct DNS_HEADER *rdns = NULL;
rdns = (struct DNS_HEADER *)&bufReply;
rdns->id = id;
rdns->qr = 1;
rdns->opcode = 0;
rdns->aa = 1;
rdns->tc = 0;
rdns->rd = 0;
rdns->ra = 0;
rdns->z = 0;
rdns->ad = 0;
rdns->cd = 0;
rdns->rcode = 0;
rdns->q_count = 0;
rdns->ans_count = htons(1);
rdns->auth_count = 0;
rdns->add_count = 0;
// Add the QUERY name (the same as the query received)
rname = (unsigned char*)&bufReply[sizeof(struct DNS_HEADER)];
convertName2RFC(rname , query);
// Add the reply structure (according to the RFC)
rinfo = (struct R_DATA*)&bufReply[sizeof(struct DNS_HEADER) + (strlen((const char*)rname)+1)];
rinfo->type = htons(1);
rinfo->_class = htons(1);
rinfo->ttl = htonl(3600);
rinfo->data_len = htons(sizeof(ip_addr)); // Size of the reply IP address
// Add the reply IP address for the query name
rip = (char *)&bufReply[sizeof(struct DNS_HEADER) + (strlen((const char*)rname)+1) + sizeof(struct R_DATA)];
memcpy(rip, (struct in_addr *) &ip_addr, sizeof(ip_addr));
// Send DNS reply
printf("\nSending Answer... ");
if( sendto(sockfd, (char*)bufReply, sizeof(struct DNS_HEADER) + (strlen((const char*)rname) + 1) + sizeof(struct R_DATA) + sizeof(ip_addr),0,(struct sockaddr*)&dest,sizeof(dest)) < 0) {
printf("FAILED!!\n");
} else {
printf("SENT!!!\n");
}
}
/**
convertRFC2Name: converts DNS RFC name to name
**/
u_char* convertRFC2Name(unsigned char* reader,unsigned char* buffer,int* count) {
unsigned char *name;
unsigned int p=0,jumped=0,offset;
int i , j;
*count = 1;
name = (unsigned char*)malloc(256);
name[0]='\0';
while(*reader!=0) {
if(*reader>=192) {
offset = (*reader)*256 + *(reader+1) - 49152;
reader = buffer + offset - 1;
jumped = 1;
} else {
name[p++]=*reader;
}
reader = reader+1;
if(jumped==0) {
*count = *count + 1;
}
}
name[p]='\0';
if(jumped==1) {
*count = *count + 1;
}
for(i=0;i<(int)strlen((const char*)name);i++) {
p=name[i];
for(j=0;j<(int)p;j++) {
name[i]=name[i+1];
i=i+1;
}
name[i]='.';
}
name[i-1]='\0';
return name;
}
/**
convertName2RFC: converts name to DNS RFC name
**/
void convertName2RFC(unsigned char* dns,unsigned char* host) {
int lock = 0 , i;
strcat((char*)host,".");
for(i = 0 ; i < strlen((char*)host) ; i++) {
if(host[i]=='.') {
*dns++ = i-lock;
for(;lock<i;lock++) {
*dns++=host[lock];
}
lock++;
}
}
*dns++='\0';
}
void gestor_pedidos( int argc , char *argv[], char *local, Info_Pipe *info){
signal(SIGINT, cleanup);
signal(SIGALRM,SIG_IGN);
signal(SIGTERM,exit_processes);
signal(SIGUSR1,manutencao);
char *nome;
char* c;
int y;
printf("A enviar para o named pipe...\n\n");
write_pipe(d,info);
lprioritaria= (Lpedido)malloc(sizeof(Lpedido_no));
lprioritaria->next=NULL;
lnormal= (Lpedido)malloc(sizeof(Lpedido_no));
lnormal->next=NULL;
unsigned char buf[65536], *reader;
int sockfd, stop;
struct DNS_HEADER *dns = NULL;
struct sockaddr_in servaddr,dest;
socklen_t len;
// Check arguments
if(argc <= 1) {
printf("Usage: dnsserver <port>\n");
exit(1);
}
// Get server UDP port number
int port = atoi(argv[1]);
if(port <= 0) {
printf("Usage: dnsserver <port>\n");
exit(1);
}
// ****************************************
// Create socket & bind
// ****************************************
// Create UDP socket
sockfd = socket(AF_INET , SOCK_DGRAM , IPPROTO_UDP); //UDP packet for DNS queries
if (sockfd < 0) {
printf("ERROR opening socket.\n");
exit(1);
}
// Prepare UDP to bind port
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(port);
// Bind application to UDP port
int res = bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
if(res < 0) {
printf("Error binding to port %d.\n", servaddr.sin_port);
if(servaddr.sin_port <= 1024) {
printf("To use ports below 1024 you may need additional permitions. Try to use a port higher than 1024.\n");
} else {
printf("Please make sure this UDP port is not being used.\n");
}
exit(1);
}
// ****************************************
// Receive questions
// ****************************************
while(1) {
// Receive questions
sleep(1);
len = sizeof(dest);
printf("\n\n-- Wating for DNS message --\n\n");
if(recvfrom (sockfd,(char*)buf , 65536 , 0 , (struct sockaddr*)&dest , &len) < 0) {
printf("Error while waiting for DNS message. Exiting...\n");
exit(1);
}
printf("DNS message received\n");
// Process received message
dns = (struct DNS_HEADER*) buf;
//qname =(unsigned char*)&buf[sizeof(struct DNS_HEADER)];
reader = &buf[sizeof(struct DNS_HEADER)];
printf("\nThe query %d contains: ", ntohs(dns->id));
printf("\n %d Questions.",ntohs(dns->q_count));
printf("\n %d Answers.",ntohs(dns->ans_count));
printf("\n %d Authoritative Servers.",ntohs(dns->auth_count));
printf("\n %d Additional records.\n\n",ntohs(dns->add_count));
// We only need to process the questions
// We only process DNS messages with one question
// Get the query fields according to the RFC specification
struct QUERY query;
if(ntohs(dns->q_count) == 1) {
// Get NAME
query.name = convertRFC2Name(reader,buf,&stop);
reader = reader + stop;
// Get QUESTION structure
query.ques = (struct QUESTION*)(reader);
reader = reader + sizeof(struct QUESTION);
// Check question type. We only need to process A records.
if(ntohs(query.ques->qtype) == 1) {
printf("A record request.\n\n");
} else {
printf("NOT A record request!! Ignoring DNS message!\n");
continue;
}
} else {
printf("\n\nDNS message must contain one question!! Ignoring DNS message!\n\n");
continue;
}
// Received DNS message fulfills all requirements.
// ****************************************
// Print received DNS message QUERY
// ****************************************
printf(">> QUERY: %s\n", query.name);
printf(">> Type (A): %d\n", ntohs(query.ques->qtype));
printf(">> Class (IN): %d\n\n", ntohs(query.ques->qclass));
// ****************************************
// Example reply to the received QUERY
// (Currently replying 10.0.0.2 to all QUERY names)
// ****************************************
#ifdef DEBUG
printf("Verificar se o domino e local\n");
#endif
nome=(char *)query.name;
c=strstr(nome,d->LocalDomain);
if(c!=NULL){
printf("%s e um dominio local\n",nome);
insere_lpedidos(lprioritaria,query,dns->id,sockfd,dest);
sem_post(mutex);
}
else{
#ifdef DEBUG
printf("Verificar se o domino esta no config.txt ou nao\n");
#endif
if(d->flag[0]==TRUE && d->flag[1]==FALSE){
printf("Em modo de manutencao, opcao indisponivel\n");
sendReply(dns->id, query.name, inet_addr("0.0.0.0"), sockfd, dest);
info->pedidos_recusados++;
}
else{
if((y=verifica_validade(d->Domains,nome))==0){
printf("Dominio nao e valido\n");
sendReply(dns->id, query.name, inet_addr("0.0.0.0"), sockfd, dest);
info->atual=sistema();
info->pedidos_recusados++;
}
else{
printf("Dominio e externo\n");
insere_lpedidos(lnormal,query,dns->id,sockfd,dest);
sem_post(mutex);
}
}
}
info->pedidos_total++;
}
}