Updated with hash related strings.
[doldaconnect.git] / daemon / net.c
CommitLineData
d3372da9 1/*
2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19/* XXX: Implement SOCKS proxyability */
20
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24#include <string.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <fcntl.h>
28#include <sys/ioctl.h>
29#include <sys/socket.h>
30#include <sys/un.h>
31#include <sys/poll.h>
32#include <arpa/inet.h>
33#include <netinet/in.h>
34#include <netdb.h>
35#include <sys/signal.h>
36#include <printf.h>
37#ifdef HAVE_LINUX_SOCKIOS_H
38#include <linux/sockios.h>
39#endif
40#include <errno.h>
41#include <net/if.h>
42
43#include "conf.h"
44#include "net.h"
45#include "module.h"
46#include "log.h"
47#include "utils.h"
48#include "sysevents.h"
49
50static struct configvar myvars[] =
51{
52 /* 0 = Direct mode, 1 = Passive mode, 2 = SOCKS proxy */
53 {CONF_VAR_INT, "mode", {.num = 0}},
347d6d76 54 {CONF_VAR_BOOL, "reuseaddr", {.num = 0}},
d3372da9 55 /* Only for direct mode */
56 {CONF_VAR_IPV4, "visibleipv4", {.ipv4 = {0}}},
57 {CONF_VAR_STRING, "publicif", {.str = L""}},
58 {CONF_VAR_END}
59};
60
61static struct socket *sockets = NULL;
62int numsocks = 0;
63
64/* XXX: Get autoconf for all this... */
65int getpublicaddr(int af, struct sockaddr **addr, socklen_t *lenbuf)
66{
67 struct sockaddr_in *ipv4;
68 struct configvar *var;
69 void *bufend;
70 int sock;
71 struct ifconf conf;
72 struct ifreq *ifr, req;
73 char *pif;
74
75 if(af == AF_INET)
76 {
77 var = confgetvar("net", "visibleipv4");
78 if(var->val.ipv4.s_addr != 0)
79 {
80 ipv4 = smalloc(sizeof(*ipv4));
81 ipv4->sin_family = AF_INET;
82 ipv4->sin_addr.s_addr = var->val.ipv4.s_addr;
83 *addr = (struct sockaddr *)ipv4;
84 *lenbuf = sizeof(*ipv4);
85 return(0);
86 }
87 if((pif = icwcstombs(confgetstr("net", "publicif"), NULL)) == NULL)
88 {
89 flog(LOG_ERR, "could not convert net.publicif into local charset: %s", strerror(errno));
90 return(-1);
91 }
92 if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
93 return(-1);
94 conf.ifc_buf = smalloc(conf.ifc_len = 65536);
95 if(ioctl(sock, SIOCGIFCONF, &conf) < 0)
96 {
97 free(conf.ifc_buf);
98 close(sock);
99 return(-1);
100 }
101 bufend = ((char *)conf.ifc_buf) + conf.ifc_len;
102 ipv4 = NULL;
103 for(ifr = conf.ifc_ifcu.ifcu_req; (void *)ifr < bufend; ifr++)
104 {
105 memset(&req, 0, sizeof(req));
106 memcpy(req.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name));
107 if(ioctl(sock, SIOCGIFFLAGS, &req) < 0)
108 {
109 free(conf.ifc_buf);
110 close(sock);
111 return(-1);
112 }
113 if(!(req.ifr_flags & IFF_UP))
114 continue;
115 if(ifr->ifr_addr.sa_family == AF_INET)
116 {
117 if(ntohl(((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr) == 0x7f000001)
118 continue;
119 if(ipv4 == NULL)
120 {
121 ipv4 = smalloc(sizeof(*ipv4));
122 memcpy(ipv4, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
123 } else {
124 free(ipv4);
125 flog(LOG_WARNING, "could not locate an unambiguous interface for determining your public IP address - set net.publicif");
126 errno = ENFILE; /* XXX: There's no appropriate one for this... */
127 return(-1);
128 }
129 }
130 }
131 close(sock);
132 if(ipv4 != NULL)
133 {
134 *addr = (struct sockaddr *)ipv4;
135 *lenbuf = sizeof(*ipv4);
136 return(0);
137 }
138 errno = ENETDOWN;
139 return(-1);
140 }
141 errno = EPFNOSUPPORT;
142 return(-1);
143}
144
145static struct socket *newsock(int type)
146{
147 struct socket *new;
148
149 new = smalloc(sizeof(*new));
150 new->refcount = 2;
151 new->fd = -1;
152 new->isrealsocket = 1;
153 new->family = -1;
154 new->tos = 0;
155 new->type = type;
156 new->state = -1;
157 new->ignread = 0;
158 new->close = 0;
159 new->remote = NULL;
160 new->remotelen = 0;
161 switch(type)
162 {
163 case SOCK_STREAM:
164 new->outbuf.s.buf = NULL;
165 new->outbuf.s.bufsize = 0;
166 new->outbuf.s.datasize = 0;
167 new->inbuf.s.buf = NULL;
168 new->inbuf.s.bufsize = 0;
169 new->inbuf.s.datasize = 0;
170 break;
171 case SOCK_DGRAM:
172 new->outbuf.d.f = new->outbuf.d.l = NULL;
173 new->inbuf.d.f = new->inbuf.d.l = NULL;
174 break;
175 }
176 new->conncb = NULL;
177 new->errcb = NULL;
178 new->readcb = NULL;
179 new->writecb = NULL;
180 new->acceptcb = NULL;
181 new->next = sockets;
182 new->prev = NULL;
183 if(sockets != NULL)
184 sockets->prev = new;
185 sockets = new;
186 numsocks++;
187 return(new);
188}
189
190static struct socket *mksock(int domain, int type)
191{
192 int fd;
193 struct socket *new;
194
195 if((fd = socket(domain, type, 0)) < 0)
196 {
197 flog(LOG_CRIT, "could not create socket: %s", strerror(errno));
198 return(NULL);
199 }
200 new = newsock(type);
201 new->fd = fd;
202 new->family = domain;
203 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
204 return(new);
205}
206
207struct socket *wrapsock(int fd)
208{
209 struct socket *new;
210
211 new = newsock(SOCK_STREAM);
212 new->fd = fd;
213 new->state = SOCK_EST;
214 new->isrealsocket = 0;
215 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
216 return(new);
217}
218
219static void unlinksock(struct socket *sk)
220{
221 if(sk->prev != NULL)
222 sk->prev->next = sk->next;
223 if(sk->next != NULL)
224 sk->next->prev = sk->prev;
225 if(sk == sockets)
226 sockets = sk->next;
227 putsock(sk);
228 numsocks--;
229}
230
231void getsock(struct socket *sk)
232{
233 sk->refcount++;
234}
235
236void putsock(struct socket *sk)
237{
238 struct dgrambuf *buf;
239
240 if(--(sk->refcount) == 0)
241 {
242 switch(sk->type)
243 {
244 case SOCK_STREAM:
245 if(sk->outbuf.s.buf != NULL)
246 free(sk->outbuf.s.buf);
247 if(sk->inbuf.s.buf != NULL)
248 free(sk->inbuf.s.buf);
249 break;
250 case SOCK_DGRAM:
251 while((buf = sk->outbuf.d.f) != NULL)
252 {
253 sk->outbuf.d.f = buf->next;
254 free(buf->data);
255 free(buf);
256 }
257 while((buf = sk->inbuf.d.f) != NULL)
258 {
259 sk->inbuf.d.f = buf->next;
260 free(buf->data);
261 free(buf);
262 }
263 break;
264 }
265 if(sk->fd >= 0)
266 close(sk->fd);
267 if(sk->remote != NULL)
268 free(sk->remote);
269 free(sk);
270 }
271}
272
336539c2 273void sockpushdata(struct socket *sk, void *buf, size_t size)
274{
275 switch(sk->type)
276 {
277 case SOCK_STREAM:
278 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + size, 1, 1);
279 memmove(sk->inbuf.s.buf + size, sk->inbuf.s.buf, sk->inbuf.s.datasize);
280 memcpy(sk->inbuf.s.buf, buf, size);
281 sk->inbuf.s.datasize += size;
282 break;
283 case SOCK_DGRAM:
284 /* XXX */
285 break;
286 }
287 return;
288}
289
d3372da9 290void *sockgetinbuf(struct socket *sk, size_t *size)
291{
292 void *buf;
293 struct dgrambuf *dbuf;
294
295 switch(sk->type)
296 {
297 case SOCK_STREAM:
298 if((sk->inbuf.s.buf == NULL) || (sk->inbuf.s.datasize == 0))
299 {
300 *size = 0;
301 return(NULL);
302 }
303 buf = sk->inbuf.s.buf;
304 *size = sk->inbuf.s.datasize;
305 sk->inbuf.s.buf = NULL;
306 sk->inbuf.s.bufsize = sk->inbuf.s.datasize = 0;
307 return(buf);
308 case SOCK_DGRAM:
309 if((dbuf = sk->inbuf.d.f) == NULL)
310 return(NULL);
311 sk->inbuf.d.f = dbuf->next;
312 if(dbuf->next == NULL)
313 sk->inbuf.d.l = NULL;
314 buf = dbuf->data;
315 *size = dbuf->size;
316 free(dbuf->addr);
317 free(dbuf);
318 return(buf);
319 }
320 return(NULL);
321}
322
323static void sockrecv(struct socket *sk)
324{
325 int ret, inq;
326 struct dgrambuf *dbuf;
327
328 switch(sk->type)
329 {
330 case SOCK_STREAM:
331#if defined(HAVE_LINUX_SOCKIOS_H) && defined(SIOCINQ)
332 /* SIOCINQ is Linux-specific AFAIK, but I really have no idea
333 * how to read the inqueue size on other OSs */
334 if(ioctl(sk->fd, SIOCINQ, &inq))
335 {
336 /* I don't really know what could go wrong here, so let's
337 * assume it's transient. */
338 flog(LOG_WARNING, "SIOCINQ return %s on socket %i, falling back to 2048 bytes", strerror(errno), sk->fd);
339 inq = 2048;
340 }
341#else
342 inq = 2048;
343#endif
344 if(inq > 65536)
345 inq = 65536;
346 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + inq, 1, 1);
347 ret = read(sk->fd, sk->inbuf.s.buf + sk->inbuf.s.datasize, inq);
348 if(ret < 0)
349 {
350 if((errno == EINTR) || (errno == EAGAIN))
351 return;
352 if(sk->errcb != NULL)
353 sk->errcb(sk, errno, sk->data);
354 closesock(sk);
355 return;
356 }
357 if(ret == 0)
358 {
359 if(sk->errcb != NULL)
360 sk->errcb(sk, 0, sk->data);
361 closesock(sk);
362 return;
363 }
364 sk->inbuf.s.datasize += ret;
365 if(sk->readcb != NULL)
366 sk->readcb(sk, sk->data);
367 break;
368 case SOCK_DGRAM:
369 if(ioctl(sk->fd, SIOCINQ, &inq))
370 {
371 /* I don't really know what could go wrong here, so let's
372 * assume it's transient. */
373 flog(LOG_WARNING, "SIOCINQ return %s on socket %i", strerror(errno), sk->fd);
374 return;
375 }
376 dbuf = smalloc(sizeof(*dbuf));
377 dbuf->data = smalloc(inq);
378 dbuf->addr = smalloc(dbuf->addrlen = sizeof(struct sockaddr_storage));
379 ret = recvfrom(sk->fd, dbuf->data, inq, 0, dbuf->addr, &dbuf->addrlen);
380 if(ret < 0)
381 {
382 free(dbuf->addr);
383 free(dbuf->data);
384 free(dbuf);
385 if((errno == EINTR) || (errno == EAGAIN))
386 return;
387 if(sk->errcb != NULL)
388 sk->errcb(sk, errno, sk->data);
389 closesock(sk);
390 return;
391 }
392 /* On UDP/IPv[46], ret == 0 doesn't mean EOF (since UDP can't
393 * have EOF), but rather an empty packet. I don't know if any
394 * other potential DGRAM protocols might have an EOF
395 * condition, so let's play safe. */
396 if(ret == 0)
397 {
398 free(dbuf->addr);
399 free(dbuf->data);
400 free(dbuf);
401 if(!((sk->family == AF_INET) || (sk->family == AF_INET6)))
402 {
403 if(sk->errcb != NULL)
404 sk->errcb(sk, 0, sk->data);
405 closesock(sk);
406 }
407 return;
408 }
409 dbuf->addr = srealloc(dbuf->addr, dbuf->addrlen);
410 dbuf->data = srealloc(dbuf->data, dbuf->size = ret);
411 dbuf->next = NULL;
412 if(sk->inbuf.d.l != NULL)
413 sk->inbuf.d.l->next = dbuf;
414 else
415 sk->inbuf.d.f = dbuf;
416 sk->inbuf.d.l = dbuf;
417 if(sk->readcb != NULL)
418 sk->readcb(sk, sk->data);
419 break;
420 }
421}
422
423static void sockflush(struct socket *sk)
424{
425 int ret;
426 struct dgrambuf *dbuf;
427
428 switch(sk->type)
429 {
430 case SOCK_STREAM:
431 if(sk->isrealsocket)
432 ret = send(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize, MSG_DONTWAIT | MSG_NOSIGNAL);
433 else
434 ret = write(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize);
435 if(ret < 0)
436 {
437 /* For now, assume transient error, since
438 * the socket is polled for errors */
439 break;
440 }
441 if(ret > 0)
442 {
443 memmove(sk->outbuf.s.buf, ((char *)sk->outbuf.s.buf) + ret, sk->outbuf.s.datasize -= ret);
444 if(sk->writecb != NULL)
445 sk->writecb(sk, sk->data);
446 }
447 break;
448 case SOCK_DGRAM:
449 dbuf = sk->outbuf.d.f;
450 if((sk->outbuf.d.f = dbuf->next) == NULL)
451 sk->outbuf.d.l = NULL;
452 sendto(sk->fd, dbuf->data, dbuf->size, MSG_DONTWAIT | MSG_NOSIGNAL, dbuf->addr, dbuf->addrlen);
453 free(dbuf->data);
454 free(dbuf->addr);
455 free(dbuf);
456 if(sk->writecb != NULL)
457 sk->writecb(sk, sk->data);
458 break;
459 }
460}
461
462void closesock(struct socket *sk)
463{
464 sk->state = SOCK_STL;
465 close(sk->fd);
466 sk->fd = -1;
467 sk->close = 0;
468}
469
470void sockqueue(struct socket *sk, void *data, size_t size)
471{
472 struct dgrambuf *new;
473
474 if(sk->state == SOCK_STL)
475 return;
476 switch(sk->type)
477 {
478 case SOCK_STREAM:
479 sizebuf(&(sk->outbuf.s.buf), &(sk->outbuf.s.bufsize), sk->outbuf.s.datasize + size, 1, 1);
480 memcpy(sk->outbuf.s.buf + sk->outbuf.s.datasize, data, size);
481 sk->outbuf.s.datasize += size;
482 break;
483 case SOCK_DGRAM:
484 if(sk->remote == NULL)
485 return;
486 new = smalloc(sizeof(*new));
487 new->next = NULL;
488 memcpy(new->data = smalloc(size), data, new->size = size);
489 memcpy(new->addr = smalloc(sk->remotelen), sk->remote, new->addrlen = sk->remotelen);
490 if(sk->outbuf.d.l == NULL)
491 {
492 sk->outbuf.d.l = sk->outbuf.d.f = new;
493 } else {
494 sk->outbuf.d.l->next = new;
495 sk->outbuf.d.l = new;
496 }
497 break;
498 }
499}
500
501size_t sockgetdatalen(struct socket *sk)
502{
503 struct dgrambuf *b;
504 size_t ret;
505
506 switch(sk->type)
507 {
508 case SOCK_STREAM:
509 ret = sk->inbuf.s.datasize;
510 break;
511 case SOCK_DGRAM:
512 ret = 0;
513 for(b = sk->inbuf.d.f; b != NULL; b = b->next)
514 ret += b->size;
515 break;
516 }
517 return(ret);
518}
519
520size_t sockqueuesize(struct socket *sk)
521{
522 struct dgrambuf *b;
523 size_t ret;
524
525 switch(sk->type)
526 {
527 case SOCK_STREAM:
528 ret = sk->outbuf.s.datasize;
529 break;
530 case SOCK_DGRAM:
531 ret = 0;
532 for(b = sk->outbuf.d.f; b != NULL; b = b->next)
533 ret += b->size;
534 break;
535 }
536 return(ret);
537}
538
539struct socket *netcslisten(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
540{
541 struct socket *sk;
347d6d76 542 int intbuf;
d3372da9 543
544 if(confgetint("net", "mode") == 1)
545 {
546 errno = EOPNOTSUPP;
547 return(NULL);
548 }
549 /* I don't know if this is actually correct (it probably isn't),
550 * but since, at on least Linux systems, PF_* are specifically
551 * #define'd to their AF_* counterparts, it allows for a severely
552 * smoother implementation. If it breaks something on your
553 * platform, please tell me so.
554 */
555 if(confgetint("net", "mode") == 0)
556 {
557 if((sk = mksock(name->sa_family, type)) == NULL)
558 return(NULL);
559 sk->state = SOCK_LST;
347d6d76 560 if(confgetint("net", "reuseaddr"))
561 {
562 intbuf = 1;
563 setsockopt(sk->fd, SOL_SOCKET, SO_REUSEADDR, &intbuf, sizeof(intbuf));
564 }
d3372da9 565 if(bind(sk->fd, name, namelen) < 0)
566 {
567 putsock(sk);
568 return(NULL);
569 }
570 if(listen(sk->fd, 16) < 0)
571 {
572 putsock(sk);
573 return(NULL);
574 }
575 sk->acceptcb = func;
576 sk->data = data;
577 return(sk);
578 }
579 errno = EOPNOTSUPP;
580 return(NULL);
581}
582
583/*
584 * The difference between netcslisten() and netcslistenlocal() is that
585 * netcslistenlocal() always listens on the local host, instead of
586 * following proxy/passive mode directions. It is suitable for eg. the
587 * UI channel, while the file sharing networks should, naturally, use
588 * netcslisten() instead.
589*/
590
591struct socket *netcslistenlocal(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
592{
593 struct socket *sk;
66c517d2 594 int intbuf;
d3372da9 595
596 /* I don't know if this is actually correct (it probably isn't),
597 * but since, at on least Linux systems, PF_* are specifically
598 * #define'd to their AF_* counterparts, it allows for a severely
599 * smoother implementation. If it breaks something on your
600 * platform, please tell me so.
601 */
602 if((sk = mksock(name->sa_family, type)) == NULL)
603 return(NULL);
604 sk->state = SOCK_LST;
687b2ee2 605 if(confgetint("net", "reuseaddr"))
606 {
607 intbuf = 1;
608 setsockopt(sk->fd, SOL_SOCKET, SO_REUSEADDR, &intbuf, sizeof(intbuf));
609 }
d3372da9 610 if(bind(sk->fd, name, namelen) < 0)
611 {
612 putsock(sk);
613 return(NULL);
614 }
615 if(listen(sk->fd, 16) < 0)
616 {
617 putsock(sk);
618 return(NULL);
619 }
620 sk->acceptcb = func;
621 sk->data = data;
622 return(sk);
623}
624
625struct socket *netcsdgram(struct sockaddr *name, socklen_t namelen)
626{
627 struct socket *sk;
628 int mode;
629
630 mode = confgetint("net", "mode");
631 if((mode == 0) || (mode == 1))
632 {
633 if((sk = mksock(name->sa_family, SOCK_DGRAM)) == NULL)
634 return(NULL);
635 if(bind(sk->fd, name, namelen) < 0)
636 {
637 putsock(sk);
638 return(NULL);
639 }
640 sk->state = SOCK_EST;
641 return(sk);
642 }
643 errno = EOPNOTSUPP;
644 return(NULL);
645}
646
647struct socket *netdupsock(struct socket *sk)
648{
649 struct socket *newsk;
650
651 newsk = newsock(sk->type);
652 if((newsk->fd = dup(sk->fd)) < 0)
653 {
654 flog(LOG_WARNING, "could not dup() socket: %s", strerror(errno));
655 putsock(newsk);
656 return(NULL);
657 }
658 newsk->state = sk->state;
659 newsk->ignread = sk->ignread;
660 if(sk->remote != NULL)
661 memcpy(newsk->remote = smalloc(sk->remotelen), sk->remote, newsk->remotelen = sk->remotelen);
662 return(newsk);
663}
664
665void netdgramconn(struct socket *sk, struct sockaddr *addr, socklen_t addrlen)
666{
667 if(sk->remote != NULL)
668 free(sk->remote);
669 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
670 sk->ignread = 1;
671}
672
673struct socket *netcsconn(struct sockaddr *addr, socklen_t addrlen, void (*func)(struct socket *, int, void *), void *data)
674{
675 struct socket *sk;
676 int mode;
677
678 mode = confgetint("net", "mode");
679 if((mode == 0) || (mode == 1))
680 {
681 if((sk = mksock(addr->sa_family, SOCK_STREAM)) == NULL)
682 return(NULL);
683 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
684 if(!connect(sk->fd, addr, addrlen))
685 {
686 sk->state = SOCK_EST;
687 func(sk, 0, data);
688 return(sk);
689 }
690 if(errno == EINPROGRESS)
691 {
692 sk->state = SOCK_SYN;
693 sk->conncb = func;
694 sk->data = data;
695 return(sk);
696 }
697 putsock(sk);
698 return(NULL);
699 }
700 errno = EOPNOTSUPP;
701 return(NULL);
702}
703
704int pollsocks(int timeout)
705{
706 int i, num, ret, retlen;
707 int newfd;
708 struct pollfd *pfds;
709 struct socket *sk, *next, *newsk;
710 struct sockaddr_storage ss;
711 socklen_t sslen;
712
713 pfds = smalloc(sizeof(*pfds) * (num = numsocks));
714 for(i = 0, sk = sockets; i < num; sk = sk->next)
715 {
716 if(sk->state == SOCK_STL)
717 {
718 num--;
719 continue;
720 }
721 pfds[i].fd = sk->fd;
722 pfds[i].events = 0;
723 if(!sk->ignread)
724 pfds[i].events |= POLLIN;
725 if((sk->state == SOCK_SYN) || (sockqueuesize(sk) > 0))
726 pfds[i].events |= POLLOUT;
727 pfds[i].revents = 0;
728 i++;
729 }
730 ret = poll(pfds, num, timeout);
731 if(ret < 0)
732 {
733 if(errno != EINTR)
734 {
735 flog(LOG_CRIT, "pollsocks: poll errored out: %s", strerror(errno));
736 /* To avoid CPU hogging in case it's bad, which it
737 * probably is. */
738 sleep(1);
739 }
740 free(pfds);
741 return(1);
742 }
743 for(sk = sockets; sk != NULL; sk = next)
744 {
745 next = sk->next;
746 for(i = 0; i < num; i++)
747 {
748 if(pfds[i].fd == sk->fd)
749 break;
750 }
751 if(i == num)
752 continue;
753 switch(sk->state)
754 {
755 case SOCK_LST:
756 if(pfds[i].revents & POLLIN)
757 {
758 sslen = sizeof(ss);
759 if((newfd = accept(sk->fd, (struct sockaddr *)&ss, &sslen)) < 0)
760 {
761 if(sk->errcb != NULL)
762 sk->errcb(sk, errno, sk->data);
763 }
764 newsk = newsock(sk->type);
765 newsk->fd = newfd;
766 newsk->family = sk->family;
767 newsk->state = SOCK_EST;
768 memcpy(newsk->remote = smalloc(sslen), &ss, sslen);
769 newsk->remotelen = sslen;
770 putsock(newsk);
771 if(sk->acceptcb != NULL)
772 sk->acceptcb(sk, newsk, sk->data);
773 }
774 if(pfds[i].revents & POLLERR)
775 {
776 retlen = sizeof(ret);
777 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
778 if(sk->errcb != NULL)
779 sk->errcb(sk, ret, sk->data);
780 continue;
781 }
782 break;
783 case SOCK_SYN:
784 if(pfds[i].revents & POLLERR)
785 {
786 retlen = sizeof(ret);
787 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
788 if(sk->conncb != NULL)
789 sk->conncb(sk, ret, sk->data);
790 closesock(sk);
791 continue;
792 }
793 if(pfds[i].revents & (POLLIN | POLLOUT))
794 {
795 sk->state = SOCK_EST;
796 if(sk->conncb != NULL)
797 sk->conncb(sk, 0, sk->data);
798 }
799 break;
800 case SOCK_EST:
801 if(pfds[i].revents & POLLERR)
802 {
803 retlen = sizeof(ret);
804 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
805 if(sk->errcb != NULL)
806 sk->errcb(sk, ret, sk->data);
807 closesock(sk);
808 continue;
809 }
810 if(pfds[i].revents & POLLIN)
811 sockrecv(sk);
812 if(pfds[i].revents & POLLOUT)
813 {
814 if(sockqueuesize(sk) > 0)
815 sockflush(sk);
816 }
817 break;
818 }
819 if(pfds[i].revents & POLLNVAL)
820 {
821 flog(LOG_CRIT, "BUG: stale socket struct on fd %i", sk->fd);
822 sk->state = SOCK_STL;
823 unlinksock(sk);
824 continue;
825 }
826 if(pfds[i].revents & POLLHUP)
827 {
828 if(sk->errcb != NULL)
829 sk->errcb(sk, 0, sk->data);
830 closesock(sk);
831 unlinksock(sk);
832 continue;
833 }
834 }
835 free(pfds);
836 for(sk = sockets; sk != NULL; sk = next)
837 {
838 next = sk->next;
839 if(sk->refcount == 1 && (sockqueuesize(sk) == 0))
840 {
841 unlinksock(sk);
842 continue;
843 }
844 if(sk->close && (sockqueuesize(sk) == 0))
845 closesock(sk);
846 if(sk->state == SOCK_STL)
847 {
848 unlinksock(sk);
849 continue;
850 }
851 }
852 return(1);
853}
854
855int socksettos(struct socket *sk, int tos)
856{
857 if(sk->family == AF_INET)
858 {
859 if(setsockopt(sk->fd, SOL_IP, IP_TOS, &tos, sizeof(tos)) < 0)
860 {
861 flog(LOG_WARNING, "could not set sock TOS to %i: %s", tos, strerror(errno));
862 return(-1);
863 }
864 return(0);
865 }
866 /* XXX: How does the IPv6 traffic class work? */
867 flog(LOG_WARNING, "could not set TOS on sock of family %i", sk->family);
868 return(1);
869}
870
871struct resolvedata
872{
873 int fd;
874 void (*callback)(struct sockaddr *addr, int addrlen, void *data);
875 void *data;
876 struct sockaddr_storage addr;
877 int addrlen;
878};
879
880static void resolvecb(pid_t pid, int status, struct resolvedata *data)
881{
882 static char buf[80];
883 int ret;
884 struct sockaddr_in *ipv4;
885
886 if(!status)
887 {
888 if((ret = read(data->fd, buf, sizeof(buf))) != 4)
889 {
890 errno = ENONET;
891 data->callback(NULL, 0, data->data);
892 } else {
893 ipv4 = (struct sockaddr_in *)&data->addr;
894 memcpy(&ipv4->sin_addr, buf, 4);
895 data->callback((struct sockaddr *)ipv4, sizeof(*ipv4), data->data);
896 }
897 } else {
898 errno = ENONET;
899 data->callback(NULL, 0, data->data);
900 }
901 close(data->fd);
902 free(data);
903}
904
905int netresolve(char *addr, void (*callback)(struct sockaddr *addr, int addrlen, void *data), void *data)
906{
907 int i;
908 char *p;
909 int port;
910 int pfd[2];
911 pid_t child;
912 struct resolvedata *rdata;
913 struct sockaddr_in ipv4;
914 struct hostent *he;
915 sigset_t sigset;
916
917 /* IPv4 */
918 port = -1;
919 if((p = strchr(addr, ':')) != NULL)
920 {
921 *p = 0;
922 port = atoi(p + 1);
923 }
924 ipv4.sin_family = AF_INET;
925 ipv4.sin_port = htons(port);
926 if(inet_aton(addr, &ipv4.sin_addr))
927 {
928 callback((struct sockaddr *)&ipv4, sizeof(ipv4), data);
929 } else {
930 sigemptyset(&sigset);
931 sigaddset(&sigset, SIGCHLD);
932 sigprocmask(SIG_BLOCK, &sigset, NULL);
933 if((pipe(pfd) < 0) || ((child = fork()) < 0))
934 {
935 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
936 return(-1);
937 }
938 if(child == 0)
939 {
940 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
941 for(i = 3; i < FD_SETSIZE; i++)
942 {
943 if(i != pfd[1])
944 close(i);
945 }
946 signal(SIGALRM, SIG_DFL);
947 alarm(30);
948 if((he = gethostbyname(addr)) == NULL)
949 exit(1);
950 write(pfd[1], he->h_addr_list[0], 4);
951 exit(0);
952 } else {
953 close(pfd[1]);
954 fcntl(pfd[0], F_SETFL, fcntl(pfd[0], F_GETFL) | O_NONBLOCK);
955 rdata = smalloc(sizeof(*rdata));
956 rdata->fd = pfd[0];
957 rdata->callback = callback;
958 rdata->data = data;
959 memcpy(&rdata->addr, &ipv4, rdata->addrlen = sizeof(ipv4));
960 childcallback(child, (void (*)(pid_t, int, void *))resolvecb, rdata);
961 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
962 return(1);
963 }
964 }
965 return(0);
966}
967
968int sockgetlocalname(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
969{
970 socklen_t len;
971 struct sockaddr_storage name;
972
973 *namebuf = NULL;
974 if((sk->state == SOCK_STL) || (sk->fd < 0))
975 return(-1);
976 len = sizeof(name);
977 if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0)
978 {
979 flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetlocalname");
980 return(-1);
981 }
982 *namebuf = memcpy(smalloc(len), &name, len);
983 *lenbuf = len;
984 return(0);
985}
986
987int sockgetremotename(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
988{
989 socklen_t len;
990 struct sockaddr_storage name;
991 struct sockaddr_in *ipv4;
992 struct sockaddr *pname;
993 socklen_t pnamelen;
994
995 switch(confgetint("net", "mode"))
996 {
997 case 0:
998 *namebuf = NULL;
999 if((sk->state == SOCK_STL) || (sk->fd < 0))
1000 return(-1);
1001 len = sizeof(name);
1002 if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0)
1003 {
1004 flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetremotename");
1005 return(-1);
1006 }
1007 if(name.ss_family == AF_INET)
1008 {
1009 ipv4 = (struct sockaddr_in *)&name;
1010 if(getpublicaddr(AF_INET, &pname, &pnamelen) < 0)
1011 {
1012 flog(LOG_WARNING, "could not determine public IP address - strange things may happen");
1013 return(-1);
1014 }
1015 ipv4->sin_addr.s_addr = ((struct sockaddr_in *)pname)->sin_addr.s_addr;
1016 free(pname);
1017 }
1018 *namebuf = memcpy(smalloc(len), &name, len);
1019 *lenbuf = len;
1020 return(0);
1021 case 1:
1022 errno = EOPNOTSUPP;
1023 return(-1);
1024 default:
1025 flog(LOG_CRIT, "unknown net mode %i active", confgetint("net", "mode"));
1026 errno = EOPNOTSUPP;
1027 return(-1);
1028 }
1029}
1030
1031char *formataddress(struct sockaddr *arg, socklen_t arglen)
1032{
1033 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1034 * lowercase letters to 1, so I do this
1035 * instead. */
1036 struct sockaddr_in *ipv4;
1037#ifdef HAVE_IPV6
1038 struct sockaddr_in6 *ipv6;
1039#endif
1040 static char *ret = NULL;
1041 char buf[1024];
1042
1043 if(ret != NULL)
1044 free(ret);
1045 ret = NULL;
1046 switch(arg->sa_family)
1047 {
1048 case AF_UNIX:
1049 UNIX = (struct sockaddr_un *)arg;
1050 ret = sprintf2("%s", UNIX->sun_path);
1051 break;
1052 case AF_INET:
1053 ipv4 = (struct sockaddr_in *)arg;
1054 if(inet_ntop(AF_INET, &ipv4->sin_addr, buf, sizeof(buf)) == NULL)
1055 return(NULL);
1056 ret = sprintf2("%s:%i", buf, (int)ntohs(ipv4->sin_port));
1057 break;
1058#ifdef HAVE_IPV6
1059 case AF_INET6:
1060 ipv6 = (struct sockaddr_in6 *)arg;
1061 if(inet_ntop(AF_INET6, &ipv6->sin6_addr, buf, sizeof(buf)) == NULL)
1062 return(NULL);
1063 ret = sprintf2("%s:%i", buf, (int)ntohs(ipv6->sin6_port));
1064 break;
1065#endif
1066 default:
1067 errno = EPFNOSUPPORT;
1068 break;
1069 }
1070 return(ret);
1071}
1072
1073#if 0
1074
1075/*
1076 * It was very nice to use this, but it seems
1077 * to mess things up, so I guess it has to go... :-(
1078 */
1079
1080static int formataddress(FILE *stream, const struct printf_info *info, const void *const *args)
1081{
1082 struct sockaddr *arg;
1083 socklen_t arglen;
1084 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1085 * lowercase letters to 1, so I do this
1086 * instead. */
1087 struct sockaddr_in *ipv4;
1088 int ret;
1089
1090 arg = *(struct sockaddr **)(args[0]);
1091 arglen = *(socklen_t *)(args[1]);
1092 switch(arg->sa_family)
1093 {
1094 case AF_UNIX:
1095 UNIX = (struct sockaddr_un *)arg;
1096 ret = fprintf(stream, "%s", UNIX->sun_path);
1097 break;
1098 case AF_INET:
1099 ipv4 = (struct sockaddr_in *)arg;
1100 ret = fprintf(stream, "%s:%i", inet_ntoa(ipv4->sin_addr), (int)ntohs(ipv4->sin_port));
1101 break;
1102 default:
1103 ret = -1;
1104 errno = EPFNOSUPPORT;
1105 break;
1106 }
1107 return(ret);
1108}
1109
1110static int formataddress_arginfo(const struct printf_info *info, size_t n, int *argtypes)
1111{
1112 if(n > 0)
1113 argtypes[0] = PA_POINTER;
1114 if(n > 1)
1115 argtypes[1] = PA_INT; /* Sources tell me that socklen_t _must_
1116 * be an int, so I guess this should be
1117 * safe. */
1118 return(2);
1119}
1120#endif
1121
1122static int init(int hup)
1123{
1124 if(!hup)
1125 {
1126 /*
1127 if(register_printf_function('N', formataddress, formataddress_arginfo))
1128 {
1129 flog(LOG_CRIT, "could not register printf handler %%N: %s", strerror(errno));
1130 return(1);
1131 }
1132 */
1133 }
1134 return(0);
1135}
1136
1137static void terminate(void)
1138{
1139 while(sockets != NULL)
1140 unlinksock(sockets);
1141}
1142
1143static struct module me =
1144{
1145 .name = "net",
1146 .conf =
1147 {
1148 .vars = myvars
1149 },
1150 .init = init,
1151 .terminate = terminate
1152};
1153
1154MODULE(me)