2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
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.
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.
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
19 /* XXX: Implement SOCKS proxyability */
28 #include <sys/ioctl.h>
29 #include <sys/socket.h>
32 #include <arpa/inet.h>
33 #include <netinet/in.h>
35 #include <sys/signal.h>
36 #include <sys/stat.h> /* For rebindunix() */
37 #ifdef HAVE_LINUX_SOCKIOS_H
38 #include <linux/sockios.h>
48 #include "sysevents.h"
50 static struct configvar myvars[] =
52 /** The network mode to use. Currently supported values are 0 for
53 * active mode and 1 for passive mode. In the future, SOCKS5 proxy
54 * support may be added. */
55 {CONF_VAR_INT, "mode", {.num = 0}},
56 /** Set the SO_REUSEADDR socket option on listening sockets, so
57 * that dead TCP connections waiting for timeout are ignored. */
58 {CONF_VAR_BOOL, "reuseaddr", {.num = 0}},
59 /** Overrides the IPv4 address reported to other clients in active
60 * mode. Useful for servers behind NAT routers. If both this and
61 * net.publicif are unspecified the address of the hub connection
63 {CONF_VAR_IPV4, "visibleipv4", {.ipv4 = {0}}},
64 /** Specifies an interface name from which to fetch the IPv4
65 * address reported to other clients in active mode. If both this
66 * and net.visibleipv4 are unspecified the address of the hub
67 * connection is used. */
68 {CONF_VAR_STRING, "publicif", {.str = L""}},
69 /* Diffserv should be supported on IPv4, too, but I don't know the
71 /** The Diffserv value to use on IPv6 connections when the
72 * minimize cost TOS value is used (see the TOS VALUES
74 {CONF_VAR_INT, "diffserv-mincost", {.num = 0}},
75 /** The Diffserv value to use on IPv6 connections when the
76 * maximize reliability TOS value is used (see the TOS VALUES
78 {CONF_VAR_INT, "diffserv-maxrel", {.num = 0}},
79 /** The Diffserv value to use on IPv6 connections when the
80 * maximize throughput TOS value is used (see the TOS VALUES
82 {CONF_VAR_INT, "diffserv-maxtp", {.num = 0}},
83 /** The Diffserv value to use on IPv6 connections when the
84 * minimize delay TOS value is used (see the TOS VALUES
86 {CONF_VAR_INT, "diffserv-mindelay", {.num = 0}},
90 static struct socket *sockets = NULL;
93 /* XXX: Get autoconf for all this... */
94 int getpublicaddr(int af, struct sockaddr **addr, socklen_t *lenbuf)
96 struct sockaddr_in *ipv4;
97 struct configvar *var;
101 struct ifreq *ifr, req;
106 var = confgetvar("net", "visibleipv4");
107 if(var->val.ipv4.s_addr != 0)
109 ipv4 = smalloc(sizeof(*ipv4));
110 ipv4->sin_family = AF_INET;
111 ipv4->sin_addr.s_addr = var->val.ipv4.s_addr;
112 *addr = (struct sockaddr *)ipv4;
113 *lenbuf = sizeof(*ipv4);
116 if((pif = icswcstombs(confgetstr("net", "publicif"), NULL, NULL)) == NULL)
118 flog(LOG_ERR, "could not convert net.publicif into local charset: %s", strerror(errno));
123 if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
125 conf.ifc_buf = smalloc(conf.ifc_len = 65536);
126 if(ioctl(sock, SIOCGIFCONF, &conf) < 0)
132 bufend = ((char *)conf.ifc_buf) + conf.ifc_len;
134 for(ifr = conf.ifc_ifcu.ifcu_req; (void *)ifr < bufend; ifr++)
136 if(strcmp(ifr->ifr_name, pif))
138 memset(&req, 0, sizeof(req));
139 memcpy(req.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name));
140 if(ioctl(sock, SIOCGIFFLAGS, &req) < 0)
142 if(!(req.ifr_flags & IFF_UP))
144 flog(LOG_WARNING, "public interface is down");
147 if(ifr->ifr_addr.sa_family != AF_INET)
149 flog(LOG_WARNING, "address of the public interface is not AF_INET");
152 ipv4 = smalloc(sizeof(*ipv4));
153 memcpy(ipv4, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
160 *addr = (struct sockaddr *)ipv4;
161 *lenbuf = sizeof(*ipv4);
170 static struct socket *newsock(int type)
174 new = smalloc(sizeof(*new));
177 new->isrealsocket = 1;
189 new->outbuf.s.buf = NULL;
190 new->outbuf.s.bufsize = 0;
191 new->outbuf.s.datasize = 0;
192 new->inbuf.s.buf = NULL;
193 new->inbuf.s.bufsize = 0;
194 new->inbuf.s.datasize = 0;
197 new->outbuf.d.f = new->outbuf.d.l = NULL;
198 new->inbuf.d.f = new->inbuf.d.l = NULL;
205 new->acceptcb = NULL;
215 static struct socket *mksock(int domain, int type)
220 if((fd = socket(domain, type, 0)) < 0)
222 flog(LOG_CRIT, "could not create socket: %s", strerror(errno));
227 new->family = domain;
228 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
232 struct socket *wrapsock(int fd)
236 new = newsock(SOCK_STREAM);
238 new->state = SOCK_EST;
239 new->isrealsocket = 0;
240 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
244 static void unlinksock(struct socket *sk)
247 sk->prev->next = sk->next;
249 sk->next->prev = sk->prev;
256 void getsock(struct socket *sk)
261 void putsock(struct socket *sk)
263 struct dgrambuf *buf;
265 if(--(sk->refcount) == 0)
270 if(sk->outbuf.s.buf != NULL)
271 free(sk->outbuf.s.buf);
272 if(sk->inbuf.s.buf != NULL)
273 free(sk->inbuf.s.buf);
276 while((buf = sk->outbuf.d.f) != NULL)
278 sk->outbuf.d.f = buf->next;
283 while((buf = sk->inbuf.d.f) != NULL)
285 sk->inbuf.d.f = buf->next;
293 if(sk->remote != NULL)
299 void sockpushdata(struct socket *sk, void *buf, size_t size)
304 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + size, 1, 1);
305 memmove(sk->inbuf.s.buf + size, sk->inbuf.s.buf, sk->inbuf.s.datasize);
306 memcpy(sk->inbuf.s.buf, buf, size);
307 sk->inbuf.s.datasize += size;
316 void *sockgetinbuf(struct socket *sk, size_t *size)
319 struct dgrambuf *dbuf;
324 if((sk->inbuf.s.buf == NULL) || (sk->inbuf.s.datasize == 0))
329 buf = sk->inbuf.s.buf;
330 *size = sk->inbuf.s.datasize;
331 sk->inbuf.s.buf = NULL;
332 sk->inbuf.s.bufsize = sk->inbuf.s.datasize = 0;
335 if((dbuf = sk->inbuf.d.f) == NULL)
337 sk->inbuf.d.f = dbuf->next;
338 if(dbuf->next == NULL)
339 sk->inbuf.d.l = NULL;
349 static void sockrecv(struct socket *sk)
352 struct dgrambuf *dbuf;
357 #if defined(HAVE_LINUX_SOCKIOS_H) && defined(SIOCINQ)
358 /* SIOCINQ is Linux-specific AFAIK, but I really have no idea
359 * how to read the inqueue size on other OSs */
360 if(ioctl(sk->fd, SIOCINQ, &inq))
362 /* I don't really know what could go wrong here, so let's
363 * assume it's transient. */
364 flog(LOG_WARNING, "SIOCINQ return %s on socket %i, falling back to 2048 bytes", strerror(errno), sk->fd);
372 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + inq, 1, 1);
373 ret = read(sk->fd, sk->inbuf.s.buf + sk->inbuf.s.datasize, inq);
376 if((errno == EINTR) || (errno == EAGAIN))
378 if(sk->errcb != NULL)
379 sk->errcb(sk, errno, sk->data);
385 if(sk->errcb != NULL)
386 sk->errcb(sk, 0, sk->data);
390 sk->inbuf.s.datasize += ret;
391 if(sk->readcb != NULL)
392 sk->readcb(sk, sk->data);
395 if(ioctl(sk->fd, SIOCINQ, &inq))
397 /* I don't really know what could go wrong here, so let's
398 * assume it's transient. */
399 flog(LOG_WARNING, "SIOCINQ return %s on socket %i", strerror(errno), sk->fd);
402 dbuf = smalloc(sizeof(*dbuf));
403 dbuf->data = smalloc(inq);
404 dbuf->addr = smalloc(dbuf->addrlen = sizeof(struct sockaddr_storage));
405 ret = recvfrom(sk->fd, dbuf->data, inq, 0, dbuf->addr, &dbuf->addrlen);
411 if((errno == EINTR) || (errno == EAGAIN))
413 if(sk->errcb != NULL)
414 sk->errcb(sk, errno, sk->data);
418 /* On UDP/IPv[46], ret == 0 doesn't mean EOF (since UDP can't
419 * have EOF), but rather an empty packet. I don't know if any
420 * other potential DGRAM protocols might have an EOF
421 * condition, so let's play safe. */
427 if(!((sk->family == AF_INET) || (sk->family == AF_INET6)))
429 if(sk->errcb != NULL)
430 sk->errcb(sk, 0, sk->data);
435 dbuf->addr = srealloc(dbuf->addr, dbuf->addrlen);
436 dbuf->data = srealloc(dbuf->data, dbuf->size = ret);
438 if(sk->inbuf.d.l != NULL)
439 sk->inbuf.d.l->next = dbuf;
441 sk->inbuf.d.f = dbuf;
442 sk->inbuf.d.l = dbuf;
443 if(sk->readcb != NULL)
444 sk->readcb(sk, sk->data);
449 static void sockflush(struct socket *sk)
452 struct dgrambuf *dbuf;
458 ret = send(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize, MSG_DONTWAIT | MSG_NOSIGNAL);
460 ret = write(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize);
463 /* For now, assume transient error, since
464 * the socket is polled for errors */
469 memmove(sk->outbuf.s.buf, ((char *)sk->outbuf.s.buf) + ret, sk->outbuf.s.datasize -= ret);
470 if(sk->writecb != NULL)
471 sk->writecb(sk, sk->data);
475 dbuf = sk->outbuf.d.f;
476 if((sk->outbuf.d.f = dbuf->next) == NULL)
477 sk->outbuf.d.l = NULL;
478 sendto(sk->fd, dbuf->data, dbuf->size, MSG_DONTWAIT | MSG_NOSIGNAL, dbuf->addr, dbuf->addrlen);
482 if(sk->writecb != NULL)
483 sk->writecb(sk, sk->data);
488 void closesock(struct socket *sk)
490 struct sockaddr_un *un;
492 if((sk->family == AF_UNIX) && !sockgetlocalname(sk, (struct sockaddr **)&un, NULL) && (un->sun_family == PF_UNIX))
494 if((sk->state == SOCK_LST) && strchr(un->sun_path, '/'))
496 if(unlink(un->sun_path))
497 flog(LOG_WARNING, "could not unlink Unix socket %s: %s", un->sun_path, strerror(errno));
500 sk->state = SOCK_STL;
506 void sockqueue(struct socket *sk, void *data, size_t size)
508 struct dgrambuf *new;
510 if(sk->state == SOCK_STL)
515 sizebuf(&(sk->outbuf.s.buf), &(sk->outbuf.s.bufsize), sk->outbuf.s.datasize + size, 1, 1);
516 memcpy(sk->outbuf.s.buf + sk->outbuf.s.datasize, data, size);
517 sk->outbuf.s.datasize += size;
520 if(sk->remote == NULL)
522 new = smalloc(sizeof(*new));
524 memcpy(new->data = smalloc(size), data, new->size = size);
525 memcpy(new->addr = smalloc(sk->remotelen), sk->remote, new->addrlen = sk->remotelen);
526 if(sk->outbuf.d.l == NULL)
528 sk->outbuf.d.l = sk->outbuf.d.f = new;
530 sk->outbuf.d.l->next = new;
531 sk->outbuf.d.l = new;
537 size_t sockgetdatalen(struct socket *sk)
545 ret = sk->inbuf.s.datasize;
549 for(b = sk->inbuf.d.f; b != NULL; b = b->next)
556 size_t sockqueuesize(struct socket *sk)
564 ret = sk->outbuf.s.datasize;
568 for(b = sk->outbuf.d.f; b != NULL; b = b->next)
576 * Seriously, I don't know if it's naughty or not to remove
577 * pre-existing Unix sockets.
579 static int rebindunix(struct socket *sk, struct sockaddr *name, socklen_t namelen)
581 struct sockaddr_un *un;
584 if((sk->family != AF_UNIX) || (name->sa_family != PF_UNIX))
586 un = (struct sockaddr_un *)name;
587 if(stat(un->sun_path, &sb))
589 if(!S_ISSOCK(sb.st_mode))
591 if(unlink(un->sun_path))
593 if(bind(sk->fd, name, namelen) < 0)
599 * The difference between netcslisten() and netcslistenlocal() is that
600 * netcslistenlocal() always listens on the local host, instead of
601 * following proxy/passive mode directions. It is suitable for eg. the
602 * UI channel, while the file sharing networks should, naturally, use
603 * netcslisten() instead.
606 struct socket *netcslistenlocal(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
611 /* I don't know if this is actually correct (it probably isn't),
612 * but since, at on least Linux systems, PF_* are specifically
613 * #define'd to their AF_* counterparts, it allows for a severely
614 * smoother implementation. If it breaks something on your
615 * platform, please tell me so.
617 if((sk = mksock(name->sa_family, type)) == NULL)
619 sk->state = SOCK_LST;
620 if(confgetint("net", "reuseaddr"))
623 setsockopt(sk->fd, SOL_SOCKET, SO_REUSEADDR, &intbuf, sizeof(intbuf));
625 if((bind(sk->fd, name, namelen) < 0) && ((errno != EADDRINUSE) || (rebindunix(sk, name, namelen) < 0)))
630 if(listen(sk->fd, 16) < 0)
640 struct socket *netcslisten(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
642 if(confgetint("net", "mode") == 1)
647 if(confgetint("net", "mode") == 0)
648 return(netcslistenlocal(type, name, namelen, func, data));
653 struct socket *netcstcplisten(int port, int local, void (*func)(struct socket *, struct socket *, void *), void *data)
655 struct sockaddr_in addr;
657 struct sockaddr_in6 addr6;
659 struct socket *(*csfunc)(int, struct sockaddr *, socklen_t, void (*)(struct socket *, struct socket *, void *), void *);
663 csfunc = netcslistenlocal;
665 csfunc = netcslisten;
667 memset(&addr6, 0, sizeof(addr6));
668 addr6.sin6_family = AF_INET6;
669 addr6.sin6_port = htons(port);
670 addr6.sin6_addr = in6addr_any;
671 if((ret = csfunc(SOCK_STREAM, (struct sockaddr *)&addr6, sizeof(addr6), func, data)) != NULL)
673 if((ret == NULL) && (errno != EAFNOSUPPORT))
676 memset(&addr, 0, sizeof(addr));
677 addr.sin_family = AF_INET;
678 addr.sin_port = htons(port);
679 return(csfunc(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), func, data));
682 struct socket *netcsdgram(struct sockaddr *name, socklen_t namelen)
687 mode = confgetint("net", "mode");
688 if((mode == 0) || (mode == 1))
690 if((sk = mksock(name->sa_family, SOCK_DGRAM)) == NULL)
692 if(bind(sk->fd, name, namelen) < 0)
697 sk->state = SOCK_EST;
704 struct socket *netdupsock(struct socket *sk)
706 struct socket *newsk;
708 newsk = newsock(sk->type);
709 if((newsk->fd = dup(sk->fd)) < 0)
711 flog(LOG_WARNING, "could not dup() socket: %s", strerror(errno));
715 newsk->state = sk->state;
716 newsk->ignread = sk->ignread;
717 if(sk->remote != NULL)
718 memcpy(newsk->remote = smalloc(sk->remotelen), sk->remote, newsk->remotelen = sk->remotelen);
722 void netdgramconn(struct socket *sk, struct sockaddr *addr, socklen_t addrlen)
724 if(sk->remote != NULL)
726 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
730 struct socket *netcsconn(struct sockaddr *addr, socklen_t addrlen, void (*func)(struct socket *, int, void *), void *data)
735 mode = confgetint("net", "mode");
736 if((mode == 0) || (mode == 1))
738 if((sk = mksock(addr->sa_family, SOCK_STREAM)) == NULL)
740 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
741 if(!connect(sk->fd, addr, addrlen))
743 sk->state = SOCK_EST;
747 if(errno == EINPROGRESS)
749 sk->state = SOCK_SYN;
761 int pollsocks(int timeout)
767 struct socket *sk, *next, *newsk;
768 struct sockaddr_storage ss;
771 pfds = smalloc(sizeof(*pfds) * (num = numsocks));
772 for(i = 0, sk = sockets; i < num; sk = sk->next)
774 if(sk->state == SOCK_STL)
782 pfds[i].events |= POLLIN;
783 if((sk->state == SOCK_SYN) || (sockqueuesize(sk) > 0))
784 pfds[i].events |= POLLOUT;
788 ret = poll(pfds, num, timeout);
793 flog(LOG_CRIT, "pollsocks: poll errored out: %s", strerror(errno));
794 /* To avoid CPU hogging in case it's bad, which it
801 for(sk = sockets; sk != NULL; sk = next)
804 for(i = 0; i < num; i++)
806 if(pfds[i].fd == sk->fd)
814 if(pfds[i].revents & POLLIN)
817 if((newfd = accept(sk->fd, (struct sockaddr *)&ss, &sslen)) < 0)
819 if(sk->errcb != NULL)
820 sk->errcb(sk, errno, sk->data);
822 newsk = newsock(sk->type);
824 newsk->family = sk->family;
825 newsk->state = SOCK_EST;
826 memcpy(newsk->remote = smalloc(sslen), &ss, sslen);
827 newsk->remotelen = sslen;
829 if(sk->acceptcb != NULL)
830 sk->acceptcb(sk, newsk, sk->data);
832 if(pfds[i].revents & POLLERR)
834 retlen = sizeof(ret);
835 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
836 if(sk->errcb != NULL)
837 sk->errcb(sk, ret, sk->data);
842 if(pfds[i].revents & POLLERR)
844 retlen = sizeof(ret);
845 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
846 if(sk->conncb != NULL)
847 sk->conncb(sk, ret, sk->data);
851 if(pfds[i].revents & (POLLIN | POLLOUT))
853 sk->state = SOCK_EST;
854 if(sk->conncb != NULL)
855 sk->conncb(sk, 0, sk->data);
859 if(pfds[i].revents & POLLERR)
861 retlen = sizeof(ret);
862 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
863 if(sk->errcb != NULL)
864 sk->errcb(sk, ret, sk->data);
868 if(pfds[i].revents & POLLIN)
870 if(pfds[i].revents & POLLOUT)
872 if(sockqueuesize(sk) > 0)
877 if(pfds[i].revents & POLLNVAL)
879 flog(LOG_CRIT, "BUG: stale socket struct on fd %i", sk->fd);
880 sk->state = SOCK_STL;
884 if(pfds[i].revents & POLLHUP)
886 if(sk->errcb != NULL)
887 sk->errcb(sk, 0, sk->data);
894 for(sk = sockets; sk != NULL; sk = next)
897 if(sk->refcount == 1 && (sockqueuesize(sk) == 0))
902 if(sk->close && (sockqueuesize(sk) == 0))
904 if(sk->state == SOCK_STL)
913 int socksettos(struct socket *sk, int tos)
917 if(sk->family == AF_UNIX)
918 return(0); /* Unix sockets are always perfect. :) */
919 if(sk->family == AF_INET)
926 case SOCK_TOS_MINCOST:
929 case SOCK_TOS_MAXREL:
935 case SOCK_TOS_MINDELAY:
939 flog(LOG_WARNING, "attempted to set unknown TOS value %i to IPv4 sock", tos);
942 if(setsockopt(sk->fd, SOL_IP, IP_TOS, &buf, sizeof(buf)) < 0)
944 flog(LOG_WARNING, "could not set sock TOS to %i: %s", tos, strerror(errno));
949 if(sk->family == AF_INET6)
955 case SOCK_TOS_MINCOST:
956 buf = confgetint("net", "diffserv-mincost");
958 case SOCK_TOS_MAXREL:
959 buf = confgetint("net", "diffserv-maxrel");
962 buf = confgetint("net", "diffserv-maxtp");
964 case SOCK_TOS_MINDELAY:
965 buf = confgetint("net", "diffserv-mindelay");
968 flog(LOG_WARNING, "attempted to set unknown TOS value %i to IPv4 sock", tos);
972 On Linux, the API IPv6 flow label management doesn't seem to
973 be entirely complete, so I guess this will have to wait.
975 if(setsockopt(...) < 0)
977 flog(LOG_WARNING, "could not set sock traffic class to %i: %s", tos, strerror(errno));
983 flog(LOG_WARNING, "could not set TOS on sock of family %i", sk->family);
990 void (*callback)(struct sockaddr *addr, int addrlen, void *data);
992 struct sockaddr_storage addr;
996 static void resolvecb(pid_t pid, int status, struct resolvedata *data)
1000 struct sockaddr_in *ipv4;
1004 if((ret = read(data->fd, buf, sizeof(buf))) != 4)
1007 data->callback(NULL, 0, data->data);
1009 ipv4 = (struct sockaddr_in *)&data->addr;
1010 memcpy(&ipv4->sin_addr, buf, 4);
1011 data->callback((struct sockaddr *)ipv4, sizeof(*ipv4), data->data);
1015 data->callback(NULL, 0, data->data);
1021 int netresolve(char *addr, void (*callback)(struct sockaddr *addr, int addrlen, void *data), void *data)
1028 struct resolvedata *rdata;
1029 struct sockaddr_in ipv4;
1035 if((p = strchr(addr, ':')) != NULL)
1040 ipv4.sin_family = AF_INET;
1041 ipv4.sin_port = htons(port);
1042 if(inet_aton(addr, &ipv4.sin_addr))
1044 callback((struct sockaddr *)&ipv4, sizeof(ipv4), data);
1046 sigemptyset(&sigset);
1047 sigaddset(&sigset, SIGCHLD);
1048 sigprocmask(SIG_BLOCK, &sigset, NULL);
1049 if((pipe(pfd) < 0) || ((child = fork()) < 0))
1051 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1056 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1057 for(i = 3; i < FD_SETSIZE; i++)
1062 signal(SIGALRM, SIG_DFL);
1064 if((he = gethostbyname(addr)) == NULL)
1066 write(pfd[1], he->h_addr_list[0], 4);
1070 fcntl(pfd[0], F_SETFL, fcntl(pfd[0], F_GETFL) | O_NONBLOCK);
1071 rdata = smalloc(sizeof(*rdata));
1073 rdata->callback = callback;
1075 memcpy(&rdata->addr, &ipv4, rdata->addrlen = sizeof(ipv4));
1076 childcallback(child, (void (*)(pid_t, int, void *))resolvecb, rdata);
1077 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1084 int sockgetlocalname(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
1087 struct sockaddr_storage name;
1090 if((sk->state == SOCK_STL) || (sk->fd < 0))
1093 if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0)
1095 flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetlocalname (%s)", strerror(errno));
1098 *namebuf = memcpy(smalloc(len), &name, len);
1104 static void sethostaddr(struct sockaddr *dst, struct sockaddr *src)
1106 if(dst->sa_family != src->sa_family)
1108 flog(LOG_ERR, "BUG: non-matching socket families in sethostaddr (%i -> %i)", src->sa_family, dst->sa_family);
1111 switch(src->sa_family)
1114 ((struct sockaddr_in *)dst)->sin_addr = ((struct sockaddr_in *)src)->sin_addr;
1117 ((struct sockaddr_in6 *)dst)->sin6_addr = ((struct sockaddr_in6 *)src)->sin6_addr;
1120 flog(LOG_WARNING, "sethostaddr unimplemented for family %i", src->sa_family);
1125 static int makepublic(struct sockaddr *addr)
1129 struct sockaddr *pname;
1131 if((ret = getpublicaddr(addr->sa_family, &pname, &plen)) < 0)
1133 flog(LOG_ERR, "could not get public address: %s", strerror(errno));
1138 sethostaddr(addr, pname);
1143 int sockgetremotename(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
1146 struct sockaddr *name;
1148 switch(confgetint("net", "mode"))
1152 if((sk->state == SOCK_STL) || (sk->fd < 0))
1157 if(!sockgetlocalname(sk, &name, &len))
1164 flog(LOG_ERR, "could not get remotely accessible name by any means");
1170 flog(LOG_CRIT, "unknown net mode %i active", confgetint("net", "mode"));
1176 int sockgetremotename2(struct socket *sk, struct socket *sk2, struct sockaddr **namebuf, socklen_t *lenbuf)
1178 struct sockaddr *name1, *name2;
1179 socklen_t len1, len2;
1181 if(sk->family != sk2->family)
1183 flog(LOG_ERR, "using sockgetremotename2 with sockets of differing family: %i %i", sk->family, sk2->family);
1186 if(sockgetremotename(sk, &name1, &len1))
1188 if(sockgetremotename(sk2, &name2, &len2)) {
1192 sethostaddr(name1, name2);
1199 int addreq(struct sockaddr *x, struct sockaddr *y)
1201 struct sockaddr_un *u1, *u2;
1202 struct sockaddr_in *n1, *n2;
1204 struct sockaddr_in6 *s1, *s2;
1207 if(x->sa_family != y->sa_family)
1209 switch(x->sa_family) {
1211 u1 = (struct sockaddr_un *)x; u2 = (struct sockaddr_un *)y;
1212 if(strncmp(u1->sun_path, u2->sun_path, sizeof(u1->sun_path)))
1216 n1 = (struct sockaddr_in *)x; n2 = (struct sockaddr_in *)y;
1217 if(n1->sin_port != n2->sin_port)
1219 if(n1->sin_addr.s_addr != n2->sin_addr.s_addr)
1224 s1 = (struct sockaddr_in6 *)x; s2 = (struct sockaddr_in6 *)y;
1225 if(s1->sin6_port != s2->sin6_port)
1227 if(memcmp(s1->sin6_addr.s6_addr, s2->sin6_addr.s6_addr, sizeof(s1->sin6_addr.s6_addr)))
1235 char *formataddress(struct sockaddr *arg, socklen_t arglen)
1237 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1238 * lowercase letters to 1, so I do this
1240 struct sockaddr_in *ipv4;
1242 struct sockaddr_in6 *ipv6;
1244 static char *ret = NULL;
1250 switch(arg->sa_family)
1253 UNIX = (struct sockaddr_un *)arg;
1254 ret = sprintf2("%s", UNIX->sun_path);
1257 ipv4 = (struct sockaddr_in *)arg;
1258 if(inet_ntop(AF_INET, &ipv4->sin_addr, buf, sizeof(buf)) == NULL)
1260 ret = sprintf2("%s:%i", buf, (int)ntohs(ipv4->sin_port));
1264 ipv6 = (struct sockaddr_in6 *)arg;
1265 if(inet_ntop(AF_INET6, &ipv6->sin6_addr, buf, sizeof(buf)) == NULL)
1267 ret = sprintf2("[%s]:%i", buf, (int)ntohs(ipv6->sin6_port));
1271 errno = EPFNOSUPPORT;
1280 * It was very nice to use this, but it seems
1281 * to mess things up, so I guess it has to go... :-(
1284 static int formataddress(FILE *stream, const struct printf_info *info, const void *const *args)
1286 struct sockaddr *arg;
1288 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1289 * lowercase letters to 1, so I do this
1291 struct sockaddr_in *ipv4;
1294 arg = *(struct sockaddr **)(args[0]);
1295 arglen = *(socklen_t *)(args[1]);
1296 switch(arg->sa_family)
1299 UNIX = (struct sockaddr_un *)arg;
1300 ret = fprintf(stream, "%s", UNIX->sun_path);
1303 ipv4 = (struct sockaddr_in *)arg;
1304 ret = fprintf(stream, "%s:%i", inet_ntoa(ipv4->sin_addr), (int)ntohs(ipv4->sin_port));
1308 errno = EPFNOSUPPORT;
1314 static int formataddress_arginfo(const struct printf_info *info, size_t n, int *argtypes)
1317 argtypes[0] = PA_POINTER;
1319 argtypes[1] = PA_INT; /* Sources tell me that socklen_t _must_
1320 * be an int, so I guess this should be
1326 static int init(int hup)
1331 if(register_printf_function('N', formataddress, formataddress_arginfo))
1333 flog(LOG_CRIT, "could not register printf handler %%N: %s", strerror(errno));
1341 static void terminate(void)
1343 while(sockets != NULL)
1344 unlinksock(sockets);
1347 static struct module me =
1355 .terminate = terminate