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>
37 #ifdef HAVE_LINUX_SOCKIOS_H
38 #include <linux/sockios.h>
48 #include "sysevents.h"
50 static struct configvar myvars[] =
52 /* 0 = Direct mode, 1 = Passive mode, 2 = SOCKS proxy */
53 {CONF_VAR_INT, "mode", {.num = 0}},
54 {CONF_VAR_BOOL, "reuseaddr", {.num = 0}},
55 /* Only for direct mode */
56 {CONF_VAR_IPV4, "visibleipv4", {.ipv4 = {0}}},
57 {CONF_VAR_STRING, "publicif", {.str = L""}},
58 /* Diffserv should be supported on IPv4, too, but I don't know the
60 {CONF_VAR_INT, "diffserv-mincost", {.num = 0}},
61 {CONF_VAR_INT, "diffserv-maxrel", {.num = 0}},
62 {CONF_VAR_INT, "diffserv-maxtp", {.num = 0}},
63 {CONF_VAR_INT, "diffserv-mindelay", {.num = 0}},
67 static struct socket *sockets = NULL;
70 /* XXX: Get autoconf for all this... */
71 int getpublicaddr(int af, struct sockaddr **addr, socklen_t *lenbuf)
73 struct sockaddr_in *ipv4;
74 struct configvar *var;
78 struct ifreq *ifr, req;
83 var = confgetvar("net", "visibleipv4");
84 if(var->val.ipv4.s_addr != 0)
86 ipv4 = smalloc(sizeof(*ipv4));
87 ipv4->sin_family = AF_INET;
88 ipv4->sin_addr.s_addr = var->val.ipv4.s_addr;
89 *addr = (struct sockaddr *)ipv4;
90 *lenbuf = sizeof(*ipv4);
93 if((pif = icswcstombs(confgetstr("net", "publicif"), NULL, NULL)) == NULL)
95 flog(LOG_ERR, "could not convert net.publicif into local charset: %s", strerror(errno));
98 if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
100 conf.ifc_buf = smalloc(conf.ifc_len = 65536);
101 if(ioctl(sock, SIOCGIFCONF, &conf) < 0)
107 bufend = ((char *)conf.ifc_buf) + conf.ifc_len;
109 for(ifr = conf.ifc_ifcu.ifcu_req; (void *)ifr < bufend; ifr++)
111 memset(&req, 0, sizeof(req));
112 memcpy(req.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name));
113 if(ioctl(sock, SIOCGIFFLAGS, &req) < 0)
119 if(!(req.ifr_flags & IFF_UP))
121 if(ifr->ifr_addr.sa_family == AF_INET)
123 if(ntohl(((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr) == 0x7f000001)
127 ipv4 = smalloc(sizeof(*ipv4));
128 memcpy(ipv4, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
132 flog(LOG_WARNING, "could not locate an unambiguous interface for determining your public IP address - set net.publicif");
133 errno = ENFILE; /* XXX: There's no appropriate one for this... */
142 *addr = (struct sockaddr *)ipv4;
143 *lenbuf = sizeof(*ipv4);
149 errno = EPFNOSUPPORT;
153 static struct socket *newsock(int type)
157 new = smalloc(sizeof(*new));
160 new->isrealsocket = 1;
172 new->outbuf.s.buf = NULL;
173 new->outbuf.s.bufsize = 0;
174 new->outbuf.s.datasize = 0;
175 new->inbuf.s.buf = NULL;
176 new->inbuf.s.bufsize = 0;
177 new->inbuf.s.datasize = 0;
180 new->outbuf.d.f = new->outbuf.d.l = NULL;
181 new->inbuf.d.f = new->inbuf.d.l = NULL;
188 new->acceptcb = NULL;
198 static struct socket *mksock(int domain, int type)
203 if((fd = socket(domain, type, 0)) < 0)
205 flog(LOG_CRIT, "could not create socket: %s", strerror(errno));
210 new->family = domain;
211 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
215 struct socket *wrapsock(int fd)
219 new = newsock(SOCK_STREAM);
221 new->state = SOCK_EST;
222 new->isrealsocket = 0;
223 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
227 static void unlinksock(struct socket *sk)
230 sk->prev->next = sk->next;
232 sk->next->prev = sk->prev;
239 void getsock(struct socket *sk)
244 void putsock(struct socket *sk)
246 struct dgrambuf *buf;
248 if(--(sk->refcount) == 0)
253 if(sk->outbuf.s.buf != NULL)
254 free(sk->outbuf.s.buf);
255 if(sk->inbuf.s.buf != NULL)
256 free(sk->inbuf.s.buf);
259 while((buf = sk->outbuf.d.f) != NULL)
261 sk->outbuf.d.f = buf->next;
265 while((buf = sk->inbuf.d.f) != NULL)
267 sk->inbuf.d.f = buf->next;
275 if(sk->remote != NULL)
281 void sockpushdata(struct socket *sk, void *buf, size_t size)
286 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + size, 1, 1);
287 memmove(sk->inbuf.s.buf + size, sk->inbuf.s.buf, sk->inbuf.s.datasize);
288 memcpy(sk->inbuf.s.buf, buf, size);
289 sk->inbuf.s.datasize += size;
298 void *sockgetinbuf(struct socket *sk, size_t *size)
301 struct dgrambuf *dbuf;
306 if((sk->inbuf.s.buf == NULL) || (sk->inbuf.s.datasize == 0))
311 buf = sk->inbuf.s.buf;
312 *size = sk->inbuf.s.datasize;
313 sk->inbuf.s.buf = NULL;
314 sk->inbuf.s.bufsize = sk->inbuf.s.datasize = 0;
317 if((dbuf = sk->inbuf.d.f) == NULL)
319 sk->inbuf.d.f = dbuf->next;
320 if(dbuf->next == NULL)
321 sk->inbuf.d.l = NULL;
331 static void sockrecv(struct socket *sk)
334 struct dgrambuf *dbuf;
339 #if defined(HAVE_LINUX_SOCKIOS_H) && defined(SIOCINQ)
340 /* SIOCINQ is Linux-specific AFAIK, but I really have no idea
341 * how to read the inqueue size on other OSs */
342 if(ioctl(sk->fd, SIOCINQ, &inq))
344 /* I don't really know what could go wrong here, so let's
345 * assume it's transient. */
346 flog(LOG_WARNING, "SIOCINQ return %s on socket %i, falling back to 2048 bytes", strerror(errno), sk->fd);
354 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + inq, 1, 1);
355 ret = read(sk->fd, sk->inbuf.s.buf + sk->inbuf.s.datasize, inq);
358 if((errno == EINTR) || (errno == EAGAIN))
360 if(sk->errcb != NULL)
361 sk->errcb(sk, errno, sk->data);
367 if(sk->errcb != NULL)
368 sk->errcb(sk, 0, sk->data);
372 sk->inbuf.s.datasize += ret;
373 if(sk->readcb != NULL)
374 sk->readcb(sk, sk->data);
377 if(ioctl(sk->fd, SIOCINQ, &inq))
379 /* I don't really know what could go wrong here, so let's
380 * assume it's transient. */
381 flog(LOG_WARNING, "SIOCINQ return %s on socket %i", strerror(errno), sk->fd);
384 dbuf = smalloc(sizeof(*dbuf));
385 dbuf->data = smalloc(inq);
386 dbuf->addr = smalloc(dbuf->addrlen = sizeof(struct sockaddr_storage));
387 ret = recvfrom(sk->fd, dbuf->data, inq, 0, dbuf->addr, &dbuf->addrlen);
393 if((errno == EINTR) || (errno == EAGAIN))
395 if(sk->errcb != NULL)
396 sk->errcb(sk, errno, sk->data);
400 /* On UDP/IPv[46], ret == 0 doesn't mean EOF (since UDP can't
401 * have EOF), but rather an empty packet. I don't know if any
402 * other potential DGRAM protocols might have an EOF
403 * condition, so let's play safe. */
409 if(!((sk->family == AF_INET) || (sk->family == AF_INET6)))
411 if(sk->errcb != NULL)
412 sk->errcb(sk, 0, sk->data);
417 dbuf->addr = srealloc(dbuf->addr, dbuf->addrlen);
418 dbuf->data = srealloc(dbuf->data, dbuf->size = ret);
420 if(sk->inbuf.d.l != NULL)
421 sk->inbuf.d.l->next = dbuf;
423 sk->inbuf.d.f = dbuf;
424 sk->inbuf.d.l = dbuf;
425 if(sk->readcb != NULL)
426 sk->readcb(sk, sk->data);
431 static void sockflush(struct socket *sk)
434 struct dgrambuf *dbuf;
440 ret = send(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize, MSG_DONTWAIT | MSG_NOSIGNAL);
442 ret = write(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize);
445 /* For now, assume transient error, since
446 * the socket is polled for errors */
451 memmove(sk->outbuf.s.buf, ((char *)sk->outbuf.s.buf) + ret, sk->outbuf.s.datasize -= ret);
452 if(sk->writecb != NULL)
453 sk->writecb(sk, sk->data);
457 dbuf = sk->outbuf.d.f;
458 if((sk->outbuf.d.f = dbuf->next) == NULL)
459 sk->outbuf.d.l = NULL;
460 sendto(sk->fd, dbuf->data, dbuf->size, MSG_DONTWAIT | MSG_NOSIGNAL, dbuf->addr, dbuf->addrlen);
464 if(sk->writecb != NULL)
465 sk->writecb(sk, sk->data);
470 void closesock(struct socket *sk)
472 sk->state = SOCK_STL;
478 void sockqueue(struct socket *sk, void *data, size_t size)
480 struct dgrambuf *new;
482 if(sk->state == SOCK_STL)
487 sizebuf(&(sk->outbuf.s.buf), &(sk->outbuf.s.bufsize), sk->outbuf.s.datasize + size, 1, 1);
488 memcpy(sk->outbuf.s.buf + sk->outbuf.s.datasize, data, size);
489 sk->outbuf.s.datasize += size;
492 if(sk->remote == NULL)
494 new = smalloc(sizeof(*new));
496 memcpy(new->data = smalloc(size), data, new->size = size);
497 memcpy(new->addr = smalloc(sk->remotelen), sk->remote, new->addrlen = sk->remotelen);
498 if(sk->outbuf.d.l == NULL)
500 sk->outbuf.d.l = sk->outbuf.d.f = new;
502 sk->outbuf.d.l->next = new;
503 sk->outbuf.d.l = new;
509 size_t sockgetdatalen(struct socket *sk)
517 ret = sk->inbuf.s.datasize;
521 for(b = sk->inbuf.d.f; b != NULL; b = b->next)
528 size_t sockqueuesize(struct socket *sk)
536 ret = sk->outbuf.s.datasize;
540 for(b = sk->outbuf.d.f; b != NULL; b = b->next)
547 struct socket *netcslisten(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
552 if(confgetint("net", "mode") == 1)
557 /* I don't know if this is actually correct (it probably isn't),
558 * but since, at on least Linux systems, PF_* are specifically
559 * #define'd to their AF_* counterparts, it allows for a severely
560 * smoother implementation. If it breaks something on your
561 * platform, please tell me so.
563 if(confgetint("net", "mode") == 0)
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)
592 * The difference between netcslisten() and netcslistenlocal() is that
593 * netcslistenlocal() always listens on the local host, instead of
594 * following proxy/passive mode directions. It is suitable for eg. the
595 * UI channel, while the file sharing networks should, naturally, use
596 * netcslisten() instead.
599 struct socket *netcslistenlocal(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
604 /* I don't know if this is actually correct (it probably isn't),
605 * but since, at on least Linux systems, PF_* are specifically
606 * #define'd to their AF_* counterparts, it allows for a severely
607 * smoother implementation. If it breaks something on your
608 * platform, please tell me so.
610 if((sk = mksock(name->sa_family, type)) == NULL)
612 sk->state = SOCK_LST;
613 if(confgetint("net", "reuseaddr"))
616 setsockopt(sk->fd, SOL_SOCKET, SO_REUSEADDR, &intbuf, sizeof(intbuf));
618 if(bind(sk->fd, name, namelen) < 0)
623 if(listen(sk->fd, 16) < 0)
633 struct socket *netcsdgram(struct sockaddr *name, socklen_t namelen)
638 mode = confgetint("net", "mode");
639 if((mode == 0) || (mode == 1))
641 if((sk = mksock(name->sa_family, SOCK_DGRAM)) == NULL)
643 if(bind(sk->fd, name, namelen) < 0)
648 sk->state = SOCK_EST;
655 struct socket *netdupsock(struct socket *sk)
657 struct socket *newsk;
659 newsk = newsock(sk->type);
660 if((newsk->fd = dup(sk->fd)) < 0)
662 flog(LOG_WARNING, "could not dup() socket: %s", strerror(errno));
666 newsk->state = sk->state;
667 newsk->ignread = sk->ignread;
668 if(sk->remote != NULL)
669 memcpy(newsk->remote = smalloc(sk->remotelen), sk->remote, newsk->remotelen = sk->remotelen);
673 void netdgramconn(struct socket *sk, struct sockaddr *addr, socklen_t addrlen)
675 if(sk->remote != NULL)
677 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
681 struct socket *netcsconn(struct sockaddr *addr, socklen_t addrlen, void (*func)(struct socket *, int, void *), void *data)
686 mode = confgetint("net", "mode");
687 if((mode == 0) || (mode == 1))
689 if((sk = mksock(addr->sa_family, SOCK_STREAM)) == NULL)
691 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
692 if(!connect(sk->fd, addr, addrlen))
694 sk->state = SOCK_EST;
698 if(errno == EINPROGRESS)
700 sk->state = SOCK_SYN;
712 int pollsocks(int timeout)
714 int i, num, ret, retlen;
717 struct socket *sk, *next, *newsk;
718 struct sockaddr_storage ss;
721 pfds = smalloc(sizeof(*pfds) * (num = numsocks));
722 for(i = 0, sk = sockets; i < num; sk = sk->next)
724 if(sk->state == SOCK_STL)
732 pfds[i].events |= POLLIN;
733 if((sk->state == SOCK_SYN) || (sockqueuesize(sk) > 0))
734 pfds[i].events |= POLLOUT;
738 ret = poll(pfds, num, timeout);
743 flog(LOG_CRIT, "pollsocks: poll errored out: %s", strerror(errno));
744 /* To avoid CPU hogging in case it's bad, which it
751 for(sk = sockets; sk != NULL; sk = next)
754 for(i = 0; i < num; i++)
756 if(pfds[i].fd == sk->fd)
764 if(pfds[i].revents & POLLIN)
767 if((newfd = accept(sk->fd, (struct sockaddr *)&ss, &sslen)) < 0)
769 if(sk->errcb != NULL)
770 sk->errcb(sk, errno, sk->data);
772 newsk = newsock(sk->type);
774 newsk->family = sk->family;
775 newsk->state = SOCK_EST;
776 memcpy(newsk->remote = smalloc(sslen), &ss, sslen);
777 newsk->remotelen = sslen;
779 if(sk->acceptcb != NULL)
780 sk->acceptcb(sk, newsk, sk->data);
782 if(pfds[i].revents & POLLERR)
784 retlen = sizeof(ret);
785 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
786 if(sk->errcb != NULL)
787 sk->errcb(sk, ret, sk->data);
792 if(pfds[i].revents & POLLERR)
794 retlen = sizeof(ret);
795 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
796 if(sk->conncb != NULL)
797 sk->conncb(sk, ret, sk->data);
801 if(pfds[i].revents & (POLLIN | POLLOUT))
803 sk->state = SOCK_EST;
804 if(sk->conncb != NULL)
805 sk->conncb(sk, 0, sk->data);
809 if(pfds[i].revents & POLLERR)
811 retlen = sizeof(ret);
812 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
813 if(sk->errcb != NULL)
814 sk->errcb(sk, ret, sk->data);
818 if(pfds[i].revents & POLLIN)
820 if(pfds[i].revents & POLLOUT)
822 if(sockqueuesize(sk) > 0)
827 if(pfds[i].revents & POLLNVAL)
829 flog(LOG_CRIT, "BUG: stale socket struct on fd %i", sk->fd);
830 sk->state = SOCK_STL;
834 if(pfds[i].revents & POLLHUP)
836 if(sk->errcb != NULL)
837 sk->errcb(sk, 0, sk->data);
844 for(sk = sockets; sk != NULL; sk = next)
847 if(sk->refcount == 1 && (sockqueuesize(sk) == 0))
852 if(sk->close && (sockqueuesize(sk) == 0))
854 if(sk->state == SOCK_STL)
863 int socksettos(struct socket *sk, int tos)
867 if(sk->family == AF_INET)
874 case SOCK_TOS_MINCOST:
877 case SOCK_TOS_MAXREL:
883 case SOCK_TOS_MINDELAY:
887 flog(LOG_WARNING, "attempted to set unknown TOS value %i to IPv4 sock", tos);
890 if(setsockopt(sk->fd, SOL_IP, IP_TOS, &buf, sizeof(buf)) < 0)
892 flog(LOG_WARNING, "could not set sock TOS to %i: %s", tos, strerror(errno));
897 if(sk->family == AF_INET6)
903 case SOCK_TOS_MINCOST:
904 buf = confgetint("net", "diffserv-mincost");
906 case SOCK_TOS_MAXREL:
907 buf = confgetint("net", "diffserv-maxrel");
910 buf = confgetint("net", "diffserv-maxtp");
912 case SOCK_TOS_MINDELAY:
913 buf = confgetint("net", "diffserv-mindelay");
916 flog(LOG_WARNING, "attempted to set unknown TOS value %i to IPv4 sock", tos);
920 On Linux, the API IPv6 flow label management doesn't seem to
921 be entirely complete, so I guess this will have to wait.
923 if(setsockopt(...) < 0)
925 flog(LOG_WARNING, "could not set sock traffic class to %i: %s", tos, strerror(errno));
931 flog(LOG_WARNING, "could not set TOS on sock of family %i", sk->family);
938 void (*callback)(struct sockaddr *addr, int addrlen, void *data);
940 struct sockaddr_storage addr;
944 static void resolvecb(pid_t pid, int status, struct resolvedata *data)
948 struct sockaddr_in *ipv4;
952 if((ret = read(data->fd, buf, sizeof(buf))) != 4)
955 data->callback(NULL, 0, data->data);
957 ipv4 = (struct sockaddr_in *)&data->addr;
958 memcpy(&ipv4->sin_addr, buf, 4);
959 data->callback((struct sockaddr *)ipv4, sizeof(*ipv4), data->data);
963 data->callback(NULL, 0, data->data);
969 int netresolve(char *addr, void (*callback)(struct sockaddr *addr, int addrlen, void *data), void *data)
976 struct resolvedata *rdata;
977 struct sockaddr_in ipv4;
983 if((p = strchr(addr, ':')) != NULL)
988 ipv4.sin_family = AF_INET;
989 ipv4.sin_port = htons(port);
990 if(inet_aton(addr, &ipv4.sin_addr))
992 callback((struct sockaddr *)&ipv4, sizeof(ipv4), data);
994 sigemptyset(&sigset);
995 sigaddset(&sigset, SIGCHLD);
996 sigprocmask(SIG_BLOCK, &sigset, NULL);
997 if((pipe(pfd) < 0) || ((child = fork()) < 0))
999 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1004 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1005 for(i = 3; i < FD_SETSIZE; i++)
1010 signal(SIGALRM, SIG_DFL);
1012 if((he = gethostbyname(addr)) == NULL)
1014 write(pfd[1], he->h_addr_list[0], 4);
1018 fcntl(pfd[0], F_SETFL, fcntl(pfd[0], F_GETFL) | O_NONBLOCK);
1019 rdata = smalloc(sizeof(*rdata));
1021 rdata->callback = callback;
1023 memcpy(&rdata->addr, &ipv4, rdata->addrlen = sizeof(ipv4));
1024 childcallback(child, (void (*)(pid_t, int, void *))resolvecb, rdata);
1025 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1032 int sockgetlocalname(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
1035 struct sockaddr_storage name;
1038 if((sk->state == SOCK_STL) || (sk->fd < 0))
1041 if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0)
1043 flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetlocalname");
1046 *namebuf = memcpy(smalloc(len), &name, len);
1051 int sockgetremotename(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
1054 struct sockaddr_storage name;
1055 struct sockaddr_in *ipv4;
1056 struct sockaddr *pname;
1059 switch(confgetint("net", "mode"))
1063 if((sk->state == SOCK_STL) || (sk->fd < 0))
1066 if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0)
1068 flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetremotename");
1071 if(name.ss_family == AF_INET)
1073 ipv4 = (struct sockaddr_in *)&name;
1074 if(getpublicaddr(AF_INET, &pname, &pnamelen) < 0)
1076 flog(LOG_WARNING, "could not determine public IP address - strange things may happen");
1079 ipv4->sin_addr.s_addr = ((struct sockaddr_in *)pname)->sin_addr.s_addr;
1082 *namebuf = memcpy(smalloc(len), &name, len);
1089 flog(LOG_CRIT, "unknown net mode %i active", confgetint("net", "mode"));
1095 int addreq(struct sockaddr *x, struct sockaddr *y)
1097 struct sockaddr_un *u1, *u2;
1098 struct sockaddr_in *n1, *n2;
1100 struct sockaddr_in6 *s1, *s2;
1103 if(x->sa_family != y->sa_family)
1105 switch(x->sa_family) {
1107 u1 = (struct sockaddr_un *)x; u2 = (struct sockaddr_un *)y;
1108 if(strncmp(u1->sun_path, u2->sun_path, sizeof(u1->sun_path)))
1112 n1 = (struct sockaddr_in *)x; n2 = (struct sockaddr_in *)y;
1113 if(n1->sin_port != n2->sin_port)
1115 if(n1->sin_addr.s_addr != n2->sin_addr.s_addr)
1119 s1 = (struct sockaddr_in6 *)x; s2 = (struct sockaddr_in6 *)y;
1120 if(s1->sin6_port != s2->sin6_port)
1122 if(memcmp(s1->sin6_addr.s6_addr, s2->sin6_addr.s6_addr, sizeof(s1->sin6_addr.s6_addr)))
1129 char *formataddress(struct sockaddr *arg, socklen_t arglen)
1131 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1132 * lowercase letters to 1, so I do this
1134 struct sockaddr_in *ipv4;
1136 struct sockaddr_in6 *ipv6;
1138 static char *ret = NULL;
1144 switch(arg->sa_family)
1147 UNIX = (struct sockaddr_un *)arg;
1148 ret = sprintf2("%s", UNIX->sun_path);
1151 ipv4 = (struct sockaddr_in *)arg;
1152 if(inet_ntop(AF_INET, &ipv4->sin_addr, buf, sizeof(buf)) == NULL)
1154 ret = sprintf2("%s:%i", buf, (int)ntohs(ipv4->sin_port));
1158 ipv6 = (struct sockaddr_in6 *)arg;
1159 if(inet_ntop(AF_INET6, &ipv6->sin6_addr, buf, sizeof(buf)) == NULL)
1161 ret = sprintf2("[%s]:%i", buf, (int)ntohs(ipv6->sin6_port));
1165 errno = EPFNOSUPPORT;
1174 * It was very nice to use this, but it seems
1175 * to mess things up, so I guess it has to go... :-(
1178 static int formataddress(FILE *stream, const struct printf_info *info, const void *const *args)
1180 struct sockaddr *arg;
1182 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1183 * lowercase letters to 1, so I do this
1185 struct sockaddr_in *ipv4;
1188 arg = *(struct sockaddr **)(args[0]);
1189 arglen = *(socklen_t *)(args[1]);
1190 switch(arg->sa_family)
1193 UNIX = (struct sockaddr_un *)arg;
1194 ret = fprintf(stream, "%s", UNIX->sun_path);
1197 ipv4 = (struct sockaddr_in *)arg;
1198 ret = fprintf(stream, "%s:%i", inet_ntoa(ipv4->sin_addr), (int)ntohs(ipv4->sin_port));
1202 errno = EPFNOSUPPORT;
1208 static int formataddress_arginfo(const struct printf_info *info, size_t n, int *argtypes)
1211 argtypes[0] = PA_POINTER;
1213 argtypes[1] = PA_INT; /* Sources tell me that socklen_t _must_
1214 * be an int, so I guess this should be
1220 static int init(int hup)
1225 if(register_printf_function('N', formataddress, formataddress_arginfo))
1227 flog(LOG_CRIT, "could not register printf handler %%N: %s", strerror(errno));
1235 static void terminate(void)
1237 while(sockets != NULL)
1238 unlinksock(sockets);
1241 static struct module me =
1249 .terminate = terminate