/[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.52 - (hide annotations) (download) (as text)
Thu Jan 17 01:13:07 2002 UTC (17 years, 9 months ago) by ite
Branch: MAIN
Changes since 1.51: +14 -4 lines
File MIME type: text/x-chdr
* Hopefully fixed SIGBUS crashes on 64bit archs (synch 1.6)\n

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23