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

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

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


Revision 1.17 - (hide annotations) (download) (as text)
Mon Jan 17 16:14:45 2000 UTC (19 years, 10 months ago) by per
Branch: MAIN
CVS Tags: eggdrop104030RC2, eggdrop10403RC1, eggdrop10402RC1, eggdrop10404, eggdrop10403, eggdrop10402, HEAD
Changes since 1.16: +2 -4 lines
File MIME type: text/x-chdr
relayfix, extern_cleanup

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23