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

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

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


Revision 1.2 - (hide annotations) (download) (as text)
Thu Aug 5 18:12:05 2010 UTC (8 years, 10 months ago) by pseudo
Branch: MAIN
Changes since 1.1: +312 -295 lines
File MIME type: text/x-chdr
Added new, full IPv6 support to eggdrop.

1 simple 1.1 /*
2     * net.c -- handles:
3     * all raw network i/o
4     *
5 pseudo 1.2 * $Id: net.c,v 1.1.1.1 2010/07/26 21:11:06 simple Exp $
6 simple 1.1 */
7     /*
8     * This is hereby released into the public domain.
9     * Robey Pointer, robey@netcom.com
10     *
11     * Changes after Feb 23, 1999 Copyright Eggheads Development Team
12     *
13     * Copyright (C) 1999 - 2010 Eggheads Development Team
14     *
15     * This program is free software; you can redistribute it and/or
16     * modify it under the terms of the GNU General Public License
17     * as published by the Free Software Foundation; either version 2
18     * of the License, or (at your option) any later version.
19     *
20     * This program is distributed in the hope that it will be useful,
21     * but WITHOUT ANY WARRANTY; without even the implied warranty of
22     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23     * GNU General Public License for more details.
24     *
25     * You should have received a copy of the GNU General Public License
26     * along with this program; if not, write to the Free Software
27     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28     */
29    
30     #include <fcntl.h>
31     #include "main.h"
32     #include <limits.h>
33     #include <string.h>
34     #include <netdb.h>
35     #include <sys/socket.h>
36     #if HAVE_SYS_SELECT_H
37     # include <sys/select.h>
38     #endif
39     #include <netinet/in.h>
40     #include <arpa/inet.h>
41     #include <errno.h>
42     #if HAVE_UNISTD_H
43     # include <unistd.h>
44     #endif
45     #include <setjmp.h>
46    
47     #ifndef HAVE_GETDTABLESIZE
48     # ifdef FD_SETSIZE
49     # define getdtablesize() FD_SETSIZE
50     # else
51     # define getdtablesize() 200
52     # endif
53     #endif
54    
55     extern struct dcc_t *dcc;
56     extern int backgrd, use_stderr, resolve_timeout, dcc_total;
57     extern unsigned long otraffic_irc_today, otraffic_bn_today, otraffic_dcc_today,
58     otraffic_filesys_today, otraffic_trans_today,
59     otraffic_unknown_today;
60    
61 pseudo 1.2 char natip[121] = ""; /* Public IPv4 to report for systems behind NAT */
62     char listen_ip[121] = ""; /* IP (or hostname) for listening sockets */
63     char vhost[121] = ""; /* IPv4 vhost for outgoing connections */
64     #ifdef IPV6
65     char vhost6[121] = ""; /* IPv6 vhost for outgoing connections */
66     int pref_af = 0; /* Prefer IPv6 over IPv4? */
67     #endif
68 simple 1.1 char firewall[121] = ""; /* Socks server for firewall. */
69     int firewallport = 1080; /* Default port of socks 4/5 firewalls. */
70     char botuser[21] = "eggdrop"; /* Username of the user running the bot. */
71     int dcc_sanitycheck = 0; /* Do some sanity checking on dcc connections. */
72    
73     sock_list *socklist = NULL; /* Enough to be safe. */
74     sigjmp_buf alarmret; /* Env buffer for alarm() returns. */
75    
76     /* Types of proxies */
77     #define PROXY_SOCKS 1
78     #define PROXY_SUN 2
79    
80    
81     /* I need an UNSIGNED long for dcc type stuff
82     */
83     IP my_atoul(char *s)
84     {
85     IP ret = 0;
86    
87     while ((*s >= '0') && (*s <= '9')) {
88     ret *= 10;
89     ret += ((*s) - '0');
90     s++;
91     }
92     return ret;
93     }
94    
95     int expmem_net()
96     {
97     int i, tot = 0;
98     struct threaddata *td = threaddata();
99    
100     for (i = 0; i < td->MAXSOCKS; i++) {
101     if (!(td->socklist[i].flags & (SOCK_UNUSED | SOCK_TCL))) {
102     if (td->socklist[i].handler.sock.inbuf != NULL)
103     tot += strlen(td->socklist[i].handler.sock.inbuf) + 1;
104     if (td->socklist[i].handler.sock.outbuf != NULL)
105     tot += td->socklist[i].handler.sock.outbuflen;
106     }
107     }
108     return tot;
109     }
110    
111 pseudo 1.2 /* Extract the IP address from a sockaddr struct and convert it
112     * to presentation format.
113     */
114     char *iptostr(struct sockaddr *sa)
115     {
116     #ifdef IPV6
117     static char s[INET6_ADDRSTRLEN] = "";
118     if (sa->sa_family == AF_INET6)
119     inet_ntop(AF_INET6, &((struct sockaddr_in6 *)sa)->sin6_addr,
120     s, sizeof s);
121     else
122     #else
123     static char s[sizeof "255.255.255.255"] = "";
124     #endif
125     inet_ntop(AF_INET, &((struct sockaddr_in *)sa)->sin_addr.s_addr, s,
126     sizeof s);
127     return s;
128     }
129    
130     /* Fills in a sockname struct with the given server and port. If the string
131     * pointed by src isn't an IP address and allowres is not null, the function
132     * will assume it's a hostname and will attempt to resolve it. This is
133     * convenient, but you should use the async dns functions where possible, to
134     * avoid blocking the bot while the lookup is performed.
135 simple 1.1 */
136 pseudo 1.2 int setsockname(sockname_t *addr, char *src, int port, int allowres)
137 simple 1.1 {
138     struct hostent *hp;
139 pseudo 1.2 int af = AF_UNSPEC;
140     #ifdef IPV6
141     int pref;
142    
143     /* Clean start */
144     egg_bzero(addr, sizeof(sockname_t));
145     af = pref = pref_af ? AF_INET6 : AF_INET;
146     if (pref == AF_INET) {
147     if (!egg_inet_aton(src, &addr->addr.s4.sin_addr))
148     af = AF_INET6;
149     } else {
150     if (inet_pton(af, src, &addr->addr.s6.sin6_addr) != 1)
151     af = AF_INET;
152     }
153     if (af != pref)
154     if (((af == AF_INET6) &&
155     (inet_pton(af, src, &addr->addr.s6.sin6_addr) != 1)) ||
156     ((af == AF_INET) &&
157     !egg_inet_aton(src, &addr->addr.s4.sin_addr)))
158     af = AF_UNSPEC;
159    
160     if (af == AF_UNSPEC && allowres) {
161     /* src is a hostname. Attempt to resolve it.. */
162     if (!sigsetjmp(alarmret, 1)) {
163     alarm(resolve_timeout);
164     hp = gethostbyname2(src, pref_af ? AF_INET6 : AF_INET);
165     if (!hp)
166     hp = gethostbyname2(src, pref_af ? AF_INET : AF_INET6);
167     alarm(0);
168     } else
169     hp = NULL;
170     if (hp) {
171     if (hp->h_addrtype == AF_INET)
172     egg_memcpy(&addr->addr.s4.sin_addr, hp->h_addr, hp->h_length);
173     else
174     egg_memcpy(&addr->addr.s6.sin6_addr, hp->h_addr, hp->h_length);
175     af = hp->h_addrtype;
176     }
177     }
178    
179     addr->family = (af == AF_UNSPEC) ? pref : af;
180     addr->addr.sa.sa_family = addr->family;
181     if (addr->family == AF_INET6) {
182     addr->addrlen = sizeof(struct sockaddr_in6);
183     addr->addr.s6.sin6_port = htons(port);
184     addr->addr.s6.sin6_family = AF_INET6;
185     } else {
186     addr->addrlen = sizeof(struct sockaddr_in);
187     addr->addr.s4.sin_port = htons(port);
188     addr->addr.s4.sin_family = AF_INET;
189     }
190     #else
191     egg_bzero(addr, sizeof(sockname_t));
192     if (!egg_inet_aton(src, &addr->addr.s4.sin_addr) && allowres) {
193     /* src is a hostname. Attempt to resolve it.. */
194     if (!sigsetjmp(alarmret, 1)) {
195     alarm(resolve_timeout);
196     hp = gethostbyname(src);
197     alarm(0);
198     } else
199     hp = NULL;
200     if (hp) {
201     egg_memcpy(&addr->addr.s4.sin_addr, hp->h_addr, hp->h_length);
202     af = hp->h_addrtype;
203     }
204     }
205    
206     addr->family = addr->addr.s4.sin_family = AF_INET;
207     addr->addr.sa.sa_family = addr->family;
208     addr->addrlen = sizeof(struct sockaddr_in);
209     addr->addr.s4.sin_port = htons(port);
210 simple 1.1 #endif
211 pseudo 1.2 return af;
212     }
213    
214     /* Get socket address to bind to for outbound connections
215     */
216     void getvhost(sockname_t *addr, int af)
217     {
218     char *h;
219    
220     if (af == AF_INET)
221     h = vhost;
222     #ifdef IPV6
223     else
224     h = vhost6;
225 simple 1.1 #endif
226 pseudo 1.2 if (setsockname(addr, h, 0, 1) != af)
227     setsockname(addr, (af == AF_INET ? "0" : "::"), 0, 0);
228     /* Remember this 'self-lookup failed' thingie?
229     I have good news - you won't see it again ;) */
230 simple 1.1 }
231    
232     /* Sets/Unsets options for a specific socket.
233     *
234     * Returns: 0 - on success
235     * -1 - socket not found
236     * -2 - illegal operation
237     */
238     int sockoptions(int sock, int operation, int sock_options)
239     {
240     int i;
241     struct threaddata *td = threaddata();
242    
243     for (i = 0; i < td->MAXSOCKS; i++)
244     if ((td->socklist[i].sock == sock) &&
245     !(td->socklist[i].flags & SOCK_UNUSED)) {
246     if (operation == EGG_OPTION_SET)
247     td->socklist[i].flags |= sock_options;
248     else if (operation == EGG_OPTION_UNSET)
249     td->socklist[i].flags &= ~sock_options;
250     else
251     return -2;
252     return 0;
253     }
254     return -1;
255     }
256    
257     /* Return a free entry in the socket entry
258     */
259     int allocsock(int sock, int options)
260     {
261     int i;
262     struct threaddata *td = threaddata();
263    
264     for (i = 0; i < td->MAXSOCKS; i++) {
265     if (td->socklist[i].flags & SOCK_UNUSED) {
266     /* yay! there is table space */
267     td->socklist[i].handler.sock.inbuf = NULL;
268     td->socklist[i].handler.sock.outbuf = NULL;
269     td->socklist[i].handler.sock.inbuflen = 0;
270     td->socklist[i].handler.sock.outbuflen = 0;
271     td->socklist[i].flags = options;
272     td->socklist[i].sock = sock;
273     return i;
274     }
275     }
276     /* Try again if enlarging socketlist works */
277     if (increase_socks_max())
278     return -1;
279     else
280     return allocsock(sock, options);
281     }
282    
283     /* Return a free entry in the socket entry for a tcl socket
284     *
285     * alloctclsock() can be called by Tcl threads
286     */
287     int alloctclsock(register int sock, int mask, Tcl_FileProc *proc, ClientData cd)
288     {
289     int f = -1;
290     register int i;
291     struct threaddata *td = threaddata();
292    
293     for (i = 0; i < td->MAXSOCKS; i++) {
294     if (td->socklist[i].flags & SOCK_UNUSED) {
295     if (f == -1)
296     f = i;
297     } else if ((td->socklist[i].flags & SOCK_TCL) &&
298     td->socklist[i].sock == sock) {
299     f = i;
300     break;
301     }
302     }
303     if (f != -1) {
304     td->socklist[f].sock = sock;
305     td->socklist[f].flags = SOCK_TCL;
306     td->socklist[f].handler.tclsock.mask = mask;
307     td->socklist[f].handler.tclsock.proc = proc;
308     td->socklist[f].handler.tclsock.cd = cd;
309     return f;
310     }
311     /* Try again if enlarging socketlist works */
312     if (increase_socks_max())
313     return -1;
314     else
315     return alloctclsock(sock, mask, proc, cd);
316     }
317    
318     /* Request a normal socket for i/o
319     */
320     void setsock(int sock, int options)
321     {
322     int i = allocsock(sock, options), parm;
323     struct threaddata *td = threaddata();
324    
325     if (i == -1) {
326     putlog(LOG_MISC, "*", "Sockettable full.");
327     return;
328     }
329     if (((sock != STDOUT) || backgrd) && !(td->socklist[i].flags & SOCK_NONSOCK)) {
330     parm = 1;
331     setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *) &parm, sizeof(int));
332    
333     parm = 0;
334     setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &parm, sizeof(int));
335     }
336     if (options & SOCK_LISTEN) {
337     /* Tris says this lets us grab the same port again next time */
338     parm = 1;
339     setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *) &parm, sizeof(int));
340     }
341     /* Yay async i/o ! */
342     fcntl(sock, F_SETFL, O_NONBLOCK);
343     }
344    
345 pseudo 1.2 int getsock(int af, int options)
346 simple 1.1 {
347 pseudo 1.2 int sock = socket(af, SOCK_STREAM, 0);
348    
349 simple 1.1 if (sock >= 0)
350     setsock(sock, options);
351     else
352 pseudo 1.2 putlog(LOG_MISC, "*", "Warning: Can't create new socket: %s!",
353     strerror(errno));
354 simple 1.1 return sock;
355     }
356    
357     /* Done with a socket
358     */
359     void killsock(register int sock)
360     {
361     register int i;
362     struct threaddata *td = threaddata();
363    
364     /* Ignore invalid sockets. */
365     if (sock < 0)
366     return;
367    
368     for (i = 0; i < td->MAXSOCKS; i++) {
369     if ((td->socklist[i].sock == sock) && !(td->socklist[i].flags & SOCK_UNUSED)) {
370     if (!(td->socklist[i].flags & SOCK_TCL)) { /* nothing to free for tclsocks */
371     close(td->socklist[i].sock);
372     if (td->socklist[i].handler.sock.inbuf != NULL) {
373     nfree(td->socklist[i].handler.sock.inbuf);
374     td->socklist[i].handler.sock.inbuf = NULL;
375     }
376     if (td->socklist[i].handler.sock.outbuf != NULL) {
377     nfree(td->socklist[i].handler.sock.outbuf);
378     td->socklist[i].handler.sock.outbuf = NULL;
379     td->socklist[i].handler.sock.outbuflen = 0;
380     }
381     }
382     td->socklist[i].flags = SOCK_UNUSED;
383     return;
384     }
385     }
386     putlog(LOG_MISC, "*", "Warning: Attempt to kill un-allocated socket %d!", sock);
387     }
388    
389     /* Done with a tcl socket
390     *
391     * killtclsock() can be called by Tcl threads
392     */
393     void killtclsock(register int sock)
394     {
395     register int i;
396     struct threaddata *td = threaddata();
397    
398     if (sock < 0)
399     return;
400    
401     for (i = 0; i < td->MAXSOCKS; i++) {
402     if ((td->socklist[i].flags & SOCK_TCL) && td->socklist[i].sock == sock) {
403     td->socklist[i].flags = SOCK_UNUSED;
404     return;
405     }
406     }
407     }
408    
409     /* Send connection request to proxy
410     */
411 pseudo 1.2 static int proxy_connect(int sock, sockname_t *addr)
412 simple 1.1 {
413 pseudo 1.2 sockname_t name;
414     char host[121], s[256];
415     int i, port, proxy;
416    
417     if (!firewall[0])
418     return -2;
419     #ifdef IPV6
420     if (addr->family == AF_INET6) {
421     putlog(LOG_MISC, "*", "Eggdrop doesn't support IPv6 connections "
422     "through proxies yet.");
423     return -1;
424     }
425     #endif
426     if (firewall[0] == '!') {
427     proxy = PROXY_SUN;
428     strcpy(host, &firewall[1]);
429     } else {
430     proxy = PROXY_SOCKS;
431     strcpy(host, firewall);
432     }
433     port = addr->addr.s4.sin_port;
434     setsockname(&name, host, firewallport, 1);
435     if (connect(sock, &name.addr.sa, name.addrlen) < 0 && errno != EINPROGRESS)
436     return -1;
437 simple 1.1 if (proxy == PROXY_SOCKS) {
438 pseudo 1.2 for (i = 0; i < threaddata()->MAXSOCKS; i++)
439 simple 1.1 if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
440     socklist[i].flags |= SOCK_PROXYWAIT; /* drummer */
441 pseudo 1.2 egg_memcpy(host, &addr->addr.s4.sin_addr.s_addr, 4);
442     egg_snprintf(s, sizeof s, "\004\001%c%c%c%c%c%c%s", port % 256,
443     (port >> 8) % 256, host[0], host[1], host[2], host[3], botuser);
444 simple 1.1 tputs(sock, s, strlen(botuser) + 9); /* drummer */
445     } else if (proxy == PROXY_SUN) {
446 pseudo 1.2 inet_ntop(AF_INET, &addr->addr.s4.sin_addr, host, sizeof host);
447 simple 1.1 egg_snprintf(s, sizeof s, "%s %d\n", host, port);
448     tputs(sock, s, strlen(s)); /* drummer */
449     }
450     return sock;
451     }
452    
453 pseudo 1.2 /* Starts a connection attempt through a socket
454 simple 1.1 *
455 pseudo 1.2 * The server address should be filled in addr by setsockname() or by the
456     * non-blocking dns functions and setsnport().
457 simple 1.1 *
458 pseudo 1.2 * returns < 0 if connection refused:
459     * -1 strerror() type error
460 simple 1.1 */
461 pseudo 1.2 int open_telnet_raw(int sock, sockname_t *addr)
462 simple 1.1 {
463 pseudo 1.2 sockname_t name;
464     int i, rc;
465    
466     getvhost(&name, addr->family);
467     if (bind(sock, &name.addr.sa, name.addrlen) < 0) {
468 simple 1.1 return -1;
469     }
470     for (i = 0; i < threaddata()->MAXSOCKS; i++) {
471     if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == sock))
472     socklist[i].flags = (socklist[i].flags & ~SOCK_VIRTUAL) | SOCK_CONNECT;
473     }
474 pseudo 1.2 if (addr->family == AF_INET && firewall[0])
475     return proxy_connect(sock, addr);
476     rc = connect(sock, &addr->addr.sa, addr->addrlen);
477 simple 1.1 if (rc < 0) {
478 pseudo 1.2 if (errno == EINPROGRESS)
479 simple 1.1 return sock; /* async success! */
480 pseudo 1.2 else
481 simple 1.1 return -1;
482     }
483     return sock;
484     }
485    
486 pseudo 1.2 /* Ordinary non-binary connection attempt
487     * Return values:
488     * >=0: connect successful, returned is the socket number
489     * -1: look at errno or use strerror()
490     * -2: lookup failed or server is not a valid IP string
491     */
492 simple 1.1 int open_telnet(char *server, int port)
493     {
494 pseudo 1.2 int ret, sock;
495     sockname_t name;
496    
497     ret = setsockname(&name, server, port, 1);
498     if (ret == AF_UNSPEC)
499     return -2;
500     sock = getsock(ret, 0);
501     if (sock < 0)
502     return -1;
503     ret = open_telnet_raw(sock, &name);
504     if (ret < 0)
505     killsock(sock);
506 simple 1.1 return ret;
507     }
508    
509     /* Returns a socket number for a listening socket that will accept any
510 pseudo 1.2 * connection on the given address. The address can be filled in by
511     * setsockname().
512 simple 1.1 */
513 pseudo 1.2 int open_address_listen(sockname_t *addr)
514     {
515 simple 1.1 int sock = 0;
516    
517 pseudo 1.2 sock = getsock(addr->family, SOCK_LISTEN);
518     if (sock < 0)
519 simple 1.1 return -1;
520 pseudo 1.2 #if defined IPV6 && IPV6_V6ONLY
521     if (addr->family == AF_INET6) {
522     int on = 0;
523     setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &on, sizeof(on));
524     }
525     #endif
526     if (bind(sock, &addr->addr.sa, addr->addrlen) < 0) {
527     killsock(sock);
528     return -2;
529 simple 1.1 }
530    
531 pseudo 1.2 if (getsockname(sock, &addr->addr.sa, &addr->addrlen) < 0) {
532     killsock(sock);
533     return -1;
534     }
535     if (listen(sock, 1) < 0) {
536     killsock(sock);
537     return -1;
538 simple 1.1 }
539    
540     return sock;
541     }
542    
543     /* Returns a socket number for a listening socket that will accept any
544     * connection -- port # is returned in port
545     */
546     inline int open_listen(int *port)
547     {
548 pseudo 1.2 int sock;
549     sockname_t name;
550 simple 1.1
551 pseudo 1.2 (void) setsockname(&name, listen_ip, *port, 1);
552     sock = open_address_listen(&name);
553     if (name.addr.sa.sa_family == AF_INET)
554     *port = ntohs(name.addr.s4.sin_port);
555     #ifdef IPV6
556     else
557     *port = ntohs(name.addr.s6.sin6_port);
558     #endif
559     return sock;
560 simple 1.1 }
561    
562 pseudo 1.2 /* Short routine to answer a connect received on a listening socket.
563     * Returned is the new socket.
564     * If port is not NULL, it points to an integer to hold the port number
565     * of the caller.
566 simple 1.1 */
567 pseudo 1.2 int answer(int sock, sockname_t *caller, unsigned short *port, int binary)
568 simple 1.1 {
569     int new_sock;
570 pseudo 1.2 caller->addrlen = sizeof(caller->addr);
571     new_sock = accept(sock, &caller->addr.sa, &caller->addrlen);
572    
573 simple 1.1 if (new_sock < 0)
574     return -1;
575 pseudo 1.2
576     caller->family = caller->addr.sa.sa_family;
577     if (port) {
578     if (caller->family == AF_INET)
579     *port = ntohs(caller->addr.s4.sin_port);
580     #ifdef IPV6
581     else
582     *port = ntohs(caller->addr.s6.sin6_port);
583     #endif
584     }
585 simple 1.1 setsock(new_sock, (binary ? SOCK_BINARY : 0));
586     return new_sock;
587     }
588    
589 pseudo 1.2 /* Get DCC compatible address for a client to connect (e.g. 1660944385)
590     * If addr is not NULL, it should point to the listening socket's address.
591     * Otherwise, this function will try to figure out the public address of the
592     * machine, using listen_ip and natip.
593     * The result is a string useable for DCC requests
594 simple 1.1 */
595 pseudo 1.2 int getdccaddr(sockname_t *addr, char *s, size_t l)
596 simple 1.1 {
597 pseudo 1.2 char h[121];
598     sockname_t name, *r = &name;
599 simple 1.1
600 pseudo 1.2 if (addr)
601     r = addr;
602 simple 1.1 else
603 pseudo 1.2 setsockname(r, listen_ip, 0, 1);
604     if (
605     #ifdef IPV6
606     ((r->family == AF_INET6) &&
607     IN6_IS_ADDR_UNSPECIFIED(&r->addr.s6.sin6_addr)) ||
608     #endif
609     (r->family == AF_INET && !r->addr.s4.sin_addr.s_addr)) {
610     /* We can't send :: or 0.0.0.0 for dcc, so try
611     to figure out some real address */
612     r = &name;
613     gethostname(h, sizeof h);
614     setsockname(r, h, 0, 1);
615     }
616    
617     if (
618     #ifdef IPV6
619     ((r->family == AF_INET6) &&
620     IN6_IS_ADDR_UNSPECIFIED(&r->addr.s6.sin6_addr)) ||
621     #endif
622     (!natip[0] && (r->family == AF_INET) && !r->addr.s4.sin_addr.s_addr))
623     return 0;
624    
625     #ifdef IPV6
626     if (r->family == AF_INET6) {
627     if (IN6_IS_ADDR_V4MAPPED(&r->addr.s6.sin6_addr) ||
628     IN6_IS_ADDR_UNSPECIFIED(&r->addr.s6.sin6_addr))
629     snprintf(s, l, "%lu", natip[0] ? iptolong(inet_addr(natip)) :
630     ntohl((IP) &r->addr.s6.sin6_addr.s6_addr[12]));
631     else
632     inet_ntop(AF_INET6, &r->addr.s6.sin6_addr, s, l);
633     } else
634     #endif
635     snprintf(s, l, "%lu", natip[0] ? iptolong(inet_addr(natip)) :
636     ntohl(r->addr.s4.sin_addr.s_addr));
637     return 1;
638 simple 1.1 }
639    
640     /* Builds the fd_sets for select(). Eggdrop only cares about readable
641     * sockets, but tcl also cares for writable/exceptions.
642     * preparefdset() can be called by Tcl Threads
643     */
644     int preparefdset(fd_set *fd, sock_list *slist, int slistmax, int tclonly, int tclmask)
645     {
646     int fdtmp, i, foundsocks = 0;
647    
648     FD_ZERO(fd);
649     for (i = 0; i < slistmax; i++) {
650     if (!(slist[i].flags & (SOCK_UNUSED | SOCK_VIRTUAL))) {
651     if ((slist[i].sock == STDOUT) && !backgrd)
652     fdtmp = STDIN;
653     else
654     fdtmp = slist[i].sock;
655     /*
656     * Looks like that having more than a call, in the same
657     * program, to the FD_SET macro, triggers a bug in gcc.
658     * SIGBUS crashing binaries used to be produced on a number
659     * (prolly all?) of 64 bits architectures.
660     * Make your best to avoid to make it happen again.
661     *
662     * ITE
663     */
664     if (slist[i].flags & SOCK_TCL) {
665     if (!(slist[i].handler.tclsock.mask & tclmask))
666     continue;
667     } else if (tclonly)
668     continue;
669     foundsocks = 1;
670     FD_SET(fdtmp, fd);
671     }
672     }
673     return foundsocks;
674     }
675    
676     /* Attempts to read from all sockets in slist (upper array boundary slistmax-1)
677     * fills s with up to 511 bytes if available, and returns the array index
678     * Also calls all handler procs for Tcl sockets
679     * sockread() can be called by Tcl threads
680     *
681     * on EOF: returns -1, with socket in len
682     * on socket error: returns -2
683     * if nothing is ready: returns -3
684     * tcl sockets busy: returns -5
685     */
686     int sockread(char *s, int *len, sock_list *slist, int slistmax, int tclonly)
687     {
688     struct timeval t;
689     fd_set fdr, fdw, fde;
690     int fds, i, x, have_r, have_w, have_e;
691     int grab = 511, tclsock = -1, events = 0;
692     struct threaddata *td = threaddata();
693    
694     fds = getdtablesize();
695     #ifdef FD_SETSIZE
696     if (fds > FD_SETSIZE)
697     fds = FD_SETSIZE; /* Fixes YET ANOTHER freebsd bug!!! */
698     #endif
699    
700     have_r = preparefdset(&fdr, slist, slistmax, tclonly, TCL_READABLE);
701     have_w = preparefdset(&fdw, slist, slistmax, 1, TCL_WRITABLE);
702     have_e = preparefdset(&fde, slist, slistmax, 1, TCL_EXCEPTION);
703    
704     /* select() may modify the timeval argument - copy it */
705     t.tv_sec = td->blocktime.tv_sec;
706     t.tv_usec = td->blocktime.tv_usec;
707    
708     x = select((SELECT_TYPE_ARG1) fds,
709     SELECT_TYPE_ARG234 (have_r ? &fdr : NULL),
710     SELECT_TYPE_ARG234 (have_w ? &fdw : NULL),
711     SELECT_TYPE_ARG234 (have_e ? &fde : NULL),
712     SELECT_TYPE_ARG5 &t);
713     if (x > 0) {
714     /* Something happened */
715     for (i = 0; i < slistmax; i++) {
716     if (!tclonly && ((!(slist[i].flags & (SOCK_UNUSED | SOCK_TCL))) &&
717     ((FD_ISSET(slist[i].sock, &fdr)) ||
718     ((slist[i].sock == STDOUT) && (!backgrd) &&
719     (FD_ISSET(STDIN, &fdr)))))) {
720     if (slist[i].flags & (SOCK_LISTEN | SOCK_CONNECT)) {
721     /* Listening socket -- don't read, just return activity */
722     /* Same for connection attempt */
723     /* (for strong connections, require a read to succeed first) */
724     if (slist[i].flags & SOCK_PROXYWAIT) /* drummer */
725     /* Hang around to get the return code from proxy */
726     grab = 10;
727     else if (!(slist[i].flags & SOCK_STRONGCONN)) {
728     debug1("net: connect! sock %d", slist[i].sock);
729     s[0] = 0;
730     *len = 0;
731     return i;
732     }
733     } else if (slist[i].flags & SOCK_PASS) {
734     s[0] = 0;
735     *len = 0;
736     return i;
737     }
738     errno = 0;
739     if ((slist[i].sock == STDOUT) && !backgrd)
740     x = read(STDIN, s, grab);
741     else
742     x = read(slist[i].sock, s, grab);
743     if (x <= 0) { /* eof */
744     if (errno != EAGAIN) { /* EAGAIN happens when the operation would
745     * block on a non-blocking socket, if the
746     * socket is going to die, it will die later,
747     * otherwise it will connect. */
748     *len = slist[i].sock;
749     slist[i].flags &= ~SOCK_CONNECT;
750     debug1("net: eof!(read) socket %d", slist[i].sock);
751     return -1;
752     } else {
753     debug3("sockread EAGAIN: %d %d (%s)", slist[i].sock, errno,
754     strerror(errno));
755     continue; /* EAGAIN */
756     }
757     }
758     s[x] = 0;
759     *len = x;
760     if (slist[i].flags & SOCK_PROXYWAIT) {
761     debug2("net: socket: %d proxy errno: %d", slist[i].sock, s[1]);
762     slist[i].flags &= ~(SOCK_CONNECT | SOCK_PROXYWAIT);
763     switch (s[1]) {
764     case 90: /* Success */
765     s[0] = 0;
766     *len = 0;
767     return i;
768     case 91: /* Failed */
769     errno = ECONNREFUSED;
770     break;
771     case 92: /* No identd */
772     case 93: /* Identd said wrong username */
773     /* A better error message would be "socks misconfigured"
774     * or "identd not working" but this is simplest.
775     */
776     errno = ENETUNREACH;
777     break;
778     }
779     *len = slist[i].sock;
780     return -1;
781     }
782     return i;
783     } else if (tclsock == -1 && (slist[i].flags & SOCK_TCL)) {
784     events = FD_ISSET(slist[i].sock, &fdr) ? TCL_READABLE : 0;
785     events |= FD_ISSET(slist[i].sock, &fdw) ? TCL_WRITABLE : 0;
786     events |= FD_ISSET(slist[i].sock, &fde) ? TCL_EXCEPTION : 0;
787     events &= slist[i].handler.tclsock.mask;
788     if (events)
789     tclsock = i;
790     }
791     }
792     } else if (x == -1)
793     return -2; /* socket error */
794     else if (!tclonly) {
795     s[0] = 0;
796     *len = 0;
797     }
798     if (tclsock != -1) {
799     (*slist[tclsock].handler.tclsock.proc)(slist[tclsock].handler.tclsock.cd,
800     events);
801     return -5;
802     }
803     return -3;
804     }
805    
806     /* sockgets: buffer and read from sockets
807     *
808     * Attempts to read from all registered sockets for up to one second. if
809     * after one second, no complete data has been received from any of the
810     * sockets, 's' will be empty, 'len' will be 0, and sockgets will return -3.
811     * if there is returnable data received from a socket, the data will be
812     * in 's' (null-terminated if non-binary), the length will be returned
813     * in len, and the socket number will be returned.
814     * normal sockets have their input buffered, and each call to sockgets
815     * will return one line terminated with a '\n'. binary sockets are not
816     * buffered and return whatever coems in as soon as it arrives.
817     * listening sockets will return an empty string when a connection comes in.
818     * connecting sockets will return an empty string on a successful connect,
819     * or EOF on a failed connect.
820     * if an EOF is detected from any of the sockets, that socket number will be
821     * put in len, and -1 will be returned.
822     * the maximum length of the string returned is 512 (including null)
823     *
824     * Returns -4 if we handled something that shouldn't be handled by the
825     * dcc functions. Simply ignore it.
826     * Returns -5 if tcl sockets are busy but not eggdrop sockets.
827     */
828    
829     int sockgets(char *s, int *len)
830     {
831     char xx[514], *p, *px;
832     int ret, i, data = 0;
833    
834     for (i = 0; i < threaddata()->MAXSOCKS; i++) {
835     /* Check for stored-up data waiting to be processed */
836     if (!(socklist[i].flags & (SOCK_UNUSED | SOCK_TCL | SOCK_BUFFER)) &&
837     (socklist[i].handler.sock.inbuf != NULL)) {
838     if (!(socklist[i].flags & SOCK_BINARY)) {
839     /* look for \r too cos windows can't follow RFCs */
840     p = strchr(socklist[i].handler.sock.inbuf, '\n');
841     if (p == NULL)
842     p = strchr(socklist[i].handler.sock.inbuf, '\r');
843     if (p != NULL) {
844     *p = 0;
845     if (strlen(socklist[i].handler.sock.inbuf) > 510)
846     socklist[i].handler.sock.inbuf[510] = 0;
847     strcpy(s, socklist[i].handler.sock.inbuf);
848     px = nmalloc(strlen(p + 1) + 1);
849     strcpy(px, p + 1);
850     nfree(socklist[i].handler.sock.inbuf);
851     if (px[0])
852     socklist[i].handler.sock.inbuf = px;
853     else {
854     nfree(px);
855     socklist[i].handler.sock.inbuf = NULL;
856     }
857     /* Strip CR if this was CR/LF combo */
858     if (s[strlen(s) - 1] == '\r')
859     s[strlen(s) - 1] = 0;
860     *len = strlen(s);
861     return socklist[i].sock;
862     }
863     } else {
864     /* Handling buffered binary data (must have been SOCK_BUFFER before). */
865     if (socklist[i].handler.sock.inbuflen <= 510) {
866     *len = socklist[i].handler.sock.inbuflen;
867     egg_memcpy(s, socklist[i].handler.sock.inbuf, socklist[i].handler.sock.inbuflen);
868     nfree(socklist[i].handler.sock.inbuf);
869     socklist[i].handler.sock.inbuf = NULL;
870     socklist[i].handler.sock.inbuflen = 0;
871     } else {
872     /* Split up into chunks of 510 bytes. */
873     *len = 510;
874     egg_memcpy(s, socklist[i].handler.sock.inbuf, *len);
875     egg_memcpy(socklist[i].handler.sock.inbuf, socklist[i].handler.sock.inbuf + *len, *len);
876     socklist[i].handler.sock.inbuflen -= *len;
877     socklist[i].handler.sock.inbuf = nrealloc(socklist[i].handler.sock.inbuf, socklist[i].handler.sock.inbuflen);
878     }
879     return socklist[i].sock;
880     }
881     }
882     /* Also check any sockets that might have EOF'd during write */
883     if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].flags & SOCK_EOFD)) {
884     s[0] = 0;
885     *len = socklist[i].sock;
886     return -1;
887     }
888     }
889     /* No pent-up data of any worth -- down to business */
890     *len = 0;
891     ret = sockread(xx, len, socklist, threaddata()->MAXSOCKS, 0);
892     if (ret < 0) {
893     s[0] = 0;
894     return ret;
895     }
896     /* Binary, listening and passed on sockets don't get buffered. */
897     if (socklist[ret].flags & SOCK_CONNECT) {
898     if (socklist[ret].flags & SOCK_STRONGCONN) {
899     socklist[ret].flags &= ~SOCK_STRONGCONN;
900     /* Buffer any data that came in, for future read. */
901     socklist[ret].handler.sock.inbuflen = *len;
902     socklist[ret].handler.sock.inbuf = nmalloc(*len + 1);
903     /* It might be binary data. You never know. */
904     egg_memcpy(socklist[ret].handler.sock.inbuf, xx, *len);
905     socklist[ret].handler.sock.inbuf[*len] = 0;
906     }
907     socklist[ret].flags &= ~SOCK_CONNECT;
908     s[0] = 0;
909     return socklist[ret].sock;
910     }
911     if (socklist[ret].flags & SOCK_BINARY) {
912     egg_memcpy(s, xx, *len);
913     return socklist[ret].sock;
914     }
915     if (socklist[ret].flags & (SOCK_LISTEN | SOCK_PASS | SOCK_TCL)) {
916     s[0] = 0; /* for the dcc traffic counters in the mainloop */
917     return socklist[ret].sock;
918     }
919     if (socklist[ret].flags & SOCK_BUFFER) {
920     socklist[ret].handler.sock.inbuf = (char *) nrealloc(socklist[ret].handler.sock.inbuf,
921     socklist[ret].handler.sock.inbuflen + *len + 1);
922     egg_memcpy(socklist[ret].handler.sock.inbuf + socklist[ret].handler.sock.inbuflen, xx, *len);
923     socklist[ret].handler.sock.inbuflen += *len;
924     /* We don't know whether it's binary data. Make sure normal strings
925     * will be handled properly later on too. */
926     socklist[ret].handler.sock.inbuf[socklist[ret].handler.sock.inbuflen] = 0;
927     return -4; /* Ignore this one. */
928     }
929     /* Might be necessary to prepend stored-up data! */
930     if (socklist[ret].handler.sock.inbuf != NULL) {
931     p = socklist[ret].handler.sock.inbuf;
932     socklist[ret].handler.sock.inbuf = nmalloc(strlen(p) + strlen(xx) + 1);
933     strcpy(socklist[ret].handler.sock.inbuf, p);
934     strcat(socklist[ret].handler.sock.inbuf, xx);
935     nfree(p);
936     if (strlen(socklist[ret].handler.sock.inbuf) < 512) {
937     strcpy(xx, socklist[ret].handler.sock.inbuf);
938     nfree(socklist[ret].handler.sock.inbuf);
939     socklist[ret].handler.sock.inbuf = NULL;
940     socklist[ret].handler.sock.inbuflen = 0;
941     } else {
942     p = socklist[ret].handler.sock.inbuf;
943     socklist[ret].handler.sock.inbuflen = strlen(p) - 510;
944     socklist[ret].handler.sock.inbuf = nmalloc(socklist[ret].handler.sock.inbuflen + 1);
945     strcpy(socklist[ret].handler.sock.inbuf, p + 510);
946     *(p + 510) = 0;
947     strcpy(xx, p);
948     nfree(p);
949     /* (leave the rest to be post-pended later) */
950     }
951     }
952     /* Look for EOL marker; if it's there, i have something to show */
953     p = strchr(xx, '\n');
954     if (p == NULL)
955     p = strchr(xx, '\r');
956     if (p != NULL) {
957     *p = 0;
958     strcpy(s, xx);
959     memmove(xx, p + 1, strlen(p + 1) + 1);
960     if (s[strlen(s) - 1] == '\r')
961     s[strlen(s) - 1] = 0;
962     data = 1; /* DCC_CHAT may now need to process a blank line */
963     /* NO! */
964     /* if (!s[0]) strcpy(s," "); */
965     } else {
966     s[0] = 0;
967     if (strlen(xx) >= 510) {
968     /* String is too long, so just insert fake \n */
969     strcpy(s, xx);
970     xx[0] = 0;
971     data = 1;
972     }
973     }
974     *len = strlen(s);
975     /* Anything left that needs to be saved? */
976     if (!xx[0]) {
977     if (data)
978     return socklist[ret].sock;
979     else
980     return -3;
981     }
982     /* Prepend old data back */
983     if (socklist[ret].handler.sock.inbuf != NULL) {
984     p = socklist[ret].handler.sock.inbuf;
985     socklist[ret].handler.sock.inbuflen = strlen(p) + strlen(xx);
986     socklist[ret].handler.sock.inbuf = nmalloc(socklist[ret].handler.sock.inbuflen + 1);
987     strcpy(socklist[ret].handler.sock.inbuf, xx);
988     strcat(socklist[ret].handler.sock.inbuf, p);
989     nfree(p);
990     } else {
991     socklist[ret].handler.sock.inbuflen = strlen(xx);
992     socklist[ret].handler.sock.inbuf = nmalloc(socklist[ret].handler.sock.inbuflen + 1);
993     strcpy(socklist[ret].handler.sock.inbuf, xx);
994     }
995     if (data)
996     return socklist[ret].sock;
997     else
998     return -3;
999     }
1000    
1001     /* Dump something to a socket
1002     *
1003     * NOTE: Do NOT put Contexts in here if you want DEBUG to be meaningful!!
1004     */
1005     void tputs(register int z, char *s, unsigned int len)
1006     {
1007     register int i, x, idx;
1008     char *p;
1009     static int inhere = 0;
1010    
1011     if (z < 0) /* um... HELLO?! sanity check please! */
1012     return;
1013    
1014     if (((z == STDOUT) || (z == STDERR)) && (!backgrd || use_stderr)) {
1015     write(z, s, len);
1016     return;
1017     }
1018    
1019     for (i = 0; i < threaddata()->MAXSOCKS; i++) {
1020     if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == z)) {
1021     for (idx = 0; idx < dcc_total; idx++) {
1022     if ((dcc[idx].sock == z) && dcc[idx].type && dcc[idx].type->name) {
1023     if (!strncmp(dcc[idx].type->name, "BOT", 3))
1024     otraffic_bn_today += len;
1025     else if (!strcmp(dcc[idx].type->name, "SERVER"))
1026     otraffic_irc_today += len;
1027     else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
1028     otraffic_dcc_today += len;
1029     else if (!strncmp(dcc[idx].type->name, "FILES", 5))
1030     otraffic_filesys_today += len;
1031     else if (!strcmp(dcc[idx].type->name, "SEND"))
1032     otraffic_trans_today += len;
1033     else if (!strncmp(dcc[idx].type->name, "GET", 3))
1034     otraffic_trans_today += len;
1035     else
1036     otraffic_unknown_today += len;
1037     break;
1038     }
1039     }
1040    
1041     if (socklist[i].handler.sock.outbuf != NULL) {
1042     /* Already queueing: just add it */
1043     p = (char *) nrealloc(socklist[i].handler.sock.outbuf, socklist[i].handler.sock.outbuflen + len);
1044     egg_memcpy(p + socklist[i].handler.sock.outbuflen, s, len);
1045     socklist[i].handler.sock.outbuf = p;
1046     socklist[i].handler.sock.outbuflen += len;
1047     return;
1048     }
1049     /* Try. */
1050     x = write(z, s, len);
1051     if (x == -1)
1052     x = 0;
1053     if (x < len) {
1054     /* Socket is full, queue it */
1055     socklist[i].handler.sock.outbuf = nmalloc(len - x);
1056     egg_memcpy(socklist[i].handler.sock.outbuf, &s[x], len - x);
1057     socklist[i].handler.sock.outbuflen = len - x;
1058     }
1059     return;
1060     }
1061     }
1062     /* Make sure we don't cause a crash by looping here */
1063     if (!inhere) {
1064     inhere = 1;
1065    
1066     putlog(LOG_MISC, "*", "!!! writing to nonexistent socket: %d", z);
1067     s[strlen(s) - 1] = 0;
1068     putlog(LOG_MISC, "*", "!-> '%s'", s);
1069    
1070     inhere = 0;
1071     }
1072     }
1073    
1074     /* tputs might queue data for sockets, let's dump as much of it as
1075     * possible.
1076     */
1077     void dequeue_sockets()
1078     {
1079     int i, x;
1080    
1081     int z = 0, fds;
1082     fd_set wfds;
1083     struct timeval tv;
1084    
1085     /* ^-- start poptix test code, this should avoid writes to sockets not ready to be written to. */
1086     fds = getdtablesize();
1087    
1088     #ifdef FD_SETSIZE
1089     if (fds > FD_SETSIZE)
1090     fds = FD_SETSIZE; /* Fixes YET ANOTHER freebsd bug!!! */
1091     #endif
1092     FD_ZERO(&wfds);
1093     tv.tv_sec = 0;
1094     tv.tv_usec = 0; /* we only want to see if it's ready for writing, no need to actually wait.. */
1095     for (i = 0; i < threaddata()->MAXSOCKS; i++) {
1096     if (!(socklist[i].flags & (SOCK_UNUSED | SOCK_TCL)) &&
1097     socklist[i].handler.sock.outbuf != NULL) {
1098     FD_SET(socklist[i].sock, &wfds);
1099     z = 1;
1100     }
1101     }
1102     if (!z)
1103     return; /* nothing to write */
1104    
1105     select((SELECT_TYPE_ARG1) fds, SELECT_TYPE_ARG234 NULL,
1106     SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 NULL,
1107     SELECT_TYPE_ARG5 &tv);
1108    
1109     /* end poptix */
1110    
1111     for (i = 0; i < threaddata()->MAXSOCKS; i++) {
1112     if (!(socklist[i].flags & (SOCK_UNUSED | SOCK_TCL)) &&
1113     (socklist[i].handler.sock.outbuf != NULL) && (FD_ISSET(socklist[i].sock, &wfds))) {
1114     /* Trick tputs into doing the work */
1115     errno = 0;
1116     x = write(socklist[i].sock, socklist[i].handler.sock.outbuf, socklist[i].handler.sock.outbuflen);
1117     if ((x < 0) && (errno != EAGAIN)
1118     #ifdef EBADSLT
1119     && (errno != EBADSLT)
1120     #endif
1121     #ifdef ENOTCONN
1122     && (errno != ENOTCONN)
1123     #endif
1124     ) {
1125     /* This detects an EOF during writing */
1126     debug3("net: eof!(write) socket %d (%s,%d)", socklist[i].sock,
1127     strerror(errno), errno);
1128     socklist[i].flags |= SOCK_EOFD;
1129     } else if (x == socklist[i].handler.sock.outbuflen) {
1130     /* If the whole buffer was sent, nuke it */
1131     nfree(socklist[i].handler.sock.outbuf);
1132     socklist[i].handler.sock.outbuf = NULL;
1133     socklist[i].handler.sock.outbuflen = 0;
1134     } else if (x > 0) {
1135     char *p = socklist[i].handler.sock.outbuf;
1136    
1137     /* This removes any sent bytes from the beginning of the buffer */
1138     socklist[i].handler.sock.outbuf = nmalloc(socklist[i].handler.sock.outbuflen - x);
1139     egg_memcpy(socklist[i].handler.sock.outbuf, p + x, socklist[i].handler.sock.outbuflen - x);
1140     socklist[i].handler.sock.outbuflen -= x;
1141     nfree(p);
1142     } else {
1143     debug3("dequeue_sockets(): errno = %d (%s) on %d", errno,
1144     strerror(errno), socklist[i].sock);
1145     }
1146     /* All queued data was sent. Call handler if one exists and the
1147     * dcc entry wants it.
1148     */
1149     if (!socklist[i].handler.sock.outbuf) {
1150     int idx = findanyidx(socklist[i].sock);
1151    
1152     if (idx > 0 && dcc[idx].type && dcc[idx].type->outdone)
1153     dcc[idx].type->outdone(idx);
1154     }
1155     }
1156     }
1157     }
1158    
1159    
1160     /*
1161     * Debugging stuff
1162     */
1163    
1164     void tell_netdebug(int idx)
1165     {
1166     int i;
1167     char s[80];
1168    
1169     dprintf(idx, "Open sockets:");
1170     for (i = 0; i < threaddata()->MAXSOCKS; i++) {
1171     if (!(socklist[i].flags & SOCK_UNUSED)) {
1172     sprintf(s, " %d", socklist[i].sock);
1173     if (socklist[i].flags & SOCK_BINARY)
1174     strcat(s, " (binary)");
1175     if (socklist[i].flags & SOCK_LISTEN)
1176     strcat(s, " (listen)");
1177     if (socklist[i].flags & SOCK_PASS)
1178     strcat(s, " (passed on)");
1179     if (socklist[i].flags & SOCK_CONNECT)
1180     strcat(s, " (connecting)");
1181     if (socklist[i].flags & SOCK_STRONGCONN)
1182     strcat(s, " (strong)");
1183     if (socklist[i].flags & SOCK_NONSOCK)
1184     strcat(s, " (file)");
1185     if (socklist[i].flags & SOCK_TCL)
1186     strcat(s, " (tcl)");
1187     if (!(socklist[i].flags & SOCK_TCL)) {
1188     if (socklist[i].handler.sock.inbuf != NULL)
1189     sprintf(&s[strlen(s)], " (inbuf: %04X)",
1190     (unsigned int) strlen(socklist[i].handler.sock.inbuf));
1191     if (socklist[i].handler.sock.outbuf != NULL)
1192     sprintf(&s[strlen(s)], " (outbuf: %06lX)", socklist[i].handler.sock.outbuflen);
1193     }
1194     strcat(s, ",");
1195     dprintf(idx, "%s", s);
1196     }
1197     }
1198     dprintf(idx, " done.\n");
1199     }
1200    
1201     /* Security-flavoured sanity checking on DCC connections of all sorts can be
1202     * done with this routine. Feed it the proper information from your DCC
1203     * before you attempt the connection, and this will make an attempt at
1204     * figuring out if the connection is really that person, or someone screwing
1205     * around. It's not foolproof, but anything that fails this check probably
1206     * isn't going to work anyway due to masquerading firewalls, NAT routers,
1207     * or bugs in mIRC.
1208     */
1209     int sanitycheck_dcc(char *nick, char *from, char *ipaddy, char *port)
1210     {
1211     /* According to the latest RFC, the clients SHOULD be able to handle
1212     * DNS names that are up to 255 characters long. This is not broken.
1213     */
1214    
1215 pseudo 1.2 #ifdef IPV6
1216     char badaddress[INET6_ADDRSTRLEN];
1217     sockname_t name;
1218     IP ip = 0;
1219     #else
1220     char badaddress[sizeof "255.255.255.255"];
1221 simple 1.1 IP ip = my_atoul(ipaddy);
1222 pseudo 1.2 #endif
1223 simple 1.1 int prt = atoi(port);
1224    
1225     /* It is disabled HERE so we only have to check in *one* spot! */
1226     if (!dcc_sanitycheck)
1227     return 1;
1228    
1229     if (prt < 1) {
1230     putlog(LOG_MISC, "*", "ALERT: (%s!%s) specified an impossible port of %u!",
1231     nick, from, prt);
1232     return 0;
1233     }
1234 pseudo 1.2 #ifdef IPV6
1235     if (strchr(ipaddy, ':')) {
1236     if (inet_pton(AF_INET6, ipaddy, &name.addr.s6.sin6_addr) != 1) {
1237     putlog(LOG_MISC, "*", "ALERT: (%s!%s) specified an invalid IPv6 "
1238     "address of %s!", nick, from, ipaddy);
1239     return 0;
1240     }
1241     if (IN6_IS_ADDR_V4MAPPED(&name.addr.s6.sin6_addr))
1242     ip = ntohl(*(IP *) &name.addr.s6.sin6_addr.s6_addr[12]);
1243     }
1244     #endif
1245     if (ip && inet_ntop(AF_INET, &ip, badaddress, sizeof badaddress) &&
1246     (ip < (1 << 24))) {
1247 simple 1.1 putlog(LOG_MISC, "*", "ALERT: (%s!%s) specified an impossible IP of %s!",
1248     nick, from, badaddress);
1249     return 0;
1250     }
1251     return 1;
1252     }
1253    
1254 pseudo 1.2 int hostsanitycheck_dcc(char *nick, char *from, sockname_t *ip, char *dnsname,
1255 simple 1.1 char *prt)
1256     {
1257 pseudo 1.2 char badaddress[INET6_ADDRSTRLEN];
1258    
1259 simple 1.1 /* According to the latest RFC, the clients SHOULD be able to handle
1260     * DNS names that are up to 255 characters long. This is not broken.
1261     */
1262 pseudo 1.2 char hostn[256];
1263 simple 1.1
1264     /* It is disabled HERE so we only have to check in *one* spot! */
1265     if (!dcc_sanitycheck)
1266     return 1;
1267 pseudo 1.2 strcpy(badaddress, iptostr(&ip->addr.sa));
1268 simple 1.1 /* These should pad like crazy with zeros, since 120 bytes or so is
1269     * where the routines providing our data currently lose interest. I'm
1270     * using the n-variant in case someone changes that...
1271     */
1272     strncpyz(hostn, extracthostname(from), sizeof hostn);
1273     if (!egg_strcasecmp(hostn, dnsname)) {
1274     putlog(LOG_DEBUG, "*", "DNS information for submitted IP checks out.");
1275     return 1;
1276     }
1277     if (!strcmp(badaddress, dnsname))
1278     putlog(LOG_MISC, "*", "ALERT: (%s!%s) sent a DCC request with bogus IP "
1279     "information of %s port %s. %s does not resolve to %s!", nick, from,
1280     badaddress, prt, from, badaddress);
1281     else
1282     return 1; /* <- usually happens when we have
1283     * a user with an unresolved hostmask! */
1284     return 0;
1285     }
1286    
1287     /* Checks wether the referenced socket has data queued.
1288     *
1289     * Returns true if the incoming/outgoing (depending on 'type') queues
1290     * contain data, otherwise false.
1291     */
1292     int sock_has_data(int type, int sock)
1293     {
1294     int ret = 0, i;
1295    
1296     for (i = 0; i < threaddata()->MAXSOCKS; i++)
1297     if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
1298     break;
1299     if (i < threaddata()->MAXSOCKS) {
1300     switch (type) {
1301     case SOCK_DATA_OUTGOING:
1302     ret = (socklist[i].handler.sock.outbuf != NULL);
1303     break;
1304     case SOCK_DATA_INCOMING:
1305     ret = (socklist[i].handler.sock.inbuf != NULL);
1306     break;
1307     }
1308     } else
1309     debug1("sock_has_data: could not find socket #%d, returning false.", sock);
1310     return ret;
1311     }
1312    
1313     /* flush_inbuf():
1314     * checks if there's data in the incoming buffer of an connection
1315     * and flushs the buffer if possible
1316     *
1317     * returns: -1 if the dcc entry wasn't found
1318     * -2 if dcc[idx].type->activity doesn't exist and the data couldn't
1319     * be handled
1320     * 0 if buffer was empty
1321     * otherwise length of flushed buffer
1322     */
1323     int flush_inbuf(int idx)
1324     {
1325     int i, len;
1326     char *inbuf;
1327    
1328     Assert((idx >= 0) && (idx < dcc_total));
1329     for (i = 0; i < threaddata()->MAXSOCKS; i++) {
1330     if ((dcc[idx].sock == socklist[i].sock) &&
1331     !(socklist[i].flags & SOCK_UNUSED)) {
1332     len = socklist[i].handler.sock.inbuflen;
1333     if ((len > 0) && socklist[i].handler.sock.inbuf) {
1334     if (dcc[idx].type && dcc[idx].type->activity) {
1335     inbuf = socklist[i].handler.sock.inbuf;
1336     socklist[i].handler.sock.inbuf = NULL;
1337     dcc[idx].type->activity(idx, inbuf, len);
1338     nfree(inbuf);
1339     return len;
1340     } else
1341     return -2;
1342     } else
1343     return 0;
1344     }
1345     }
1346     return -1;
1347     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23