/[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.56 - (show annotations) (download) (as text)
Mon Apr 1 23:39:36 2002 UTC (17 years, 2 months ago) by ite
Branch: MAIN
Changes since 1.55: +1 -73 lines
File MIME type: text/x-chdr
Stripped neterror() so it will always use strerror(). Provided a full strerror() fallback function.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23