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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23