/[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.43 - (hide annotations) (download) (as text)
Thu Oct 11 13:01:35 2001 UTC (18 years ago) by tothwolf
Branch: MAIN
Changes since 1.42: +2 -2 lines
File MIME type: text/x-chdr
* malloc_strcpy/realloc_strcpy bugfixes

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23