/[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.49 - (hide annotations) (download) (as text)
Sun Oct 28 13:30:33 2001 UTC (17 years, 7 months ago) by ite
Branch: MAIN
Changes since 1.48: +2 -2 lines
File MIME type: text/x-chdr
Renamed src/adns, src/compat, src/egglib to lib/adns, lib/compat, lib/egglib respectively.

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.49 * $Id: net.c,v 1.48 2001/10/19 01:55:05 tothwolf 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 tothwolf 1.42 malloc_strcpy(orign2, 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 segfault 1.1 int fds, i, x;
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     FD_SET(STDIN, &fd);
832     else
833     FD_SET(socklist[i].sock, &fd);
834     }
835 drummer 1.33 tnow.tv_sec = time(NULL);
836     tnow.tv_usec = 0;
837     adns_beforeselect(ads, &fds, &fd, &fdw, &fde, &pt, 0, &tnow);
838 segfault 1.1 #ifdef HPUX_HACKS
839     #ifndef HPUX10_HACKS
840     x = select(fds, (int *) &fd, (int *) NULL, (int *) NULL, &t);
841     #else
842     x = select(fds, &fd, NULL, NULL, &t);
843     #endif
844     #else
845     x = select(fds, &fd, NULL, NULL, &t);
846     #endif
847 drummer 1.33 tnow.tv_sec = time(NULL);
848     tnow.tv_usec = 0;
849     adns_afterselect(ads, fds, &fd, &fdw, &fde, &tnow);
850    
851     /* dns stuff */
852     egg_dns_checkall();
853    
854 segfault 1.1 if (x > 0) {
855 fabian 1.16 /* Something happened */
856 segfault 1.1 for (i = 0; i < MAXSOCKS; i++) {
857     if ((!(socklist[i].flags & SOCK_UNUSED)) &&
858     ((FD_ISSET(socklist[i].sock, &fd)) ||
859     ((socklist[i].sock == STDOUT) && (!backgrd) &&
860     (FD_ISSET(STDIN, &fd))))) {
861     if (socklist[i].flags & (SOCK_LISTEN | SOCK_CONNECT)) {
862 fabian 1.16 /* Listening socket -- don't read, just return activity */
863     /* Same for connection attempt */
864 segfault 1.1 /* (for strong connections, require a read to succeed first) */
865     if (socklist[i].flags & SOCK_PROXYWAIT) { /* drummer */
866 fabian 1.16 /* Hang around to get the return code from proxy */
867 segfault 1.1 grab = 10;
868     } else if (!(socklist[i].flags & SOCK_STRONGCONN)) {
869     debug1("net: connect! sock %d", socklist[i].sock);
870     s[0] = 0;
871     *len = 0;
872     return i;
873     }
874 fabian 1.3 } else if (socklist[i].flags & SOCK_PASS) {
875     s[0] = 0;
876     *len = 0;
877     return i;
878 segfault 1.1 }
879     if ((socklist[i].sock == STDOUT) && !backgrd)
880     x = read(STDIN, s, grab);
881     else
882     x = read(socklist[i].sock, s, grab);
883     if (x <= 0) { /* eof */
884 guppy 1.31 if (errno == EAGAIN) {
885 segfault 1.1 s[0] = 0;
886     *len = 0;
887     return -3;
888     }
889     *len = socklist[i].sock;
890     socklist[i].flags &= ~SOCK_CONNECT;
891     debug1("net: eof!(read) socket %d", socklist[i].sock);
892     return -1;
893     }
894     s[x] = 0;
895     *len = x;
896     if (socklist[i].flags & SOCK_PROXYWAIT) {
897     debug2("net: socket: %d proxy errno: %d", socklist[i].sock, s[1]);
898     socklist[i].flags &= ~(SOCK_CONNECT | SOCK_PROXYWAIT);
899     switch (s[1]) {
900 fabian 1.16 case 90: /* Success */
901 segfault 1.1 s[0] = 0;
902     *len = 0;
903     return i;
904 fabian 1.16 case 91: /* Failed */
905 segfault 1.1 errno = ECONNREFUSED;
906     break;
907 fabian 1.16 case 92: /* No identd */
908     case 93: /* Identd said wrong username */
909     /* A better error message would be "socks misconfigured"
910     * or "identd not working" but this is simplest.
911     */
912 segfault 1.1 errno = ENETUNREACH;
913     break;
914     }
915     *len = socklist[i].sock;
916     return -1;
917     }
918     return i;
919     }
920     }
921     } else if (x == -1)
922     return -2; /* socket error */
923     else {
924     s[0] = 0;
925     *len = 0;
926     }
927     return -3;
928     }
929    
930     /* sockgets: buffer and read from sockets
931     *
932 fabian 1.16 * Attempts to read from all registered sockets for up to one second. if
933 segfault 1.1 * after one second, no complete data has been received from any of the
934     * sockets, 's' will be empty, 'len' will be 0, and sockgets will return -3.
935     * if there is returnable data received from a socket, the data will be
936     * in 's' (null-terminated if non-binary), the length will be returned
937     * in len, and the socket number will be returned.
938     * normal sockets have their input buffered, and each call to sockgets
939     * will return one line terminated with a '\n'. binary sockets are not
940     * buffered and return whatever coems in as soon as it arrives.
941     * listening sockets will return an empty string when a connection comes in.
942     * connecting sockets will return an empty string on a successful connect,
943     * or EOF on a failed connect.
944     * if an EOF is detected from any of the sockets, that socket number will be
945     * put in len, and -1 will be returned.
946     * the maximum length of the string returned is 512 (including null)
947 fabian 1.25 *
948     * Returns -4 if we handled something that shouldn't be handled by the
949     * dcc functions. Simply ignore it.
950 segfault 1.1 */
951    
952     int sockgets(char *s, int *len)
953     {
954     char xx[514], *p, *px;
955     int ret, i, data = 0;
956    
957     for (i = 0; i < MAXSOCKS; i++) {
958 fabian 1.16 /* Check for stored-up data waiting to be processed */
959 fabian 1.25 if (!(socklist[i].flags & SOCK_UNUSED) &&
960     !(socklist[i].flags & SOCK_BUFFER) && (socklist[i].inbuf != NULL)) {
961     if (!(socklist[i].flags & SOCK_BINARY)) {
962     /* look for \r too cos windows can't follow RFCs */
963     p = strchr(socklist[i].inbuf, '\n');
964     if (p == NULL)
965     p = strchr(socklist[i].inbuf, '\r');
966     if (p != NULL) {
967     *p = 0;
968     if (strlen(socklist[i].inbuf) > 510)
969     socklist[i].inbuf[510] = 0;
970     strcpy(s, socklist[i].inbuf);
971 tothwolf 1.42 /* Uhm... very strange way to code this... */
972     px = (char *) malloc(strlen(p + 1) + 1);
973 fabian 1.25 strcpy(px, p + 1);
974 tothwolf 1.42 free(socklist[i].inbuf);
975 fabian 1.25 if (px[0])
976     socklist[i].inbuf = px;
977     else {
978 tothwolf 1.42 free(px);
979 fabian 1.25 socklist[i].inbuf = NULL;
980     }
981     /* Strip CR if this was CR/LF combo */
982     if (s[strlen(s) - 1] == '\r')
983     s[strlen(s) - 1] = 0;
984     *len = strlen(s);
985     return socklist[i].sock;
986     }
987     } else {
988     /* Handling buffered binary data (must have been SOCK_BUFFER before). */
989     if (socklist[i].inbuflen <= 510) {
990     *len = socklist[i].inbuflen;
991 tothwolf 1.42 memcpy(s, socklist[i].inbuf, socklist[i].inbuflen);
992     free_null(socklist[i].inbuf);
993 fabian 1.25 socklist[i].inbuflen = 0;
994     } else {
995     /* Split up into chunks of 510 bytes. */
996     *len = 510;
997 tothwolf 1.42 memcpy(s, socklist[i].inbuf, *len);
998     memcpy(socklist[i].inbuf, socklist[i].inbuf + *len, *len);
999 fabian 1.25 socklist[i].inbuflen -= *len;
1000 tothwolf 1.42 socklist[i].inbuf = realloc(socklist[i].inbuf,
1001 fabian 1.25 socklist[i].inbuflen);
1002 segfault 1.1 }
1003     return socklist[i].sock;
1004     }
1005     }
1006 fabian 1.16 /* Also check any sockets that might have EOF'd during write */
1007 segfault 1.1 if (!(socklist[i].flags & SOCK_UNUSED)
1008     && (socklist[i].flags & SOCK_EOFD)) {
1009     s[0] = 0;
1010     *len = socklist[i].sock;
1011     return -1;
1012     }
1013     }
1014 fabian 1.16 /* No pent-up data of any worth -- down to business */
1015 segfault 1.1 *len = 0;
1016     ret = sockread(xx, len);
1017     if (ret < 0) {
1018     s[0] = 0;
1019     return ret;
1020     }
1021 fabian 1.16 /* Binary, listening and passed on sockets don't get buffered. */
1022 segfault 1.1 if (socklist[ret].flags & SOCK_CONNECT) {
1023     if (socklist[ret].flags & SOCK_STRONGCONN) {
1024     socklist[ret].flags &= ~SOCK_STRONGCONN;
1025 fabian 1.25 /* Buffer any data that came in, for future read. */
1026     socklist[ret].inbuflen = *len;
1027 tothwolf 1.42 socklist[ret].inbuf = (char *) malloc(*len + 1);
1028 fabian 1.25 /* It might be binary data. You never know. */
1029 tothwolf 1.42 memcpy(socklist[ret].inbuf, xx, *len);
1030 fabian 1.25 socklist[ret].inbuf[*len] = 0;
1031 segfault 1.1 }
1032     socklist[ret].flags &= ~SOCK_CONNECT;
1033     s[0] = 0;
1034     return socklist[ret].sock;
1035     }
1036     if (socklist[ret].flags & SOCK_BINARY) {
1037 tothwolf 1.42 memcpy(s, xx, *len);
1038 segfault 1.1 return socklist[ret].sock;
1039     }
1040 fabian 1.3 if ((socklist[ret].flags & SOCK_LISTEN) ||
1041     (socklist[ret].flags & SOCK_PASS))
1042 segfault 1.1 return socklist[ret].sock;
1043 fabian 1.25 if (socklist[ret].flags & SOCK_BUFFER) {
1044 tothwolf 1.42 socklist[ret].inbuf = (char *) realloc(socklist[ret].inbuf,
1045 fabian 1.25 socklist[ret].inbuflen + *len + 1);
1046 tothwolf 1.42 memcpy(socklist[ret].inbuf + socklist[ret].inbuflen, xx, *len);
1047 fabian 1.25 socklist[ret].inbuflen += *len;
1048     /* We don't know whether it's binary data. Make sure normal strings
1049     will be handled properly later on too. */
1050     socklist[ret].inbuf[socklist[ret].inbuflen] = 0;
1051     return -4; /* Ignore this one. */
1052     }
1053 fabian 1.16 /* Might be necessary to prepend stored-up data! */
1054 segfault 1.1 if (socklist[ret].inbuf != NULL) {
1055     p = socklist[ret].inbuf;
1056 tothwolf 1.42 socklist[ret].inbuf = (char *) malloc(strlen(p) + strlen(xx) + 1);
1057 segfault 1.1 strcpy(socklist[ret].inbuf, p);
1058     strcat(socklist[ret].inbuf, xx);
1059 tothwolf 1.42 free(p);
1060 segfault 1.1 if (strlen(socklist[ret].inbuf) < 512) {
1061     strcpy(xx, socklist[ret].inbuf);
1062 tothwolf 1.42 free_null(socklist[ret].inbuf);
1063 fabian 1.25 socklist[ret].inbuflen = 0;
1064 segfault 1.1 } else {
1065     p = socklist[ret].inbuf;
1066 fabian 1.25 socklist[ret].inbuflen = strlen(p) - 510;
1067 tothwolf 1.42 socklist[ret].inbuf = (char *) malloc(socklist[ret].inbuflen + 1);
1068 segfault 1.1 strcpy(socklist[ret].inbuf, p + 510);
1069     *(p + 510) = 0;
1070     strcpy(xx, p);
1071 tothwolf 1.42 free(p);
1072 segfault 1.1 /* (leave the rest to be post-pended later) */
1073     }
1074     }
1075 fabian 1.16 /* Look for EOL marker; if it's there, i have something to show */
1076 segfault 1.1 p = strchr(xx, '\n');
1077     if (p == NULL)
1078     p = strchr(xx, '\r');
1079     if (p != NULL) {
1080     *p = 0;
1081     strcpy(s, xx);
1082     strcpy(xx, p + 1);
1083     if (s[strlen(s) - 1] == '\r')
1084     s[strlen(s) - 1] = 0;
1085     data = 1; /* DCC_CHAT may now need to process a
1086 fabian 1.16 blank line */
1087 segfault 1.1 /* NO! */
1088     /* if (!s[0]) strcpy(s," "); */
1089     } else {
1090     s[0] = 0;
1091     if (strlen(xx) >= 510) {
1092 fabian 1.16 /* String is too long, so just insert fake \n */
1093 segfault 1.1 strcpy(s, xx);
1094     xx[0] = 0;
1095     data = 1;
1096     }
1097     }
1098     *len = strlen(s);
1099 fabian 1.16 /* Anything left that needs to be saved? */
1100 segfault 1.1 if (!xx[0]) {
1101     if (data)
1102     return socklist[ret].sock;
1103     else
1104     return -3;
1105     }
1106 fabian 1.16 /* Prepend old data back */
1107 segfault 1.1 if (socklist[ret].inbuf != NULL) {
1108     p = socklist[ret].inbuf;
1109 fabian 1.25 socklist[ret].inbuflen = strlen(p) + strlen(xx);
1110 tothwolf 1.42 socklist[ret].inbuf = (char *) malloc(socklist[ret].inbuflen + 1);
1111 segfault 1.1 strcpy(socklist[ret].inbuf, xx);
1112     strcat(socklist[ret].inbuf, p);
1113 tothwolf 1.42 free(p);
1114 segfault 1.1 } else {
1115 fabian 1.25 socklist[ret].inbuflen = strlen(xx);
1116 tothwolf 1.42 socklist[ret].inbuf = (char *) malloc(socklist[ret].inbuflen + 1);
1117 segfault 1.1 strcpy(socklist[ret].inbuf, xx);
1118     }
1119     if (data) {
1120     return socklist[ret].sock;
1121     } else {
1122     return -3;
1123     }
1124     }
1125    
1126 fabian 1.16 /* Dump something to a socket
1127     *
1128     * NOTE: Do NOT put Contexts in here if you want DEBUG to be meaningful!!
1129     */
1130 fabian 1.12 void tputs(register int z, char *s, unsigned int len)
1131 segfault 1.1 {
1132 fabian 1.12 register int i, x, idx;
1133 segfault 1.1 char *p;
1134 fabian 1.12 static int inhere = 0;
1135 segfault 1.1
1136     if (z < 0)
1137     return; /* um... HELLO?! sanity check please! */
1138     if (((z == STDOUT) || (z == STDERR)) && (!backgrd || use_stderr)) {
1139     write(z, s, len);
1140     return;
1141     }
1142     for (i = 0; i < MAXSOCKS; i++) {
1143     if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == z)) {
1144 fabian 1.10 for (idx = 0; idx < dcc_total; idx++) {
1145     if (dcc[idx].sock == z) {
1146     if (dcc[idx].type) {
1147     if (dcc[idx].type->name) {
1148     if (!strncmp(dcc[idx].type->name, "BOT", 3)) {
1149     otraffic_bn_today += len;
1150     break;
1151     } else if (!strcmp(dcc[idx].type->name, "SERVER")) {
1152     otraffic_irc_today += len;
1153     break;
1154     } else if (!strncmp(dcc[idx].type->name, "CHAT", 4)) {
1155     otraffic_dcc_today += len;
1156     break;
1157     } else if (!strncmp(dcc[idx].type->name, "FILES", 5)) {
1158     otraffic_filesys_today += len;
1159     break;
1160     } else if (!strcmp(dcc[idx].type->name, "SEND")) {
1161     otraffic_trans_today += len;
1162     break;
1163     } else if (!strncmp(dcc[idx].type->name, "GET", 3)) {
1164     otraffic_trans_today += len;
1165     break;
1166     } else {
1167     otraffic_unknown_today += len;
1168     break;
1169     }
1170     }
1171     }
1172     }
1173     }
1174    
1175 drummer 1.37 if (socklist[i].outbuf != NULL) {
1176     /* Already queueing: just add it */
1177 tothwolf 1.42 p = (char *) realloc(socklist[i].outbuf, socklist[i].outbuflen + len);
1178     memcpy(p + socklist[i].outbuflen, s, len);
1179 segfault 1.1 socklist[i].outbuf = p;
1180     socklist[i].outbuflen += len;
1181     return;
1182     }
1183 fabian 1.16 /* Try. */
1184 segfault 1.1 x = write(z, s, len);
1185     if (x == (-1))
1186     x = 0;
1187     if (x < len) {
1188 fabian 1.16 /* Socket is full, queue it */
1189 tothwolf 1.42 socklist[i].outbuf = (char *) malloc(len - x);
1190     memcpy(socklist[i].outbuf, &s[x], len - x);
1191 segfault 1.1 socklist[i].outbuflen = len - x;
1192     }
1193     return;
1194     }
1195     }
1196 fabian 1.12 /* Make sure we don't cause a crash by looping here */
1197     if (!inhere) {
1198     inhere = 1;
1199 fabian 1.16
1200 fabian 1.12 putlog(LOG_MISC, "*", "!!! writing to nonexistent socket: %d", z);
1201     s[strlen(s) - 1] = 0;
1202     putlog(LOG_MISC, "*", "!-> '%s'", s);
1203 fabian 1.16
1204 fabian 1.12 inhere = 0;
1205     }
1206 segfault 1.1 }
1207    
1208     /* tputs might queue data for sockets, let's dump as much of it as
1209 fabian 1.16 * possible.
1210     */
1211 segfault 1.1 void dequeue_sockets()
1212     {
1213     int i, x;
1214    
1215     for (i = 0; i < MAXSOCKS; i++) {
1216 drummer 1.37 if (!(socklist[i].flags & SOCK_UNUSED) &&
1217 fabian 1.22 socklist[i].outbuf != NULL) {
1218 fabian 1.16 /* Trick tputs into doing the work */
1219 segfault 1.1 x = write(socklist[i].sock, socklist[i].outbuf,
1220     socklist[i].outbuflen);
1221     if ((x < 0) && (errno != EAGAIN)
1222     #ifdef EBADSLT
1223     && (errno != EBADSLT)
1224     #endif
1225     #ifdef ENOTCONN
1226     && (errno != ENOTCONN)
1227     #endif
1228     ) {
1229 fabian 1.16 /* This detects an EOF during writing */
1230 segfault 1.1 debug3("net: eof!(write) socket %d (%s,%d)", socklist[i].sock,
1231     strerror(errno), errno);
1232     socklist[i].flags |= SOCK_EOFD;
1233     } else if (x == socklist[i].outbuflen) {
1234 fabian 1.16 /* If the whole buffer was sent, nuke it */
1235 tothwolf 1.42 free_null(socklist[i].outbuf);
1236 segfault 1.1 socklist[i].outbuflen = 0;
1237     } else if (x > 0) {
1238     char *p = socklist[i].outbuf;
1239    
1240 fabian 1.16 /* This removes any sent bytes from the beginning of the buffer */
1241 tothwolf 1.42 socklist[i].outbuf = (char *) malloc(socklist[i].outbuflen - x);
1242     memcpy(socklist[i].outbuf, p + x, socklist[i].outbuflen - x);
1243 segfault 1.1 socklist[i].outbuflen -= x;
1244 tothwolf 1.42 free(p);
1245 segfault 1.1 }
1246 fabian 1.15
1247     /* All queued data was sent. Call handler if one exists and the
1248     * dcc entry wants it.
1249     */
1250     if (!socklist[i].outbuf) {
1251     int idx = findanyidx(socklist[i].sock);
1252    
1253 fabian 1.22 if (idx > 0 && dcc[idx].type && dcc[idx].type->outdone)
1254 fabian 1.15 dcc[idx].type->outdone(idx);
1255     }
1256 segfault 1.1 }
1257     }
1258     }
1259 fabian 1.16
1260 segfault 1.1
1261 fabian 1.16 /*
1262     * Debugging stuff
1263     */
1264 segfault 1.1
1265     void tell_netdebug(int idx)
1266     {
1267     int i;
1268     char s[80];
1269    
1270     dprintf(idx, "Open sockets:");
1271     for (i = 0; i < MAXSOCKS; i++) {
1272     if (!(socklist[i].flags & SOCK_UNUSED)) {
1273     sprintf(s, " %d", socklist[i].sock);
1274     if (socklist[i].flags & SOCK_BINARY)
1275     strcat(s, " (binary)");
1276     if (socklist[i].flags & SOCK_LISTEN)
1277     strcat(s, " (listen)");
1278 fabian 1.3 if (socklist[i].flags & SOCK_PASS)
1279     strcat(s, " (passed on)");
1280 segfault 1.1 if (socklist[i].flags & SOCK_CONNECT)
1281     strcat(s, " (connecting)");
1282     if (socklist[i].flags & SOCK_STRONGCONN)
1283     strcat(s, " (strong)");
1284     if (socklist[i].flags & SOCK_NONSOCK)
1285     strcat(s, " (file)");
1286     if (socklist[i].inbuf != NULL)
1287     sprintf(&s[strlen(s)], " (inbuf: %04X)", strlen(socklist[i].inbuf));
1288     if (socklist[i].outbuf != NULL)
1289     sprintf(&s[strlen(s)], " (outbuf: %06lX)", socklist[i].outbuflen);
1290     strcat(s, ",");
1291     dprintf(idx, "%s", s);
1292     }
1293     }
1294     dprintf(idx, " done.\n");
1295 fabian 1.15 }
1296    
1297     /* Checks wether the referenced socket has data queued.
1298     *
1299     * Returns true if the incoming/outgoing (depending on 'type') queues
1300     * contain data, otherwise false.
1301     */
1302     int sock_has_data(int type, int sock)
1303     {
1304     int ret = 0, i;
1305    
1306     for (i = 0; i < MAXSOCKS; i++)
1307 fabian 1.22 if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
1308 fabian 1.15 break;
1309     if (i < MAXSOCKS) {
1310     switch (type) {
1311     case SOCK_DATA_OUTGOING:
1312     ret = (socklist[i].outbuf != NULL);
1313     break;
1314     case SOCK_DATA_INCOMING:
1315     ret = (socklist[i].inbuf != NULL);
1316     break;
1317     }
1318 fabian 1.22 } else
1319     debug1("sock_has_data: could not find socket #%d, returning false.", sock);
1320 fabian 1.15 return ret;
1321 poptix 1.30 }
1322    
1323     /* flush_inbuf():
1324     * checks if there's data in the incoming buffer of an connection
1325     * and flushs the buffer if possible
1326     *
1327     * returns: -1 if the dcc entry wasn't found
1328     * -2 if dcc[idx].type->activity doesn't exist and the data couldn't
1329     * be handled
1330     * 0 if buffer was empty
1331     * otherwise length of flushed buffer
1332     */
1333     int flush_inbuf(int idx)
1334     {
1335     int i, len;
1336     char *inbuf;
1337    
1338 tothwolf 1.45 assert((idx >= 0) && (idx < dcc_total));
1339 poptix 1.30 for (i = 0; i < MAXSOCKS; i++) {
1340     if ((dcc[idx].sock == socklist[i].sock)
1341     && !(socklist[i].flags & SOCK_UNUSED)) {
1342     len = socklist[i].inbuflen;
1343     if ((len > 0) && socklist[i].inbuf) {
1344     if (dcc[idx].type && dcc[idx].type->activity) {
1345     inbuf = socklist[i].inbuf;
1346     socklist[i].inbuf = NULL;
1347     dcc[idx].type->activity(idx, inbuf, len);
1348 tothwolf 1.42 free(inbuf);
1349 poptix 1.30 return len;
1350     } else
1351     return -2;
1352     } else
1353     return 0;
1354     }
1355     }
1356     return -1;
1357 segfault 1.1 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23