/[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.42 - (show annotations) (download) (as text)
Wed Oct 10 10:44:04 2001 UTC (17 years, 8 months ago) by tothwolf
Branch: MAIN
Changes since 1.41: +47 -65 lines
File MIME type: text/x-chdr
all expmem functions and support removed
all memory status/debugging code removed
all #ifdef DEBUG_MEM code removed

nmalloc macro/n_malloc function removed, use malloc() now
mod_malloc function removed
my_strdup function removed, use malloc_strcpy macro now
channel_malloc removed, use malloc_memset macro now
n_malloc_null function removed, use malloc_memset macro now
user_malloc macro/_user_malloc function removed, use malloc() now

nrealloc macro/n_realloc function removed, use realloc() now
mod_realloc function removed

nfree macro/n_free function removed, use free() now
mod_free function removed
my_free macro removed, use free_null macro now
Tcl_Free compat macro now uses free() instead of removed n_free function

malloc->strcpy now uses malloc_strcpy macro
free(ptr);ptr=NULL; now uses free_null macro
malloc->bzero now uses malloc_memset macro
get_data_ptr macro/_get_data_ptr function removed, use malloc_memset macro now

compat memcpy removed
compat memset removed
egg_memcpy/my_memcpy misuse/mess changed to memcpy
egg_bzero/bzero macro removed, use memset() now

many uninitialized pointers now default to NULL

match.c is now compiled on it's own instead of being included into tclhash.c
match.c cleanup, removed broken/incomplete ircII compat code
wild_match macro removed, _wild_match function renamed to wild_match
wild_match_per function no longer declared static
added prototypes for wild_match and wild_match_per functions

Major function/macro changes:
  nmalloc -> malloc()
  nrealloc -> realloc()
  nfree -> free()

Bugs found and fixed during audit:
  buffer overflow in adduser()

Bugs fixed when code was removed:
  typo in filedb3.c: EBUG_MEM -> DEBUG_MEM
  extra egg_bzero call from filesys.c

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23