/[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.36 - (hide annotations) (download) (as text)
Wed Aug 8 13:45:48 2001 UTC (17 years, 10 months ago) by drummer
Branch: MAIN
Changes since 1.35: +39 -9 lines
File MIME type: text/x-chdr
byteorder problem

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 drummer 1.36 * $Id: net.c,v 1.35 2001/08/01 16:31:41 drummer 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 drummer 1.33 #include "adns/adns.h"
30    
31 segfault 1.1 #if !HAVE_GETDTABLESIZE
32 fabian 1.16 # ifdef FD_SETSIZE
33     # define getdtablesize() FD_SETSIZE
34     # else
35     # define getdtablesize() 200
36     # endif
37 segfault 1.1 #endif
38    
39 drummer 1.33 #ifndef IN6_IS_ADDR_V4MAPPED
40     # define IN6_IS_ADDR_V4MAPPED(a) \
41     ((((uint32_t *) (a))[0] == 0) && (((uint32_t *) (a))[1] == 0) && \
42     (((uint32_t *) (a))[2] == htonl (0xffff)))
43     #endif
44    
45 fabian 1.16 extern struct dcc_t *dcc;
46 guppy 1.28 extern int backgrd, use_stderr, resolve_timeout, dcc_total;
47     extern unsigned long otraffic_irc_today, otraffic_bn_today,
48 fabian 1.16 otraffic_dcc_today, otraffic_filesys_today,
49     otraffic_trans_today, otraffic_unknown_today;
50 drummer 1.33 extern char natip[];
51 fabian 1.16
52     char hostname[121] = ""; /* Hostname can be specified in the config
53     file */
54     char myip[121] = ""; /* IP can be specified in the config file */
55 drummer 1.33 char myip6[121] = ""; /* IP6 can be specified in the config file */
56 fabian 1.16 char firewall[121] = ""; /* Socks server for firewall */
57     int firewallport = 1080; /* Default port of Sock4/5 firewalls */
58     char botuser[21] = "eggdrop"; /* Username of the user running the bot */
59     int dcc_sanitycheck = 0; /* We should do some sanity checking on dcc
60     connections. */
61     sock_list *socklist = NULL; /* Enough to be safe */
62     int MAXSOCKS = 0;
63     jmp_buf alarmret; /* Env buffer for alarm() returns */
64 segfault 1.1
65 drummer 1.33 adns_state ads;
66    
67 fabian 1.16 /* Types of proxy */
68 segfault 1.1 #define PROXY_SOCKS 1
69     #define PROXY_SUN 2
70 drummer 1.36 #define PROXY_POST 3
71 segfault 1.1
72    
73 fabian 1.16 /* I need an UNSIGNED long for dcc type stuff
74     */
75 segfault 1.1 IP my_atoul(char *s)
76     {
77     IP ret = 0;
78    
79     while ((*s >= '0') && (*s <= '9')) {
80     ret *= 10;
81     ret += ((*s) - '0');
82     s++;
83     }
84     return ret;
85     }
86    
87 fabian 1.16 /* Initialize the socklist
88     */
89 segfault 1.1 void init_net()
90     {
91     int i;
92    
93     for (i = 0; i < MAXSOCKS; i++) {
94     socklist[i].flags = SOCK_UNUSED;
95     }
96 drummer 1.33
97     /* init ADNS */
98     i = adns_init(&ads, adns_if_noautosys, 0);
99     if (i)
100     fatal(adns_strerror(i), 0);
101 segfault 1.1 }
102    
103     int expmem_net()
104     {
105     int i, tot = 0;
106    
107     for (i = 0; i < MAXSOCKS; i++) {
108     if (!(socklist[i].flags & SOCK_UNUSED)) {
109     if (socklist[i].inbuf != NULL)
110     tot += strlen(socklist[i].inbuf) + 1;
111     if (socklist[i].outbuf != NULL)
112     tot += socklist[i].outbuflen;
113     }
114     }
115     return tot;
116     }
117    
118 fabian 1.16 /* Get my ip number
119     */
120 segfault 1.1 IP getmyip()
121     {
122     struct hostent *hp;
123     char s[121];
124     IP ip;
125     struct in_addr *in;
126    
127 fabian 1.16 /* Could be pre-defined */
128 segfault 1.1 if (myip[0]) {
129     if ((myip[strlen(myip) - 1] >= '0') && (myip[strlen(myip) - 1] <= '9'))
130     return (IP) inet_addr(myip);
131     }
132 fabian 1.16 /* Also could be pre-defined */
133 segfault 1.1 if (hostname[0])
134     hp = gethostbyname(hostname);
135     else {
136     gethostname(s, 120);
137     hp = gethostbyname(s);
138     }
139     if (hp == NULL)
140     fatal("Hostname self-lookup failed.", 0);
141     in = (struct in_addr *) (hp->h_addr_list[0]);
142     ip = (IP) (in->s_addr);
143     return ip;
144     }
145    
146 drummer 1.33 #ifdef IPV6
147     struct in6_addr getmyip6()
148     {
149     struct in6_addr ip;
150    
151     /* Could be pre-defined */
152     if (myip6[0])
153     egg_inet_pton(AF_INET6, myip6, &ip);
154     else {
155     /* get system's default IPv6 ip -- FIXME!! */
156     /* there is know way?! - drummer */
157     ip = in6addr_any;
158     }
159     return ip;
160     }
161    
162     struct in6_addr ipv4to6(IP a)
163     {
164     struct in6_addr ip;
165     if (a == INADDR_ANY)
166     return in6addr_any;
167     else {
168     ((uint32_t *)&ip)[0] = 0;
169     ((uint32_t *)&ip)[1] = 0;
170     ((uint16_t *)&ip)[4] = 0;
171     ((uint16_t *)&ip)[5] = 0xffff;
172     ((uint32_t *)&ip)[3] = a;
173     }
174     return ip;
175     }
176    
177     #endif
178    
179 segfault 1.1 void neterror(char *s)
180     {
181     switch (errno) {
182     case EADDRINUSE:
183     strcpy(s, "Address already in use");
184     break;
185     case EADDRNOTAVAIL:
186     strcpy(s, "Address invalid on remote machine");
187     break;
188     case EAFNOSUPPORT:
189     strcpy(s, "Address family not supported");
190     break;
191     case EALREADY:
192     strcpy(s, "Socket already in use");
193     break;
194     case EBADF:
195     strcpy(s, "Socket descriptor is bad");
196     break;
197     case ECONNREFUSED:
198     strcpy(s, "Connection refused");
199     break;
200     case EFAULT:
201     strcpy(s, "Namespace segment violation");
202     break;
203     case EINPROGRESS:
204     strcpy(s, "Operation in progress");
205     break;
206     case EINTR:
207     strcpy(s, "Timeout");
208     break;
209     case EINVAL:
210     strcpy(s, "Invalid namespace");
211     break;
212     case EISCONN:
213     strcpy(s, "Socket already connected");
214     break;
215     case ENETUNREACH:
216     strcpy(s, "Network unreachable");
217     break;
218     case ENOTSOCK:
219     strcpy(s, "File descriptor, not a socket");
220     break;
221     case ETIMEDOUT:
222     strcpy(s, "Connection timed out");
223     break;
224     case ENOTCONN:
225     strcpy(s, "Socket is not connected");
226     break;
227     case EHOSTUNREACH:
228     strcpy(s, "Host is unreachable");
229     break;
230     case EPIPE:
231     strcpy(s, "Broken pipe");
232     break;
233     #ifdef ECONNRESET
234     case ECONNRESET:
235     strcpy(s, "Connection reset by peer");
236     break;
237     #endif
238     #ifdef EACCES
239     case EACCES:
240     strcpy(s, "Permission denied");
241     break;
242     #endif
243     case 0:
244     strcpy(s, "Error 0");
245     break;
246     default:
247     sprintf(s, "Unforseen error %d", errno);
248     break;
249     }
250     }
251    
252 fabian 1.25 /* Sets/Unsets options for a specific socket.
253     *
254     * Returns: 0 - on success
255     * -1 - socket not found
256     * -2 - illegal operation
257     */
258     int sockoptions(int sock, int operation, int sock_options)
259     {
260     int i;
261    
262     for (i = 0; i < MAXSOCKS; i++)
263 poptix 1.30 if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED)) {
264 fabian 1.25 if (operation == EGG_OPTION_SET)
265     socklist[i].flags |= sock_options;
266     else if (operation == EGG_OPTION_UNSET)
267     socklist[i].flags &= ~sock_options;
268     else
269     return -2;
270     return 0;
271     }
272     return -1;
273     }
274    
275 fabian 1.16 /* Return a free entry in the socket entry
276     */
277 fabian 1.3 int allocsock(int sock, int options)
278 segfault 1.1 {
279     int i;
280    
281     for (i = 0; i < MAXSOCKS; i++) {
282     if (socklist[i].flags & SOCK_UNUSED) {
283     /* yay! there is table space */
284     socklist[i].inbuf = socklist[i].outbuf = NULL;
285 fabian 1.25 socklist[i].inbuflen = socklist[i].outbuflen = 0;
286 segfault 1.1 socklist[i].flags = options;
287     socklist[i].sock = sock;
288 fabian 1.3 return i;
289     }
290     }
291     fatal("Socket table is full!", 0);
292 fabian 1.16 return -1; /* Never reached */
293 fabian 1.3 }
294    
295 fabian 1.16 /* Request a normal socket for i/o
296     */
297 fabian 1.3 void setsock(int sock, int options)
298     {
299     int i = allocsock(sock, options);
300     int parm;
301    
302 fabian 1.16 if (((sock != STDOUT) || backgrd) &&
303     !(socklist[i].flags & SOCK_NONSOCK)) {
304     parm = 1;
305 fabian 1.3 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *) &parm, sizeof(int));
306 segfault 1.1
307 fabian 1.16 parm = 0;
308     setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &parm, sizeof(int));
309     }
310     if (options & SOCK_LISTEN) {
311     /* Tris says this lets us grab the same port again next time */
312     parm = 1;
313     setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *) &parm, sizeof(int));
314     }
315     /* Yay async i/o ! */
316     fcntl(sock, F_SETFL, O_NONBLOCK);
317 segfault 1.1 }
318    
319     int getsock(int options)
320     {
321 drummer 1.33 #ifdef IPV6
322     int sock = socket(AF_INET6, SOCK_STREAM, 0);
323     #else
324 segfault 1.1 int sock = socket(AF_INET, SOCK_STREAM, 0);
325 drummer 1.33 #endif
326 segfault 1.1
327 fabian 1.24 if (sock >= 0)
328     setsock(sock, options);
329     else
330     putlog(LOG_MISC, "*", "Warning: Can't create new socket!");
331 segfault 1.1 return sock;
332     }
333    
334 fabian 1.16 /* Done with a socket
335     */
336 fabian 1.24 void killsock(register int sock)
337 segfault 1.1 {
338 fabian 1.24 register int i;
339 segfault 1.1
340 fabian 1.24 /* Ignore invalid sockets. */
341     if (sock < 0)
342     return;
343 segfault 1.1 for (i = 0; i < MAXSOCKS; i++) {
344 fabian 1.2 if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED)) {
345 segfault 1.1 close(socklist[i].sock);
346     if (socklist[i].inbuf != NULL) {
347     nfree(socklist[i].inbuf);
348     socklist[i].inbuf = NULL;
349     }
350     if (socklist[i].outbuf != NULL) {
351     nfree(socklist[i].outbuf);
352     socklist[i].outbuf = NULL;
353     socklist[i].outbuflen = 0;
354     }
355     socklist[i].flags = SOCK_UNUSED;
356     return;
357     }
358     }
359     putlog(LOG_MISC, "*", "Attempt to kill un-allocated socket %d !!", sock);
360     }
361    
362 fabian 1.16 /* Send connection request to proxy
363     */
364 segfault 1.1 static int proxy_connect(int sock, char *host, int port, int proxy)
365     {
366     unsigned char x[10];
367     struct hostent *hp;
368 fabian 1.26 char s[256];
369 segfault 1.1 int i;
370    
371     /* socks proxy */
372     if (proxy == PROXY_SOCKS) {
373     /* numeric IP? */
374 fabian 1.26 if (host[strlen(host) - 1] >= '0' && host[strlen(host) - 1] <= '9') {
375 segfault 1.1 IP ip = ((IP) inet_addr(host)); /* drummer */
376 fabian 1.18 egg_memcpy(x, &ip, 4); /* Beige@Efnet */
377 segfault 1.1 } else {
378     /* no, must be host.domain */
379     if (!setjmp(alarmret)) {
380     alarm(resolve_timeout);
381     hp = gethostbyname(host);
382     alarm(0);
383     } else {
384     hp = NULL;
385     }
386     if (hp == NULL) {
387     killsock(sock);
388     return -2;
389     }
390 fabian 1.18 egg_memcpy(x, hp->h_addr, hp->h_length);
391 segfault 1.1 }
392 fabian 1.26 egg_snprintf(s, sizeof s, "\004\001%c%c%c%c%c%c%s", (port >> 8) % 256,
393     (port % 256), x[0], x[1], x[2], x[3], botuser);
394 segfault 1.1 tputs(sock, s, strlen(botuser) + 9); /* drummer */
395     } else if (proxy == PROXY_SUN) {
396 drummer 1.36 egg_snprintf(s, sizeof s, "CONNECT %s:%d HTTP/1.0\n\n", host, port);
397     fcntl(sock, F_SETFL, 0); /* azta jo qrva anyadat eggdrop! */
398 segfault 1.1 tputs(sock, s, strlen(s)); /* drummer */
399 drummer 1.36 fcntl(sock, F_SETFL, O_NONBLOCK);
400     } else if (proxy == PROXY_POST) {
401     egg_snprintf(s, sizeof s, "POST http://%s:%d HTTP/1.0\n\n", host, port);
402     fcntl(sock, F_SETFL, 0); /* azta jo qrva anyadat eggdrop! */
403     tputs(sock, s, strlen(s)); /* drummer */
404     fcntl(sock, F_SETFL, O_NONBLOCK);
405 segfault 1.1 }
406 drummer 1.36 if (proxy != PROXY_POST)
407     for (i = 0; i < MAXSOCKS; i++)
408     if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
409     socklist[i].flags |= SOCK_PROXYWAIT; /* drummer */
410 segfault 1.1 return sock;
411     }
412    
413 fabian 1.16 /* Starts a connection attempt to a socket
414 fabian 1.11 *
415 fabian 1.3 * If given a normal hostname, this will be resolved to the corresponding
416     * IP address first. PLEASE try to use the non-blocking dns functions
417     * instead and then call this function with the IP address to avoid blocking.
418 fabian 1.11 *
419 segfault 1.1 * returns <0 if connection refused:
420     * -1 neterror() type error
421 fabian 1.16 * -2 can't resolve hostname
422     */
423 segfault 1.1 int open_telnet_raw(int sock, char *server, int sport)
424     {
425 drummer 1.33 #ifdef IPV6
426     struct sockaddr_in6 name, vname;
427     #else
428     struct sockaddr_in name, vname;
429     #endif
430 segfault 1.1 struct hostent *hp;
431     char host[121];
432     int i, port;
433     volatile int proxy;
434    
435 drummer 1.33 debug2("|NET| open_telnet_raw: %s %d", server, sport);
436    
437 segfault 1.1 /* firewall? use socks */
438     if (firewall[0]) {
439     if (firewall[0] == '!') {
440     proxy = PROXY_SUN;
441     strcpy(host, &firewall[1]);
442 drummer 1.36 } else if (firewall[0] == '@') {
443     proxy = PROXY_POST;
444     strcpy(host, &firewall[1]);
445 segfault 1.1 } else {
446     proxy = PROXY_SOCKS;
447     strcpy(host, firewall);
448     }
449     port = firewallport;
450     } else {
451     proxy = 0;
452     strcpy(host, server);
453     port = sport;
454     }
455    
456 drummer 1.33 egg_bzero((char *) &name, sizeof name);
457     #ifdef IPV6
458     name.sin6_family = AF_INET6;
459     name.sin6_port = htons(port);
460     #else
461 segfault 1.1 name.sin_family = AF_INET;
462 drummer 1.33 name.sin_port = htons(port);
463     #endif
464    
465     if (!setjmp(alarmret)) {
466     char *p;
467     int type;
468    
469     alarm(resolve_timeout);
470 segfault 1.1
471 drummer 1.33 if (!egg_strncasecmp("ipv6%", host, 5)) {
472     type = AF_INET6;
473     p = host + 5;
474     debug1("|NET| checking only AAAA record for %s", p);
475     } else if (!egg_strncasecmp("ipv4%", host, 5)) {
476     type = AF_INET;
477     p = host + 5;
478     debug1("|NET| checking only A record for %s", p);
479     } else {
480     type = AF_INET; /* af_preferred */
481     p = host;
482     }
483 segfault 1.1 alarm(resolve_timeout);
484 drummer 1.33 #ifndef IPV6
485     hp = gethostbyname(p);
486     #else
487     hp = gethostbyname2(p, type);
488     if (!hp && (p == host))
489     hp = gethostbyname2(p, (type == AF_INET6 ? AF_INET : AF_INET6));
490     #endif
491 segfault 1.1 alarm(0);
492 drummer 1.33 } else {
493 segfault 1.1 hp = NULL;
494 drummer 1.33 }
495     if (hp == NULL)
496 segfault 1.1 return -2;
497 drummer 1.33
498     #ifndef IPV6
499     egg_memcpy(&name.sin_addr, hp->h_addr, hp->h_length);
500     name.sin_family = hp->h_addrtype;
501     #else
502     if (hp->h_addrtype == AF_INET6)
503     egg_memcpy(&name.sin6_addr, hp->h_addr, hp->h_length);
504     else if (hp->h_addrtype == AF_INET)
505     name.sin6_addr = ipv4to6(*((unsigned int*) hp->h_addr));
506     else
507     return -123; /* panic :P */
508     name.sin6_family = AF_INET6;
509     #endif
510    
511     egg_bzero((char *) &vname, sizeof vname);
512     #ifndef IPV6
513     vname.sin_family = AF_INET;
514     vname.sin_addr.s_addr = (myip[0] ? getmyip() : INADDR_ANY);
515     #else
516     vname.sin6_family = AF_INET6;
517 drummer 1.35 if (IN6_IS_ADDR_V4MAPPED(&name.sin6_addr))
518 drummer 1.33 vname.sin6_addr = ipv4to6(myip[0] ? getmyip() : INADDR_ANY);
519     else
520     vname.sin6_addr = (myip6[0] ? getmyip6() : in6addr_any);
521     #endif
522     if (bind(sock, (struct sockaddr *) &vname, sizeof vname) < 0)
523     return -1;
524    
525 segfault 1.1 for (i = 0; i < MAXSOCKS; i++) {
526     if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == sock))
527 fabian 1.17 socklist[i].flags = (socklist[i].flags & ~SOCK_VIRTUAL) | SOCK_CONNECT;
528 segfault 1.1 }
529 drummer 1.33 if (connect(sock, (struct sockaddr *) &name, (sizeof name)) < 0) {
530 segfault 1.1 if (errno == EINPROGRESS) {
531 fabian 1.16 /* Firewall? announce connect attempt to proxy */
532 segfault 1.1 if (firewall[0])
533     return proxy_connect(sock, server, sport, proxy);
534     return sock; /* async success! */
535 fabian 1.4 } else
536 segfault 1.1 return -1;
537     }
538 fabian 1.16 /* Synchronous? :/ */
539 segfault 1.1 if (firewall[0])
540     return proxy_connect(sock, server, sport, proxy);
541     return sock;
542     }
543    
544 fabian 1.16 /* Ordinary non-binary connection attempt */
545 segfault 1.1 int open_telnet(char *server, int port)
546     {
547 fabian 1.4 int sock = getsock(0),
548     ret = open_telnet_raw(sock, server, port);
549    
550     if (ret < 0)
551     killsock(sock);
552     return ret;
553 segfault 1.1 }
554    
555 fabian 1.16 /* Returns a socket number for a listening socket that will accept any
556 fabian 1.19 * connection on a certain address -- port # is returned in port
557 drummer 1.33 * "" means on any address
558 fabian 1.16 */
559 drummer 1.33 int open_address_listen(char *addr, int *port)
560 segfault 1.1 {
561 fabian 1.13 int sock;
562     unsigned int addrlen;
563 drummer 1.33 #ifdef IPV6
564     struct sockaddr_in6 name;
565     #else
566 segfault 1.1 struct sockaddr_in name;
567 drummer 1.33 #endif
568 segfault 1.1
569     if (firewall[0]) {
570     /* FIXME: can't do listen port thru firewall yet */
571 fabian 1.6 putlog(LOG_MISC, "*", "!! Cant open a listen port (you are using a firewall)");
572 segfault 1.1 return -1;
573     }
574 fabian 1.24
575 segfault 1.1 sock = getsock(SOCK_LISTEN);
576 fabian 1.24 if (sock < 1)
577     return -1;
578 drummer 1.33
579     debug2("|NET| open_address_listen(\"%s\", %d)", addr, *port);
580 fabian 1.24
581 drummer 1.33 egg_bzero((char *) &name, sizeof name);
582     #ifdef IPV6
583     name.sin6_family = AF_INET6;
584     name.sin6_port = htons(*port); /* 0 = just assign us a port */
585     if (!addr[0])
586     name.sin6_addr = in6addr_any;
587     else if (!egg_inet_pton(AF_INET6, addr, &name.sin6_addr)) {
588     struct in_addr a4;
589     if (egg_inet_aton(addr, &a4))
590     name.sin6_addr = ipv4to6(a4.s_addr);
591     else
592     name.sin6_addr = in6addr_any;
593     }
594     #else
595 segfault 1.1 name.sin_family = AF_INET;
596 fabian 1.19 name.sin_port = htons(*port); /* 0 = just assign us a port */
597 drummer 1.33 if (addr[0])
598     egg_inet_aton(addr, &name.sin_addr);
599     else
600     name.sin_addr.s_addr = INADDR_ANY;
601     #endif
602     if (bind(sock, (struct sockaddr *) &name, sizeof name) < 0) {
603 segfault 1.1 killsock(sock);
604 guppy 1.29 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT)
605     return -2;
606     else
607     return -1;
608 segfault 1.1 }
609     /* what port are we on? */
610 drummer 1.33 addrlen = sizeof name;
611 segfault 1.1 if (getsockname(sock, (struct sockaddr *) &name, &addrlen) < 0) {
612     killsock(sock);
613     return -1;
614     }
615 drummer 1.33 #ifdef IPV6
616     *port = ntohs(name.sin6_port);
617     #else
618 fabian 1.19 *port = ntohs(name.sin_port);
619 drummer 1.33 #endif
620 segfault 1.1 if (listen(sock, 1) < 0) {
621     killsock(sock);
622     return -1;
623     }
624     return sock;
625 fabian 1.19 }
626    
627     /* Returns a socket number for a listening socket that will accept any
628     * connection -- port # is returned in port
629     */
630 drummer 1.33 inline int open_listen(int *port, int af)
631 fabian 1.19 {
632 drummer 1.33 if (af == AF_INET)
633     return open_address_listen(myip, port);
634     #ifdef IPV6
635     else if (af == AF_INET6)
636     return open_address_listen(myip6, port);
637     #endif
638     else
639     return -1;
640 segfault 1.1 }
641    
642 fabian 1.3 /* Given a network-style IP address, returns the hostname. The hostname
643     * will be in the "##.##.##.##" format if there was an error.
644     *
645     * NOTE: This function is depreciated. Try using the async dns approach
646 fabian 1.16 * instead.
647     */
648 segfault 1.1 char *hostnamefromip(unsigned long ip)
649     {
650     struct hostent *hp;
651     unsigned long addr = ip;
652     unsigned char *p;
653 fabian 1.3 static char s[UHOSTLEN];
654 segfault 1.1
655     if (!setjmp(alarmret)) {
656     alarm(resolve_timeout);
657     hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
658     alarm(0);
659     } else {
660     hp = NULL;
661     }
662     if (hp == NULL) {
663     p = (unsigned char *) &addr;
664     sprintf(s, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
665     return s;
666     }
667 guppy 1.27 strncpyz(s, hp->h_name, sizeof s);
668 segfault 1.1 return s;
669     }
670    
671 fabian 1.5 /* Returns the given network byte order IP address in the
672 fabian 1.16 * dotted format - "##.##.##.##"
673 drummer 1.33 * (it's IPv4 only, and it's okey - drummer)
674 fabian 1.16 */
675 fabian 1.3 char *iptostr(IP ip)
676     {
677     struct in_addr a;
678    
679     a.s_addr = ip;
680     return inet_ntoa(a);
681     }
682    
683 drummer 1.33 char *getlocaladdr(int sock)
684     {
685     static char buf[ADDRLEN];
686     #ifdef IPV6
687     struct sockaddr_in6 sa;
688     #else
689     struct sockaddr_in sa;
690     #endif
691     int len = sizeof sa;
692    
693     if (sock == -1) { /* assuming IPv4... */
694     sprintf(buf, "%lu", (unsigned long int)
695     ntohl(natip[0] ? inet_addr(natip) : getmyip()));
696     return buf;
697     }
698    
699     if (getsockname(sock, (struct sockaddr*) &sa, &len) == -1) {
700     debug1("|NET| getsockname() failed for sock %d", sock);
701     return 0;
702     }
703     #ifdef IPV6
704     if (IN6_IS_ADDR_V4MAPPED(&sa.sin6_addr))
705     sprintf(buf, "%lu",
706 drummer 1.34 (unsigned long int) ntohl(((uint32_t *)&sa.sin6_addr)[3]));
707 drummer 1.33 else
708     egg_inet_ntop(AF_INET6, &(sa.sin6_addr), buf, sizeof buf);
709     #else
710 drummer 1.36 sprintf(buf, "%lu", (unsigned long int) ntohl(sa.sin_addr.s_addr));
711 drummer 1.33 #endif
712     return buf;
713     }
714    
715 fabian 1.16 /* Short routine to answer a connect received on a socket made previously
716 segfault 1.1 * by open_listen ... returns hostname of the caller & the new socket
717 fabian 1.16 * does NOT dispose of old "public" socket!
718     */
719 drummer 1.33 int answer(int sock, char *caller, char *ip, unsigned short *port,
720 fabian 1.16 int binary)
721 segfault 1.1 {
722 fabian 1.13 int new_sock;
723     unsigned int addrlen;
724 drummer 1.33 #ifdef IPV6
725     struct sockaddr_in6 from;
726     #else
727 segfault 1.1 struct sockaddr_in from;
728 drummer 1.33 #endif
729 fabian 1.25
730 drummer 1.33 addrlen = sizeof from;
731 segfault 1.1 new_sock = accept(sock, (struct sockaddr *) &from, &addrlen);
732     if (new_sock < 0)
733     return -1;
734     if (ip != NULL) {
735 drummer 1.33 #ifdef IPV6
736 drummer 1.35 if (IN6_IS_ADDR_V4MAPPED(&from.sin6_addr))
737 drummer 1.34 egg_inet_ntop(AF_INET, &(((uint32_t *)&from.sin6_addr)[3]), ip, ADDRMAX);
738 drummer 1.33 else
739     egg_inet_ntop(AF_INET6, &(from.sin6_addr), ip, ADDRMAX);
740     #else
741     egg_inet_ntop(AF_INET, &(from.sin_addr.s_addr), ip, ADDRMAX);
742     #endif
743 fabian 1.16 /* This is now done asynchronously. We now only provide the IP address.
744     *
745 fabian 1.3 * strncpy(caller, hostnamefromip(*ip), 120);
746     */
747 drummer 1.33 strncpyz(caller, ip, 121);
748 segfault 1.1 }
749     if (port != NULL)
750 drummer 1.33 #ifdef IPV6
751     *port = ntohs(from.sin6_port);
752     #else
753 fabian 1.21 *port = ntohs(from.sin_port);
754 drummer 1.33 #endif
755 fabian 1.16 /* Set up all the normal socket crap */
756 segfault 1.1 setsock(new_sock, (binary ? SOCK_BINARY : 0));
757     return new_sock;
758     }
759    
760 fabian 1.16 /* Like open_telnet, but uses server & port specifications of dcc
761     */
762 segfault 1.1 int open_telnet_dcc(int sock, char *server, char *port)
763     {
764     int p;
765 drummer 1.33 struct in_addr ia;
766 segfault 1.1
767 drummer 1.33 debug2("|NET| open_telnet_dcc: %s %s", server, port);
768 segfault 1.1 if (port != NULL)
769     p = atoi(port);
770     else
771 drummer 1.33 return -3;
772     if (server == NULL)
773     return -3;
774     /* fix the IPv4 IP format (ie: 167772161 -> 10.0.0.1) */
775     if (egg_inet_aton(server, &ia))
776     return open_telnet_raw(sock, inet_ntoa(ia), p);
777 segfault 1.1 else
778 drummer 1.33 return open_telnet_raw(sock, server, p);
779     }
780    
781     void egg_dns_gotanswer(int status, adns_answer *aw, char *origname)
782     {
783     char name[UHOSTLEN];
784    
785     if (!aw) {
786     debug0("|DNS| egg_dns_gotanswer: ANSWER IS NULL!");
787     return;
788     }
789     if (!origname) {
790     debug0("|DNS| egg_dns_gotanswer: origname is NULL!");
791     return;
792     }
793    
794     debug2("|DNS| egg_dns_gotanswer: status=%d adns_answer=%x", status, (int)aw);
795     status = 0;
796     if ((aw->type == adns_r_addr)
797     #ifdef IPV6
798     || (aw->type == adns_r_addr6)
799     #endif
800     ) {
801     if ((aw->status == adns_s_ok) && (aw->nrrs > 0)) {
802     adns_rr_addr *rrp = aw->rrs.untyped;
803     if (rrp->addr.sa.sa_family == AF_INET) {
804     egg_inet_ntop(AF_INET, &(rrp->addr.inet.sin_addr), name, UHOSTLEN-1);
805     status = 1;
806     #ifdef IPV6
807     } else if (rrp->addr.sa.sa_family == AF_INET6) {
808     egg_inet_ntop(AF_INET6, &(rrp->addr.inet6.sin6_addr), name, UHOSTLEN-1);
809     status = 1;
810     #endif
811     }
812     #ifdef IPV6
813     } else if ((aw->type == adns_r_addr /* af_preferred */) &&
814     egg_strncasecmp(origname, "ipv6%", 5) &&
815     egg_strncasecmp(origname, "ipv4%", 5)) {
816     adns_query q6;
817     char *orign2 = (char*) nmalloc(strlen(origname) + 1);
818     strcpy(orign2, origname);
819     /* ...it may be AAAA */
820     debug1("|DNS| egg_dns_gotanswer: A failed, checking for AAAA (%s)", origname);
821     adns_submit(ads, origname, adns_r_addr6, 0, orign2, &q6);
822     status = -1;
823     #endif
824     }
825     if (status == 0)
826     strcpy(name, "0.0.0.0");
827     if (status >= 0) {
828     debug3("|DNS| egg_dns_gotanswer: (ipbyhost) host: %s ip: %s status: %d", origname, name, status);
829     call_ipbyhost(origname, name, status);
830     }
831     } else if ((aw->type == adns_r_ptr_ip6) || (aw->type == adns_r_ptr)) {
832     if ((aw->status == adns_s_ok) && (aw->nrrs > 0)) {
833     if (aw->rrs.str) {
834     strncpyz(name, *(aw->rrs.str), UHOSTLEN);
835     status = 1;
836     }
837     }
838     if (!status) {
839     if (origname)
840     strncpyz(name, origname, UHOSTLEN);
841     else
842     strcpy(name, "error");
843     }
844     debug3("|DNS| egg_dns_gotanswer: (hostbyip) ip: %s host: %s status: %d", origname, name, status);
845     call_hostbyip(origname, name, status);
846     } else
847     debug0("|DNS| egg_dns_gotanswer: got unknow type of answer ?!");
848     nfree(origname);
849     }
850    
851     void egg_dns_checkall()
852     {
853     adns_query q, r;
854     adns_answer *answer;
855     char *origname;
856    
857     adns_forallqueries_begin(ads);
858     while ((q = adns_forallqueries_next(ads, (void **)&r)) != NULL) {
859     switch (adns_check(ads, &q, &answer, (void **)&origname)) {
860     case 0: /* ok */
861     egg_dns_gotanswer(1, answer, origname);
862     break;
863     case EAGAIN: /* Go into the queue again */
864     break;
865     default: /* failed */
866     egg_dns_gotanswer(0, answer, origname);
867     break;
868     }
869     }
870 segfault 1.1 }
871    
872 fabian 1.16 /* Attempts to read from all the sockets in socklist
873 segfault 1.1 * fills s with up to 511 bytes if available, and returns the array index
874 fabian 1.16 *
875     * on EOF: returns -1, with socket in len
876     * on socket error: returns -2
877     * if nothing is ready: returns -3
878     */
879 segfault 1.1 static int sockread(char *s, int *len)
880     {
881 drummer 1.33 fd_set fd, fdw, fde;
882 segfault 1.1 int fds, i, x;
883 drummer 1.33 struct timeval t, tnow;
884     struct timeval *pt = &t;
885 segfault 1.1 int grab = 511;
886    
887     fds = getdtablesize();
888     #ifdef FD_SETSIZE
889     if (fds > FD_SETSIZE)
890 fabian 1.16 fds = FD_SETSIZE; /* Fixes YET ANOTHER freebsd bug!!! */
891 segfault 1.1 #endif
892     /* timeout: 1 sec */
893     t.tv_sec = 1;
894     t.tv_usec = 0;
895     FD_ZERO(&fd);
896 fabian 1.17
897 segfault 1.1 for (i = 0; i < MAXSOCKS; i++)
898 fabian 1.17 if (!(socklist[i].flags & (SOCK_UNUSED | SOCK_VIRTUAL))) {
899 segfault 1.1 if ((socklist[i].sock == STDOUT) && !backgrd)
900     FD_SET(STDIN, &fd);
901     else
902     FD_SET(socklist[i].sock, &fd);
903     }
904 drummer 1.33 tnow.tv_sec = time(NULL);
905     tnow.tv_usec = 0;
906     adns_beforeselect(ads, &fds, &fd, &fdw, &fde, &pt, 0, &tnow);
907 segfault 1.1 #ifdef HPUX_HACKS
908     #ifndef HPUX10_HACKS
909     x = select(fds, (int *) &fd, (int *) NULL, (int *) NULL, &t);
910     #else
911     x = select(fds, &fd, NULL, NULL, &t);
912     #endif
913     #else
914     x = select(fds, &fd, NULL, NULL, &t);
915     #endif
916 drummer 1.33 tnow.tv_sec = time(NULL);
917     tnow.tv_usec = 0;
918     adns_afterselect(ads, fds, &fd, &fdw, &fde, &tnow);
919    
920     /* dns stuff */
921     egg_dns_checkall();
922    
923 segfault 1.1 if (x > 0) {
924 fabian 1.16 /* Something happened */
925 segfault 1.1 for (i = 0; i < MAXSOCKS; i++) {
926     if ((!(socklist[i].flags & SOCK_UNUSED)) &&
927     ((FD_ISSET(socklist[i].sock, &fd)) ||
928     ((socklist[i].sock == STDOUT) && (!backgrd) &&
929     (FD_ISSET(STDIN, &fd))))) {
930     if (socklist[i].flags & (SOCK_LISTEN | SOCK_CONNECT)) {
931 fabian 1.16 /* Listening socket -- don't read, just return activity */
932     /* Same for connection attempt */
933 segfault 1.1 /* (for strong connections, require a read to succeed first) */
934     if (socklist[i].flags & SOCK_PROXYWAIT) { /* drummer */
935 fabian 1.16 /* Hang around to get the return code from proxy */
936 segfault 1.1 grab = 10;
937     } else if (!(socklist[i].flags & SOCK_STRONGCONN)) {
938     debug1("net: connect! sock %d", socklist[i].sock);
939     s[0] = 0;
940     *len = 0;
941     return i;
942     }
943 fabian 1.3 } else if (socklist[i].flags & SOCK_PASS) {
944     s[0] = 0;
945     *len = 0;
946     return i;
947 segfault 1.1 }
948     if ((socklist[i].sock == STDOUT) && !backgrd)
949     x = read(STDIN, s, grab);
950     else
951     x = read(socklist[i].sock, s, grab);
952     if (x <= 0) { /* eof */
953 guppy 1.31 if (errno == EAGAIN) {
954 segfault 1.1 s[0] = 0;
955     *len = 0;
956     return -3;
957     }
958     *len = socklist[i].sock;
959     socklist[i].flags &= ~SOCK_CONNECT;
960     debug1("net: eof!(read) socket %d", socklist[i].sock);
961     return -1;
962     }
963     s[x] = 0;
964     *len = x;
965     if (socklist[i].flags & SOCK_PROXYWAIT) {
966     debug2("net: socket: %d proxy errno: %d", socklist[i].sock, s[1]);
967     socklist[i].flags &= ~(SOCK_CONNECT | SOCK_PROXYWAIT);
968     switch (s[1]) {
969 drummer 1.36 case 84: /* HTTP/1.0 2 */
970     {
971     char c;
972     int cc = 0;
973     while (read(socklist[i].sock, &c, 1) > 0) {
974     if (c == 10) {
975     if (cc == 0)
976     cc++;
977     else
978     break;
979     } else if (c != 13) {
980     cc = 0;
981     }
982     }
983     }
984     s[0] = 0;
985     *len = 0;
986     return i;
987 fabian 1.16 case 90: /* Success */
988 segfault 1.1 s[0] = 0;
989     *len = 0;
990     return i;
991 fabian 1.16 case 91: /* Failed */
992 segfault 1.1 errno = ECONNREFUSED;
993     break;
994 fabian 1.16 case 92: /* No identd */
995     case 93: /* Identd said wrong username */
996     /* A better error message would be "socks misconfigured"
997     * or "identd not working" but this is simplest.
998     */
999 segfault 1.1 errno = ENETUNREACH;
1000     break;
1001     }
1002     *len = socklist[i].sock;
1003     return -1;
1004     }
1005     return i;
1006     }
1007     }
1008     } else if (x == -1)
1009     return -2; /* socket error */
1010     else {
1011     s[0] = 0;
1012     *len = 0;
1013     }
1014     return -3;
1015     }
1016    
1017     /* sockgets: buffer and read from sockets
1018     *
1019 fabian 1.16 * Attempts to read from all registered sockets for up to one second. if
1020 segfault 1.1 * after one second, no complete data has been received from any of the
1021     * sockets, 's' will be empty, 'len' will be 0, and sockgets will return -3.
1022     * if there is returnable data received from a socket, the data will be
1023     * in 's' (null-terminated if non-binary), the length will be returned
1024     * in len, and the socket number will be returned.
1025     * normal sockets have their input buffered, and each call to sockgets
1026     * will return one line terminated with a '\n'. binary sockets are not
1027     * buffered and return whatever coems in as soon as it arrives.
1028     * listening sockets will return an empty string when a connection comes in.
1029     * connecting sockets will return an empty string on a successful connect,
1030     * or EOF on a failed connect.
1031     * if an EOF is detected from any of the sockets, that socket number will be
1032     * put in len, and -1 will be returned.
1033     * the maximum length of the string returned is 512 (including null)
1034 fabian 1.25 *
1035     * Returns -4 if we handled something that shouldn't be handled by the
1036     * dcc functions. Simply ignore it.
1037 segfault 1.1 */
1038    
1039     int sockgets(char *s, int *len)
1040     {
1041     char xx[514], *p, *px;
1042     int ret, i, data = 0;
1043    
1044     for (i = 0; i < MAXSOCKS; i++) {
1045 fabian 1.16 /* Check for stored-up data waiting to be processed */
1046 fabian 1.25 if (!(socklist[i].flags & SOCK_UNUSED) &&
1047     !(socklist[i].flags & SOCK_BUFFER) && (socklist[i].inbuf != NULL)) {
1048     if (!(socklist[i].flags & SOCK_BINARY)) {
1049     /* look for \r too cos windows can't follow RFCs */
1050     p = strchr(socklist[i].inbuf, '\n');
1051     if (p == NULL)
1052     p = strchr(socklist[i].inbuf, '\r');
1053     if (p != NULL) {
1054     *p = 0;
1055     if (strlen(socklist[i].inbuf) > 510)
1056     socklist[i].inbuf[510] = 0;
1057     strcpy(s, socklist[i].inbuf);
1058     px = (char *) nmalloc(strlen(p + 1) + 1);
1059     strcpy(px, p + 1);
1060     nfree(socklist[i].inbuf);
1061     if (px[0])
1062     socklist[i].inbuf = px;
1063     else {
1064     nfree(px);
1065     socklist[i].inbuf = NULL;
1066     }
1067     /* Strip CR if this was CR/LF combo */
1068     if (s[strlen(s) - 1] == '\r')
1069     s[strlen(s) - 1] = 0;
1070     *len = strlen(s);
1071     return socklist[i].sock;
1072     }
1073     } else {
1074     /* Handling buffered binary data (must have been SOCK_BUFFER before). */
1075     if (socklist[i].inbuflen <= 510) {
1076     *len = socklist[i].inbuflen;
1077     egg_memcpy(s, socklist[i].inbuf, socklist[i].inbuflen);
1078     nfree(socklist[i].inbuf);
1079 poptix 1.30 socklist[i].inbuf = NULL;
1080 fabian 1.25 socklist[i].inbuflen = 0;
1081     } else {
1082     /* Split up into chunks of 510 bytes. */
1083     *len = 510;
1084     egg_memcpy(s, socklist[i].inbuf, *len);
1085     egg_memcpy(socklist[i].inbuf, socklist[i].inbuf + *len, *len);
1086     socklist[i].inbuflen -= *len;
1087     socklist[i].inbuf = nrealloc(socklist[i].inbuf,
1088     socklist[i].inbuflen);
1089 segfault 1.1 }
1090     return socklist[i].sock;
1091     }
1092     }
1093 fabian 1.16 /* Also check any sockets that might have EOF'd during write */
1094 segfault 1.1 if (!(socklist[i].flags & SOCK_UNUSED)
1095     && (socklist[i].flags & SOCK_EOFD)) {
1096     s[0] = 0;
1097     *len = socklist[i].sock;
1098     return -1;
1099     }
1100     }
1101 fabian 1.16 /* No pent-up data of any worth -- down to business */
1102 segfault 1.1 *len = 0;
1103     ret = sockread(xx, len);
1104     if (ret < 0) {
1105     s[0] = 0;
1106     return ret;
1107     }
1108 fabian 1.16 /* Binary, listening and passed on sockets don't get buffered. */
1109 segfault 1.1 if (socklist[ret].flags & SOCK_CONNECT) {
1110     if (socklist[ret].flags & SOCK_STRONGCONN) {
1111     socklist[ret].flags &= ~SOCK_STRONGCONN;
1112 fabian 1.25 /* Buffer any data that came in, for future read. */
1113     socklist[ret].inbuflen = *len;
1114     socklist[ret].inbuf = (char *) nmalloc(*len + 1);
1115     /* It might be binary data. You never know. */
1116     egg_memcpy(socklist[ret].inbuf, xx, *len);
1117     socklist[ret].inbuf[*len] = 0;
1118 segfault 1.1 }
1119     socklist[ret].flags &= ~SOCK_CONNECT;
1120     s[0] = 0;
1121     return socklist[ret].sock;
1122     }
1123     if (socklist[ret].flags & SOCK_BINARY) {
1124 fabian 1.18 egg_memcpy(s, xx, *len);
1125 segfault 1.1 return socklist[ret].sock;
1126     }
1127 fabian 1.3 if ((socklist[ret].flags & SOCK_LISTEN) ||
1128     (socklist[ret].flags & SOCK_PASS))
1129 segfault 1.1 return socklist[ret].sock;
1130 fabian 1.25 if (socklist[ret].flags & SOCK_BUFFER) {
1131     socklist[ret].inbuf = (char *) nrealloc(socklist[ret].inbuf,
1132     socklist[ret].inbuflen + *len + 1);
1133     egg_memcpy(socklist[ret].inbuf + socklist[ret].inbuflen, xx, *len);
1134     socklist[ret].inbuflen += *len;
1135     /* We don't know whether it's binary data. Make sure normal strings
1136     will be handled properly later on too. */
1137     socklist[ret].inbuf[socklist[ret].inbuflen] = 0;
1138     return -4; /* Ignore this one. */
1139     }
1140 fabian 1.16 /* Might be necessary to prepend stored-up data! */
1141 segfault 1.1 if (socklist[ret].inbuf != NULL) {
1142     p = socklist[ret].inbuf;
1143     socklist[ret].inbuf = (char *) nmalloc(strlen(p) + strlen(xx) + 1);
1144     strcpy(socklist[ret].inbuf, p);
1145     strcat(socklist[ret].inbuf, xx);
1146     nfree(p);
1147     if (strlen(socklist[ret].inbuf) < 512) {
1148     strcpy(xx, socklist[ret].inbuf);
1149     nfree(socklist[ret].inbuf);
1150     socklist[ret].inbuf = NULL;
1151 fabian 1.25 socklist[ret].inbuflen = 0;
1152 segfault 1.1 } else {
1153     p = socklist[ret].inbuf;
1154 fabian 1.25 socklist[ret].inbuflen = strlen(p) - 510;
1155     socklist[ret].inbuf = (char *) nmalloc(socklist[ret].inbuflen + 1);
1156 segfault 1.1 strcpy(socklist[ret].inbuf, p + 510);
1157     *(p + 510) = 0;
1158     strcpy(xx, p);
1159     nfree(p);
1160     /* (leave the rest to be post-pended later) */
1161     }
1162     }
1163 fabian 1.16 /* Look for EOL marker; if it's there, i have something to show */
1164 segfault 1.1 p = strchr(xx, '\n');
1165     if (p == NULL)
1166     p = strchr(xx, '\r');
1167     if (p != NULL) {
1168     *p = 0;
1169     strcpy(s, xx);
1170     strcpy(xx, p + 1);
1171     if (s[strlen(s) - 1] == '\r')
1172     s[strlen(s) - 1] = 0;
1173     data = 1; /* DCC_CHAT may now need to process a
1174 fabian 1.16 blank line */
1175 segfault 1.1 /* NO! */
1176     /* if (!s[0]) strcpy(s," "); */
1177     } else {
1178     s[0] = 0;
1179     if (strlen(xx) >= 510) {
1180 fabian 1.16 /* String is too long, so just insert fake \n */
1181 segfault 1.1 strcpy(s, xx);
1182     xx[0] = 0;
1183     data = 1;
1184     }
1185     }
1186     *len = strlen(s);
1187 fabian 1.16 /* Anything left that needs to be saved? */
1188 segfault 1.1 if (!xx[0]) {
1189     if (data)
1190     return socklist[ret].sock;
1191     else
1192     return -3;
1193     }
1194 fabian 1.16 /* Prepend old data back */
1195 segfault 1.1 if (socklist[ret].inbuf != NULL) {
1196     p = socklist[ret].inbuf;
1197 fabian 1.25 socklist[ret].inbuflen = strlen(p) + strlen(xx);
1198     socklist[ret].inbuf = (char *) nmalloc(socklist[ret].inbuflen + 1);
1199 segfault 1.1 strcpy(socklist[ret].inbuf, xx);
1200     strcat(socklist[ret].inbuf, p);
1201     nfree(p);
1202     } else {
1203 fabian 1.25 socklist[ret].inbuflen = strlen(xx);
1204     socklist[ret].inbuf = (char *) nmalloc(socklist[ret].inbuflen + 1);
1205 segfault 1.1 strcpy(socklist[ret].inbuf, xx);
1206     }
1207     if (data) {
1208     return socklist[ret].sock;
1209     } else {
1210     return -3;
1211     }
1212     }
1213    
1214 fabian 1.16 /* Dump something to a socket
1215     *
1216     * NOTE: Do NOT put Contexts in here if you want DEBUG to be meaningful!!
1217     */
1218 fabian 1.12 void tputs(register int z, char *s, unsigned int len)
1219 segfault 1.1 {
1220 fabian 1.12 register int i, x, idx;
1221 segfault 1.1 char *p;
1222 fabian 1.12 static int inhere = 0;
1223 segfault 1.1
1224     if (z < 0)
1225     return; /* um... HELLO?! sanity check please! */
1226     if (((z == STDOUT) || (z == STDERR)) && (!backgrd || use_stderr)) {
1227     write(z, s, len);
1228     return;
1229     }
1230     for (i = 0; i < MAXSOCKS; i++) {
1231     if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == z)) {
1232 fabian 1.10 for (idx = 0; idx < dcc_total; idx++) {
1233     if (dcc[idx].sock == z) {
1234     if (dcc[idx].type) {
1235     if (dcc[idx].type->name) {
1236     if (!strncmp(dcc[idx].type->name, "BOT", 3)) {
1237     otraffic_bn_today += len;
1238     break;
1239     } else if (!strcmp(dcc[idx].type->name, "SERVER")) {
1240     otraffic_irc_today += len;
1241     break;
1242     } else if (!strncmp(dcc[idx].type->name, "CHAT", 4)) {
1243     otraffic_dcc_today += len;
1244     break;
1245     } else if (!strncmp(dcc[idx].type->name, "FILES", 5)) {
1246     otraffic_filesys_today += len;
1247     break;
1248     } else if (!strcmp(dcc[idx].type->name, "SEND")) {
1249     otraffic_trans_today += len;
1250     break;
1251     } else if (!strncmp(dcc[idx].type->name, "GET", 3)) {
1252     otraffic_trans_today += len;
1253     break;
1254     } else {
1255     otraffic_unknown_today += len;
1256     break;
1257     }
1258     }
1259     }
1260     }
1261     }
1262    
1263 drummer 1.36 if ((socklist[i].outbuf != NULL) || (socklist[i].flags & SOCK_PROXYWAIT)) {
1264     /* Already queueing or waiting for proxy: just add it */
1265 segfault 1.1 p = (char *) nrealloc(socklist[i].outbuf, socklist[i].outbuflen + len);
1266 fabian 1.18 egg_memcpy(p + socklist[i].outbuflen, s, len);
1267 segfault 1.1 socklist[i].outbuf = p;
1268     socklist[i].outbuflen += len;
1269     return;
1270     }
1271 fabian 1.16 /* Try. */
1272 segfault 1.1 x = write(z, s, len);
1273     if (x == (-1))
1274     x = 0;
1275     if (x < len) {
1276 fabian 1.16 /* Socket is full, queue it */
1277 segfault 1.1 socklist[i].outbuf = (char *) nmalloc(len - x);
1278 fabian 1.18 egg_memcpy(socklist[i].outbuf, &s[x], len - x);
1279 segfault 1.1 socklist[i].outbuflen = len - x;
1280     }
1281     return;
1282     }
1283     }
1284 fabian 1.12 /* Make sure we don't cause a crash by looping here */
1285     if (!inhere) {
1286     inhere = 1;
1287 fabian 1.16
1288 fabian 1.12 putlog(LOG_MISC, "*", "!!! writing to nonexistent socket: %d", z);
1289     s[strlen(s) - 1] = 0;
1290     putlog(LOG_MISC, "*", "!-> '%s'", s);
1291 fabian 1.16
1292 fabian 1.12 inhere = 0;
1293     }
1294 segfault 1.1 }
1295    
1296     /* tputs might queue data for sockets, let's dump as much of it as
1297 fabian 1.16 * possible.
1298     */
1299 segfault 1.1 void dequeue_sockets()
1300     {
1301     int i, x;
1302    
1303     for (i = 0; i < MAXSOCKS; i++) {
1304 drummer 1.36 if (!(socklist[i].flags & (SOCK_UNUSED | SOCK_PROXYWAIT)) &&
1305 fabian 1.22 socklist[i].outbuf != NULL) {
1306 fabian 1.16 /* Trick tputs into doing the work */
1307 segfault 1.1 x = write(socklist[i].sock, socklist[i].outbuf,
1308     socklist[i].outbuflen);
1309     if ((x < 0) && (errno != EAGAIN)
1310     #ifdef EBADSLT
1311     && (errno != EBADSLT)
1312     #endif
1313     #ifdef ENOTCONN
1314     && (errno != ENOTCONN)
1315     #endif
1316     ) {
1317 fabian 1.16 /* This detects an EOF during writing */
1318 segfault 1.1 debug3("net: eof!(write) socket %d (%s,%d)", socklist[i].sock,
1319     strerror(errno), errno);
1320     socklist[i].flags |= SOCK_EOFD;
1321     } else if (x == socklist[i].outbuflen) {
1322 fabian 1.16 /* If the whole buffer was sent, nuke it */
1323 segfault 1.1 nfree(socklist[i].outbuf);
1324     socklist[i].outbuf = NULL;
1325     socklist[i].outbuflen = 0;
1326     } else if (x > 0) {
1327     char *p = socklist[i].outbuf;
1328    
1329 fabian 1.16 /* This removes any sent bytes from the beginning of the buffer */
1330 segfault 1.1 socklist[i].outbuf = (char *) nmalloc(socklist[i].outbuflen - x);
1331 fabian 1.18 egg_memcpy(socklist[i].outbuf, p + x, socklist[i].outbuflen - x);
1332 segfault 1.1 socklist[i].outbuflen -= x;
1333     nfree(p);
1334     }
1335 fabian 1.15
1336     /* All queued data was sent. Call handler if one exists and the
1337     * dcc entry wants it.
1338     */
1339     if (!socklist[i].outbuf) {
1340     int idx = findanyidx(socklist[i].sock);
1341    
1342 fabian 1.22 if (idx > 0 && dcc[idx].type && dcc[idx].type->outdone)
1343 fabian 1.15 dcc[idx].type->outdone(idx);
1344     }
1345 segfault 1.1 }
1346     }
1347     }
1348 fabian 1.16
1349 segfault 1.1
1350 fabian 1.16 /*
1351     * Debugging stuff
1352     */
1353 segfault 1.1
1354     void tell_netdebug(int idx)
1355     {
1356     int i;
1357     char s[80];
1358    
1359     dprintf(idx, "Open sockets:");
1360     for (i = 0; i < MAXSOCKS; i++) {
1361     if (!(socklist[i].flags & SOCK_UNUSED)) {
1362     sprintf(s, " %d", socklist[i].sock);
1363     if (socklist[i].flags & SOCK_BINARY)
1364     strcat(s, " (binary)");
1365     if (socklist[i].flags & SOCK_LISTEN)
1366     strcat(s, " (listen)");
1367 fabian 1.3 if (socklist[i].flags & SOCK_PASS)
1368     strcat(s, " (passed on)");
1369 segfault 1.1 if (socklist[i].flags & SOCK_CONNECT)
1370     strcat(s, " (connecting)");
1371     if (socklist[i].flags & SOCK_STRONGCONN)
1372     strcat(s, " (strong)");
1373     if (socklist[i].flags & SOCK_NONSOCK)
1374     strcat(s, " (file)");
1375     if (socklist[i].inbuf != NULL)
1376     sprintf(&s[strlen(s)], " (inbuf: %04X)", strlen(socklist[i].inbuf));
1377     if (socklist[i].outbuf != NULL)
1378     sprintf(&s[strlen(s)], " (outbuf: %06lX)", socklist[i].outbuflen);
1379     strcat(s, ",");
1380     dprintf(idx, "%s", s);
1381     }
1382     }
1383     dprintf(idx, " done.\n");
1384     }
1385    
1386     /* Security-flavoured sanity checking on DCC connections of all sorts can be
1387     * done with this routine. Feed it the proper information from your DCC
1388     * before you attempt the connection, and this will make an attempt at
1389     * figuring out if the connection is really that person, or someone screwing
1390     * around. It's not foolproof, but anything that fails this check probably
1391     * isn't going to work anyway due to masquerading firewalls, NAT routers,
1392 fabian 1.16 * or bugs in mIRC.
1393     */
1394 segfault 1.1 int sanitycheck_dcc(char *nick, char *from, char *ipaddy, char *port)
1395     {
1396     /* According to the latest RFC, the clients SHOULD be able to handle
1397 fabian 1.16 * DNS names that are up to 255 characters long. This is not broken.
1398     */
1399 fabian 1.3 char badaddress[16];
1400 segfault 1.1 IP ip = my_atoul(ipaddy);
1401     int prt = atoi(port);
1402    
1403     /* It is disabled HERE so we only have to check in *one* spot! */
1404     if (!dcc_sanitycheck)
1405     return 1;
1406     if (prt < 1) {
1407     putlog(LOG_MISC, "*", "ALERT: (%s!%s) specified an impossible port of %u!",
1408     nick, from, prt);
1409     return 0;
1410     }
1411 fabian 1.3 sprintf(badaddress, "%u.%u.%u.%u", (ip >> 24) & 0xff, (ip >> 16) & 0xff,
1412     (ip >> 8) & 0xff, ip & 0xff);
1413 segfault 1.1 if (ip < (1 << 24)) {
1414     putlog(LOG_MISC, "*", "ALERT: (%s!%s) specified an impossible IP of %s!",
1415     nick, from, badaddress);
1416     return 0;
1417     }
1418 fabian 1.3 return 1;
1419     }
1420    
1421     int hostsanitycheck_dcc(char *nick, char *from, IP ip, char *dnsname,
1422     char *prt)
1423     {
1424     /* According to the latest RFC, the clients SHOULD be able to handle
1425 fabian 1.16 * DNS names that are up to 255 characters long. This is not broken.
1426     */
1427 fabian 1.23 char hostn[256], badaddress[16];
1428 fabian 1.3
1429     /* It is disabled HERE so we only have to check in *one* spot! */
1430     if (!dcc_sanitycheck)
1431     return 1;
1432     sprintf(badaddress, "%u.%u.%u.%u", (ip >> 24) & 0xff, (ip >> 16) & 0xff,
1433     (ip >> 8) & 0xff, ip & 0xff);
1434 segfault 1.1 /* These should pad like crazy with zeros, since 120 bytes or so is
1435     * where the routines providing our data currently lose interest. I'm
1436 fabian 1.16 * using the n-variant in case someone changes that...
1437     */
1438 guppy 1.27 strncpyz(hostn, extracthostname(from), sizeof hostn);
1439 fabian 1.23 if (!egg_strcasecmp(hostn, dnsname)) {
1440 segfault 1.1 putlog(LOG_DEBUG, "*", "DNS information for submitted IP checks out.");
1441     return 1;
1442     }
1443     if (!strcmp(badaddress, dnsname))
1444 fabian 1.23 putlog(LOG_MISC, "*", "ALERT: (%s!%s) sent a DCC request with bogus IP "
1445 guppy 1.32 "information of %s port %s. %s does not resolve to %s!", nick, from,
1446     badaddress, prt, from, badaddress);
1447 segfault 1.1 else
1448     return 1; /* <- usually happens when we have
1449 fabian 1.16 a user with an unresolved hostmask! */
1450 segfault 1.1 return 0;
1451 fabian 1.15 }
1452    
1453     /* Checks wether the referenced socket has data queued.
1454     *
1455     * Returns true if the incoming/outgoing (depending on 'type') queues
1456     * contain data, otherwise false.
1457     */
1458     int sock_has_data(int type, int sock)
1459     {
1460     int ret = 0, i;
1461    
1462     for (i = 0; i < MAXSOCKS; i++)
1463 fabian 1.22 if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
1464 fabian 1.15 break;
1465     if (i < MAXSOCKS) {
1466     switch (type) {
1467     case SOCK_DATA_OUTGOING:
1468     ret = (socklist[i].outbuf != NULL);
1469     break;
1470     case SOCK_DATA_INCOMING:
1471     ret = (socklist[i].inbuf != NULL);
1472     break;
1473     }
1474 fabian 1.22 } else
1475     debug1("sock_has_data: could not find socket #%d, returning false.", sock);
1476 fabian 1.15 return ret;
1477 poptix 1.30 }
1478    
1479     /* flush_inbuf():
1480     * checks if there's data in the incoming buffer of an connection
1481     * and flushs the buffer if possible
1482     *
1483     * returns: -1 if the dcc entry wasn't found
1484     * -2 if dcc[idx].type->activity doesn't exist and the data couldn't
1485     * be handled
1486     * 0 if buffer was empty
1487     * otherwise length of flushed buffer
1488     */
1489     int flush_inbuf(int idx)
1490     {
1491     int i, len;
1492     char *inbuf;
1493    
1494     Assert((idx >= 0) && (idx < dcc_total));
1495     for (i = 0; i < MAXSOCKS; i++) {
1496     if ((dcc[idx].sock == socklist[i].sock)
1497     && !(socklist[i].flags & SOCK_UNUSED)) {
1498     len = socklist[i].inbuflen;
1499     if ((len > 0) && socklist[i].inbuf) {
1500     if (dcc[idx].type && dcc[idx].type->activity) {
1501     inbuf = socklist[i].inbuf;
1502     socklist[i].inbuf = NULL;
1503     dcc[idx].type->activity(idx, inbuf, len);
1504     nfree(inbuf);
1505     return len;
1506     } else
1507     return -2;
1508     } else
1509     return 0;
1510     }
1511     }
1512     return -1;
1513 segfault 1.1 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23