/[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.17 - (show annotations) (download) (as text)
Mon Mar 6 19:10:12 2000 UTC (19 years, 4 months ago) by fabian
Branch: MAIN
Changes since 1.16: +4 -3 lines
File MIME type: text/x-chdr
drummer's relaydns patch

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23