/[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.67 - (hide annotations) (download) (as text)
Mon Feb 3 06:42:40 2003 UTC (16 years, 4 months ago) by stdarg
Branch: MAIN
Changes since 1.66: +3 -10 lines
File MIME type: text/x-chdr
*** empty log message ***

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23