/[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.24 - (show annotations) (download) (as text)
Fri Oct 27 19:34:54 2000 UTC (18 years, 11 months ago) by fabian
Branch: MAIN
Changes since 1.23: +14 -7 lines
File MIME type: text/x-chdr
fabian: applied net_sockfail_nonfatal patch

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23