/[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.13 - (show annotations) (download) (as text)
Thu Jan 6 19:45:03 2000 UTC (19 years, 6 months ago) by fabian
Branch: MAIN
Changes since 1.12: +5 -3 lines
File MIME type: text/x-chdr
errors_fix, simialer to 1.4

1 /*
2 * net.c -- handles:
3 * all raw network i/o
4 *
5 * $Id: net.c,v 1.12 1999/12/22 12:11:02 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;
441 unsigned int addrlen;
442 struct sockaddr_in name;
443
444 if (firewall[0]) {
445 /* FIXME: can't do listen port thru firewall yet */
446 putlog(LOG_MISC, "*", "!! Cant open a listen port (you are using a firewall)");
447 return -1;
448 }
449 sock = getsock(SOCK_LISTEN);
450 bzero((char *) &name, sizeof(struct sockaddr_in));
451
452 name.sin_family = AF_INET;
453 name.sin_port = my_htons(*port); /* 0 = just assign us a port */
454 name.sin_addr.s_addr = (myip[0] ? getmyip() : INADDR_ANY);
455 if (bind(sock, (struct sockaddr *) &name, sizeof(name)) < 0) {
456 killsock(sock);
457 return -1;
458 }
459 /* what port are we on? */
460 addrlen = sizeof(name);
461 if (getsockname(sock, (struct sockaddr *) &name, &addrlen) < 0) {
462 killsock(sock);
463 return -1;
464 }
465 *port = my_ntohs(name.sin_port);
466 if (listen(sock, 1) < 0) {
467 killsock(sock);
468 return -1;
469 }
470 return sock;
471 }
472
473 /* Given a network-style IP address, returns the hostname. The hostname
474 * will be in the "##.##.##.##" format if there was an error.
475 *
476 * NOTE: This function is depreciated. Try using the async dns approach
477 * instead. */
478 char *hostnamefromip(unsigned long ip)
479 {
480 struct hostent *hp;
481 unsigned long addr = ip;
482 unsigned char *p;
483 static char s[UHOSTLEN];
484
485 if (!setjmp(alarmret)) {
486 alarm(resolve_timeout);
487 hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
488 alarm(0);
489 } else {
490 hp = NULL;
491 }
492 if (hp == NULL) {
493 p = (unsigned char *) &addr;
494 sprintf(s, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
495 return s;
496 }
497 strncpy(s, hp->h_name, UHOSTLEN - 1);
498 s[UHOSTLEN - 1] = 0;
499 return s;
500 }
501
502 /* Returns the given network byte order IP address in the
503 * dotted format - "##.##.##.##" */
504 char *iptostr(IP ip)
505 {
506 struct in_addr a;
507
508 a.s_addr = ip;
509 return inet_ntoa(a);
510 }
511
512 /* short routine to answer a connect received on a socket made previously
513 * by open_listen ... returns hostname of the caller & the new socket
514 * does NOT dispose of old "public" socket! */
515 int answer(int sock, char *caller, unsigned long *ip,
516 unsigned short *port, int binary)
517 {
518 int new_sock;
519 unsigned int addrlen;
520 struct sockaddr_in from;
521 addrlen = sizeof(struct sockaddr);
522
523 new_sock = accept(sock, (struct sockaddr *) &from, &addrlen);
524 if (new_sock < 0)
525 return -1;
526 if (ip != NULL) {
527 *ip = from.sin_addr.s_addr;
528 /* This is now done asynchronously. Instead, we just provide the
529 * IP address.
530 * strncpy(caller, hostnamefromip(*ip), 120);
531 */
532 strncpy(caller, iptostr(*ip), 120);
533 caller[120] = 0;
534 *ip = my_ntohl(*ip);
535 }
536 if (port != NULL)
537 *port = my_ntohs(from.sin_port);
538 /* set up all the normal socket crap */
539 setsock(new_sock, (binary ? SOCK_BINARY : 0));
540 return new_sock;
541 }
542
543 /* like open_telnet, but uses server & port specifications of dcc */
544 int open_telnet_dcc(int sock, char *server, char *port)
545 {
546 int p;
547 unsigned long addr;
548 char sv[121];
549 unsigned char c[4];
550
551 if (port != NULL)
552 p = atoi(port);
553 else
554 p = 2000;
555 if (server != NULL)
556 addr = my_atoul(server);
557 else
558 addr = 0L;
559 if (addr < (1 << 24))
560 return -3; /* fake address */
561 c[0] = (addr >> 24) & 0xff;
562 c[1] = (addr >> 16) & 0xff;
563 c[2] = (addr >> 8) & 0xff;
564 c[3] = addr & 0xff;
565 sprintf(sv, "%u.%u.%u.%u", c[0], c[1], c[2], c[3]);
566 /* strcpy(sv,hostnamefromip(addr)); */
567 p = open_telnet_raw(sock, sv, p);
568 return p;
569 }
570
571 /* all new replacements for mtgets/mtread */
572
573 /* attempts to read from all the sockets in socklist
574 * fills s with up to 511 bytes if available, and returns the array index
575 * on EOF, returns -1, with socket in len
576 * on socket error, returns -2
577 * if nothing is ready, returns -3 */
578 static int sockread(char *s, int *len)
579 {
580 fd_set fd;
581 int fds, i, x;
582 struct timeval t;
583 int grab = 511;
584
585 fds = getdtablesize();
586 #ifdef FD_SETSIZE
587 if (fds > FD_SETSIZE)
588 fds = FD_SETSIZE; /* fixes YET ANOTHER freebsd bug!!! */
589 #endif
590 /* timeout: 1 sec */
591 t.tv_sec = 1;
592 t.tv_usec = 0;
593 FD_ZERO(&fd);
594 for (i = 0; i < MAXSOCKS; i++)
595 if (!(socklist[i].flags & SOCK_UNUSED)) {
596 if ((socklist[i].sock == STDOUT) && !backgrd)
597 FD_SET(STDIN, &fd);
598 else
599 FD_SET(socklist[i].sock, &fd);
600 }
601 #ifdef HPUX_HACKS
602 #ifndef HPUX10_HACKS
603 x = select(fds, (int *) &fd, (int *) NULL, (int *) NULL, &t);
604 #else
605 x = select(fds, &fd, NULL, NULL, &t);
606 #endif
607 #else
608 x = select(fds, &fd, NULL, NULL, &t);
609 #endif
610 if (x > 0) {
611 /* something happened */
612 for (i = 0; i < MAXSOCKS; i++) {
613 if ((!(socklist[i].flags & SOCK_UNUSED)) &&
614 ((FD_ISSET(socklist[i].sock, &fd)) ||
615 ((socklist[i].sock == STDOUT) && (!backgrd) &&
616 (FD_ISSET(STDIN, &fd))))) {
617 if (socklist[i].flags & (SOCK_LISTEN | SOCK_CONNECT)) {
618 /* listening socket -- don't read, just return activity */
619 /* same for connection attempt */
620 /* (for strong connections, require a read to succeed first) */
621 if (socklist[i].flags & SOCK_PROXYWAIT) { /* drummer */
622 /* hang around to get the return code from proxy */
623 grab = 10;
624 } else if (!(socklist[i].flags & SOCK_STRONGCONN)) {
625 debug1("net: connect! sock %d", socklist[i].sock);
626 s[0] = 0;
627 *len = 0;
628 return i;
629 }
630 } else if (socklist[i].flags & SOCK_PASS) {
631 s[0] = 0;
632 *len = 0;
633 return i;
634 }
635 if ((socklist[i].sock == STDOUT) && !backgrd)
636 x = read(STDIN, s, grab);
637 else
638 x = read(socklist[i].sock, s, grab);
639 if (x <= 0) { /* eof */
640 if (x == EAGAIN) {
641 s[0] = 0;
642 *len = 0;
643 return -3;
644 }
645 *len = socklist[i].sock;
646 socklist[i].flags &= ~SOCK_CONNECT;
647 debug1("net: eof!(read) socket %d", socklist[i].sock);
648 return -1;
649 }
650 s[x] = 0;
651 *len = x;
652 if (socklist[i].flags & SOCK_PROXYWAIT) {
653 debug2("net: socket: %d proxy errno: %d", socklist[i].sock, s[1]);
654 socklist[i].flags &= ~(SOCK_CONNECT | SOCK_PROXYWAIT);
655 switch (s[1]) {
656 case 90: /* success */
657 s[0] = 0;
658 *len = 0;
659 return i;
660 case 91: /* failed */
661 errno = ECONNREFUSED;
662 break;
663 case 92: /* no identd */
664 case 93: /* identd said wrong username */
665 errno = ENETUNREACH;
666 break;
667 /* a better error message would be "socks misconfigured" */
668 /* or "identd not working" but this is simplest */
669 }
670 *len = socklist[i].sock;
671 return -1;
672 }
673 return i;
674 }
675 }
676 } else if (x == -1)
677 return -2; /* socket error */
678 else {
679 s[0] = 0;
680 *len = 0;
681 }
682 return -3;
683 }
684
685 /* sockgets: buffer and read from sockets
686 *
687 * attempts to read from all registered sockets for up to one second. if
688 * after one second, no complete data has been received from any of the
689 * sockets, 's' will be empty, 'len' will be 0, and sockgets will return -3.
690 * if there is returnable data received from a socket, the data will be
691 * in 's' (null-terminated if non-binary), the length will be returned
692 * in len, and the socket number will be returned.
693 * normal sockets have their input buffered, and each call to sockgets
694 * will return one line terminated with a '\n'. binary sockets are not
695 * buffered and return whatever coems in as soon as it arrives.
696 * listening sockets will return an empty string when a connection comes in.
697 * connecting sockets will return an empty string on a successful connect,
698 * or EOF on a failed connect.
699 * if an EOF is detected from any of the sockets, that socket number will be
700 * put in len, and -1 will be returned.
701 * the maximum length of the string returned is 512 (including null)
702 */
703
704 int sockgets(char *s, int *len)
705 {
706 char xx[514], *p, *px;
707 int ret, i, data = 0;
708
709 Context;
710 for (i = 0; i < MAXSOCKS; i++) {
711 /* check for stored-up data waiting to be processed */
712 if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].inbuf != NULL)) {
713 /* look for \r too cos windows can't follow RFCs */
714 p = strchr(socklist[i].inbuf, '\n');
715 if (p == NULL)
716 p = strchr(socklist[i].inbuf, '\r');
717 if (p != NULL) {
718 *p = 0;
719 if (strlen(socklist[i].inbuf) > 510)
720 socklist[i].inbuf[510] = 0;
721 strcpy(s, socklist[i].inbuf);
722 px = (char *) nmalloc(strlen(p + 1) + 1);
723 strcpy(px, p + 1);
724 nfree(socklist[i].inbuf);
725 if (px[0])
726 socklist[i].inbuf = px;
727 else {
728 nfree(px);
729 socklist[i].inbuf = NULL;
730 }
731 /* strip CR if this was CR/LF combo */
732 if (s[strlen(s) - 1] == '\r')
733 s[strlen(s) - 1] = 0;
734 *len = strlen(s);
735 return socklist[i].sock;
736 }
737 }
738 /* also check any sockets that might have EOF'd during write */
739 if (!(socklist[i].flags & SOCK_UNUSED)
740 && (socklist[i].flags & SOCK_EOFD)) {
741 Context;
742 s[0] = 0;
743 *len = socklist[i].sock;
744 return -1;
745 }
746 }
747 /* no pent-up data of any worth -- down to business */
748 Context;
749 *len = 0;
750 ret = sockread(xx, len);
751 if (ret < 0) {
752 s[0] = 0;
753 return ret;
754 }
755 /* binary and listening sockets don't get buffered */
756 /* passed on sockets don't get buffered either (Fabian)*/
757 if (socklist[ret].flags & SOCK_CONNECT) {
758 if (socklist[ret].flags & SOCK_STRONGCONN) {
759 socklist[ret].flags &= ~SOCK_STRONGCONN;
760 /* buffer any data that came in, for future read */
761 socklist[ret].inbuf = (char *) nmalloc(strlen(xx) + 1);
762 strcpy(socklist[ret].inbuf, xx);
763 }
764 socklist[ret].flags &= ~SOCK_CONNECT;
765 s[0] = 0;
766 return socklist[ret].sock;
767 }
768 if (socklist[ret].flags & SOCK_BINARY) {
769 my_memcpy(s, xx, *len);
770 return socklist[ret].sock;
771 }
772 if ((socklist[ret].flags & SOCK_LISTEN) ||
773 (socklist[ret].flags & SOCK_PASS))
774 return socklist[ret].sock;
775 Context;
776 /* might be necessary to prepend stored-up data! */
777 if (socklist[ret].inbuf != NULL) {
778 p = socklist[ret].inbuf;
779 socklist[ret].inbuf = (char *) nmalloc(strlen(p) + strlen(xx) + 1);
780 strcpy(socklist[ret].inbuf, p);
781 strcat(socklist[ret].inbuf, xx);
782 nfree(p);
783 if (strlen(socklist[ret].inbuf) < 512) {
784 strcpy(xx, socklist[ret].inbuf);
785 nfree(socklist[ret].inbuf);
786 socklist[ret].inbuf = NULL;
787 } else {
788 p = socklist[ret].inbuf;
789 socklist[ret].inbuf = (char *) nmalloc(strlen(p) - 509);
790 strcpy(socklist[ret].inbuf, p + 510);
791 *(p + 510) = 0;
792 strcpy(xx, p);
793 nfree(p);
794 /* (leave the rest to be post-pended later) */
795 }
796 }
797 Context;
798 /* look for EOL marker; if it's there, i have something to show */
799 p = strchr(xx, '\n');
800 if (p == NULL)
801 p = strchr(xx, '\r');
802 if (p != NULL) {
803 *p = 0;
804 strcpy(s, xx);
805 strcpy(xx, p + 1);
806 if (s[strlen(s) - 1] == '\r')
807 s[strlen(s) - 1] = 0;
808 data = 1; /* DCC_CHAT may now need to process a
809 * blank line */
810 /* NO! */
811 /* if (!s[0]) strcpy(s," "); */
812 } else {
813 s[0] = 0;
814 if (strlen(xx) >= 510) {
815 /* string is too long, so just insert fake \n */
816 strcpy(s, xx);
817 xx[0] = 0;
818 data = 1;
819 }
820 }
821 Context;
822 *len = strlen(s);
823 /* anything left that needs to be saved? */
824 if (!xx[0]) {
825 if (data)
826 return socklist[ret].sock;
827 else
828 return -3;
829 }
830 Context;
831 /* prepend old data back */
832 if (socklist[ret].inbuf != NULL) {
833 Context;
834 p = socklist[ret].inbuf;
835 socklist[ret].inbuf = (char *) nmalloc(strlen(p) + strlen(xx) + 1);
836 strcpy(socklist[ret].inbuf, xx);
837 strcat(socklist[ret].inbuf, p);
838 nfree(p);
839 } else {
840 Context;
841 socklist[ret].inbuf = (char *) nmalloc(strlen(xx) + 1);
842 strcpy(socklist[ret].inbuf, xx);
843 }
844 Context;
845 if (data) {
846 Context;
847 return socklist[ret].sock;
848 } else {
849 Context;
850 return -3;
851 }
852 }
853
854 /* dump something to a socket */
855 /* DO NOT PUT CONTEXTS IN HERE IF YOU WANT DEBUG TO BE MEANINGFUL!!! */
856 void tputs(register int z, char *s, unsigned int len)
857 {
858 register int i, x, idx;
859 char *p;
860 static int inhere = 0;
861
862 if (z < 0)
863 return; /* um... HELLO?! sanity check please! */
864 if (((z == STDOUT) || (z == STDERR)) && (!backgrd || use_stderr)) {
865 write(z, s, len);
866 return;
867 }
868 for (i = 0; i < MAXSOCKS; i++) {
869 if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == z)) {
870
871 for (idx = 0; idx < dcc_total; idx++) {
872 if (dcc[idx].sock == z) {
873 if (dcc[idx].type) {
874 if (dcc[idx].type->name) {
875 if (!strncmp(dcc[idx].type->name, "BOT", 3)) {
876 otraffic_bn_today += len;
877 break;
878 } else if (!strcmp(dcc[idx].type->name, "SERVER")) {
879 otraffic_irc_today += len;
880 break;
881 } else if (!strncmp(dcc[idx].type->name, "CHAT", 4)) {
882 otraffic_dcc_today += len;
883 break;
884 } else if (!strncmp(dcc[idx].type->name, "FILES", 5)) {
885 otraffic_filesys_today += len;
886 break;
887 } else if (!strcmp(dcc[idx].type->name, "SEND")) {
888 otraffic_trans_today += len;
889 break;
890 } else if (!strncmp(dcc[idx].type->name, "GET", 3)) {
891 otraffic_trans_today += len;
892 break;
893 } else {
894 otraffic_unknown_today += len;
895 break;
896 }
897 }
898 }
899 }
900 }
901
902 if (socklist[i].outbuf != NULL) {
903 /* already queueing: just add it */
904 p = (char *) nrealloc(socklist[i].outbuf, socklist[i].outbuflen + len);
905 my_memcpy(p + socklist[i].outbuflen, s, len);
906 socklist[i].outbuf = p;
907 socklist[i].outbuflen += len;
908 return;
909 }
910 /* try. */
911 x = write(z, s, len);
912 if (x == (-1))
913 x = 0;
914 if (x < len) {
915 /* socket is full, queue it */
916 socklist[i].outbuf = (char *) nmalloc(len - x);
917 my_memcpy(socklist[i].outbuf, &s[x], len - x);
918 socklist[i].outbuflen = len - x;
919 }
920 return;
921 }
922 }
923 /* Make sure we don't cause a crash by looping here */
924 if (!inhere) {
925 inhere = 1;
926 putlog(LOG_MISC, "*", "!!! writing to nonexistent socket: %d", z);
927 s[strlen(s) - 1] = 0;
928 putlog(LOG_MISC, "*", "!-> '%s'", s);
929 inhere = 0;
930 }
931 }
932
933 /* tputs might queue data for sockets, let's dump as much of it as
934 * possible */
935 void dequeue_sockets()
936 {
937 int i, x;
938
939 for (i = 0; i < MAXSOCKS; i++) {
940 if (!(socklist[i].flags & SOCK_UNUSED) &&
941 (socklist[i].outbuf != NULL)) {
942 /* trick tputs into doing the work */
943 x = write(socklist[i].sock, socklist[i].outbuf,
944 socklist[i].outbuflen);
945 if ((x < 0) && (errno != EAGAIN)
946 #ifdef EBADSLT
947 && (errno != EBADSLT)
948 #endif
949 #ifdef ENOTCONN
950 && (errno != ENOTCONN)
951 #endif
952 ) {
953 /* this detects an EOF during writing */
954 debug3("net: eof!(write) socket %d (%s,%d)", socklist[i].sock,
955 strerror(errno), errno);
956 socklist[i].flags |= SOCK_EOFD;
957 } else if (x == socklist[i].outbuflen) {
958 /* if the whole buffer was sent, nuke it */
959 nfree(socklist[i].outbuf);
960 socklist[i].outbuf = NULL;
961 socklist[i].outbuflen = 0;
962 } else if (x > 0) {
963 char *p = socklist[i].outbuf;
964
965 /* this removes any sent bytes from the beginning of the buffer */
966 socklist[i].outbuf = (char *) nmalloc(socklist[i].outbuflen - x);
967 my_memcpy(socklist[i].outbuf, p + x, socklist[i].outbuflen - x);
968 socklist[i].outbuflen -= x;
969 nfree(p);
970 }
971 }
972 }
973 }
974
975 /* DEBUGGING STUFF */
976
977 void tell_netdebug(int idx)
978 {
979 int i;
980 char s[80];
981
982 dprintf(idx, "Open sockets:");
983 for (i = 0; i < MAXSOCKS; i++) {
984 if (!(socklist[i].flags & SOCK_UNUSED)) {
985 sprintf(s, " %d", socklist[i].sock);
986 if (socklist[i].flags & SOCK_BINARY)
987 strcat(s, " (binary)");
988 if (socklist[i].flags & SOCK_LISTEN)
989 strcat(s, " (listen)");
990 if (socklist[i].flags & SOCK_PASS)
991 strcat(s, " (passed on)");
992 if (socklist[i].flags & SOCK_CONNECT)
993 strcat(s, " (connecting)");
994 if (socklist[i].flags & SOCK_STRONGCONN)
995 strcat(s, " (strong)");
996 if (socklist[i].flags & SOCK_NONSOCK)
997 strcat(s, " (file)");
998 if (socklist[i].inbuf != NULL)
999 sprintf(&s[strlen(s)], " (inbuf: %04X)", strlen(socklist[i].inbuf));
1000 if (socklist[i].outbuf != NULL)
1001 sprintf(&s[strlen(s)], " (outbuf: %06lX)", socklist[i].outbuflen);
1002 strcat(s, ",");
1003 dprintf(idx, "%s", s);
1004 }
1005 }
1006 dprintf(idx, " done.\n");
1007 }
1008
1009 /* Security-flavoured sanity checking on DCC connections of all sorts can be
1010 * done with this routine. Feed it the proper information from your DCC
1011 * before you attempt the connection, and this will make an attempt at
1012 * figuring out if the connection is really that person, or someone screwing
1013 * around. It's not foolproof, but anything that fails this check probably
1014 * isn't going to work anyway due to masquerading firewalls, NAT routers,
1015 * or bugs in mIRC. */
1016 int sanitycheck_dcc(char *nick, char *from, char *ipaddy, char *port)
1017 {
1018 /* According to the latest RFC, the clients SHOULD be able to handle
1019 * DNS names that are up to 255 characters long. This is not broken. */
1020 char badaddress[16];
1021 IP ip = my_atoul(ipaddy);
1022 int prt = atoi(port);
1023
1024 /* It is disabled HERE so we only have to check in *one* spot! */
1025 if (!dcc_sanitycheck)
1026 return 1;
1027 Context; /* This should be pretty solid, but
1028 * something _might_ break. */
1029 if (prt < 1) {
1030 putlog(LOG_MISC, "*", "ALERT: (%s!%s) specified an impossible port of %u!",
1031 nick, from, prt);
1032 return 0;
1033 }
1034 sprintf(badaddress, "%u.%u.%u.%u", (ip >> 24) & 0xff, (ip >> 16) & 0xff,
1035 (ip >> 8) & 0xff, ip & 0xff);
1036 if (ip < (1 << 24)) {
1037 putlog(LOG_MISC, "*", "ALERT: (%s!%s) specified an impossible IP of %s!",
1038 nick, from, badaddress);
1039 return 0;
1040 }
1041 return 1;
1042 }
1043
1044 int hostsanitycheck_dcc(char *nick, char *from, IP ip, char *dnsname,
1045 char *prt)
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 char hostname[256], badaddress[16];
1050
1051 /* It is disabled HERE so we only have to check in *one* spot! */
1052 if (!dcc_sanitycheck)
1053 return 1;
1054 Context; /* This should be pretty solid, but
1055 * something _might_ break. */
1056 sprintf(badaddress, "%u.%u.%u.%u", (ip >> 24) & 0xff, (ip >> 16) & 0xff,
1057 (ip >> 8) & 0xff, ip & 0xff);
1058 /* These should pad like crazy with zeros, since 120 bytes or so is
1059 * where the routines providing our data currently lose interest. I'm
1060 * using the n-variant in case someone changes that... */
1061 strncpy(hostname, extracthostname(from), 255);
1062 hostname[255] = 0;
1063 if (!strcasecmp(hostname, dnsname)) {
1064 putlog(LOG_DEBUG, "*", "DNS information for submitted IP checks out.");
1065 return 1;
1066 }
1067 if (!strcmp(badaddress, dnsname))
1068 putlog(LOG_MISC, "*", "ALERT: (%s!%s) sent a DCC request with bogus IP information of %s port %u!",
1069 nick, from, badaddress, prt);
1070 else
1071 return 1; /* <- usually happens when we have
1072 a user with an unresolved hostmask! */
1073 return 0;
1074 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23