/[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.57 - (hide annotations) (download) (as text)
Fri May 3 07:57:12 2002 UTC (17 years, 5 months ago) by stdarg
Branch: MAIN
Changes since 1.56: +13 -11 lines
File MIME type: text/x-chdr
* Made itraffic_* otraffic_* into a single struct.
* mod-path -> mod_path

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23