/[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.53 - (hide annotations) (download) (as text)
Sat Jan 19 20:08:58 2002 UTC (17 years, 9 months ago) by ite
Branch: MAIN
Changes since 1.52: +3 -1 lines
File MIME type: text/x-chdr
* varoius header inclusion cleanups
* moved FILEDB* defines from src/egg.h to a more suitable place in modules/filesys/filesys.h
* moved NORMAL and QUICK defines from src/egg.h to a more suitable place in modules/irc/irc.h
* fixed a warning in modules/perlscript/perlscript.c
* created src/misc.h and src/logfile.h. Moved in them the respective prototypes from src/proto.h, and the respective defines from src/egg.h
* moved private structures from src/dns.h to src/dns.c
* moved src/dns.c's related prototypes from src/egg.h to src/dns.h
* moved RES_* defines from src/egg.h to dns.h
* removed some unneeded defines here and there
* moved LOG_* defines from src/egg.h to src/logfile.h
* dns_events wasn't static

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23