forked from andreafabrizi/DNSProxy
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdnsp.c.pthread.bak
1377 lines (1184 loc) · 46.3 KB
/
dnsp.c.pthread.bak
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* DNS proxy 1.01
*
* Copyright (C) 2014-2015 Massimiliano Fantuzzi <[email protected]>
* Copyright (C) 2009-2013 Andrea Fabrizi <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Especially, no
* "INTERNET PRIVACY" is guaranteed, except within lab testing. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <pthread.h>
#include <sys/timeb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <netdb.h>
#include <unistd.h>
#include <ctype.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include <signal.h>
#include <semaphore.h>
//#include <omp.h>
#ifndef SIGCLD
# define SIGCLD SIGCHLD
#endif
#define DELAY 100
#define MAXCONN 8192
#define UDP_DATAGRAM_SIZE 256
#define DNSREWRITE 256
#define HTTP_RESPONSE_SIZE 256
#define URL_SIZE 256
#define VERSION "1.01"
#define DNS_MODE_ANSWER 1
#define DNS_MODE_ERROR 2
#define DEFAULT_LOCAL_PORT 53
#define DEFAULT_WEB_PORT 80
#define NUMT 1
#define NUM_THREADS 1
#define NUM_HANDLER_THREADS 1
//#define TYPEQ 2
//#define DEBUG 0
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
pthread_key_t glob_var_key_ip;
pthread_key_t glob_var_key_client;
//static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
//pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
pthread_mutexattr_t MAttr;
/*
#ifdef TLS
__thread int i;
#else
pthread_key_t key_i;
#endif
pthread_t *tid;
*/
struct readThreadParams {
// struct BoundedBuffer b;
char* input;
char* xproxy_user;
char* xproxy_pass;
char* xproxy_host;
char* xlookup_script;
char* xtypeq;
int digit;
int xproxy_port;
int xwport;
int sockfd;
int xsockfd;
size_t xrequestlen;
//char* xhostname;
struct dns_request *xhostname;
struct sockaddr_in *xclient;
struct sockaddr_in *yclient;
struct dns_request *xdns_req;
struct dns_request *dns_req;
};
struct thread_info { /* Used as argument to thread_start() */
pthread_t thread_id; /* ID returned by pthread_create() */
int thread_num; /* Application-defined thread # */
char *argv_string; /* From command-line argument */
};
//void start_thread(pthread_t *mt)
//{
// mystruct local_data = {};
// mystruct *data = malloc(sizeof(*data));
// *data = local_data;
// pthread_create(mt, NULL, do_work_son, data);
//}
////static void *
////thread_start(void *arg)
////{
//// struct thread_info *tinfo = arg;
//// char *uargv, *p;
////
//// printf("Thread %d: top of stack near %p; argv_string=%s\n",
//// tinfo->thread_num, &p, tinfo->argv_string);
////
//// uargv = strdup(tinfo->argv_string);
//// if (uargv == NULL)
//// handle_error("strdup");
////
//// for (p = uargv; *p != '\0'; p++)
//// *p = toupper(*p);
////
//// return uargv;
////}
//struct thread_data{
// int threads;
// int thread_id;
// int exec; //total number of executions
// int max_req_client;
// int random; //1=yes 0=no whether requests are the max or randomdouble
// int ssl; //1=yes 0=no
// int uselogin; //1=yes 0=no
// char domain[256];
// char login[256];
// char password[256];
//};
int DEBUG;
struct dns_request
{
uint16_t transaction_id,
questions_num,
flags,
qtype,
qclass;
char hostname[256],
query[256];
size_t hostname_len;
};
struct dns_reponse
{
size_t lenght;
char *payload;
};
/* * usage */
void usage(void)
{
fprintf(stderr, "\n dnsp %s\n"
" usage: dnsp -l [local_host] -h [proxy_host] -r [proxy_port] -w [webport] -s [lookup_script]\n\n"
" OPTIONS:\n"
" -l\t\t Local server address\n"
" -p\t\t Local server port\n"
" -h\t\t Remote proxy address\n"
" -r\t\t Remote proxy port\n"
" -u\t\t Proxy username (optional)\n"
" -k\t\t Proxy password (optional)\n"
" -s\t\t Lookup script URL\n"
" -w\t\t Webserver port (optional, default 80)\n"
" -t\t\t Stack size in format 0x1000000 (MB)\n"
" -v\t\t Enable juicy DEBUG logging\n"
"\n"
" Example: dnsp -p 53 -l 127.0.0.1 -h 127.0.0.1 -r 8118 -w 80 -s https://www.fantuz.net/nslookup.php -t 0x1000000\n\n"
,VERSION);
exit(EXIT_FAILURE);
}
/* * Prints an error message and exit */
void error(const char *msg)
{
fprintf(stderr," *** %s: %s\n", msg, strerror(errno));
exit(EXIT_FAILURE);
}
/* * Prints debug messages */
void debug_msg(const char* fmt, ...)
{
va_list ap;
if (DEBUG) {
fprintf(stdout, " [%d]> ", getpid());
va_start(ap, fmt);
vfprintf(stdout, fmt, ap);
va_end(ap);
}
}
/* * Return the length of the pointed buffer */
size_t memlen(const char *buff)
{
size_t len = 0;
while (1) {
if (buff[len] == 0) break;
len ++;
}
return len;
}
/* Parses the dns request and returns the pointer to dns_request struct. Returns NULL on errors */
struct dns_request *parse_dns_request(const char *udp_request, size_t request_len)
{
struct dns_request *dns_req;
dns_req = malloc(sizeof(struct dns_request));
/* Transaction ID */
dns_req->transaction_id = (uint8_t)udp_request[1] + (uint16_t)(udp_request[0] << 8);
udp_request+=2;
/* Flags */
dns_req->flags = (uint8_t)udp_request[1] + (uint16_t)(udp_request[0] << 8);
udp_request+=2;
/* Questions num */
dns_req->questions_num = (uint8_t)udp_request[1] + (uint16_t)(udp_request[0] << 8);
udp_request+=2;
/* Skipping 6 not interesting bytes
uint16_t Answers number
uint16_t Records number
uint16_t Additionals records number
*/
udp_request+=6;
/* Getting the dns query */
bzero(dns_req->query, sizeof(dns_req->query));
memcpy(dns_req->query, udp_request, sizeof(dns_req->query)-1);
/* Hostname */
bzero(dns_req->hostname, sizeof(dns_req->hostname));
dns_req->hostname_len = 0;
while (1) {
uint8_t len = udp_request[0]; /* Length of the next label */
if (len == 0) {
udp_request++;
break;
}
udp_request++;
if (dns_req->hostname_len + len >= sizeof(dns_req->hostname)) {
free(dns_req);
printf("CORE: size issue in DNS request\n");
return NULL;
}
strncat(dns_req->hostname, udp_request, len); /* Append the current label to dns_req->hostname */
strncat(dns_req->hostname, ".", 1); /* Append a '.' */
dns_req->hostname_len+=len+1;
udp_request+=len;
}
/* Qtype */
dns_req->qtype = (uint8_t)udp_request[1] + (uint16_t)(udp_request[0] << 8);
udp_request+=2;
/* Qclass */
dns_req->qclass = (uint8_t)udp_request[1] + (uint16_t)(udp_request[0] << 8);
udp_request+=2;
return dns_req;
}
/* * Builds and sends the dns response datagram */
//void uild_dns_reponse(int sd, struct sockaddr_in *client, struct dns_request *xhostname, const char *ip, int mode, struct sockaddr_in *xclient)
//void build_dns_reponse(int sd, struct sockaddr_in *client, struct dns_request *dns_req, const char *ip, int mode, struct sockaddr *yclient)
void build_dns_reponse(int sd, struct sockaddr_in *yclient, struct dns_request *dns_req, const char *ip, int mode, size_t xrequestlen)
{
char *rip = malloc(256 * sizeof(char));
//struct dns_request *dns_req;
//struct sockaddr_in *client;
//char *str, *arg;
//struct readThreadParams *params = malloc(sizeof(struct readThreadParams));
//struct readThreadParams *params = (struct readThreadParams*)arg;
//str=(char*)arg;
int sockfd; // = params->xsockfd;
int xsockfd; // = params->xsockfd;
char *response,
//struct dns_request *xhostname = (struct dns_request *)xhostname->hostname;
*qhostname, // = dns_req->hostname,
*token,
*pch,
*maxim,
//*ppch,
//*typeq,
*response_ptr;
int i, ppch;
ssize_t bytes_sent;
if (DEBUG) {
//printf("BUILD-xhostname-int : %u\n", (uint32_t)strlen(xhostname));
printf("BUILD-req-query : %s\n", dns_req->query);
printf("BUILD-yclient->sin_addr.s_addr : %u\n", (uint32_t)(yclient->sin_addr).s_addr);
printf("BUILD-yclient->sin_port : %u\n", (uint32_t)(yclient->sin_port));
printf("BUILD-yclient->sin_family : %d\n", (uint32_t)(yclient->sin_family));
printf("BUILD-xrequestlen : %d\n", (uint32_t)(xrequestlen));
//printf("BUILD-client->sa_family : %u\n", (struct sockaddr)&xclient->sa_family);
//printf("BUILD-client->sa_data : %u\n", (uint32_t)client->sa_data);
printf("BUILD-xsockfd : %u\n", xsockfd);
printf("BUILD-sockfd : %d\n", sockfd);
//printf("BUILD-hostname : %s\n", qhostname);
//printf("build-qry =%s\n",(xdns_req->query));
printf("BUILD-hostname : %s\n", dns_req->hostname);
//// printf("build-host=%s\n",(char *)(xdns_req->hostname));
//// printf("build-answ=%s\n", rip);
//// printf("build-anmd=%d\n", DNS_MODE_ANSWER);
}
response = malloc (UDP_DATAGRAM_SIZE);
bzero(response, UDP_DATAGRAM_SIZE);
maxim = malloc (DNSREWRITE);
bzero(maxim, DNSREWRITE);
response_ptr = response;
//maxim_ptr = maxim;
/* Transaction ID */
response[0] = (uint8_t)(dns_req->transaction_id >> 8);
response[1] = (uint8_t)dns_req->transaction_id;
response+=2;
if (mode == DNS_MODE_ANSWER) {
/* Default flags for a standard query (0x8580) */
/* Shall it be authoritative answer... or not ? :) */
//response[0] = 0x81;
response[0] = 0x85;
response[1] = 0x80;
response+=2;
/* Questions 1 */
response[0] = 0x00;
response[1] = 0x01;
response+=2;
/* Answers 1 */
response[0] = 0x00;
response[1] = 0x01;
response+=2;
} else if (mode == DNS_MODE_ERROR) {
//} else {
/* DNS_MODE_ERROR should truncate message instead of building it up ... */
/* Server failure (0x8182), but what if we want NXDOMAIN (0x....) ???*/
/*
* NOERROR (RCODE:0) : DNS Query completed successfully
* FORMERR (RCODE:1) : DNS Query Format Error
* SERVFAIL (RCODE:2) : Server failed to complete the DNS request
* NXDOMAIN (RCODE:3) : Domain name does not exist
* NOTIMP (RCODE:4) : Function not implemented
* REFUSED (RCODE:5) : The server refused to answer for the query
* YXDOMAIN (RCODE:6) : Name that should not exist, does exist
* XRRSET (RCODE:7) : RRset that should not exist, does exist
* NOTAUTH (RCODE:9) : Server not authoritative for the zone
* NOTZONE (RCODE:10) : Name not in zone
* 11-15 available for assignment
* 16 BADVERS Bad OPT Version
* 16 BADSIG TSIG Signature Failure
* 17 BADKEY Key not recognized
* 18 BADTIME Signature out of time window
* 19 BADMODE Bad TKEY Mode
* 20 BADNAME Duplicate key name
* 21 BADALG Algorithm not supported
* 22-3840 available for assignment
* 0x0016-0x0F00
* 3841-4095 Private Use
* 0x0F01-0x0FFF
* 4096-65535 available for assignment
* 0x1000-0xFFFF
* */
response[0] = 0x81;
response[1] = 0x82;
response+=2;
/* Questions 1 */
response[0] = 0x00;
response[1] = 0x01;
response+=2;
/* Answers 0 */
response[0] = 0x00;
response[1] = 0x00;
response+=2;
}
/* Authority RRs 0 */
response[0] = 0x00;
response[1] = 0x00;
response+=2;
/* Additional RRs 0 */
response[0] = 0x00;
response[1] = 0x00;
response+=2;
/* Query */
strncat(response, dns_req->query, dns_req->hostname_len);
//printf("BUILD-INSIDE-dns_req->query : %s\n",(dns_req->query));
response+=dns_req->hostname_len+1;
/* Type */
response[0] = (uint8_t)(dns_req->qtype >> 8);
response[1] = (uint8_t)dns_req->qtype;
response+=2;
/* Class */
response[0] = (uint8_t)(dns_req->qclass >> 8);
response[1] = (uint8_t)dns_req->qclass;
response+=2;
/* Answer */
if (mode == DNS_MODE_ANSWER) {
/* Pointer to host name in query section */
response[0] = 0xc0;
response[1] = 0x0c;
response+=2;
if (dns_req->qtype == 0x0f) { //MX
response[0] = 0x00;
response[1] = 0x0f;
response+=2;
} else if (dns_req->qtype == 0xFF) { //ALL
response[0] = 0x00;
response[1] = 0xFF;
response+=2;
} else if (dns_req->qtype == 0x01) { //A
*response++ = 0x00;
*response++ = 0x01;
} else if (dns_req->qtype == 0x05) { //CNAME
response[0] = 0x00;
response[1] = 0x05;
response+=2;
} else if (dns_req->qtype == 0x0c) { //PTR
response[0] = 0x00;
response[1] = 0x0c;
response+=2;
} else if (dns_req->qtype == 0x02) { //NS
response[0] = 0x00;
response[1] = 0x02;
response+=2;
} else { return; }
/* Class IN */
*response++ = 0x00;
*response++ = 0x01;
/* TTL (4 hours) */
*response++ = 0x00;
*response++ = 0x00;
*response++ = 0x38;
*response++ = 0x40;
//ptr,ns
if (dns_req->qtype == 0x0c || dns_req->qtype == 0x02) {
/* Data length (4 bytes)*/
response[0] = 0x00;
response[1] = 0x04;
response+=2;
response[0] = 0xc0;
response[1] = 0x0c;
response+=2;
} else if (dns_req->qtype == 0x05) { //CNAME RECORD
/* Data length (4 bytes)*/
response[0] = 0x00;
response[1] = (strlen(ip)+1);
response+=2;
pch = strtok((char *)ip,".\r\n\t");
while (pch != NULL)
{
ppch = strlen(pch);
*response++ = strlen(pch);
for (i = 0; i < strlen(pch); ++i) {
*response++ = pch[i];
maxim[i] = pch[i];
}
pch = strtok (NULL, ".");
if (pch == NULL) {
for (i = 0; i < ppch+1; ++i) {
response--;
}
*response++ = ppch-1;
for (i = 0; i < ppch-1; ++i) {
*response++ = maxim[i];
}
}
}
*response++ = 0x00;
} else if (dns_req->qtype == 0x0f) { //MX RECORD
/* Data length (4 bytes)*/
response[0] = 0x00;
response[1] = (strlen(ip)+3);
//response[1] = 0x00;
response+=2;
/* PRIO (4 bytes)*/
response[0] = 0x00;
response[1] = 0x0a;
response+=2;
/* POINTER, IF YOU ARE SO BRAVE OR ABLE TO USE IT (4 bytes) -> do not use label then... so you should re-write the code to have super-duper minimal responses. That code would also need domain comparison, to see if suffix can be appended */
//response[0] = 0xc0;
//response[1] = 0x0c;
//response+=2;
pch = strtok((char *)ip,".\r\n\t");
while (pch != NULL)
{
//maxim = NULL;
ppch = strlen(pch);
*response++ = strlen(pch);
for (i = 0; i < strlen(pch); ++i) {
*response++ = pch[i];
//maxim[0] += 0x00;
maxim[i] = pch[i];
}
//strcat(response, pch);
//*response++ = *maxim;
pch = strtok (NULL, ".");
if (pch == NULL) {
for (i = 0; i < ppch+1; ++i) {
response--;
}
*response++ = ppch-1;
for (i = 0; i < ppch-1; ++i) {
*response++ = maxim[i];
}
}
}
*response++ = 0x00;
} else if (dns_req->qtype == 0x01) { // A RECORD
/* Data length (4 bytes)*/
*response++ = 0x00;
*response++ = 0x04;
token = strtok((char *)ip,".");
if (token != NULL) response[0] = atoi(token);
else return;
token = strtok(NULL,".");
if (token != NULL) response[1] = atoi(token);
else return;
token = strtok(NULL,".");
if (token != NULL) response[2] = atoi(token);
else return;
token = strtok(NULL,".");
if (token != NULL) response[3] = atoi(token);
else return;
response+=4;
//fprintf(stdout, "DNS_MODE_COMPLETE\n");
} else {
fprintf(stdout, "DNS_MODE_ISSUE\n");
return;
}
//*response++=(unsigned char)(strlen(ip)+1);
//memcpy(response,ip,strlen(ip)-1);
//strncpy(response,ip,strlen(ip)-1);
//recvfrom(3, "\326`\1 \0\1\0\0\0\0\0\1\6google\2it\0\0\1\0\1\0\0)\20\0"..., 256, 0, {sa_family=AF_INET, sin_port=htons(48379), sin_addr=inet_addr("192.168.2.84")}, [16]) = 38
//(3, "\24\0\0\0\26\0\1\3\23\306;U\0\0\0\0\0\0\0\0", 20, 0, {sa_family=AF_NETLINK, pid=0, groups=00000000}, 12) = 20
//(3, "z\271\205\200\0\1\0\1\0\0\0\0\6google\2hr\0\0\2\0\1\300\f\0\2\0"..., 41, 0, {sa_family=0x1a70 /* AF_??? */, sa_data="s\334\376\177\0\0\20D\1\270\223\177\0\0"}, 8)
//(struct sockaddr *)xclient->sin_family = AF_INET;
//printf("finishing .. , %d\n",inet_aton("192.168.2.84", &(yclient->sin_addr)));
int yclient_len = sizeof(yclient);
yclient->sin_family = AF_INET;
//yclient->sin_addr.s_addr = inet_addr("192.168.2.84");
//yclient->sin_port = htons(yclient->sin_port);
yclient->sin_port = yclient->sin_port;
memset(&(yclient->sin_zero), 0, sizeof(yclient->sin_zero)); // zero the rest of the struct
//memset(yclient, 0, 0);
if (DEBUG) {
printf("BUILD-INSIDE-response : %s\n", response);
printf("BUILD-INSIDE-yclient->sin_addr.s_addr : %u\n", (uint32_t)(yclient->sin_addr).s_addr);
printf("BUILD-INSIDE-yclient->sin_port : %u\n", (uint32_t)(yclient->sin_port));
printf("BUILD-INSIDE-yclient->sin_port : %u\n", htons(yclient->sin_port));
printf("BUILD-INSIDE-yclient->sin_family : %d\n", (uint32_t)(yclient->sin_family));
printf("BUILD-INSIDE-dns-req->hostname : %s\n", dns_req->hostname);
printf("BUILD-INSIDE-dns_req->query : %s\n", dns_req->query);
printf("BUILD-INSIDE-xrequestlen : %u\n", (uint16_t)xrequestlen);
// printf("BUILD-INSIDE-xdns_req->query : %s\n", xdns_req->query);
// printf("BUILD-INSIDE-xdns_req->hostname-to-char : %s\n", (char *)(xdns_req->hostname));
// printf("BUILD-INSIDE-xdns_req->hostname : %s\n", xdns_req->hostname);
// printf("BUILD-INSIDE-xdns_req->query : %s\n", xdns_req->query);
}
//bytes_sent = sendto(sd, response_ptr, response - response_ptr, 0, (struct sockaddr *)(&yclient), sizeof(yclient));
bytes_sent = sendto(sd, response_ptr, response - response_ptr, 0, (struct sockaddr *)yclient, 16);
//bytes_sent = sendto(3, "\270\204\205\200\0\1\0\1\0\0\0\0\6google\2jp\0\0\1\0\1\300\f\0\1\0"..., 43, 0, {sa_family=0x0002 /* AF_??? */, sa_data="\365\366\374\177\0\0\1\0\0\0\3\0\0\0"}, 16)
//fdatasync(sd);
close(sd);
} else if (mode == DNS_MODE_ERROR) {
/* Are we into "No such name" ?... just an NXDOMAIN ?? */
fprintf(stdout, "DNS_MODE_ERROR\n");
bytes_sent = sendto(sd, response_ptr, response - response_ptr, 0, (struct sockaddr *)yclient, 16);
//fdatasync(sd);
close(sd);
} else {
fprintf(stdout, "DNS_MODE_UNKNOWN\n");
bytes_sent = sendto(sd, response_ptr, response - response_ptr, 0, (struct sockaddr *)yclient, 16);
//fdatasync(sd);
close(sd);
}
/* DNS VOLUME CALCULATION */
//debug_msg("Dns response sent to client (DEC %d bytes)\n", bytes_sent);
if (DEBUG) {
printf("SENT %d bytes", (uint32_t)bytes_sent);
}
free(response_ptr);
free(dns_req);
//free(ip);
//return 0;
}
/* * libCurl write data callback */
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
size_t stream_size;
stream_size = size * nmemb + 1;
bzero(stream, HTTP_RESPONSE_SIZE);
memcpy(stream, ptr, stream_size);
return 0;
}
/* * Hostname lookup * Return: * OK: Resolved IP * KO: Null */
char *lookup_host(const char *host, const char *proxy_host, unsigned int proxy_port, const char *proxy_user, const char *proxy_pass, const char *lookup_script, const char *typeq, unsigned int wport)
{
CURL *ch;
CURLSH *curlsh;
char *http_response,
*script_url;
int ret;
struct curl_slist *hosting = NULL;
struct curl_slist *list = NULL;
script_url = malloc(URL_SIZE);
http_response = malloc(HTTP_RESPONSE_SIZE);
bzero(script_url, URL_SIZE);
// OPTIONS --> add resolver & CURL headers
/*
CALLBACK TO PHP, BEHIND WHICH SITS THE "REAL" RESOLVER
CAN BE HIDDEN BY MANUAL RESOLVE OVERRIDE, I.E.
--resolve my.site.com:80:1.2.3.4, -H "Host: my.site.com"
*/
snprintf(script_url, URL_SIZE-1, "%s?host=%s&type=%s", lookup_script, host, typeq);
/* curl setup */
ch = curl_easy_init();
curlsh = curl_share_init();
curl_easy_setopt(ch, CURLOPT_URL, script_url);
curl_easy_setopt(ch, CURLOPT_PORT, wport); //80
curl_easy_setopt(ch, CURLOPT_DNS_CACHE_TIMEOUT, 900);
curl_easy_setopt(ch, CURLOPT_DNS_USE_GLOBAL_CACHE, 1); /* DNS CACHE */
curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1L); /* No progress meter */
curl_easy_setopt(ch, CURLOPT_PROXY, proxy_host);
curl_easy_setopt(ch, CURLOPT_PROXYPORT, proxy_port); /* 8118 */
curl_easy_setopt(ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_easy_setopt(ch, CURLOPT_VERBOSE, 0); /* Verbose OFF */
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0)
curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, 0L);;
/* option proxy username and password */
if ((proxy_user != NULL) && (proxy_pass != NULL)) {
curl_easy_setopt(ch, CURLOPT_PROXYUSERNAME, proxy_user);
curl_easy_setopt(ch, CURLOPT_PROXYPASSWORD, proxy_pass);
}
// curl -H "Host: www.fantuz.net" -H "Remote Address:217.114.216.51:80" -H "Request URL:http://www.fantuz.net/nslookup.php" -H "Host:www.fantuz.net" -H "User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36" http://www.fantuz.net/nslookup.php?host=fantuz.net
/* try to see.... */
//curl_easy_setopt(ch, CURLINFO_HEADER_OUT, "" );
//curl_easy_setopt(ch, CURLOPT_HEADER, 1L);
//list = curl_slist_append(list, "Shoesize: 10");
//list = curl_slist_append(list, "Accept:");
//// OPTION --> HEADERS
// list = curl_slist_append(list, "Host: www.fantuz.net");
// list = curl_slist_append(list, "Remote Address: 217.114.216.51:80");
// list = curl_slist_append(list, "Request URL: http://www.fantuz.net/nslookup.php");
// list = curl_slist_append(list, "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36");
// curl_easy_setopt(ch, CURLOPT_HTTPHEADER, list);
//hosting = curl_slist_append(hosting, "www.fantuz.net:80:217.114.216.51");
//curl_easy_setopt(ch, CURLOPT_RESOLVE, hosting);
curl_easy_setopt(ch, CURLOPT_MAXCONNECTS, MAXCONN);
curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 0); /* HOW DOES A NEW TCP INFLUENCE WEB CACHE ?? */
curl_easy_setopt(ch, CURLOPT_FORBID_REUSE, 0);
//curl_setopt ($curl, CURLOPT_AUTOREFERER, 1);
//// OPTION --> FOLLOW-LOCATION
//curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, 1);
//CURL_LOCK_DATA_SHARE
//curl_share_setopt(curlsh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
/* Problem in performing the http request ?? */
curl_share_setopt(curlsh, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
curl_easy_setopt(ch, CURLOPT_SHARE, curlsh);
curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, write_data); /* Set write function */
curl_easy_setopt(ch, CURLOPT_WRITEDATA, http_response);
ret = curl_easy_perform(ch);
//if ((ret < 0) || (ret > 0)) {
if (ret < 0) {
debug_msg ("Error performing HTTP request (Error %d) - spot on !!!\n");
printf("Error performing HTTP request (Error %d) - spot on !!!\n",ret);
curl_easy_cleanup(ch);
//curl_share_cleanup(curlsh);
free(script_url);
curl_slist_free_all(list);
curl_slist_free_all(hosting);
//free(http_response);
return NULL;
}
/* Can't resolve host */
if ((strlen(http_response) > 256) || (strncmp(http_response, "0.0.0.0", 7) == 0)) {
/* insert error answers here, as NXDOMAIN, SERVFAIL etc */
printf("CORE: MALFORMED DNS, possible SERVFAIL from origin... investigate !\n");
curl_easy_cleanup(ch);
free(script_url);
curl_slist_free_all(list);
curl_slist_free_all(hosting);
printf("inside curl (MALF) .... %s",http_response);
//return http_response;
//free(http_response);
http_response = "0.0.0.0\r\n";
return NULL;
}
printf("inside curl .... %s",http_response);
curl_easy_cleanup(ch);
free(script_url);
curl_slist_free_all(list);
curl_slist_free_all(hosting);
return http_response;
}
/* This is our thread function. It is like main(), but for a thread*/
void *threadFunc(void *arg)
{
// BoundedBuffer *buffer = params->b;
struct readThreadParams *params = (struct readThreadParams*)arg;
//struct dns_request *xdns_req = malloc(sizeof(struct dns_request));
//struct dns_request *xdns_req = (struct dns_request *)dns_request;
struct dns_request *xdns_req = (struct dns_request *)params->xhostname;
//struct sockaddr_in *yclient = malloc(sizeof(struct sockaddr_in));
struct sockaddr_in *yclient = (struct sockaddr_in *)params->yclient;
//struct sockaddr_in *xclient = (struct sockaddr_in *)params->xclient;
struct dns_request *dns_req = malloc(sizeof(struct dns_request));
struct dns_request *xhostname = (struct dns_request *)params->xhostname;
//struct dns_request *xhostname = (struct dns_request *)params->xhostname->hostname;
//struct dns_request *xhostname = malloc(sizeof(struct dns_request));
//char* xhostname = params->xhostname;
char *yhostname = (char *)params->xhostname->hostname;
//struct sockaddr_in *yclient = (struct sockaddr_in *)params->xclient;
//struct sockaddr_in *xdns_req = malloc(sizeof(struct dns_request));
//struct dns_req *dns_req ; //= (struct dns_request *)params->xhostname;
//struct dns_request *dns_req = (struct dns_request *)params->xhostname;
//struct dns_request *dns_req;
char *str;
//int test = params->digit;
int proxy_port = params->xproxy_port;
int wport = params->xwport;
char* data = params->input;
char* proxy_user = params->xproxy_user;
char* proxy_pass = params->xproxy_pass;
char* proxy_host = params->xproxy_host;
char* lookup_script = params->xlookup_script;
char* typeq = params->xtypeq;
int xsockfd = params->xsockfd;
int sockfd = params->sockfd;
size_t request_len = params->xrequestlen;
//int request_len = params->xrequestlen;
int ret;
char *rip = malloc(256 * sizeof(char));
char *ip = NULL;
pthread_key_t key_i;
pthread_key_create(&key_i, NULL);
//str=(char*)arg;
//if (pthread_mutex_trylock(&mutex)) {
if (pthread_mutex_lock(&mutex)) {
printf("init lock OK ... \n");
} else {
printf("init lock NOT OK ... \n");
}
if (DEBUG) {
//char *p = &xclient->sin_addr.s_addr;
char *s = inet_ntoa(yclient->sin_addr);
printf("test: %s\n",(char *)params->xhostname->hostname);
printf("test: %s\n",(char *)params->xdns_req->hostname);
printf("test: %s\n",(char *)xdns_req->hostname);
printf("VARIABLE-RECV: %d\n", (uint32_t)(yclient->sin_addr).s_addr);
printf("VARIABLE-RECV: %s\n", s);
printf("VARIABLE-RECV: %s\n", lookup_script);
//printf("VARIABLE-RECV: %s\n", dns_req->hostname);
printf("VARIABLE-RECV: %s\n", yhostname);
//printf("VARIABLE-RECV: %d\n", (size_t)xhostname->hostname_len);
}
rip = lookup_host(yhostname, proxy_host, proxy_port, proxy_user, proxy_pass, lookup_script, typeq, wport);
pthread_setspecific(glob_var_key_ip, rip);
if (DEBUG) {
printf("VARIABLE-RET-HTTP: %d", ret);
printf("VARIABLE-RET-HTTP: %s", rip);
//pthread_setspecific(glob_var_key_ip, rip);
//pthread_getspecific(glob_var_key_ip);
printf("VARIABLE-RET-HTTP-GLOBAL: %x\n", glob_var_key_ip);
//printf("VARIABLE-HTTP: %x\n", pthread_getspecific(glob_var_key_ip));
//printf("build: %s", inet_ntop(AF_INET, &ip_header->saddr, ipbuf, sizeof(ipbuf)));
}
//if (rip != NULL) {
//if ((rip != NULL) && (rip != "0.0.0.0"))
if ((rip != NULL) && (strncmp(rip, "0.0.0.0", 7) != 0)) {
if (DEBUG) {
printf("THREAD-V-ret : [%d]\n",ret);
printf("THREAD-V-type : %d\n", dns_req->qtype);
printf("THREAD-V-type : %s\n", typeq);
printf("THREAD-V-size : %u\n", (uint32_t)request_len);
printf("THREAD-V-socket-sockfd : %u\n", sockfd);
printf("THREAD-V-socket-xsockfd : %u\n", xsockfd);
printf("THREAD-V-socket-xsockfd : %d\n", xsockfd);
printf("THREAD-V-MODE-ANSWER : %d\n", DNS_MODE_ANSWER);
printf("THREAD-V-xclient->sin_addr.s_addr : %u\n", (uint32_t)(yclient->sin_addr).s_addr);
printf("THREAD-V-xclient->sin_port : %u\n", (uint32_t)(yclient->sin_port));
printf("THREAD-V-xclient->sin_family : %u\n", (uint32_t)(yclient->sin_family));
printf("THREAD-V-answer : %s\n", rip);
printf("THREAD-V-xhostname : %s\n", yhostname);
printf("THREAD-V-dns-req->hostname : %s\n", dns_req->hostname);
printf("THREAD-V-dns_req->query : %s\n", dns_req->query);
printf("THREAD-V-dns_req->query : %s\n", xdns_req->query);
printf("THREAD-V-xdns_req->hostname-to-char : %s\n", (char *)(xdns_req->hostname));
printf("THREAD-V-xdns_req->hostname : %s\n", xdns_req->hostname);
printf("THREAD-V-xdns_req->query : %s\n", xdns_req->query);
}
build_dns_reponse(sockfd, yclient, xhostname, rip, DNS_MODE_ANSWER, request_len);
//printf("THREAD-V-xclient->sin_addr.s_addr : %s\n",(char *)(xclient->sin_family));
//build_dns_reponse(sockfd, xclient, dns_req, rip, DNS_MODE_ANSWER);
//build_dns_reponse(xsockfd, xclient, dns_req, rip, DNS_MODE_ANSWER);
} else if ( strstr(dns_req->hostname, "hamachi.cc") != NULL ) {
printf("BALCKLIST: pid [%d] - name %s - host %s - size %d \r\n", getpid(), dns_req->hostname, rip, (uint32_t)request_len);
printf("BLACKLIST: xsockfd %d - hostname %s \r\n", xsockfd, xdns_req->hostname);
printf("BLACKLIST: xsockfd %d - hostname %s \r\n", xsockfd, yhostname);
//build_dns_reponse(xsockfd, xclient, dns_req, rip, DNS_MODE_ANSWER);
build_dns_reponse(xsockfd, yclient, xhostname, rip, DNS_MODE_ANSWER, request_len);
} else if ( rip == "0.0.0.0" ) {
printf("ERROR: pid [%d] - name %s - host %s - size %d \r\n", getpid(), dns_req->hostname, rip, (uint32_t)request_len);
printf("ERROR: xsockfd %d - hostname %s \r\n", xsockfd, yhostname);
printf("Generic resolution problem \n");
//build_dns_reponse(xsockfd, xclient, dns_req, rip, DNS_MODE_ERROR);
build_dns_reponse(xsockfd, yclient, xhostname, rip, DNS_MODE_ERROR, request_len);
}
//rip = NULL;
//free(rip);
//printf("\nfreeing up...\n");
//char *s = inet_ntoa(xclient->sin_addr);
//pthread_exit(s);
//pthread_setspecific(glob_var_key_ip, NULL);
// pthread_exit(0);
if (pthread_mutex_unlock(&mutex)) {
printf("unlock OK..\n");
} else {
printf("unlock NOT OK..\n");
}
pthread_mutex_destroy(&mutex);
printf("destroy OK..\n");
pthread_exit(NULL);
}
/* * main */
int main(int argc, char *argv[])
{
int sockfd, port = DEFAULT_LOCAL_PORT, wport = DEFAULT_WEB_PORT, proxy_port = 0, c;
int r = 0;
struct sockaddr_in serv_addr;
struct hostent *local_address;
char *bind_address = NULL, *proxy_host = NULL, *proxy_user = NULL,
*proxy_pass = NULL, *typeq = NULL, *lookup_script = NULL;
opterr = 0;
DEBUG = 0;
////sem_t mutex;
int s, tnum, opt, num_threads;
struct thread_info *tinfo;
pthread_attr_t attr;
int stack_size;
void *res;
int thr = 0;
//int *ptr[2];
int *ptr[2];
//struct sockaddr_in *ptr[2];
/* The "-s" option specifies a stack size for our threads */
stack_size = -1;
/* Command line args */
while ((c = getopt (argc, argv, "s:p:l:r:h:t:w:u:k:v::")) != -1)
switch (c)
{
case 't':
stack_size = strtoul(optarg, NULL, 0);
fprintf(stdout," *** Stack size %d\n",stack_size);
break;
case 'p':
port = atoi(optarg);
if (port <= 0) {
fprintf(stdout," *** Invalid local port\n");
exit(EXIT_FAILURE);
}
break;
case 'w':
wport = atoi(optarg);
if (wport <= 0) {
fprintf(stdout," *** Invalid webserver port\n");
exit(EXIT_FAILURE);
}
break;
case 'r':
proxy_port = atoi(optarg);
if (proxy_port <= 0) {
fprintf(stdout," *** Invalid proxy port\n");
exit(EXIT_FAILURE);
}