/[cvs]/eggdrop1.9/src/net.c
ViewVC logotype

Annotation of /eggdrop1.9/src/net.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.44 - (hide annotations) (download) (as text)
Fri Oct 12 02:27:45 2001 UTC (18 years ago) by stdarg
Branch: MAIN
Changes since 1.43: +15 -11 lines
File MIME type: text/x-chdr
Moved some stuff from main.c to botnet.c (for modularizing botnet)
Changed irctoupper to _irctoupper in match.c (temporary fix, cuz something is making it crash (I think irctoupper isn't initialized or exported or something))
Added code to expand the socket table when it's full.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23