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 /* Only for direct mode */
55 {CONF_VAR_IPV4, "visibleipv4", {.ipv4 = {0}}},
56 {CONF_VAR_STRING, "publicif", {.str = L""}},
60 static struct socket *sockets = NULL;
63 /* XXX: Get autoconf for all this... */
64 int getpublicaddr(int af, struct sockaddr **addr, socklen_t *lenbuf)
66 struct sockaddr_in *ipv4;
67 struct configvar *var;
71 struct ifreq *ifr, req;
76 var = confgetvar("net", "visibleipv4");
77 if(var->val.ipv4.s_addr != 0)
79 ipv4 = smalloc(sizeof(*ipv4));
80 ipv4->sin_family = AF_INET;
81 ipv4->sin_addr.s_addr = var->val.ipv4.s_addr;
82 *addr = (struct sockaddr *)ipv4;
83 *lenbuf = sizeof(*ipv4);
86 if((pif = icwcstombs(confgetstr("net", "publicif"), NULL)) == NULL)
88 flog(LOG_ERR, "could not convert net.publicif into local charset: %s", strerror(errno));
91 if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
93 conf.ifc_buf = smalloc(conf.ifc_len = 65536);
94 if(ioctl(sock, SIOCGIFCONF, &conf) < 0)
100 bufend = ((char *)conf.ifc_buf) + conf.ifc_len;
102 for(ifr = conf.ifc_ifcu.ifcu_req; (void *)ifr < bufend; ifr++)
104 memset(&req, 0, sizeof(req));
105 memcpy(req.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name));
106 if(ioctl(sock, SIOCGIFFLAGS, &req) < 0)
112 if(!(req.ifr_flags & IFF_UP))
114 if(ifr->ifr_addr.sa_family == AF_INET)
116 if(ntohl(((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr) == 0x7f000001)
120 ipv4 = smalloc(sizeof(*ipv4));
121 memcpy(ipv4, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
124 flog(LOG_WARNING, "could not locate an unambiguous interface for determining your public IP address - set net.publicif");
125 errno = ENFILE; /* XXX: There's no appropriate one for this... */
133 *addr = (struct sockaddr *)ipv4;
134 *lenbuf = sizeof(*ipv4);
140 errno = EPFNOSUPPORT;
144 static struct socket *newsock(int type)
148 new = smalloc(sizeof(*new));
151 new->isrealsocket = 1;
163 new->outbuf.s.buf = NULL;
164 new->outbuf.s.bufsize = 0;
165 new->outbuf.s.datasize = 0;
166 new->inbuf.s.buf = NULL;
167 new->inbuf.s.bufsize = 0;
168 new->inbuf.s.datasize = 0;
171 new->outbuf.d.f = new->outbuf.d.l = NULL;
172 new->inbuf.d.f = new->inbuf.d.l = NULL;
179 new->acceptcb = NULL;
189 static struct socket *mksock(int domain, int type)
194 if((fd = socket(domain, type, 0)) < 0)
196 flog(LOG_CRIT, "could not create socket: %s", strerror(errno));
201 new->family = domain;
202 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
206 struct socket *wrapsock(int fd)
210 new = newsock(SOCK_STREAM);
212 new->state = SOCK_EST;
213 new->isrealsocket = 0;
214 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
218 static void unlinksock(struct socket *sk)
221 sk->prev->next = sk->next;
223 sk->next->prev = sk->prev;
230 void getsock(struct socket *sk)
235 void putsock(struct socket *sk)
237 struct dgrambuf *buf;
239 if(--(sk->refcount) == 0)
244 if(sk->outbuf.s.buf != NULL)
245 free(sk->outbuf.s.buf);
246 if(sk->inbuf.s.buf != NULL)
247 free(sk->inbuf.s.buf);
250 while((buf = sk->outbuf.d.f) != NULL)
252 sk->outbuf.d.f = buf->next;
256 while((buf = sk->inbuf.d.f) != NULL)
258 sk->inbuf.d.f = buf->next;
266 if(sk->remote != NULL)
272 void *sockgetinbuf(struct socket *sk, size_t *size)
275 struct dgrambuf *dbuf;
280 if((sk->inbuf.s.buf == NULL) || (sk->inbuf.s.datasize == 0))
285 buf = sk->inbuf.s.buf;
286 *size = sk->inbuf.s.datasize;
287 sk->inbuf.s.buf = NULL;
288 sk->inbuf.s.bufsize = sk->inbuf.s.datasize = 0;
291 if((dbuf = sk->inbuf.d.f) == NULL)
293 sk->inbuf.d.f = dbuf->next;
294 if(dbuf->next == NULL)
295 sk->inbuf.d.l = NULL;
305 static void sockrecv(struct socket *sk)
308 struct dgrambuf *dbuf;
313 #if defined(HAVE_LINUX_SOCKIOS_H) && defined(SIOCINQ)
314 /* SIOCINQ is Linux-specific AFAIK, but I really have no idea
315 * how to read the inqueue size on other OSs */
316 if(ioctl(sk->fd, SIOCINQ, &inq))
318 /* I don't really know what could go wrong here, so let's
319 * assume it's transient. */
320 flog(LOG_WARNING, "SIOCINQ return %s on socket %i, falling back to 2048 bytes", strerror(errno), sk->fd);
328 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + inq, 1, 1);
329 ret = read(sk->fd, sk->inbuf.s.buf + sk->inbuf.s.datasize, inq);
332 if((errno == EINTR) || (errno == EAGAIN))
334 if(sk->errcb != NULL)
335 sk->errcb(sk, errno, sk->data);
341 if(sk->errcb != NULL)
342 sk->errcb(sk, 0, sk->data);
346 sk->inbuf.s.datasize += ret;
347 if(sk->readcb != NULL)
348 sk->readcb(sk, sk->data);
351 if(ioctl(sk->fd, SIOCINQ, &inq))
353 /* I don't really know what could go wrong here, so let's
354 * assume it's transient. */
355 flog(LOG_WARNING, "SIOCINQ return %s on socket %i", strerror(errno), sk->fd);
358 dbuf = smalloc(sizeof(*dbuf));
359 dbuf->data = smalloc(inq);
360 dbuf->addr = smalloc(dbuf->addrlen = sizeof(struct sockaddr_storage));
361 ret = recvfrom(sk->fd, dbuf->data, inq, 0, dbuf->addr, &dbuf->addrlen);
367 if((errno == EINTR) || (errno == EAGAIN))
369 if(sk->errcb != NULL)
370 sk->errcb(sk, errno, sk->data);
374 /* On UDP/IPv[46], ret == 0 doesn't mean EOF (since UDP can't
375 * have EOF), but rather an empty packet. I don't know if any
376 * other potential DGRAM protocols might have an EOF
377 * condition, so let's play safe. */
383 if(!((sk->family == AF_INET) || (sk->family == AF_INET6)))
385 if(sk->errcb != NULL)
386 sk->errcb(sk, 0, sk->data);
391 dbuf->addr = srealloc(dbuf->addr, dbuf->addrlen);
392 dbuf->data = srealloc(dbuf->data, dbuf->size = ret);
394 if(sk->inbuf.d.l != NULL)
395 sk->inbuf.d.l->next = dbuf;
397 sk->inbuf.d.f = dbuf;
398 sk->inbuf.d.l = dbuf;
399 if(sk->readcb != NULL)
400 sk->readcb(sk, sk->data);
405 static void sockflush(struct socket *sk)
408 struct dgrambuf *dbuf;
414 ret = send(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize, MSG_DONTWAIT | MSG_NOSIGNAL);
416 ret = write(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize);
419 /* For now, assume transient error, since
420 * the socket is polled for errors */
425 memmove(sk->outbuf.s.buf, ((char *)sk->outbuf.s.buf) + ret, sk->outbuf.s.datasize -= ret);
426 if(sk->writecb != NULL)
427 sk->writecb(sk, sk->data);
431 dbuf = sk->outbuf.d.f;
432 if((sk->outbuf.d.f = dbuf->next) == NULL)
433 sk->outbuf.d.l = NULL;
434 sendto(sk->fd, dbuf->data, dbuf->size, MSG_DONTWAIT | MSG_NOSIGNAL, dbuf->addr, dbuf->addrlen);
438 if(sk->writecb != NULL)
439 sk->writecb(sk, sk->data);
444 void closesock(struct socket *sk)
446 sk->state = SOCK_STL;
452 void sockqueue(struct socket *sk, void *data, size_t size)
454 struct dgrambuf *new;
456 if(sk->state == SOCK_STL)
461 sizebuf(&(sk->outbuf.s.buf), &(sk->outbuf.s.bufsize), sk->outbuf.s.datasize + size, 1, 1);
462 memcpy(sk->outbuf.s.buf + sk->outbuf.s.datasize, data, size);
463 sk->outbuf.s.datasize += size;
466 if(sk->remote == NULL)
468 new = smalloc(sizeof(*new));
470 memcpy(new->data = smalloc(size), data, new->size = size);
471 memcpy(new->addr = smalloc(sk->remotelen), sk->remote, new->addrlen = sk->remotelen);
472 if(sk->outbuf.d.l == NULL)
474 sk->outbuf.d.l = sk->outbuf.d.f = new;
476 sk->outbuf.d.l->next = new;
477 sk->outbuf.d.l = new;
483 size_t sockgetdatalen(struct socket *sk)
491 ret = sk->inbuf.s.datasize;
495 for(b = sk->inbuf.d.f; b != NULL; b = b->next)
502 size_t sockqueuesize(struct socket *sk)
510 ret = sk->outbuf.s.datasize;
514 for(b = sk->outbuf.d.f; b != NULL; b = b->next)
521 struct socket *netcslisten(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
525 if(confgetint("net", "mode") == 1)
530 /* I don't know if this is actually correct (it probably isn't),
531 * but since, at on least Linux systems, PF_* are specifically
532 * #define'd to their AF_* counterparts, it allows for a severely
533 * smoother implementation. If it breaks something on your
534 * platform, please tell me so.
536 if(confgetint("net", "mode") == 0)
538 if((sk = mksock(name->sa_family, type)) == NULL)
540 sk->state = SOCK_LST;
541 if(bind(sk->fd, name, namelen) < 0)
546 if(listen(sk->fd, 16) < 0)
560 * The difference between netcslisten() and netcslistenlocal() is that
561 * netcslistenlocal() always listens on the local host, instead of
562 * following proxy/passive mode directions. It is suitable for eg. the
563 * UI channel, while the file sharing networks should, naturally, use
564 * netcslisten() instead.
567 struct socket *netcslistenlocal(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
571 /* I don't know if this is actually correct (it probably isn't),
572 * but since, at on least Linux systems, PF_* are specifically
573 * #define'd to their AF_* counterparts, it allows for a severely
574 * smoother implementation. If it breaks something on your
575 * platform, please tell me so.
577 if((sk = mksock(name->sa_family, type)) == NULL)
579 sk->state = SOCK_LST;
580 if(bind(sk->fd, name, namelen) < 0)
585 if(listen(sk->fd, 16) < 0)
595 struct socket *netcsdgram(struct sockaddr *name, socklen_t namelen)
600 mode = confgetint("net", "mode");
601 if((mode == 0) || (mode == 1))
603 if((sk = mksock(name->sa_family, SOCK_DGRAM)) == NULL)
605 if(bind(sk->fd, name, namelen) < 0)
610 sk->state = SOCK_EST;
617 struct socket *netdupsock(struct socket *sk)
619 struct socket *newsk;
621 newsk = newsock(sk->type);
622 if((newsk->fd = dup(sk->fd)) < 0)
624 flog(LOG_WARNING, "could not dup() socket: %s", strerror(errno));
628 newsk->state = sk->state;
629 newsk->ignread = sk->ignread;
630 if(sk->remote != NULL)
631 memcpy(newsk->remote = smalloc(sk->remotelen), sk->remote, newsk->remotelen = sk->remotelen);
635 void netdgramconn(struct socket *sk, struct sockaddr *addr, socklen_t addrlen)
637 if(sk->remote != NULL)
639 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
643 struct socket *netcsconn(struct sockaddr *addr, socklen_t addrlen, void (*func)(struct socket *, int, void *), void *data)
648 mode = confgetint("net", "mode");
649 if((mode == 0) || (mode == 1))
651 if((sk = mksock(addr->sa_family, SOCK_STREAM)) == NULL)
653 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
654 if(!connect(sk->fd, addr, addrlen))
656 sk->state = SOCK_EST;
660 if(errno == EINPROGRESS)
662 sk->state = SOCK_SYN;
674 int pollsocks(int timeout)
676 int i, num, ret, retlen;
679 struct socket *sk, *next, *newsk;
680 struct sockaddr_storage ss;
683 pfds = smalloc(sizeof(*pfds) * (num = numsocks));
684 for(i = 0, sk = sockets; i < num; sk = sk->next)
686 if(sk->state == SOCK_STL)
694 pfds[i].events |= POLLIN;
695 if((sk->state == SOCK_SYN) || (sockqueuesize(sk) > 0))
696 pfds[i].events |= POLLOUT;
700 ret = poll(pfds, num, timeout);
705 flog(LOG_CRIT, "pollsocks: poll errored out: %s", strerror(errno));
706 /* To avoid CPU hogging in case it's bad, which it
713 for(sk = sockets; sk != NULL; sk = next)
716 for(i = 0; i < num; i++)
718 if(pfds[i].fd == sk->fd)
726 if(pfds[i].revents & POLLIN)
729 if((newfd = accept(sk->fd, (struct sockaddr *)&ss, &sslen)) < 0)
731 if(sk->errcb != NULL)
732 sk->errcb(sk, errno, sk->data);
734 newsk = newsock(sk->type);
736 newsk->family = sk->family;
737 newsk->state = SOCK_EST;
738 memcpy(newsk->remote = smalloc(sslen), &ss, sslen);
739 newsk->remotelen = sslen;
741 if(sk->acceptcb != NULL)
742 sk->acceptcb(sk, newsk, sk->data);
744 if(pfds[i].revents & POLLERR)
746 retlen = sizeof(ret);
747 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
748 if(sk->errcb != NULL)
749 sk->errcb(sk, ret, sk->data);
754 if(pfds[i].revents & POLLERR)
756 retlen = sizeof(ret);
757 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
758 if(sk->conncb != NULL)
759 sk->conncb(sk, ret, sk->data);
763 if(pfds[i].revents & (POLLIN | POLLOUT))
765 sk->state = SOCK_EST;
766 if(sk->conncb != NULL)
767 sk->conncb(sk, 0, sk->data);
771 if(pfds[i].revents & POLLERR)
773 retlen = sizeof(ret);
774 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
775 if(sk->errcb != NULL)
776 sk->errcb(sk, ret, sk->data);
780 if(pfds[i].revents & POLLIN)
782 if(pfds[i].revents & POLLOUT)
784 if(sockqueuesize(sk) > 0)
789 if(pfds[i].revents & POLLNVAL)
791 flog(LOG_CRIT, "BUG: stale socket struct on fd %i", sk->fd);
792 sk->state = SOCK_STL;
796 if(pfds[i].revents & POLLHUP)
798 if(sk->errcb != NULL)
799 sk->errcb(sk, 0, sk->data);
806 for(sk = sockets; sk != NULL; sk = next)
809 if(sk->refcount == 1 && (sockqueuesize(sk) == 0))
814 if(sk->close && (sockqueuesize(sk) == 0))
816 if(sk->state == SOCK_STL)
825 int socksettos(struct socket *sk, int tos)
827 if(sk->family == AF_INET)
829 if(setsockopt(sk->fd, SOL_IP, IP_TOS, &tos, sizeof(tos)) < 0)
831 flog(LOG_WARNING, "could not set sock TOS to %i: %s", tos, strerror(errno));
836 /* XXX: How does the IPv6 traffic class work? */
837 flog(LOG_WARNING, "could not set TOS on sock of family %i", sk->family);
844 void (*callback)(struct sockaddr *addr, int addrlen, void *data);
846 struct sockaddr_storage addr;
850 static void resolvecb(pid_t pid, int status, struct resolvedata *data)
854 struct sockaddr_in *ipv4;
858 if((ret = read(data->fd, buf, sizeof(buf))) != 4)
861 data->callback(NULL, 0, data->data);
863 ipv4 = (struct sockaddr_in *)&data->addr;
864 memcpy(&ipv4->sin_addr, buf, 4);
865 data->callback((struct sockaddr *)ipv4, sizeof(*ipv4), data->data);
869 data->callback(NULL, 0, data->data);
875 int netresolve(char *addr, void (*callback)(struct sockaddr *addr, int addrlen, void *data), void *data)
882 struct resolvedata *rdata;
883 struct sockaddr_in ipv4;
889 if((p = strchr(addr, ':')) != NULL)
894 ipv4.sin_family = AF_INET;
895 ipv4.sin_port = htons(port);
896 if(inet_aton(addr, &ipv4.sin_addr))
898 callback((struct sockaddr *)&ipv4, sizeof(ipv4), data);
900 sigemptyset(&sigset);
901 sigaddset(&sigset, SIGCHLD);
902 sigprocmask(SIG_BLOCK, &sigset, NULL);
903 if((pipe(pfd) < 0) || ((child = fork()) < 0))
905 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
910 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
911 for(i = 3; i < FD_SETSIZE; i++)
916 signal(SIGALRM, SIG_DFL);
918 if((he = gethostbyname(addr)) == NULL)
920 write(pfd[1], he->h_addr_list[0], 4);
924 fcntl(pfd[0], F_SETFL, fcntl(pfd[0], F_GETFL) | O_NONBLOCK);
925 rdata = smalloc(sizeof(*rdata));
927 rdata->callback = callback;
929 memcpy(&rdata->addr, &ipv4, rdata->addrlen = sizeof(ipv4));
930 childcallback(child, (void (*)(pid_t, int, void *))resolvecb, rdata);
931 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
938 int sockgetlocalname(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
941 struct sockaddr_storage name;
944 if((sk->state == SOCK_STL) || (sk->fd < 0))
947 if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0)
949 flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetlocalname");
952 *namebuf = memcpy(smalloc(len), &name, len);
957 int sockgetremotename(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
960 struct sockaddr_storage name;
961 struct sockaddr_in *ipv4;
962 struct sockaddr *pname;
965 switch(confgetint("net", "mode"))
969 if((sk->state == SOCK_STL) || (sk->fd < 0))
972 if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0)
974 flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetremotename");
977 if(name.ss_family == AF_INET)
979 ipv4 = (struct sockaddr_in *)&name;
980 if(getpublicaddr(AF_INET, &pname, &pnamelen) < 0)
982 flog(LOG_WARNING, "could not determine public IP address - strange things may happen");
985 ipv4->sin_addr.s_addr = ((struct sockaddr_in *)pname)->sin_addr.s_addr;
988 *namebuf = memcpy(smalloc(len), &name, len);
995 flog(LOG_CRIT, "unknown net mode %i active", confgetint("net", "mode"));
1001 char *formataddress(struct sockaddr *arg, socklen_t arglen)
1003 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1004 * lowercase letters to 1, so I do this
1006 struct sockaddr_in *ipv4;
1008 struct sockaddr_in6 *ipv6;
1010 static char *ret = NULL;
1016 switch(arg->sa_family)
1019 UNIX = (struct sockaddr_un *)arg;
1020 ret = sprintf2("%s", UNIX->sun_path);
1023 ipv4 = (struct sockaddr_in *)arg;
1024 if(inet_ntop(AF_INET, &ipv4->sin_addr, buf, sizeof(buf)) == NULL)
1026 ret = sprintf2("%s:%i", buf, (int)ntohs(ipv4->sin_port));
1030 ipv6 = (struct sockaddr_in6 *)arg;
1031 if(inet_ntop(AF_INET6, &ipv6->sin6_addr, buf, sizeof(buf)) == NULL)
1033 ret = sprintf2("%s:%i", buf, (int)ntohs(ipv6->sin6_port));
1037 errno = EPFNOSUPPORT;
1046 * It was very nice to use this, but it seems
1047 * to mess things up, so I guess it has to go... :-(
1050 static int formataddress(FILE *stream, const struct printf_info *info, const void *const *args)
1052 struct sockaddr *arg;
1054 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1055 * lowercase letters to 1, so I do this
1057 struct sockaddr_in *ipv4;
1060 arg = *(struct sockaddr **)(args[0]);
1061 arglen = *(socklen_t *)(args[1]);
1062 switch(arg->sa_family)
1065 UNIX = (struct sockaddr_un *)arg;
1066 ret = fprintf(stream, "%s", UNIX->sun_path);
1069 ipv4 = (struct sockaddr_in *)arg;
1070 ret = fprintf(stream, "%s:%i", inet_ntoa(ipv4->sin_addr), (int)ntohs(ipv4->sin_port));
1074 errno = EPFNOSUPPORT;
1080 static int formataddress_arginfo(const struct printf_info *info, size_t n, int *argtypes)
1083 argtypes[0] = PA_POINTER;
1085 argtypes[1] = PA_INT; /* Sources tell me that socklen_t _must_
1086 * be an int, so I guess this should be
1092 static int init(int hup)
1097 if(register_printf_function('N', formataddress, formataddress_arginfo))
1099 flog(LOG_CRIT, "could not register printf handler %%N: %s", strerror(errno));
1107 static void terminate(void)
1109 while(sockets != NULL)
1110 unlinksock(sockets);
1113 static struct module me =
1121 .terminate = terminate