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

Contents of /eggdrop1.9/src/net.c

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


Revision 1.35 - (show annotations) (download) (as text)
Wed Aug 1 16:31:41 2001 UTC (18 years, 2 months ago) by drummer
Branch: MAIN
Changes since 1.34: +3 -3 lines
File MIME type: text/x-chdr
freebsd warnings part2

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23