/[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.56 - (hide annotations) (download) (as text)
Mon Apr 1 23:39:36 2002 UTC (17 years, 2 months ago) by ite
Branch: MAIN
Changes since 1.55: +1 -73 lines
File MIME type: text/x-chdr
Stripped neterror() so it will always use strerror(). Provided a full strerror() fallback function.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23