/[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.61 - (hide annotations) (download) (as text)
Sun May 19 04:41:32 2002 UTC (17 years, 5 months ago) by stdarg
Branch: MAIN
Changes since 1.60: +4 -8 lines
File MIME type: text/x-chdr
* Got rid of some inet_ntoa
* Converted dcc ip nums to dotted decimal notation

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.61 static const char rcsid[] = "$Id: net.c,v 1.60 2002/05/18 07:41:33 stdarg 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 stdarg 1.61 static char ipbuf[32];
548 fabian 1.3
549     a.s_addr = ip;
550 stdarg 1.61 return (char *)inet_ntop(AF_INET, &a, ipbuf, sizeof(ipbuf));
551 fabian 1.3 }
552    
553 drummer 1.33 char *getlocaladdr(int sock)
554     {
555     static char buf[ADDRLEN];
556     #ifdef IPV6
557     struct sockaddr_in6 sa;
558     #else
559     struct sockaddr_in sa;
560     #endif
561     int len = sizeof sa;
562    
563     if (sock == -1) { /* assuming IPv4... */
564     sprintf(buf, "%lu", (unsigned long int)
565     ntohl(natip[0] ? inet_addr(natip) : getmyip()));
566     return buf;
567     }
568    
569     if (getsockname(sock, (struct sockaddr*) &sa, &len) == -1) {
570     debug1("|NET| getsockname() failed for sock %d", sock);
571     return 0;
572     }
573     #ifdef IPV6
574     if (IN6_IS_ADDR_V4MAPPED(&sa.sin6_addr))
575     sprintf(buf, "%lu",
576 drummer 1.34 (unsigned long int) ntohl(((uint32_t *)&sa.sin6_addr)[3]));
577 drummer 1.33 else
578 tothwolf 1.48 inet_ntop(AF_INET6, &(sa.sin6_addr), buf, sizeof buf);
579 drummer 1.33 #else
580 drummer 1.36 sprintf(buf, "%lu", (unsigned long int) ntohl(sa.sin_addr.s_addr));
581 drummer 1.33 #endif
582     return buf;
583     }
584    
585 fabian 1.16 /* Short routine to answer a connect received on a socket made previously
586 segfault 1.1 * by open_listen ... returns hostname of the caller & the new socket
587 fabian 1.16 * does NOT dispose of old "public" socket!
588     */
589 drummer 1.33 int answer(int sock, char *caller, char *ip, unsigned short *port,
590 fabian 1.16 int binary)
591 segfault 1.1 {
592 fabian 1.13 int new_sock;
593     unsigned int addrlen;
594 drummer 1.33 #ifdef IPV6
595     struct sockaddr_in6 from;
596     #else
597 segfault 1.1 struct sockaddr_in from;
598 drummer 1.33 #endif
599 fabian 1.25
600 drummer 1.33 addrlen = sizeof from;
601 segfault 1.1 new_sock = accept(sock, (struct sockaddr *) &from, &addrlen);
602     if (new_sock < 0)
603     return -1;
604     if (ip != NULL) {
605 drummer 1.33 #ifdef IPV6
606 drummer 1.35 if (IN6_IS_ADDR_V4MAPPED(&from.sin6_addr))
607 tothwolf 1.48 inet_ntop(AF_INET, &(((uint32_t *)&from.sin6_addr)[3]), ip, ADDRMAX);
608 drummer 1.33 else
609 tothwolf 1.48 inet_ntop(AF_INET6, &(from.sin6_addr), ip, ADDRMAX);
610 drummer 1.33 #else
611 tothwolf 1.48 inet_ntop(AF_INET, &(from.sin_addr.s_addr), ip, ADDRMAX);
612 drummer 1.33 #endif
613 fabian 1.16 /* This is now done asynchronously. We now only provide the IP address.
614 fabian 1.3 */
615 ite 1.55 strlcpy(caller, ip, 121);
616 segfault 1.1 }
617     if (port != NULL)
618 drummer 1.33 #ifdef IPV6
619     *port = ntohs(from.sin6_port);
620     #else
621 fabian 1.21 *port = ntohs(from.sin_port);
622 drummer 1.33 #endif
623 fabian 1.16 /* Set up all the normal socket crap */
624 segfault 1.1 setsock(new_sock, (binary ? SOCK_BINARY : 0));
625     return new_sock;
626     }
627    
628 fabian 1.16 /* Like open_telnet, but uses server & port specifications of dcc
629     */
630 segfault 1.1 int open_telnet_dcc(int sock, char *server, char *port)
631     {
632     int p;
633    
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 stdarg 1.61 return open_telnet_raw(sock, server, p);
642 drummer 1.33 }
643    
644     void egg_dns_gotanswer(int status, adns_answer *aw, char *origname)
645     {
646     char name[UHOSTLEN];
647 tothwolf 1.42 #ifdef IPV6
648 tothwolf 1.43 char *orign2;
649 tothwolf 1.42 #endif
650 drummer 1.33
651     if (!aw) {
652     debug0("|DNS| egg_dns_gotanswer: ANSWER IS NULL!");
653     return;
654     }
655     if (!origname) {
656     debug0("|DNS| egg_dns_gotanswer: origname is NULL!");
657     return;
658     }
659    
660     debug2("|DNS| egg_dns_gotanswer: status=%d adns_answer=%x", status, (int)aw);
661     status = 0;
662     if ((aw->type == adns_r_addr)
663     #ifdef IPV6
664     || (aw->type == adns_r_addr6)
665     #endif
666     ) {
667     if ((aw->status == adns_s_ok) && (aw->nrrs > 0)) {
668     adns_rr_addr *rrp = aw->rrs.untyped;
669     if (rrp->addr.sa.sa_family == AF_INET) {
670 tothwolf 1.48 inet_ntop(AF_INET, &(rrp->addr.inet.sin_addr), name, UHOSTLEN-1);
671 drummer 1.33 status = 1;
672     #ifdef IPV6
673     } else if (rrp->addr.sa.sa_family == AF_INET6) {
674 tothwolf 1.48 inet_ntop(AF_INET6, &(rrp->addr.inet6.sin6_addr), name, UHOSTLEN-1);
675 drummer 1.33 status = 1;
676     #endif
677     }
678     #ifdef IPV6
679     } else if ((aw->type == adns_r_addr /* af_preferred */) &&
680 tothwolf 1.48 strncasecmp(origname, "ipv6%", 5) &&
681     strncasecmp(origname, "ipv4%", 5)) {
682 drummer 1.33 adns_query q6;
683 ite 1.51 orign2 = strdup(origname);
684 drummer 1.33 /* ...it may be AAAA */
685     debug1("|DNS| egg_dns_gotanswer: A failed, checking for AAAA (%s)", origname);
686     adns_submit(ads, origname, adns_r_addr6, 0, orign2, &q6);
687     status = -1;
688     #endif
689     }
690     if (status == 0)
691     strcpy(name, "0.0.0.0");
692     if (status >= 0) {
693     debug3("|DNS| egg_dns_gotanswer: (ipbyhost) host: %s ip: %s status: %d", origname, name, status);
694     call_ipbyhost(origname, name, status);
695     }
696     } else if ((aw->type == adns_r_ptr_ip6) || (aw->type == adns_r_ptr)) {
697     if ((aw->status == adns_s_ok) && (aw->nrrs > 0)) {
698     if (aw->rrs.str) {
699 ite 1.55 strlcpy(name, *(aw->rrs.str), UHOSTLEN);
700 drummer 1.33 status = 1;
701     }
702     }
703     if (!status) {
704     if (origname)
705 ite 1.55 strlcpy(name, origname, UHOSTLEN);
706 drummer 1.33 else
707     strcpy(name, "error");
708     }
709     debug3("|DNS| egg_dns_gotanswer: (hostbyip) ip: %s host: %s status: %d", origname, name, status);
710     call_hostbyip(origname, name, status);
711     } else
712     debug0("|DNS| egg_dns_gotanswer: got unknow type of answer ?!");
713 tothwolf 1.42 free(origname);
714 drummer 1.33 }
715    
716     void egg_dns_checkall()
717     {
718     adns_query q, r;
719     adns_answer *answer;
720     char *origname;
721    
722     adns_forallqueries_begin(ads);
723     while ((q = adns_forallqueries_next(ads, (void **)&r)) != NULL) {
724     switch (adns_check(ads, &q, &answer, (void **)&origname)) {
725     case 0: /* ok */
726     egg_dns_gotanswer(1, answer, origname);
727     break;
728     case EAGAIN: /* Go into the queue again */
729     break;
730     default: /* failed */
731     egg_dns_gotanswer(0, answer, origname);
732     break;
733     }
734     }
735 segfault 1.1 }
736    
737 fabian 1.16 /* Attempts to read from all the sockets in socklist
738 segfault 1.1 * fills s with up to 511 bytes if available, and returns the array index
739 fabian 1.16 *
740     * on EOF: returns -1, with socket in len
741     * on socket error: returns -2
742     * if nothing is ready: returns -3
743     */
744 segfault 1.1 static int sockread(char *s, int *len)
745     {
746 drummer 1.33 fd_set fd, fdw, fde;
747 ite 1.52 int fds, i, x, fdtmp;
748 drummer 1.33 struct timeval t, tnow;
749     struct timeval *pt = &t;
750 segfault 1.1 int grab = 511;
751 stdarg 1.46 egg_timeval_t howlong;
752 segfault 1.1
753     fds = getdtablesize();
754     #ifdef FD_SETSIZE
755     if (fds > FD_SETSIZE)
756 fabian 1.16 fds = FD_SETSIZE; /* Fixes YET ANOTHER freebsd bug!!! */
757 segfault 1.1 #endif
758 stdarg 1.46
759     if (timer_get_shortest(&howlong)) {
760     /* No timer, default to 1 second. */
761     t.tv_sec = 1;
762     t.tv_usec = 0;
763     }
764     else {
765     t.tv_sec = howlong.sec;
766     t.tv_usec = howlong.usec;
767     }
768    
769 segfault 1.1 FD_ZERO(&fd);
770 fabian 1.17
771 segfault 1.1 for (i = 0; i < MAXSOCKS; i++)
772 fabian 1.17 if (!(socklist[i].flags & (SOCK_UNUSED | SOCK_VIRTUAL))) {
773 segfault 1.1 if ((socklist[i].sock == STDOUT) && !backgrd)
774 ite 1.52 fdtmp = STDIN;
775 segfault 1.1 else
776 ite 1.52 fdtmp = socklist[i].sock;
777     /*
778     * Looks like that having more than a call, in the same
779     * program, to the FD_SET macro, triggers a bug in gcc.
780     * SIGBUS crashing binaries used to be produced on a number
781     * (prolly all?) of 64 bits architectures.
782     * Make your best to avoid to make it happen again.
783     *
784     * ITE
785     */
786     FD_SET(fdtmp , &fd);
787 segfault 1.1 }
788 drummer 1.33 tnow.tv_sec = time(NULL);
789     tnow.tv_usec = 0;
790     adns_beforeselect(ads, &fds, &fd, &fdw, &fde, &pt, 0, &tnow);
791 segfault 1.1 #ifdef HPUX_HACKS
792     #ifndef HPUX10_HACKS
793     x = select(fds, (int *) &fd, (int *) NULL, (int *) NULL, &t);
794     #else
795     x = select(fds, &fd, NULL, NULL, &t);
796     #endif
797     #else
798     x = select(fds, &fd, NULL, NULL, &t);
799     #endif
800 drummer 1.33 tnow.tv_sec = time(NULL);
801     tnow.tv_usec = 0;
802     adns_afterselect(ads, fds, &fd, &fdw, &fde, &tnow);
803    
804     /* dns stuff */
805     egg_dns_checkall();
806    
807 segfault 1.1 if (x > 0) {
808 fabian 1.16 /* Something happened */
809 segfault 1.1 for (i = 0; i < MAXSOCKS; i++) {
810     if ((!(socklist[i].flags & SOCK_UNUSED)) &&
811     ((FD_ISSET(socklist[i].sock, &fd)) ||
812     ((socklist[i].sock == STDOUT) && (!backgrd) &&
813     (FD_ISSET(STDIN, &fd))))) {
814     if (socklist[i].flags & (SOCK_LISTEN | SOCK_CONNECT)) {
815 fabian 1.16 /* Listening socket -- don't read, just return activity */
816     /* Same for connection attempt */
817 segfault 1.1 /* (for strong connections, require a read to succeed first) */
818     if (socklist[i].flags & SOCK_PROXYWAIT) { /* drummer */
819 fabian 1.16 /* Hang around to get the return code from proxy */
820 segfault 1.1 grab = 10;
821     } else if (!(socklist[i].flags & SOCK_STRONGCONN)) {
822     debug1("net: connect! sock %d", socklist[i].sock);
823     s[0] = 0;
824     *len = 0;
825     return i;
826     }
827 fabian 1.3 } else if (socklist[i].flags & SOCK_PASS) {
828     s[0] = 0;
829     *len = 0;
830     return i;
831 segfault 1.1 }
832     if ((socklist[i].sock == STDOUT) && !backgrd)
833     x = read(STDIN, s, grab);
834     else
835     x = read(socklist[i].sock, s, grab);
836     if (x <= 0) { /* eof */
837 guppy 1.31 if (errno == EAGAIN) {
838 segfault 1.1 s[0] = 0;
839     *len = 0;
840     return -3;
841     }
842     *len = socklist[i].sock;
843     socklist[i].flags &= ~SOCK_CONNECT;
844     debug1("net: eof!(read) socket %d", socklist[i].sock);
845     return -1;
846     }
847     s[x] = 0;
848     *len = x;
849     if (socklist[i].flags & SOCK_PROXYWAIT) {
850     debug2("net: socket: %d proxy errno: %d", socklist[i].sock, s[1]);
851     socklist[i].flags &= ~(SOCK_CONNECT | SOCK_PROXYWAIT);
852     switch (s[1]) {
853 fabian 1.16 case 90: /* Success */
854 segfault 1.1 s[0] = 0;
855     *len = 0;
856     return i;
857 fabian 1.16 case 91: /* Failed */
858 segfault 1.1 errno = ECONNREFUSED;
859     break;
860 fabian 1.16 case 92: /* No identd */
861     case 93: /* Identd said wrong username */
862     /* A better error message would be "socks misconfigured"
863     * or "identd not working" but this is simplest.
864     */
865 segfault 1.1 errno = ENETUNREACH;
866     break;
867     }
868     *len = socklist[i].sock;
869     return -1;
870     }
871     return i;
872     }
873     }
874     } else if (x == -1)
875     return -2; /* socket error */
876     else {
877     s[0] = 0;
878     *len = 0;
879     }
880     return -3;
881     }
882    
883     /* sockgets: buffer and read from sockets
884     *
885 fabian 1.16 * Attempts to read from all registered sockets for up to one second. if
886 segfault 1.1 * after one second, no complete data has been received from any of the
887     * sockets, 's' will be empty, 'len' will be 0, and sockgets will return -3.
888     * if there is returnable data received from a socket, the data will be
889     * in 's' (null-terminated if non-binary), the length will be returned
890     * in len, and the socket number will be returned.
891     * normal sockets have their input buffered, and each call to sockgets
892     * will return one line terminated with a '\n'. binary sockets are not
893     * buffered and return whatever coems in as soon as it arrives.
894     * listening sockets will return an empty string when a connection comes in.
895     * connecting sockets will return an empty string on a successful connect,
896     * or EOF on a failed connect.
897     * if an EOF is detected from any of the sockets, that socket number will be
898     * put in len, and -1 will be returned.
899     * the maximum length of the string returned is 512 (including null)
900 fabian 1.25 *
901     * Returns -4 if we handled something that shouldn't be handled by the
902     * dcc functions. Simply ignore it.
903 segfault 1.1 */
904    
905     int sockgets(char *s, int *len)
906     {
907     char xx[514], *p, *px;
908     int ret, i, data = 0;
909    
910     for (i = 0; i < MAXSOCKS; i++) {
911 fabian 1.16 /* Check for stored-up data waiting to be processed */
912 fabian 1.25 if (!(socklist[i].flags & SOCK_UNUSED) &&
913     !(socklist[i].flags & SOCK_BUFFER) && (socklist[i].inbuf != NULL)) {
914     if (!(socklist[i].flags & SOCK_BINARY)) {
915     /* look for \r too cos windows can't follow RFCs */
916     p = strchr(socklist[i].inbuf, '\n');
917     if (p == NULL)
918     p = strchr(socklist[i].inbuf, '\r');
919     if (p != NULL) {
920     *p = 0;
921     if (strlen(socklist[i].inbuf) > 510)
922     socklist[i].inbuf[510] = 0;
923     strcpy(s, socklist[i].inbuf);
924 tothwolf 1.42 /* Uhm... very strange way to code this... */
925     px = (char *) malloc(strlen(p + 1) + 1);
926 fabian 1.25 strcpy(px, p + 1);
927 tothwolf 1.42 free(socklist[i].inbuf);
928 fabian 1.25 if (px[0])
929     socklist[i].inbuf = px;
930     else {
931 tothwolf 1.42 free(px);
932 fabian 1.25 socklist[i].inbuf = NULL;
933     }
934     /* Strip CR if this was CR/LF combo */
935     if (s[strlen(s) - 1] == '\r')
936     s[strlen(s) - 1] = 0;
937     *len = strlen(s);
938     return socklist[i].sock;
939     }
940     } else {
941     /* Handling buffered binary data (must have been SOCK_BUFFER before). */
942     if (socklist[i].inbuflen <= 510) {
943     *len = socklist[i].inbuflen;
944 tothwolf 1.42 memcpy(s, socklist[i].inbuf, socklist[i].inbuflen);
945     free_null(socklist[i].inbuf);
946 fabian 1.25 socklist[i].inbuflen = 0;
947     } else {
948     /* Split up into chunks of 510 bytes. */
949     *len = 510;
950 tothwolf 1.42 memcpy(s, socklist[i].inbuf, *len);
951     memcpy(socklist[i].inbuf, socklist[i].inbuf + *len, *len);
952 fabian 1.25 socklist[i].inbuflen -= *len;
953 tothwolf 1.42 socklist[i].inbuf = realloc(socklist[i].inbuf,
954 fabian 1.25 socklist[i].inbuflen);
955 segfault 1.1 }
956     return socklist[i].sock;
957     }
958     }
959 fabian 1.16 /* Also check any sockets that might have EOF'd during write */
960 segfault 1.1 if (!(socklist[i].flags & SOCK_UNUSED)
961     && (socklist[i].flags & SOCK_EOFD)) {
962     s[0] = 0;
963     *len = socklist[i].sock;
964     return -1;
965     }
966     }
967 fabian 1.16 /* No pent-up data of any worth -- down to business */
968 segfault 1.1 *len = 0;
969     ret = sockread(xx, len);
970     if (ret < 0) {
971     s[0] = 0;
972     return ret;
973     }
974 fabian 1.16 /* Binary, listening and passed on sockets don't get buffered. */
975 segfault 1.1 if (socklist[ret].flags & SOCK_CONNECT) {
976     if (socklist[ret].flags & SOCK_STRONGCONN) {
977     socklist[ret].flags &= ~SOCK_STRONGCONN;
978 fabian 1.25 /* Buffer any data that came in, for future read. */
979     socklist[ret].inbuflen = *len;
980 tothwolf 1.42 socklist[ret].inbuf = (char *) malloc(*len + 1);
981 fabian 1.25 /* It might be binary data. You never know. */
982 tothwolf 1.42 memcpy(socklist[ret].inbuf, xx, *len);
983 fabian 1.25 socklist[ret].inbuf[*len] = 0;
984 segfault 1.1 }
985     socklist[ret].flags &= ~SOCK_CONNECT;
986     s[0] = 0;
987     return socklist[ret].sock;
988     }
989     if (socklist[ret].flags & SOCK_BINARY) {
990 tothwolf 1.42 memcpy(s, xx, *len);
991 segfault 1.1 return socklist[ret].sock;
992     }
993 fabian 1.3 if ((socklist[ret].flags & SOCK_LISTEN) ||
994     (socklist[ret].flags & SOCK_PASS))
995 segfault 1.1 return socklist[ret].sock;
996 fabian 1.25 if (socklist[ret].flags & SOCK_BUFFER) {
997 tothwolf 1.42 socklist[ret].inbuf = (char *) realloc(socklist[ret].inbuf,
998 fabian 1.25 socklist[ret].inbuflen + *len + 1);
999 tothwolf 1.42 memcpy(socklist[ret].inbuf + socklist[ret].inbuflen, xx, *len);
1000 fabian 1.25 socklist[ret].inbuflen += *len;
1001     /* We don't know whether it's binary data. Make sure normal strings
1002     will be handled properly later on too. */
1003     socklist[ret].inbuf[socklist[ret].inbuflen] = 0;
1004     return -4; /* Ignore this one. */
1005     }
1006 fabian 1.16 /* Might be necessary to prepend stored-up data! */
1007 segfault 1.1 if (socklist[ret].inbuf != NULL) {
1008     p = socklist[ret].inbuf;
1009 tothwolf 1.42 socklist[ret].inbuf = (char *) malloc(strlen(p) + strlen(xx) + 1);
1010 segfault 1.1 strcpy(socklist[ret].inbuf, p);
1011     strcat(socklist[ret].inbuf, xx);
1012 tothwolf 1.42 free(p);
1013 segfault 1.1 if (strlen(socklist[ret].inbuf) < 512) {
1014     strcpy(xx, socklist[ret].inbuf);
1015 tothwolf 1.42 free_null(socklist[ret].inbuf);
1016 fabian 1.25 socklist[ret].inbuflen = 0;
1017 segfault 1.1 } else {
1018     p = socklist[ret].inbuf;
1019 fabian 1.25 socklist[ret].inbuflen = strlen(p) - 510;
1020 tothwolf 1.42 socklist[ret].inbuf = (char *) malloc(socklist[ret].inbuflen + 1);
1021 segfault 1.1 strcpy(socklist[ret].inbuf, p + 510);
1022     *(p + 510) = 0;
1023     strcpy(xx, p);
1024 tothwolf 1.42 free(p);
1025 segfault 1.1 /* (leave the rest to be post-pended later) */
1026     }
1027     }
1028 fabian 1.16 /* Look for EOL marker; if it's there, i have something to show */
1029 segfault 1.1 p = strchr(xx, '\n');
1030     if (p == NULL)
1031     p = strchr(xx, '\r');
1032     if (p != NULL) {
1033     *p = 0;
1034     strcpy(s, xx);
1035     strcpy(xx, p + 1);
1036     if (s[strlen(s) - 1] == '\r')
1037     s[strlen(s) - 1] = 0;
1038     data = 1; /* DCC_CHAT may now need to process a
1039 fabian 1.16 blank line */
1040 segfault 1.1 /* NO! */
1041     /* if (!s[0]) strcpy(s," "); */
1042     } else {
1043     s[0] = 0;
1044     if (strlen(xx) >= 510) {
1045 fabian 1.16 /* String is too long, so just insert fake \n */
1046 segfault 1.1 strcpy(s, xx);
1047     xx[0] = 0;
1048     data = 1;
1049     }
1050     }
1051     *len = strlen(s);
1052 fabian 1.16 /* Anything left that needs to be saved? */
1053 segfault 1.1 if (!xx[0]) {
1054     if (data)
1055     return socklist[ret].sock;
1056     else
1057     return -3;
1058     }
1059 fabian 1.16 /* Prepend old data back */
1060 segfault 1.1 if (socklist[ret].inbuf != NULL) {
1061     p = socklist[ret].inbuf;
1062 fabian 1.25 socklist[ret].inbuflen = strlen(p) + strlen(xx);
1063 tothwolf 1.42 socklist[ret].inbuf = (char *) malloc(socklist[ret].inbuflen + 1);
1064 segfault 1.1 strcpy(socklist[ret].inbuf, xx);
1065     strcat(socklist[ret].inbuf, p);
1066 tothwolf 1.42 free(p);
1067 segfault 1.1 } else {
1068 fabian 1.25 socklist[ret].inbuflen = strlen(xx);
1069 tothwolf 1.42 socklist[ret].inbuf = (char *) malloc(socklist[ret].inbuflen + 1);
1070 segfault 1.1 strcpy(socklist[ret].inbuf, xx);
1071     }
1072     if (data) {
1073     return socklist[ret].sock;
1074     } else {
1075     return -3;
1076     }
1077     }
1078    
1079 fabian 1.16 /* Dump something to a socket
1080     */
1081 fabian 1.12 void tputs(register int z, char *s, unsigned int len)
1082 segfault 1.1 {
1083 fabian 1.12 register int i, x, idx;
1084 segfault 1.1 char *p;
1085 fabian 1.12 static int inhere = 0;
1086 segfault 1.1
1087     if (z < 0)
1088     return; /* um... HELLO?! sanity check please! */
1089     if (((z == STDOUT) || (z == STDERR)) && (!backgrd || use_stderr)) {
1090     write(z, s, len);
1091     return;
1092     }
1093     for (i = 0; i < MAXSOCKS; i++) {
1094     if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == z)) {
1095 fabian 1.10 for (idx = 0; idx < dcc_total; idx++) {
1096     if (dcc[idx].sock == z) {
1097     if (dcc[idx].type) {
1098     if (dcc[idx].type->name) {
1099     if (!strncmp(dcc[idx].type->name, "BOT", 3)) {
1100 stdarg 1.57 traffic.out_today.bn += len;
1101 fabian 1.10 break;
1102     } else if (!strcmp(dcc[idx].type->name, "SERVER")) {
1103 stdarg 1.57 traffic.out_today.irc += len;
1104 fabian 1.10 break;
1105     } else if (!strncmp(dcc[idx].type->name, "CHAT", 4)) {
1106 stdarg 1.57 traffic.out_today.dcc += len;
1107 fabian 1.10 break;
1108     } else if (!strncmp(dcc[idx].type->name, "FILES", 5)) {
1109 stdarg 1.57 traffic.out_today.filesys += len;
1110 fabian 1.10 break;
1111     } else if (!strcmp(dcc[idx].type->name, "SEND")) {
1112 stdarg 1.57 traffic.out_today.trans += len;
1113 fabian 1.10 break;
1114     } else if (!strncmp(dcc[idx].type->name, "GET", 3)) {
1115 stdarg 1.57 traffic.out_today.trans += len;
1116 fabian 1.10 break;
1117     } else {
1118 stdarg 1.57 traffic.out_today.unknown += len;
1119 fabian 1.10 break;
1120     }
1121     }
1122     }
1123     }
1124     }
1125    
1126 drummer 1.37 if (socklist[i].outbuf != NULL) {
1127     /* Already queueing: just add it */
1128 tothwolf 1.42 p = (char *) realloc(socklist[i].outbuf, socklist[i].outbuflen + len);
1129     memcpy(p + socklist[i].outbuflen, s, len);
1130 segfault 1.1 socklist[i].outbuf = p;
1131     socklist[i].outbuflen += len;
1132     return;
1133     }
1134 fabian 1.16 /* Try. */
1135 segfault 1.1 x = write(z, s, len);
1136     if (x == (-1))
1137     x = 0;
1138     if (x < len) {
1139 fabian 1.16 /* Socket is full, queue it */
1140 tothwolf 1.42 socklist[i].outbuf = (char *) malloc(len - x);
1141     memcpy(socklist[i].outbuf, &s[x], len - x);
1142 segfault 1.1 socklist[i].outbuflen = len - x;
1143     }
1144     return;
1145     }
1146     }
1147 fabian 1.12 /* Make sure we don't cause a crash by looping here */
1148     if (!inhere) {
1149     inhere = 1;
1150 fabian 1.16
1151 fabian 1.12 putlog(LOG_MISC, "*", "!!! writing to nonexistent socket: %d", z);
1152     s[strlen(s) - 1] = 0;
1153     putlog(LOG_MISC, "*", "!-> '%s'", s);
1154 fabian 1.16
1155 fabian 1.12 inhere = 0;
1156     }
1157 segfault 1.1 }
1158    
1159     /* tputs might queue data for sockets, let's dump as much of it as
1160 fabian 1.16 * possible.
1161     */
1162 segfault 1.1 void dequeue_sockets()
1163     {
1164     int i, x;
1165    
1166     for (i = 0; i < MAXSOCKS; i++) {
1167 drummer 1.37 if (!(socklist[i].flags & SOCK_UNUSED) &&
1168 fabian 1.22 socklist[i].outbuf != NULL) {
1169 fabian 1.16 /* Trick tputs into doing the work */
1170 segfault 1.1 x = write(socklist[i].sock, socklist[i].outbuf,
1171     socklist[i].outbuflen);
1172     if ((x < 0) && (errno != EAGAIN)
1173     #ifdef EBADSLT
1174     && (errno != EBADSLT)
1175     #endif
1176     #ifdef ENOTCONN
1177     && (errno != ENOTCONN)
1178     #endif
1179     ) {
1180 fabian 1.16 /* This detects an EOF during writing */
1181 segfault 1.1 debug3("net: eof!(write) socket %d (%s,%d)", socklist[i].sock,
1182     strerror(errno), errno);
1183     socklist[i].flags |= SOCK_EOFD;
1184     } else if (x == socklist[i].outbuflen) {
1185 fabian 1.16 /* If the whole buffer was sent, nuke it */
1186 tothwolf 1.42 free_null(socklist[i].outbuf);
1187 segfault 1.1 socklist[i].outbuflen = 0;
1188     } else if (x > 0) {
1189     char *p = socklist[i].outbuf;
1190    
1191 fabian 1.16 /* This removes any sent bytes from the beginning of the buffer */
1192 tothwolf 1.42 socklist[i].outbuf = (char *) malloc(socklist[i].outbuflen - x);
1193     memcpy(socklist[i].outbuf, p + x, socklist[i].outbuflen - x);
1194 segfault 1.1 socklist[i].outbuflen -= x;
1195 tothwolf 1.42 free(p);
1196 segfault 1.1 }
1197 fabian 1.15
1198     /* All queued data was sent. Call handler if one exists and the
1199     * dcc entry wants it.
1200     */
1201     if (!socklist[i].outbuf) {
1202     int idx = findanyidx(socklist[i].sock);
1203    
1204 fabian 1.22 if (idx > 0 && dcc[idx].type && dcc[idx].type->outdone)
1205 fabian 1.15 dcc[idx].type->outdone(idx);
1206     }
1207 segfault 1.1 }
1208     }
1209     }
1210 fabian 1.16
1211 segfault 1.1
1212 fabian 1.16 /*
1213     * Debugging stuff
1214     */
1215 segfault 1.1
1216     void tell_netdebug(int idx)
1217     {
1218     int i;
1219     char s[80];
1220    
1221     dprintf(idx, "Open sockets:");
1222     for (i = 0; i < MAXSOCKS; i++) {
1223     if (!(socklist[i].flags & SOCK_UNUSED)) {
1224     sprintf(s, " %d", socklist[i].sock);
1225     if (socklist[i].flags & SOCK_BINARY)
1226     strcat(s, " (binary)");
1227     if (socklist[i].flags & SOCK_LISTEN)
1228     strcat(s, " (listen)");
1229 fabian 1.3 if (socklist[i].flags & SOCK_PASS)
1230     strcat(s, " (passed on)");
1231 segfault 1.1 if (socklist[i].flags & SOCK_CONNECT)
1232     strcat(s, " (connecting)");
1233     if (socklist[i].flags & SOCK_STRONGCONN)
1234     strcat(s, " (strong)");
1235     if (socklist[i].flags & SOCK_NONSOCK)
1236     strcat(s, " (file)");
1237     if (socklist[i].inbuf != NULL)
1238     sprintf(&s[strlen(s)], " (inbuf: %04X)", strlen(socklist[i].inbuf));
1239     if (socklist[i].outbuf != NULL)
1240     sprintf(&s[strlen(s)], " (outbuf: %06lX)", socklist[i].outbuflen);
1241     strcat(s, ",");
1242     dprintf(idx, "%s", s);
1243     }
1244     }
1245     dprintf(idx, " done.\n");
1246 fabian 1.15 }
1247    
1248     /* Checks wether the referenced socket has data queued.
1249     *
1250     * Returns true if the incoming/outgoing (depending on 'type') queues
1251     * contain data, otherwise false.
1252     */
1253     int sock_has_data(int type, int sock)
1254     {
1255     int ret = 0, i;
1256    
1257     for (i = 0; i < MAXSOCKS; i++)
1258 fabian 1.22 if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
1259 fabian 1.15 break;
1260     if (i < MAXSOCKS) {
1261     switch (type) {
1262     case SOCK_DATA_OUTGOING:
1263     ret = (socklist[i].outbuf != NULL);
1264     break;
1265     case SOCK_DATA_INCOMING:
1266     ret = (socklist[i].inbuf != NULL);
1267     break;
1268     }
1269 fabian 1.22 } else
1270     debug1("sock_has_data: could not find socket #%d, returning false.", sock);
1271 fabian 1.15 return ret;
1272 poptix 1.30 }
1273    
1274     /* flush_inbuf():
1275     * checks if there's data in the incoming buffer of an connection
1276     * and flushs the buffer if possible
1277     *
1278     * returns: -1 if the dcc entry wasn't found
1279     * -2 if dcc[idx].type->activity doesn't exist and the data couldn't
1280     * be handled
1281     * 0 if buffer was empty
1282     * otherwise length of flushed buffer
1283     */
1284     int flush_inbuf(int idx)
1285     {
1286     int i, len;
1287     char *inbuf;
1288    
1289 tothwolf 1.45 assert((idx >= 0) && (idx < dcc_total));
1290 poptix 1.30 for (i = 0; i < MAXSOCKS; i++) {
1291     if ((dcc[idx].sock == socklist[i].sock)
1292     && !(socklist[i].flags & SOCK_UNUSED)) {
1293     len = socklist[i].inbuflen;
1294     if ((len > 0) && socklist[i].inbuf) {
1295     if (dcc[idx].type && dcc[idx].type->activity) {
1296     inbuf = socklist[i].inbuf;
1297     socklist[i].inbuf = NULL;
1298     dcc[idx].type->activity(idx, inbuf, len);
1299 tothwolf 1.42 free(inbuf);
1300 poptix 1.30 return len;
1301     } else
1302     return -2;
1303     } else
1304     return 0;
1305     }
1306     }
1307     return -1;
1308 segfault 1.1 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23