Fix incorrect stristr() parameter order
[squirrelmail.git] / plugins / filters / bulkquery / bulkquery.c
CommitLineData
b1b8988d 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
6ade76e0 13#define DEFTTL 600
b1b8988d 14
15extern int errno;
16extern int h_errno;
17
18
19struct ipnode;
20typedef struct ipnode *iplist;
21struct ipnode {
22 char *IP;
23 iplist next;
24};
25
26iplist IPs;
27
28pthread_mutex_t *mutexp;
29pthread_mutex_t *mutexoutput;
30
31char *dnsrbls[MAXRBLS];
6ade76e0 32int numrbls, numthreads, numqueries, defttl;
b1b8988d 33
34void 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");
e50f5ac2 52 return;
b1b8988d 53 } else {
54 lwres_conf_parse(ctx, lwres_resolv_conf);
55 //pthread_mutex_lock(mutexoutput);
e50f5ac2 56 n = lwres_getrdatabyname(ctx, tIP->IP, ns_c_in, ns_t_a, 0, &response);
b1b8988d 57 //pthread_mutex_unlock(mutexoutput);
e50f5ac2 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");
6ade76e0 67 printf ("%s, %s, %d\n", tIP->IP, tIP->IP, defttl);
e50f5ac2 68 }
69 //fprintf (stderr, "freeing context\n"); fflush(stderr);
70 lwres_context_destroy(&ctx);
71 //fprintf (stderr, "done freeing\n"); fflush(stderr);
b1b8988d 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
83void 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);
e50f5ac2 94 exit (10);
b1b8988d 95 } else {
96 strcpy (dnsrbls[numrbls], instr);
97 numrbls++;
98 }
99 }
100}
101
102
103main () {
104 pthread_t threads[MAXTHREADS];
105 char instr[MAXSTR];
106 iplist tIP;
107 int loop1;
108
6ade76e0 109 if (fgets(instr, MAXSTR, stdin) != NULL) {
110 defttl = atoi(instr);
111 }
112 if (defttl < 0)
113 defttl = DEFTTL;
114
b1b8988d 115 GetRBLs();
116
117// for (loop1=0; loop1<numrbls; loop1++)
118// fprintf (stderr, "%s\n", dnsrbls[loop1]);
119// fprintf (stderr, "----------\n");
120
121 numqueries = 0;
122 IPs = NULL;
123 while (fgets(instr, MAXSTR, stdin) != NULL) {
124 instr[strlen(instr)-1] = 0;
125 for (loop1 = 0; loop1 < numrbls; loop1++) {
126 tIP = (iplist)malloc(sizeof(struct ipnode));
127 tIP->IP = (char *)malloc(strlen(instr)+strlen(dnsrbls[loop1])+2);
128 strcpy (tIP->IP, instr);
e50f5ac2 129 strcat (tIP->IP, dnsrbls[loop1]);
b1b8988d 130 tIP->next = IPs;
131 IPs = tIP;
132 numqueries++;
133 }
134 }
135
136// fprintf (stderr, "%d queries to make\n", numqueries);
137// tIP = IPs;
138// while (tIP != NULL) {
139// fprintf (stderr, "%s\n", tIP->IP);
140// tIP = tIP->next;
141// }
142// fprintf (stderr, "done\n");
143// exit (0);
144
145 mutexp=(pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
146 pthread_mutex_init(mutexp, NULL);
147 mutexoutput=(pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
148 pthread_mutex_init(mutexoutput, NULL);
149
150 numthreads = 0; // number of threads created successfully
151 for (loop1 = 0; ((loop1<MAXTHREADS) && (loop1<numqueries)); loop1++) {
152 if (pthread_create(&threads[loop1], NULL,
153 (void *) do_queries, NULL) != 0) {
e50f5ac2 154 fprintf (stderr, "Couldn't make more than %d threads\n", numthreads);
b1b8988d 155 break;
156 } else {
157 numthreads++;
158 }
159 }
160
161 for (loop1 = 0; loop1 < numthreads ; loop1++) {
162 pthread_join(threads[loop1], NULL);
163 }
164
165//do_queries();
166
167 exit (0);
168}