/[cvs]/eggdrop1.9/src/net.c
ViewVC logotype

Contents of /eggdrop1.9/src/net.c

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


Revision 1.58 - (show annotations) (download) (as text)
Sun May 5 15:21:30 2002 UTC (18 years, 4 months ago) by wingman
Branch: MAIN
Changes since 1.57: +4 -1 lines
File MIME type: text/x-chdr
i knew i forgot something :-P

1 /*
2 * net.c -- handles:
3 * all raw network i/o
4 *
5 * $Id: net.c,v 1.57 2002/05/03 07:57:12 stdarg Exp $
6 */
7 /*
8 * This is hereby released into the public domain.
9 * Robey Pointer, robey@netcom.com
10 */
11
12 #include <fcntl.h>
13 #include "main.h"
14 #include <limits.h>
15 #include <string.h>
16 #include <netdb.h>
17 #include <sys/socket.h>
18 #if HAVE_SYS_SELECT_H
19 # include <sys/select.h>
20 #endif
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <errno.h>
24 #if HAVE_UNISTD_H
25 # include <unistd.h>
26 #endif
27 #include <setjmp.h>
28
29 #include "lib/adns/adns.h"
30 #include "egg_timer.h"
31 #include "logfile.h"
32 #include "dns.h"
33 #include "dccutil.h" /* dprintf_eggdrop */
34 #include "tcl.h" /* findanyidx */
35 #include "net.h" /* prototypes */
36
37 #include "traffic.h" /* egg_traffic_t */
38
39 #if !HAVE_GETDTABLESIZE
40 # ifdef FD_SETSIZE
41 # define getdtablesize() FD_SETSIZE
42 # else
43 # define getdtablesize() 200
44 # endif
45 #endif
46
47 #ifndef IN6_IS_ADDR_V4MAPPED
48 # define IN6_IS_ADDR_V4MAPPED(a) \
49 ((((uint32_t *) (a))[0] == 0) && (((uint32_t *) (a))[1] == 0) && \
50 (((uint32_t *) (a))[2] == htonl (0xffff)))
51 #endif
52
53 extern struct dcc_t *dcc;
54 extern int backgrd, use_stderr, resolve_timeout, dcc_total;
55
56 extern egg_traffic_t traffic;
57
58 extern char natip[];
59
60 char myip[121] = ""; /* IP can be specified in the config file */
61 char myip6[121] = ""; /* IP6 can be specified in the config file */
62 char firewall[121] = ""; /* Socks server for firewall */
63 int firewallport = 1080; /* Default port of Sock4/5 firewalls */
64 char botuser[21] = "eggdrop"; /* Username of the user running the bot */
65 sock_list *socklist = NULL; /* Enough to be safe */
66 int MAXSOCKS = 0;
67 jmp_buf alarmret; /* Env buffer for alarm() returns */
68 IP localipv4addr = 0; /* Cache the local IPv4 address */
69
70 adns_state ads;
71
72 /* Types of proxy */
73 #define PROXY_SOCKS 1
74 #define PROXY_SUN 2
75
76 /* Initialize the socklist
77 */
78 void init_net()
79 {
80 int i;
81 char s[256];
82 struct hostent *hp;
83
84 socklist = (sock_list *)calloc(MAXSOCKS, sizeof(sock_list));
85 for (i = 0; i < MAXSOCKS; i++) {
86 socklist[i].flags = SOCK_UNUSED;
87 }
88
89 gethostname(s, sizeof s);
90 if ((hp = gethostbyname(s)) == NULL)
91 fatal("Hostname self-lookup failed.", 0);
92 localipv4addr = *((IP*) (hp->h_addr_list[0]));
93
94 /* init ADNS */
95 i = adns_init(&ads, adns_if_noautosys, 0);
96 if (i)
97 fatal(adns_strerror(i), 0);
98 }
99
100 /* Get my ip number
101 */
102 IP getmyip()
103 {
104 /* Could be pre-defined */
105 if (myip[0] && (myip[strlen(myip) - 1] >= '0') &&
106 (myip[strlen(myip) - 1] <= '9')) {
107 return (IP) inet_addr(myip);
108 }
109 return localipv4addr;
110 }
111
112 #ifdef IPV6
113 struct in6_addr getmyip6()
114 {
115 struct in6_addr ip;
116
117 /* Could be pre-defined */
118 if (myip6[0])
119 inet_pton(AF_INET6, myip6, &ip);
120 else {
121 /* get system's default IPv6 ip -- FIXME!? */
122 /* is there a know way?! - drummer */
123 ip = in6addr_any;
124 }
125 return ip;
126 }
127
128 struct in6_addr ipv4to6(IP a)
129 {
130 struct in6_addr ip;
131 if (a == INADDR_ANY)
132 return in6addr_any;
133 else {
134 ((uint32_t *)&ip)[0] = 0;
135 ((uint32_t *)&ip)[1] = 0;
136 ((uint16_t *)&ip)[4] = 0;
137 ((uint16_t *)&ip)[5] = 0xffff;
138 ((uint32_t *)&ip)[3] = a;
139 }
140 return ip;
141 }
142
143 #endif
144
145 void neterror(char *s)
146 {
147 int e = errno;
148 char *err = strerror(e);
149
150 /* Calling procs usually use char s[UHOSTLEN] for the error message. */
151 if (err) strlcpy(s, err, UHOSTLEN);
152 else sprintf(s, "Unforeseen error %d", e);
153 }
154
155 /* Sets/Unsets options for a specific socket.
156 *
157 * Returns: 0 - on success
158 * -1 - socket not found
159 * -2 - illegal operation
160 */
161 int sockoptions(int sock, int operation, int sock_options)
162 {
163 int i;
164
165 for (i = 0; i < MAXSOCKS; i++)
166 if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED)) {
167 if (operation == EGG_OPTION_SET)
168 socklist[i].flags |= sock_options;
169 else if (operation == EGG_OPTION_UNSET)
170 socklist[i].flags &= ~sock_options;
171 else
172 return -2;
173 return 0;
174 }
175 return -1;
176 }
177
178 /* Return a free entry in the socket entry
179 */
180 int allocsock(int sock, int options)
181 {
182 int i;
183
184 for (i = 0; i < MAXSOCKS; i++) {
185 if (socklist[i].flags & SOCK_UNUSED) break;
186 }
187
188 if (i == MAXSOCKS) {
189 int j;
190
191 /* Expand table by 5 */
192 socklist = (sock_list *)realloc(socklist, (MAXSOCKS+5) * sizeof(sock_list));
193 memset(socklist+MAXSOCKS, 0, 5 * sizeof(sock_list));
194 for (j = 0; j < 5; j++) socklist[MAXSOCKS+j].flags = SOCK_UNUSED;
195 MAXSOCKS += 5;
196 }
197
198 memset(socklist+i, 0, sizeof(sock_list));
199 socklist[i].flags = options;
200 socklist[i].sock = sock;
201 return(i);
202 }
203
204 /* Request a normal socket for i/o
205 */
206 void setsock(int sock, int options)
207 {
208 int i = allocsock(sock, options);
209 int parm;
210
211 if (((sock != STDOUT) || backgrd) &&
212 !(socklist[i].flags & SOCK_NONSOCK)) {
213 parm = 1;
214 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *) &parm, sizeof(int));
215
216 parm = 0;
217 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &parm, sizeof(int));
218 }
219 if (options & SOCK_LISTEN) {
220 /* Tris says this lets us grab the same port again next time */
221 parm = 1;
222 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *) &parm, sizeof(int));
223 }
224 /* Yay async i/o ! */
225 fcntl(sock, F_SETFL, O_NONBLOCK);
226 }
227
228 int getsock(int options)
229 {
230 #ifdef IPV6
231 int sock = socket(AF_INET6, SOCK_STREAM, 0);
232 #else
233 int sock = socket(AF_INET, SOCK_STREAM, 0);
234 #endif
235
236 if (sock >= 0)
237 setsock(sock, options);
238 else
239 putlog(LOG_MISC, "*", "Warning: Can't create new socket!");
240 return sock;
241 }
242
243 /* Done with a socket
244 */
245 void killsock(register int sock)
246 {
247 register int i;
248
249 /* Ignore invalid sockets. */
250 if (sock < 0)
251 return;
252 for (i = 0; i < MAXSOCKS; i++) {
253 if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED)) {
254 close(socklist[i].sock);
255 if (socklist[i].inbuf != NULL)
256 free_null(socklist[i].inbuf);
257 if (socklist[i].outbuf != NULL) {
258 free_null(socklist[i].outbuf);
259 socklist[i].outbuflen = 0;
260 }
261 socklist[i].flags = SOCK_UNUSED;
262 return;
263 }
264 }
265 putlog(LOG_MISC, "*", "Attempt to kill un-allocated socket %d !!", sock);
266 }
267
268 /* Send connection request to proxy
269 */
270 static int proxy_connect(int sock, char *host, int port, int proxy)
271 {
272 unsigned char x[10];
273 struct hostent *hp;
274 char s[256];
275 int i;
276
277 /* socks proxy */
278 if (proxy == PROXY_SOCKS) {
279 /* numeric IP? */
280 if (host[strlen(host) - 1] >= '0' && host[strlen(host) - 1] <= '9') {
281 IP ip = ((IP) inet_addr(host)); /* drummer */
282 memcpy(x, &ip, 4); /* Beige@Efnet */
283 } else {
284 /* no, must be host.domain */
285 if (!setjmp(alarmret)) {
286 alarm(resolve_timeout);
287 hp = gethostbyname(host);
288 alarm(0);
289 } else {
290 hp = NULL;
291 }
292 if (hp == NULL) {
293 killsock(sock);
294 return -2;
295 }
296 memcpy(x, hp->h_addr, hp->h_length);
297 }
298 for (i = 0; i < MAXSOCKS; i++)
299 if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
300 socklist[i].flags |= SOCK_PROXYWAIT; /* drummer */
301 snprintf(s, sizeof s, "\004\001%c%c%c%c%c%c%s", (port >> 8) % 256,
302 (port % 256), x[0], x[1], x[2], x[3], botuser);
303 tputs(sock, s, strlen(botuser) + 9); /* drummer */
304 } else if (proxy == PROXY_SUN) {
305 snprintf(s, sizeof s, "%s %d\n", host, port);
306 tputs(sock, s, strlen(s)); /* drummer */
307 }
308 return sock;
309 }
310
311 /* Starts a connection attempt to a socket
312 *
313 * If given a normal hostname, this will be resolved to the corresponding
314 * IP address first. PLEASE try to use the non-blocking dns functions
315 * instead and then call this function with the IP address to avoid blocking.
316 *
317 * returns <0 if connection refused:
318 * -1 neterror() type error
319 * -2 can't resolve hostname
320 */
321 int open_telnet_raw(int sock, char *server, int sport)
322 {
323 #ifdef IPV6
324 struct sockaddr_in6 name, vname;
325 #else
326 struct sockaddr_in name, vname;
327 #endif
328 struct hostent *hp;
329 char host[121];
330 int i, port;
331 volatile int proxy;
332
333 debug2("|NET| open_telnet_raw: %s %d", server, sport);
334
335 /* firewall? use socks */
336 if (firewall[0]) {
337 if (firewall[0] == '!') {
338 proxy = PROXY_SUN;
339 strcpy(host, &firewall[1]);
340 } else {
341 proxy = PROXY_SOCKS;
342 strcpy(host, firewall);
343 }
344 port = firewallport;
345 } else {
346 proxy = 0;
347 strcpy(host, server);
348 port = sport;
349 }
350
351 memset((char *) &name, 0, sizeof name);
352 #ifdef IPV6
353 name.sin6_family = AF_INET6;
354 name.sin6_port = htons(port);
355 #else
356 name.sin_family = AF_INET;
357 name.sin_port = htons(port);
358 #endif
359
360 if (!setjmp(alarmret)) {
361 char *p;
362 int type;
363
364 alarm(resolve_timeout);
365
366 if (!strncasecmp("ipv6%", host, 5)) {
367 type = AF_INET6;
368 p = host + 5;
369 debug1("|NET| checking only AAAA record for %s", p);
370 } else if (!strncasecmp("ipv4%", host, 5)) {
371 type = AF_INET;
372 p = host + 5;
373 debug1("|NET| checking only A record for %s", p);
374 } else {
375 type = AF_INET; /* af_preferred */
376 p = host;
377 }
378 alarm(resolve_timeout);
379 #ifndef IPV6
380 hp = gethostbyname(p);
381 #else
382 hp = gethostbyname2(p, type);
383 if (!hp && (p == host))
384 hp = gethostbyname2(p, (type == AF_INET6 ? AF_INET : AF_INET6));
385 #endif
386 alarm(0);
387 } else {
388 hp = NULL;
389 }
390 if (hp == NULL)
391 return -2;
392
393 #ifndef IPV6
394 memcpy(&name.sin_addr, hp->h_addr, hp->h_length);
395 name.sin_family = hp->h_addrtype;
396 #else
397 if (hp->h_addrtype == AF_INET6)
398 memcpy(&name.sin6_addr, hp->h_addr, hp->h_length);
399 else if (hp->h_addrtype == AF_INET)
400 name.sin6_addr = ipv4to6(*((unsigned int*) hp->h_addr));
401 else
402 return -123; /* panic :P */
403 name.sin6_family = AF_INET6;
404 #endif
405
406 memset((char *) &vname, 0, sizeof vname);
407 #ifndef IPV6
408 vname.sin_family = AF_INET;
409 vname.sin_addr.s_addr = (myip[0] ? getmyip() : INADDR_ANY);
410 #else
411 vname.sin6_family = AF_INET6;
412 if (IN6_IS_ADDR_V4MAPPED(&name.sin6_addr))
413 vname.sin6_addr = ipv4to6(myip[0] ? getmyip() : INADDR_ANY);
414 else
415 vname.sin6_addr = (myip6[0] ? getmyip6() : in6addr_any);
416 #endif
417 if (bind(sock, (struct sockaddr *) &vname, sizeof vname) < 0)
418 return -1;
419
420 for (i = 0; i < MAXSOCKS; i++) {
421 if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == sock))
422 socklist[i].flags = (socklist[i].flags & ~SOCK_VIRTUAL) | SOCK_CONNECT;
423 }
424 if (connect(sock, (struct sockaddr *) &name, (sizeof name)) < 0) {
425 if (errno == EINPROGRESS) {
426 /* Firewall? announce connect attempt to proxy */
427 if (firewall[0])
428 return proxy_connect(sock, server, sport, proxy);
429 return sock; /* async success! */
430 } else
431 return -1;
432 }
433 /* Synchronous? :/ */
434 if (firewall[0])
435 return proxy_connect(sock, server, sport, proxy);
436 return sock;
437 }
438
439 /* Ordinary non-binary connection attempt */
440 int open_telnet(char *server, int port)
441 {
442 int sock = getsock(0),
443 ret = open_telnet_raw(sock, server, port);
444
445 if (ret < 0)
446 killsock(sock);
447 return ret;
448 }
449
450 /* Returns a socket number for a listening socket that will accept any
451 * connection on a certain address -- port # is returned in port
452 * "" means on any address
453 */
454 int open_address_listen(char *addr, int *port)
455 {
456 int sock;
457 unsigned int addrlen;
458 #ifdef IPV6
459 struct sockaddr_in6 name;
460 #else
461 struct sockaddr_in name;
462 #endif
463
464 if (firewall[0]) {
465 /* FIXME: can't do listen port thru firewall yet */
466 putlog(LOG_MISC, "*", "!! Cant open a listen port (you are using a firewall)");
467 return -1;
468 }
469
470 sock = getsock(SOCK_LISTEN);
471 if (sock < 1)
472 return -1;
473
474 debug2("|NET| open_address_listen(\"%s\", %d)", addr, *port);
475
476 memset((char *) &name, 0, sizeof name);
477 #ifdef IPV6
478 name.sin6_family = AF_INET6;
479 name.sin6_port = htons(*port); /* 0 = just assign us a port */
480 if (!addr[0])
481 name.sin6_addr = in6addr_any;
482 else if (!inet_pton(AF_INET6, addr, &name.sin6_addr)) {
483 struct in_addr a4;
484 if (inet_aton(addr, &a4))
485 name.sin6_addr = ipv4to6(a4.s_addr);
486 else
487 name.sin6_addr = in6addr_any;
488 }
489 #else
490 name.sin_family = AF_INET;
491 name.sin_port = htons(*port); /* 0 = just assign us a port */
492 if (addr[0])
493 inet_aton(addr, &name.sin_addr);
494 else
495 name.sin_addr.s_addr = INADDR_ANY;
496 #endif
497 if (bind(sock, (struct sockaddr *) &name, sizeof name) < 0) {
498 killsock(sock);
499 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT)
500 return -2;
501 else
502 return -1;
503 }
504 /* what port are we on? */
505 addrlen = sizeof name;
506 if (getsockname(sock, (struct sockaddr *) &name, &addrlen) < 0) {
507 killsock(sock);
508 return -1;
509 }
510 #ifdef IPV6
511 *port = ntohs(name.sin6_port);
512 #else
513 *port = ntohs(name.sin_port);
514 #endif
515 if (listen(sock, 1) < 0) {
516 killsock(sock);
517 return -1;
518 }
519 return sock;
520 }
521
522 /* Returns a socket number for a listening socket that will accept any
523 * connection -- port # is returned in port
524 */
525 inline int open_listen(int *port, int af)
526 {
527 if (af == AF_INET)
528 return open_address_listen(myip, port);
529 #ifdef IPV6
530 else if (af == AF_INET6)
531 return open_address_listen(myip6, port);
532 #endif
533 else
534 return -1;
535 }
536
537 /* Returns the given network byte order IP address in the
538 * dotted format - "##.##.##.##"
539 * (it's IPv4 only, and it's okey - drummer)
540 */
541 char *iptostr(IP ip)
542 {
543 struct in_addr a;
544
545 a.s_addr = ip;
546 return inet_ntoa(a);
547 }
548
549 char *getlocaladdr(int sock)
550 {
551 static char buf[ADDRLEN];
552 #ifdef IPV6
553 struct sockaddr_in6 sa;
554 #else
555 struct sockaddr_in sa;
556 #endif
557 int len = sizeof sa;
558
559 if (sock == -1) { /* assuming IPv4... */
560 sprintf(buf, "%lu", (unsigned long int)
561 ntohl(natip[0] ? inet_addr(natip) : getmyip()));
562 return buf;
563 }
564
565 if (getsockname(sock, (struct sockaddr*) &sa, &len) == -1) {
566 debug1("|NET| getsockname() failed for sock %d", sock);
567 return 0;
568 }
569 #ifdef IPV6
570 if (IN6_IS_ADDR_V4MAPPED(&sa.sin6_addr))
571 sprintf(buf, "%lu",
572 (unsigned long int) ntohl(((uint32_t *)&sa.sin6_addr)[3]));
573 else
574 inet_ntop(AF_INET6, &(sa.sin6_addr), buf, sizeof buf);
575 #else
576 sprintf(buf, "%lu", (unsigned long int) ntohl(sa.sin_addr.s_addr));
577 #endif
578 return buf;
579 }
580
581 /* Short routine to answer a connect received on a socket made previously
582 * by open_listen ... returns hostname of the caller & the new socket
583 * does NOT dispose of old "public" socket!
584 */
585 int answer(int sock, char *caller, char *ip, unsigned short *port,
586 int binary)
587 {
588 int new_sock;
589 unsigned int addrlen;
590 #ifdef IPV6
591 struct sockaddr_in6 from;
592 #else
593 struct sockaddr_in from;
594 #endif
595
596 addrlen = sizeof from;
597 new_sock = accept(sock, (struct sockaddr *) &from, &addrlen);
598 if (new_sock < 0)
599 return -1;
600 if (ip != NULL) {
601 #ifdef IPV6
602 if (IN6_IS_ADDR_V4MAPPED(&from.sin6_addr))
603 inet_ntop(AF_INET, &(((uint32_t *)&from.sin6_addr)[3]), ip, ADDRMAX);
604 else
605 inet_ntop(AF_INET6, &(from.sin6_addr), ip, ADDRMAX);
606 #else
607 inet_ntop(AF_INET, &(from.sin_addr.s_addr), ip, ADDRMAX);
608 #endif
609 /* This is now done asynchronously. We now only provide the IP address.
610 */
611 strlcpy(caller, ip, 121);
612 }
613 if (port != NULL)
614 #ifdef IPV6
615 *port = ntohs(from.sin6_port);
616 #else
617 *port = ntohs(from.sin_port);
618 #endif
619 /* Set up all the normal socket crap */
620 setsock(new_sock, (binary ? SOCK_BINARY : 0));
621 return new_sock;
622 }
623
624 /* Like open_telnet, but uses server & port specifications of dcc
625 */
626 int open_telnet_dcc(int sock, char *server, char *port)
627 {
628 int p;
629 struct in_addr ia;
630
631 debug2("|NET| open_telnet_dcc: %s %s", server, port);
632 if (port != NULL)
633 p = atoi(port);
634 else
635 return -3;
636 if (server == NULL)
637 return -3;
638 /* fix the IPv4 IP format (ie: 167772161 -> 10.0.0.1) */
639 if (inet_aton(server, &ia))
640 return open_telnet_raw(sock, inet_ntoa(ia), p);
641 else
642 return open_telnet_raw(sock, server, p);
643 }
644
645 void egg_dns_gotanswer(int status, adns_answer *aw, char *origname)
646 {
647 char name[UHOSTLEN];
648 #ifdef IPV6
649 char *orign2;
650 #endif
651
652 if (!aw) {
653 debug0("|DNS| egg_dns_gotanswer: ANSWER IS NULL!");
654 return;
655 }
656 if (!origname) {
657 debug0("|DNS| egg_dns_gotanswer: origname is NULL!");
658 return;
659 }
660
661 debug2("|DNS| egg_dns_gotanswer: status=%d adns_answer=%x", status, (int)aw);
662 status = 0;
663 if ((aw->type == adns_r_addr)
664 #ifdef IPV6
665 || (aw->type == adns_r_addr6)
666 #endif
667 ) {
668 if ((aw->status == adns_s_ok) && (aw->nrrs > 0)) {
669 adns_rr_addr *rrp = aw->rrs.untyped;
670 if (rrp->addr.sa.sa_family == AF_INET) {
671 inet_ntop(AF_INET, &(rrp->addr.inet.sin_addr), name, UHOSTLEN-1);
672 status = 1;
673 #ifdef IPV6
674 } else if (rrp->addr.sa.sa_family == AF_INET6) {
675 inet_ntop(AF_INET6, &(rrp->addr.inet6.sin6_addr), name, UHOSTLEN-1);
676 status = 1;
677 #endif
678 }
679 #ifdef IPV6
680 } else if ((aw->type == adns_r_addr /* af_preferred */) &&
681 strncasecmp(origname, "ipv6%", 5) &&
682 strncasecmp(origname, "ipv4%", 5)) {
683 adns_query q6;
684 orign2 = strdup(origname);
685 /* ...it may be AAAA */
686 debug1("|DNS| egg_dns_gotanswer: A failed, checking for AAAA (%s)", origname);
687 adns_submit(ads, origname, adns_r_addr6, 0, orign2, &q6);
688 status = -1;
689 #endif
690 }
691 if (status == 0)
692 strcpy(name, "0.0.0.0");
693 if (status >= 0) {
694 debug3("|DNS| egg_dns_gotanswer: (ipbyhost) host: %s ip: %s status: %d", origname, name, status);
695 call_ipbyhost(origname, name, status);
696 }
697 } else if ((aw->type == adns_r_ptr_ip6) || (aw->type == adns_r_ptr)) {
698 if ((aw->status == adns_s_ok) && (aw->nrrs > 0)) {
699 if (aw->rrs.str) {
700 strlcpy(name, *(aw->rrs.str), UHOSTLEN);
701 status = 1;
702 }
703 }
704 if (!status) {
705 if (origname)
706 strlcpy(name, origname, UHOSTLEN);
707 else
708 strcpy(name, "error");
709 }
710 debug3("|DNS| egg_dns_gotanswer: (hostbyip) ip: %s host: %s status: %d", origname, name, status);
711 call_hostbyip(origname, name, status);
712 } else
713 debug0("|DNS| egg_dns_gotanswer: got unknow type of answer ?!");
714 free(origname);
715 }
716
717 void egg_dns_checkall()
718 {
719 adns_query q, r;
720 adns_answer *answer;
721 char *origname;
722
723 adns_forallqueries_begin(ads);
724 while ((q = adns_forallqueries_next(ads, (void **)&r)) != NULL) {
725 switch (adns_check(ads, &q, &answer, (void **)&origname)) {
726 case 0: /* ok */
727 egg_dns_gotanswer(1, answer, origname);
728 break;
729 case EAGAIN: /* Go into the queue again */
730 break;
731 default: /* failed */
732 egg_dns_gotanswer(0, answer, origname);
733 break;
734 }
735 }
736 }
737
738 /* Attempts to read from all the sockets in socklist
739 * fills s with up to 511 bytes if available, and returns the array index
740 *
741 * on EOF: returns -1, with socket in len
742 * on socket error: returns -2
743 * if nothing is ready: returns -3
744 */
745 static int sockread(char *s, int *len)
746 {
747 fd_set fd, fdw, fde;
748 int fds, i, x, fdtmp;
749 struct timeval t, tnow;
750 struct timeval *pt = &t;
751 int grab = 511;
752 egg_timeval_t howlong;
753
754 fds = getdtablesize();
755 #ifdef FD_SETSIZE
756 if (fds > FD_SETSIZE)
757 fds = FD_SETSIZE; /* Fixes YET ANOTHER freebsd bug!!! */
758 #endif
759
760 if (timer_get_shortest(&howlong)) {
761 /* No timer, default to 1 second. */
762 t.tv_sec = 1;
763 t.tv_usec = 0;
764 }
765 else {
766 t.tv_sec = howlong.sec;
767 t.tv_usec = howlong.usec;
768 }
769
770 FD_ZERO(&fd);
771
772 for (i = 0; i < MAXSOCKS; i++)
773 if (!(socklist[i].flags & (SOCK_UNUSED | SOCK_VIRTUAL))) {
774 if ((socklist[i].sock == STDOUT) && !backgrd)
775 fdtmp = STDIN;
776 else
777 fdtmp = socklist[i].sock;
778 /*
779 * Looks like that having more than a call, in the same
780 * program, to the FD_SET macro, triggers a bug in gcc.
781 * SIGBUS crashing binaries used to be produced on a number
782 * (prolly all?) of 64 bits architectures.
783 * Make your best to avoid to make it happen again.
784 *
785 * ITE
786 */
787 FD_SET(fdtmp , &fd);
788 }
789 tnow.tv_sec = time(NULL);
790 tnow.tv_usec = 0;
791 adns_beforeselect(ads, &fds, &fd, &fdw, &fde, &pt, 0, &tnow);
792 #ifdef HPUX_HACKS
793 #ifndef HPUX10_HACKS
794 x = select(fds, (int *) &fd, (int *) NULL, (int *) NULL, &t);
795 #else
796 x = select(fds, &fd, NULL, NULL, &t);
797 #endif
798 #else
799 x = select(fds, &fd, NULL, NULL, &t);
800 #endif
801 tnow.tv_sec = time(NULL);
802 tnow.tv_usec = 0;
803 adns_afterselect(ads, fds, &fd, &fdw, &fde, &tnow);
804
805 /* dns stuff */
806 egg_dns_checkall();
807
808 if (x > 0) {
809 /* Something happened */
810 for (i = 0; i < MAXSOCKS; i++) {
811 if ((!(socklist[i].flags & SOCK_UNUSED)) &&
812 ((FD_ISSET(socklist[i].sock, &fd)) ||
813 ((socklist[i].sock == STDOUT) && (!backgrd) &&
814 (FD_ISSET(STDIN, &fd))))) {
815 if (socklist[i].flags & (SOCK_LISTEN | SOCK_CONNECT)) {
816 /* Listening socket -- don't read, just return activity */
817 /* Same for connection attempt */
818 /* (for strong connections, require a read to succeed first) */
819 if (socklist[i].flags & SOCK_PROXYWAIT) { /* drummer */
820 /* Hang around to get the return code from proxy */
821 grab = 10;
822 } else if (!(socklist[i].flags & SOCK_STRONGCONN)) {
823 debug1("net: connect! sock %d", socklist[i].sock);
824 s[0] = 0;
825 *len = 0;
826 return i;
827 }
828 } else if (socklist[i].flags & SOCK_PASS) {
829 s[0] = 0;
830 *len = 0;
831 return i;
832 }
833 if ((socklist[i].sock == STDOUT) && !backgrd)
834 x = read(STDIN, s, grab);
835 else
836 x = read(socklist[i].sock, s, grab);
837 if (x <= 0) { /* eof */
838 if (errno == EAGAIN) {
839 s[0] = 0;
840 *len = 0;
841 return -3;
842 }
843 *len = socklist[i].sock;
844 socklist[i].flags &= ~SOCK_CONNECT;
845 debug1("net: eof!(read) socket %d", socklist[i].sock);
846 return -1;
847 }
848 s[x] = 0;
849 *len = x;
850 if (socklist[i].flags & SOCK_PROXYWAIT) {
851 debug2("net: socket: %d proxy errno: %d", socklist[i].sock, s[1]);
852 socklist[i].flags &= ~(SOCK_CONNECT | SOCK_PROXYWAIT);
853 switch (s[1]) {
854 case 90: /* Success */
855 s[0] = 0;
856 *len = 0;
857 return i;
858 case 91: /* Failed */
859 errno = ECONNREFUSED;
860 break;
861 case 92: /* No identd */
862 case 93: /* Identd said wrong username */
863 /* A better error message would be "socks misconfigured"
864 * or "identd not working" but this is simplest.
865 */
866 errno = ENETUNREACH;
867 break;
868 }
869 *len = socklist[i].sock;
870 return -1;
871 }
872 return i;
873 }
874 }
875 } else if (x == -1)
876 return -2; /* socket error */
877 else {
878 s[0] = 0;
879 *len = 0;
880 }
881 return -3;
882 }
883
884 /* sockgets: buffer and read from sockets
885 *
886 * Attempts to read from all registered sockets for up to one second. if
887 * after one second, no complete data has been received from any of the
888 * sockets, 's' will be empty, 'len' will be 0, and sockgets will return -3.
889 * if there is returnable data received from a socket, the data will be
890 * in 's' (null-terminated if non-binary), the length will be returned
891 * in len, and the socket number will be returned.
892 * normal sockets have their input buffered, and each call to sockgets
893 * will return one line terminated with a '\n'. binary sockets are not
894 * buffered and return whatever coems in as soon as it arrives.
895 * listening sockets will return an empty string when a connection comes in.
896 * connecting sockets will return an empty string on a successful connect,
897 * or EOF on a failed connect.
898 * if an EOF is detected from any of the sockets, that socket number will be
899 * put in len, and -1 will be returned.
900 * the maximum length of the string returned is 512 (including null)
901 *
902 * Returns -4 if we handled something that shouldn't be handled by the
903 * dcc functions. Simply ignore it.
904 */
905
906 int sockgets(char *s, int *len)
907 {
908 char xx[514], *p, *px;
909 int ret, i, data = 0;
910
911 for (i = 0; i < MAXSOCKS; i++) {
912 /* Check for stored-up data waiting to be processed */
913 if (!(socklist[i].flags & SOCK_UNUSED) &&
914 !(socklist[i].flags & SOCK_BUFFER) && (socklist[i].inbuf != NULL)) {
915 if (!(socklist[i].flags & SOCK_BINARY)) {
916 /* look for \r too cos windows can't follow RFCs */
917 p = strchr(socklist[i].inbuf, '\n');
918 if (p == NULL)
919 p = strchr(socklist[i].inbuf, '\r');
920 if (p != NULL) {
921 *p = 0;
922 if (strlen(socklist[i].inbuf) > 510)
923 socklist[i].inbuf[510] = 0;
924 strcpy(s, socklist[i].inbuf);
925 /* Uhm... very strange way to code this... */
926 px = (char *) malloc(strlen(p + 1) + 1);
927 strcpy(px, p + 1);
928 free(socklist[i].inbuf);
929 if (px[0])
930 socklist[i].inbuf = px;
931 else {
932 free(px);
933 socklist[i].inbuf = NULL;
934 }
935 /* Strip CR if this was CR/LF combo */
936 if (s[strlen(s) - 1] == '\r')
937 s[strlen(s) - 1] = 0;
938 *len = strlen(s);
939 return socklist[i].sock;
940 }
941 } else {
942 /* Handling buffered binary data (must have been SOCK_BUFFER before). */
943 if (socklist[i].inbuflen <= 510) {
944 *len = socklist[i].inbuflen;
945 memcpy(s, socklist[i].inbuf, socklist[i].inbuflen);
946 free_null(socklist[i].inbuf);
947 socklist[i].inbuflen = 0;
948 } else {
949 /* Split up into chunks of 510 bytes. */
950 *len = 510;
951 memcpy(s, socklist[i].inbuf, *len);
952 memcpy(socklist[i].inbuf, socklist[i].inbuf + *len, *len);
953 socklist[i].inbuflen -= *len;
954 socklist[i].inbuf = realloc(socklist[i].inbuf,
955 socklist[i].inbuflen);
956 }
957 return socklist[i].sock;
958 }
959 }
960 /* Also check any sockets that might have EOF'd during write */
961 if (!(socklist[i].flags & SOCK_UNUSED)
962 && (socklist[i].flags & SOCK_EOFD)) {
963 s[0] = 0;
964 *len = socklist[i].sock;
965 return -1;
966 }
967 }
968 /* No pent-up data of any worth -- down to business */
969 *len = 0;
970 ret = sockread(xx, len);
971 if (ret < 0) {
972 s[0] = 0;
973 return ret;
974 }
975 /* Binary, listening and passed on sockets don't get buffered. */
976 if (socklist[ret].flags & SOCK_CONNECT) {
977 if (socklist[ret].flags & SOCK_STRONGCONN) {
978 socklist[ret].flags &= ~SOCK_STRONGCONN;
979 /* Buffer any data that came in, for future read. */
980 socklist[ret].inbuflen = *len;
981 socklist[ret].inbuf = (char *) malloc(*len + 1);
982 /* It might be binary data. You never know. */
983 memcpy(socklist[ret].inbuf, xx, *len);
984 socklist[ret].inbuf[*len] = 0;
985 }
986 socklist[ret].flags &= ~SOCK_CONNECT;
987 s[0] = 0;
988 return socklist[ret].sock;
989 }
990 if (socklist[ret].flags & SOCK_BINARY) {
991 memcpy(s, xx, *len);
992 return socklist[ret].sock;
993 }
994 if ((socklist[ret].flags & SOCK_LISTEN) ||
995 (socklist[ret].flags & SOCK_PASS))
996 return socklist[ret].sock;
997 if (socklist[ret].flags & SOCK_BUFFER) {
998 socklist[ret].inbuf = (char *) realloc(socklist[ret].inbuf,
999 socklist[ret].inbuflen + *len + 1);
1000 memcpy(socklist[ret].inbuf + socklist[ret].inbuflen, xx, *len);
1001 socklist[ret].inbuflen += *len;
1002 /* We don't know whether it's binary data. Make sure normal strings
1003 will be handled properly later on too. */
1004 socklist[ret].inbuf[socklist[ret].inbuflen] = 0;
1005 return -4; /* Ignore this one. */
1006 }
1007 /* Might be necessary to prepend stored-up data! */
1008 if (socklist[ret].inbuf != NULL) {
1009 p = socklist[ret].inbuf;
1010 socklist[ret].inbuf = (char *) malloc(strlen(p) + strlen(xx) + 1);
1011 strcpy(socklist[ret].inbuf, p);
1012 strcat(socklist[ret].inbuf, xx);
1013 free(p);
1014 if (strlen(socklist[ret].inbuf) < 512) {
1015 strcpy(xx, socklist[ret].inbuf);
1016 free_null(socklist[ret].inbuf);
1017 socklist[ret].inbuflen = 0;
1018 } else {
1019 p = socklist[ret].inbuf;
1020 socklist[ret].inbuflen = strlen(p) - 510;
1021 socklist[ret].inbuf = (char *) malloc(socklist[ret].inbuflen + 1);
1022 strcpy(socklist[ret].inbuf, p + 510);
1023 *(p + 510) = 0;
1024 strcpy(xx, p);
1025 free(p);
1026 /* (leave the rest to be post-pended later) */
1027 }
1028 }
1029 /* Look for EOL marker; if it's there, i have something to show */
1030 p = strchr(xx, '\n');
1031 if (p == NULL)
1032 p = strchr(xx, '\r');
1033 if (p != NULL) {
1034 *p = 0;
1035 strcpy(s, xx);
1036 strcpy(xx, p + 1);
1037 if (s[strlen(s) - 1] == '\r')
1038 s[strlen(s) - 1] = 0;
1039 data = 1; /* DCC_CHAT may now need to process a
1040 blank line */
1041 /* NO! */
1042 /* if (!s[0]) strcpy(s," "); */
1043 } else {
1044 s[0] = 0;
1045 if (strlen(xx) >= 510) {
1046 /* String is too long, so just insert fake \n */
1047 strcpy(s, xx);
1048 xx[0] = 0;
1049 data = 1;
1050 }
1051 }
1052 *len = strlen(s);
1053 /* Anything left that needs to be saved? */
1054 if (!xx[0]) {
1055 if (data)
1056 return socklist[ret].sock;
1057 else
1058 return -3;
1059 }
1060 /* Prepend old data back */
1061 if (socklist[ret].inbuf != NULL) {
1062 p = socklist[ret].inbuf;
1063 socklist[ret].inbuflen = strlen(p) + strlen(xx);
1064 socklist[ret].inbuf = (char *) malloc(socklist[ret].inbuflen + 1);
1065 strcpy(socklist[ret].inbuf, xx);
1066 strcat(socklist[ret].inbuf, p);
1067 free(p);
1068 } else {
1069 socklist[ret].inbuflen = strlen(xx);
1070 socklist[ret].inbuf = (char *) malloc(socklist[ret].inbuflen + 1);
1071 strcpy(socklist[ret].inbuf, xx);
1072 }
1073 if (data) {
1074 return socklist[ret].sock;
1075 } else {
1076 return -3;
1077 }
1078 }
1079
1080 /* Dump something to a socket
1081 */
1082 void tputs(register int z, char *s, unsigned int len)
1083 {
1084 register int i, x, idx;
1085 char *p;
1086 static int inhere = 0;
1087
1088 if (z < 0)
1089 return; /* um... HELLO?! sanity check please! */
1090 if (((z == STDOUT) || (z == STDERR)) && (!backgrd || use_stderr)) {
1091 write(z, s, len);
1092 return;
1093 }
1094 for (i = 0; i < MAXSOCKS; i++) {
1095 if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == z)) {
1096 for (idx = 0; idx < dcc_total; idx++) {
1097 if (dcc[idx].sock == z) {
1098 if (dcc[idx].type) {
1099 if (dcc[idx].type->name) {
1100 if (!strncmp(dcc[idx].type->name, "BOT", 3)) {
1101 traffic.out_today.bn += len;
1102 break;
1103 } else if (!strcmp(dcc[idx].type->name, "SERVER")) {
1104 traffic.out_today.irc += len;
1105 break;
1106 } else if (!strncmp(dcc[idx].type->name, "CHAT", 4)) {
1107 traffic.out_today.dcc += len;
1108 break;
1109 } else if (!strncmp(dcc[idx].type->name, "FILES", 5)) {
1110 traffic.out_today.filesys += len;
1111 break;
1112 } else if (!strcmp(dcc[idx].type->name, "SEND")) {
1113 traffic.out_today.trans += len;
1114 break;
1115 } else if (!strncmp(dcc[idx].type->name, "GET", 3)) {
1116 traffic.out_today.trans += len;
1117 break;
1118 } else {
1119 traffic.out_today.unknown += len;
1120 break;
1121 }
1122 }
1123 }
1124 }
1125 }
1126
1127 if (socklist[i].outbuf != NULL) {
1128 /* Already queueing: just add it */
1129 p = (char *) realloc(socklist[i].outbuf, socklist[i].outbuflen + len);
1130 memcpy(p + socklist[i].outbuflen, s, len);
1131 socklist[i].outbuf = p;
1132 socklist[i].outbuflen += len;
1133 return;
1134 }
1135 /* Try. */
1136 x = write(z, s, len);
1137 if (x == (-1))
1138 x = 0;
1139 if (x < len) {
1140 /* Socket is full, queue it */
1141 socklist[i].outbuf = (char *) malloc(len - x);
1142 memcpy(socklist[i].outbuf, &s[x], len - x);
1143 socklist[i].outbuflen = len - x;
1144 }
1145 return;
1146 }
1147 }
1148 /* Make sure we don't cause a crash by looping here */
1149 if (!inhere) {
1150 inhere = 1;
1151
1152 putlog(LOG_MISC, "*", "!!! writing to nonexistent socket: %d", z);
1153 s[strlen(s) - 1] = 0;
1154 putlog(LOG_MISC, "*", "!-> '%s'", s);
1155
1156 inhere = 0;
1157 }
1158 }
1159
1160 /* tputs might queue data for sockets, let's dump as much of it as
1161 * possible.
1162 */
1163 void dequeue_sockets()
1164 {
1165 int i, x;
1166
1167 for (i = 0; i < MAXSOCKS; i++) {
1168 if (!(socklist[i].flags & SOCK_UNUSED) &&
1169 socklist[i].outbuf != NULL) {
1170 /* Trick tputs into doing the work */
1171 x = write(socklist[i].sock, socklist[i].outbuf,
1172 socklist[i].outbuflen);
1173 if ((x < 0) && (errno != EAGAIN)
1174 #ifdef EBADSLT
1175 && (errno != EBADSLT)
1176 #endif
1177 #ifdef ENOTCONN
1178 && (errno != ENOTCONN)
1179 #endif
1180 ) {
1181 /* This detects an EOF during writing */
1182 debug3("net: eof!(write) socket %d (%s,%d)", socklist[i].sock,
1183 strerror(errno), errno);
1184 socklist[i].flags |= SOCK_EOFD;
1185 } else if (x == socklist[i].outbuflen) {
1186 /* If the whole buffer was sent, nuke it */
1187 free_null(socklist[i].outbuf);
1188 socklist[i].outbuflen = 0;
1189 } else if (x > 0) {
1190 char *p = socklist[i].outbuf;
1191
1192 /* This removes any sent bytes from the beginning of the buffer */
1193 socklist[i].outbuf = (char *) malloc(socklist[i].outbuflen - x);
1194 memcpy(socklist[i].outbuf, p + x, socklist[i].outbuflen - x);
1195 socklist[i].outbuflen -= x;
1196 free(p);
1197 }
1198
1199 /* All queued data was sent. Call handler if one exists and the
1200 * dcc entry wants it.
1201 */
1202 if (!socklist[i].outbuf) {
1203 int idx = findanyidx(socklist[i].sock);
1204
1205 if (idx > 0 && dcc[idx].type && dcc[idx].type->outdone)
1206 dcc[idx].type->outdone(idx);
1207 }
1208 }
1209 }
1210 }
1211
1212
1213 /*
1214 * Debugging stuff
1215 */
1216
1217 void tell_netdebug(int idx)
1218 {
1219 int i;
1220 char s[80];
1221
1222 dprintf(idx, "Open sockets:");
1223 for (i = 0; i < MAXSOCKS; i++) {
1224 if (!(socklist[i].flags & SOCK_UNUSED)) {
1225 sprintf(s, " %d", socklist[i].sock);
1226 if (socklist[i].flags & SOCK_BINARY)
1227 strcat(s, " (binary)");
1228 if (socklist[i].flags & SOCK_LISTEN)
1229 strcat(s, " (listen)");
1230 if (socklist[i].flags & SOCK_PASS)
1231 strcat(s, " (passed on)");
1232 if (socklist[i].flags & SOCK_CONNECT)
1233 strcat(s, " (connecting)");
1234 if (socklist[i].flags & SOCK_STRONGCONN)
1235 strcat(s, " (strong)");
1236 if (socklist[i].flags & SOCK_NONSOCK)
1237 strcat(s, " (file)");
1238 if (socklist[i].inbuf != NULL)
1239 sprintf(&s[strlen(s)], " (inbuf: %04X)", strlen(socklist[i].inbuf));
1240 if (socklist[i].outbuf != NULL)
1241 sprintf(&s[strlen(s)], " (outbuf: %06lX)", socklist[i].outbuflen);
1242 strcat(s, ",");
1243 dprintf(idx, "%s", s);
1244 }
1245 }
1246 dprintf(idx, " done.\n");
1247 }
1248
1249 /* Checks wether the referenced socket has data queued.
1250 *
1251 * Returns true if the incoming/outgoing (depending on 'type') queues
1252 * contain data, otherwise false.
1253 */
1254 int sock_has_data(int type, int sock)
1255 {
1256 int ret = 0, i;
1257
1258 for (i = 0; i < MAXSOCKS; i++)
1259 if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
1260 break;
1261 if (i < MAXSOCKS) {
1262 switch (type) {
1263 case SOCK_DATA_OUTGOING:
1264 ret = (socklist[i].outbuf != NULL);
1265 break;
1266 case SOCK_DATA_INCOMING:
1267 ret = (socklist[i].inbuf != NULL);
1268 break;
1269 }
1270 } else
1271 debug1("sock_has_data: could not find socket #%d, returning false.", sock);
1272 return ret;
1273 }
1274
1275 /* flush_inbuf():
1276 * checks if there's data in the incoming buffer of an connection
1277 * and flushs the buffer if possible
1278 *
1279 * returns: -1 if the dcc entry wasn't found
1280 * -2 if dcc[idx].type->activity doesn't exist and the data couldn't
1281 * be handled
1282 * 0 if buffer was empty
1283 * otherwise length of flushed buffer
1284 */
1285 int flush_inbuf(int idx)
1286 {
1287 int i, len;
1288 char *inbuf;
1289
1290 assert((idx >= 0) && (idx < dcc_total));
1291 for (i = 0; i < MAXSOCKS; i++) {
1292 if ((dcc[idx].sock == socklist[i].sock)
1293 && !(socklist[i].flags & SOCK_UNUSED)) {
1294 len = socklist[i].inbuflen;
1295 if ((len > 0) && socklist[i].inbuf) {
1296 if (dcc[idx].type && dcc[idx].type->activity) {
1297 inbuf = socklist[i].inbuf;
1298 socklist[i].inbuf = NULL;
1299 dcc[idx].type->activity(idx, inbuf, len);
1300 free(inbuf);
1301 return len;
1302 } else
1303 return -2;
1304 } else
1305 return 0;
1306 }
1307 }
1308 return -1;
1309 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23