/[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.40 - (show annotations) (download) (as text)
Sun Aug 19 02:36:23 2001 UTC (17 years, 10 months ago) by drummer
Branch: MAIN
Changes since 1.39: +14 -24 lines
File MIME type: text/x-chdr
Removed my-hostname. Cache local ipv4 address. Updated docs. FIXME for tcl_getmyip6(). Cleanups for channel.mod.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23