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));
99 if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
101 conf.ifc_buf = smalloc(conf.ifc_len = 65536);
102 if(ioctl(sock, SIOCGIFCONF, &conf) < 0)
108 bufend = ((char *)conf.ifc_buf) + conf.ifc_len;
110 for(ifr = conf.ifc_ifcu.ifcu_req; (void *)ifr < bufend; ifr++)
112 if(strcmp(ifr->ifr_name, pif))
114 memset(&req, 0, sizeof(req));
115 memcpy(req.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name));
116 if(ioctl(sock, SIOCGIFFLAGS, &req) < 0)
118 if(!(req.ifr_flags & IFF_UP))
120 flog(LOG_WARNING, "public interface is down");
123 if(ifr->ifr_addr.sa_family != AF_INET)
125 flog(LOG_WARNING, "address of the public interface is not AF_INET");
128 ipv4 = smalloc(sizeof(*ipv4));
129 memcpy(ipv4, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
136 *addr = (struct sockaddr *)ipv4;
137 *lenbuf = sizeof(*ipv4);
146 static struct socket *newsock(int type)
150 new = smalloc(sizeof(*new));
153 new->isrealsocket = 1;
165 new->outbuf.s.buf = NULL;
166 new->outbuf.s.bufsize = 0;
167 new->outbuf.s.datasize = 0;
168 new->inbuf.s.buf = NULL;
169 new->inbuf.s.bufsize = 0;
170 new->inbuf.s.datasize = 0;
173 new->outbuf.d.f = new->outbuf.d.l = NULL;
174 new->inbuf.d.f = new->inbuf.d.l = NULL;
181 new->acceptcb = NULL;
191 static struct socket *mksock(int domain, int type)
196 if((fd = socket(domain, type, 0)) < 0)
198 flog(LOG_CRIT, "could not create socket: %s", strerror(errno));
203 new->family = domain;
204 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
208 struct socket *wrapsock(int fd)
212 new = newsock(SOCK_STREAM);
214 new->state = SOCK_EST;
215 new->isrealsocket = 0;
216 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
220 static void unlinksock(struct socket *sk)
223 sk->prev->next = sk->next;
225 sk->next->prev = sk->prev;
232 void getsock(struct socket *sk)
237 void putsock(struct socket *sk)
239 struct dgrambuf *buf;
241 if(--(sk->refcount) == 0)
246 if(sk->outbuf.s.buf != NULL)
247 free(sk->outbuf.s.buf);
248 if(sk->inbuf.s.buf != NULL)
249 free(sk->inbuf.s.buf);
252 while((buf = sk->outbuf.d.f) != NULL)
254 sk->outbuf.d.f = buf->next;
259 while((buf = sk->inbuf.d.f) != NULL)
261 sk->inbuf.d.f = buf->next;
270 if(sk->remote != NULL)
276 void sockpushdata(struct socket *sk, void *buf, size_t size)
281 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + size, 1, 1);
282 memmove(sk->inbuf.s.buf + size, sk->inbuf.s.buf, sk->inbuf.s.datasize);
283 memcpy(sk->inbuf.s.buf, buf, size);
284 sk->inbuf.s.datasize += size;
293 void *sockgetinbuf(struct socket *sk, size_t *size)
296 struct dgrambuf *dbuf;
301 if((sk->inbuf.s.buf == NULL) || (sk->inbuf.s.datasize == 0))
306 buf = sk->inbuf.s.buf;
307 *size = sk->inbuf.s.datasize;
308 sk->inbuf.s.buf = NULL;
309 sk->inbuf.s.bufsize = sk->inbuf.s.datasize = 0;
312 if((dbuf = sk->inbuf.d.f) == NULL)
314 sk->inbuf.d.f = dbuf->next;
315 if(dbuf->next == NULL)
316 sk->inbuf.d.l = NULL;
326 static void sockrecv(struct socket *sk)
329 struct dgrambuf *dbuf;
334 #if defined(HAVE_LINUX_SOCKIOS_H) && defined(SIOCINQ)
335 /* SIOCINQ is Linux-specific AFAIK, but I really have no idea
336 * how to read the inqueue size on other OSs */
337 if(ioctl(sk->fd, SIOCINQ, &inq))
339 /* I don't really know what could go wrong here, so let's
340 * assume it's transient. */
341 flog(LOG_WARNING, "SIOCINQ return %s on socket %i, falling back to 2048 bytes", strerror(errno), sk->fd);
349 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + inq, 1, 1);
350 ret = read(sk->fd, sk->inbuf.s.buf + sk->inbuf.s.datasize, inq);
353 if((errno == EINTR) || (errno == EAGAIN))
355 if(sk->errcb != NULL)
356 sk->errcb(sk, errno, sk->data);
362 if(sk->errcb != NULL)
363 sk->errcb(sk, 0, sk->data);
367 sk->inbuf.s.datasize += ret;
368 if(sk->readcb != NULL)
369 sk->readcb(sk, sk->data);
372 if(ioctl(sk->fd, SIOCINQ, &inq))
374 /* I don't really know what could go wrong here, so let's
375 * assume it's transient. */
376 flog(LOG_WARNING, "SIOCINQ return %s on socket %i", strerror(errno), sk->fd);
379 dbuf = smalloc(sizeof(*dbuf));
380 dbuf->data = smalloc(inq);
381 dbuf->addr = smalloc(dbuf->addrlen = sizeof(struct sockaddr_storage));
382 ret = recvfrom(sk->fd, dbuf->data, inq, 0, dbuf->addr, &dbuf->addrlen);
388 if((errno == EINTR) || (errno == EAGAIN))
390 if(sk->errcb != NULL)
391 sk->errcb(sk, errno, sk->data);
395 /* On UDP/IPv[46], ret == 0 doesn't mean EOF (since UDP can't
396 * have EOF), but rather an empty packet. I don't know if any
397 * other potential DGRAM protocols might have an EOF
398 * condition, so let's play safe. */
404 if(!((sk->family == AF_INET) || (sk->family == AF_INET6)))
406 if(sk->errcb != NULL)
407 sk->errcb(sk, 0, sk->data);
412 dbuf->addr = srealloc(dbuf->addr, dbuf->addrlen);
413 dbuf->data = srealloc(dbuf->data, dbuf->size = ret);
415 if(sk->inbuf.d.l != NULL)
416 sk->inbuf.d.l->next = dbuf;
418 sk->inbuf.d.f = dbuf;
419 sk->inbuf.d.l = dbuf;
420 if(sk->readcb != NULL)
421 sk->readcb(sk, sk->data);
426 static void sockflush(struct socket *sk)
429 struct dgrambuf *dbuf;
435 ret = send(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize, MSG_DONTWAIT | MSG_NOSIGNAL);
437 ret = write(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize);
440 /* For now, assume transient error, since
441 * the socket is polled for errors */
446 memmove(sk->outbuf.s.buf, ((char *)sk->outbuf.s.buf) + ret, sk->outbuf.s.datasize -= ret);
447 if(sk->writecb != NULL)
448 sk->writecb(sk, sk->data);
452 dbuf = sk->outbuf.d.f;
453 if((sk->outbuf.d.f = dbuf->next) == NULL)
454 sk->outbuf.d.l = NULL;
455 sendto(sk->fd, dbuf->data, dbuf->size, MSG_DONTWAIT | MSG_NOSIGNAL, dbuf->addr, dbuf->addrlen);
459 if(sk->writecb != NULL)
460 sk->writecb(sk, sk->data);
465 void closesock(struct socket *sk)
467 sk->state = SOCK_STL;
473 void sockqueue(struct socket *sk, void *data, size_t size)
475 struct dgrambuf *new;
477 if(sk->state == SOCK_STL)
482 sizebuf(&(sk->outbuf.s.buf), &(sk->outbuf.s.bufsize), sk->outbuf.s.datasize + size, 1, 1);
483 memcpy(sk->outbuf.s.buf + sk->outbuf.s.datasize, data, size);
484 sk->outbuf.s.datasize += size;
487 if(sk->remote == NULL)
489 new = smalloc(sizeof(*new));
491 memcpy(new->data = smalloc(size), data, new->size = size);
492 memcpy(new->addr = smalloc(sk->remotelen), sk->remote, new->addrlen = sk->remotelen);
493 if(sk->outbuf.d.l == NULL)
495 sk->outbuf.d.l = sk->outbuf.d.f = new;
497 sk->outbuf.d.l->next = new;
498 sk->outbuf.d.l = new;
504 size_t sockgetdatalen(struct socket *sk)
512 ret = sk->inbuf.s.datasize;
516 for(b = sk->inbuf.d.f; b != NULL; b = b->next)
523 size_t sockqueuesize(struct socket *sk)
531 ret = sk->outbuf.s.datasize;
535 for(b = sk->outbuf.d.f; b != NULL; b = b->next)
543 * The difference between netcslisten() and netcslistenlocal() is that
544 * netcslistenlocal() always listens on the local host, instead of
545 * following proxy/passive mode directions. It is suitable for eg. the
546 * UI channel, while the file sharing networks should, naturally, use
547 * netcslisten() instead.
550 struct socket *netcslistenlocal(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
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((sk = mksock(name->sa_family, type)) == NULL)
563 sk->state = SOCK_LST;
564 if(confgetint("net", "reuseaddr"))
567 setsockopt(sk->fd, SOL_SOCKET, SO_REUSEADDR, &intbuf, sizeof(intbuf));
569 if(bind(sk->fd, name, namelen) < 0)
574 if(listen(sk->fd, 16) < 0)
584 struct socket *netcslisten(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
586 if(confgetint("net", "mode") == 1)
591 if(confgetint("net", "mode") == 0)
592 return(netcslistenlocal(type, name, namelen, func, data));
597 struct socket *netcstcplisten(int port, int local, void (*func)(struct socket *, struct socket *, void *), void *data)
599 struct sockaddr_in addr;
601 struct sockaddr_in6 addr6;
603 struct socket *(*csfunc)(int, struct sockaddr *, socklen_t, void (*)(struct socket *, struct socket *, void *), void *);
607 csfunc = netcslistenlocal;
609 csfunc = netcslisten;
611 memset(&addr6, 0, sizeof(addr6));
612 addr6.sin6_family = AF_INET6;
613 addr6.sin6_port = htons(port);
614 addr6.sin6_addr = in6addr_any;
615 if((ret = csfunc(SOCK_STREAM, (struct sockaddr *)&addr6, sizeof(addr6), func, data)) != NULL)
617 if((ret == NULL) && (errno != EAFNOSUPPORT))
620 memset(&addr, 0, sizeof(addr));
621 addr.sin_family = AF_INET;
622 addr.sin_port = htons(port);
623 return(csfunc(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), func, data));
626 struct socket *netcsdgram(struct sockaddr *name, socklen_t namelen)
631 mode = confgetint("net", "mode");
632 if((mode == 0) || (mode == 1))
634 if((sk = mksock(name->sa_family, SOCK_DGRAM)) == NULL)
636 if(bind(sk->fd, name, namelen) < 0)
641 sk->state = SOCK_EST;
648 struct socket *netdupsock(struct socket *sk)
650 struct socket *newsk;
652 newsk = newsock(sk->type);
653 if((newsk->fd = dup(sk->fd)) < 0)
655 flog(LOG_WARNING, "could not dup() socket: %s", strerror(errno));
659 newsk->state = sk->state;
660 newsk->ignread = sk->ignread;
661 if(sk->remote != NULL)
662 memcpy(newsk->remote = smalloc(sk->remotelen), sk->remote, newsk->remotelen = sk->remotelen);
666 void netdgramconn(struct socket *sk, struct sockaddr *addr, socklen_t addrlen)
668 if(sk->remote != NULL)
670 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
674 struct socket *netcsconn(struct sockaddr *addr, socklen_t addrlen, void (*func)(struct socket *, int, void *), void *data)
679 mode = confgetint("net", "mode");
680 if((mode == 0) || (mode == 1))
682 if((sk = mksock(addr->sa_family, SOCK_STREAM)) == NULL)
684 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
685 if(!connect(sk->fd, addr, addrlen))
687 sk->state = SOCK_EST;
691 if(errno == EINPROGRESS)
693 sk->state = SOCK_SYN;
705 int pollsocks(int timeout)
711 struct socket *sk, *next, *newsk;
712 struct sockaddr_storage ss;
715 pfds = smalloc(sizeof(*pfds) * (num = numsocks));
716 for(i = 0, sk = sockets; i < num; sk = sk->next)
718 if(sk->state == SOCK_STL)
726 pfds[i].events |= POLLIN;
727 if((sk->state == SOCK_SYN) || (sockqueuesize(sk) > 0))
728 pfds[i].events |= POLLOUT;
732 ret = poll(pfds, num, timeout);
737 flog(LOG_CRIT, "pollsocks: poll errored out: %s", strerror(errno));
738 /* To avoid CPU hogging in case it's bad, which it
745 for(sk = sockets; sk != NULL; sk = next)
748 for(i = 0; i < num; i++)
750 if(pfds[i].fd == sk->fd)
758 if(pfds[i].revents & POLLIN)
761 if((newfd = accept(sk->fd, (struct sockaddr *)&ss, &sslen)) < 0)
763 if(sk->errcb != NULL)
764 sk->errcb(sk, errno, sk->data);
766 newsk = newsock(sk->type);
768 newsk->family = sk->family;
769 newsk->state = SOCK_EST;
770 memcpy(newsk->remote = smalloc(sslen), &ss, sslen);
771 newsk->remotelen = sslen;
773 if(sk->acceptcb != NULL)
774 sk->acceptcb(sk, newsk, sk->data);
776 if(pfds[i].revents & POLLERR)
778 retlen = sizeof(ret);
779 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
780 if(sk->errcb != NULL)
781 sk->errcb(sk, ret, sk->data);
786 if(pfds[i].revents & POLLERR)
788 retlen = sizeof(ret);
789 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
790 if(sk->conncb != NULL)
791 sk->conncb(sk, ret, sk->data);
795 if(pfds[i].revents & (POLLIN | POLLOUT))
797 sk->state = SOCK_EST;
798 if(sk->conncb != NULL)
799 sk->conncb(sk, 0, sk->data);
803 if(pfds[i].revents & POLLERR)
805 retlen = sizeof(ret);
806 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
807 if(sk->errcb != NULL)
808 sk->errcb(sk, ret, sk->data);
812 if(pfds[i].revents & POLLIN)
814 if(pfds[i].revents & POLLOUT)
816 if(sockqueuesize(sk) > 0)
821 if(pfds[i].revents & POLLNVAL)
823 flog(LOG_CRIT, "BUG: stale socket struct on fd %i", sk->fd);
824 sk->state = SOCK_STL;
828 if(pfds[i].revents & POLLHUP)
830 if(sk->errcb != NULL)
831 sk->errcb(sk, 0, sk->data);
838 for(sk = sockets; sk != NULL; sk = next)
841 if(sk->refcount == 1 && (sockqueuesize(sk) == 0))
846 if(sk->close && (sockqueuesize(sk) == 0))
848 if(sk->state == SOCK_STL)
857 int socksettos(struct socket *sk, int tos)
861 if(sk->family == AF_INET)
868 case SOCK_TOS_MINCOST:
871 case SOCK_TOS_MAXREL:
877 case SOCK_TOS_MINDELAY:
881 flog(LOG_WARNING, "attempted to set unknown TOS value %i to IPv4 sock", tos);
884 if(setsockopt(sk->fd, SOL_IP, IP_TOS, &buf, sizeof(buf)) < 0)
886 flog(LOG_WARNING, "could not set sock TOS to %i: %s", tos, strerror(errno));
891 if(sk->family == AF_INET6)
897 case SOCK_TOS_MINCOST:
898 buf = confgetint("net", "diffserv-mincost");
900 case SOCK_TOS_MAXREL:
901 buf = confgetint("net", "diffserv-maxrel");
904 buf = confgetint("net", "diffserv-maxtp");
906 case SOCK_TOS_MINDELAY:
907 buf = confgetint("net", "diffserv-mindelay");
910 flog(LOG_WARNING, "attempted to set unknown TOS value %i to IPv4 sock", tos);
914 On Linux, the API IPv6 flow label management doesn't seem to
915 be entirely complete, so I guess this will have to wait.
917 if(setsockopt(...) < 0)
919 flog(LOG_WARNING, "could not set sock traffic class to %i: %s", tos, strerror(errno));
925 flog(LOG_WARNING, "could not set TOS on sock of family %i", sk->family);
932 void (*callback)(struct sockaddr *addr, int addrlen, void *data);
934 struct sockaddr_storage addr;
938 static void resolvecb(pid_t pid, int status, struct resolvedata *data)
942 struct sockaddr_in *ipv4;
946 if((ret = read(data->fd, buf, sizeof(buf))) != 4)
949 data->callback(NULL, 0, data->data);
951 ipv4 = (struct sockaddr_in *)&data->addr;
952 memcpy(&ipv4->sin_addr, buf, 4);
953 data->callback((struct sockaddr *)ipv4, sizeof(*ipv4), data->data);
957 data->callback(NULL, 0, data->data);
963 int netresolve(char *addr, void (*callback)(struct sockaddr *addr, int addrlen, void *data), void *data)
970 struct resolvedata *rdata;
971 struct sockaddr_in ipv4;
977 if((p = strchr(addr, ':')) != NULL)
982 ipv4.sin_family = AF_INET;
983 ipv4.sin_port = htons(port);
984 if(inet_aton(addr, &ipv4.sin_addr))
986 callback((struct sockaddr *)&ipv4, sizeof(ipv4), data);
988 sigemptyset(&sigset);
989 sigaddset(&sigset, SIGCHLD);
990 sigprocmask(SIG_BLOCK, &sigset, NULL);
991 if((pipe(pfd) < 0) || ((child = fork()) < 0))
993 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
998 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
999 for(i = 3; i < FD_SETSIZE; i++)
1004 signal(SIGALRM, SIG_DFL);
1006 if((he = gethostbyname(addr)) == NULL)
1008 write(pfd[1], he->h_addr_list[0], 4);
1012 fcntl(pfd[0], F_SETFL, fcntl(pfd[0], F_GETFL) | O_NONBLOCK);
1013 rdata = smalloc(sizeof(*rdata));
1015 rdata->callback = callback;
1017 memcpy(&rdata->addr, &ipv4, rdata->addrlen = sizeof(ipv4));
1018 childcallback(child, (void (*)(pid_t, int, void *))resolvecb, rdata);
1019 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1026 int sockgetlocalname(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
1029 struct sockaddr_storage name;
1032 if((sk->state == SOCK_STL) || (sk->fd < 0))
1035 if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0)
1037 flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetlocalname (%s)", strerror(errno));
1040 *namebuf = memcpy(smalloc(len), &name, len);
1045 static void sethostaddr(struct sockaddr *dst, struct sockaddr *src)
1047 if(dst->sa_family != src->sa_family)
1049 flog(LOG_ERR, "BUG: non-matching socket families in sethostaddr (%i -> %i)", src->sa_family, dst->sa_family);
1052 switch(src->sa_family)
1055 ((struct sockaddr_in *)dst)->sin_addr = ((struct sockaddr_in *)src)->sin_addr;
1058 ((struct sockaddr_in6 *)dst)->sin6_addr = ((struct sockaddr_in6 *)src)->sin6_addr;
1061 flog(LOG_WARNING, "sethostaddr unimplemented for family %i", src->sa_family);
1066 static int makepublic(struct sockaddr *addr)
1070 struct sockaddr *pname;
1072 if((ret = getpublicaddr(addr->sa_family, &pname, &plen)) < 0)
1074 flog(LOG_ERR, "could not get public address: %s", strerror(errno));
1079 sethostaddr(addr, pname);
1084 int sockgetremotename(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
1087 struct sockaddr *name;
1089 switch(confgetint("net", "mode"))
1093 if((sk->state == SOCK_STL) || (sk->fd < 0))
1098 if(!sockgetlocalname(sk, &name, &len))
1105 flog(LOG_ERR, "could not get remotely accessible name by any means");
1111 flog(LOG_CRIT, "unknown net mode %i active", confgetint("net", "mode"));
1117 int sockgetremotename2(struct socket *sk, struct socket *sk2, struct sockaddr **namebuf, socklen_t *lenbuf)
1119 struct sockaddr *name1, *name2;
1120 socklen_t len1, len2;
1122 if(sk->family != sk2->family)
1124 flog(LOG_ERR, "using sockgetremotename2 with sockets of differing family: %i %i", sk->family, sk2->family);
1127 if(sockgetremotename(sk, &name1, &len1))
1129 if(sockgetremotename(sk2, &name2, &len2)) {
1133 sethostaddr(name1, name2);
1140 int addreq(struct sockaddr *x, struct sockaddr *y)
1142 struct sockaddr_un *u1, *u2;
1143 struct sockaddr_in *n1, *n2;
1145 struct sockaddr_in6 *s1, *s2;
1148 if(x->sa_family != y->sa_family)
1150 switch(x->sa_family) {
1152 u1 = (struct sockaddr_un *)x; u2 = (struct sockaddr_un *)y;
1153 if(strncmp(u1->sun_path, u2->sun_path, sizeof(u1->sun_path)))
1157 n1 = (struct sockaddr_in *)x; n2 = (struct sockaddr_in *)y;
1158 if(n1->sin_port != n2->sin_port)
1160 if(n1->sin_addr.s_addr != n2->sin_addr.s_addr)
1165 s1 = (struct sockaddr_in6 *)x; s2 = (struct sockaddr_in6 *)y;
1166 if(s1->sin6_port != s2->sin6_port)
1168 if(memcmp(s1->sin6_addr.s6_addr, s2->sin6_addr.s6_addr, sizeof(s1->sin6_addr.s6_addr)))
1176 char *formataddress(struct sockaddr *arg, socklen_t arglen)
1178 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1179 * lowercase letters to 1, so I do this
1181 struct sockaddr_in *ipv4;
1183 struct sockaddr_in6 *ipv6;
1185 static char *ret = NULL;
1191 switch(arg->sa_family)
1194 UNIX = (struct sockaddr_un *)arg;
1195 ret = sprintf2("%s", UNIX->sun_path);
1198 ipv4 = (struct sockaddr_in *)arg;
1199 if(inet_ntop(AF_INET, &ipv4->sin_addr, buf, sizeof(buf)) == NULL)
1201 ret = sprintf2("%s:%i", buf, (int)ntohs(ipv4->sin_port));
1205 ipv6 = (struct sockaddr_in6 *)arg;
1206 if(inet_ntop(AF_INET6, &ipv6->sin6_addr, buf, sizeof(buf)) == NULL)
1208 ret = sprintf2("[%s]:%i", buf, (int)ntohs(ipv6->sin6_port));
1212 errno = EPFNOSUPPORT;
1221 * It was very nice to use this, but it seems
1222 * to mess things up, so I guess it has to go... :-(
1225 static int formataddress(FILE *stream, const struct printf_info *info, const void *const *args)
1227 struct sockaddr *arg;
1229 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1230 * lowercase letters to 1, so I do this
1232 struct sockaddr_in *ipv4;
1235 arg = *(struct sockaddr **)(args[0]);
1236 arglen = *(socklen_t *)(args[1]);
1237 switch(arg->sa_family)
1240 UNIX = (struct sockaddr_un *)arg;
1241 ret = fprintf(stream, "%s", UNIX->sun_path);
1244 ipv4 = (struct sockaddr_in *)arg;
1245 ret = fprintf(stream, "%s:%i", inet_ntoa(ipv4->sin_addr), (int)ntohs(ipv4->sin_port));
1249 errno = EPFNOSUPPORT;
1255 static int formataddress_arginfo(const struct printf_info *info, size_t n, int *argtypes)
1258 argtypes[0] = PA_POINTER;
1260 argtypes[1] = PA_INT; /* Sources tell me that socklen_t _must_
1261 * be an int, so I guess this should be
1267 static int init(int hup)
1272 if(register_printf_function('N', formataddress, formataddress_arginfo))
1274 flog(LOG_CRIT, "could not register printf handler %%N: %s", strerror(errno));
1282 static void terminate(void)
1284 while(sockets != NULL)
1285 unlinksock(sockets);
1288 static struct module me =
1296 .terminate = terminate