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 = icwcstombs(confgetstr("net", "publicif"), 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));
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... */
140 *addr = (struct sockaddr *)ipv4;
141 *lenbuf = sizeof(*ipv4);
147 errno = EPFNOSUPPORT;
151 static struct socket *newsock(int type)
155 new = smalloc(sizeof(*new));
158 new->isrealsocket = 1;
170 new->outbuf.s.buf = NULL;
171 new->outbuf.s.bufsize = 0;
172 new->outbuf.s.datasize = 0;
173 new->inbuf.s.buf = NULL;
174 new->inbuf.s.bufsize = 0;
175 new->inbuf.s.datasize = 0;
178 new->outbuf.d.f = new->outbuf.d.l = NULL;
179 new->inbuf.d.f = new->inbuf.d.l = NULL;
186 new->acceptcb = NULL;
196 static struct socket *mksock(int domain, int type)
201 if((fd = socket(domain, type, 0)) < 0)
203 flog(LOG_CRIT, "could not create socket: %s", strerror(errno));
208 new->family = domain;
209 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
213 struct socket *wrapsock(int fd)
217 new = newsock(SOCK_STREAM);
219 new->state = SOCK_EST;
220 new->isrealsocket = 0;
221 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
225 static void unlinksock(struct socket *sk)
228 sk->prev->next = sk->next;
230 sk->next->prev = sk->prev;
237 void getsock(struct socket *sk)
242 void putsock(struct socket *sk)
244 struct dgrambuf *buf;
246 if(--(sk->refcount) == 0)
251 if(sk->outbuf.s.buf != NULL)
252 free(sk->outbuf.s.buf);
253 if(sk->inbuf.s.buf != NULL)
254 free(sk->inbuf.s.buf);
257 while((buf = sk->outbuf.d.f) != NULL)
259 sk->outbuf.d.f = buf->next;
263 while((buf = sk->inbuf.d.f) != NULL)
265 sk->inbuf.d.f = buf->next;
273 if(sk->remote != NULL)
279 void sockpushdata(struct socket *sk, void *buf, size_t size)
284 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + size, 1, 1);
285 memmove(sk->inbuf.s.buf + size, sk->inbuf.s.buf, sk->inbuf.s.datasize);
286 memcpy(sk->inbuf.s.buf, buf, size);
287 sk->inbuf.s.datasize += size;
296 void *sockgetinbuf(struct socket *sk, size_t *size)
299 struct dgrambuf *dbuf;
304 if((sk->inbuf.s.buf == NULL) || (sk->inbuf.s.datasize == 0))
309 buf = sk->inbuf.s.buf;
310 *size = sk->inbuf.s.datasize;
311 sk->inbuf.s.buf = NULL;
312 sk->inbuf.s.bufsize = sk->inbuf.s.datasize = 0;
315 if((dbuf = sk->inbuf.d.f) == NULL)
317 sk->inbuf.d.f = dbuf->next;
318 if(dbuf->next == NULL)
319 sk->inbuf.d.l = NULL;
329 static void sockrecv(struct socket *sk)
332 struct dgrambuf *dbuf;
337 #if defined(HAVE_LINUX_SOCKIOS_H) && defined(SIOCINQ)
338 /* SIOCINQ is Linux-specific AFAIK, but I really have no idea
339 * how to read the inqueue size on other OSs */
340 if(ioctl(sk->fd, SIOCINQ, &inq))
342 /* I don't really know what could go wrong here, so let's
343 * assume it's transient. */
344 flog(LOG_WARNING, "SIOCINQ return %s on socket %i, falling back to 2048 bytes", strerror(errno), sk->fd);
352 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + inq, 1, 1);
353 ret = read(sk->fd, sk->inbuf.s.buf + sk->inbuf.s.datasize, inq);
356 if((errno == EINTR) || (errno == EAGAIN))
358 if(sk->errcb != NULL)
359 sk->errcb(sk, errno, sk->data);
365 if(sk->errcb != NULL)
366 sk->errcb(sk, 0, sk->data);
370 sk->inbuf.s.datasize += ret;
371 if(sk->readcb != NULL)
372 sk->readcb(sk, sk->data);
375 if(ioctl(sk->fd, SIOCINQ, &inq))
377 /* I don't really know what could go wrong here, so let's
378 * assume it's transient. */
379 flog(LOG_WARNING, "SIOCINQ return %s on socket %i", strerror(errno), sk->fd);
382 dbuf = smalloc(sizeof(*dbuf));
383 dbuf->data = smalloc(inq);
384 dbuf->addr = smalloc(dbuf->addrlen = sizeof(struct sockaddr_storage));
385 ret = recvfrom(sk->fd, dbuf->data, inq, 0, dbuf->addr, &dbuf->addrlen);
391 if((errno == EINTR) || (errno == EAGAIN))
393 if(sk->errcb != NULL)
394 sk->errcb(sk, errno, sk->data);
398 /* On UDP/IPv[46], ret == 0 doesn't mean EOF (since UDP can't
399 * have EOF), but rather an empty packet. I don't know if any
400 * other potential DGRAM protocols might have an EOF
401 * condition, so let's play safe. */
407 if(!((sk->family == AF_INET) || (sk->family == AF_INET6)))
409 if(sk->errcb != NULL)
410 sk->errcb(sk, 0, sk->data);
415 dbuf->addr = srealloc(dbuf->addr, dbuf->addrlen);
416 dbuf->data = srealloc(dbuf->data, dbuf->size = ret);
418 if(sk->inbuf.d.l != NULL)
419 sk->inbuf.d.l->next = dbuf;
421 sk->inbuf.d.f = dbuf;
422 sk->inbuf.d.l = dbuf;
423 if(sk->readcb != NULL)
424 sk->readcb(sk, sk->data);
429 static void sockflush(struct socket *sk)
432 struct dgrambuf *dbuf;
438 ret = send(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize, MSG_DONTWAIT | MSG_NOSIGNAL);
440 ret = write(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize);
443 /* For now, assume transient error, since
444 * the socket is polled for errors */
449 memmove(sk->outbuf.s.buf, ((char *)sk->outbuf.s.buf) + ret, sk->outbuf.s.datasize -= ret);
450 if(sk->writecb != NULL)
451 sk->writecb(sk, sk->data);
455 dbuf = sk->outbuf.d.f;
456 if((sk->outbuf.d.f = dbuf->next) == NULL)
457 sk->outbuf.d.l = NULL;
458 sendto(sk->fd, dbuf->data, dbuf->size, MSG_DONTWAIT | MSG_NOSIGNAL, dbuf->addr, dbuf->addrlen);
462 if(sk->writecb != NULL)
463 sk->writecb(sk, sk->data);
468 void closesock(struct socket *sk)
470 sk->state = SOCK_STL;
476 void sockqueue(struct socket *sk, void *data, size_t size)
478 struct dgrambuf *new;
480 if(sk->state == SOCK_STL)
485 sizebuf(&(sk->outbuf.s.buf), &(sk->outbuf.s.bufsize), sk->outbuf.s.datasize + size, 1, 1);
486 memcpy(sk->outbuf.s.buf + sk->outbuf.s.datasize, data, size);
487 sk->outbuf.s.datasize += size;
490 if(sk->remote == NULL)
492 new = smalloc(sizeof(*new));
494 memcpy(new->data = smalloc(size), data, new->size = size);
495 memcpy(new->addr = smalloc(sk->remotelen), sk->remote, new->addrlen = sk->remotelen);
496 if(sk->outbuf.d.l == NULL)
498 sk->outbuf.d.l = sk->outbuf.d.f = new;
500 sk->outbuf.d.l->next = new;
501 sk->outbuf.d.l = new;
507 size_t sockgetdatalen(struct socket *sk)
515 ret = sk->inbuf.s.datasize;
519 for(b = sk->inbuf.d.f; b != NULL; b = b->next)
526 size_t sockqueuesize(struct socket *sk)
534 ret = sk->outbuf.s.datasize;
538 for(b = sk->outbuf.d.f; b != NULL; b = b->next)
545 struct socket *netcslisten(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
550 if(confgetint("net", "mode") == 1)
555 /* I don't know if this is actually correct (it probably isn't),
556 * but since, at on least Linux systems, PF_* are specifically
557 * #define'd to their AF_* counterparts, it allows for a severely
558 * smoother implementation. If it breaks something on your
559 * platform, please tell me so.
561 if(confgetint("net", "mode") == 0)
563 if((sk = mksock(name->sa_family, type)) == NULL)
565 sk->state = SOCK_LST;
566 if(confgetint("net", "reuseaddr"))
569 setsockopt(sk->fd, SOL_SOCKET, SO_REUSEADDR, &intbuf, sizeof(intbuf));
571 if(bind(sk->fd, name, namelen) < 0)
576 if(listen(sk->fd, 16) < 0)
590 * The difference between netcslisten() and netcslistenlocal() is that
591 * netcslistenlocal() always listens on the local host, instead of
592 * following proxy/passive mode directions. It is suitable for eg. the
593 * UI channel, while the file sharing networks should, naturally, use
594 * netcslisten() instead.
597 struct socket *netcslistenlocal(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
602 /* I don't know if this is actually correct (it probably isn't),
603 * but since, at on least Linux systems, PF_* are specifically
604 * #define'd to their AF_* counterparts, it allows for a severely
605 * smoother implementation. If it breaks something on your
606 * platform, please tell me so.
608 if((sk = mksock(name->sa_family, type)) == NULL)
610 sk->state = SOCK_LST;
611 if(confgetint("net", "reuseaddr"))
614 setsockopt(sk->fd, SOL_SOCKET, SO_REUSEADDR, &intbuf, sizeof(intbuf));
616 if(bind(sk->fd, name, namelen) < 0)
621 if(listen(sk->fd, 16) < 0)
631 struct socket *netcsdgram(struct sockaddr *name, socklen_t namelen)
636 mode = confgetint("net", "mode");
637 if((mode == 0) || (mode == 1))
639 if((sk = mksock(name->sa_family, SOCK_DGRAM)) == NULL)
641 if(bind(sk->fd, name, namelen) < 0)
646 sk->state = SOCK_EST;
653 struct socket *netdupsock(struct socket *sk)
655 struct socket *newsk;
657 newsk = newsock(sk->type);
658 if((newsk->fd = dup(sk->fd)) < 0)
660 flog(LOG_WARNING, "could not dup() socket: %s", strerror(errno));
664 newsk->state = sk->state;
665 newsk->ignread = sk->ignread;
666 if(sk->remote != NULL)
667 memcpy(newsk->remote = smalloc(sk->remotelen), sk->remote, newsk->remotelen = sk->remotelen);
671 void netdgramconn(struct socket *sk, struct sockaddr *addr, socklen_t addrlen)
673 if(sk->remote != NULL)
675 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
679 struct socket *netcsconn(struct sockaddr *addr, socklen_t addrlen, void (*func)(struct socket *, int, void *), void *data)
684 mode = confgetint("net", "mode");
685 if((mode == 0) || (mode == 1))
687 if((sk = mksock(addr->sa_family, SOCK_STREAM)) == NULL)
689 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
690 if(!connect(sk->fd, addr, addrlen))
692 sk->state = SOCK_EST;
696 if(errno == EINPROGRESS)
698 sk->state = SOCK_SYN;
710 int pollsocks(int timeout)
712 int i, num, ret, retlen;
715 struct socket *sk, *next, *newsk;
716 struct sockaddr_storage ss;
719 pfds = smalloc(sizeof(*pfds) * (num = numsocks));
720 for(i = 0, sk = sockets; i < num; sk = sk->next)
722 if(sk->state == SOCK_STL)
730 pfds[i].events |= POLLIN;
731 if((sk->state == SOCK_SYN) || (sockqueuesize(sk) > 0))
732 pfds[i].events |= POLLOUT;
736 ret = poll(pfds, num, timeout);
741 flog(LOG_CRIT, "pollsocks: poll errored out: %s", strerror(errno));
742 /* To avoid CPU hogging in case it's bad, which it
749 for(sk = sockets; sk != NULL; sk = next)
752 for(i = 0; i < num; i++)
754 if(pfds[i].fd == sk->fd)
762 if(pfds[i].revents & POLLIN)
765 if((newfd = accept(sk->fd, (struct sockaddr *)&ss, &sslen)) < 0)
767 if(sk->errcb != NULL)
768 sk->errcb(sk, errno, sk->data);
770 newsk = newsock(sk->type);
772 newsk->family = sk->family;
773 newsk->state = SOCK_EST;
774 memcpy(newsk->remote = smalloc(sslen), &ss, sslen);
775 newsk->remotelen = sslen;
777 if(sk->acceptcb != NULL)
778 sk->acceptcb(sk, newsk, sk->data);
780 if(pfds[i].revents & POLLERR)
782 retlen = sizeof(ret);
783 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
784 if(sk->errcb != NULL)
785 sk->errcb(sk, ret, sk->data);
790 if(pfds[i].revents & POLLERR)
792 retlen = sizeof(ret);
793 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
794 if(sk->conncb != NULL)
795 sk->conncb(sk, ret, sk->data);
799 if(pfds[i].revents & (POLLIN | POLLOUT))
801 sk->state = SOCK_EST;
802 if(sk->conncb != NULL)
803 sk->conncb(sk, 0, sk->data);
807 if(pfds[i].revents & POLLERR)
809 retlen = sizeof(ret);
810 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
811 if(sk->errcb != NULL)
812 sk->errcb(sk, ret, sk->data);
816 if(pfds[i].revents & POLLIN)
818 if(pfds[i].revents & POLLOUT)
820 if(sockqueuesize(sk) > 0)
825 if(pfds[i].revents & POLLNVAL)
827 flog(LOG_CRIT, "BUG: stale socket struct on fd %i", sk->fd);
828 sk->state = SOCK_STL;
832 if(pfds[i].revents & POLLHUP)
834 if(sk->errcb != NULL)
835 sk->errcb(sk, 0, sk->data);
842 for(sk = sockets; sk != NULL; sk = next)
845 if(sk->refcount == 1 && (sockqueuesize(sk) == 0))
850 if(sk->close && (sockqueuesize(sk) == 0))
852 if(sk->state == SOCK_STL)
861 int socksettos(struct socket *sk, int tos)
865 if(sk->family == AF_INET)
872 case SOCK_TOS_MINCOST:
875 case SOCK_TOS_MAXREL:
881 case SOCK_TOS_MINDELAY:
885 flog(LOG_WARNING, "attempted to set unknown TOS value %i to IPv4 sock", tos);
888 if(setsockopt(sk->fd, SOL_IP, IP_TOS, &buf, sizeof(buf)) < 0)
890 flog(LOG_WARNING, "could not set sock TOS to %i: %s", tos, strerror(errno));
895 if(sk->family == AF_INET6)
901 case SOCK_TOS_MINCOST:
902 buf = confgetint("net", "diffserv-mincost");
904 case SOCK_TOS_MAXREL:
905 buf = confgetint("net", "diffserv-maxrel");
908 buf = confgetint("net", "diffserv-maxtp");
910 case SOCK_TOS_MINDELAY:
911 buf = confgetint("net", "diffserv-mindelay");
914 flog(LOG_WARNING, "attempted to set unknown TOS value %i to IPv4 sock", tos);
918 On Linux, the API IPv6 flow label management doesn't seem to
919 be entirely complete, so I guess this will have to wait.
921 if(setsockopt(...) < 0)
923 flog(LOG_WARNING, "could not set sock traffic class to %i: %s", tos, strerror(errno));
929 flog(LOG_WARNING, "could not set TOS on sock of family %i", sk->family);
936 void (*callback)(struct sockaddr *addr, int addrlen, void *data);
938 struct sockaddr_storage addr;
942 static void resolvecb(pid_t pid, int status, struct resolvedata *data)
946 struct sockaddr_in *ipv4;
950 if((ret = read(data->fd, buf, sizeof(buf))) != 4)
953 data->callback(NULL, 0, data->data);
955 ipv4 = (struct sockaddr_in *)&data->addr;
956 memcpy(&ipv4->sin_addr, buf, 4);
957 data->callback((struct sockaddr *)ipv4, sizeof(*ipv4), data->data);
961 data->callback(NULL, 0, data->data);
967 int netresolve(char *addr, void (*callback)(struct sockaddr *addr, int addrlen, void *data), void *data)
974 struct resolvedata *rdata;
975 struct sockaddr_in ipv4;
981 if((p = strchr(addr, ':')) != NULL)
986 ipv4.sin_family = AF_INET;
987 ipv4.sin_port = htons(port);
988 if(inet_aton(addr, &ipv4.sin_addr))
990 callback((struct sockaddr *)&ipv4, sizeof(ipv4), data);
992 sigemptyset(&sigset);
993 sigaddset(&sigset, SIGCHLD);
994 sigprocmask(SIG_BLOCK, &sigset, NULL);
995 if((pipe(pfd) < 0) || ((child = fork()) < 0))
997 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1002 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1003 for(i = 3; i < FD_SETSIZE; i++)
1008 signal(SIGALRM, SIG_DFL);
1010 if((he = gethostbyname(addr)) == NULL)
1012 write(pfd[1], he->h_addr_list[0], 4);
1016 fcntl(pfd[0], F_SETFL, fcntl(pfd[0], F_GETFL) | O_NONBLOCK);
1017 rdata = smalloc(sizeof(*rdata));
1019 rdata->callback = callback;
1021 memcpy(&rdata->addr, &ipv4, rdata->addrlen = sizeof(ipv4));
1022 childcallback(child, (void (*)(pid_t, int, void *))resolvecb, rdata);
1023 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1030 int sockgetlocalname(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
1033 struct sockaddr_storage name;
1036 if((sk->state == SOCK_STL) || (sk->fd < 0))
1039 if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0)
1041 flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetlocalname");
1044 *namebuf = memcpy(smalloc(len), &name, len);
1049 int sockgetremotename(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
1052 struct sockaddr_storage name;
1053 struct sockaddr_in *ipv4;
1054 struct sockaddr *pname;
1057 switch(confgetint("net", "mode"))
1061 if((sk->state == SOCK_STL) || (sk->fd < 0))
1064 if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0)
1066 flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetremotename");
1069 if(name.ss_family == AF_INET)
1071 ipv4 = (struct sockaddr_in *)&name;
1072 if(getpublicaddr(AF_INET, &pname, &pnamelen) < 0)
1074 flog(LOG_WARNING, "could not determine public IP address - strange things may happen");
1077 ipv4->sin_addr.s_addr = ((struct sockaddr_in *)pname)->sin_addr.s_addr;
1080 *namebuf = memcpy(smalloc(len), &name, len);
1087 flog(LOG_CRIT, "unknown net mode %i active", confgetint("net", "mode"));
1093 char *formataddress(struct sockaddr *arg, socklen_t arglen)
1095 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1096 * lowercase letters to 1, so I do this
1098 struct sockaddr_in *ipv4;
1100 struct sockaddr_in6 *ipv6;
1102 static char *ret = NULL;
1108 switch(arg->sa_family)
1111 UNIX = (struct sockaddr_un *)arg;
1112 ret = sprintf2("%s", UNIX->sun_path);
1115 ipv4 = (struct sockaddr_in *)arg;
1116 if(inet_ntop(AF_INET, &ipv4->sin_addr, buf, sizeof(buf)) == NULL)
1118 ret = sprintf2("%s:%i", buf, (int)ntohs(ipv4->sin_port));
1122 ipv6 = (struct sockaddr_in6 *)arg;
1123 if(inet_ntop(AF_INET6, &ipv6->sin6_addr, buf, sizeof(buf)) == NULL)
1125 ret = sprintf2("%s:%i", buf, (int)ntohs(ipv6->sin6_port));
1129 errno = EPFNOSUPPORT;
1138 * It was very nice to use this, but it seems
1139 * to mess things up, so I guess it has to go... :-(
1142 static int formataddress(FILE *stream, const struct printf_info *info, const void *const *args)
1144 struct sockaddr *arg;
1146 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1147 * lowercase letters to 1, so I do this
1149 struct sockaddr_in *ipv4;
1152 arg = *(struct sockaddr **)(args[0]);
1153 arglen = *(socklen_t *)(args[1]);
1154 switch(arg->sa_family)
1157 UNIX = (struct sockaddr_un *)arg;
1158 ret = fprintf(stream, "%s", UNIX->sun_path);
1161 ipv4 = (struct sockaddr_in *)arg;
1162 ret = fprintf(stream, "%s:%i", inet_ntoa(ipv4->sin_addr), (int)ntohs(ipv4->sin_port));
1166 errno = EPFNOSUPPORT;
1172 static int formataddress_arginfo(const struct printf_info *info, size_t n, int *argtypes)
1175 argtypes[0] = PA_POINTER;
1177 argtypes[1] = PA_INT; /* Sources tell me that socklen_t _must_
1178 * be an int, so I guess this should be
1184 static int init(int hup)
1189 if(register_printf_function('N', formataddress, formataddress_arginfo))
1191 flog(LOG_CRIT, "could not register printf handler %%N: %s", strerror(errno));
1199 static void terminate(void)
1201 while(sockets != NULL)
1202 unlinksock(sockets);
1205 static struct module me =
1213 .terminate = terminate