Check hub addresses for sources hubs.
[doldaconnect.git] / daemon / net.c
CommitLineData
d3372da9 1/*
2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
4 *
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.
9 *
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.
14 *
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
18*/
19/* XXX: Implement SOCKS proxyability */
20
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24#include <string.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <fcntl.h>
28#include <sys/ioctl.h>
29#include <sys/socket.h>
30#include <sys/un.h>
31#include <sys/poll.h>
32#include <arpa/inet.h>
33#include <netinet/in.h>
34#include <netdb.h>
35#include <sys/signal.h>
36#include <printf.h>
37#ifdef HAVE_LINUX_SOCKIOS_H
38#include <linux/sockios.h>
39#endif
40#include <errno.h>
41#include <net/if.h>
42
43#include "conf.h"
44#include "net.h"
45#include "module.h"
46#include "log.h"
47#include "utils.h"
48#include "sysevents.h"
49
50static struct configvar myvars[] =
51{
52 /* 0 = Direct mode, 1 = Passive mode, 2 = SOCKS proxy */
53 {CONF_VAR_INT, "mode", {.num = 0}},
347d6d76 54 {CONF_VAR_BOOL, "reuseaddr", {.num = 0}},
d3372da9 55 /* Only for direct mode */
56 {CONF_VAR_IPV4, "visibleipv4", {.ipv4 = {0}}},
57 {CONF_VAR_STRING, "publicif", {.str = L""}},
b020fb3d 58 /* Diffserv should be supported on IPv4, too, but I don't know the
59 * API to do that. */
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}},
d3372da9 64 {CONF_VAR_END}
65};
66
67static struct socket *sockets = NULL;
68int numsocks = 0;
69
70/* XXX: Get autoconf for all this... */
71int getpublicaddr(int af, struct sockaddr **addr, socklen_t *lenbuf)
72{
73 struct sockaddr_in *ipv4;
74 struct configvar *var;
75 void *bufend;
76 int sock;
77 struct ifconf conf;
78 struct ifreq *ifr, req;
79 char *pif;
80
81 if(af == AF_INET)
82 {
83 var = confgetvar("net", "visibleipv4");
84 if(var->val.ipv4.s_addr != 0)
85 {
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);
91 return(0);
92 }
bcb73bb3 93 if((pif = icswcstombs(confgetstr("net", "publicif"), NULL, NULL)) == NULL)
d3372da9 94 {
95 flog(LOG_ERR, "could not convert net.publicif into local charset: %s", strerror(errno));
96 return(-1);
97 }
98 if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
99 return(-1);
100 conf.ifc_buf = smalloc(conf.ifc_len = 65536);
101 if(ioctl(sock, SIOCGIFCONF, &conf) < 0)
102 {
103 free(conf.ifc_buf);
104 close(sock);
105 return(-1);
106 }
107 bufend = ((char *)conf.ifc_buf) + conf.ifc_len;
108 ipv4 = NULL;
109 for(ifr = conf.ifc_ifcu.ifcu_req; (void *)ifr < bufend; ifr++)
110 {
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)
114 {
115 free(conf.ifc_buf);
116 close(sock);
117 return(-1);
118 }
119 if(!(req.ifr_flags & IFF_UP))
120 continue;
121 if(ifr->ifr_addr.sa_family == AF_INET)
122 {
123 if(ntohl(((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr) == 0x7f000001)
124 continue;
125 if(ipv4 == NULL)
126 {
127 ipv4 = smalloc(sizeof(*ipv4));
128 memcpy(ipv4, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
129 } else {
130 free(ipv4);
bcb73bb3 131 free(conf.ifc_buf);
d3372da9 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... */
134 return(-1);
135 }
136 }
137 }
bcb73bb3 138 free(conf.ifc_buf);
d3372da9 139 close(sock);
140 if(ipv4 != NULL)
141 {
142 *addr = (struct sockaddr *)ipv4;
143 *lenbuf = sizeof(*ipv4);
144 return(0);
145 }
146 errno = ENETDOWN;
147 return(-1);
148 }
149 errno = EPFNOSUPPORT;
150 return(-1);
151}
152
153static struct socket *newsock(int type)
154{
155 struct socket *new;
156
157 new = smalloc(sizeof(*new));
158 new->refcount = 2;
159 new->fd = -1;
160 new->isrealsocket = 1;
161 new->family = -1;
162 new->tos = 0;
163 new->type = type;
164 new->state = -1;
165 new->ignread = 0;
166 new->close = 0;
167 new->remote = NULL;
168 new->remotelen = 0;
169 switch(type)
170 {
171 case SOCK_STREAM:
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;
178 break;
179 case SOCK_DGRAM:
180 new->outbuf.d.f = new->outbuf.d.l = NULL;
181 new->inbuf.d.f = new->inbuf.d.l = NULL;
182 break;
183 }
184 new->conncb = NULL;
185 new->errcb = NULL;
186 new->readcb = NULL;
187 new->writecb = NULL;
188 new->acceptcb = NULL;
189 new->next = sockets;
190 new->prev = NULL;
191 if(sockets != NULL)
192 sockets->prev = new;
193 sockets = new;
194 numsocks++;
195 return(new);
196}
197
198static struct socket *mksock(int domain, int type)
199{
200 int fd;
201 struct socket *new;
202
203 if((fd = socket(domain, type, 0)) < 0)
204 {
205 flog(LOG_CRIT, "could not create socket: %s", strerror(errno));
206 return(NULL);
207 }
208 new = newsock(type);
209 new->fd = fd;
210 new->family = domain;
211 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
212 return(new);
213}
214
215struct socket *wrapsock(int fd)
216{
217 struct socket *new;
218
219 new = newsock(SOCK_STREAM);
220 new->fd = fd;
221 new->state = SOCK_EST;
222 new->isrealsocket = 0;
223 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
224 return(new);
225}
226
227static void unlinksock(struct socket *sk)
228{
229 if(sk->prev != NULL)
230 sk->prev->next = sk->next;
231 if(sk->next != NULL)
232 sk->next->prev = sk->prev;
233 if(sk == sockets)
234 sockets = sk->next;
235 putsock(sk);
236 numsocks--;
237}
238
239void getsock(struct socket *sk)
240{
241 sk->refcount++;
242}
243
244void putsock(struct socket *sk)
245{
246 struct dgrambuf *buf;
247
248 if(--(sk->refcount) == 0)
249 {
250 switch(sk->type)
251 {
252 case SOCK_STREAM:
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);
257 break;
258 case SOCK_DGRAM:
259 while((buf = sk->outbuf.d.f) != NULL)
260 {
261 sk->outbuf.d.f = buf->next;
262 free(buf->data);
263 free(buf);
264 }
265 while((buf = sk->inbuf.d.f) != NULL)
266 {
267 sk->inbuf.d.f = buf->next;
268 free(buf->data);
269 free(buf);
270 }
271 break;
272 }
273 if(sk->fd >= 0)
274 close(sk->fd);
275 if(sk->remote != NULL)
276 free(sk->remote);
277 free(sk);
278 }
279}
280
336539c2 281void sockpushdata(struct socket *sk, void *buf, size_t size)
282{
283 switch(sk->type)
284 {
285 case SOCK_STREAM:
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;
290 break;
291 case SOCK_DGRAM:
292 /* XXX */
293 break;
294 }
295 return;
296}
297
d3372da9 298void *sockgetinbuf(struct socket *sk, size_t *size)
299{
300 void *buf;
301 struct dgrambuf *dbuf;
302
303 switch(sk->type)
304 {
305 case SOCK_STREAM:
306 if((sk->inbuf.s.buf == NULL) || (sk->inbuf.s.datasize == 0))
307 {
308 *size = 0;
309 return(NULL);
310 }
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;
315 return(buf);
316 case SOCK_DGRAM:
317 if((dbuf = sk->inbuf.d.f) == NULL)
318 return(NULL);
319 sk->inbuf.d.f = dbuf->next;
320 if(dbuf->next == NULL)
321 sk->inbuf.d.l = NULL;
322 buf = dbuf->data;
323 *size = dbuf->size;
324 free(dbuf->addr);
325 free(dbuf);
326 return(buf);
327 }
328 return(NULL);
329}
330
331static void sockrecv(struct socket *sk)
332{
333 int ret, inq;
334 struct dgrambuf *dbuf;
335
336 switch(sk->type)
337 {
338 case SOCK_STREAM:
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))
343 {
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);
347 inq = 2048;
348 }
349#else
350 inq = 2048;
351#endif
352 if(inq > 65536)
353 inq = 65536;
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);
356 if(ret < 0)
357 {
358 if((errno == EINTR) || (errno == EAGAIN))
359 return;
360 if(sk->errcb != NULL)
361 sk->errcb(sk, errno, sk->data);
362 closesock(sk);
363 return;
364 }
365 if(ret == 0)
366 {
367 if(sk->errcb != NULL)
368 sk->errcb(sk, 0, sk->data);
369 closesock(sk);
370 return;
371 }
372 sk->inbuf.s.datasize += ret;
373 if(sk->readcb != NULL)
374 sk->readcb(sk, sk->data);
375 break;
376 case SOCK_DGRAM:
377 if(ioctl(sk->fd, SIOCINQ, &inq))
378 {
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);
382 return;
383 }
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);
388 if(ret < 0)
389 {
390 free(dbuf->addr);
391 free(dbuf->data);
392 free(dbuf);
393 if((errno == EINTR) || (errno == EAGAIN))
394 return;
395 if(sk->errcb != NULL)
396 sk->errcb(sk, errno, sk->data);
397 closesock(sk);
398 return;
399 }
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. */
404 if(ret == 0)
405 {
406 free(dbuf->addr);
407 free(dbuf->data);
408 free(dbuf);
409 if(!((sk->family == AF_INET) || (sk->family == AF_INET6)))
410 {
411 if(sk->errcb != NULL)
412 sk->errcb(sk, 0, sk->data);
413 closesock(sk);
414 }
415 return;
416 }
417 dbuf->addr = srealloc(dbuf->addr, dbuf->addrlen);
418 dbuf->data = srealloc(dbuf->data, dbuf->size = ret);
419 dbuf->next = NULL;
420 if(sk->inbuf.d.l != NULL)
421 sk->inbuf.d.l->next = dbuf;
422 else
423 sk->inbuf.d.f = dbuf;
424 sk->inbuf.d.l = dbuf;
425 if(sk->readcb != NULL)
426 sk->readcb(sk, sk->data);
427 break;
428 }
429}
430
431static void sockflush(struct socket *sk)
432{
433 int ret;
434 struct dgrambuf *dbuf;
435
436 switch(sk->type)
437 {
438 case SOCK_STREAM:
439 if(sk->isrealsocket)
440 ret = send(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize, MSG_DONTWAIT | MSG_NOSIGNAL);
441 else
442 ret = write(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize);
443 if(ret < 0)
444 {
445 /* For now, assume transient error, since
446 * the socket is polled for errors */
447 break;
448 }
449 if(ret > 0)
450 {
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);
454 }
455 break;
456 case SOCK_DGRAM:
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);
461 free(dbuf->data);
462 free(dbuf->addr);
463 free(dbuf);
464 if(sk->writecb != NULL)
465 sk->writecb(sk, sk->data);
466 break;
467 }
468}
469
470void closesock(struct socket *sk)
471{
472 sk->state = SOCK_STL;
473 close(sk->fd);
474 sk->fd = -1;
475 sk->close = 0;
476}
477
478void sockqueue(struct socket *sk, void *data, size_t size)
479{
480 struct dgrambuf *new;
481
482 if(sk->state == SOCK_STL)
483 return;
484 switch(sk->type)
485 {
486 case SOCK_STREAM:
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;
490 break;
491 case SOCK_DGRAM:
492 if(sk->remote == NULL)
493 return;
494 new = smalloc(sizeof(*new));
495 new->next = NULL;
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)
499 {
500 sk->outbuf.d.l = sk->outbuf.d.f = new;
501 } else {
502 sk->outbuf.d.l->next = new;
503 sk->outbuf.d.l = new;
504 }
505 break;
506 }
507}
508
509size_t sockgetdatalen(struct socket *sk)
510{
511 struct dgrambuf *b;
512 size_t ret;
513
514 switch(sk->type)
515 {
516 case SOCK_STREAM:
517 ret = sk->inbuf.s.datasize;
518 break;
519 case SOCK_DGRAM:
520 ret = 0;
521 for(b = sk->inbuf.d.f; b != NULL; b = b->next)
522 ret += b->size;
523 break;
524 }
525 return(ret);
526}
527
528size_t sockqueuesize(struct socket *sk)
529{
530 struct dgrambuf *b;
531 size_t ret;
532
533 switch(sk->type)
534 {
535 case SOCK_STREAM:
536 ret = sk->outbuf.s.datasize;
537 break;
538 case SOCK_DGRAM:
539 ret = 0;
540 for(b = sk->outbuf.d.f; b != NULL; b = b->next)
541 ret += b->size;
542 break;
543 }
544 return(ret);
545}
546
547struct socket *netcslisten(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
548{
549 struct socket *sk;
347d6d76 550 int intbuf;
d3372da9 551
552 if(confgetint("net", "mode") == 1)
553 {
554 errno = EOPNOTSUPP;
555 return(NULL);
556 }
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.
562 */
563 if(confgetint("net", "mode") == 0)
564 {
565 if((sk = mksock(name->sa_family, type)) == NULL)
566 return(NULL);
567 sk->state = SOCK_LST;
347d6d76 568 if(confgetint("net", "reuseaddr"))
569 {
570 intbuf = 1;
571 setsockopt(sk->fd, SOL_SOCKET, SO_REUSEADDR, &intbuf, sizeof(intbuf));
572 }
d3372da9 573 if(bind(sk->fd, name, namelen) < 0)
574 {
575 putsock(sk);
576 return(NULL);
577 }
578 if(listen(sk->fd, 16) < 0)
579 {
580 putsock(sk);
581 return(NULL);
582 }
583 sk->acceptcb = func;
584 sk->data = data;
585 return(sk);
586 }
587 errno = EOPNOTSUPP;
588 return(NULL);
589}
590
591/*
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.
597*/
598
599struct socket *netcslistenlocal(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
600{
601 struct socket *sk;
66c517d2 602 int intbuf;
d3372da9 603
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.
609 */
610 if((sk = mksock(name->sa_family, type)) == NULL)
611 return(NULL);
612 sk->state = SOCK_LST;
687b2ee2 613 if(confgetint("net", "reuseaddr"))
614 {
615 intbuf = 1;
616 setsockopt(sk->fd, SOL_SOCKET, SO_REUSEADDR, &intbuf, sizeof(intbuf));
617 }
d3372da9 618 if(bind(sk->fd, name, namelen) < 0)
619 {
620 putsock(sk);
621 return(NULL);
622 }
623 if(listen(sk->fd, 16) < 0)
624 {
625 putsock(sk);
626 return(NULL);
627 }
628 sk->acceptcb = func;
629 sk->data = data;
630 return(sk);
631}
632
633struct socket *netcsdgram(struct sockaddr *name, socklen_t namelen)
634{
635 struct socket *sk;
636 int mode;
637
638 mode = confgetint("net", "mode");
639 if((mode == 0) || (mode == 1))
640 {
641 if((sk = mksock(name->sa_family, SOCK_DGRAM)) == NULL)
642 return(NULL);
643 if(bind(sk->fd, name, namelen) < 0)
644 {
645 putsock(sk);
646 return(NULL);
647 }
648 sk->state = SOCK_EST;
649 return(sk);
650 }
651 errno = EOPNOTSUPP;
652 return(NULL);
653}
654
655struct socket *netdupsock(struct socket *sk)
656{
657 struct socket *newsk;
658
659 newsk = newsock(sk->type);
660 if((newsk->fd = dup(sk->fd)) < 0)
661 {
662 flog(LOG_WARNING, "could not dup() socket: %s", strerror(errno));
663 putsock(newsk);
664 return(NULL);
665 }
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);
670 return(newsk);
671}
672
673void netdgramconn(struct socket *sk, struct sockaddr *addr, socklen_t addrlen)
674{
675 if(sk->remote != NULL)
676 free(sk->remote);
677 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
678 sk->ignread = 1;
679}
680
681struct socket *netcsconn(struct sockaddr *addr, socklen_t addrlen, void (*func)(struct socket *, int, void *), void *data)
682{
683 struct socket *sk;
684 int mode;
685
686 mode = confgetint("net", "mode");
687 if((mode == 0) || (mode == 1))
688 {
689 if((sk = mksock(addr->sa_family, SOCK_STREAM)) == NULL)
690 return(NULL);
691 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
692 if(!connect(sk->fd, addr, addrlen))
693 {
694 sk->state = SOCK_EST;
695 func(sk, 0, data);
696 return(sk);
697 }
698 if(errno == EINPROGRESS)
699 {
700 sk->state = SOCK_SYN;
701 sk->conncb = func;
702 sk->data = data;
703 return(sk);
704 }
705 putsock(sk);
706 return(NULL);
707 }
708 errno = EOPNOTSUPP;
709 return(NULL);
710}
711
712int pollsocks(int timeout)
713{
714 int i, num, ret, retlen;
715 int newfd;
716 struct pollfd *pfds;
717 struct socket *sk, *next, *newsk;
718 struct sockaddr_storage ss;
719 socklen_t sslen;
720
721 pfds = smalloc(sizeof(*pfds) * (num = numsocks));
722 for(i = 0, sk = sockets; i < num; sk = sk->next)
723 {
724 if(sk->state == SOCK_STL)
725 {
726 num--;
727 continue;
728 }
729 pfds[i].fd = sk->fd;
730 pfds[i].events = 0;
731 if(!sk->ignread)
732 pfds[i].events |= POLLIN;
733 if((sk->state == SOCK_SYN) || (sockqueuesize(sk) > 0))
734 pfds[i].events |= POLLOUT;
735 pfds[i].revents = 0;
736 i++;
737 }
738 ret = poll(pfds, num, timeout);
739 if(ret < 0)
740 {
741 if(errno != EINTR)
742 {
743 flog(LOG_CRIT, "pollsocks: poll errored out: %s", strerror(errno));
744 /* To avoid CPU hogging in case it's bad, which it
745 * probably is. */
746 sleep(1);
747 }
748 free(pfds);
749 return(1);
750 }
751 for(sk = sockets; sk != NULL; sk = next)
752 {
753 next = sk->next;
754 for(i = 0; i < num; i++)
755 {
756 if(pfds[i].fd == sk->fd)
757 break;
758 }
759 if(i == num)
760 continue;
761 switch(sk->state)
762 {
763 case SOCK_LST:
764 if(pfds[i].revents & POLLIN)
765 {
766 sslen = sizeof(ss);
767 if((newfd = accept(sk->fd, (struct sockaddr *)&ss, &sslen)) < 0)
768 {
769 if(sk->errcb != NULL)
770 sk->errcb(sk, errno, sk->data);
771 }
772 newsk = newsock(sk->type);
773 newsk->fd = newfd;
774 newsk->family = sk->family;
775 newsk->state = SOCK_EST;
776 memcpy(newsk->remote = smalloc(sslen), &ss, sslen);
777 newsk->remotelen = sslen;
778 putsock(newsk);
779 if(sk->acceptcb != NULL)
780 sk->acceptcb(sk, newsk, sk->data);
781 }
782 if(pfds[i].revents & POLLERR)
783 {
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);
788 continue;
789 }
790 break;
791 case SOCK_SYN:
792 if(pfds[i].revents & POLLERR)
793 {
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);
798 closesock(sk);
799 continue;
800 }
801 if(pfds[i].revents & (POLLIN | POLLOUT))
802 {
803 sk->state = SOCK_EST;
804 if(sk->conncb != NULL)
805 sk->conncb(sk, 0, sk->data);
806 }
807 break;
808 case SOCK_EST:
809 if(pfds[i].revents & POLLERR)
810 {
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);
815 closesock(sk);
816 continue;
817 }
818 if(pfds[i].revents & POLLIN)
819 sockrecv(sk);
820 if(pfds[i].revents & POLLOUT)
821 {
822 if(sockqueuesize(sk) > 0)
823 sockflush(sk);
824 }
825 break;
826 }
827 if(pfds[i].revents & POLLNVAL)
828 {
829 flog(LOG_CRIT, "BUG: stale socket struct on fd %i", sk->fd);
830 sk->state = SOCK_STL;
831 unlinksock(sk);
832 continue;
833 }
834 if(pfds[i].revents & POLLHUP)
835 {
836 if(sk->errcb != NULL)
837 sk->errcb(sk, 0, sk->data);
838 closesock(sk);
839 unlinksock(sk);
840 continue;
841 }
842 }
843 free(pfds);
844 for(sk = sockets; sk != NULL; sk = next)
845 {
846 next = sk->next;
847 if(sk->refcount == 1 && (sockqueuesize(sk) == 0))
848 {
849 unlinksock(sk);
850 continue;
851 }
852 if(sk->close && (sockqueuesize(sk) == 0))
853 closesock(sk);
854 if(sk->state == SOCK_STL)
855 {
856 unlinksock(sk);
857 continue;
858 }
859 }
860 return(1);
861}
862
863int socksettos(struct socket *sk, int tos)
864{
b020fb3d 865 int buf;
866
d3372da9 867 if(sk->family == AF_INET)
868 {
b020fb3d 869 switch(tos)
870 {
b198bed6 871 case 0:
872 buf = 0;
873 break;
b020fb3d 874 case SOCK_TOS_MINCOST:
18c1ae1d 875 buf = 0x02;
b020fb3d 876 break;
877 case SOCK_TOS_MAXREL:
18c1ae1d 878 buf = 0x04;
b020fb3d 879 break;
880 case SOCK_TOS_MAXTP:
18c1ae1d 881 buf = 0x08;
b020fb3d 882 break;
883 case SOCK_TOS_MINDELAY:
18c1ae1d 884 buf = 0x10;
b020fb3d 885 break;
886 default:
887 flog(LOG_WARNING, "attempted to set unknown TOS value %i to IPv4 sock", tos);
888 return(-1);
889 }
890 if(setsockopt(sk->fd, SOL_IP, IP_TOS, &buf, sizeof(buf)) < 0)
d3372da9 891 {
892 flog(LOG_WARNING, "could not set sock TOS to %i: %s", tos, strerror(errno));
893 return(-1);
894 }
895 return(0);
896 }
b020fb3d 897 if(sk->family == AF_INET6)
898 {
899 switch(tos)
900 {
b198bed6 901 case 0:
902 buf = 0;
b020fb3d 903 case SOCK_TOS_MINCOST:
904 buf = confgetint("net", "diffserv-mincost");
905 break;
906 case SOCK_TOS_MAXREL:
907 buf = confgetint("net", "diffserv-maxrel");
908 break;
909 case SOCK_TOS_MAXTP:
910 buf = confgetint("net", "diffserv-maxtp");
911 break;
912 case SOCK_TOS_MINDELAY:
913 buf = confgetint("net", "diffserv-mindelay");
914 break;
915 default:
916 flog(LOG_WARNING, "attempted to set unknown TOS value %i to IPv4 sock", tos);
917 return(-1);
918 }
919 /*
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.
922
923 if(setsockopt(...) < 0)
924 {
925 flog(LOG_WARNING, "could not set sock traffic class to %i: %s", tos, strerror(errno));
926 return(-1);
927 }
928 */
929 return(0);
930 }
d3372da9 931 flog(LOG_WARNING, "could not set TOS on sock of family %i", sk->family);
932 return(1);
933}
934
935struct resolvedata
936{
937 int fd;
938 void (*callback)(struct sockaddr *addr, int addrlen, void *data);
939 void *data;
940 struct sockaddr_storage addr;
941 int addrlen;
942};
943
944static void resolvecb(pid_t pid, int status, struct resolvedata *data)
945{
946 static char buf[80];
947 int ret;
948 struct sockaddr_in *ipv4;
949
950 if(!status)
951 {
952 if((ret = read(data->fd, buf, sizeof(buf))) != 4)
953 {
954 errno = ENONET;
955 data->callback(NULL, 0, data->data);
956 } else {
957 ipv4 = (struct sockaddr_in *)&data->addr;
958 memcpy(&ipv4->sin_addr, buf, 4);
959 data->callback((struct sockaddr *)ipv4, sizeof(*ipv4), data->data);
960 }
961 } else {
962 errno = ENONET;
963 data->callback(NULL, 0, data->data);
964 }
965 close(data->fd);
966 free(data);
967}
968
969int netresolve(char *addr, void (*callback)(struct sockaddr *addr, int addrlen, void *data), void *data)
970{
971 int i;
972 char *p;
973 int port;
974 int pfd[2];
975 pid_t child;
976 struct resolvedata *rdata;
977 struct sockaddr_in ipv4;
978 struct hostent *he;
979 sigset_t sigset;
980
981 /* IPv4 */
982 port = -1;
983 if((p = strchr(addr, ':')) != NULL)
984 {
985 *p = 0;
986 port = atoi(p + 1);
987 }
988 ipv4.sin_family = AF_INET;
989 ipv4.sin_port = htons(port);
990 if(inet_aton(addr, &ipv4.sin_addr))
991 {
992 callback((struct sockaddr *)&ipv4, sizeof(ipv4), data);
993 } else {
994 sigemptyset(&sigset);
995 sigaddset(&sigset, SIGCHLD);
996 sigprocmask(SIG_BLOCK, &sigset, NULL);
997 if((pipe(pfd) < 0) || ((child = fork()) < 0))
998 {
999 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1000 return(-1);
1001 }
1002 if(child == 0)
1003 {
1004 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1005 for(i = 3; i < FD_SETSIZE; i++)
1006 {
1007 if(i != pfd[1])
1008 close(i);
1009 }
1010 signal(SIGALRM, SIG_DFL);
1011 alarm(30);
1012 if((he = gethostbyname(addr)) == NULL)
1013 exit(1);
1014 write(pfd[1], he->h_addr_list[0], 4);
1015 exit(0);
1016 } else {
1017 close(pfd[1]);
1018 fcntl(pfd[0], F_SETFL, fcntl(pfd[0], F_GETFL) | O_NONBLOCK);
1019 rdata = smalloc(sizeof(*rdata));
1020 rdata->fd = pfd[0];
1021 rdata->callback = callback;
1022 rdata->data = data;
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);
1026 return(1);
1027 }
1028 }
1029 return(0);
1030}
1031
1032int sockgetlocalname(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
1033{
1034 socklen_t len;
1035 struct sockaddr_storage name;
1036
1037 *namebuf = NULL;
1038 if((sk->state == SOCK_STL) || (sk->fd < 0))
1039 return(-1);
1040 len = sizeof(name);
1041 if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0)
1042 {
1043 flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetlocalname");
1044 return(-1);
1045 }
1046 *namebuf = memcpy(smalloc(len), &name, len);
1047 *lenbuf = len;
1048 return(0);
1049}
1050
1051int sockgetremotename(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
1052{
1053 socklen_t len;
1054 struct sockaddr_storage name;
1055 struct sockaddr_in *ipv4;
1056 struct sockaddr *pname;
1057 socklen_t pnamelen;
1058
1059 switch(confgetint("net", "mode"))
1060 {
1061 case 0:
1062 *namebuf = NULL;
1063 if((sk->state == SOCK_STL) || (sk->fd < 0))
1064 return(-1);
1065 len = sizeof(name);
1066 if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0)
1067 {
1068 flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetremotename");
1069 return(-1);
1070 }
1071 if(name.ss_family == AF_INET)
1072 {
1073 ipv4 = (struct sockaddr_in *)&name;
1074 if(getpublicaddr(AF_INET, &pname, &pnamelen) < 0)
1075 {
1076 flog(LOG_WARNING, "could not determine public IP address - strange things may happen");
1077 return(-1);
1078 }
1079 ipv4->sin_addr.s_addr = ((struct sockaddr_in *)pname)->sin_addr.s_addr;
1080 free(pname);
1081 }
1082 *namebuf = memcpy(smalloc(len), &name, len);
1083 *lenbuf = len;
1084 return(0);
1085 case 1:
1086 errno = EOPNOTSUPP;
1087 return(-1);
1088 default:
1089 flog(LOG_CRIT, "unknown net mode %i active", confgetint("net", "mode"));
1090 errno = EOPNOTSUPP;
1091 return(-1);
1092 }
1093}
1094
99a28d47 1095int addreq(struct sockaddr *x, struct sockaddr *y)
1096{
1097 struct sockaddr_un *u1, *u2;
1098 struct sockaddr_in *n1, *n2;
1099#ifdef HAVE_IPV6
1100 struct sockaddr_in6 *s1, *s2;
1101#endif
1102
1103 if(x->sa_family != y->sa_family)
1104 return(0);
1105 switch(x->sa_family) {
1106 case AF_UNIX:
1107 u1 = (struct sockaddr_un *)x; u2 = (struct sockaddr_un *)y;
1108 if(strncmp(u1->sun_path, u2->sun_path, sizeof(u1->sun_path)))
1109 return(0);
1110 break;
1111 case AF_INET:
1112 n1 = (struct sockaddr_in *)x; n2 = (struct sockaddr_in *)y;
1113 if(n1->sin_port != n2->sin_port)
1114 return(0);
1115 if(n1->sin_addr.s_addr != n2->sin_addr.s_addr)
1116 return(0);
1117 break;
1118 case AF_INET6:
1119 s1 = (struct sockaddr_in6 *)x; s2 = (struct sockaddr_in6 *)y;
1120 if(s1->sin6_port != s2->sin6_port)
1121 return(0);
1122 if(memcmp(s1->sin6_addr.s6_addr, s2->sin6_addr.s6_addr, sizeof(s1->sin6_addr.s6_addr)))
1123 return(0);
1124 break;
1125 }
1126 return(1);
1127}
1128
d3372da9 1129char *formataddress(struct sockaddr *arg, socklen_t arglen)
1130{
1131 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1132 * lowercase letters to 1, so I do this
1133 * instead. */
1134 struct sockaddr_in *ipv4;
1135#ifdef HAVE_IPV6
1136 struct sockaddr_in6 *ipv6;
1137#endif
1138 static char *ret = NULL;
1139 char buf[1024];
1140
1141 if(ret != NULL)
1142 free(ret);
1143 ret = NULL;
1144 switch(arg->sa_family)
1145 {
1146 case AF_UNIX:
1147 UNIX = (struct sockaddr_un *)arg;
1148 ret = sprintf2("%s", UNIX->sun_path);
1149 break;
1150 case AF_INET:
1151 ipv4 = (struct sockaddr_in *)arg;
1152 if(inet_ntop(AF_INET, &ipv4->sin_addr, buf, sizeof(buf)) == NULL)
1153 return(NULL);
1154 ret = sprintf2("%s:%i", buf, (int)ntohs(ipv4->sin_port));
1155 break;
1156#ifdef HAVE_IPV6
1157 case AF_INET6:
1158 ipv6 = (struct sockaddr_in6 *)arg;
1159 if(inet_ntop(AF_INET6, &ipv6->sin6_addr, buf, sizeof(buf)) == NULL)
1160 return(NULL);
fc7f7735 1161 ret = sprintf2("[%s]:%i", buf, (int)ntohs(ipv6->sin6_port));
d3372da9 1162 break;
1163#endif
1164 default:
1165 errno = EPFNOSUPPORT;
1166 break;
1167 }
1168 return(ret);
1169}
1170
1171#if 0
1172
1173/*
1174 * It was very nice to use this, but it seems
1175 * to mess things up, so I guess it has to go... :-(
1176 */
1177
1178static int formataddress(FILE *stream, const struct printf_info *info, const void *const *args)
1179{
1180 struct sockaddr *arg;
1181 socklen_t arglen;
1182 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1183 * lowercase letters to 1, so I do this
1184 * instead. */
1185 struct sockaddr_in *ipv4;
1186 int ret;
1187
1188 arg = *(struct sockaddr **)(args[0]);
1189 arglen = *(socklen_t *)(args[1]);
1190 switch(arg->sa_family)
1191 {
1192 case AF_UNIX:
1193 UNIX = (struct sockaddr_un *)arg;
1194 ret = fprintf(stream, "%s", UNIX->sun_path);
1195 break;
1196 case AF_INET:
1197 ipv4 = (struct sockaddr_in *)arg;
1198 ret = fprintf(stream, "%s:%i", inet_ntoa(ipv4->sin_addr), (int)ntohs(ipv4->sin_port));
1199 break;
1200 default:
1201 ret = -1;
1202 errno = EPFNOSUPPORT;
1203 break;
1204 }
1205 return(ret);
1206}
1207
1208static int formataddress_arginfo(const struct printf_info *info, size_t n, int *argtypes)
1209{
1210 if(n > 0)
1211 argtypes[0] = PA_POINTER;
1212 if(n > 1)
1213 argtypes[1] = PA_INT; /* Sources tell me that socklen_t _must_
1214 * be an int, so I guess this should be
1215 * safe. */
1216 return(2);
1217}
1218#endif
1219
1220static int init(int hup)
1221{
1222 if(!hup)
1223 {
1224 /*
1225 if(register_printf_function('N', formataddress, formataddress_arginfo))
1226 {
1227 flog(LOG_CRIT, "could not register printf handler %%N: %s", strerror(errno));
1228 return(1);
1229 }
1230 */
1231 }
1232 return(0);
1233}
1234
1235static void terminate(void)
1236{
1237 while(sockets != NULL)
1238 unlinksock(sockets);
1239}
1240
1241static struct module me =
1242{
1243 .name = "net",
1244 .conf =
1245 {
1246 .vars = myvars
1247 },
1248 .init = init,
1249 .terminate = terminate
1250};
1251
1252MODULE(me)