/[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.16 - (hide annotations) (download) (as text)
Sun Jan 30 19:26:21 2000 UTC (19 years, 4 months ago) by fabian
Branch: MAIN
CVS Tags: eggdrop105020
Changes since 1.15: +154 -130 lines
File MIME type: text/x-chdr
cleanup3 patch

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23