/[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.60 - (hide annotations) (download) (as text)
Sat May 18 07:41:33 2002 UTC (17 years, 1 month ago) by stdarg
Branch: MAIN
Changes since 1.59: +4 -4 lines
File MIME type: text/x-chdr
* Changed inet_aton(...) to inet_pton(AF_INET, ...) and removed inet_aton from lib/compat -- why have both?

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23