/[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.11 - (hide annotations) (download) (as text)
Tue Dec 21 17:35:10 1999 UTC (19 years, 7 months ago) by fabian
Branch: MAIN
Changes since 1.10: +7 -4 lines
File MIME type: text/x-chdr
Tothwolf's id-header

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23