/[cvs]/eggdrop1.9/src/net.c
ViewVC logotype

Annotation of /eggdrop1.9/src/net.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.55 - (hide annotations) (download) (as text)
Mon Apr 1 13:33:33 2002 UTC (17 years, 6 months ago) by ite
Branch: MAIN
Changes since 1.54: +5 -5 lines
File MIME type: text/x-chdr
* Got rid of strncpyz() in favour of BSD's strlcpy(). Fallback function provided.
* BSD's strlcat() provided as fallback function.
* Fallback functions weren't linked as expected when needed.

1 fabian 1.11 /*
2 segfault 1.1 * net.c -- handles:
3 fabian 1.11 * all raw network i/o
4 segfault 1.1 *
5 ite 1.55 * $Id: net.c,v 1.54 2002/03/05 19:53:48 stdarg Exp $
6 fabian 1.11 */
7     /*
8 segfault 1.1 * This is hereby released into the public domain.
9     * Robey Pointer, robey@netcom.com
10     */
11    
12 fabian 1.16 #include <fcntl.h>
13 segfault 1.1 #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 fabian 1.16 # include <sys/select.h>
20 segfault 1.1 #endif
21     #include <netinet/in.h>
22 drummer 1.39 #include <arpa/inet.h>
23 segfault 1.1 #include <errno.h>
24     #if HAVE_UNISTD_H
25 fabian 1.16 # include <unistd.h>
26 segfault 1.1 #endif
27     #include <setjmp.h>
28    
29 ite 1.49 #include "lib/adns/adns.h"
30 stdarg 1.46 #include "egg_timer.h"
31 ite 1.53 #include "logfile.h"
32     #include "dns.h"
33 drummer 1.33
34 segfault 1.1 #if !HAVE_GETDTABLESIZE
35 fabian 1.16 # ifdef FD_SETSIZE
36     # define getdtablesize() FD_SETSIZE
37     # else
38     # define getdtablesize() 200
39     # endif
40 segfault 1.1 #endif
41    
42 drummer 1.33 #ifndef IN6_IS_ADDR_V4MAPPED
43     # define IN6_IS_ADDR_V4MAPPED(a) \
44     ((((uint32_t *) (a))[0] == 0) && (((uint32_t *) (a))[1] == 0) && \
45     (((uint32_t *) (a))[2] == htonl (0xffff)))
46     #endif
47    
48 fabian 1.16 extern struct dcc_t *dcc;
49 guppy 1.28 extern int backgrd, use_stderr, resolve_timeout, dcc_total;
50     extern unsigned long otraffic_irc_today, otraffic_bn_today,
51 fabian 1.16 otraffic_dcc_today, otraffic_filesys_today,
52     otraffic_trans_today, otraffic_unknown_today;
53 drummer 1.33 extern char natip[];
54 fabian 1.16
55     char myip[121] = ""; /* IP can be specified in the config file */
56 drummer 1.33 char myip6[121] = ""; /* IP6 can be specified in the config file */
57 fabian 1.16 char firewall[121] = ""; /* Socks server for firewall */
58     int firewallport = 1080; /* Default port of Sock4/5 firewalls */
59     char botuser[21] = "eggdrop"; /* Username of the user running the bot */
60     sock_list *socklist = NULL; /* Enough to be safe */
61     int MAXSOCKS = 0;
62     jmp_buf alarmret; /* Env buffer for alarm() returns */
63 drummer 1.40 IP localipv4addr = 0; /* Cache the local IPv4 address */
64 segfault 1.1
65 drummer 1.33 adns_state ads;
66    
67 fabian 1.16 /* Types of proxy */
68 segfault 1.1 #define PROXY_SOCKS 1
69     #define PROXY_SUN 2
70    
71 fabian 1.16 /* Initialize the socklist
72     */
73 segfault 1.1 void init_net()
74     {
75     int i;
76 drummer 1.40 char s[256];
77     struct hostent *hp;
78 segfault 1.1
79 stdarg 1.44 socklist = (sock_list *)calloc(MAXSOCKS, sizeof(sock_list));
80 segfault 1.1 for (i = 0; i < MAXSOCKS; i++) {
81     socklist[i].flags = SOCK_UNUSED;
82     }
83 drummer 1.33
84 drummer 1.40 gethostname(s, sizeof s);
85     if ((hp = gethostbyname(s)) == NULL)
86     fatal("Hostname self-lookup failed.", 0);
87     localipv4addr = *((IP*) (hp->h_addr_list[0]));
88    
89 drummer 1.33 /* init ADNS */
90     i = adns_init(&ads, adns_if_noautosys, 0);
91     if (i)
92     fatal(adns_strerror(i), 0);
93 segfault 1.1 }
94    
95 fabian 1.16 /* Get my ip number
96     */
97 segfault 1.1 IP getmyip()
98     {
99 fabian 1.16 /* Could be pre-defined */
100 drummer 1.40 if (myip[0] && (myip[strlen(myip) - 1] >= '0') &&
101     (myip[strlen(myip) - 1] <= '9')) {
102 segfault 1.1 return (IP) inet_addr(myip);
103     }
104 drummer 1.40 return localipv4addr;
105 segfault 1.1 }
106    
107 drummer 1.33 #ifdef IPV6
108     struct in6_addr getmyip6()
109     {
110     struct in6_addr ip;
111    
112     /* Could be pre-defined */
113     if (myip6[0])
114 tothwolf 1.48 inet_pton(AF_INET6, myip6, &ip);
115 drummer 1.33 else {
116 drummer 1.40 /* get system's default IPv6 ip -- FIXME!? */
117     /* is there a know way?! - drummer */
118 drummer 1.33 ip = in6addr_any;
119     }
120     return ip;
121     }
122    
123     struct in6_addr ipv4to6(IP a)
124     {
125     struct in6_addr ip;
126     if (a == INADDR_ANY)
127     return in6addr_any;
128     else {
129     ((uint32_t *)&ip)[0] = 0;
130     ((uint32_t *)&ip)[1] = 0;
131     ((uint16_t *)&ip)[4] = 0;
132     ((uint16_t *)&ip)[5] = 0xffff;
133     ((uint32_t *)&ip)[3] = a;
134     }
135     return ip;
136     }
137    
138     #endif
139    
140 segfault 1.1 void neterror(char *s)
141     {
142 stdarg 1.54 #ifdef HAVE_STRERROR
143     int e = errno;
144     char *err = strerror(e);
145    
146     /* Calling procs usually use char s[UHOSTLEN] for the error message. */
147 ite 1.55 if (err) strlcpy(s, err, UHOSTLEN);
148 stdarg 1.54 else sprintf(s, "Unforeseen error %d", e);
149     #else
150 segfault 1.1 switch (errno) {
151     case EADDRINUSE:
152     strcpy(s, "Address already in use");
153     break;
154     case EADDRNOTAVAIL:
155     strcpy(s, "Address invalid on remote machine");
156     break;
157     case EAFNOSUPPORT:
158     strcpy(s, "Address family not supported");
159     break;
160     case EALREADY:
161     strcpy(s, "Socket already in use");
162     break;
163     case EBADF:
164     strcpy(s, "Socket descriptor is bad");
165     break;
166     case ECONNREFUSED:
167     strcpy(s, "Connection refused");
168     break;
169     case EFAULT:
170     strcpy(s, "Namespace segment violation");
171     break;
172     case EINPROGRESS:
173     strcpy(s, "Operation in progress");
174     break;
175     case EINTR:
176     strcpy(s, "Timeout");
177     break;
178     case EINVAL:
179     strcpy(s, "Invalid namespace");
180     break;
181     case EISCONN:
182     strcpy(s, "Socket already connected");
183     break;
184     case ENETUNREACH:
185     strcpy(s, "Network unreachable");
186     break;
187     case ENOTSOCK:
188     strcpy(s, "File descriptor, not a socket");
189     break;
190     case ETIMEDOUT:
191     strcpy(s, "Connection timed out");
192     break;
193     case ENOTCONN:
194     strcpy(s, "Socket is not connected");
195     break;
196     case EHOSTUNREACH:
197     strcpy(s, "Host is unreachable");
198     break;
199     case EPIPE:
200     strcpy(s, "Broken pipe");
201     break;
202     #ifdef ECONNRESET
203     case ECONNRESET:
204     strcpy(s, "Connection reset by peer");
205     break;
206     #endif
207     #ifdef EACCES
208     case EACCES:
209     strcpy(s, "Permission denied");
210     break;
211     #endif
212     case 0:
213     strcpy(s, "Error 0");
214     break;
215     default:
216     sprintf(s, "Unforseen error %d", errno);
217     break;
218     }
219 stdarg 1.54 #endif
220 segfault 1.1 }
221    
222 fabian 1.25 /* Sets/Unsets options for a specific socket.
223     *
224     * Returns: 0 - on success
225     * -1 - socket not found
226     * -2 - illegal operation
227     */
228     int sockoptions(int sock, int operation, int sock_options)
229     {
230     int i;
231    
232     for (i = 0; i < MAXSOCKS; i++)
233 poptix 1.30 if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED)) {
234 fabian 1.25 if (operation == EGG_OPTION_SET)
235     socklist[i].flags |= sock_options;
236     else if (operation == EGG_OPTION_UNSET)
237     socklist[i].flags &= ~sock_options;
238     else
239     return -2;
240     return 0;
241     }
242     return -1;
243     }
244    
245 fabian 1.16 /* Return a free entry in the socket entry
246     */
247 fabian 1.3 int allocsock(int sock, int options)
248 segfault 1.1 {
249     int i;
250    
251     for (i = 0; i < MAXSOCKS; i++) {
252 stdarg 1.44 if (socklist[i].flags & SOCK_UNUSED) break;
253     }
254    
255     if (i == MAXSOCKS) {
256 stdarg 1.46 int j;
257    
258 stdarg 1.44 /* Expand table by 5 */
259     socklist = (sock_list *)realloc(socklist, (MAXSOCKS+5) * sizeof(sock_list));
260     memset(socklist+MAXSOCKS, 0, 5 * sizeof(sock_list));
261 stdarg 1.46 for (j = 0; j < 5; j++) socklist[MAXSOCKS+j].flags = SOCK_UNUSED;
262 stdarg 1.44 MAXSOCKS += 5;
263 fabian 1.3 }
264 stdarg 1.44
265     memset(socklist+i, 0, sizeof(sock_list));
266     socklist[i].flags = options;
267     socklist[i].sock = sock;
268     return(i);
269 fabian 1.3 }
270    
271 fabian 1.16 /* Request a normal socket for i/o
272     */
273 fabian 1.3 void setsock(int sock, int options)
274     {
275     int i = allocsock(sock, options);
276     int parm;
277    
278 fabian 1.16 if (((sock != STDOUT) || backgrd) &&
279     !(socklist[i].flags & SOCK_NONSOCK)) {
280     parm = 1;
281 fabian 1.3 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *) &parm, sizeof(int));
282 segfault 1.1
283 fabian 1.16 parm = 0;
284     setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &parm, sizeof(int));
285     }
286     if (options & SOCK_LISTEN) {
287     /* Tris says this lets us grab the same port again next time */
288     parm = 1;
289     setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *) &parm, sizeof(int));
290     }
291     /* Yay async i/o ! */
292     fcntl(sock, F_SETFL, O_NONBLOCK);
293 segfault 1.1 }
294    
295     int getsock(int options)
296     {
297 drummer 1.33 #ifdef IPV6
298     int sock = socket(AF_INET6, SOCK_STREAM, 0);
299     #else
300 segfault 1.1 int sock = socket(AF_INET, SOCK_STREAM, 0);
301 drummer 1.33 #endif
302 segfault 1.1
303 fabian 1.24 if (sock >= 0)
304     setsock(sock, options);
305     else
306     putlog(LOG_MISC, "*", "Warning: Can't create new socket!");
307 segfault 1.1 return sock;
308     }
309    
310 fabian 1.16 /* Done with a socket
311     */
312 fabian 1.24 void killsock(register int sock)
313 segfault 1.1 {
314 fabian 1.24 register int i;
315 segfault 1.1
316 fabian 1.24 /* Ignore invalid sockets. */
317     if (sock < 0)
318     return;
319 segfault 1.1 for (i = 0; i < MAXSOCKS; i++) {
320 fabian 1.2 if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED)) {
321 segfault 1.1 close(socklist[i].sock);
322 tothwolf 1.42 if (socklist[i].inbuf != NULL)
323     free_null(socklist[i].inbuf);
324 segfault 1.1 if (socklist[i].outbuf != NULL) {
325 tothwolf 1.42 free_null(socklist[i].outbuf);
326 segfault 1.1 socklist[i].outbuflen = 0;
327     }
328     socklist[i].flags = SOCK_UNUSED;
329     return;
330     }
331     }
332     putlog(LOG_MISC, "*", "Attempt to kill un-allocated socket %d !!", sock);
333     }
334    
335 fabian 1.16 /* Send connection request to proxy
336     */
337 segfault 1.1 static int proxy_connect(int sock, char *host, int port, int proxy)
338     {
339     unsigned char x[10];
340     struct hostent *hp;
341 fabian 1.26 char s[256];
342 segfault 1.1 int i;
343    
344     /* socks proxy */
345     if (proxy == PROXY_SOCKS) {
346     /* numeric IP? */
347 fabian 1.26 if (host[strlen(host) - 1] >= '0' && host[strlen(host) - 1] <= '9') {
348 segfault 1.1 IP ip = ((IP) inet_addr(host)); /* drummer */
349 tothwolf 1.42 memcpy(x, &ip, 4); /* Beige@Efnet */
350 segfault 1.1 } else {
351     /* no, must be host.domain */
352     if (!setjmp(alarmret)) {
353     alarm(resolve_timeout);
354     hp = gethostbyname(host);
355     alarm(0);
356     } else {
357     hp = NULL;
358     }
359     if (hp == NULL) {
360     killsock(sock);
361     return -2;
362     }
363 tothwolf 1.42 memcpy(x, hp->h_addr, hp->h_length);
364 segfault 1.1 }
365 drummer 1.37 for (i = 0; i < MAXSOCKS; i++)
366     if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
367     socklist[i].flags |= SOCK_PROXYWAIT; /* drummer */
368 tothwolf 1.48 snprintf(s, sizeof s, "\004\001%c%c%c%c%c%c%s", (port >> 8) % 256,
369 fabian 1.26 (port % 256), x[0], x[1], x[2], x[3], botuser);
370 segfault 1.1 tputs(sock, s, strlen(botuser) + 9); /* drummer */
371     } else if (proxy == PROXY_SUN) {
372 tothwolf 1.48 snprintf(s, sizeof s, "%s %d\n", host, port);
373 segfault 1.1 tputs(sock, s, strlen(s)); /* drummer */
374     }
375     return sock;
376     }
377    
378 fabian 1.16 /* Starts a connection attempt to a socket
379 fabian 1.11 *
380 fabian 1.3 * If given a normal hostname, this will be resolved to the corresponding
381     * IP address first. PLEASE try to use the non-blocking dns functions
382     * instead and then call this function with the IP address to avoid blocking.
383 fabian 1.11 *
384 segfault 1.1 * returns <0 if connection refused:
385     * -1 neterror() type error
386 fabian 1.16 * -2 can't resolve hostname
387     */
388 segfault 1.1 int open_telnet_raw(int sock, char *server, int sport)
389     {
390 drummer 1.33 #ifdef IPV6
391     struct sockaddr_in6 name, vname;
392     #else
393     struct sockaddr_in name, vname;
394     #endif
395 segfault 1.1 struct hostent *hp;
396     char host[121];
397     int i, port;
398     volatile int proxy;
399    
400 drummer 1.33 debug2("|NET| open_telnet_raw: %s %d", server, sport);
401    
402 segfault 1.1 /* firewall? use socks */
403     if (firewall[0]) {
404     if (firewall[0] == '!') {
405     proxy = PROXY_SUN;
406     strcpy(host, &firewall[1]);
407     } else {
408     proxy = PROXY_SOCKS;
409     strcpy(host, firewall);
410     }
411     port = firewallport;
412     } else {
413     proxy = 0;
414     strcpy(host, server);
415     port = sport;
416     }
417    
418 tothwolf 1.42 memset((char *) &name, 0, sizeof name);
419 drummer 1.33 #ifdef IPV6
420     name.sin6_family = AF_INET6;
421     name.sin6_port = htons(port);
422     #else
423 segfault 1.1 name.sin_family = AF_INET;
424 drummer 1.33 name.sin_port = htons(port);
425     #endif
426    
427     if (!setjmp(alarmret)) {
428     char *p;
429     int type;
430    
431     alarm(resolve_timeout);
432 segfault 1.1
433 tothwolf 1.48 if (!strncasecmp("ipv6%", host, 5)) {
434 drummer 1.33 type = AF_INET6;
435     p = host + 5;
436     debug1("|NET| checking only AAAA record for %s", p);
437 tothwolf 1.48 } else if (!strncasecmp("ipv4%", host, 5)) {
438 drummer 1.33 type = AF_INET;
439     p = host + 5;
440     debug1("|NET| checking only A record for %s", p);
441     } else {
442     type = AF_INET; /* af_preferred */
443     p = host;
444     }
445 segfault 1.1 alarm(resolve_timeout);
446 drummer 1.33 #ifndef IPV6
447     hp = gethostbyname(p);
448     #else
449     hp = gethostbyname2(p, type);
450     if (!hp && (p == host))
451     hp = gethostbyname2(p, (type == AF_INET6 ? AF_INET : AF_INET6));
452     #endif
453 segfault 1.1 alarm(0);
454 drummer 1.33 } else {
455 segfault 1.1 hp = NULL;
456 drummer 1.33 }
457     if (hp == NULL)
458 segfault 1.1 return -2;
459 drummer 1.33
460     #ifndef IPV6
461 tothwolf 1.42 memcpy(&name.sin_addr, hp->h_addr, hp->h_length);
462 drummer 1.33 name.sin_family = hp->h_addrtype;
463     #else
464     if (hp->h_addrtype == AF_INET6)
465 tothwolf 1.42 memcpy(&name.sin6_addr, hp->h_addr, hp->h_length);
466 drummer 1.33 else if (hp->h_addrtype == AF_INET)
467     name.sin6_addr = ipv4to6(*((unsigned int*) hp->h_addr));
468     else
469     return -123; /* panic :P */
470     name.sin6_family = AF_INET6;
471     #endif
472    
473 tothwolf 1.42 memset((char *) &vname, 0, sizeof vname);
474 drummer 1.33 #ifndef IPV6
475     vname.sin_family = AF_INET;
476     vname.sin_addr.s_addr = (myip[0] ? getmyip() : INADDR_ANY);
477     #else
478     vname.sin6_family = AF_INET6;
479 drummer 1.35 if (IN6_IS_ADDR_V4MAPPED(&name.sin6_addr))
480 drummer 1.33 vname.sin6_addr = ipv4to6(myip[0] ? getmyip() : INADDR_ANY);
481     else
482     vname.sin6_addr = (myip6[0] ? getmyip6() : in6addr_any);
483     #endif
484     if (bind(sock, (struct sockaddr *) &vname, sizeof vname) < 0)
485     return -1;
486    
487 segfault 1.1 for (i = 0; i < MAXSOCKS; i++) {
488     if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == sock))
489 fabian 1.17 socklist[i].flags = (socklist[i].flags & ~SOCK_VIRTUAL) | SOCK_CONNECT;
490 segfault 1.1 }
491 drummer 1.33 if (connect(sock, (struct sockaddr *) &name, (sizeof name)) < 0) {
492 segfault 1.1 if (errno == EINPROGRESS) {
493 fabian 1.16 /* Firewall? announce connect attempt to proxy */
494 segfault 1.1 if (firewall[0])
495     return proxy_connect(sock, server, sport, proxy);
496     return sock; /* async success! */
497 fabian 1.4 } else
498 segfault 1.1 return -1;
499     }
500 fabian 1.16 /* Synchronous? :/ */
501 segfault 1.1 if (firewall[0])
502     return proxy_connect(sock, server, sport, proxy);
503     return sock;
504     }
505    
506 fabian 1.16 /* Ordinary non-binary connection attempt */
507 segfault 1.1 int open_telnet(char *server, int port)
508     {
509 fabian 1.4 int sock = getsock(0),
510     ret = open_telnet_raw(sock, server, port);
511    
512     if (ret < 0)
513     killsock(sock);
514     return ret;
515 segfault 1.1 }
516    
517 fabian 1.16 /* Returns a socket number for a listening socket that will accept any
518 fabian 1.19 * connection on a certain address -- port # is returned in port
519 drummer 1.33 * "" means on any address
520 fabian 1.16 */
521 drummer 1.33 int open_address_listen(char *addr, int *port)
522 segfault 1.1 {
523 fabian 1.13 int sock;
524     unsigned int addrlen;
525 drummer 1.33 #ifdef IPV6
526     struct sockaddr_in6 name;
527     #else
528 segfault 1.1 struct sockaddr_in name;
529 drummer 1.33 #endif
530 segfault 1.1
531     if (firewall[0]) {
532     /* FIXME: can't do listen port thru firewall yet */
533 fabian 1.6 putlog(LOG_MISC, "*", "!! Cant open a listen port (you are using a firewall)");
534 segfault 1.1 return -1;
535     }
536 fabian 1.24
537 segfault 1.1 sock = getsock(SOCK_LISTEN);
538 fabian 1.24 if (sock < 1)
539     return -1;
540 drummer 1.33
541     debug2("|NET| open_address_listen(\"%s\", %d)", addr, *port);
542 fabian 1.24
543 tothwolf 1.42 memset((char *) &name, 0, sizeof name);
544 drummer 1.33 #ifdef IPV6
545     name.sin6_family = AF_INET6;
546     name.sin6_port = htons(*port); /* 0 = just assign us a port */
547     if (!addr[0])
548     name.sin6_addr = in6addr_any;
549 tothwolf 1.48 else if (!inet_pton(AF_INET6, addr, &name.sin6_addr)) {
550 drummer 1.33 struct in_addr a4;
551 tothwolf 1.48 if (inet_aton(addr, &a4))
552 drummer 1.33 name.sin6_addr = ipv4to6(a4.s_addr);
553     else
554     name.sin6_addr = in6addr_any;
555     }
556     #else
557 segfault 1.1 name.sin_family = AF_INET;
558 fabian 1.19 name.sin_port = htons(*port); /* 0 = just assign us a port */
559 drummer 1.33 if (addr[0])
560 tothwolf 1.48 inet_aton(addr, &name.sin_addr);
561 drummer 1.33 else
562     name.sin_addr.s_addr = INADDR_ANY;
563     #endif
564     if (bind(sock, (struct sockaddr *) &name, sizeof name) < 0) {
565 segfault 1.1 killsock(sock);
566 guppy 1.29 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT)
567     return -2;
568     else
569     return -1;
570 segfault 1.1 }
571     /* what port are we on? */
572 drummer 1.33 addrlen = sizeof name;
573 segfault 1.1 if (getsockname(sock, (struct sockaddr *) &name, &addrlen) < 0) {
574     killsock(sock);
575     return -1;
576     }
577 drummer 1.33 #ifdef IPV6
578     *port = ntohs(name.sin6_port);
579     #else
580 fabian 1.19 *port = ntohs(name.sin_port);
581 drummer 1.33 #endif
582 segfault 1.1 if (listen(sock, 1) < 0) {
583     killsock(sock);
584     return -1;
585     }
586     return sock;
587 fabian 1.19 }
588    
589     /* Returns a socket number for a listening socket that will accept any
590     * connection -- port # is returned in port
591     */
592 drummer 1.33 inline int open_listen(int *port, int af)
593 fabian 1.19 {
594 drummer 1.33 if (af == AF_INET)
595     return open_address_listen(myip, port);
596     #ifdef IPV6
597     else if (af == AF_INET6)
598     return open_address_listen(myip6, port);
599     #endif
600     else
601     return -1;
602 segfault 1.1 }
603    
604 fabian 1.5 /* Returns the given network byte order IP address in the
605 fabian 1.16 * dotted format - "##.##.##.##"
606 drummer 1.33 * (it's IPv4 only, and it's okey - drummer)
607 fabian 1.16 */
608 fabian 1.3 char *iptostr(IP ip)
609     {
610     struct in_addr a;
611    
612     a.s_addr = ip;
613     return inet_ntoa(a);
614     }
615    
616 drummer 1.33 char *getlocaladdr(int sock)
617     {
618     static char buf[ADDRLEN];
619     #ifdef IPV6
620     struct sockaddr_in6 sa;
621     #else
622     struct sockaddr_in sa;
623     #endif
624     int len = sizeof sa;
625    
626     if (sock == -1) { /* assuming IPv4... */
627     sprintf(buf, "%lu", (unsigned long int)
628     ntohl(natip[0] ? inet_addr(natip) : getmyip()));
629     return buf;
630     }
631    
632     if (getsockname(sock, (struct sockaddr*) &sa, &len) == -1) {
633     debug1("|NET| getsockname() failed for sock %d", sock);
634     return 0;
635     }
636     #ifdef IPV6
637     if (IN6_IS_ADDR_V4MAPPED(&sa.sin6_addr))
638     sprintf(buf, "%lu",
639 drummer 1.34 (unsigned long int) ntohl(((uint32_t *)&sa.sin6_addr)[3]));
640 drummer 1.33 else
641 tothwolf 1.48 inet_ntop(AF_INET6, &(sa.sin6_addr), buf, sizeof buf);
642 drummer 1.33 #else
643 drummer 1.36 sprintf(buf, "%lu", (unsigned long int) ntohl(sa.sin_addr.s_addr));
644 drummer 1.33 #endif
645     return buf;
646     }
647    
648 fabian 1.16 /* Short routine to answer a connect received on a socket made previously
649 segfault 1.1 * by open_listen ... returns hostname of the caller & the new socket
650 fabian 1.16 * does NOT dispose of old "public" socket!
651     */
652 drummer 1.33 int answer(int sock, char *caller, char *ip, unsigned short *port,
653 fabian 1.16 int binary)
654 segfault 1.1 {
655 fabian 1.13 int new_sock;
656     unsigned int addrlen;
657 drummer 1.33 #ifdef IPV6
658     struct sockaddr_in6 from;
659     #else
660 segfault 1.1 struct sockaddr_in from;
661 drummer 1.33 #endif
662 fabian 1.25
663 drummer 1.33 addrlen = sizeof from;
664 segfault 1.1 new_sock = accept(sock, (struct sockaddr *) &from, &addrlen);
665     if (new_sock < 0)
666     return -1;
667     if (ip != NULL) {
668 drummer 1.33 #ifdef IPV6
669 drummer 1.35 if (IN6_IS_ADDR_V4MAPPED(&from.sin6_addr))
670 tothwolf 1.48 inet_ntop(AF_INET, &(((uint32_t *)&from.sin6_addr)[3]), ip, ADDRMAX);
671 drummer 1.33 else
672 tothwolf 1.48 inet_ntop(AF_INET6, &(from.sin6_addr), ip, ADDRMAX);
673 drummer 1.33 #else
674 tothwolf 1.48 inet_ntop(AF_INET, &(from.sin_addr.s_addr), ip, ADDRMAX);
675 drummer 1.33 #endif
676 fabian 1.16 /* This is now done asynchronously. We now only provide the IP address.
677 fabian 1.3 */
678 ite 1.55 strlcpy(caller, ip, 121);
679 segfault 1.1 }
680     if (port != NULL)
681 drummer 1.33 #ifdef IPV6
682     *port = ntohs(from.sin6_port);
683     #else
684 fabian 1.21 *port = ntohs(from.sin_port);
685 drummer 1.33 #endif
686 fabian 1.16 /* Set up all the normal socket crap */
687 segfault 1.1 setsock(new_sock, (binary ? SOCK_BINARY : 0));
688     return new_sock;
689     }
690    
691 fabian 1.16 /* Like open_telnet, but uses server & port specifications of dcc
692     */
693 segfault 1.1 int open_telnet_dcc(int sock, char *server, char *port)
694     {
695     int p;
696 drummer 1.33 struct in_addr ia;
697 segfault 1.1
698 drummer 1.33 debug2("|NET| open_telnet_dcc: %s %s", server, port);
699 segfault 1.1 if (port != NULL)
700     p = atoi(port);
701     else
702 drummer 1.33 return -3;
703     if (server == NULL)
704     return -3;
705     /* fix the IPv4 IP format (ie: 167772161 -> 10.0.0.1) */
706 tothwolf 1.48 if (inet_aton(server, &ia))
707 drummer 1.33 return open_telnet_raw(sock, inet_ntoa(ia), p);
708 segfault 1.1 else
709 drummer 1.33 return open_telnet_raw(sock, server, p);
710     }
711    
712     void egg_dns_gotanswer(int status, adns_answer *aw, char *origname)
713     {
714     char name[UHOSTLEN];
715 tothwolf 1.42 #ifdef IPV6
716 tothwolf 1.43 char *orign2;
717 tothwolf 1.42 #endif
718 drummer 1.33
719     if (!aw) {
720     debug0("|DNS| egg_dns_gotanswer: ANSWER IS NULL!");
721     return;
722     }
723     if (!origname) {
724     debug0("|DNS| egg_dns_gotanswer: origname is NULL!");
725     return;
726     }
727    
728     debug2("|DNS| egg_dns_gotanswer: status=%d adns_answer=%x", status, (int)aw);
729     status = 0;
730     if ((aw->type == adns_r_addr)
731     #ifdef IPV6
732     || (aw->type == adns_r_addr6)
733     #endif
734     ) {
735     if ((aw->status == adns_s_ok) && (aw->nrrs > 0)) {
736     adns_rr_addr *rrp = aw->rrs.untyped;
737     if (rrp->addr.sa.sa_family == AF_INET) {
738 tothwolf 1.48 inet_ntop(AF_INET, &(rrp->addr.inet.sin_addr), name, UHOSTLEN-1);
739 drummer 1.33 status = 1;
740     #ifdef IPV6
741     } else if (rrp->addr.sa.sa_family == AF_INET6) {
742 tothwolf 1.48 inet_ntop(AF_INET6, &(rrp->addr.inet6.sin6_addr), name, UHOSTLEN-1);
743 drummer 1.33 status = 1;
744     #endif
745     }
746     #ifdef IPV6
747     } else if ((aw->type == adns_r_addr /* af_preferred */) &&
748 tothwolf 1.48 strncasecmp(origname, "ipv6%", 5) &&
749     strncasecmp(origname, "ipv4%", 5)) {
750 drummer 1.33 adns_query q6;
751 ite 1.51 orign2 = strdup(origname);
752 drummer 1.33 /* ...it may be AAAA */
753     debug1("|DNS| egg_dns_gotanswer: A failed, checking for AAAA (%s)", origname);
754     adns_submit(ads, origname, adns_r_addr6, 0, orign2, &q6);
755     status = -1;
756     #endif
757     }
758     if (status == 0)
759     strcpy(name, "0.0.0.0");
760     if (status >= 0) {
761     debug3("|DNS| egg_dns_gotanswer: (ipbyhost) host: %s ip: %s status: %d", origname, name, status);
762     call_ipbyhost(origname, name, status);
763     }
764     } else if ((aw->type == adns_r_ptr_ip6) || (aw->type == adns_r_ptr)) {
765     if ((aw->status == adns_s_ok) && (aw->nrrs > 0)) {
766     if (aw->rrs.str) {
767 ite 1.55 strlcpy(name, *(aw->rrs.str), UHOSTLEN);
768 drummer 1.33 status = 1;
769     }
770     }
771     if (!status) {
772     if (origname)
773 ite 1.55 strlcpy(name, origname, UHOSTLEN);
774 drummer 1.33 else
775     strcpy(name, "error");
776     }
777     debug3("|DNS| egg_dns_gotanswer: (hostbyip) ip: %s host: %s status: %d", origname, name, status);
778     call_hostbyip(origname, name, status);
779     } else
780     debug0("|DNS| egg_dns_gotanswer: got unknow type of answer ?!");
781 tothwolf 1.42 free(origname);
782 drummer 1.33 }
783    
784     void egg_dns_checkall()
785     {
786     adns_query q, r;
787     adns_answer *answer;
788     char *origname;
789    
790     adns_forallqueries_begin(ads);
791     while ((q = adns_forallqueries_next(ads, (void **)&r)) != NULL) {
792     switch (adns_check(ads, &q, &answer, (void **)&origname)) {
793     case 0: /* ok */
794     egg_dns_gotanswer(1, answer, origname);
795     break;
796     case EAGAIN: /* Go into the queue again */
797     break;
798     default: /* failed */
799     egg_dns_gotanswer(0, answer, origname);
800     break;
801     }
802     }
803 segfault 1.1 }
804    
805 fabian 1.16 /* Attempts to read from all the sockets in socklist
806 segfault 1.1 * fills s with up to 511 bytes if available, and returns the array index
807 fabian 1.16 *
808     * on EOF: returns -1, with socket in len
809     * on socket error: returns -2
810     * if nothing is ready: returns -3
811     */
812 segfault 1.1 static int sockread(char *s, int *len)
813     {
814 drummer 1.33 fd_set fd, fdw, fde;
815 ite 1.52 int fds, i, x, fdtmp;
816 drummer 1.33 struct timeval t, tnow;
817     struct timeval *pt = &t;
818 segfault 1.1 int grab = 511;
819 stdarg 1.46 egg_timeval_t howlong;
820 segfault 1.1
821     fds = getdtablesize();
822     #ifdef FD_SETSIZE
823     if (fds > FD_SETSIZE)
824 fabian 1.16 fds = FD_SETSIZE; /* Fixes YET ANOTHER freebsd bug!!! */
825 segfault 1.1 #endif
826 stdarg 1.46
827     if (timer_get_shortest(&howlong)) {
828     /* No timer, default to 1 second. */
829     t.tv_sec = 1;
830     t.tv_usec = 0;
831     }
832     else {
833     t.tv_sec = howlong.sec;
834     t.tv_usec = howlong.usec;
835     }
836    
837 segfault 1.1 FD_ZERO(&fd);
838 fabian 1.17
839 segfault 1.1 for (i = 0; i < MAXSOCKS; i++)
840 fabian 1.17 if (!(socklist[i].flags & (SOCK_UNUSED | SOCK_VIRTUAL))) {
841 segfault 1.1 if ((socklist[i].sock == STDOUT) && !backgrd)
842 ite 1.52 fdtmp = STDIN;
843 segfault 1.1 else
844 ite 1.52 fdtmp = socklist[i].sock;
845     /*
846     * Looks like that having more than a call, in the same
847     * program, to the FD_SET macro, triggers a bug in gcc.
848     * SIGBUS crashing binaries used to be produced on a number
849     * (prolly all?) of 64 bits architectures.
850     * Make your best to avoid to make it happen again.
851     *
852     * ITE
853     */
854     FD_SET(fdtmp , &fd);
855 segfault 1.1 }
856 drummer 1.33 tnow.tv_sec = time(NULL);
857     tnow.tv_usec = 0;
858     adns_beforeselect(ads, &fds, &fd, &fdw, &fde, &pt, 0, &tnow);
859 segfault 1.1 #ifdef HPUX_HACKS
860     #ifndef HPUX10_HACKS
861     x = select(fds, (int *) &fd, (int *) NULL, (int *) NULL, &t);
862     #else
863     x = select(fds, &fd, NULL, NULL, &t);
864     #endif
865     #else
866     x = select(fds, &fd, NULL, NULL, &t);
867     #endif
868 drummer 1.33 tnow.tv_sec = time(NULL);
869     tnow.tv_usec = 0;
870     adns_afterselect(ads, fds, &fd, &fdw, &fde, &tnow);
871    
872     /* dns stuff */
873     egg_dns_checkall();
874    
875 segfault 1.1 if (x > 0) {
876 fabian 1.16 /* Something happened */
877 segfault 1.1 for (i = 0; i < MAXSOCKS; i++) {
878     if ((!(socklist[i].flags & SOCK_UNUSED)) &&
879     ((FD_ISSET(socklist[i].sock, &fd)) ||
880     ((socklist[i].sock == STDOUT) && (!backgrd) &&
881     (FD_ISSET(STDIN, &fd))))) {
882     if (socklist[i].flags & (SOCK_LISTEN | SOCK_CONNECT)) {
883 fabian 1.16 /* Listening socket -- don't read, just return activity */
884     /* Same for connection attempt */
885 segfault 1.1 /* (for strong connections, require a read to succeed first) */
886     if (socklist[i].flags & SOCK_PROXYWAIT) { /* drummer */
887 fabian 1.16 /* Hang around to get the return code from proxy */
888 segfault 1.1 grab = 10;
889     } else if (!(socklist[i].flags & SOCK_STRONGCONN)) {
890     debug1("net: connect! sock %d", socklist[i].sock);
891     s[0] = 0;
892     *len = 0;
893     return i;
894     }
895 fabian 1.3 } else if (socklist[i].flags & SOCK_PASS) {
896     s[0] = 0;
897     *len = 0;
898     return i;
899 segfault 1.1 }
900     if ((socklist[i].sock == STDOUT) && !backgrd)
901     x = read(STDIN, s, grab);
902     else
903     x = read(socklist[i].sock, s, grab);
904     if (x <= 0) { /* eof */
905 guppy 1.31 if (errno == EAGAIN) {
906 segfault 1.1 s[0] = 0;
907     *len = 0;
908     return -3;
909     }
910     *len = socklist[i].sock;
911     socklist[i].flags &= ~SOCK_CONNECT;
912     debug1("net: eof!(read) socket %d", socklist[i].sock);
913     return -1;
914     }
915     s[x] = 0;
916     *len = x;
917     if (socklist[i].flags & SOCK_PROXYWAIT) {
918     debug2("net: socket: %d proxy errno: %d", socklist[i].sock, s[1]);
919     socklist[i].flags &= ~(SOCK_CONNECT | SOCK_PROXYWAIT);
920     switch (s[1]) {
921 fabian 1.16 case 90: /* Success */
922 segfault 1.1 s[0] = 0;
923     *len = 0;
924     return i;
925 fabian 1.16 case 91: /* Failed */
926 segfault 1.1 errno = ECONNREFUSED;
927     break;
928 fabian 1.16 case 92: /* No identd */
929     case 93: /* Identd said wrong username */
930     /* A better error message would be "socks misconfigured"
931     * or "identd not working" but this is simplest.
932     */
933 segfault 1.1 errno = ENETUNREACH;
934     break;
935     }
936     *len = socklist[i].sock;
937     return -1;
938     }
939     return i;
940     }
941     }
942     } else if (x == -1)
943     return -2; /* socket error */
944     else {
945     s[0] = 0;
946     *len = 0;
947     }
948     return -3;
949     }
950    
951     /* sockgets: buffer and read from sockets
952     *
953 fabian 1.16 * Attempts to read from all registered sockets for up to one second. if
954 segfault 1.1 * after one second, no complete data has been received from any of the
955     * sockets, 's' will be empty, 'len' will be 0, and sockgets will return -3.
956     * if there is returnable data received from a socket, the data will be
957     * in 's' (null-terminated if non-binary), the length will be returned
958     * in len, and the socket number will be returned.
959     * normal sockets have their input buffered, and each call to sockgets
960     * will return one line terminated with a '\n'. binary sockets are not
961     * buffered and return whatever coems in as soon as it arrives.
962     * listening sockets will return an empty string when a connection comes in.
963     * connecting sockets will return an empty string on a successful connect,
964     * or EOF on a failed connect.
965     * if an EOF is detected from any of the sockets, that socket number will be
966     * put in len, and -1 will be returned.
967     * the maximum length of the string returned is 512 (including null)
968 fabian 1.25 *
969     * Returns -4 if we handled something that shouldn't be handled by the
970     * dcc functions. Simply ignore it.
971 segfault 1.1 */
972    
973     int sockgets(char *s, int *len)
974     {
975     char xx[514], *p, *px;
976     int ret, i, data = 0;
977    
978     for (i = 0; i < MAXSOCKS; i++) {
979 fabian 1.16 /* Check for stored-up data waiting to be processed */
980 fabian 1.25 if (!(socklist[i].flags & SOCK_UNUSED) &&
981     !(socklist[i].flags & SOCK_BUFFER) && (socklist[i].inbuf != NULL)) {
982     if (!(socklist[i].flags & SOCK_BINARY)) {
983     /* look for \r too cos windows can't follow RFCs */
984     p = strchr(socklist[i].inbuf, '\n');
985     if (p == NULL)
986     p = strchr(socklist[i].inbuf, '\r');
987     if (p != NULL) {
988     *p = 0;
989     if (strlen(socklist[i].inbuf) > 510)
990     socklist[i].inbuf[510] = 0;
991     strcpy(s, socklist[i].inbuf);
992 tothwolf 1.42 /* Uhm... very strange way to code this... */
993     px = (char *) malloc(strlen(p + 1) + 1);
994 fabian 1.25 strcpy(px, p + 1);
995 tothwolf 1.42 free(socklist[i].inbuf);
996 fabian 1.25 if (px[0])
997     socklist[i].inbuf = px;
998     else {
999 tothwolf 1.42 free(px);
1000 fabian 1.25 socklist[i].inbuf = NULL;
1001     }
1002     /* Strip CR if this was CR/LF combo */
1003     if (s[strlen(s) - 1] == '\r')
1004     s[strlen(s) - 1] = 0;
1005     *len = strlen(s);
1006     return socklist[i].sock;
1007     }
1008     } else {
1009     /* Handling buffered binary data (must have been SOCK_BUFFER before). */
1010     if (socklist[i].inbuflen <= 510) {
1011     *len = socklist[i].inbuflen;
1012 tothwolf 1.42 memcpy(s, socklist[i].inbuf, socklist[i].inbuflen);
1013     free_null(socklist[i].inbuf);
1014 fabian 1.25 socklist[i].inbuflen = 0;
1015     } else {
1016     /* Split up into chunks of 510 bytes. */
1017     *len = 510;
1018 tothwolf 1.42 memcpy(s, socklist[i].inbuf, *len);
1019     memcpy(socklist[i].inbuf, socklist[i].inbuf + *len, *len);
1020 fabian 1.25 socklist[i].inbuflen -= *len;
1021 tothwolf 1.42 socklist[i].inbuf = realloc(socklist[i].inbuf,
1022 fabian 1.25 socklist[i].inbuflen);
1023 segfault 1.1 }
1024     return socklist[i].sock;
1025     }
1026     }
1027 fabian 1.16 /* Also check any sockets that might have EOF'd during write */
1028 segfault 1.1 if (!(socklist[i].flags & SOCK_UNUSED)
1029     && (socklist[i].flags & SOCK_EOFD)) {
1030     s[0] = 0;
1031     *len = socklist[i].sock;
1032     return -1;
1033     }
1034     }
1035 fabian 1.16 /* No pent-up data of any worth -- down to business */
1036 segfault 1.1 *len = 0;
1037     ret = sockread(xx, len);
1038     if (ret < 0) {
1039     s[0] = 0;
1040     return ret;
1041     }
1042 fabian 1.16 /* Binary, listening and passed on sockets don't get buffered. */
1043 segfault 1.1 if (socklist[ret].flags & SOCK_CONNECT) {
1044     if (socklist[ret].flags & SOCK_STRONGCONN) {
1045     socklist[ret].flags &= ~SOCK_STRONGCONN;
1046 fabian 1.25 /* Buffer any data that came in, for future read. */
1047     socklist[ret].inbuflen = *len;
1048 tothwolf 1.42 socklist[ret].inbuf = (char *) malloc(*len + 1);
1049 fabian 1.25 /* It might be binary data. You never know. */
1050 tothwolf 1.42 memcpy(socklist[ret].inbuf, xx, *len);
1051 fabian 1.25 socklist[ret].inbuf[*len] = 0;
1052 segfault 1.1 }
1053     socklist[ret].flags &= ~SOCK_CONNECT;
1054     s[0] = 0;
1055     return socklist[ret].sock;
1056     }
1057     if (socklist[ret].flags & SOCK_BINARY) {
1058 tothwolf 1.42 memcpy(s, xx, *len);
1059 segfault 1.1 return socklist[ret].sock;
1060     }
1061 fabian 1.3 if ((socklist[ret].flags & SOCK_LISTEN) ||
1062     (socklist[ret].flags & SOCK_PASS))
1063 segfault 1.1 return socklist[ret].sock;
1064 fabian 1.25 if (socklist[ret].flags & SOCK_BUFFER) {
1065 tothwolf 1.42 socklist[ret].inbuf = (char *) realloc(socklist[ret].inbuf,
1066 fabian 1.25 socklist[ret].inbuflen + *len + 1);
1067 tothwolf 1.42 memcpy(socklist[ret].inbuf + socklist[ret].inbuflen, xx, *len);
1068 fabian 1.25 socklist[ret].inbuflen += *len;
1069     /* We don't know whether it's binary data. Make sure normal strings
1070     will be handled properly later on too. */
1071     socklist[ret].inbuf[socklist[ret].inbuflen] = 0;
1072     return -4; /* Ignore this one. */
1073     }
1074 fabian 1.16 /* Might be necessary to prepend stored-up data! */
1075 segfault 1.1 if (socklist[ret].inbuf != NULL) {
1076     p = socklist[ret].inbuf;
1077 tothwolf 1.42 socklist[ret].inbuf = (char *) malloc(strlen(p) + strlen(xx) + 1);
1078 segfault 1.1 strcpy(socklist[ret].inbuf, p);
1079     strcat(socklist[ret].inbuf, xx);
1080 tothwolf 1.42 free(p);
1081 segfault 1.1 if (strlen(socklist[ret].inbuf) < 512) {
1082     strcpy(xx, socklist[ret].inbuf);
1083 tothwolf 1.42 free_null(socklist[ret].inbuf);
1084 fabian 1.25 socklist[ret].inbuflen = 0;
1085 segfault 1.1 } else {
1086     p = socklist[ret].inbuf;
1087 fabian 1.25 socklist[ret].inbuflen = strlen(p) - 510;
1088 tothwolf 1.42 socklist[ret].inbuf = (char *) malloc(socklist[ret].inbuflen + 1);
1089 segfault 1.1 strcpy(socklist[ret].inbuf, p + 510);
1090     *(p + 510) = 0;
1091     strcpy(xx, p);
1092 tothwolf 1.42 free(p);
1093 segfault 1.1 /* (leave the rest to be post-pended later) */
1094     }
1095     }
1096 fabian 1.16 /* Look for EOL marker; if it's there, i have something to show */
1097 segfault 1.1 p = strchr(xx, '\n');
1098     if (p == NULL)
1099     p = strchr(xx, '\r');
1100     if (p != NULL) {
1101     *p = 0;
1102     strcpy(s, xx);
1103     strcpy(xx, p + 1);
1104     if (s[strlen(s) - 1] == '\r')
1105     s[strlen(s) - 1] = 0;
1106     data = 1; /* DCC_CHAT may now need to process a
1107 fabian 1.16 blank line */
1108 segfault 1.1 /* NO! */
1109     /* if (!s[0]) strcpy(s," "); */
1110     } else {
1111     s[0] = 0;
1112     if (strlen(xx) >= 510) {
1113 fabian 1.16 /* String is too long, so just insert fake \n */
1114 segfault 1.1 strcpy(s, xx);
1115     xx[0] = 0;
1116     data = 1;
1117     }
1118     }
1119     *len = strlen(s);
1120 fabian 1.16 /* Anything left that needs to be saved? */
1121 segfault 1.1 if (!xx[0]) {
1122     if (data)
1123     return socklist[ret].sock;
1124     else
1125     return -3;
1126     }
1127 fabian 1.16 /* Prepend old data back */
1128 segfault 1.1 if (socklist[ret].inbuf != NULL) {
1129     p = socklist[ret].inbuf;
1130 fabian 1.25 socklist[ret].inbuflen = strlen(p) + strlen(xx);
1131 tothwolf 1.42 socklist[ret].inbuf = (char *) malloc(socklist[ret].inbuflen + 1);
1132 segfault 1.1 strcpy(socklist[ret].inbuf, xx);
1133     strcat(socklist[ret].inbuf, p);
1134 tothwolf 1.42 free(p);
1135 segfault 1.1 } else {
1136 fabian 1.25 socklist[ret].inbuflen = strlen(xx);
1137 tothwolf 1.42 socklist[ret].inbuf = (char *) malloc(socklist[ret].inbuflen + 1);
1138 segfault 1.1 strcpy(socklist[ret].inbuf, xx);
1139     }
1140     if (data) {
1141     return socklist[ret].sock;
1142     } else {
1143     return -3;
1144     }
1145     }
1146    
1147 fabian 1.16 /* Dump something to a socket
1148     */
1149 fabian 1.12 void tputs(register int z, char *s, unsigned int len)
1150 segfault 1.1 {
1151 fabian 1.12 register int i, x, idx;
1152 segfault 1.1 char *p;
1153 fabian 1.12 static int inhere = 0;
1154 segfault 1.1
1155     if (z < 0)
1156     return; /* um... HELLO?! sanity check please! */
1157     if (((z == STDOUT) || (z == STDERR)) && (!backgrd || use_stderr)) {
1158     write(z, s, len);
1159     return;
1160     }
1161     for (i = 0; i < MAXSOCKS; i++) {
1162     if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == z)) {
1163 fabian 1.10 for (idx = 0; idx < dcc_total; idx++) {
1164     if (dcc[idx].sock == z) {
1165     if (dcc[idx].type) {
1166     if (dcc[idx].type->name) {
1167     if (!strncmp(dcc[idx].type->name, "BOT", 3)) {
1168     otraffic_bn_today += len;
1169     break;
1170     } else if (!strcmp(dcc[idx].type->name, "SERVER")) {
1171     otraffic_irc_today += len;
1172     break;
1173     } else if (!strncmp(dcc[idx].type->name, "CHAT", 4)) {
1174     otraffic_dcc_today += len;
1175     break;
1176     } else if (!strncmp(dcc[idx].type->name, "FILES", 5)) {
1177     otraffic_filesys_today += len;
1178     break;
1179     } else if (!strcmp(dcc[idx].type->name, "SEND")) {
1180     otraffic_trans_today += len;
1181     break;
1182     } else if (!strncmp(dcc[idx].type->name, "GET", 3)) {
1183     otraffic_trans_today += len;
1184     break;
1185     } else {
1186     otraffic_unknown_today += len;
1187     break;
1188     }
1189     }
1190     }
1191     }
1192     }
1193    
1194 drummer 1.37 if (socklist[i].outbuf != NULL) {
1195     /* Already queueing: just add it */
1196 tothwolf 1.42 p = (char *) realloc(socklist[i].outbuf, socklist[i].outbuflen + len);
1197     memcpy(p + socklist[i].outbuflen, s, len);
1198 segfault 1.1 socklist[i].outbuf = p;
1199     socklist[i].outbuflen += len;
1200     return;
1201     }
1202 fabian 1.16 /* Try. */
1203 segfault 1.1 x = write(z, s, len);
1204     if (x == (-1))
1205     x = 0;
1206     if (x < len) {
1207 fabian 1.16 /* Socket is full, queue it */
1208 tothwolf 1.42 socklist[i].outbuf = (char *) malloc(len - x);
1209     memcpy(socklist[i].outbuf, &s[x], len - x);
1210 segfault 1.1 socklist[i].outbuflen = len - x;
1211     }
1212     return;
1213     }
1214     }
1215 fabian 1.12 /* Make sure we don't cause a crash by looping here */
1216     if (!inhere) {
1217     inhere = 1;
1218 fabian 1.16
1219 fabian 1.12 putlog(LOG_MISC, "*", "!!! writing to nonexistent socket: %d", z);
1220     s[strlen(s) - 1] = 0;
1221     putlog(LOG_MISC, "*", "!-> '%s'", s);
1222 fabian 1.16
1223 fabian 1.12 inhere = 0;
1224     }
1225 segfault 1.1 }
1226    
1227     /* tputs might queue data for sockets, let's dump as much of it as
1228 fabian 1.16 * possible.
1229     */
1230 segfault 1.1 void dequeue_sockets()
1231     {
1232     int i, x;
1233    
1234     for (i = 0; i < MAXSOCKS; i++) {
1235 drummer 1.37 if (!(socklist[i].flags & SOCK_UNUSED) &&
1236 fabian 1.22 socklist[i].outbuf != NULL) {
1237 fabian 1.16 /* Trick tputs into doing the work */
1238 segfault 1.1 x = write(socklist[i].sock, socklist[i].outbuf,
1239     socklist[i].outbuflen);
1240     if ((x < 0) && (errno != EAGAIN)
1241     #ifdef EBADSLT
1242     && (errno != EBADSLT)
1243     #endif
1244     #ifdef ENOTCONN
1245     && (errno != ENOTCONN)
1246     #endif
1247     ) {
1248 fabian 1.16 /* This detects an EOF during writing */
1249 segfault 1.1 debug3("net: eof!(write) socket %d (%s,%d)", socklist[i].sock,
1250     strerror(errno), errno);
1251     socklist[i].flags |= SOCK_EOFD;
1252     } else if (x == socklist[i].outbuflen) {
1253 fabian 1.16 /* If the whole buffer was sent, nuke it */
1254 tothwolf 1.42 free_null(socklist[i].outbuf);
1255 segfault 1.1 socklist[i].outbuflen = 0;
1256     } else if (x > 0) {
1257     char *p = socklist[i].outbuf;
1258    
1259 fabian 1.16 /* This removes any sent bytes from the beginning of the buffer */
1260 tothwolf 1.42 socklist[i].outbuf = (char *) malloc(socklist[i].outbuflen - x);
1261     memcpy(socklist[i].outbuf, p + x, socklist[i].outbuflen - x);
1262 segfault 1.1 socklist[i].outbuflen -= x;
1263 tothwolf 1.42 free(p);
1264 segfault 1.1 }
1265 fabian 1.15
1266     /* All queued data was sent. Call handler if one exists and the
1267     * dcc entry wants it.
1268     */
1269     if (!socklist[i].outbuf) {
1270     int idx = findanyidx(socklist[i].sock);
1271    
1272 fabian 1.22 if (idx > 0 && dcc[idx].type && dcc[idx].type->outdone)
1273 fabian 1.15 dcc[idx].type->outdone(idx);
1274     }
1275 segfault 1.1 }
1276     }
1277     }
1278 fabian 1.16
1279 segfault 1.1
1280 fabian 1.16 /*
1281     * Debugging stuff
1282     */
1283 segfault 1.1
1284     void tell_netdebug(int idx)
1285     {
1286     int i;
1287     char s[80];
1288    
1289     dprintf(idx, "Open sockets:");
1290     for (i = 0; i < MAXSOCKS; i++) {
1291     if (!(socklist[i].flags & SOCK_UNUSED)) {
1292     sprintf(s, " %d", socklist[i].sock);
1293     if (socklist[i].flags & SOCK_BINARY)
1294     strcat(s, " (binary)");
1295     if (socklist[i].flags & SOCK_LISTEN)
1296     strcat(s, " (listen)");
1297 fabian 1.3 if (socklist[i].flags & SOCK_PASS)
1298     strcat(s, " (passed on)");
1299 segfault 1.1 if (socklist[i].flags & SOCK_CONNECT)
1300     strcat(s, " (connecting)");
1301     if (socklist[i].flags & SOCK_STRONGCONN)
1302     strcat(s, " (strong)");
1303     if (socklist[i].flags & SOCK_NONSOCK)
1304     strcat(s, " (file)");
1305     if (socklist[i].inbuf != NULL)
1306     sprintf(&s[strlen(s)], " (inbuf: %04X)", strlen(socklist[i].inbuf));
1307     if (socklist[i].outbuf != NULL)
1308     sprintf(&s[strlen(s)], " (outbuf: %06lX)", socklist[i].outbuflen);
1309     strcat(s, ",");
1310     dprintf(idx, "%s", s);
1311     }
1312     }
1313     dprintf(idx, " done.\n");
1314 fabian 1.15 }
1315    
1316     /* Checks wether the referenced socket has data queued.
1317     *
1318     * Returns true if the incoming/outgoing (depending on 'type') queues
1319     * contain data, otherwise false.
1320     */
1321     int sock_has_data(int type, int sock)
1322     {
1323     int ret = 0, i;
1324    
1325     for (i = 0; i < MAXSOCKS; i++)
1326 fabian 1.22 if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
1327 fabian 1.15 break;
1328     if (i < MAXSOCKS) {
1329     switch (type) {
1330     case SOCK_DATA_OUTGOING:
1331     ret = (socklist[i].outbuf != NULL);
1332     break;
1333     case SOCK_DATA_INCOMING:
1334     ret = (socklist[i].inbuf != NULL);
1335     break;
1336     }
1337 fabian 1.22 } else
1338     debug1("sock_has_data: could not find socket #%d, returning false.", sock);
1339 fabian 1.15 return ret;
1340 poptix 1.30 }
1341    
1342     /* flush_inbuf():
1343     * checks if there's data in the incoming buffer of an connection
1344     * and flushs the buffer if possible
1345     *
1346     * returns: -1 if the dcc entry wasn't found
1347     * -2 if dcc[idx].type->activity doesn't exist and the data couldn't
1348     * be handled
1349     * 0 if buffer was empty
1350     * otherwise length of flushed buffer
1351     */
1352     int flush_inbuf(int idx)
1353     {
1354     int i, len;
1355     char *inbuf;
1356    
1357 tothwolf 1.45 assert((idx >= 0) && (idx < dcc_total));
1358 poptix 1.30 for (i = 0; i < MAXSOCKS; i++) {
1359     if ((dcc[idx].sock == socklist[i].sock)
1360     && !(socklist[i].flags & SOCK_UNUSED)) {
1361     len = socklist[i].inbuflen;
1362     if ((len > 0) && socklist[i].inbuf) {
1363     if (dcc[idx].type && dcc[idx].type->activity) {
1364     inbuf = socklist[i].inbuf;
1365     socklist[i].inbuf = NULL;
1366     dcc[idx].type->activity(idx, inbuf, len);
1367 tothwolf 1.42 free(inbuf);
1368 poptix 1.30 return len;
1369     } else
1370     return -2;
1371     } else
1372     return 0;
1373     }
1374     }
1375     return -1;
1376 segfault 1.1 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23