/[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.61 - (show annotations) (download) (as text)
Sun May 19 04:41:32 2002 UTC (17 years, 1 month ago) by stdarg
Branch: MAIN
Changes since 1.60: +4 -8 lines
File MIME type: text/x-chdr
* Got rid of some inet_ntoa
* Converted dcc ip nums to dotted decimal notation

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23