/[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.10 - (hide annotations) (download) (as text)
Sun Dec 19 16:06:34 1999 UTC (19 years, 7 months ago) by fabian
Branch: MAIN
Changes since 1.9: +41 -1 lines
File MIME type: text/x-chdr
G`Quann's traffic patch

1 segfault 1.1 /*
2     * net.c -- handles:
3     * all raw network i/o
4     *
5     * This is hereby released into the public domain.
6     * Robey Pointer, robey@netcom.com
7     */
8    
9     #include "main.h"
10     #include <limits.h>
11     #include <string.h>
12     #include <netdb.h>
13     #include <sys/socket.h>
14     #if HAVE_SYS_SELECT_H
15     #include <sys/select.h>
16     #endif
17     #include <netinet/in.h>
18     #include <arpa/inet.h> /* is this really necessary? */
19     #include <errno.h>
20     #if HAVE_UNISTD_H
21     #include <unistd.h>
22     #endif
23     #include <fcntl.h>
24     #include <setjmp.h>
25    
26     #if !HAVE_GETDTABLESIZE
27     #ifdef FD_SETSIZE
28     #define getdtablesize() FD_SETSIZE
29     #else
30     #define getdtablesize() 200
31     #endif
32     #endif
33    
34     extern int backgrd;
35     extern int use_stderr;
36     extern int resolve_timeout;
37 fabian 1.10 extern int dcc_total;
38     extern struct dcc_t *dcc;
39 segfault 1.1
40     char hostname[121] = ""; /* hostname can be specified in the config
41     * file */
42     char myip[121] = ""; /* IP can be specified in the config file */
43     char firewall[121] = ""; /* socks server for firewall */
44     int firewallport = 1080; /* default port of Sock4/5 firewalls */
45     char botuser[21] = "eggdrop"; /* username of the user running the bot */
46     int dcc_sanitycheck = 0; /* we should do some sanity checking on dcc
47     * connections. */
48     sock_list *socklist = 0; /* enough to be safe */
49     int MAXSOCKS = 0;
50 fabian 1.10 extern int otraffic_irc_today;
51     extern int otraffic_bn_today;
52     extern int otraffic_dcc_today;
53     extern int otraffic_filesys_today;
54     extern int otraffic_trans_today;
55     extern int otraffic_unknown_today;
56 segfault 1.1
57     /* types of proxy */
58     #define PROXY_SOCKS 1
59     #define PROXY_SUN 2
60    
61     jmp_buf alarmret; /* env buffer for alarm() returns */
62    
63     /* i need an UNSIGNED long for dcc type stuff */
64     IP my_atoul(char *s)
65     {
66     IP ret = 0;
67    
68     while ((*s >= '0') && (*s <= '9')) {
69     ret *= 10;
70     ret += ((*s) - '0');
71     s++;
72     }
73     return ret;
74     }
75    
76     /* i read somewhere that memcpy() is broken on some machines */
77     /* it's easy to replace, so i'm not gonna take any chances, because
78     * it's pretty important that it work correctly here */
79     void my_memcpy(char *dest, char *src, int len)
80     {
81     while (len--)
82     *dest++ = *src++;
83     }
84    
85     #ifndef HAVE_BZERO
86     /* bzero() is bsd-only, so here's one for non-bsd systems */
87     void bzero(char *dest, int len)
88     {
89     while (len--)
90     *dest++ = 0;
91     }
92     #endif
93    
94     /* initialize the socklist */
95     void init_net()
96     {
97     int i;
98    
99     for (i = 0; i < MAXSOCKS; i++) {
100     socklist[i].flags = SOCK_UNUSED;
101     }
102     }
103    
104     int expmem_net()
105     {
106     int i, tot = 0;
107    
108 fabian 1.9 Context;
109 segfault 1.1 for (i = 0; i < MAXSOCKS; i++) {
110     if (!(socklist[i].flags & SOCK_UNUSED)) {
111     if (socklist[i].inbuf != NULL)
112     tot += strlen(socklist[i].inbuf) + 1;
113     if (socklist[i].outbuf != NULL)
114     tot += socklist[i].outbuflen;
115     }
116     }
117     return tot;
118     }
119    
120     /* get my ip number */
121     IP getmyip()
122     {
123     struct hostent *hp;
124     char s[121];
125     IP ip;
126     struct in_addr *in;
127    
128     /* could be pre-defined */
129     if (myip[0]) {
130     if ((myip[strlen(myip) - 1] >= '0') && (myip[strlen(myip) - 1] <= '9'))
131     return (IP) inet_addr(myip);
132     }
133     /* also could be pre-defined */
134     if (hostname[0])
135     hp = gethostbyname(hostname);
136     else {
137     gethostname(s, 120);
138     hp = gethostbyname(s);
139     }
140     if (hp == NULL)
141     fatal("Hostname self-lookup failed.", 0);
142     in = (struct in_addr *) (hp->h_addr_list[0]);
143     ip = (IP) (in->s_addr);
144     return ip;
145     }
146    
147     void neterror(char *s)
148     {
149     switch (errno) {
150     case EADDRINUSE:
151     strcpy(s, "Address already in use");
152     break;
153     case EADDRNOTAVAIL:
154     strcpy(s, "Address invalid on remote machine");
155     break;
156     case EAFNOSUPPORT:
157     strcpy(s, "Address family not supported");
158     break;
159     case EALREADY:
160     strcpy(s, "Socket already in use");
161     break;
162     case EBADF:
163     strcpy(s, "Socket descriptor is bad");
164     break;
165     case ECONNREFUSED:
166     strcpy(s, "Connection refused");
167     break;
168     case EFAULT:
169     strcpy(s, "Namespace segment violation");
170     break;
171     case EINPROGRESS:
172     strcpy(s, "Operation in progress");
173     break;
174     case EINTR:
175     strcpy(s, "Timeout");
176     break;
177     case EINVAL:
178     strcpy(s, "Invalid namespace");
179     break;
180     case EISCONN:
181     strcpy(s, "Socket already connected");
182     break;
183     case ENETUNREACH:
184     strcpy(s, "Network unreachable");
185     break;
186     case ENOTSOCK:
187     strcpy(s, "File descriptor, not a socket");
188     break;
189     case ETIMEDOUT:
190     strcpy(s, "Connection timed out");
191     break;
192     case ENOTCONN:
193     strcpy(s, "Socket is not connected");
194     break;
195     case EHOSTUNREACH:
196     strcpy(s, "Host is unreachable");
197     break;
198     case EPIPE:
199     strcpy(s, "Broken pipe");
200     break;
201     #ifdef ECONNRESET
202     case ECONNRESET:
203     strcpy(s, "Connection reset by peer");
204     break;
205     #endif
206     #ifdef EACCES
207     case EACCES:
208     strcpy(s, "Permission denied");
209     break;
210     #endif
211     case 0:
212     strcpy(s, "Error 0");
213     break;
214     default:
215     sprintf(s, "Unforseen error %d", errno);
216     break;
217     }
218     }
219    
220 fabian 1.3 /* return a free entry in the socket entry */
221     int allocsock(int sock, int options)
222 segfault 1.1 {
223     int i;
224    
225 fabian 1.9 Context;
226 segfault 1.1 for (i = 0; i < MAXSOCKS; i++) {
227     if (socklist[i].flags & SOCK_UNUSED) {
228     /* yay! there is table space */
229     socklist[i].inbuf = socklist[i].outbuf = NULL;
230     socklist[i].outbuflen = 0;
231     socklist[i].flags = options;
232     socklist[i].sock = sock;
233 fabian 1.3 return i;
234     }
235     }
236     fatal("Socket table is full!", 0);
237     return -1; /* never reached */
238     }
239    
240     /* request a normal socket for i/o */
241     void setsock(int sock, int options)
242     {
243     int i = allocsock(sock, options);
244     int parm;
245    
246 segfault 1.1 if (((sock != STDOUT) || backgrd) &&
247     !(socklist[i].flags & SOCK_NONSOCK)) {
248     parm = 1;
249 fabian 1.3 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *) &parm, sizeof(int));
250 segfault 1.1
251     parm = 0;
252     setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &parm, sizeof(int));
253     }
254     if (options & SOCK_LISTEN) {
255     /* Tris says this lets us grab the same port again next time */
256     parm = 1;
257     setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *) &parm,
258     sizeof(int));
259     }
260     /* yay async i/o ! */
261     fcntl(sock, F_SETFL, O_NONBLOCK);
262     }
263    
264     int getsock(int options)
265     {
266     int sock = socket(AF_INET, SOCK_STREAM, 0);
267    
268     if (sock < 0)
269     fatal("Can't open a socket at all!", 0);
270     setsock(sock, options);
271     return sock;
272     }
273    
274     /* done with a socket */
275     void killsock(int sock)
276     {
277     int i;
278    
279     for (i = 0; i < MAXSOCKS; i++) {
280 fabian 1.2 if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED)) {
281 segfault 1.1 close(socklist[i].sock);
282     if (socklist[i].inbuf != NULL) {
283     nfree(socklist[i].inbuf);
284     socklist[i].inbuf = NULL;
285     }
286     if (socklist[i].outbuf != NULL) {
287     nfree(socklist[i].outbuf);
288     socklist[i].outbuf = NULL;
289     socklist[i].outbuflen = 0;
290     }
291     socklist[i].flags = SOCK_UNUSED;
292     return;
293     }
294     }
295     putlog(LOG_MISC, "*", "Attempt to kill un-allocated socket %d !!", sock);
296     }
297    
298     /* send connection request to proxy */
299     static int proxy_connect(int sock, char *host, int port, int proxy)
300     {
301     unsigned char x[10];
302     struct hostent *hp;
303     char s[30];
304     int i;
305    
306     /* socks proxy */
307     if (proxy == PROXY_SOCKS) {
308     /* numeric IP? */
309     if ((host[strlen(host) - 1] >= '0') && (host[strlen(host) - 1] <= '9')) {
310     IP ip = ((IP) inet_addr(host)); /* drummer */
311     my_memcpy((char *) x, (char *) &ip, 4); /* Beige@Efnet */
312     } else {
313     /* no, must be host.domain */
314     if (!setjmp(alarmret)) {
315     alarm(resolve_timeout);
316     hp = gethostbyname(host);
317     alarm(0);
318     } else {
319     hp = NULL;
320     }
321     if (hp == NULL) {
322     killsock(sock);
323     return -2;
324     }
325     my_memcpy((char *) x, (char *) hp->h_addr, hp->h_length);
326     }
327     for (i = 0; i < MAXSOCKS; i++) {
328     if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == sock))
329     socklist[i].flags |= SOCK_PROXYWAIT; /* drummer */
330     }
331     sprintf(s, "\004\001%c%c%c%c%c%c%s", (port >> 8) % 256, (port % 256),
332     x[0], x[1], x[2], x[3], botuser);
333     tputs(sock, s, strlen(botuser) + 9); /* drummer */
334     } else if (proxy == PROXY_SUN) {
335     sprintf(s, "%s %d\n", host, port);
336     tputs(sock, s, strlen(s)); /* drummer */
337     }
338     return sock;
339     }
340    
341     /* starts a connection attempt to a socket
342 fabian 1.3 *
343     * If given a normal hostname, this will be resolved to the corresponding
344     * IP address first. PLEASE try to use the non-blocking dns functions
345     * instead and then call this function with the IP address to avoid blocking.
346     *
347 segfault 1.1 * returns <0 if connection refused:
348     * -1 neterror() type error
349     * -2 can't resolve hostname */
350     int open_telnet_raw(int sock, char *server, int sport)
351     {
352     struct sockaddr_in name;
353     struct hostent *hp;
354     char host[121];
355     int i, port;
356     volatile int proxy;
357    
358     /* firewall? use socks */
359     if (firewall[0]) {
360     if (firewall[0] == '!') {
361     proxy = PROXY_SUN;
362     strcpy(host, &firewall[1]);
363     } else {
364     proxy = PROXY_SOCKS;
365     strcpy(host, firewall);
366     }
367     port = firewallport;
368     } else {
369     proxy = 0;
370     strcpy(host, server);
371     port = sport;
372     }
373     /* patch by tris for multi-hosted machines: */
374     bzero((char *) &name, sizeof(struct sockaddr_in));
375    
376     name.sin_family = AF_INET;
377     name.sin_addr.s_addr = (myip[0] ? getmyip() : INADDR_ANY);
378 fabian 1.4 if (bind(sock, (struct sockaddr *) &name, sizeof(name)) < 0)
379 segfault 1.1 return -1;
380     bzero((char *) &name, sizeof(struct sockaddr_in));
381    
382     name.sin_family = AF_INET;
383     name.sin_port = my_htons(port);
384     /* numeric IP? */
385     if ((host[strlen(host) - 1] >= '0') && (host[strlen(host) - 1] <= '9'))
386     name.sin_addr.s_addr = inet_addr(host);
387     else {
388     /* no, must be host.domain */
389 fabian 1.3 debug0("Warning: open_telnet_raw() is about to block in gethostbyname()!");
390 segfault 1.1 if (!setjmp(alarmret)) {
391     alarm(resolve_timeout);
392     hp = gethostbyname(host);
393     alarm(0);
394     } else {
395     hp = NULL;
396     }
397 fabian 1.4 if (hp == NULL)
398 segfault 1.1 return -2;
399     my_memcpy((char *) &name.sin_addr, hp->h_addr, hp->h_length);
400     name.sin_family = hp->h_addrtype;
401     }
402     for (i = 0; i < MAXSOCKS; i++) {
403     if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == sock))
404     socklist[i].flags |= SOCK_CONNECT;
405     }
406     if (connect(sock, (struct sockaddr *) &name,
407     sizeof(struct sockaddr_in)) < 0) {
408     if (errno == EINPROGRESS) {
409     /* firewall? announce connect attempt to proxy */
410     if (firewall[0])
411     return proxy_connect(sock, server, sport, proxy);
412     return sock; /* async success! */
413 fabian 1.4 } else
414 segfault 1.1 return -1;
415     }
416     /* synchronous? :/ */
417     if (firewall[0])
418     return proxy_connect(sock, server, sport, proxy);
419     return sock;
420     }
421    
422     /* ordinary non-binary connection attempt */
423     int open_telnet(char *server, int port)
424     {
425 fabian 1.4 int sock = getsock(0),
426     ret = open_telnet_raw(sock, server, port);
427    
428     if (ret < 0)
429     killsock(sock);
430     return ret;
431 segfault 1.1 }
432    
433     /* returns a socket number for a listening socket that will accept any
434     * connection -- port # is returned in port */
435     int open_listen(int *port)
436     {
437     int sock, addrlen;
438     struct sockaddr_in name;
439    
440     if (firewall[0]) {
441     /* FIXME: can't do listen port thru firewall yet */
442 fabian 1.6 putlog(LOG_MISC, "*", "!! Cant open a listen port (you are using a firewall)");
443 segfault 1.1 return -1;
444     }
445     sock = getsock(SOCK_LISTEN);
446     bzero((char *) &name, sizeof(struct sockaddr_in));
447    
448     name.sin_family = AF_INET;
449     name.sin_port = my_htons(*port); /* 0 = just assign us a port */
450     name.sin_addr.s_addr = (myip[0] ? getmyip() : INADDR_ANY);
451     if (bind(sock, (struct sockaddr *) &name, sizeof(name)) < 0) {
452     killsock(sock);
453     return -1;
454     }
455     /* what port are we on? */
456     addrlen = sizeof(name);
457     if (getsockname(sock, (struct sockaddr *) &name, &addrlen) < 0) {
458     killsock(sock);
459     return -1;
460     }
461     *port = my_ntohs(name.sin_port);
462     if (listen(sock, 1) < 0) {
463     killsock(sock);
464     return -1;
465     }
466     return sock;
467     }
468    
469 fabian 1.3 /* Given a network-style IP address, returns the hostname. The hostname
470     * will be in the "##.##.##.##" format if there was an error.
471     *
472     * NOTE: This function is depreciated. Try using the async dns approach
473     * instead. */
474 segfault 1.1 char *hostnamefromip(unsigned long ip)
475     {
476     struct hostent *hp;
477     unsigned long addr = ip;
478     unsigned char *p;
479 fabian 1.3 static char s[UHOSTLEN];
480 segfault 1.1
481     if (!setjmp(alarmret)) {
482     alarm(resolve_timeout);
483     hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
484     alarm(0);
485     } else {
486     hp = NULL;
487     }
488     if (hp == NULL) {
489     p = (unsigned char *) &addr;
490     sprintf(s, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
491     return s;
492     }
493 fabian 1.3 strncpy(s, hp->h_name, UHOSTLEN - 1);
494     s[UHOSTLEN - 1] = 0;
495 segfault 1.1 return s;
496     }
497    
498 fabian 1.5 /* Returns the given network byte order IP address in the
499     * dotted format - "##.##.##.##" */
500 fabian 1.3 char *iptostr(IP ip)
501     {
502     struct in_addr a;
503    
504     a.s_addr = ip;
505     return inet_ntoa(a);
506     }
507    
508 segfault 1.1 /* short routine to answer a connect received on a socket made previously
509     * by open_listen ... returns hostname of the caller & the new socket
510     * does NOT dispose of old "public" socket! */
511     int answer(int sock, char *caller, unsigned long *ip,
512     unsigned short *port, int binary)
513     {
514     int new_sock, addrlen;
515     struct sockaddr_in from;
516 fabian 1.8 addrlen = sizeof(struct sockaddr);
517 fabian 1.3
518 segfault 1.1 new_sock = accept(sock, (struct sockaddr *) &from, &addrlen);
519     if (new_sock < 0)
520     return -1;
521     if (ip != NULL) {
522     *ip = from.sin_addr.s_addr;
523 fabian 1.3 /* This is now done asynchronously. Instead, we just provide the
524     * IP address.
525     * strncpy(caller, hostnamefromip(*ip), 120);
526     */
527     strncpy(caller, iptostr(*ip), 120);
528 segfault 1.1 caller[120] = 0;
529     *ip = my_ntohl(*ip);
530     }
531     if (port != NULL)
532     *port = my_ntohs(from.sin_port);
533     /* set up all the normal socket crap */
534     setsock(new_sock, (binary ? SOCK_BINARY : 0));
535     return new_sock;
536     }
537    
538     /* like open_telnet, but uses server & port specifications of dcc */
539     int open_telnet_dcc(int sock, char *server, char *port)
540     {
541     int p;
542     unsigned long addr;
543     char sv[121];
544     unsigned char c[4];
545    
546     if (port != NULL)
547     p = atoi(port);
548     else
549     p = 2000;
550     if (server != NULL)
551     addr = my_atoul(server);
552     else
553     addr = 0L;
554     if (addr < (1 << 24))
555     return -3; /* fake address */
556     c[0] = (addr >> 24) & 0xff;
557     c[1] = (addr >> 16) & 0xff;
558     c[2] = (addr >> 8) & 0xff;
559     c[3] = addr & 0xff;
560     sprintf(sv, "%u.%u.%u.%u", c[0], c[1], c[2], c[3]);
561     /* strcpy(sv,hostnamefromip(addr)); */
562     p = open_telnet_raw(sock, sv, p);
563     return p;
564     }
565    
566     /* all new replacements for mtgets/mtread */
567    
568     /* attempts to read from all the sockets in socklist
569     * fills s with up to 511 bytes if available, and returns the array index
570     * on EOF, returns -1, with socket in len
571     * on socket error, returns -2
572     * if nothing is ready, returns -3 */
573     static int sockread(char *s, int *len)
574     {
575     fd_set fd;
576     int fds, i, x;
577     struct timeval t;
578     int grab = 511;
579    
580     fds = getdtablesize();
581     #ifdef FD_SETSIZE
582     if (fds > FD_SETSIZE)
583     fds = FD_SETSIZE; /* fixes YET ANOTHER freebsd bug!!! */
584     #endif
585     /* timeout: 1 sec */
586     t.tv_sec = 1;
587     t.tv_usec = 0;
588     FD_ZERO(&fd);
589     for (i = 0; i < MAXSOCKS; i++)
590     if (!(socklist[i].flags & SOCK_UNUSED)) {
591     if ((socklist[i].sock == STDOUT) && !backgrd)
592     FD_SET(STDIN, &fd);
593     else
594     FD_SET(socklist[i].sock, &fd);
595     }
596     #ifdef HPUX_HACKS
597     #ifndef HPUX10_HACKS
598     x = select(fds, (int *) &fd, (int *) NULL, (int *) NULL, &t);
599     #else
600     x = select(fds, &fd, NULL, NULL, &t);
601     #endif
602     #else
603     x = select(fds, &fd, NULL, NULL, &t);
604     #endif
605     if (x > 0) {
606     /* something happened */
607     for (i = 0; i < MAXSOCKS; i++) {
608     if ((!(socklist[i].flags & SOCK_UNUSED)) &&
609     ((FD_ISSET(socklist[i].sock, &fd)) ||
610     ((socklist[i].sock == STDOUT) && (!backgrd) &&
611     (FD_ISSET(STDIN, &fd))))) {
612     if (socklist[i].flags & (SOCK_LISTEN | SOCK_CONNECT)) {
613     /* listening socket -- don't read, just return activity */
614     /* same for connection attempt */
615     /* (for strong connections, require a read to succeed first) */
616     if (socklist[i].flags & SOCK_PROXYWAIT) { /* drummer */
617     /* hang around to get the return code from proxy */
618     grab = 10;
619     } else if (!(socklist[i].flags & SOCK_STRONGCONN)) {
620     debug1("net: connect! sock %d", socklist[i].sock);
621     s[0] = 0;
622     *len = 0;
623     return i;
624     }
625 fabian 1.3 } else if (socklist[i].flags & SOCK_PASS) {
626     s[0] = 0;
627     *len = 0;
628     return i;
629 segfault 1.1 }
630     if ((socklist[i].sock == STDOUT) && !backgrd)
631     x = read(STDIN, s, grab);
632     else
633     x = read(socklist[i].sock, s, grab);
634     if (x <= 0) { /* eof */
635     if (x == EAGAIN) {
636     s[0] = 0;
637     *len = 0;
638     return -3;
639     }
640     *len = socklist[i].sock;
641     socklist[i].flags &= ~SOCK_CONNECT;
642     debug1("net: eof!(read) socket %d", socklist[i].sock);
643     return -1;
644     }
645     s[x] = 0;
646     *len = x;
647     if (socklist[i].flags & SOCK_PROXYWAIT) {
648     debug2("net: socket: %d proxy errno: %d", socklist[i].sock, s[1]);
649     socklist[i].flags &= ~(SOCK_CONNECT | SOCK_PROXYWAIT);
650     switch (s[1]) {
651     case 90: /* success */
652     s[0] = 0;
653     *len = 0;
654     return i;
655     case 91: /* failed */
656     errno = ECONNREFUSED;
657     break;
658     case 92: /* no identd */
659     case 93: /* identd said wrong username */
660     errno = ENETUNREACH;
661     break;
662     /* a better error message would be "socks misconfigured" */
663     /* or "identd not working" but this is simplest */
664     }
665     *len = socklist[i].sock;
666     return -1;
667     }
668     return i;
669     }
670     }
671     } else if (x == -1)
672     return -2; /* socket error */
673     else {
674     s[0] = 0;
675     *len = 0;
676     }
677     return -3;
678     }
679    
680     /* sockgets: buffer and read from sockets
681     *
682     * attempts to read from all registered sockets for up to one second. if
683     * after one second, no complete data has been received from any of the
684     * sockets, 's' will be empty, 'len' will be 0, and sockgets will return -3.
685     * if there is returnable data received from a socket, the data will be
686     * in 's' (null-terminated if non-binary), the length will be returned
687     * in len, and the socket number will be returned.
688     * normal sockets have their input buffered, and each call to sockgets
689     * will return one line terminated with a '\n'. binary sockets are not
690     * buffered and return whatever coems in as soon as it arrives.
691     * listening sockets will return an empty string when a connection comes in.
692     * connecting sockets will return an empty string on a successful connect,
693     * or EOF on a failed connect.
694     * if an EOF is detected from any of the sockets, that socket number will be
695     * put in len, and -1 will be returned.
696     * the maximum length of the string returned is 512 (including null)
697     */
698    
699     int sockgets(char *s, int *len)
700     {
701     char xx[514], *p, *px;
702     int ret, i, data = 0;
703    
704 fabian 1.9 Context;
705 segfault 1.1 for (i = 0; i < MAXSOCKS; i++) {
706     /* check for stored-up data waiting to be processed */
707     if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].inbuf != NULL)) {
708     /* look for \r too cos windows can't follow RFCs */
709     p = strchr(socklist[i].inbuf, '\n');
710     if (p == NULL)
711     p = strchr(socklist[i].inbuf, '\r');
712     if (p != NULL) {
713     *p = 0;
714     if (strlen(socklist[i].inbuf) > 510)
715     socklist[i].inbuf[510] = 0;
716     strcpy(s, socklist[i].inbuf);
717     px = (char *) nmalloc(strlen(p + 1) + 1);
718     strcpy(px, p + 1);
719     nfree(socklist[i].inbuf);
720     if (px[0])
721     socklist[i].inbuf = px;
722     else {
723     nfree(px);
724     socklist[i].inbuf = NULL;
725     }
726     /* strip CR if this was CR/LF combo */
727     if (s[strlen(s) - 1] == '\r')
728     s[strlen(s) - 1] = 0;
729     *len = strlen(s);
730     return socklist[i].sock;
731     }
732     }
733     /* also check any sockets that might have EOF'd during write */
734     if (!(socklist[i].flags & SOCK_UNUSED)
735     && (socklist[i].flags & SOCK_EOFD)) {
736 fabian 1.9 Context;
737 segfault 1.1 s[0] = 0;
738     *len = socklist[i].sock;
739     return -1;
740     }
741     }
742     /* no pent-up data of any worth -- down to business */
743 fabian 1.9 Context;
744 segfault 1.1 *len = 0;
745     ret = sockread(xx, len);
746     if (ret < 0) {
747     s[0] = 0;
748     return ret;
749     }
750     /* binary and listening sockets don't get buffered */
751 fabian 1.3 /* passed on sockets don't get buffered either (Fabian)*/
752 segfault 1.1 if (socklist[ret].flags & SOCK_CONNECT) {
753     if (socklist[ret].flags & SOCK_STRONGCONN) {
754     socklist[ret].flags &= ~SOCK_STRONGCONN;
755     /* buffer any data that came in, for future read */
756     socklist[ret].inbuf = (char *) nmalloc(strlen(xx) + 1);
757     strcpy(socklist[ret].inbuf, xx);
758     }
759     socklist[ret].flags &= ~SOCK_CONNECT;
760     s[0] = 0;
761     return socklist[ret].sock;
762     }
763     if (socklist[ret].flags & SOCK_BINARY) {
764     my_memcpy(s, xx, *len);
765     return socklist[ret].sock;
766     }
767 fabian 1.3 if ((socklist[ret].flags & SOCK_LISTEN) ||
768     (socklist[ret].flags & SOCK_PASS))
769 segfault 1.1 return socklist[ret].sock;
770 fabian 1.9 Context;
771 segfault 1.1 /* might be necessary to prepend stored-up data! */
772     if (socklist[ret].inbuf != NULL) {
773     p = socklist[ret].inbuf;
774     socklist[ret].inbuf = (char *) nmalloc(strlen(p) + strlen(xx) + 1);
775     strcpy(socklist[ret].inbuf, p);
776     strcat(socklist[ret].inbuf, xx);
777     nfree(p);
778     if (strlen(socklist[ret].inbuf) < 512) {
779     strcpy(xx, socklist[ret].inbuf);
780     nfree(socklist[ret].inbuf);
781     socklist[ret].inbuf = NULL;
782     } else {
783     p = socklist[ret].inbuf;
784     socklist[ret].inbuf = (char *) nmalloc(strlen(p) - 509);
785     strcpy(socklist[ret].inbuf, p + 510);
786     *(p + 510) = 0;
787     strcpy(xx, p);
788     nfree(p);
789     /* (leave the rest to be post-pended later) */
790     }
791     }
792 fabian 1.9 Context;
793 segfault 1.1 /* look for EOL marker; if it's there, i have something to show */
794     p = strchr(xx, '\n');
795     if (p == NULL)
796     p = strchr(xx, '\r');
797     if (p != NULL) {
798     *p = 0;
799     strcpy(s, xx);
800     strcpy(xx, p + 1);
801     if (s[strlen(s) - 1] == '\r')
802     s[strlen(s) - 1] = 0;
803     data = 1; /* DCC_CHAT may now need to process a
804     * blank line */
805     /* NO! */
806     /* if (!s[0]) strcpy(s," "); */
807     } else {
808     s[0] = 0;
809     if (strlen(xx) >= 510) {
810     /* string is too long, so just insert fake \n */
811     strcpy(s, xx);
812     xx[0] = 0;
813     data = 1;
814     }
815     }
816 fabian 1.9 Context;
817 segfault 1.1 *len = strlen(s);
818     /* anything left that needs to be saved? */
819     if (!xx[0]) {
820     if (data)
821     return socklist[ret].sock;
822     else
823     return -3;
824     }
825 fabian 1.9 Context;
826 segfault 1.1 /* prepend old data back */
827     if (socklist[ret].inbuf != NULL) {
828 fabian 1.9 Context;
829 segfault 1.1 p = socklist[ret].inbuf;
830     socklist[ret].inbuf = (char *) nmalloc(strlen(p) + strlen(xx) + 1);
831     strcpy(socklist[ret].inbuf, xx);
832     strcat(socklist[ret].inbuf, p);
833     nfree(p);
834     } else {
835 fabian 1.9 Context;
836 segfault 1.1 socklist[ret].inbuf = (char *) nmalloc(strlen(xx) + 1);
837     strcpy(socklist[ret].inbuf, xx);
838     }
839 fabian 1.9 Context;
840 segfault 1.1 if (data) {
841 fabian 1.9 Context;
842 segfault 1.1 return socklist[ret].sock;
843     } else {
844 fabian 1.9 Context;
845 segfault 1.1 return -3;
846     }
847     }
848    
849     /* dump something to a socket */
850     /* DO NOT PUT CONTEXTS IN HERE IF YOU WANT DEBUG TO BE MEANINGFUL!!! */
851     void tputs(int z, char *s, unsigned int len)
852     {
853 fabian 1.10 int i, x, idx;
854 segfault 1.1 char *p;
855    
856     if (z < 0)
857     return; /* um... HELLO?! sanity check please! */
858     if (((z == STDOUT) || (z == STDERR)) && (!backgrd || use_stderr)) {
859     write(z, s, len);
860     return;
861     }
862     for (i = 0; i < MAXSOCKS; i++) {
863     if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == z)) {
864 fabian 1.10
865     for (idx = 0; idx < dcc_total; idx++) {
866     if (dcc[idx].sock == z) {
867     if (dcc[idx].type) {
868     if (dcc[idx].type->name) {
869     if (!strncmp(dcc[idx].type->name, "BOT", 3)) {
870     otraffic_bn_today += len;
871     break;
872     } else if (!strcmp(dcc[idx].type->name, "SERVER")) {
873     otraffic_irc_today += len;
874     break;
875     } else if (!strncmp(dcc[idx].type->name, "CHAT", 4)) {
876     otraffic_dcc_today += len;
877     break;
878     } else if (!strncmp(dcc[idx].type->name, "FILES", 5)) {
879     otraffic_filesys_today += len;
880     break;
881     } else if (!strcmp(dcc[idx].type->name, "SEND")) {
882     otraffic_trans_today += len;
883     break;
884     } else if (!strncmp(dcc[idx].type->name, "GET", 3)) {
885     otraffic_trans_today += len;
886     break;
887     } else {
888     otraffic_unknown_today += len;
889     break;
890     }
891     }
892     }
893     }
894     }
895    
896 segfault 1.1 if (socklist[i].outbuf != NULL) {
897     /* already queueing: just add it */
898     p = (char *) nrealloc(socklist[i].outbuf, socklist[i].outbuflen + len);
899     my_memcpy(p + socklist[i].outbuflen, s, len);
900     socklist[i].outbuf = p;
901     socklist[i].outbuflen += len;
902     return;
903     }
904     /* try. */
905     x = write(z, s, len);
906     if (x == (-1))
907     x = 0;
908     if (x < len) {
909     /* socket is full, queue it */
910     socklist[i].outbuf = (char *) nmalloc(len - x);
911     my_memcpy(socklist[i].outbuf, &s[x], len - x);
912     socklist[i].outbuflen = len - x;
913     }
914     return;
915     }
916     }
917     putlog(LOG_MISC, "*", "!!! writing to nonexistent socket: %d", z);
918     s[strlen(s) - 1] = 0;
919     putlog(LOG_MISC, "*", "!-> '%s'", s);
920     }
921    
922     /* tputs might queue data for sockets, let's dump as much of it as
923     * possible */
924     void dequeue_sockets()
925     {
926     int i, x;
927    
928     for (i = 0; i < MAXSOCKS; i++) {
929     if (!(socklist[i].flags & SOCK_UNUSED) &&
930     (socklist[i].outbuf != NULL)) {
931     /* trick tputs into doing the work */
932     x = write(socklist[i].sock, socklist[i].outbuf,
933     socklist[i].outbuflen);
934     if ((x < 0) && (errno != EAGAIN)
935     #ifdef EBADSLT
936     && (errno != EBADSLT)
937     #endif
938     #ifdef ENOTCONN
939     && (errno != ENOTCONN)
940     #endif
941     ) {
942     /* this detects an EOF during writing */
943     debug3("net: eof!(write) socket %d (%s,%d)", socklist[i].sock,
944     strerror(errno), errno);
945     socklist[i].flags |= SOCK_EOFD;
946     } else if (x == socklist[i].outbuflen) {
947     /* if the whole buffer was sent, nuke it */
948     nfree(socklist[i].outbuf);
949     socklist[i].outbuf = NULL;
950     socklist[i].outbuflen = 0;
951     } else if (x > 0) {
952     char *p = socklist[i].outbuf;
953    
954     /* this removes any sent bytes from the beginning of the buffer */
955     socklist[i].outbuf = (char *) nmalloc(socklist[i].outbuflen - x);
956     my_memcpy(socklist[i].outbuf, p + x, socklist[i].outbuflen - x);
957     socklist[i].outbuflen -= x;
958     nfree(p);
959     }
960     }
961     }
962     }
963    
964     /* DEBUGGING STUFF */
965    
966     void tell_netdebug(int idx)
967     {
968     int i;
969     char s[80];
970    
971     dprintf(idx, "Open sockets:");
972     for (i = 0; i < MAXSOCKS; i++) {
973     if (!(socklist[i].flags & SOCK_UNUSED)) {
974     sprintf(s, " %d", socklist[i].sock);
975     if (socklist[i].flags & SOCK_BINARY)
976     strcat(s, " (binary)");
977     if (socklist[i].flags & SOCK_LISTEN)
978     strcat(s, " (listen)");
979 fabian 1.3 if (socklist[i].flags & SOCK_PASS)
980     strcat(s, " (passed on)");
981 segfault 1.1 if (socklist[i].flags & SOCK_CONNECT)
982     strcat(s, " (connecting)");
983     if (socklist[i].flags & SOCK_STRONGCONN)
984     strcat(s, " (strong)");
985     if (socklist[i].flags & SOCK_NONSOCK)
986     strcat(s, " (file)");
987     if (socklist[i].inbuf != NULL)
988     sprintf(&s[strlen(s)], " (inbuf: %04X)", strlen(socklist[i].inbuf));
989     if (socklist[i].outbuf != NULL)
990     sprintf(&s[strlen(s)], " (outbuf: %06lX)", socklist[i].outbuflen);
991     strcat(s, ",");
992     dprintf(idx, "%s", s);
993     }
994     }
995     dprintf(idx, " done.\n");
996     }
997    
998     /* Security-flavoured sanity checking on DCC connections of all sorts can be
999     * done with this routine. Feed it the proper information from your DCC
1000     * before you attempt the connection, and this will make an attempt at
1001     * figuring out if the connection is really that person, or someone screwing
1002     * around. It's not foolproof, but anything that fails this check probably
1003     * isn't going to work anyway due to masquerading firewalls, NAT routers,
1004     * or bugs in mIRC. */
1005     int sanitycheck_dcc(char *nick, char *from, char *ipaddy, char *port)
1006     {
1007     /* According to the latest RFC, the clients SHOULD be able to handle
1008     * DNS names that are up to 255 characters long. This is not broken. */
1009 fabian 1.3 char badaddress[16];
1010 segfault 1.1 IP ip = my_atoul(ipaddy);
1011     int prt = atoi(port);
1012    
1013     /* It is disabled HERE so we only have to check in *one* spot! */
1014     if (!dcc_sanitycheck)
1015     return 1;
1016 fabian 1.9 Context; /* This should be pretty solid, but
1017 segfault 1.1 * something _might_ break. */
1018     if (prt < 1) {
1019     putlog(LOG_MISC, "*", "ALERT: (%s!%s) specified an impossible port of %u!",
1020     nick, from, prt);
1021     return 0;
1022     }
1023 fabian 1.3 sprintf(badaddress, "%u.%u.%u.%u", (ip >> 24) & 0xff, (ip >> 16) & 0xff,
1024     (ip >> 8) & 0xff, ip & 0xff);
1025 segfault 1.1 if (ip < (1 << 24)) {
1026     putlog(LOG_MISC, "*", "ALERT: (%s!%s) specified an impossible IP of %s!",
1027     nick, from, badaddress);
1028     return 0;
1029     }
1030 fabian 1.3 return 1;
1031     }
1032    
1033     int hostsanitycheck_dcc(char *nick, char *from, IP ip, char *dnsname,
1034     char *prt)
1035     {
1036     /* According to the latest RFC, the clients SHOULD be able to handle
1037     * DNS names that are up to 255 characters long. This is not broken. */
1038     char hostname[256], badaddress[16];
1039    
1040     /* It is disabled HERE so we only have to check in *one* spot! */
1041     if (!dcc_sanitycheck)
1042     return 1;
1043 fabian 1.9 Context; /* This should be pretty solid, but
1044 fabian 1.3 * something _might_ break. */
1045     sprintf(badaddress, "%u.%u.%u.%u", (ip >> 24) & 0xff, (ip >> 16) & 0xff,
1046     (ip >> 8) & 0xff, ip & 0xff);
1047 segfault 1.1 /* These should pad like crazy with zeros, since 120 bytes or so is
1048     * where the routines providing our data currently lose interest. I'm
1049     * using the n-variant in case someone changes that... */
1050 fabian 1.7 strncpy(hostname, extracthostname(from), 255);
1051     hostname[255] = 0;
1052 segfault 1.1 if (!strcasecmp(hostname, dnsname)) {
1053     putlog(LOG_DEBUG, "*", "DNS information for submitted IP checks out.");
1054     return 1;
1055     }
1056     if (!strcmp(badaddress, dnsname))
1057     putlog(LOG_MISC, "*", "ALERT: (%s!%s) sent a DCC request with bogus IP information of %s port %u!",
1058     nick, from, badaddress, prt);
1059     else
1060     return 1; /* <- usually happens when we have
1061     a user with an unresolved hostmask! */
1062     return 0;
1063     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23