/[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.59 - (show annotations) (download) (as text)
Sun May 5 16:40:38 2002 UTC (17 years, 1 month ago) by tothwolf
Branch: MAIN
Changes since 1.58: +9 -6 lines
File MIME type: text/x-chdr
Grafted GPL header onto newer files.
Storing rcsid in binary files now.

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$";
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_aton(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_aton(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
548 a.s_addr = ip;
549 return inet_ntoa(a);
550 }
551
552 char *getlocaladdr(int sock)
553 {
554 static char buf[ADDRLEN];
555 #ifdef IPV6
556 struct sockaddr_in6 sa;
557 #else
558 struct sockaddr_in sa;
559 #endif
560 int len = sizeof sa;
561
562 if (sock == -1) { /* assuming IPv4... */
563 sprintf(buf, "%lu", (unsigned long int)
564 ntohl(natip[0] ? inet_addr(natip) : getmyip()));
565 return buf;
566 }
567
568 if (getsockname(sock, (struct sockaddr*) &sa, &len) == -1) {
569 debug1("|NET| getsockname() failed for sock %d", sock);
570 return 0;
571 }
572 #ifdef IPV6
573 if (IN6_IS_ADDR_V4MAPPED(&sa.sin6_addr))
574 sprintf(buf, "%lu",
575 (unsigned long int) ntohl(((uint32_t *)&sa.sin6_addr)[3]));
576 else
577 inet_ntop(AF_INET6, &(sa.sin6_addr), buf, sizeof buf);
578 #else
579 sprintf(buf, "%lu", (unsigned long int) ntohl(sa.sin_addr.s_addr));
580 #endif
581 return buf;
582 }
583
584 /* Short routine to answer a connect received on a socket made previously
585 * by open_listen ... returns hostname of the caller & the new socket
586 * does NOT dispose of old "public" socket!
587 */
588 int answer(int sock, char *caller, char *ip, unsigned short *port,
589 int binary)
590 {
591 int new_sock;
592 unsigned int addrlen;
593 #ifdef IPV6
594 struct sockaddr_in6 from;
595 #else
596 struct sockaddr_in from;
597 #endif
598
599 addrlen = sizeof from;
600 new_sock = accept(sock, (struct sockaddr *) &from, &addrlen);
601 if (new_sock < 0)
602 return -1;
603 if (ip != NULL) {
604 #ifdef IPV6
605 if (IN6_IS_ADDR_V4MAPPED(&from.sin6_addr))
606 inet_ntop(AF_INET, &(((uint32_t *)&from.sin6_addr)[3]), ip, ADDRMAX);
607 else
608 inet_ntop(AF_INET6, &(from.sin6_addr), ip, ADDRMAX);
609 #else
610 inet_ntop(AF_INET, &(from.sin_addr.s_addr), ip, ADDRMAX);
611 #endif
612 /* This is now done asynchronously. We now only provide the IP address.
613 */
614 strlcpy(caller, ip, 121);
615 }
616 if (port != NULL)
617 #ifdef IPV6
618 *port = ntohs(from.sin6_port);
619 #else
620 *port = ntohs(from.sin_port);
621 #endif
622 /* Set up all the normal socket crap */
623 setsock(new_sock, (binary ? SOCK_BINARY : 0));
624 return new_sock;
625 }
626
627 /* Like open_telnet, but uses server & port specifications of dcc
628 */
629 int open_telnet_dcc(int sock, char *server, char *port)
630 {
631 int p;
632 struct in_addr ia;
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 /* fix the IPv4 IP format (ie: 167772161 -> 10.0.0.1) */
642 if (inet_aton(server, &ia))
643 return open_telnet_raw(sock, inet_ntoa(ia), p);
644 else
645 return open_telnet_raw(sock, server, p);
646 }
647
648 void egg_dns_gotanswer(int status, adns_answer *aw, char *origname)
649 {
650 char name[UHOSTLEN];
651 #ifdef IPV6
652 char *orign2;
653 #endif
654
655 if (!aw) {
656 debug0("|DNS| egg_dns_gotanswer: ANSWER IS NULL!");
657 return;
658 }
659 if (!origname) {
660 debug0("|DNS| egg_dns_gotanswer: origname is NULL!");
661 return;
662 }
663
664 debug2("|DNS| egg_dns_gotanswer: status=%d adns_answer=%x", status, (int)aw);
665 status = 0;
666 if ((aw->type == adns_r_addr)
667 #ifdef IPV6
668 || (aw->type == adns_r_addr6)
669 #endif
670 ) {
671 if ((aw->status == adns_s_ok) && (aw->nrrs > 0)) {
672 adns_rr_addr *rrp = aw->rrs.untyped;
673 if (rrp->addr.sa.sa_family == AF_INET) {
674 inet_ntop(AF_INET, &(rrp->addr.inet.sin_addr), name, UHOSTLEN-1);
675 status = 1;
676 #ifdef IPV6
677 } else if (rrp->addr.sa.sa_family == AF_INET6) {
678 inet_ntop(AF_INET6, &(rrp->addr.inet6.sin6_addr), name, UHOSTLEN-1);
679 status = 1;
680 #endif
681 }
682 #ifdef IPV6
683 } else if ((aw->type == adns_r_addr /* af_preferred */) &&
684 strncasecmp(origname, "ipv6%", 5) &&
685 strncasecmp(origname, "ipv4%", 5)) {
686 adns_query q6;
687 orign2 = strdup(origname);
688 /* ...it may be AAAA */
689 debug1("|DNS| egg_dns_gotanswer: A failed, checking for AAAA (%s)", origname);
690 adns_submit(ads, origname, adns_r_addr6, 0, orign2, &q6);
691 status = -1;
692 #endif
693 }
694 if (status == 0)
695 strcpy(name, "0.0.0.0");
696 if (status >= 0) {
697 debug3("|DNS| egg_dns_gotanswer: (ipbyhost) host: %s ip: %s status: %d", origname, name, status);
698 call_ipbyhost(origname, name, status);
699 }
700 } else if ((aw->type == adns_r_ptr_ip6) || (aw->type == adns_r_ptr)) {
701 if ((aw->status == adns_s_ok) && (aw->nrrs > 0)) {
702 if (aw->rrs.str) {
703 strlcpy(name, *(aw->rrs.str), UHOSTLEN);
704 status = 1;
705 }
706 }
707 if (!status) {
708 if (origname)
709 strlcpy(name, origname, UHOSTLEN);
710 else
711 strcpy(name, "error");
712 }
713 debug3("|DNS| egg_dns_gotanswer: (hostbyip) ip: %s host: %s status: %d", origname, name, status);
714 call_hostbyip(origname, name, status);
715 } else
716 debug0("|DNS| egg_dns_gotanswer: got unknow type of answer ?!");
717 free(origname);
718 }
719
720 void egg_dns_checkall()
721 {
722 adns_query q, r;
723 adns_answer *answer;
724 char *origname;
725
726 adns_forallqueries_begin(ads);
727 while ((q = adns_forallqueries_next(ads, (void **)&r)) != NULL) {
728 switch (adns_check(ads, &q, &answer, (void **)&origname)) {
729 case 0: /* ok */
730 egg_dns_gotanswer(1, answer, origname);
731 break;
732 case EAGAIN: /* Go into the queue again */
733 break;
734 default: /* failed */
735 egg_dns_gotanswer(0, answer, origname);
736 break;
737 }
738 }
739 }
740
741 /* Attempts to read from all the sockets in socklist
742 * fills s with up to 511 bytes if available, and returns the array index
743 *
744 * on EOF: returns -1, with socket in len
745 * on socket error: returns -2
746 * if nothing is ready: returns -3
747 */
748 static int sockread(char *s, int *len)
749 {
750 fd_set fd, fdw, fde;
751 int fds, i, x, fdtmp;
752 struct timeval t, tnow;
753 struct timeval *pt = &t;
754 int grab = 511;
755 egg_timeval_t howlong;
756
757 fds = getdtablesize();
758 #ifdef FD_SETSIZE
759 if (fds > FD_SETSIZE)
760 fds = FD_SETSIZE; /* Fixes YET ANOTHER freebsd bug!!! */
761 #endif
762
763 if (timer_get_shortest(&howlong)) {
764 /* No timer, default to 1 second. */
765 t.tv_sec = 1;
766 t.tv_usec = 0;
767 }
768 else {
769 t.tv_sec = howlong.sec;
770 t.tv_usec = howlong.usec;
771 }
772
773 FD_ZERO(&fd);
774
775 for (i = 0; i < MAXSOCKS; i++)
776 if (!(socklist[i].flags & (SOCK_UNUSED | SOCK_VIRTUAL))) {
777 if ((socklist[i].sock == STDOUT) && !backgrd)
778 fdtmp = STDIN;
779 else
780 fdtmp = socklist[i].sock;
781 /*
782 * Looks like that having more than a call, in the same
783 * program, to the FD_SET macro, triggers a bug in gcc.
784 * SIGBUS crashing binaries used to be produced on a number
785 * (prolly all?) of 64 bits architectures.
786 * Make your best to avoid to make it happen again.
787 *
788 * ITE
789 */
790 FD_SET(fdtmp , &fd);
791 }
792 tnow.tv_sec = time(NULL);
793 tnow.tv_usec = 0;
794 adns_beforeselect(ads, &fds, &fd, &fdw, &fde, &pt, 0, &tnow);
795 #ifdef HPUX_HACKS
796 #ifndef HPUX10_HACKS
797 x = select(fds, (int *) &fd, (int *) NULL, (int *) NULL, &t);
798 #else
799 x = select(fds, &fd, NULL, NULL, &t);
800 #endif
801 #else
802 x = select(fds, &fd, NULL, NULL, &t);
803 #endif
804 tnow.tv_sec = time(NULL);
805 tnow.tv_usec = 0;
806 adns_afterselect(ads, fds, &fd, &fdw, &fde, &tnow);
807
808 /* dns stuff */
809 egg_dns_checkall();
810
811 if (x > 0) {
812 /* Something happened */
813 for (i = 0; i < MAXSOCKS; i++) {
814 if ((!(socklist[i].flags & SOCK_UNUSED)) &&
815 ((FD_ISSET(socklist[i].sock, &fd)) ||
816 ((socklist[i].sock == STDOUT) && (!backgrd) &&
817 (FD_ISSET(STDIN, &fd))))) {
818 if (socklist[i].flags & (SOCK_LISTEN | SOCK_CONNECT)) {
819 /* Listening socket -- don't read, just return activity */
820 /* Same for connection attempt */
821 /* (for strong connections, require a read to succeed first) */
822 if (socklist[i].flags & SOCK_PROXYWAIT) { /* drummer */
823 /* Hang around to get the return code from proxy */
824 grab = 10;
825 } else if (!(socklist[i].flags & SOCK_STRONGCONN)) {
826 debug1("net: connect! sock %d", socklist[i].sock);
827 s[0] = 0;
828 *len = 0;
829 return i;
830 }
831 } else if (socklist[i].flags & SOCK_PASS) {
832 s[0] = 0;
833 *len = 0;
834 return i;
835 }
836 if ((socklist[i].sock == STDOUT) && !backgrd)
837 x = read(STDIN, s, grab);
838 else
839 x = read(socklist[i].sock, s, grab);
840 if (x <= 0) { /* eof */
841 if (errno == EAGAIN) {
842 s[0] = 0;
843 *len = 0;
844 return -3;
845 }
846 *len = socklist[i].sock;
847 socklist[i].flags &= ~SOCK_CONNECT;
848 debug1("net: eof!(read) socket %d", socklist[i].sock);
849 return -1;
850 }
851 s[x] = 0;
852 *len = x;
853 if (socklist[i].flags & SOCK_PROXYWAIT) {
854 debug2("net: socket: %d proxy errno: %d", socklist[i].sock, s[1]);
855 socklist[i].flags &= ~(SOCK_CONNECT | SOCK_PROXYWAIT);
856 switch (s[1]) {
857 case 90: /* Success */
858 s[0] = 0;
859 *len = 0;
860 return i;
861 case 91: /* Failed */
862 errno = ECONNREFUSED;
863 break;
864 case 92: /* No identd */
865 case 93: /* Identd said wrong username */
866 /* A better error message would be "socks misconfigured"
867 * or "identd not working" but this is simplest.
868 */
869 errno = ENETUNREACH;
870 break;
871 }
872 *len = socklist[i].sock;
873 return -1;
874 }
875 return i;
876 }
877 }
878 } else if (x == -1)
879 return -2; /* socket error */
880 else {
881 s[0] = 0;
882 *len = 0;
883 }
884 return -3;
885 }
886
887 /* sockgets: buffer and read from sockets
888 *
889 * Attempts to read from all registered sockets for up to one second. if
890 * after one second, no complete data has been received from any of the
891 * sockets, 's' will be empty, 'len' will be 0, and sockgets will return -3.
892 * if there is returnable data received from a socket, the data will be
893 * in 's' (null-terminated if non-binary), the length will be returned
894 * in len, and the socket number will be returned.
895 * normal sockets have their input buffered, and each call to sockgets
896 * will return one line terminated with a '\n'. binary sockets are not
897 * buffered and return whatever coems in as soon as it arrives.
898 * listening sockets will return an empty string when a connection comes in.
899 * connecting sockets will return an empty string on a successful connect,
900 * or EOF on a failed connect.
901 * if an EOF is detected from any of the sockets, that socket number will be
902 * put in len, and -1 will be returned.
903 * the maximum length of the string returned is 512 (including null)
904 *
905 * Returns -4 if we handled something that shouldn't be handled by the
906 * dcc functions. Simply ignore it.
907 */
908
909 int sockgets(char *s, int *len)
910 {
911 char xx[514], *p, *px;
912 int ret, i, data = 0;
913
914 for (i = 0; i < MAXSOCKS; i++) {
915 /* Check for stored-up data waiting to be processed */
916 if (!(socklist[i].flags & SOCK_UNUSED) &&
917 !(socklist[i].flags & SOCK_BUFFER) && (socklist[i].inbuf != NULL)) {
918 if (!(socklist[i].flags & SOCK_BINARY)) {
919 /* look for \r too cos windows can't follow RFCs */
920 p = strchr(socklist[i].inbuf, '\n');
921 if (p == NULL)
922 p = strchr(socklist[i].inbuf, '\r');
923 if (p != NULL) {
924 *p = 0;
925 if (strlen(socklist[i].inbuf) > 510)
926 socklist[i].inbuf[510] = 0;
927 strcpy(s, socklist[i].inbuf);
928 /* Uhm... very strange way to code this... */
929 px = (char *) malloc(strlen(p + 1) + 1);
930 strcpy(px, p + 1);
931 free(socklist[i].inbuf);
932 if (px[0])
933 socklist[i].inbuf = px;
934 else {
935 free(px);
936 socklist[i].inbuf = NULL;
937 }
938 /* Strip CR if this was CR/LF combo */
939 if (s[strlen(s) - 1] == '\r')
940 s[strlen(s) - 1] = 0;
941 *len = strlen(s);
942 return socklist[i].sock;
943 }
944 } else {
945 /* Handling buffered binary data (must have been SOCK_BUFFER before). */
946 if (socklist[i].inbuflen <= 510) {
947 *len = socklist[i].inbuflen;
948 memcpy(s, socklist[i].inbuf, socklist[i].inbuflen);
949 free_null(socklist[i].inbuf);
950 socklist[i].inbuflen = 0;
951 } else {
952 /* Split up into chunks of 510 bytes. */
953 *len = 510;
954 memcpy(s, socklist[i].inbuf, *len);
955 memcpy(socklist[i].inbuf, socklist[i].inbuf + *len, *len);
956 socklist[i].inbuflen -= *len;
957 socklist[i].inbuf = realloc(socklist[i].inbuf,
958 socklist[i].inbuflen);
959 }
960 return socklist[i].sock;
961 }
962 }
963 /* Also check any sockets that might have EOF'd during write */
964 if (!(socklist[i].flags & SOCK_UNUSED)
965 && (socklist[i].flags & SOCK_EOFD)) {
966 s[0] = 0;
967 *len = socklist[i].sock;
968 return -1;
969 }
970 }
971 /* No pent-up data of any worth -- down to business */
972 *len = 0;
973 ret = sockread(xx, len);
974 if (ret < 0) {
975 s[0] = 0;
976 return ret;
977 }
978 /* Binary, listening and passed on sockets don't get buffered. */
979 if (socklist[ret].flags & SOCK_CONNECT) {
980 if (socklist[ret].flags & SOCK_STRONGCONN) {
981 socklist[ret].flags &= ~SOCK_STRONGCONN;
982 /* Buffer any data that came in, for future read. */
983 socklist[ret].inbuflen = *len;
984 socklist[ret].inbuf = (char *) malloc(*len + 1);
985 /* It might be binary data. You never know. */
986 memcpy(socklist[ret].inbuf, xx, *len);
987 socklist[ret].inbuf[*len] = 0;
988 }
989 socklist[ret].flags &= ~SOCK_CONNECT;
990 s[0] = 0;
991 return socklist[ret].sock;
992 }
993 if (socklist[ret].flags & SOCK_BINARY) {
994 memcpy(s, xx, *len);
995 return socklist[ret].sock;
996 }
997 if ((socklist[ret].flags & SOCK_LISTEN) ||
998 (socklist[ret].flags & SOCK_PASS))
999 return socklist[ret].sock;
1000 if (socklist[ret].flags & SOCK_BUFFER) {
1001 socklist[ret].inbuf = (char *) realloc(socklist[ret].inbuf,
1002 socklist[ret].inbuflen + *len + 1);
1003 memcpy(socklist[ret].inbuf + socklist[ret].inbuflen, xx, *len);
1004 socklist[ret].inbuflen += *len;
1005 /* We don't know whether it's binary data. Make sure normal strings
1006 will be handled properly later on too. */
1007 socklist[ret].inbuf[socklist[ret].inbuflen] = 0;
1008 return -4; /* Ignore this one. */
1009 }
1010 /* Might be necessary to prepend stored-up data! */
1011 if (socklist[ret].inbuf != NULL) {
1012 p = socklist[ret].inbuf;
1013 socklist[ret].inbuf = (char *) malloc(strlen(p) + strlen(xx) + 1);
1014 strcpy(socklist[ret].inbuf, p);
1015 strcat(socklist[ret].inbuf, xx);
1016 free(p);
1017 if (strlen(socklist[ret].inbuf) < 512) {
1018 strcpy(xx, socklist[ret].inbuf);
1019 free_null(socklist[ret].inbuf);
1020 socklist[ret].inbuflen = 0;
1021 } else {
1022 p = socklist[ret].inbuf;
1023 socklist[ret].inbuflen = strlen(p) - 510;
1024 socklist[ret].inbuf = (char *) malloc(socklist[ret].inbuflen + 1);
1025 strcpy(socklist[ret].inbuf, p + 510);
1026 *(p + 510) = 0;
1027 strcpy(xx, p);
1028 free(p);
1029 /* (leave the rest to be post-pended later) */
1030 }
1031 }
1032 /* Look for EOL marker; if it's there, i have something to show */
1033 p = strchr(xx, '\n');
1034 if (p == NULL)
1035 p = strchr(xx, '\r');
1036 if (p != NULL) {
1037 *p = 0;
1038 strcpy(s, xx);
1039 strcpy(xx, p + 1);
1040 if (s[strlen(s) - 1] == '\r')
1041 s[strlen(s) - 1] = 0;
1042 data = 1; /* DCC_CHAT may now need to process a
1043 blank line */
1044 /* NO! */
1045 /* if (!s[0]) strcpy(s," "); */
1046 } else {
1047 s[0] = 0;
1048 if (strlen(xx) >= 510) {
1049 /* String is too long, so just insert fake \n */
1050 strcpy(s, xx);
1051 xx[0] = 0;
1052 data = 1;
1053 }
1054 }
1055 *len = strlen(s);
1056 /* Anything left that needs to be saved? */
1057 if (!xx[0]) {
1058 if (data)
1059 return socklist[ret].sock;
1060 else
1061 return -3;
1062 }
1063 /* Prepend old data back */
1064 if (socklist[ret].inbuf != NULL) {
1065 p = socklist[ret].inbuf;
1066 socklist[ret].inbuflen = strlen(p) + strlen(xx);
1067 socklist[ret].inbuf = (char *) malloc(socklist[ret].inbuflen + 1);
1068 strcpy(socklist[ret].inbuf, xx);
1069 strcat(socklist[ret].inbuf, p);
1070 free(p);
1071 } else {
1072 socklist[ret].inbuflen = strlen(xx);
1073 socklist[ret].inbuf = (char *) malloc(socklist[ret].inbuflen + 1);
1074 strcpy(socklist[ret].inbuf, xx);
1075 }
1076 if (data) {
1077 return socklist[ret].sock;
1078 } else {
1079 return -3;
1080 }
1081 }
1082
1083 /* Dump something to a socket
1084 */
1085 void tputs(register int z, char *s, unsigned int len)
1086 {
1087 register int i, x, idx;
1088 char *p;
1089 static int inhere = 0;
1090
1091 if (z < 0)
1092 return; /* um... HELLO?! sanity check please! */
1093 if (((z == STDOUT) || (z == STDERR)) && (!backgrd || use_stderr)) {
1094 write(z, s, len);
1095 return;
1096 }
1097 for (i = 0; i < MAXSOCKS; i++) {
1098 if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == z)) {
1099 for (idx = 0; idx < dcc_total; idx++) {
1100 if (dcc[idx].sock == z) {
1101 if (dcc[idx].type) {
1102 if (dcc[idx].type->name) {
1103 if (!strncmp(dcc[idx].type->name, "BOT", 3)) {
1104 traffic.out_today.bn += len;
1105 break;
1106 } else if (!strcmp(dcc[idx].type->name, "SERVER")) {
1107 traffic.out_today.irc += len;
1108 break;
1109 } else if (!strncmp(dcc[idx].type->name, "CHAT", 4)) {
1110 traffic.out_today.dcc += len;
1111 break;
1112 } else if (!strncmp(dcc[idx].type->name, "FILES", 5)) {
1113 traffic.out_today.filesys += len;
1114 break;
1115 } else if (!strcmp(dcc[idx].type->name, "SEND")) {
1116 traffic.out_today.trans += len;
1117 break;
1118 } else if (!strncmp(dcc[idx].type->name, "GET", 3)) {
1119 traffic.out_today.trans += len;
1120 break;
1121 } else {
1122 traffic.out_today.unknown += len;
1123 break;
1124 }
1125 }
1126 }
1127 }
1128 }
1129
1130 if (socklist[i].outbuf != NULL) {
1131 /* Already queueing: just add it */
1132 p = (char *) realloc(socklist[i].outbuf, socklist[i].outbuflen + len);
1133 memcpy(p + socklist[i].outbuflen, s, len);
1134 socklist[i].outbuf = p;
1135 socklist[i].outbuflen += len;
1136 return;
1137 }
1138 /* Try. */
1139 x = write(z, s, len);
1140 if (x == (-1))
1141 x = 0;
1142 if (x < len) {
1143 /* Socket is full, queue it */
1144 socklist[i].outbuf = (char *) malloc(len - x);
1145 memcpy(socklist[i].outbuf, &s[x], len - x);
1146 socklist[i].outbuflen = len - x;
1147 }
1148 return;
1149 }
1150 }
1151 /* Make sure we don't cause a crash by looping here */
1152 if (!inhere) {
1153 inhere = 1;
1154
1155 putlog(LOG_MISC, "*", "!!! writing to nonexistent socket: %d", z);
1156 s[strlen(s) - 1] = 0;
1157 putlog(LOG_MISC, "*", "!-> '%s'", s);
1158
1159 inhere = 0;
1160 }
1161 }
1162
1163 /* tputs might queue data for sockets, let's dump as much of it as
1164 * possible.
1165 */
1166 void dequeue_sockets()
1167 {
1168 int i, x;
1169
1170 for (i = 0; i < MAXSOCKS; i++) {
1171 if (!(socklist[i].flags & SOCK_UNUSED) &&
1172 socklist[i].outbuf != NULL) {
1173 /* Trick tputs into doing the work */
1174 x = write(socklist[i].sock, socklist[i].outbuf,
1175 socklist[i].outbuflen);
1176 if ((x < 0) && (errno != EAGAIN)
1177 #ifdef EBADSLT
1178 && (errno != EBADSLT)
1179 #endif
1180 #ifdef ENOTCONN
1181 && (errno != ENOTCONN)
1182 #endif
1183 ) {
1184 /* This detects an EOF during writing */
1185 debug3("net: eof!(write) socket %d (%s,%d)", socklist[i].sock,
1186 strerror(errno), errno);
1187 socklist[i].flags |= SOCK_EOFD;
1188 } else if (x == socklist[i].outbuflen) {
1189 /* If the whole buffer was sent, nuke it */
1190 free_null(socklist[i].outbuf);
1191 socklist[i].outbuflen = 0;
1192 } else if (x > 0) {
1193 char *p = socklist[i].outbuf;
1194
1195 /* This removes any sent bytes from the beginning of the buffer */
1196 socklist[i].outbuf = (char *) malloc(socklist[i].outbuflen - x);
1197 memcpy(socklist[i].outbuf, p + x, socklist[i].outbuflen - x);
1198 socklist[i].outbuflen -= x;
1199 free(p);
1200 }
1201
1202 /* All queued data was sent. Call handler if one exists and the
1203 * dcc entry wants it.
1204 */
1205 if (!socklist[i].outbuf) {
1206 int idx = findanyidx(socklist[i].sock);
1207
1208 if (idx > 0 && dcc[idx].type && dcc[idx].type->outdone)
1209 dcc[idx].type->outdone(idx);
1210 }
1211 }
1212 }
1213 }
1214
1215
1216 /*
1217 * Debugging stuff
1218 */
1219
1220 void tell_netdebug(int idx)
1221 {
1222 int i;
1223 char s[80];
1224
1225 dprintf(idx, "Open sockets:");
1226 for (i = 0; i < MAXSOCKS; i++) {
1227 if (!(socklist[i].flags & SOCK_UNUSED)) {
1228 sprintf(s, " %d", socklist[i].sock);
1229 if (socklist[i].flags & SOCK_BINARY)
1230 strcat(s, " (binary)");
1231 if (socklist[i].flags & SOCK_LISTEN)
1232 strcat(s, " (listen)");
1233 if (socklist[i].flags & SOCK_PASS)
1234 strcat(s, " (passed on)");
1235 if (socklist[i].flags & SOCK_CONNECT)
1236 strcat(s, " (connecting)");
1237 if (socklist[i].flags & SOCK_STRONGCONN)
1238 strcat(s, " (strong)");
1239 if (socklist[i].flags & SOCK_NONSOCK)
1240 strcat(s, " (file)");
1241 if (socklist[i].inbuf != NULL)
1242 sprintf(&s[strlen(s)], " (inbuf: %04X)", strlen(socklist[i].inbuf));
1243 if (socklist[i].outbuf != NULL)
1244 sprintf(&s[strlen(s)], " (outbuf: %06lX)", socklist[i].outbuflen);
1245 strcat(s, ",");
1246 dprintf(idx, "%s", s);
1247 }
1248 }
1249 dprintf(idx, " done.\n");
1250 }
1251
1252 /* Checks wether the referenced socket has data queued.
1253 *
1254 * Returns true if the incoming/outgoing (depending on 'type') queues
1255 * contain data, otherwise false.
1256 */
1257 int sock_has_data(int type, int sock)
1258 {
1259 int ret = 0, i;
1260
1261 for (i = 0; i < MAXSOCKS; i++)
1262 if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
1263 break;
1264 if (i < MAXSOCKS) {
1265 switch (type) {
1266 case SOCK_DATA_OUTGOING:
1267 ret = (socklist[i].outbuf != NULL);
1268 break;
1269 case SOCK_DATA_INCOMING:
1270 ret = (socklist[i].inbuf != NULL);
1271 break;
1272 }
1273 } else
1274 debug1("sock_has_data: could not find socket #%d, returning false.", sock);
1275 return ret;
1276 }
1277
1278 /* flush_inbuf():
1279 * checks if there's data in the incoming buffer of an connection
1280 * and flushs the buffer if possible
1281 *
1282 * returns: -1 if the dcc entry wasn't found
1283 * -2 if dcc[idx].type->activity doesn't exist and the data couldn't
1284 * be handled
1285 * 0 if buffer was empty
1286 * otherwise length of flushed buffer
1287 */
1288 int flush_inbuf(int idx)
1289 {
1290 int i, len;
1291 char *inbuf;
1292
1293 assert((idx >= 0) && (idx < dcc_total));
1294 for (i = 0; i < MAXSOCKS; i++) {
1295 if ((dcc[idx].sock == socklist[i].sock)
1296 && !(socklist[i].flags & SOCK_UNUSED)) {
1297 len = socklist[i].inbuflen;
1298 if ((len > 0) && socklist[i].inbuf) {
1299 if (dcc[idx].type && dcc[idx].type->activity) {
1300 inbuf = socklist[i].inbuf;
1301 socklist[i].inbuf = NULL;
1302 dcc[idx].type->activity(idx, inbuf, len);
1303 free(inbuf);
1304 return len;
1305 } else
1306 return -2;
1307 } else
1308 return 0;
1309 }
1310 }
1311 return -1;
1312 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23