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;
186 memset(&new->ucred, 0, sizeof(new->ucred));
190 new->outbuf.s.buf = NULL;
191 new->outbuf.s.bufsize = 0;
192 new->outbuf.s.datasize = 0;
193 new->inbuf.s.buf = NULL;
194 new->inbuf.s.bufsize = 0;
195 new->inbuf.s.datasize = 0;
198 new->outbuf.d.f = new->outbuf.d.l = NULL;
199 new->inbuf.d.f = new->inbuf.d.l = NULL;
206 new->acceptcb = NULL;
216 static struct socket *mksock(int domain, int type)
221 if((fd = socket(domain, type, 0)) < 0)
223 flog(LOG_CRIT, "could not create socket: %s", strerror(errno));
228 new->family = domain;
229 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
233 struct socket *wrapsock(int fd)
237 new = newsock(SOCK_STREAM);
239 new->state = SOCK_EST;
240 new->isrealsocket = 0;
241 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
245 static void unlinksock(struct socket *sk)
248 sk->prev->next = sk->next;
250 sk->next->prev = sk->prev;
257 void getsock(struct socket *sk)
262 void putsock(struct socket *sk)
264 struct dgrambuf *buf;
266 if(--(sk->refcount) == 0)
271 if(sk->outbuf.s.buf != NULL)
272 free(sk->outbuf.s.buf);
273 if(sk->inbuf.s.buf != NULL)
274 free(sk->inbuf.s.buf);
277 while((buf = sk->outbuf.d.f) != NULL)
279 sk->outbuf.d.f = buf->next;
284 while((buf = sk->inbuf.d.f) != NULL)
286 sk->inbuf.d.f = buf->next;
294 if(sk->remote != NULL)
300 void sockpushdata(struct socket *sk, void *buf, size_t size)
305 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + size, 1, 1);
306 memmove(sk->inbuf.s.buf + size, sk->inbuf.s.buf, sk->inbuf.s.datasize);
307 memcpy(sk->inbuf.s.buf, buf, size);
308 sk->inbuf.s.datasize += size;
317 void *sockgetinbuf(struct socket *sk, size_t *size)
320 struct dgrambuf *dbuf;
325 if((sk->inbuf.s.buf == NULL) || (sk->inbuf.s.datasize == 0))
330 buf = sk->inbuf.s.buf;
331 *size = sk->inbuf.s.datasize;
332 sk->inbuf.s.buf = NULL;
333 sk->inbuf.s.bufsize = sk->inbuf.s.datasize = 0;
336 if((dbuf = sk->inbuf.d.f) == NULL)
338 sk->inbuf.d.f = dbuf->next;
339 if(dbuf->next == NULL)
340 sk->inbuf.d.l = NULL;
350 static void recvcmsg(struct socket *sk, struct msghdr *msg)
352 struct cmsghdr *cmsg;
355 for(cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL; cmsg = CMSG_NXTHDR(msg, cmsg))
357 if((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SCM_CREDENTIALS))
359 if(sk->ucred.pid == 0)
361 cred = (struct ucred *)CMSG_DATA(cmsg);
362 memcpy(&sk->ucred, cred, sizeof(*cred));
368 static void sockrecv(struct socket *sk)
371 struct dgrambuf *dbuf;
376 memset(&msg, 0, sizeof(msg));
377 msg.msg_iov = &bufvec;
379 msg.msg_control = cbuf;
380 msg.msg_controllen = sizeof(cbuf);
384 #if defined(HAVE_LINUX_SOCKIOS_H) && defined(SIOCINQ)
385 /* SIOCINQ is Linux-specific AFAIK, but I really have no idea
386 * how to read the inqueue size on other OSs */
387 if(ioctl(sk->fd, SIOCINQ, &inq))
389 /* I don't really know what could go wrong here, so let's
390 * assume it's transient. */
391 flog(LOG_WARNING, "SIOCINQ return %s on socket %i, falling back to 2048 bytes", strerror(errno), sk->fd);
399 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + inq, 1, 1);
402 bufvec.iov_base = sk->inbuf.s.buf + sk->inbuf.s.datasize;
403 bufvec.iov_len = inq;
404 ret = recvmsg(sk->fd, &msg, 0);
406 ret = read(sk->fd, sk->inbuf.s.buf + sk->inbuf.s.datasize, inq);
407 msg.msg_controllen = 0;
412 if((errno == EINTR) || (errno == EAGAIN))
414 if(sk->errcb != NULL)
415 sk->errcb(sk, errno, sk->data);
419 if(msg.msg_flags & MSG_CTRUNC)
420 flog(LOG_DEBUG, "ancillary data was truncated");
425 if(sk->errcb != NULL)
426 sk->errcb(sk, 0, sk->data);
430 sk->inbuf.s.datasize += ret;
431 if(sk->readcb != NULL)
432 sk->readcb(sk, sk->data);
435 #if defined(HAVE_LINUX_SOCKIOS_H) && defined(SIOCINQ)
436 if(ioctl(sk->fd, SIOCINQ, &inq))
438 /* I don't really know what could go wrong here, so let's
439 * assume it's transient. */
440 flog(LOG_WARNING, "SIOCINQ return %s on socket %i", strerror(errno), sk->fd);
446 dbuf = smalloc(sizeof(*dbuf));
447 dbuf->data = smalloc(inq);
448 dbuf->addr = smalloc(dbuf->addrlen = sizeof(struct sockaddr_storage));
450 ret = recvfrom(sk->fd, dbuf->data, inq, 0, dbuf->addr, &dbuf->addrlen);
452 msg.msg_name = dbuf->addr;
453 msg.msg_namelen = dbuf->addrlen;
454 bufvec.iov_base = dbuf->data;
455 bufvec.iov_len = inq;
456 ret = recvmsg(sk->fd, &msg, 0);
457 dbuf->addrlen = msg.msg_namelen;
463 if((errno == EINTR) || (errno == EAGAIN))
465 if(sk->errcb != NULL)
466 sk->errcb(sk, errno, sk->data);
470 if(msg.msg_flags & MSG_CTRUNC)
471 flog(LOG_DEBUG, "ancillary data was truncated");
474 /* On UDP/IPv[46], ret == 0 doesn't mean EOF (since UDP can't
475 * have EOF), but rather an empty packet. I don't know if any
476 * other potential DGRAM protocols might have an EOF
477 * condition, so let's play safe. */
483 if(!((sk->family == AF_INET) || (sk->family == AF_INET6)))
485 if(sk->errcb != NULL)
486 sk->errcb(sk, 0, sk->data);
491 dbuf->addr = srealloc(dbuf->addr, dbuf->addrlen);
492 dbuf->data = srealloc(dbuf->data, dbuf->size = ret);
494 if(sk->inbuf.d.l != NULL)
495 sk->inbuf.d.l->next = dbuf;
497 sk->inbuf.d.f = dbuf;
498 sk->inbuf.d.l = dbuf;
499 if(sk->readcb != NULL)
500 sk->readcb(sk, sk->data);
505 static void sockflush(struct socket *sk)
508 struct dgrambuf *dbuf;
514 ret = send(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize, MSG_DONTWAIT | MSG_NOSIGNAL);
516 ret = write(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize);
519 /* For now, assume transient error, since
520 * the socket is polled for errors */
525 memmove(sk->outbuf.s.buf, ((char *)sk->outbuf.s.buf) + ret, sk->outbuf.s.datasize -= ret);
526 if(sk->writecb != NULL)
527 sk->writecb(sk, sk->data);
531 dbuf = sk->outbuf.d.f;
532 if((sk->outbuf.d.f = dbuf->next) == NULL)
533 sk->outbuf.d.l = NULL;
534 sendto(sk->fd, dbuf->data, dbuf->size, MSG_DONTWAIT | MSG_NOSIGNAL, dbuf->addr, dbuf->addrlen);
538 if(sk->writecb != NULL)
539 sk->writecb(sk, sk->data);
544 void closesock(struct socket *sk)
546 struct sockaddr_un *un;
548 if((sk->family == AF_UNIX) && !sockgetlocalname(sk, (struct sockaddr **)&un, NULL) && (un->sun_family == PF_UNIX))
550 if((sk->state == SOCK_LST) && strchr(un->sun_path, '/'))
552 if(unlink(un->sun_path))
553 flog(LOG_WARNING, "could not unlink Unix socket %s: %s", un->sun_path, strerror(errno));
556 sk->state = SOCK_STL;
562 void sockqueue(struct socket *sk, void *data, size_t size)
564 struct dgrambuf *new;
566 if(sk->state == SOCK_STL)
571 sizebuf(&(sk->outbuf.s.buf), &(sk->outbuf.s.bufsize), sk->outbuf.s.datasize + size, 1, 1);
572 memcpy(sk->outbuf.s.buf + sk->outbuf.s.datasize, data, size);
573 sk->outbuf.s.datasize += size;
576 if(sk->remote == NULL)
578 new = smalloc(sizeof(*new));
580 memcpy(new->data = smalloc(size), data, new->size = size);
581 memcpy(new->addr = smalloc(sk->remotelen), sk->remote, new->addrlen = sk->remotelen);
582 if(sk->outbuf.d.l == NULL)
584 sk->outbuf.d.l = sk->outbuf.d.f = new;
586 sk->outbuf.d.l->next = new;
587 sk->outbuf.d.l = new;
593 size_t sockgetdatalen(struct socket *sk)
601 ret = sk->inbuf.s.datasize;
605 for(b = sk->inbuf.d.f; b != NULL; b = b->next)
612 size_t sockqueuesize(struct socket *sk)
620 ret = sk->outbuf.s.datasize;
624 for(b = sk->outbuf.d.f; b != NULL; b = b->next)
632 * Seriously, I don't know if it's naughty or not to remove
633 * pre-existing Unix sockets.
635 static int rebindunix(struct socket *sk, struct sockaddr *name, socklen_t namelen)
637 struct sockaddr_un *un;
640 if((sk->family != AF_UNIX) || (name->sa_family != PF_UNIX))
642 un = (struct sockaddr_un *)name;
643 if(stat(un->sun_path, &sb))
645 if(!S_ISSOCK(sb.st_mode))
647 if(unlink(un->sun_path))
649 if(bind(sk->fd, name, namelen) < 0)
655 * The difference between netcslisten() and netcslistenlocal() is that
656 * netcslistenlocal() always listens on the local host, instead of
657 * following proxy/passive mode directions. It is suitable for eg. the
658 * UI channel, while the file sharing networks should, naturally, use
659 * netcslisten() instead.
662 struct socket *netcslistenlocal(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
667 /* I don't know if this is actually correct (it probably isn't),
668 * but since, at on least Linux systems, PF_* are specifically
669 * #define'd to their AF_* counterparts, it allows for a severely
670 * smoother implementation. If it breaks something on your
671 * platform, please tell me so.
673 if((sk = mksock(name->sa_family, type)) == NULL)
675 sk->state = SOCK_LST;
676 if(confgetint("net", "reuseaddr"))
679 setsockopt(sk->fd, SOL_SOCKET, SO_REUSEADDR, &intbuf, sizeof(intbuf));
681 if((bind(sk->fd, name, namelen) < 0) && ((errno != EADDRINUSE) || (rebindunix(sk, name, namelen) < 0)))
686 if(listen(sk->fd, 16) < 0)
696 struct socket *netcslisten(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
698 if(confgetint("net", "mode") == 1)
703 if(confgetint("net", "mode") == 0)
704 return(netcslistenlocal(type, name, namelen, func, data));
709 struct socket *netcstcplisten(int port, int local, void (*func)(struct socket *, struct socket *, void *), void *data)
711 struct sockaddr_in addr;
713 struct sockaddr_in6 addr6;
715 struct socket *(*csfunc)(int, struct sockaddr *, socklen_t, void (*)(struct socket *, struct socket *, void *), void *);
719 csfunc = netcslistenlocal;
721 csfunc = netcslisten;
723 memset(&addr6, 0, sizeof(addr6));
724 addr6.sin6_family = AF_INET6;
725 addr6.sin6_port = htons(port);
726 addr6.sin6_addr = in6addr_any;
727 if((ret = csfunc(SOCK_STREAM, (struct sockaddr *)&addr6, sizeof(addr6), func, data)) != NULL)
729 if((ret == NULL) && (errno != EAFNOSUPPORT))
732 memset(&addr, 0, sizeof(addr));
733 addr.sin_family = AF_INET;
734 addr.sin_port = htons(port);
735 return(csfunc(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), func, data));
738 struct socket *netcsdgram(struct sockaddr *name, socklen_t namelen)
743 mode = confgetint("net", "mode");
744 if((mode == 0) || (mode == 1))
746 if((sk = mksock(name->sa_family, SOCK_DGRAM)) == NULL)
748 if(bind(sk->fd, name, namelen) < 0)
753 sk->state = SOCK_EST;
760 struct socket *netdupsock(struct socket *sk)
762 struct socket *newsk;
764 newsk = newsock(sk->type);
765 if((newsk->fd = dup(sk->fd)) < 0)
767 flog(LOG_WARNING, "could not dup() socket: %s", strerror(errno));
771 newsk->state = sk->state;
772 newsk->ignread = sk->ignread;
773 if(sk->remote != NULL)
774 memcpy(newsk->remote = smalloc(sk->remotelen), sk->remote, newsk->remotelen = sk->remotelen);
778 void netdgramconn(struct socket *sk, struct sockaddr *addr, socklen_t addrlen)
780 if(sk->remote != NULL)
782 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
786 struct socket *netcsconn(struct sockaddr *addr, socklen_t addrlen, void (*func)(struct socket *, int, void *), void *data)
791 mode = confgetint("net", "mode");
792 if((mode == 0) || (mode == 1))
794 if((sk = mksock(addr->sa_family, SOCK_STREAM)) == NULL)
796 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
797 if(!connect(sk->fd, addr, addrlen))
799 sk->state = SOCK_EST;
803 if(errno == EINPROGRESS)
805 sk->state = SOCK_SYN;
817 static void acceptunix(struct socket *sk)
822 if(setsockopt(sk->fd, SOL_SOCKET, SO_PASSCRED, &buf, sizeof(buf)) < 0)
823 flog(LOG_WARNING, "could not enable SO_PASSCRED on Unix socket %i: %s", sk->fd, strerror(errno));
826 int pollsocks(int timeout)
832 struct socket *sk, *next, *newsk;
833 struct sockaddr_storage ss;
836 pfds = smalloc(sizeof(*pfds) * (num = numsocks));
837 for(i = 0, sk = sockets; i < num; sk = sk->next)
839 if(sk->state == SOCK_STL)
847 pfds[i].events |= POLLIN;
848 if((sk->state == SOCK_SYN) || (sockqueuesize(sk) > 0))
849 pfds[i].events |= POLLOUT;
853 ret = poll(pfds, num, timeout);
858 flog(LOG_CRIT, "pollsocks: poll errored out: %s", strerror(errno));
859 /* To avoid CPU hogging in case it's bad, which it
866 for(sk = sockets; sk != NULL; sk = next)
869 for(i = 0; i < num; i++)
871 if(pfds[i].fd == sk->fd)
879 if(pfds[i].revents & POLLIN)
882 if((newfd = accept(sk->fd, (struct sockaddr *)&ss, &sslen)) < 0)
884 if(sk->errcb != NULL)
885 sk->errcb(sk, errno, sk->data);
887 newsk = newsock(sk->type);
889 newsk->family = sk->family;
890 newsk->state = SOCK_EST;
891 memcpy(newsk->remote = smalloc(sslen), &ss, sslen);
892 newsk->remotelen = sslen;
893 if(ss.ss_family == PF_UNIX)
895 if(sk->acceptcb != NULL)
896 sk->acceptcb(sk, newsk, sk->data);
899 if(pfds[i].revents & POLLERR)
901 retlen = sizeof(ret);
902 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
903 if(sk->errcb != NULL)
904 sk->errcb(sk, ret, sk->data);
909 if(pfds[i].revents & POLLERR)
911 retlen = sizeof(ret);
912 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
913 if(sk->conncb != NULL)
914 sk->conncb(sk, ret, sk->data);
918 if(pfds[i].revents & (POLLIN | POLLOUT))
920 sk->state = SOCK_EST;
921 if(sk->conncb != NULL)
922 sk->conncb(sk, 0, sk->data);
926 if(pfds[i].revents & POLLERR)
928 retlen = sizeof(ret);
929 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
930 if(sk->errcb != NULL)
931 sk->errcb(sk, ret, sk->data);
935 if(pfds[i].revents & POLLIN)
937 if(pfds[i].revents & POLLOUT)
939 if(sockqueuesize(sk) > 0)
944 if(pfds[i].revents & POLLNVAL)
946 flog(LOG_CRIT, "BUG: stale socket struct on fd %i", sk->fd);
947 sk->state = SOCK_STL;
951 if(pfds[i].revents & POLLHUP)
953 if(sk->errcb != NULL)
954 sk->errcb(sk, 0, sk->data);
961 for(sk = sockets; sk != NULL; sk = next)
964 if(sk->refcount == 1 && (sockqueuesize(sk) == 0))
969 if(sk->close && (sockqueuesize(sk) == 0))
971 if(sk->state == SOCK_STL)
980 int socksettos(struct socket *sk, int tos)
984 if(sk->family == AF_UNIX)
985 return(0); /* Unix sockets are always perfect. :) */
986 if(sk->family == AF_INET)
993 case SOCK_TOS_MINCOST:
996 case SOCK_TOS_MAXREL:
1002 case SOCK_TOS_MINDELAY:
1006 flog(LOG_WARNING, "attempted to set unknown TOS value %i to IPv4 sock", tos);
1009 if(setsockopt(sk->fd, SOL_IP, IP_TOS, &buf, sizeof(buf)) < 0)
1011 flog(LOG_WARNING, "could not set sock TOS to %i: %s", tos, strerror(errno));
1016 if(sk->family == AF_INET6)
1022 case SOCK_TOS_MINCOST:
1023 buf = confgetint("net", "diffserv-mincost");
1025 case SOCK_TOS_MAXREL:
1026 buf = confgetint("net", "diffserv-maxrel");
1028 case SOCK_TOS_MAXTP:
1029 buf = confgetint("net", "diffserv-maxtp");
1031 case SOCK_TOS_MINDELAY:
1032 buf = confgetint("net", "diffserv-mindelay");
1035 flog(LOG_WARNING, "attempted to set unknown TOS value %i to IPv4 sock", tos);
1039 On Linux, the API IPv6 flow label management doesn't seem to
1040 be entirely complete, so I guess this will have to wait.
1042 if(setsockopt(...) < 0)
1044 flog(LOG_WARNING, "could not set sock traffic class to %i: %s", tos, strerror(errno));
1050 flog(LOG_WARNING, "could not set TOS on sock of family %i", sk->family);
1057 void (*callback)(struct sockaddr *addr, int addrlen, void *data);
1059 struct sockaddr_storage addr;
1063 static void resolvecb(pid_t pid, int status, struct resolvedata *data)
1065 static char buf[80];
1067 struct sockaddr_in *ipv4;
1071 if((ret = read(data->fd, buf, sizeof(buf))) != 4)
1074 data->callback(NULL, 0, data->data);
1076 ipv4 = (struct sockaddr_in *)&data->addr;
1077 memcpy(&ipv4->sin_addr, buf, 4);
1078 data->callback((struct sockaddr *)ipv4, sizeof(*ipv4), data->data);
1082 data->callback(NULL, 0, data->data);
1088 int netresolve(char *addr, void (*callback)(struct sockaddr *addr, int addrlen, void *data), void *data)
1095 struct resolvedata *rdata;
1096 struct sockaddr_in ipv4;
1102 if((p = strchr(addr, ':')) != NULL)
1107 ipv4.sin_family = AF_INET;
1108 ipv4.sin_port = htons(port);
1109 if(inet_aton(addr, &ipv4.sin_addr))
1111 callback((struct sockaddr *)&ipv4, sizeof(ipv4), data);
1113 sigemptyset(&sigset);
1114 sigaddset(&sigset, SIGCHLD);
1115 sigprocmask(SIG_BLOCK, &sigset, NULL);
1116 if((pipe(pfd) < 0) || ((child = fork()) < 0))
1118 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1123 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1124 for(i = 3; i < FD_SETSIZE; i++)
1129 signal(SIGALRM, SIG_DFL);
1131 if((he = gethostbyname(addr)) == NULL)
1133 write(pfd[1], he->h_addr_list[0], 4);
1137 fcntl(pfd[0], F_SETFL, fcntl(pfd[0], F_GETFL) | O_NONBLOCK);
1138 rdata = smalloc(sizeof(*rdata));
1140 rdata->callback = callback;
1142 memcpy(&rdata->addr, &ipv4, rdata->addrlen = sizeof(ipv4));
1143 childcallback(child, (void (*)(pid_t, int, void *))resolvecb, rdata);
1144 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1151 int sockgetlocalname(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
1154 struct sockaddr_storage name;
1157 if((sk->state == SOCK_STL) || (sk->fd < 0))
1160 if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0)
1162 flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetlocalname (%s)", strerror(errno));
1165 *namebuf = memcpy(smalloc(len), &name, len);
1171 static void sethostaddr(struct sockaddr *dst, struct sockaddr *src)
1173 if(dst->sa_family != src->sa_family)
1175 flog(LOG_ERR, "BUG: non-matching socket families in sethostaddr (%i -> %i)", src->sa_family, dst->sa_family);
1178 switch(src->sa_family)
1181 ((struct sockaddr_in *)dst)->sin_addr = ((struct sockaddr_in *)src)->sin_addr;
1184 ((struct sockaddr_in6 *)dst)->sin6_addr = ((struct sockaddr_in6 *)src)->sin6_addr;
1187 flog(LOG_WARNING, "sethostaddr unimplemented for family %i", src->sa_family);
1192 static int makepublic(struct sockaddr *addr)
1196 struct sockaddr *pname;
1198 if((ret = getpublicaddr(addr->sa_family, &pname, &plen)) < 0)
1200 flog(LOG_ERR, "could not get public address: %s", strerror(errno));
1205 sethostaddr(addr, pname);
1210 int sockgetremotename(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
1213 struct sockaddr *name;
1215 switch(confgetint("net", "mode"))
1219 if((sk->state == SOCK_STL) || (sk->fd < 0))
1224 if(!sockgetlocalname(sk, &name, &len))
1231 flog(LOG_ERR, "could not get remotely accessible name by any means");
1237 flog(LOG_CRIT, "unknown net mode %i active", confgetint("net", "mode"));
1243 int sockgetremotename2(struct socket *sk, struct socket *sk2, struct sockaddr **namebuf, socklen_t *lenbuf)
1245 struct sockaddr *name1, *name2;
1246 socklen_t len1, len2;
1248 if(sk->family != sk2->family)
1250 flog(LOG_ERR, "using sockgetremotename2 with sockets of differing family: %i %i", sk->family, sk2->family);
1253 if(sockgetremotename(sk, &name1, &len1))
1255 if(sockgetremotename(sk2, &name2, &len2)) {
1259 sethostaddr(name1, name2);
1266 int addreq(struct sockaddr *x, struct sockaddr *y)
1268 struct sockaddr_un *u1, *u2;
1269 struct sockaddr_in *n1, *n2;
1271 struct sockaddr_in6 *s1, *s2;
1274 if(x->sa_family != y->sa_family)
1276 switch(x->sa_family) {
1278 u1 = (struct sockaddr_un *)x; u2 = (struct sockaddr_un *)y;
1279 if(strncmp(u1->sun_path, u2->sun_path, sizeof(u1->sun_path)))
1283 n1 = (struct sockaddr_in *)x; n2 = (struct sockaddr_in *)y;
1284 if(n1->sin_port != n2->sin_port)
1286 if(n1->sin_addr.s_addr != n2->sin_addr.s_addr)
1291 s1 = (struct sockaddr_in6 *)x; s2 = (struct sockaddr_in6 *)y;
1292 if(s1->sin6_port != s2->sin6_port)
1294 if(memcmp(s1->sin6_addr.s6_addr, s2->sin6_addr.s6_addr, sizeof(s1->sin6_addr.s6_addr)))
1302 char *formataddress(struct sockaddr *arg, socklen_t arglen)
1304 struct sockaddr_in *ipv4;
1306 struct sockaddr_in6 *ipv6;
1308 static char *ret = NULL;
1314 switch(arg->sa_family)
1317 ret = sstrdup("Unix socket");
1320 ipv4 = (struct sockaddr_in *)arg;
1321 if(inet_ntop(AF_INET, &ipv4->sin_addr, buf, sizeof(buf)) == NULL)
1323 ret = sprintf2("%s:%i", buf, (int)ntohs(ipv4->sin_port));
1327 ipv6 = (struct sockaddr_in6 *)arg;
1328 if(inet_ntop(AF_INET6, &ipv6->sin6_addr, buf, sizeof(buf)) == NULL)
1330 ret = sprintf2("[%s]:%i", buf, (int)ntohs(ipv6->sin6_port));
1334 errno = EPFNOSUPPORT;
1343 * It was very nice to use this, but it seems
1344 * to mess things up, so I guess it has to go... :-(
1347 static int formataddress(FILE *stream, const struct printf_info *info, const void *const *args)
1349 struct sockaddr *arg;
1351 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1352 * lowercase letters to 1, so I do this
1354 struct sockaddr_in *ipv4;
1357 arg = *(struct sockaddr **)(args[0]);
1358 arglen = *(socklen_t *)(args[1]);
1359 switch(arg->sa_family)
1362 UNIX = (struct sockaddr_un *)arg;
1363 ret = fprintf(stream, "%s", UNIX->sun_path);
1366 ipv4 = (struct sockaddr_in *)arg;
1367 ret = fprintf(stream, "%s:%i", inet_ntoa(ipv4->sin_addr), (int)ntohs(ipv4->sin_port));
1371 errno = EPFNOSUPPORT;
1377 static int formataddress_arginfo(const struct printf_info *info, size_t n, int *argtypes)
1380 argtypes[0] = PA_POINTER;
1382 argtypes[1] = PA_INT; /* Sources tell me that socklen_t _must_
1383 * be an int, so I guess this should be
1389 static int init(int hup)
1394 if(register_printf_function('N', formataddress, formataddress_arginfo))
1396 flog(LOG_CRIT, "could not register printf handler %%N: %s", strerror(errno));
1404 static void terminate(void)
1406 while(sockets != NULL)
1407 unlinksock(sockets);
1410 static struct module me =
1418 .terminate = terminate