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""}},
61 static struct socket *sockets = NULL;
64 /* XXX: Get autoconf for all this... */
65 int getpublicaddr(int af, struct sockaddr **addr, socklen_t *lenbuf)
67 struct sockaddr_in *ipv4;
68 struct configvar *var;
72 struct ifreq *ifr, req;
77 var = confgetvar("net", "visibleipv4");
78 if(var->val.ipv4.s_addr != 0)
80 ipv4 = smalloc(sizeof(*ipv4));
81 ipv4->sin_family = AF_INET;
82 ipv4->sin_addr.s_addr = var->val.ipv4.s_addr;
83 *addr = (struct sockaddr *)ipv4;
84 *lenbuf = sizeof(*ipv4);
87 if((pif = icwcstombs(confgetstr("net", "publicif"), NULL)) == NULL)
89 flog(LOG_ERR, "could not convert net.publicif into local charset: %s", strerror(errno));
92 if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
94 conf.ifc_buf = smalloc(conf.ifc_len = 65536);
95 if(ioctl(sock, SIOCGIFCONF, &conf) < 0)
101 bufend = ((char *)conf.ifc_buf) + conf.ifc_len;
103 for(ifr = conf.ifc_ifcu.ifcu_req; (void *)ifr < bufend; ifr++)
105 memset(&req, 0, sizeof(req));
106 memcpy(req.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name));
107 if(ioctl(sock, SIOCGIFFLAGS, &req) < 0)
113 if(!(req.ifr_flags & IFF_UP))
115 if(ifr->ifr_addr.sa_family == AF_INET)
117 if(ntohl(((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr) == 0x7f000001)
121 ipv4 = smalloc(sizeof(*ipv4));
122 memcpy(ipv4, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
125 flog(LOG_WARNING, "could not locate an unambiguous interface for determining your public IP address - set net.publicif");
126 errno = ENFILE; /* XXX: There's no appropriate one for this... */
134 *addr = (struct sockaddr *)ipv4;
135 *lenbuf = sizeof(*ipv4);
141 errno = EPFNOSUPPORT;
145 static struct socket *newsock(int type)
149 new = smalloc(sizeof(*new));
152 new->isrealsocket = 1;
164 new->outbuf.s.buf = NULL;
165 new->outbuf.s.bufsize = 0;
166 new->outbuf.s.datasize = 0;
167 new->inbuf.s.buf = NULL;
168 new->inbuf.s.bufsize = 0;
169 new->inbuf.s.datasize = 0;
172 new->outbuf.d.f = new->outbuf.d.l = NULL;
173 new->inbuf.d.f = new->inbuf.d.l = NULL;
180 new->acceptcb = NULL;
190 static struct socket *mksock(int domain, int type)
195 if((fd = socket(domain, type, 0)) < 0)
197 flog(LOG_CRIT, "could not create socket: %s", strerror(errno));
202 new->family = domain;
203 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
207 struct socket *wrapsock(int fd)
211 new = newsock(SOCK_STREAM);
213 new->state = SOCK_EST;
214 new->isrealsocket = 0;
215 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
219 static void unlinksock(struct socket *sk)
222 sk->prev->next = sk->next;
224 sk->next->prev = sk->prev;
231 void getsock(struct socket *sk)
236 void putsock(struct socket *sk)
238 struct dgrambuf *buf;
240 if(--(sk->refcount) == 0)
245 if(sk->outbuf.s.buf != NULL)
246 free(sk->outbuf.s.buf);
247 if(sk->inbuf.s.buf != NULL)
248 free(sk->inbuf.s.buf);
251 while((buf = sk->outbuf.d.f) != NULL)
253 sk->outbuf.d.f = buf->next;
257 while((buf = sk->inbuf.d.f) != NULL)
259 sk->inbuf.d.f = buf->next;
267 if(sk->remote != NULL)
273 void sockpushdata(struct socket *sk, void *buf, size_t size)
278 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + size, 1, 1);
279 memmove(sk->inbuf.s.buf + size, sk->inbuf.s.buf, sk->inbuf.s.datasize);
280 memcpy(sk->inbuf.s.buf, buf, size);
281 sk->inbuf.s.datasize += size;
290 void *sockgetinbuf(struct socket *sk, size_t *size)
293 struct dgrambuf *dbuf;
298 if((sk->inbuf.s.buf == NULL) || (sk->inbuf.s.datasize == 0))
303 buf = sk->inbuf.s.buf;
304 *size = sk->inbuf.s.datasize;
305 sk->inbuf.s.buf = NULL;
306 sk->inbuf.s.bufsize = sk->inbuf.s.datasize = 0;
309 if((dbuf = sk->inbuf.d.f) == NULL)
311 sk->inbuf.d.f = dbuf->next;
312 if(dbuf->next == NULL)
313 sk->inbuf.d.l = NULL;
323 static void sockrecv(struct socket *sk)
326 struct dgrambuf *dbuf;
331 #if defined(HAVE_LINUX_SOCKIOS_H) && defined(SIOCINQ)
332 /* SIOCINQ is Linux-specific AFAIK, but I really have no idea
333 * how to read the inqueue size on other OSs */
334 if(ioctl(sk->fd, SIOCINQ, &inq))
336 /* I don't really know what could go wrong here, so let's
337 * assume it's transient. */
338 flog(LOG_WARNING, "SIOCINQ return %s on socket %i, falling back to 2048 bytes", strerror(errno), sk->fd);
346 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + inq, 1, 1);
347 ret = read(sk->fd, sk->inbuf.s.buf + sk->inbuf.s.datasize, inq);
350 if((errno == EINTR) || (errno == EAGAIN))
352 if(sk->errcb != NULL)
353 sk->errcb(sk, errno, sk->data);
359 if(sk->errcb != NULL)
360 sk->errcb(sk, 0, sk->data);
364 sk->inbuf.s.datasize += ret;
365 if(sk->readcb != NULL)
366 sk->readcb(sk, sk->data);
369 if(ioctl(sk->fd, SIOCINQ, &inq))
371 /* I don't really know what could go wrong here, so let's
372 * assume it's transient. */
373 flog(LOG_WARNING, "SIOCINQ return %s on socket %i", strerror(errno), sk->fd);
376 dbuf = smalloc(sizeof(*dbuf));
377 dbuf->data = smalloc(inq);
378 dbuf->addr = smalloc(dbuf->addrlen = sizeof(struct sockaddr_storage));
379 ret = recvfrom(sk->fd, dbuf->data, inq, 0, dbuf->addr, &dbuf->addrlen);
385 if((errno == EINTR) || (errno == EAGAIN))
387 if(sk->errcb != NULL)
388 sk->errcb(sk, errno, sk->data);
392 /* On UDP/IPv[46], ret == 0 doesn't mean EOF (since UDP can't
393 * have EOF), but rather an empty packet. I don't know if any
394 * other potential DGRAM protocols might have an EOF
395 * condition, so let's play safe. */
401 if(!((sk->family == AF_INET) || (sk->family == AF_INET6)))
403 if(sk->errcb != NULL)
404 sk->errcb(sk, 0, sk->data);
409 dbuf->addr = srealloc(dbuf->addr, dbuf->addrlen);
410 dbuf->data = srealloc(dbuf->data, dbuf->size = ret);
412 if(sk->inbuf.d.l != NULL)
413 sk->inbuf.d.l->next = dbuf;
415 sk->inbuf.d.f = dbuf;
416 sk->inbuf.d.l = dbuf;
417 if(sk->readcb != NULL)
418 sk->readcb(sk, sk->data);
423 static void sockflush(struct socket *sk)
426 struct dgrambuf *dbuf;
432 ret = send(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize, MSG_DONTWAIT | MSG_NOSIGNAL);
434 ret = write(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize);
437 /* For now, assume transient error, since
438 * the socket is polled for errors */
443 memmove(sk->outbuf.s.buf, ((char *)sk->outbuf.s.buf) + ret, sk->outbuf.s.datasize -= ret);
444 if(sk->writecb != NULL)
445 sk->writecb(sk, sk->data);
449 dbuf = sk->outbuf.d.f;
450 if((sk->outbuf.d.f = dbuf->next) == NULL)
451 sk->outbuf.d.l = NULL;
452 sendto(sk->fd, dbuf->data, dbuf->size, MSG_DONTWAIT | MSG_NOSIGNAL, dbuf->addr, dbuf->addrlen);
456 if(sk->writecb != NULL)
457 sk->writecb(sk, sk->data);
462 void closesock(struct socket *sk)
464 sk->state = SOCK_STL;
470 void sockqueue(struct socket *sk, void *data, size_t size)
472 struct dgrambuf *new;
474 if(sk->state == SOCK_STL)
479 sizebuf(&(sk->outbuf.s.buf), &(sk->outbuf.s.bufsize), sk->outbuf.s.datasize + size, 1, 1);
480 memcpy(sk->outbuf.s.buf + sk->outbuf.s.datasize, data, size);
481 sk->outbuf.s.datasize += size;
484 if(sk->remote == NULL)
486 new = smalloc(sizeof(*new));
488 memcpy(new->data = smalloc(size), data, new->size = size);
489 memcpy(new->addr = smalloc(sk->remotelen), sk->remote, new->addrlen = sk->remotelen);
490 if(sk->outbuf.d.l == NULL)
492 sk->outbuf.d.l = sk->outbuf.d.f = new;
494 sk->outbuf.d.l->next = new;
495 sk->outbuf.d.l = new;
501 size_t sockgetdatalen(struct socket *sk)
509 ret = sk->inbuf.s.datasize;
513 for(b = sk->inbuf.d.f; b != NULL; b = b->next)
520 size_t sockqueuesize(struct socket *sk)
528 ret = sk->outbuf.s.datasize;
532 for(b = sk->outbuf.d.f; b != NULL; b = b->next)
539 struct socket *netcslisten(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
544 if(confgetint("net", "mode") == 1)
549 /* I don't know if this is actually correct (it probably isn't),
550 * but since, at on least Linux systems, PF_* are specifically
551 * #define'd to their AF_* counterparts, it allows for a severely
552 * smoother implementation. If it breaks something on your
553 * platform, please tell me so.
555 if(confgetint("net", "mode") == 0)
557 if((sk = mksock(name->sa_family, type)) == NULL)
559 sk->state = SOCK_LST;
560 if(confgetint("net", "reuseaddr"))
563 setsockopt(sk->fd, SOL_SOCKET, SO_REUSEADDR, &intbuf, sizeof(intbuf));
565 if(bind(sk->fd, name, namelen) < 0)
570 if(listen(sk->fd, 16) < 0)
584 * The difference between netcslisten() and netcslistenlocal() is that
585 * netcslistenlocal() always listens on the local host, instead of
586 * following proxy/passive mode directions. It is suitable for eg. the
587 * UI channel, while the file sharing networks should, naturally, use
588 * netcslisten() instead.
591 struct socket *netcslistenlocal(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
596 /* I don't know if this is actually correct (it probably isn't),
597 * but since, at on least Linux systems, PF_* are specifically
598 * #define'd to their AF_* counterparts, it allows for a severely
599 * smoother implementation. If it breaks something on your
600 * platform, please tell me so.
602 if((sk = mksock(name->sa_family, type)) == NULL)
604 sk->state = SOCK_LST;
605 if(confgetint("net", "reuseaddr"))
608 setsockopt(sk->fd, SOL_SOCKET, SO_REUSEADDR, &intbuf, sizeof(intbuf));
610 if(bind(sk->fd, name, namelen) < 0)
615 if(listen(sk->fd, 16) < 0)
625 struct socket *netcsdgram(struct sockaddr *name, socklen_t namelen)
630 mode = confgetint("net", "mode");
631 if((mode == 0) || (mode == 1))
633 if((sk = mksock(name->sa_family, SOCK_DGRAM)) == NULL)
635 if(bind(sk->fd, name, namelen) < 0)
640 sk->state = SOCK_EST;
647 struct socket *netdupsock(struct socket *sk)
649 struct socket *newsk;
651 newsk = newsock(sk->type);
652 if((newsk->fd = dup(sk->fd)) < 0)
654 flog(LOG_WARNING, "could not dup() socket: %s", strerror(errno));
658 newsk->state = sk->state;
659 newsk->ignread = sk->ignread;
660 if(sk->remote != NULL)
661 memcpy(newsk->remote = smalloc(sk->remotelen), sk->remote, newsk->remotelen = sk->remotelen);
665 void netdgramconn(struct socket *sk, struct sockaddr *addr, socklen_t addrlen)
667 if(sk->remote != NULL)
669 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
673 struct socket *netcsconn(struct sockaddr *addr, socklen_t addrlen, void (*func)(struct socket *, int, void *), void *data)
678 mode = confgetint("net", "mode");
679 if((mode == 0) || (mode == 1))
681 if((sk = mksock(addr->sa_family, SOCK_STREAM)) == NULL)
683 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
684 if(!connect(sk->fd, addr, addrlen))
686 sk->state = SOCK_EST;
690 if(errno == EINPROGRESS)
692 sk->state = SOCK_SYN;
704 int pollsocks(int timeout)
706 int i, num, ret, retlen;
709 struct socket *sk, *next, *newsk;
710 struct sockaddr_storage ss;
713 pfds = smalloc(sizeof(*pfds) * (num = numsocks));
714 for(i = 0, sk = sockets; i < num; sk = sk->next)
716 if(sk->state == SOCK_STL)
724 pfds[i].events |= POLLIN;
725 if((sk->state == SOCK_SYN) || (sockqueuesize(sk) > 0))
726 pfds[i].events |= POLLOUT;
730 ret = poll(pfds, num, timeout);
735 flog(LOG_CRIT, "pollsocks: poll errored out: %s", strerror(errno));
736 /* To avoid CPU hogging in case it's bad, which it
743 for(sk = sockets; sk != NULL; sk = next)
746 for(i = 0; i < num; i++)
748 if(pfds[i].fd == sk->fd)
756 if(pfds[i].revents & POLLIN)
759 if((newfd = accept(sk->fd, (struct sockaddr *)&ss, &sslen)) < 0)
761 if(sk->errcb != NULL)
762 sk->errcb(sk, errno, sk->data);
764 newsk = newsock(sk->type);
766 newsk->family = sk->family;
767 newsk->state = SOCK_EST;
768 memcpy(newsk->remote = smalloc(sslen), &ss, sslen);
769 newsk->remotelen = sslen;
771 if(sk->acceptcb != NULL)
772 sk->acceptcb(sk, newsk, sk->data);
774 if(pfds[i].revents & POLLERR)
776 retlen = sizeof(ret);
777 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
778 if(sk->errcb != NULL)
779 sk->errcb(sk, ret, sk->data);
784 if(pfds[i].revents & POLLERR)
786 retlen = sizeof(ret);
787 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
788 if(sk->conncb != NULL)
789 sk->conncb(sk, ret, sk->data);
793 if(pfds[i].revents & (POLLIN | POLLOUT))
795 sk->state = SOCK_EST;
796 if(sk->conncb != NULL)
797 sk->conncb(sk, 0, sk->data);
801 if(pfds[i].revents & POLLERR)
803 retlen = sizeof(ret);
804 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
805 if(sk->errcb != NULL)
806 sk->errcb(sk, ret, sk->data);
810 if(pfds[i].revents & POLLIN)
812 if(pfds[i].revents & POLLOUT)
814 if(sockqueuesize(sk) > 0)
819 if(pfds[i].revents & POLLNVAL)
821 flog(LOG_CRIT, "BUG: stale socket struct on fd %i", sk->fd);
822 sk->state = SOCK_STL;
826 if(pfds[i].revents & POLLHUP)
828 if(sk->errcb != NULL)
829 sk->errcb(sk, 0, sk->data);
836 for(sk = sockets; sk != NULL; sk = next)
839 if(sk->refcount == 1 && (sockqueuesize(sk) == 0))
844 if(sk->close && (sockqueuesize(sk) == 0))
846 if(sk->state == SOCK_STL)
855 int socksettos(struct socket *sk, int tos)
857 if(sk->family == AF_INET)
859 if(setsockopt(sk->fd, SOL_IP, IP_TOS, &tos, sizeof(tos)) < 0)
861 flog(LOG_WARNING, "could not set sock TOS to %i: %s", tos, strerror(errno));
866 /* XXX: How does the IPv6 traffic class work? */
867 flog(LOG_WARNING, "could not set TOS on sock of family %i", sk->family);
874 void (*callback)(struct sockaddr *addr, int addrlen, void *data);
876 struct sockaddr_storage addr;
880 static void resolvecb(pid_t pid, int status, struct resolvedata *data)
884 struct sockaddr_in *ipv4;
888 if((ret = read(data->fd, buf, sizeof(buf))) != 4)
891 data->callback(NULL, 0, data->data);
893 ipv4 = (struct sockaddr_in *)&data->addr;
894 memcpy(&ipv4->sin_addr, buf, 4);
895 data->callback((struct sockaddr *)ipv4, sizeof(*ipv4), data->data);
899 data->callback(NULL, 0, data->data);
905 int netresolve(char *addr, void (*callback)(struct sockaddr *addr, int addrlen, void *data), void *data)
912 struct resolvedata *rdata;
913 struct sockaddr_in ipv4;
919 if((p = strchr(addr, ':')) != NULL)
924 ipv4.sin_family = AF_INET;
925 ipv4.sin_port = htons(port);
926 if(inet_aton(addr, &ipv4.sin_addr))
928 callback((struct sockaddr *)&ipv4, sizeof(ipv4), data);
930 sigemptyset(&sigset);
931 sigaddset(&sigset, SIGCHLD);
932 sigprocmask(SIG_BLOCK, &sigset, NULL);
933 if((pipe(pfd) < 0) || ((child = fork()) < 0))
935 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
940 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
941 for(i = 3; i < FD_SETSIZE; i++)
946 signal(SIGALRM, SIG_DFL);
948 if((he = gethostbyname(addr)) == NULL)
950 write(pfd[1], he->h_addr_list[0], 4);
954 fcntl(pfd[0], F_SETFL, fcntl(pfd[0], F_GETFL) | O_NONBLOCK);
955 rdata = smalloc(sizeof(*rdata));
957 rdata->callback = callback;
959 memcpy(&rdata->addr, &ipv4, rdata->addrlen = sizeof(ipv4));
960 childcallback(child, (void (*)(pid_t, int, void *))resolvecb, rdata);
961 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
968 int sockgetlocalname(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
971 struct sockaddr_storage name;
974 if((sk->state == SOCK_STL) || (sk->fd < 0))
977 if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0)
979 flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetlocalname");
982 *namebuf = memcpy(smalloc(len), &name, len);
987 int sockgetremotename(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
990 struct sockaddr_storage name;
991 struct sockaddr_in *ipv4;
992 struct sockaddr *pname;
995 switch(confgetint("net", "mode"))
999 if((sk->state == SOCK_STL) || (sk->fd < 0))
1002 if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0)
1004 flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetremotename");
1007 if(name.ss_family == AF_INET)
1009 ipv4 = (struct sockaddr_in *)&name;
1010 if(getpublicaddr(AF_INET, &pname, &pnamelen) < 0)
1012 flog(LOG_WARNING, "could not determine public IP address - strange things may happen");
1015 ipv4->sin_addr.s_addr = ((struct sockaddr_in *)pname)->sin_addr.s_addr;
1018 *namebuf = memcpy(smalloc(len), &name, len);
1025 flog(LOG_CRIT, "unknown net mode %i active", confgetint("net", "mode"));
1031 char *formataddress(struct sockaddr *arg, socklen_t arglen)
1033 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1034 * lowercase letters to 1, so I do this
1036 struct sockaddr_in *ipv4;
1038 struct sockaddr_in6 *ipv6;
1040 static char *ret = NULL;
1046 switch(arg->sa_family)
1049 UNIX = (struct sockaddr_un *)arg;
1050 ret = sprintf2("%s", UNIX->sun_path);
1053 ipv4 = (struct sockaddr_in *)arg;
1054 if(inet_ntop(AF_INET, &ipv4->sin_addr, buf, sizeof(buf)) == NULL)
1056 ret = sprintf2("%s:%i", buf, (int)ntohs(ipv4->sin_port));
1060 ipv6 = (struct sockaddr_in6 *)arg;
1061 if(inet_ntop(AF_INET6, &ipv6->sin6_addr, buf, sizeof(buf)) == NULL)
1063 ret = sprintf2("%s:%i", buf, (int)ntohs(ipv6->sin6_port));
1067 errno = EPFNOSUPPORT;
1076 * It was very nice to use this, but it seems
1077 * to mess things up, so I guess it has to go... :-(
1080 static int formataddress(FILE *stream, const struct printf_info *info, const void *const *args)
1082 struct sockaddr *arg;
1084 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1085 * lowercase letters to 1, so I do this
1087 struct sockaddr_in *ipv4;
1090 arg = *(struct sockaddr **)(args[0]);
1091 arglen = *(socklen_t *)(args[1]);
1092 switch(arg->sa_family)
1095 UNIX = (struct sockaddr_un *)arg;
1096 ret = fprintf(stream, "%s", UNIX->sun_path);
1099 ipv4 = (struct sockaddr_in *)arg;
1100 ret = fprintf(stream, "%s:%i", inet_ntoa(ipv4->sin_addr), (int)ntohs(ipv4->sin_port));
1104 errno = EPFNOSUPPORT;
1110 static int formataddress_arginfo(const struct printf_info *info, size_t n, int *argtypes)
1113 argtypes[0] = PA_POINTER;
1115 argtypes[1] = PA_INT; /* Sources tell me that socklen_t _must_
1116 * be an int, so I guess this should be
1122 static int init(int hup)
1127 if(register_printf_function('N', formataddress, formataddress_arginfo))
1129 flog(LOG_CRIT, "could not register printf handler %%N: %s", strerror(errno));
1137 static void terminate(void)
1139 while(sockets != NULL)
1140 unlinksock(sockets);
1143 static struct module me =
1151 .terminate = terminate