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;
191 new->outbuf.s.buf = NULL;
192 new->outbuf.s.bufsize = 0;
193 new->outbuf.s.datasize = 0;
194 new->inbuf.s.buf = NULL;
195 new->inbuf.s.bufsize = 0;
196 new->inbuf.s.datasize = 0;
199 new->outbuf.d.f = new->outbuf.d.l = NULL;
200 new->inbuf.d.f = new->inbuf.d.l = NULL;
207 new->acceptcb = NULL;
217 static struct socket *mksock(int domain, int type)
222 if((fd = socket(domain, type, 0)) < 0)
224 flog(LOG_CRIT, "could not create socket: %s", strerror(errno));
229 new->family = domain;
230 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
234 struct socket *wrapsock(int fd)
238 new = newsock(SOCK_STREAM);
240 new->state = SOCK_EST;
241 new->isrealsocket = 0;
242 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
246 static void unlinksock(struct socket *sk)
249 sk->prev->next = sk->next;
251 sk->next->prev = sk->prev;
258 void getsock(struct socket *sk)
263 void putsock(struct socket *sk)
265 struct dgrambuf *buf;
267 if(--(sk->refcount) == 0)
272 if(sk->outbuf.s.buf != NULL)
273 free(sk->outbuf.s.buf);
274 if(sk->inbuf.s.buf != NULL)
275 free(sk->inbuf.s.buf);
278 while((buf = sk->outbuf.d.f) != NULL)
280 sk->outbuf.d.f = buf->next;
285 while((buf = sk->inbuf.d.f) != NULL)
287 sk->inbuf.d.f = buf->next;
295 if(sk->remote != NULL)
301 void sockpushdata(struct socket *sk, void *buf, size_t size)
306 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + size, 1, 1);
307 memmove(sk->inbuf.s.buf + size, sk->inbuf.s.buf, sk->inbuf.s.datasize);
308 memcpy(sk->inbuf.s.buf, buf, size);
309 sk->inbuf.s.datasize += size;
318 void *sockgetinbuf(struct socket *sk, size_t *size)
321 struct dgrambuf *dbuf;
326 if((sk->inbuf.s.buf == NULL) || (sk->inbuf.s.datasize == 0))
331 buf = sk->inbuf.s.buf;
332 *size = sk->inbuf.s.datasize;
333 sk->inbuf.s.buf = NULL;
334 sk->inbuf.s.bufsize = sk->inbuf.s.datasize = 0;
337 if((dbuf = sk->inbuf.d.f) == NULL)
339 sk->inbuf.d.f = dbuf->next;
340 if(dbuf->next == NULL)
341 sk->inbuf.d.l = NULL;
351 static void recvcmsg(struct socket *sk, struct msghdr *msg)
353 struct cmsghdr *cmsg;
355 for(cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL; cmsg = CMSG_NXTHDR(msg, cmsg))
357 #if UNIX_AUTH_STYLE == 1
358 if((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SCM_CREDENTIALS))
361 if(sk->ucred.uid == -1)
363 cred = (struct ucred *)CMSG_DATA(cmsg);
364 sk->ucred.uid = cred->uid;
365 sk->ucred.gid = cred->gid;
372 static void sockrecv(struct socket *sk)
375 struct dgrambuf *dbuf;
380 memset(&msg, 0, sizeof(msg));
381 msg.msg_iov = &bufvec;
383 msg.msg_control = cbuf;
384 msg.msg_controllen = sizeof(cbuf);
388 #if defined(HAVE_LINUX_SOCKIOS_H) && defined(SIOCINQ)
389 /* SIOCINQ is Linux-specific AFAIK, but I really have no idea
390 * how to read the inqueue size on other OSs */
391 if(ioctl(sk->fd, SIOCINQ, &inq))
393 /* I don't really know what could go wrong here, so let's
394 * assume it's transient. */
395 flog(LOG_WARNING, "SIOCINQ return %s on socket %i, falling back to 2048 bytes", strerror(errno), sk->fd);
403 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + inq, 1, 1);
406 bufvec.iov_base = sk->inbuf.s.buf + sk->inbuf.s.datasize;
407 bufvec.iov_len = inq;
408 ret = recvmsg(sk->fd, &msg, 0);
410 ret = read(sk->fd, sk->inbuf.s.buf + sk->inbuf.s.datasize, inq);
411 msg.msg_controllen = 0;
416 if((errno == EINTR) || (errno == EAGAIN))
418 if(sk->errcb != NULL)
419 sk->errcb(sk, errno, sk->data);
423 if(msg.msg_flags & MSG_CTRUNC)
424 flog(LOG_DEBUG, "ancillary data was truncated");
429 if(sk->errcb != NULL)
430 sk->errcb(sk, 0, sk->data);
434 sk->inbuf.s.datasize += ret;
435 if(sk->readcb != NULL)
436 sk->readcb(sk, sk->data);
439 #if defined(HAVE_LINUX_SOCKIOS_H) && defined(SIOCINQ)
440 if(ioctl(sk->fd, SIOCINQ, &inq))
442 /* I don't really know what could go wrong here, so let's
443 * assume it's transient. */
444 flog(LOG_WARNING, "SIOCINQ return %s on socket %i", strerror(errno), sk->fd);
450 dbuf = smalloc(sizeof(*dbuf));
451 dbuf->data = smalloc(inq);
452 dbuf->addr = smalloc(dbuf->addrlen = sizeof(struct sockaddr_storage));
454 ret = recvfrom(sk->fd, dbuf->data, inq, 0, dbuf->addr, &dbuf->addrlen);
456 msg.msg_name = dbuf->addr;
457 msg.msg_namelen = dbuf->addrlen;
458 bufvec.iov_base = dbuf->data;
459 bufvec.iov_len = inq;
460 ret = recvmsg(sk->fd, &msg, 0);
461 dbuf->addrlen = msg.msg_namelen;
467 if((errno == EINTR) || (errno == EAGAIN))
469 if(sk->errcb != NULL)
470 sk->errcb(sk, errno, sk->data);
474 if(msg.msg_flags & MSG_CTRUNC)
475 flog(LOG_DEBUG, "ancillary data was truncated");
478 /* On UDP/IPv[46], ret == 0 doesn't mean EOF (since UDP can't
479 * have EOF), but rather an empty packet. I don't know if any
480 * other potential DGRAM protocols might have an EOF
481 * condition, so let's play safe. */
487 if(!((sk->family == AF_INET) || (sk->family == AF_INET6)))
489 if(sk->errcb != NULL)
490 sk->errcb(sk, 0, sk->data);
495 dbuf->addr = srealloc(dbuf->addr, dbuf->addrlen);
496 dbuf->data = srealloc(dbuf->data, dbuf->size = ret);
498 if(sk->inbuf.d.l != NULL)
499 sk->inbuf.d.l->next = dbuf;
501 sk->inbuf.d.f = dbuf;
502 sk->inbuf.d.l = dbuf;
503 if(sk->readcb != NULL)
504 sk->readcb(sk, sk->data);
509 static void sockflush(struct socket *sk)
512 struct dgrambuf *dbuf;
518 ret = send(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize, MSG_DONTWAIT | MSG_NOSIGNAL);
520 ret = write(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize);
523 /* For now, assume transient error, since
524 * the socket is polled for errors */
529 memmove(sk->outbuf.s.buf, ((char *)sk->outbuf.s.buf) + ret, sk->outbuf.s.datasize -= ret);
530 if(sk->writecb != NULL)
531 sk->writecb(sk, sk->data);
535 dbuf = sk->outbuf.d.f;
536 if((sk->outbuf.d.f = dbuf->next) == NULL)
537 sk->outbuf.d.l = NULL;
538 sendto(sk->fd, dbuf->data, dbuf->size, MSG_DONTWAIT | MSG_NOSIGNAL, dbuf->addr, dbuf->addrlen);
542 if(sk->writecb != NULL)
543 sk->writecb(sk, sk->data);
548 void closesock(struct socket *sk)
550 struct sockaddr_un *un;
552 if((sk->family == AF_UNIX) && !sockgetlocalname(sk, (struct sockaddr **)&un, NULL) && (un->sun_family == PF_UNIX))
554 if((sk->state == SOCK_LST) && strchr(un->sun_path, '/'))
556 if(unlink(un->sun_path))
557 flog(LOG_WARNING, "could not unlink Unix socket %s: %s", un->sun_path, strerror(errno));
560 sk->state = SOCK_STL;
566 void sockqueue(struct socket *sk, void *data, size_t size)
568 struct dgrambuf *new;
570 if(sk->state == SOCK_STL)
575 sizebuf(&(sk->outbuf.s.buf), &(sk->outbuf.s.bufsize), sk->outbuf.s.datasize + size, 1, 1);
576 memcpy(sk->outbuf.s.buf + sk->outbuf.s.datasize, data, size);
577 sk->outbuf.s.datasize += size;
580 if(sk->remote == NULL)
582 new = smalloc(sizeof(*new));
584 memcpy(new->data = smalloc(size), data, new->size = size);
585 memcpy(new->addr = smalloc(sk->remotelen), sk->remote, new->addrlen = sk->remotelen);
586 if(sk->outbuf.d.l == NULL)
588 sk->outbuf.d.l = sk->outbuf.d.f = new;
590 sk->outbuf.d.l->next = new;
591 sk->outbuf.d.l = new;
597 size_t sockgetdatalen(struct socket *sk)
605 ret = sk->inbuf.s.datasize;
609 for(b = sk->inbuf.d.f; b != NULL; b = b->next)
616 size_t sockqueuesize(struct socket *sk)
624 ret = sk->outbuf.s.datasize;
628 for(b = sk->outbuf.d.f; b != NULL; b = b->next)
636 * Seriously, I don't know if it's naughty or not to remove
637 * pre-existing Unix sockets.
639 static int rebindunix(struct socket *sk, struct sockaddr *name, socklen_t namelen)
641 struct sockaddr_un *un;
644 if((sk->family != AF_UNIX) || (name->sa_family != PF_UNIX))
646 un = (struct sockaddr_un *)name;
647 if(stat(un->sun_path, &sb))
649 if(!S_ISSOCK(sb.st_mode))
651 if(unlink(un->sun_path))
653 if(bind(sk->fd, name, namelen) < 0)
659 * The difference between netcslisten() and netcslistenlocal() is that
660 * netcslistenlocal() always listens on the local host, instead of
661 * following proxy/passive mode directions. It is suitable for eg. the
662 * UI channel, while the file sharing networks should, naturally, use
663 * netcslisten() instead.
666 struct socket *netcslistenlocal(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
671 /* I don't know if this is actually correct (it probably isn't),
672 * but since, at on least Linux systems, PF_* are specifically
673 * #define'd to their AF_* counterparts, it allows for a severely
674 * smoother implementation. If it breaks something on your
675 * platform, please tell me so.
677 if((sk = mksock(name->sa_family, type)) == NULL)
679 sk->state = SOCK_LST;
680 if(confgetint("net", "reuseaddr"))
683 setsockopt(sk->fd, SOL_SOCKET, SO_REUSEADDR, &intbuf, sizeof(intbuf));
685 if((bind(sk->fd, name, namelen) < 0) && ((errno != EADDRINUSE) || (rebindunix(sk, name, namelen) < 0)))
690 if(listen(sk->fd, 16) < 0)
700 struct socket *netcslisten(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
702 if(confgetint("net", "mode") == 1)
707 if(confgetint("net", "mode") == 0)
708 return(netcslistenlocal(type, name, namelen, func, data));
713 struct socket *netcstcplisten(int port, int local, void (*func)(struct socket *, struct socket *, void *), void *data)
715 struct sockaddr_in addr;
717 struct sockaddr_in6 addr6;
719 struct socket *(*csfunc)(int, struct sockaddr *, socklen_t, void (*)(struct socket *, struct socket *, void *), void *);
723 csfunc = netcslistenlocal;
725 csfunc = netcslisten;
727 memset(&addr6, 0, sizeof(addr6));
728 addr6.sin6_family = AF_INET6;
729 addr6.sin6_port = htons(port);
730 addr6.sin6_addr = in6addr_any;
731 if((ret = csfunc(SOCK_STREAM, (struct sockaddr *)&addr6, sizeof(addr6), func, data)) != NULL)
733 if((ret == NULL) && (errno != EAFNOSUPPORT))
736 memset(&addr, 0, sizeof(addr));
737 addr.sin_family = AF_INET;
738 addr.sin_port = htons(port);
739 return(csfunc(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), func, data));
742 struct socket *netcsdgram(struct sockaddr *name, socklen_t namelen)
747 mode = confgetint("net", "mode");
748 if((mode == 0) || (mode == 1))
750 if((sk = mksock(name->sa_family, SOCK_DGRAM)) == NULL)
752 if(bind(sk->fd, name, namelen) < 0)
757 sk->state = SOCK_EST;
764 struct socket *netdupsock(struct socket *sk)
766 struct socket *newsk;
768 newsk = newsock(sk->type);
769 if((newsk->fd = dup(sk->fd)) < 0)
771 flog(LOG_WARNING, "could not dup() socket: %s", strerror(errno));
775 newsk->state = sk->state;
776 newsk->ignread = sk->ignread;
777 if(sk->remote != NULL)
778 memcpy(newsk->remote = smalloc(sk->remotelen), sk->remote, newsk->remotelen = sk->remotelen);
782 void netdgramconn(struct socket *sk, struct sockaddr *addr, socklen_t addrlen)
784 if(sk->remote != NULL)
786 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
790 struct socket *netcsconn(struct sockaddr *addr, socklen_t addrlen, void (*func)(struct socket *, int, void *), void *data)
795 mode = confgetint("net", "mode");
796 if((mode == 0) || (mode == 1))
798 if((sk = mksock(addr->sa_family, SOCK_STREAM)) == NULL)
800 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
801 if(!connect(sk->fd, addr, addrlen))
803 sk->state = SOCK_EST;
807 if(errno == EINPROGRESS)
809 sk->state = SOCK_SYN;
821 static void acceptunix(struct socket *sk)
826 #if UNIX_AUTH_STYLE == 1
827 if(setsockopt(sk->fd, SOL_SOCKET, SO_PASSCRED, &buf, sizeof(buf)) < 0)
828 flog(LOG_WARNING, "could not enable SO_PASSCRED on Unix socket %i: %s", sk->fd, strerror(errno));
829 #elif UNIX_AUTH_STYLE == 2
830 if(getpeereid(sk->fd, &sk->ucred.uid, &sk->ucred.gid) < 0)
832 flog(LOG_WARNING, "could not get peer creds on Unix socket %i: %s", sk->fd, strerror(errno));
839 int pollsocks(int timeout)
845 struct socket *sk, *next, *newsk;
846 struct sockaddr_storage ss;
849 pfds = smalloc(sizeof(*pfds) * (num = numsocks));
850 for(i = 0, sk = sockets; i < num; sk = sk->next)
852 if(sk->state == SOCK_STL)
860 pfds[i].events |= POLLIN;
861 if((sk->state == SOCK_SYN) || (sockqueuesize(sk) > 0))
862 pfds[i].events |= POLLOUT;
866 ret = poll(pfds, num, timeout);
871 flog(LOG_CRIT, "pollsocks: poll errored out: %s", strerror(errno));
872 /* To avoid CPU hogging in case it's bad, which it
879 for(sk = sockets; sk != NULL; sk = next)
882 for(i = 0; i < num; i++)
884 if(pfds[i].fd == sk->fd)
892 if(pfds[i].revents & POLLIN)
895 if((newfd = accept(sk->fd, (struct sockaddr *)&ss, &sslen)) < 0)
897 if(sk->errcb != NULL)
898 sk->errcb(sk, errno, sk->data);
900 newsk = newsock(sk->type);
902 newsk->family = sk->family;
903 newsk->state = SOCK_EST;
904 memcpy(newsk->remote = smalloc(sslen), &ss, sslen);
905 newsk->remotelen = sslen;
906 if(ss.ss_family == PF_UNIX)
908 if(sk->acceptcb != NULL)
909 sk->acceptcb(sk, newsk, sk->data);
912 if(pfds[i].revents & POLLERR)
914 retlen = sizeof(ret);
915 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
916 if(sk->errcb != NULL)
917 sk->errcb(sk, ret, sk->data);
922 if(pfds[i].revents & POLLERR)
924 retlen = sizeof(ret);
925 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
926 if(sk->conncb != NULL)
927 sk->conncb(sk, ret, sk->data);
931 if(pfds[i].revents & (POLLIN | POLLOUT))
933 sk->state = SOCK_EST;
934 if(sk->conncb != NULL)
935 sk->conncb(sk, 0, sk->data);
939 if(pfds[i].revents & POLLERR)
941 retlen = sizeof(ret);
942 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
943 if(sk->errcb != NULL)
944 sk->errcb(sk, ret, sk->data);
948 if(pfds[i].revents & POLLIN)
950 if(pfds[i].revents & POLLOUT)
952 if(sockqueuesize(sk) > 0)
957 if(pfds[i].revents & POLLNVAL)
959 flog(LOG_CRIT, "BUG: stale socket struct on fd %i", sk->fd);
960 sk->state = SOCK_STL;
964 if(pfds[i].revents & POLLHUP)
966 if(sk->errcb != NULL)
967 sk->errcb(sk, 0, sk->data);
974 for(sk = sockets; sk != NULL; sk = next)
977 if(sk->refcount == 1 && (sockqueuesize(sk) == 0))
982 if(sk->close && (sockqueuesize(sk) == 0))
984 if(sk->state == SOCK_STL)
993 int socksettos(struct socket *sk, int tos)
997 if(sk->family == AF_UNIX)
998 return(0); /* Unix sockets are always perfect. :) */
999 if(sk->family == AF_INET)
1006 case SOCK_TOS_MINCOST:
1009 case SOCK_TOS_MAXREL:
1012 case SOCK_TOS_MAXTP:
1015 case SOCK_TOS_MINDELAY:
1019 flog(LOG_WARNING, "attempted to set unknown TOS value %i to IPv4 sock", tos);
1022 if(setsockopt(sk->fd, IPPROTO_IP, IP_TOS, &buf, sizeof(buf)) < 0)
1024 flog(LOG_WARNING, "could not set sock TOS to %i: %s", tos, strerror(errno));
1029 if(sk->family == AF_INET6)
1035 case SOCK_TOS_MINCOST:
1036 buf = confgetint("net", "diffserv-mincost");
1038 case SOCK_TOS_MAXREL:
1039 buf = confgetint("net", "diffserv-maxrel");
1041 case SOCK_TOS_MAXTP:
1042 buf = confgetint("net", "diffserv-maxtp");
1044 case SOCK_TOS_MINDELAY:
1045 buf = confgetint("net", "diffserv-mindelay");
1048 flog(LOG_WARNING, "attempted to set unknown TOS value %i to IPv4 sock", tos);
1052 On Linux, the API IPv6 flow label management doesn't seem to
1053 be entirely complete, so I guess this will have to wait.
1055 if(setsockopt(...) < 0)
1057 flog(LOG_WARNING, "could not set sock traffic class to %i: %s", tos, strerror(errno));
1063 flog(LOG_WARNING, "could not set TOS on sock of family %i", sk->family);
1070 void (*callback)(struct sockaddr *addr, int addrlen, void *data);
1072 struct sockaddr_storage addr;
1076 static void resolvecb(pid_t pid, int status, struct resolvedata *data)
1078 static char buf[80];
1080 struct sockaddr_in *ipv4;
1084 if((ret = read(data->fd, buf, sizeof(buf))) != 4)
1087 data->callback(NULL, 0, data->data);
1089 ipv4 = (struct sockaddr_in *)&data->addr;
1090 memcpy(&ipv4->sin_addr, buf, 4);
1091 data->callback((struct sockaddr *)ipv4, sizeof(*ipv4), data->data);
1095 data->callback(NULL, 0, data->data);
1101 int netresolve(char *addr, void (*callback)(struct sockaddr *addr, int addrlen, void *data), void *data)
1108 struct resolvedata *rdata;
1109 struct sockaddr_in ipv4;
1115 if((p = strchr(addr, ':')) != NULL)
1120 ipv4.sin_family = AF_INET;
1121 ipv4.sin_port = htons(port);
1122 if(inet_aton(addr, &ipv4.sin_addr))
1124 callback((struct sockaddr *)&ipv4, sizeof(ipv4), data);
1126 sigemptyset(&sigset);
1127 sigaddset(&sigset, SIGCHLD);
1128 sigprocmask(SIG_BLOCK, &sigset, NULL);
1129 if((pipe(pfd) < 0) || ((child = fork()) < 0))
1131 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1136 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1137 for(i = 3; i < FD_SETSIZE; i++)
1142 signal(SIGALRM, SIG_DFL);
1144 if((he = gethostbyname(addr)) == NULL)
1146 write(pfd[1], he->h_addr_list[0], 4);
1150 fcntl(pfd[0], F_SETFL, fcntl(pfd[0], F_GETFL) | O_NONBLOCK);
1151 rdata = smalloc(sizeof(*rdata));
1153 rdata->callback = callback;
1155 memcpy(&rdata->addr, &ipv4, rdata->addrlen = sizeof(ipv4));
1156 childcallback(child, (void (*)(pid_t, int, void *))resolvecb, rdata);
1157 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1164 int sockgetlocalname(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
1167 struct sockaddr_storage name;
1170 if((sk->state == SOCK_STL) || (sk->fd < 0))
1173 if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0)
1175 flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetlocalname (%s)", strerror(errno));
1178 *namebuf = memcpy(smalloc(len), &name, len);
1184 static void sethostaddr(struct sockaddr *dst, struct sockaddr *src)
1186 if(dst->sa_family != src->sa_family)
1188 flog(LOG_ERR, "BUG: non-matching socket families in sethostaddr (%i -> %i)", src->sa_family, dst->sa_family);
1191 switch(src->sa_family)
1194 ((struct sockaddr_in *)dst)->sin_addr = ((struct sockaddr_in *)src)->sin_addr;
1197 ((struct sockaddr_in6 *)dst)->sin6_addr = ((struct sockaddr_in6 *)src)->sin6_addr;
1200 flog(LOG_WARNING, "sethostaddr unimplemented for family %i", src->sa_family);
1205 static int makepublic(struct sockaddr *addr)
1209 struct sockaddr *pname;
1211 if((ret = getpublicaddr(addr->sa_family, &pname, &plen)) < 0)
1213 flog(LOG_ERR, "could not get public address: %s", strerror(errno));
1218 sethostaddr(addr, pname);
1223 int sockgetremotename(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
1226 struct sockaddr *name;
1228 switch(confgetint("net", "mode"))
1232 if((sk->state == SOCK_STL) || (sk->fd < 0))
1237 if(!sockgetlocalname(sk, &name, &len))
1244 flog(LOG_ERR, "could not get remotely accessible name by any means");
1250 flog(LOG_CRIT, "unknown net mode %i active", confgetint("net", "mode"));
1256 int sockgetremotename2(struct socket *sk, struct socket *sk2, struct sockaddr **namebuf, socklen_t *lenbuf)
1258 struct sockaddr *name1, *name2;
1259 socklen_t len1, len2;
1261 if(sk->family != sk2->family)
1263 flog(LOG_ERR, "using sockgetremotename2 with sockets of differing family: %i %i", sk->family, sk2->family);
1266 if(sockgetremotename(sk, &name1, &len1))
1268 if(sockgetremotename(sk2, &name2, &len2)) {
1272 sethostaddr(name1, name2);
1279 int addreq(struct sockaddr *x, struct sockaddr *y)
1281 struct sockaddr_un *u1, *u2;
1282 struct sockaddr_in *n1, *n2;
1284 struct sockaddr_in6 *s1, *s2;
1287 if(x->sa_family != y->sa_family)
1289 switch(x->sa_family) {
1291 u1 = (struct sockaddr_un *)x; u2 = (struct sockaddr_un *)y;
1292 if(strncmp(u1->sun_path, u2->sun_path, sizeof(u1->sun_path)))
1296 n1 = (struct sockaddr_in *)x; n2 = (struct sockaddr_in *)y;
1297 if(n1->sin_port != n2->sin_port)
1299 if(n1->sin_addr.s_addr != n2->sin_addr.s_addr)
1304 s1 = (struct sockaddr_in6 *)x; s2 = (struct sockaddr_in6 *)y;
1305 if(s1->sin6_port != s2->sin6_port)
1307 if(memcmp(s1->sin6_addr.s6_addr, s2->sin6_addr.s6_addr, sizeof(s1->sin6_addr.s6_addr)))
1315 char *formataddress(struct sockaddr *arg, socklen_t arglen)
1317 struct sockaddr_in *ipv4;
1319 struct sockaddr_in6 *ipv6;
1321 static char *ret = NULL;
1327 switch(arg->sa_family)
1330 ret = sstrdup("Unix socket");
1333 ipv4 = (struct sockaddr_in *)arg;
1334 if(inet_ntop(AF_INET, &ipv4->sin_addr, buf, sizeof(buf)) == NULL)
1336 ret = sprintf2("%s:%i", buf, (int)ntohs(ipv4->sin_port));
1340 ipv6 = (struct sockaddr_in6 *)arg;
1341 if(inet_ntop(AF_INET6, &ipv6->sin6_addr, buf, sizeof(buf)) == NULL)
1343 ret = sprintf2("[%s]:%i", buf, (int)ntohs(ipv6->sin6_port));
1347 errno = EPFNOSUPPORT;
1356 * It was very nice to use this, but it seems
1357 * to mess things up, so I guess it has to go... :-(
1360 static int formataddress(FILE *stream, const struct printf_info *info, const void *const *args)
1362 struct sockaddr *arg;
1364 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1365 * lowercase letters to 1, so I do this
1367 struct sockaddr_in *ipv4;
1370 arg = *(struct sockaddr **)(args[0]);
1371 arglen = *(socklen_t *)(args[1]);
1372 switch(arg->sa_family)
1375 UNIX = (struct sockaddr_un *)arg;
1376 ret = fprintf(stream, "%s", UNIX->sun_path);
1379 ipv4 = (struct sockaddr_in *)arg;
1380 ret = fprintf(stream, "%s:%i", inet_ntoa(ipv4->sin_addr), (int)ntohs(ipv4->sin_port));
1384 errno = EPFNOSUPPORT;
1390 static int formataddress_arginfo(const struct printf_info *info, size_t n, int *argtypes)
1393 argtypes[0] = PA_POINTER;
1395 argtypes[1] = PA_INT; /* Sources tell me that socklen_t _must_
1396 * be an int, so I guess this should be
1402 static int init(int hup)
1407 if(register_printf_function('N', formataddress, formataddress_arginfo))
1409 flog(LOG_CRIT, "could not register printf handler %%N: %s", strerror(errno));
1417 static void terminate(void)
1419 while(sockets != NULL)
1420 unlinksock(sockets);
1423 static struct module me =
1431 .terminate = terminate