Added a comment in the ChangeLog
[squirrelmail.git] / plugins / filters / bulkquery / bulkquery.c
1
2 #include <arpa/nameser.h>
3 #include <lwres/lwres.h>
4 #include <strings.h>
5 #include <stdio.h>
6 #include <pthread.h>
7
8
9 #define BUFLEN 1024
10 #define MAXSTR 80
11 #define MAXTHREADS 50
12 #define MAXRBLS 40
13 #define DEFTTL 7200
14
15 extern int errno;
16 extern int h_errno;
17
18
19 struct ipnode;
20 typedef struct ipnode *iplist;
21 struct ipnode {
22 char *IP;
23 iplist next;
24 };
25
26 iplist IPs;
27
28 pthread_mutex_t *mutexp;
29 pthread_mutex_t *mutexoutput;
30
31 char *dnsrbls[MAXRBLS];
32 int numrbls, numthreads, numqueries;
33
34 void do_queries () {
35 iplist tIP;
36 lwres_context_t *ctx = NULL;
37 lwres_grbnresponse_t *response = NULL;
38 int n, i;
39
40
41 pthread_mutex_lock(mutexp);
42 tIP = IPs;
43 if (IPs != NULL) {
44 IPs = tIP->next;
45 }
46 pthread_mutex_unlock(mutexp);
47
48 while (tIP != NULL) {
49 //fprintf (stderr, "making query %s\n", tIP->IP); fflush(stderr);
50 if (lwres_context_create(&ctx, NULL, NULL, NULL, 0) != 0) {
51 fprintf (stderr, "Couldn't create context\n");
52 return;
53 } else {
54 lwres_conf_parse(ctx, lwres_resolv_conf);
55 //pthread_mutex_lock(mutexoutput);
56 n = lwres_getrdatabyname(ctx, tIP->IP, ns_c_in, ns_t_a, 0, &response);
57 //pthread_mutex_unlock(mutexoutput);
58 if (n == LWRES_R_SUCCESS) {
59 printf ("%s,%d.%d.%d.%d,%d\n", tIP->IP,
60 response->rdatas[0][0], response->rdatas[0][1],
61 response->rdatas[0][2], response->rdatas[0][3],
62 response->ttl);
63 //fprintf (stderr, "freeing response\n"); fflush(stderr);
64 lwres_grbnresponse_free(ctx, &response);
65 } else {
66 //fprintf (stderr, "Nothing found\n");
67 printf ("%s, %s, %d\n", tIP->IP, tIP->IP, DEFTTL);
68 }
69 //fprintf (stderr, "freeing context\n"); fflush(stderr);
70 lwres_context_destroy(&ctx);
71 //fprintf (stderr, "done freeing\n"); fflush(stderr);
72 }
73
74 pthread_mutex_lock(mutexp);
75 tIP = IPs;
76 if (IPs != NULL) {
77 IPs = tIP->next;
78 }
79 pthread_mutex_unlock(mutexp);
80 }
81 }
82
83 void GetRBLs() {
84 char instr[MAXSTR];
85 numrbls = 0;
86 while ((fgets(instr, MAXSTR, stdin) != NULL) && (numrbls < MAXRBLS)) {
87 instr[strlen(instr)-1] = 0; // strip off newline
88 if (strncmp(instr, "----------", 10) == 0) {
89 return;
90 }
91 dnsrbls[numrbls] = (char *) malloc(strlen(instr)+1);
92 if (dnsrbls[numrbls] == NULL) {
93 fprintf (stderr, "Couldn't allocate memory for %d DNS RBLs\n", numrbls);
94 exit (10);
95 } else {
96 strcpy (dnsrbls[numrbls], instr);
97 numrbls++;
98 }
99 }
100 }
101
102
103 main () {
104 pthread_t threads[MAXTHREADS];
105 char instr[MAXSTR];
106 iplist tIP;
107 int loop1;
108
109 GetRBLs();
110
111 // for (loop1=0; loop1<numrbls; loop1++)
112 // fprintf (stderr, "%s\n", dnsrbls[loop1]);
113 // fprintf (stderr, "----------\n");
114
115 numqueries = 0;
116 IPs = NULL;
117 while (fgets(instr, MAXSTR, stdin) != NULL) {
118 instr[strlen(instr)-1] = 0;
119 for (loop1 = 0; loop1 < numrbls; loop1++) {
120 tIP = (iplist)malloc(sizeof(struct ipnode));
121 tIP->IP = (char *)malloc(strlen(instr)+strlen(dnsrbls[loop1])+2);
122 strcpy (tIP->IP, instr);
123 strcat (tIP->IP, dnsrbls[loop1]);
124 tIP->next = IPs;
125 IPs = tIP;
126 numqueries++;
127 }
128 }
129
130 // fprintf (stderr, "%d queries to make\n", numqueries);
131 // tIP = IPs;
132 // while (tIP != NULL) {
133 // fprintf (stderr, "%s\n", tIP->IP);
134 // tIP = tIP->next;
135 // }
136 // fprintf (stderr, "done\n");
137 // exit (0);
138
139 mutexp=(pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
140 pthread_mutex_init(mutexp, NULL);
141 mutexoutput=(pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
142 pthread_mutex_init(mutexoutput, NULL);
143
144 numthreads = 0; // number of threads created successfully
145 for (loop1 = 0; ((loop1<MAXTHREADS) && (loop1<numqueries)); loop1++) {
146 if (pthread_create(&threads[loop1], NULL,
147 (void *) do_queries, NULL) != 0) {
148 fprintf (stderr, "Couldn't make more than %d threads\n", numthreads);
149 break;
150 } else {
151 numthreads++;
152 }
153 }
154
155 for (loop1 = 0; loop1 < numthreads ; loop1++) {
156 pthread_join(threads[loop1], NULL);
157 }
158
159 //do_queries();
160
161 exit (0);
162 }
163