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 #ifdef HAVE_LINUX_SOCKIOS_H
37 #include <linux/sockios.h>
47 #include "sysevents.h"
49 static struct configvar myvars[] =
51 /* 0 = Direct mode, 1 = Passive mode, 2 = SOCKS proxy */
52 {CONF_VAR_INT, "mode", {.num = 0}},
53 {CONF_VAR_BOOL, "reuseaddr", {.num = 0}},
54 /* Only for direct mode */
55 {CONF_VAR_IPV4, "visibleipv4", {.ipv4 = {0}}},
56 {CONF_VAR_STRING, "publicif", {.str = L""}},
57 /* Diffserv should be supported on IPv4, too, but I don't know the
59 {CONF_VAR_INT, "diffserv-mincost", {.num = 0}},
60 {CONF_VAR_INT, "diffserv-maxrel", {.num = 0}},
61 {CONF_VAR_INT, "diffserv-maxtp", {.num = 0}},
62 {CONF_VAR_INT, "diffserv-mindelay", {.num = 0}},
66 static struct socket *sockets = NULL;
69 /* XXX: Get autoconf for all this... */
70 int getpublicaddr(int af, struct sockaddr **addr, socklen_t *lenbuf)
72 struct sockaddr_in *ipv4;
73 struct configvar *var;
77 struct ifreq *ifr, req;
82 var = confgetvar("net", "visibleipv4");
83 if(var->val.ipv4.s_addr != 0)
85 ipv4 = smalloc(sizeof(*ipv4));
86 ipv4->sin_family = AF_INET;
87 ipv4->sin_addr.s_addr = var->val.ipv4.s_addr;
88 *addr = (struct sockaddr *)ipv4;
89 *lenbuf = sizeof(*ipv4);
92 if((pif = icswcstombs(confgetstr("net", "publicif"), NULL, NULL)) == NULL)
94 flog(LOG_ERR, "could not convert net.publicif into local charset: %s", strerror(errno));
97 if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
99 conf.ifc_buf = smalloc(conf.ifc_len = 65536);
100 if(ioctl(sock, SIOCGIFCONF, &conf) < 0)
106 bufend = ((char *)conf.ifc_buf) + conf.ifc_len;
108 for(ifr = conf.ifc_ifcu.ifcu_req; (void *)ifr < bufend; ifr++)
110 memset(&req, 0, sizeof(req));
111 memcpy(req.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name));
112 if(ioctl(sock, SIOCGIFFLAGS, &req) < 0)
118 if(!(req.ifr_flags & IFF_UP))
120 if(ifr->ifr_addr.sa_family == AF_INET)
122 if(ntohl(((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr) == 0x7f000001)
126 ipv4 = smalloc(sizeof(*ipv4));
127 memcpy(ipv4, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
131 flog(LOG_WARNING, "could not locate an unambiguous interface for determining your public IP address - set net.publicif");
132 errno = ENFILE; /* XXX: There's no appropriate one for this... */
141 *addr = (struct sockaddr *)ipv4;
142 *lenbuf = sizeof(*ipv4);
148 errno = EPFNOSUPPORT;
152 static struct socket *newsock(int type)
156 new = smalloc(sizeof(*new));
159 new->isrealsocket = 1;
171 new->outbuf.s.buf = NULL;
172 new->outbuf.s.bufsize = 0;
173 new->outbuf.s.datasize = 0;
174 new->inbuf.s.buf = NULL;
175 new->inbuf.s.bufsize = 0;
176 new->inbuf.s.datasize = 0;
179 new->outbuf.d.f = new->outbuf.d.l = NULL;
180 new->inbuf.d.f = new->inbuf.d.l = NULL;
187 new->acceptcb = NULL;
197 static struct socket *mksock(int domain, int type)
202 if((fd = socket(domain, type, 0)) < 0)
204 flog(LOG_CRIT, "could not create socket: %s", strerror(errno));
209 new->family = domain;
210 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
214 struct socket *wrapsock(int fd)
218 new = newsock(SOCK_STREAM);
220 new->state = SOCK_EST;
221 new->isrealsocket = 0;
222 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
226 static void unlinksock(struct socket *sk)
229 sk->prev->next = sk->next;
231 sk->next->prev = sk->prev;
238 void getsock(struct socket *sk)
243 void putsock(struct socket *sk)
245 struct dgrambuf *buf;
247 if(--(sk->refcount) == 0)
252 if(sk->outbuf.s.buf != NULL)
253 free(sk->outbuf.s.buf);
254 if(sk->inbuf.s.buf != NULL)
255 free(sk->inbuf.s.buf);
258 while((buf = sk->outbuf.d.f) != NULL)
260 sk->outbuf.d.f = buf->next;
264 while((buf = sk->inbuf.d.f) != NULL)
266 sk->inbuf.d.f = buf->next;
274 if(sk->remote != NULL)
280 void sockpushdata(struct socket *sk, void *buf, size_t size)
285 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + size, 1, 1);
286 memmove(sk->inbuf.s.buf + size, sk->inbuf.s.buf, sk->inbuf.s.datasize);
287 memcpy(sk->inbuf.s.buf, buf, size);
288 sk->inbuf.s.datasize += size;
297 void *sockgetinbuf(struct socket *sk, size_t *size)
300 struct dgrambuf *dbuf;
305 if((sk->inbuf.s.buf == NULL) || (sk->inbuf.s.datasize == 0))
310 buf = sk->inbuf.s.buf;
311 *size = sk->inbuf.s.datasize;
312 sk->inbuf.s.buf = NULL;
313 sk->inbuf.s.bufsize = sk->inbuf.s.datasize = 0;
316 if((dbuf = sk->inbuf.d.f) == NULL)
318 sk->inbuf.d.f = dbuf->next;
319 if(dbuf->next == NULL)
320 sk->inbuf.d.l = NULL;
330 static void sockrecv(struct socket *sk)
333 struct dgrambuf *dbuf;
338 #if defined(HAVE_LINUX_SOCKIOS_H) && defined(SIOCINQ)
339 /* SIOCINQ is Linux-specific AFAIK, but I really have no idea
340 * how to read the inqueue size on other OSs */
341 if(ioctl(sk->fd, SIOCINQ, &inq))
343 /* I don't really know what could go wrong here, so let's
344 * assume it's transient. */
345 flog(LOG_WARNING, "SIOCINQ return %s on socket %i, falling back to 2048 bytes", strerror(errno), sk->fd);
353 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + inq, 1, 1);
354 ret = read(sk->fd, sk->inbuf.s.buf + sk->inbuf.s.datasize, inq);
357 if((errno == EINTR) || (errno == EAGAIN))
359 if(sk->errcb != NULL)
360 sk->errcb(sk, errno, sk->data);
366 if(sk->errcb != NULL)
367 sk->errcb(sk, 0, sk->data);
371 sk->inbuf.s.datasize += ret;
372 if(sk->readcb != NULL)
373 sk->readcb(sk, sk->data);
376 if(ioctl(sk->fd, SIOCINQ, &inq))
378 /* I don't really know what could go wrong here, so let's
379 * assume it's transient. */
380 flog(LOG_WARNING, "SIOCINQ return %s on socket %i", strerror(errno), sk->fd);
383 dbuf = smalloc(sizeof(*dbuf));
384 dbuf->data = smalloc(inq);
385 dbuf->addr = smalloc(dbuf->addrlen = sizeof(struct sockaddr_storage));
386 ret = recvfrom(sk->fd, dbuf->data, inq, 0, dbuf->addr, &dbuf->addrlen);
392 if((errno == EINTR) || (errno == EAGAIN))
394 if(sk->errcb != NULL)
395 sk->errcb(sk, errno, sk->data);
399 /* On UDP/IPv[46], ret == 0 doesn't mean EOF (since UDP can't
400 * have EOF), but rather an empty packet. I don't know if any
401 * other potential DGRAM protocols might have an EOF
402 * condition, so let's play safe. */
408 if(!((sk->family == AF_INET) || (sk->family == AF_INET6)))
410 if(sk->errcb != NULL)
411 sk->errcb(sk, 0, sk->data);
416 dbuf->addr = srealloc(dbuf->addr, dbuf->addrlen);
417 dbuf->data = srealloc(dbuf->data, dbuf->size = ret);
419 if(sk->inbuf.d.l != NULL)
420 sk->inbuf.d.l->next = dbuf;
422 sk->inbuf.d.f = dbuf;
423 sk->inbuf.d.l = dbuf;
424 if(sk->readcb != NULL)
425 sk->readcb(sk, sk->data);
430 static void sockflush(struct socket *sk)
433 struct dgrambuf *dbuf;
439 ret = send(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize, MSG_DONTWAIT | MSG_NOSIGNAL);
441 ret = write(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize);
444 /* For now, assume transient error, since
445 * the socket is polled for errors */
450 memmove(sk->outbuf.s.buf, ((char *)sk->outbuf.s.buf) + ret, sk->outbuf.s.datasize -= ret);
451 if(sk->writecb != NULL)
452 sk->writecb(sk, sk->data);
456 dbuf = sk->outbuf.d.f;
457 if((sk->outbuf.d.f = dbuf->next) == NULL)
458 sk->outbuf.d.l = NULL;
459 sendto(sk->fd, dbuf->data, dbuf->size, MSG_DONTWAIT | MSG_NOSIGNAL, dbuf->addr, dbuf->addrlen);
463 if(sk->writecb != NULL)
464 sk->writecb(sk, sk->data);
469 void closesock(struct socket *sk)
471 sk->state = SOCK_STL;
477 void sockqueue(struct socket *sk, void *data, size_t size)
479 struct dgrambuf *new;
481 if(sk->state == SOCK_STL)
486 sizebuf(&(sk->outbuf.s.buf), &(sk->outbuf.s.bufsize), sk->outbuf.s.datasize + size, 1, 1);
487 memcpy(sk->outbuf.s.buf + sk->outbuf.s.datasize, data, size);
488 sk->outbuf.s.datasize += size;
491 if(sk->remote == NULL)
493 new = smalloc(sizeof(*new));
495 memcpy(new->data = smalloc(size), data, new->size = size);
496 memcpy(new->addr = smalloc(sk->remotelen), sk->remote, new->addrlen = sk->remotelen);
497 if(sk->outbuf.d.l == NULL)
499 sk->outbuf.d.l = sk->outbuf.d.f = new;
501 sk->outbuf.d.l->next = new;
502 sk->outbuf.d.l = new;
508 size_t sockgetdatalen(struct socket *sk)
516 ret = sk->inbuf.s.datasize;
520 for(b = sk->inbuf.d.f; b != NULL; b = b->next)
527 size_t sockqueuesize(struct socket *sk)
535 ret = sk->outbuf.s.datasize;
539 for(b = sk->outbuf.d.f; b != NULL; b = b->next)
547 * The difference between netcslisten() and netcslistenlocal() is that
548 * netcslistenlocal() always listens on the local host, instead of
549 * following proxy/passive mode directions. It is suitable for eg. the
550 * UI channel, while the file sharing networks should, naturally, use
551 * netcslisten() instead.
554 struct socket *netcslistenlocal(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
559 /* I don't know if this is actually correct (it probably isn't),
560 * but since, at on least Linux systems, PF_* are specifically
561 * #define'd to their AF_* counterparts, it allows for a severely
562 * smoother implementation. If it breaks something on your
563 * platform, please tell me so.
565 if((sk = mksock(name->sa_family, type)) == NULL)
567 sk->state = SOCK_LST;
568 if(confgetint("net", "reuseaddr"))
571 setsockopt(sk->fd, SOL_SOCKET, SO_REUSEADDR, &intbuf, sizeof(intbuf));
573 if(bind(sk->fd, name, namelen) < 0)
578 if(listen(sk->fd, 16) < 0)
588 struct socket *netcslisten(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
590 if(confgetint("net", "mode") == 1)
595 if(confgetint("net", "mode") == 0)
596 return(netcslistenlocal(type, name, namelen, func, data));
601 struct socket *netcstcplisten(int port, int local, void (*func)(struct socket *, struct socket *, void *), void *data)
603 struct sockaddr_in addr;
605 struct sockaddr_in6 addr6;
607 struct socket *(*csfunc)(int, struct sockaddr *, socklen_t, void (*)(struct socket *, struct socket *, void *), void *);
611 csfunc = netcslistenlocal;
613 csfunc = netcslisten;
615 memset(&addr6, 0, sizeof(addr6));
616 addr6.sin6_family = AF_INET6;
617 addr6.sin6_port = htons(port);
618 addr6.sin6_addr = in6addr_any;
619 if((ret = csfunc(SOCK_STREAM, (struct sockaddr *)&addr6, sizeof(addr6), func, data)) != NULL)
621 if((ret == NULL) && (errno != EAFNOSUPPORT))
624 memset(&addr, 0, sizeof(addr));
625 addr.sin_family = AF_INET;
626 addr.sin_port = htons(port);
627 return(csfunc(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), func, data));
630 struct socket *netcsdgram(struct sockaddr *name, socklen_t namelen)
635 mode = confgetint("net", "mode");
636 if((mode == 0) || (mode == 1))
638 if((sk = mksock(name->sa_family, SOCK_DGRAM)) == NULL)
640 if(bind(sk->fd, name, namelen) < 0)
645 sk->state = SOCK_EST;
652 struct socket *netdupsock(struct socket *sk)
654 struct socket *newsk;
656 newsk = newsock(sk->type);
657 if((newsk->fd = dup(sk->fd)) < 0)
659 flog(LOG_WARNING, "could not dup() socket: %s", strerror(errno));
663 newsk->state = sk->state;
664 newsk->ignread = sk->ignread;
665 if(sk->remote != NULL)
666 memcpy(newsk->remote = smalloc(sk->remotelen), sk->remote, newsk->remotelen = sk->remotelen);
670 void netdgramconn(struct socket *sk, struct sockaddr *addr, socklen_t addrlen)
672 if(sk->remote != NULL)
674 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
678 struct socket *netcsconn(struct sockaddr *addr, socklen_t addrlen, void (*func)(struct socket *, int, void *), void *data)
683 mode = confgetint("net", "mode");
684 if((mode == 0) || (mode == 1))
686 if((sk = mksock(addr->sa_family, SOCK_STREAM)) == NULL)
688 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
689 if(!connect(sk->fd, addr, addrlen))
691 sk->state = SOCK_EST;
695 if(errno == EINPROGRESS)
697 sk->state = SOCK_SYN;
709 int pollsocks(int timeout)
711 int i, num, ret, retlen;
714 struct socket *sk, *next, *newsk;
715 struct sockaddr_storage ss;
718 pfds = smalloc(sizeof(*pfds) * (num = numsocks));
719 for(i = 0, sk = sockets; i < num; sk = sk->next)
721 if(sk->state == SOCK_STL)
729 pfds[i].events |= POLLIN;
730 if((sk->state == SOCK_SYN) || (sockqueuesize(sk) > 0))
731 pfds[i].events |= POLLOUT;
735 ret = poll(pfds, num, timeout);
740 flog(LOG_CRIT, "pollsocks: poll errored out: %s", strerror(errno));
741 /* To avoid CPU hogging in case it's bad, which it
748 for(sk = sockets; sk != NULL; sk = next)
751 for(i = 0; i < num; i++)
753 if(pfds[i].fd == sk->fd)
761 if(pfds[i].revents & POLLIN)
764 if((newfd = accept(sk->fd, (struct sockaddr *)&ss, &sslen)) < 0)
766 if(sk->errcb != NULL)
767 sk->errcb(sk, errno, sk->data);
769 newsk = newsock(sk->type);
771 newsk->family = sk->family;
772 newsk->state = SOCK_EST;
773 memcpy(newsk->remote = smalloc(sslen), &ss, sslen);
774 newsk->remotelen = sslen;
776 if(sk->acceptcb != NULL)
777 sk->acceptcb(sk, newsk, sk->data);
779 if(pfds[i].revents & POLLERR)
781 retlen = sizeof(ret);
782 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
783 if(sk->errcb != NULL)
784 sk->errcb(sk, ret, sk->data);
789 if(pfds[i].revents & POLLERR)
791 retlen = sizeof(ret);
792 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
793 if(sk->conncb != NULL)
794 sk->conncb(sk, ret, sk->data);
798 if(pfds[i].revents & (POLLIN | POLLOUT))
800 sk->state = SOCK_EST;
801 if(sk->conncb != NULL)
802 sk->conncb(sk, 0, sk->data);
806 if(pfds[i].revents & POLLERR)
808 retlen = sizeof(ret);
809 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
810 if(sk->errcb != NULL)
811 sk->errcb(sk, ret, sk->data);
815 if(pfds[i].revents & POLLIN)
817 if(pfds[i].revents & POLLOUT)
819 if(sockqueuesize(sk) > 0)
824 if(pfds[i].revents & POLLNVAL)
826 flog(LOG_CRIT, "BUG: stale socket struct on fd %i", sk->fd);
827 sk->state = SOCK_STL;
831 if(pfds[i].revents & POLLHUP)
833 if(sk->errcb != NULL)
834 sk->errcb(sk, 0, sk->data);
841 for(sk = sockets; sk != NULL; sk = next)
844 if(sk->refcount == 1 && (sockqueuesize(sk) == 0))
849 if(sk->close && (sockqueuesize(sk) == 0))
851 if(sk->state == SOCK_STL)
860 int socksettos(struct socket *sk, int tos)
864 if(sk->family == AF_INET)
871 case SOCK_TOS_MINCOST:
874 case SOCK_TOS_MAXREL:
880 case SOCK_TOS_MINDELAY:
884 flog(LOG_WARNING, "attempted to set unknown TOS value %i to IPv4 sock", tos);
887 if(setsockopt(sk->fd, SOL_IP, IP_TOS, &buf, sizeof(buf)) < 0)
889 flog(LOG_WARNING, "could not set sock TOS to %i: %s", tos, strerror(errno));
894 if(sk->family == AF_INET6)
900 case SOCK_TOS_MINCOST:
901 buf = confgetint("net", "diffserv-mincost");
903 case SOCK_TOS_MAXREL:
904 buf = confgetint("net", "diffserv-maxrel");
907 buf = confgetint("net", "diffserv-maxtp");
909 case SOCK_TOS_MINDELAY:
910 buf = confgetint("net", "diffserv-mindelay");
913 flog(LOG_WARNING, "attempted to set unknown TOS value %i to IPv4 sock", tos);
917 On Linux, the API IPv6 flow label management doesn't seem to
918 be entirely complete, so I guess this will have to wait.
920 if(setsockopt(...) < 0)
922 flog(LOG_WARNING, "could not set sock traffic class to %i: %s", tos, strerror(errno));
928 flog(LOG_WARNING, "could not set TOS on sock of family %i", sk->family);
935 void (*callback)(struct sockaddr *addr, int addrlen, void *data);
937 struct sockaddr_storage addr;
941 static void resolvecb(pid_t pid, int status, struct resolvedata *data)
945 struct sockaddr_in *ipv4;
949 if((ret = read(data->fd, buf, sizeof(buf))) != 4)
952 data->callback(NULL, 0, data->data);
954 ipv4 = (struct sockaddr_in *)&data->addr;
955 memcpy(&ipv4->sin_addr, buf, 4);
956 data->callback((struct sockaddr *)ipv4, sizeof(*ipv4), data->data);
960 data->callback(NULL, 0, data->data);
966 int netresolve(char *addr, void (*callback)(struct sockaddr *addr, int addrlen, void *data), void *data)
973 struct resolvedata *rdata;
974 struct sockaddr_in ipv4;
980 if((p = strchr(addr, ':')) != NULL)
985 ipv4.sin_family = AF_INET;
986 ipv4.sin_port = htons(port);
987 if(inet_aton(addr, &ipv4.sin_addr))
989 callback((struct sockaddr *)&ipv4, sizeof(ipv4), data);
991 sigemptyset(&sigset);
992 sigaddset(&sigset, SIGCHLD);
993 sigprocmask(SIG_BLOCK, &sigset, NULL);
994 if((pipe(pfd) < 0) || ((child = fork()) < 0))
996 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1001 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1002 for(i = 3; i < FD_SETSIZE; i++)
1007 signal(SIGALRM, SIG_DFL);
1009 if((he = gethostbyname(addr)) == NULL)
1011 write(pfd[1], he->h_addr_list[0], 4);
1015 fcntl(pfd[0], F_SETFL, fcntl(pfd[0], F_GETFL) | O_NONBLOCK);
1016 rdata = smalloc(sizeof(*rdata));
1018 rdata->callback = callback;
1020 memcpy(&rdata->addr, &ipv4, rdata->addrlen = sizeof(ipv4));
1021 childcallback(child, (void (*)(pid_t, int, void *))resolvecb, rdata);
1022 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1029 int sockgetlocalname(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
1032 struct sockaddr_storage name;
1035 if((sk->state == SOCK_STL) || (sk->fd < 0))
1038 if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0)
1040 flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetlocalname");
1043 *namebuf = memcpy(smalloc(len), &name, len);
1048 int sockgetremotename(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
1051 struct sockaddr_storage name;
1052 struct sockaddr_in *ipv4;
1053 struct sockaddr *pname;
1056 switch(confgetint("net", "mode"))
1060 if((sk->state == SOCK_STL) || (sk->fd < 0))
1063 if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0)
1065 flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetremotename");
1068 if(name.ss_family == AF_INET)
1070 ipv4 = (struct sockaddr_in *)&name;
1071 if(getpublicaddr(AF_INET, &pname, &pnamelen) < 0)
1073 flog(LOG_WARNING, "could not determine public IP address - strange things may happen");
1076 ipv4->sin_addr.s_addr = ((struct sockaddr_in *)pname)->sin_addr.s_addr;
1079 *namebuf = memcpy(smalloc(len), &name, len);
1086 flog(LOG_CRIT, "unknown net mode %i active", confgetint("net", "mode"));
1092 int addreq(struct sockaddr *x, struct sockaddr *y)
1094 struct sockaddr_un *u1, *u2;
1095 struct sockaddr_in *n1, *n2;
1097 struct sockaddr_in6 *s1, *s2;
1100 if(x->sa_family != y->sa_family)
1102 switch(x->sa_family) {
1104 u1 = (struct sockaddr_un *)x; u2 = (struct sockaddr_un *)y;
1105 if(strncmp(u1->sun_path, u2->sun_path, sizeof(u1->sun_path)))
1109 n1 = (struct sockaddr_in *)x; n2 = (struct sockaddr_in *)y;
1110 if(n1->sin_port != n2->sin_port)
1112 if(n1->sin_addr.s_addr != n2->sin_addr.s_addr)
1116 s1 = (struct sockaddr_in6 *)x; s2 = (struct sockaddr_in6 *)y;
1117 if(s1->sin6_port != s2->sin6_port)
1119 if(memcmp(s1->sin6_addr.s6_addr, s2->sin6_addr.s6_addr, sizeof(s1->sin6_addr.s6_addr)))
1126 char *formataddress(struct sockaddr *arg, socklen_t arglen)
1128 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1129 * lowercase letters to 1, so I do this
1131 struct sockaddr_in *ipv4;
1133 struct sockaddr_in6 *ipv6;
1135 static char *ret = NULL;
1141 switch(arg->sa_family)
1144 UNIX = (struct sockaddr_un *)arg;
1145 ret = sprintf2("%s", UNIX->sun_path);
1148 ipv4 = (struct sockaddr_in *)arg;
1149 if(inet_ntop(AF_INET, &ipv4->sin_addr, buf, sizeof(buf)) == NULL)
1151 ret = sprintf2("%s:%i", buf, (int)ntohs(ipv4->sin_port));
1155 ipv6 = (struct sockaddr_in6 *)arg;
1156 if(inet_ntop(AF_INET6, &ipv6->sin6_addr, buf, sizeof(buf)) == NULL)
1158 ret = sprintf2("[%s]:%i", buf, (int)ntohs(ipv6->sin6_port));
1162 errno = EPFNOSUPPORT;
1171 * It was very nice to use this, but it seems
1172 * to mess things up, so I guess it has to go... :-(
1175 static int formataddress(FILE *stream, const struct printf_info *info, const void *const *args)
1177 struct sockaddr *arg;
1179 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1180 * lowercase letters to 1, so I do this
1182 struct sockaddr_in *ipv4;
1185 arg = *(struct sockaddr **)(args[0]);
1186 arglen = *(socklen_t *)(args[1]);
1187 switch(arg->sa_family)
1190 UNIX = (struct sockaddr_un *)arg;
1191 ret = fprintf(stream, "%s", UNIX->sun_path);
1194 ipv4 = (struct sockaddr_in *)arg;
1195 ret = fprintf(stream, "%s:%i", inet_ntoa(ipv4->sin_addr), (int)ntohs(ipv4->sin_port));
1199 errno = EPFNOSUPPORT;
1205 static int formataddress_arginfo(const struct printf_info *info, size_t n, int *argtypes)
1208 argtypes[0] = PA_POINTER;
1210 argtypes[1] = PA_INT; /* Sources tell me that socklen_t _must_
1211 * be an int, so I guess this should be
1217 static int init(int hup)
1222 if(register_printf_function('N', formataddress, formataddress_arginfo))
1224 flog(LOG_CRIT, "could not register printf handler %%N: %s", strerror(errno));
1232 static void terminate(void)
1234 while(sockets != NULL)
1235 unlinksock(sockets);
1238 static struct module me =
1246 .terminate = terminate