/[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.12 - (show annotations) (download) (as text)
Wed Dec 22 12:11:02 1999 UTC (19 years, 6 months ago) by fabian
Branch: MAIN
Changes since 1.11: +12 -6 lines
File MIME type: text/x-chdr
resync with 1.4.2, 1999/12/22

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23