/[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.15 - (show annotations) (download) (as text)
Sat Jan 22 22:37:47 2000 UTC (19 years, 6 months ago) by fabian
Branch: MAIN
Changes since 1.14: +36 -1 lines
File MIME type: text/x-chdr
uff and outdone_event patch

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23