/[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.29 - (show annotations) (download) (as text)
Sun Feb 25 07:33:35 2001 UTC (18 years, 3 months ago) by guppy
Branch: MAIN
Changes since 1.28: +5 -2 lines
File MIME type: text/x-chdr
this adds Habeeb's patch which gives the user a more formal answer if their IP is set incorrectly instead of saying it cannot grab a nearby port

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23