/[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.45 - (show annotations) (download) (as text)
Fri Oct 12 15:50:26 2001 UTC (17 years, 8 months ago) by tothwolf
Branch: MAIN
Changes since 1.44: +2 -2 lines
File MIME type: text/x-chdr
* DEBUG_ASSERT renamed to DEBUG
* Assert macro removed, replaced with ANSI assert support
* Removed more kludge code

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23