/[cvs]/eggdrop1.8/src/dns.c
ViewVC logotype

Contents of /eggdrop1.8/src/dns.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Sat Jun 16 16:04:02 2012 UTC (6 years, 11 months ago) by thommey
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +7 -3 lines
File MIME type: text/x-chdr
Call Tcl's bgerror on Eggdrop background errors.

1 /*
2 * dns.c -- handles:
3 * DNS resolve calls and events
4 * provides the code used by the bot if the DNS module is not loaded
5 * DNS Tcl commands
6 *
7 * $Id: dns.c,v 1.2 2010/08/05 18:12:05 pseudo Exp $
8 */
9 /*
10 * Written by Fabian Knittel <fknittel@gmx.de>
11 *
12 * Copyright (C) 1999 - 2010 Eggheads Development Team
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29 #include "main.h"
30 #include <netdb.h>
31 #include <setjmp.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35
36 #include "dns.h"
37
38 extern struct dcc_t *dcc;
39 extern int dcc_total;
40 extern int resolve_timeout;
41 extern time_t now;
42 extern sigjmp_buf alarmret;
43 extern Tcl_Interp *interp;
44
45 devent_t *dns_events = NULL;
46
47
48 /*
49 * DCC functions
50 */
51
52 void dcc_dnswait(int idx, char *buf, int len)
53 {
54 /* Ignore anything now. */
55 }
56
57 void eof_dcc_dnswait(int idx)
58 {
59 putlog(LOG_MISC, "*", "Lost connection while resolving hostname [%s/%d]",
60 iptostr(&dcc[idx].sockname.addr.sa), dcc[idx].port);
61 killsock(dcc[idx].sock);
62 lostdcc(idx);
63 }
64
65 static void display_dcc_dnswait(int idx, char *buf)
66 {
67 sprintf(buf, "dns waited %lis", (long) now - dcc[idx].timeval);
68 }
69
70 static int expmem_dcc_dnswait(void *x)
71 {
72 register struct dns_info *p = (struct dns_info *) x;
73 int size = 0;
74
75 if (p) {
76 size = sizeof(struct dns_info);
77 if (p->host)
78 size += strlen(p->host) + 1;
79 if (p->cbuf)
80 size += strlen(p->cbuf) + 1;
81 }
82 return size;
83 }
84
85 static void kill_dcc_dnswait(int idx, void *x)
86 {
87 register struct dns_info *p = (struct dns_info *) x;
88
89 if (p) {
90 if (p->host)
91 nfree(p->host);
92 if (p->cbuf)
93 nfree(p->cbuf);
94 nfree(p);
95 }
96 }
97
98 struct dcc_table DCC_DNSWAIT = {
99 "DNSWAIT",
100 DCT_VALIDIDX,
101 eof_dcc_dnswait,
102 dcc_dnswait,
103 0,
104 0,
105 display_dcc_dnswait,
106 expmem_dcc_dnswait,
107 kill_dcc_dnswait,
108 0
109 };
110
111
112 /*
113 * DCC events
114 */
115
116 /* Walk through every dcc entry and look for waiting DNS requests
117 * of RES_HOSTBYIP for our IP address.
118 */
119 static void dns_dcchostbyip(sockname_t *ip, char *hostn, int ok, void *other)
120 {
121 int idx;
122
123 for (idx = 0; idx < dcc_total; idx++) {
124 if ((dcc[idx].type == &DCC_DNSWAIT) &&
125 (dcc[idx].u.dns->dns_type == RES_HOSTBYIP) && (
126 #ifdef IPV6
127 (ip->family == AF_INET6 &&
128 IN6_ARE_ADDR_EQUAL(&dcc[idx].u.dns->ip->addr.s6.sin6_addr,
129 &ip->addr.s6.sin6_addr)) ||
130 #endif
131 (dcc[idx].u.dns->ip->addr.s4.sin_addr.s_addr ==
132 ip->addr.s4.sin_addr.s_addr))) {
133 if (dcc[idx].u.dns->host)
134 nfree(dcc[idx].u.dns->host);
135 dcc[idx].u.dns->host = get_data_ptr(strlen(hostn) + 1);
136 strcpy(dcc[idx].u.dns->host, hostn);
137 if (ok)
138 dcc[idx].u.dns->dns_success(idx);
139 else
140 dcc[idx].u.dns->dns_failure(idx);
141 }
142 }
143 }
144
145 /* Walk through every dcc entry and look for waiting DNS requests
146 * of RES_IPBYHOST for our hostname.
147 */
148 static void dns_dccipbyhost(sockname_t *ip, char *hostn, int ok, void *other)
149 {
150 int idx;
151
152 for (idx = 0; idx < dcc_total; idx++) {
153 if ((dcc[idx].type == &DCC_DNSWAIT) &&
154 (dcc[idx].u.dns->dns_type == RES_IPBYHOST) &&
155 !egg_strcasecmp(dcc[idx].u.dns->host, hostn)) {
156 if (ok) {
157 if (dcc[idx].u.dns->ip)
158 egg_memcpy(dcc[idx].u.dns->ip, ip, sizeof(sockname_t));
159 else
160 egg_memcpy(&dcc[idx].sockname, ip, sizeof(sockname_t));
161 dcc[idx].u.dns->dns_success(idx);
162 } else
163 dcc[idx].u.dns->dns_failure(idx);
164 }
165 }
166 }
167
168 static int dns_dccexpmem(void *other)
169 {
170 return 0;
171 }
172
173 devent_type DNS_DCCEVENT_HOSTBYIP = {
174 "DCCEVENT_HOSTBYIP",
175 dns_dccexpmem,
176 dns_dcchostbyip
177 };
178
179 devent_type DNS_DCCEVENT_IPBYHOST = {
180 "DCCEVENT_IPBYHOST",
181 dns_dccexpmem,
182 dns_dccipbyhost
183 };
184
185 void dcc_dnsipbyhost(char *hostn)
186 {
187 devent_t *de;
188
189 for (de = dns_events; de; de = de->next) {
190 if (de->type && (de->type == &DNS_DCCEVENT_IPBYHOST) &&
191 (de->lookup == RES_IPBYHOST)) {
192 if (de->res_data.hostname &&
193 !egg_strcasecmp(de->res_data.hostname, hostn))
194 /* No need to add anymore. */
195 return;
196 }
197 }
198
199 de = nmalloc(sizeof(devent_t));
200 egg_bzero(de, sizeof(devent_t));
201
202 /* Link into list. */
203 de->next = dns_events;
204 dns_events = de;
205
206 de->type = &DNS_DCCEVENT_IPBYHOST;
207 de->lookup = RES_IPBYHOST;
208 de->res_data.hostname = nmalloc(strlen(hostn) + 1);
209 strcpy(de->res_data.hostname, hostn);
210
211 /* Send request. */
212 dns_ipbyhost(hostn);
213 }
214
215 void dcc_dnshostbyip(sockname_t *ip)
216 {
217 devent_t *de;
218
219 for (de = dns_events; de; de = de->next) {
220 if (de->type && (de->type == &DNS_DCCEVENT_HOSTBYIP) &&
221 (de->lookup == RES_HOSTBYIP)) {
222 if (de->res_data.ip_addr == ip)
223 /* No need to add anymore. */
224 return;
225 }
226 }
227
228 de = nmalloc(sizeof(devent_t));
229 egg_bzero(de, sizeof(devent_t));
230
231 /* Link into list. */
232 de->next = dns_events;
233 dns_events = de;
234
235 de->type = &DNS_DCCEVENT_HOSTBYIP;
236 de->lookup = RES_HOSTBYIP;
237 de->res_data.ip_addr = ip;
238
239 /* Send request. */
240 dns_hostbyip(ip);
241 }
242
243
244 /*
245 * Tcl events
246 */
247
248 static void dns_tcl_iporhostres(sockname_t *ip, char *hostn, int ok, void *other)
249 {
250 devent_tclinfo_t *tclinfo = (devent_tclinfo_t *) other;
251 Tcl_DString list;
252
253 Tcl_DStringInit(&list);
254 Tcl_DStringAppendElement(&list, tclinfo->proc);
255 Tcl_DStringAppendElement(&list, iptostr(&ip->addr.sa));
256 Tcl_DStringAppendElement(&list, hostn);
257 Tcl_DStringAppendElement(&list, ok ? "1" : "0");
258
259 if (tclinfo->paras) {
260 EGG_CONST char *argv[2];
261 char *output;
262
263 argv[0] = Tcl_DStringValue(&list);
264 argv[1] = tclinfo->paras;
265 output = Tcl_Concat(2, argv);
266
267 if (Tcl_Eval(interp, output) == TCL_ERROR) {
268 putlog(LOG_MISC, "*", DCC_TCLERROR, tclinfo->proc, tcl_resultstring());
269 Tcl_BackgroundError(interp);
270 }
271 Tcl_Free(output);
272 } else if (Tcl_Eval(interp, Tcl_DStringValue(&list)) == TCL_ERROR) {
273 putlog(LOG_MISC, "*", DCC_TCLERROR, tclinfo->proc, tcl_resultstring());
274 Tcl_BackgroundError(interp);
275 }
276
277 Tcl_DStringFree(&list);
278
279 nfree(tclinfo->proc);
280 if (tclinfo->paras)
281 nfree(tclinfo->paras);
282 nfree(tclinfo);
283 }
284
285 static int dns_tclexpmem(void *other)
286 {
287 devent_tclinfo_t *tclinfo = (devent_tclinfo_t *) other;
288 int l = 0;
289
290 if (tclinfo) {
291 l = sizeof(devent_tclinfo_t);
292 if (tclinfo->proc)
293 l += strlen(tclinfo->proc) + 1;
294 if (tclinfo->paras)
295 l += strlen(tclinfo->paras) + 1;
296 }
297 return l;
298 }
299
300 devent_type DNS_TCLEVENT_HOSTBYIP = {
301 "TCLEVENT_HOSTBYIP",
302 dns_tclexpmem,
303 dns_tcl_iporhostres
304 };
305
306 devent_type DNS_TCLEVENT_IPBYHOST = {
307 "TCLEVENT_IPBYHOST",
308 dns_tclexpmem,
309 dns_tcl_iporhostres
310 };
311
312 static void tcl_dnsipbyhost(char *hostn, char *proc, char *paras)
313 {
314 devent_t *de;
315 devent_tclinfo_t *tclinfo;
316
317 de = nmalloc(sizeof(devent_t));
318 egg_bzero(de, sizeof(devent_t));
319
320 /* Link into list. */
321 de->next = dns_events;
322 dns_events = de;
323
324 de->type = &DNS_TCLEVENT_IPBYHOST;
325 de->lookup = RES_IPBYHOST;
326 de->res_data.hostname = nmalloc(strlen(hostn) + 1);
327 strcpy(de->res_data.hostname, hostn);
328
329 /* Store additional data. */
330 tclinfo = nmalloc(sizeof(devent_tclinfo_t));
331 tclinfo->proc = nmalloc(strlen(proc) + 1);
332 strcpy(tclinfo->proc, proc);
333 if (paras) {
334 tclinfo->paras = nmalloc(strlen(paras) + 1);
335 strcpy(tclinfo->paras, paras);
336 } else
337 tclinfo->paras = NULL;
338 de->other = tclinfo;
339
340 /* Send request. */
341 dns_ipbyhost(hostn);
342 }
343
344 static void tcl_dnshostbyip(sockname_t *ip, char *proc, char *paras)
345 {
346 devent_t *de;
347 devent_tclinfo_t *tclinfo;
348
349 de = nmalloc(sizeof(devent_t));
350 egg_bzero(de, sizeof(devent_t));
351
352 /* Link into list. */
353 de->next = dns_events;
354 dns_events = de;
355
356 de->type = &DNS_TCLEVENT_HOSTBYIP;
357 de->lookup = RES_HOSTBYIP;
358 de->res_data.ip_addr = ip;
359
360 /* Store additional data. */
361 tclinfo = nmalloc(sizeof(devent_tclinfo_t));
362 tclinfo->proc = nmalloc(strlen(proc) + 1);
363 strcpy(tclinfo->proc, proc);
364 memcpy(&tclinfo->sockname, ip, sizeof(sockname_t));
365 de->res_data.ip_addr = &tclinfo->sockname;
366 if (paras) {
367 tclinfo->paras = nmalloc(strlen(paras) + 1);
368 strcpy(tclinfo->paras, paras);
369 } else
370 tclinfo->paras = NULL;
371 de->other = tclinfo;
372
373 /* Send request. */
374 dns_hostbyip(ip);
375 }
376
377
378 /*
379 * Event functions
380 */
381
382 inline static int dnsevent_expmem(void)
383 {
384 devent_t *de;
385 int tot = 0;
386
387 for (de = dns_events; de; de = de->next) {
388 tot += sizeof(devent_t);
389 if ((de->lookup == RES_IPBYHOST) && de->res_data.hostname)
390 tot += strlen(de->res_data.hostname) + 1;
391 if (de->type && de->type->expmem)
392 tot += de->type->expmem(de->other);
393 }
394 return tot;
395 }
396
397 void call_hostbyip(sockname_t *ip, char *hostn, int ok)
398 {
399 devent_t *de = dns_events, *ode = NULL, *nde = NULL;
400
401 while (de) {
402 nde = de->next;
403 if ((de->lookup == RES_HOSTBYIP) && (
404 #ifdef IPV6
405 (ip->family == AF_INET6 &&
406 IN6_ARE_ADDR_EQUAL(&de->res_data.ip_addr->addr.s6.sin6_addr,
407 &ip->addr.s6.sin6_addr)) ||
408 #endif
409 (de->res_data.ip_addr->addr.s4.sin_addr.s_addr ==
410 ip->addr.s4.sin_addr.s_addr))) {
411 /* A memcmp() could have perfectly done it .. */
412 /* Remove the event from the list here, to avoid conflicts if one of
413 * the event handlers re-adds another event. */
414 if (ode)
415 ode->next = de->next;
416 else
417 dns_events = de->next;
418
419 if (de->type && de->type->event)
420 de->type->event(ip, hostn, ok, de->other);
421 else
422 putlog(LOG_MISC, "*", "(!) Unknown DNS event type found: %s",
423 (de->type && de->type->name) ? de->type->name : "<empty>");
424 nfree(de);
425 de = ode;
426 }
427 ode = de;
428 de = nde;
429 }
430 }
431
432 void call_ipbyhost(char *hostn, sockname_t *ip, int ok)
433 {
434 devent_t *de = dns_events, *ode = NULL, *nde = NULL;
435
436 while (de) {
437 nde = de->next;
438 if ((de->lookup == RES_IPBYHOST) && (!de->res_data.hostname ||
439 !egg_strcasecmp(de->res_data.hostname, hostn))) {
440 /* Remove the event from the list here, to avoid conflicts if one of
441 * the event handlers re-adds another event. */
442 if (ode)
443 ode->next = de->next;
444 else
445 dns_events = de->next;
446
447 if (de->type && de->type->event)
448 de->type->event(ip, hostn, ok, de->other);
449 else
450 putlog(LOG_MISC, "*", "(!) Unknown DNS event type found: %s",
451 (de->type && de->type->name) ? de->type->name : "<empty>");
452
453 if (de->res_data.hostname)
454 nfree(de->res_data.hostname);
455 nfree(de);
456 de = ode;
457 }
458 ode = de;
459 de = nde;
460 }
461 }
462
463
464 /*
465 * Async DNS emulation functions
466 */
467 void block_dns_hostbyip(sockname_t *addr)
468 {
469 struct hostent *hp = 0;
470 static char s[UHOSTLEN];
471 const char *r = 0;
472
473 if (addr->family == AF_INET) {
474 if (!sigsetjmp(alarmret, 1)) {
475 alarm(resolve_timeout);
476 hp = gethostbyaddr(&addr->addr.s4.sin_addr,
477 sizeof (struct in_addr), AF_INET);
478 alarm(0);
479 }
480 if (!hp)
481 r = inet_ntop(AF_INET, &addr->addr.s4.sin_addr.s_addr, s, sizeof s);
482 #ifdef IPV6
483 } else {
484 if (!sigsetjmp(alarmret, 1)) {
485 alarm(resolve_timeout);
486 hp = gethostbyaddr((char *) &addr->addr.s6.sin6_addr,
487 sizeof (struct in6_addr), AF_INET6);
488 alarm(0);
489 }
490 if (!hp)
491 r = inet_ntop(AF_INET6, &addr->addr.s6.sin6_addr, s, sizeof s);
492 }
493 #else
494 }
495 #endif
496 if (hp)
497 strncpyz(s, hp->h_name, sizeof s);
498 call_hostbyip(addr, s, hp ? 1 : 0);
499 }
500
501 void block_dns_ipbyhost(char *host)
502 {
503 sockname_t name;
504
505 if (setsockname(&name, host, 0, 1) == AF_UNSPEC)
506 call_ipbyhost(host, &name, 0);
507 else
508 call_ipbyhost(host, &name, 1);
509 }
510
511 /*
512 * Misc functions
513 */
514
515 int expmem_dns(void)
516 {
517 return dnsevent_expmem();
518 }
519
520
521 /*
522 * Tcl functions
523 */
524
525 /* dnslookup <ip-address> <proc> */
526 static int tcl_dnslookup STDVAR
527 {
528 sockname_t addr;
529 Tcl_DString paras;
530
531 if (argc < 3) {
532 Tcl_AppendResult(irp, "wrong # args: should be \"", argv[0],
533 " ip-address/hostname proc ?args...?\"", NULL);
534 return TCL_ERROR;
535 }
536
537 Tcl_DStringInit(&paras);
538 if (argc > 3) {
539 int p;
540
541 for (p = 3; p < argc; p++)
542 Tcl_DStringAppendElement(&paras, argv[p]);
543 }
544
545 if (setsockname(&addr, argv[1], 0, 0) != AF_UNSPEC)
546 tcl_dnshostbyip(&addr, argv[2], Tcl_DStringValue(&paras));
547 else
548 tcl_dnsipbyhost(argv[1], argv[2], Tcl_DStringValue(&paras));
549
550 Tcl_DStringFree(&paras);
551 return TCL_OK;
552 }
553
554 tcl_cmds tcldns_cmds[] = {
555 {"dnslookup", tcl_dnslookup},
556 {NULL, NULL}
557 };

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23