Allow setting SO_REUSEADDR.
[doldaconnect.git] / daemon / net.c
... / ...
CommitLineData
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}},
54 {CONF_VAR_BOOL, "reuseaddr", {.num = 0}},
55 /* Only for direct mode */
56 {CONF_VAR_IPV4, "visibleipv4", {.ipv4 = {0}}},
57 {CONF_VAR_STRING, "publicif", {.str = L""}},
58 {CONF_VAR_END}
59};
60
61static struct socket *sockets = NULL;
62int numsocks = 0;
63
64/* XXX: Get autoconf for all this... */
65int getpublicaddr(int af, struct sockaddr **addr, socklen_t *lenbuf)
66{
67 struct sockaddr_in *ipv4;
68 struct configvar *var;
69 void *bufend;
70 int sock;
71 struct ifconf conf;
72 struct ifreq *ifr, req;
73 char *pif;
74
75 if(af == AF_INET)
76 {
77 var = confgetvar("net", "visibleipv4");
78 if(var->val.ipv4.s_addr != 0)
79 {
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);
85 return(0);
86 }
87 if((pif = icwcstombs(confgetstr("net", "publicif"), NULL)) == NULL)
88 {
89 flog(LOG_ERR, "could not convert net.publicif into local charset: %s", strerror(errno));
90 return(-1);
91 }
92 if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
93 return(-1);
94 conf.ifc_buf = smalloc(conf.ifc_len = 65536);
95 if(ioctl(sock, SIOCGIFCONF, &conf) < 0)
96 {
97 free(conf.ifc_buf);
98 close(sock);
99 return(-1);
100 }
101 bufend = ((char *)conf.ifc_buf) + conf.ifc_len;
102 ipv4 = NULL;
103 for(ifr = conf.ifc_ifcu.ifcu_req; (void *)ifr < bufend; ifr++)
104 {
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)
108 {
109 free(conf.ifc_buf);
110 close(sock);
111 return(-1);
112 }
113 if(!(req.ifr_flags & IFF_UP))
114 continue;
115 if(ifr->ifr_addr.sa_family == AF_INET)
116 {
117 if(ntohl(((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr) == 0x7f000001)
118 continue;
119 if(ipv4 == NULL)
120 {
121 ipv4 = smalloc(sizeof(*ipv4));
122 memcpy(ipv4, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
123 } else {
124 free(ipv4);
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... */
127 return(-1);
128 }
129 }
130 }
131 close(sock);
132 if(ipv4 != NULL)
133 {
134 *addr = (struct sockaddr *)ipv4;
135 *lenbuf = sizeof(*ipv4);
136 return(0);
137 }
138 errno = ENETDOWN;
139 return(-1);
140 }
141 errno = EPFNOSUPPORT;
142 return(-1);
143}
144
145static struct socket *newsock(int type)
146{
147 struct socket *new;
148
149 new = smalloc(sizeof(*new));
150 new->refcount = 2;
151 new->fd = -1;
152 new->isrealsocket = 1;
153 new->family = -1;
154 new->tos = 0;
155 new->type = type;
156 new->state = -1;
157 new->ignread = 0;
158 new->close = 0;
159 new->remote = NULL;
160 new->remotelen = 0;
161 switch(type)
162 {
163 case SOCK_STREAM:
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;
170 break;
171 case SOCK_DGRAM:
172 new->outbuf.d.f = new->outbuf.d.l = NULL;
173 new->inbuf.d.f = new->inbuf.d.l = NULL;
174 break;
175 }
176 new->conncb = NULL;
177 new->errcb = NULL;
178 new->readcb = NULL;
179 new->writecb = NULL;
180 new->acceptcb = NULL;
181 new->next = sockets;
182 new->prev = NULL;
183 if(sockets != NULL)
184 sockets->prev = new;
185 sockets = new;
186 numsocks++;
187 return(new);
188}
189
190static struct socket *mksock(int domain, int type)
191{
192 int fd;
193 struct socket *new;
194
195 if((fd = socket(domain, type, 0)) < 0)
196 {
197 flog(LOG_CRIT, "could not create socket: %s", strerror(errno));
198 return(NULL);
199 }
200 new = newsock(type);
201 new->fd = fd;
202 new->family = domain;
203 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
204 return(new);
205}
206
207struct socket *wrapsock(int fd)
208{
209 struct socket *new;
210
211 new = newsock(SOCK_STREAM);
212 new->fd = fd;
213 new->state = SOCK_EST;
214 new->isrealsocket = 0;
215 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
216 return(new);
217}
218
219static void unlinksock(struct socket *sk)
220{
221 if(sk->prev != NULL)
222 sk->prev->next = sk->next;
223 if(sk->next != NULL)
224 sk->next->prev = sk->prev;
225 if(sk == sockets)
226 sockets = sk->next;
227 putsock(sk);
228 numsocks--;
229}
230
231void getsock(struct socket *sk)
232{
233 sk->refcount++;
234}
235
236void putsock(struct socket *sk)
237{
238 struct dgrambuf *buf;
239
240 if(--(sk->refcount) == 0)
241 {
242 switch(sk->type)
243 {
244 case SOCK_STREAM:
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);
249 break;
250 case SOCK_DGRAM:
251 while((buf = sk->outbuf.d.f) != NULL)
252 {
253 sk->outbuf.d.f = buf->next;
254 free(buf->data);
255 free(buf);
256 }
257 while((buf = sk->inbuf.d.f) != NULL)
258 {
259 sk->inbuf.d.f = buf->next;
260 free(buf->data);
261 free(buf);
262 }
263 break;
264 }
265 if(sk->fd >= 0)
266 close(sk->fd);
267 if(sk->remote != NULL)
268 free(sk->remote);
269 free(sk);
270 }
271}
272
273void *sockgetinbuf(struct socket *sk, size_t *size)
274{
275 void *buf;
276 struct dgrambuf *dbuf;
277
278 switch(sk->type)
279 {
280 case SOCK_STREAM:
281 if((sk->inbuf.s.buf == NULL) || (sk->inbuf.s.datasize == 0))
282 {
283 *size = 0;
284 return(NULL);
285 }
286 buf = sk->inbuf.s.buf;
287 *size = sk->inbuf.s.datasize;
288 sk->inbuf.s.buf = NULL;
289 sk->inbuf.s.bufsize = sk->inbuf.s.datasize = 0;
290 return(buf);
291 case SOCK_DGRAM:
292 if((dbuf = sk->inbuf.d.f) == NULL)
293 return(NULL);
294 sk->inbuf.d.f = dbuf->next;
295 if(dbuf->next == NULL)
296 sk->inbuf.d.l = NULL;
297 buf = dbuf->data;
298 *size = dbuf->size;
299 free(dbuf->addr);
300 free(dbuf);
301 return(buf);
302 }
303 return(NULL);
304}
305
306static void sockrecv(struct socket *sk)
307{
308 int ret, inq;
309 struct dgrambuf *dbuf;
310
311 switch(sk->type)
312 {
313 case SOCK_STREAM:
314#if defined(HAVE_LINUX_SOCKIOS_H) && defined(SIOCINQ)
315 /* SIOCINQ is Linux-specific AFAIK, but I really have no idea
316 * how to read the inqueue size on other OSs */
317 if(ioctl(sk->fd, SIOCINQ, &inq))
318 {
319 /* I don't really know what could go wrong here, so let's
320 * assume it's transient. */
321 flog(LOG_WARNING, "SIOCINQ return %s on socket %i, falling back to 2048 bytes", strerror(errno), sk->fd);
322 inq = 2048;
323 }
324#else
325 inq = 2048;
326#endif
327 if(inq > 65536)
328 inq = 65536;
329 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + inq, 1, 1);
330 ret = read(sk->fd, sk->inbuf.s.buf + sk->inbuf.s.datasize, inq);
331 if(ret < 0)
332 {
333 if((errno == EINTR) || (errno == EAGAIN))
334 return;
335 if(sk->errcb != NULL)
336 sk->errcb(sk, errno, sk->data);
337 closesock(sk);
338 return;
339 }
340 if(ret == 0)
341 {
342 if(sk->errcb != NULL)
343 sk->errcb(sk, 0, sk->data);
344 closesock(sk);
345 return;
346 }
347 sk->inbuf.s.datasize += ret;
348 if(sk->readcb != NULL)
349 sk->readcb(sk, sk->data);
350 break;
351 case SOCK_DGRAM:
352 if(ioctl(sk->fd, SIOCINQ, &inq))
353 {
354 /* I don't really know what could go wrong here, so let's
355 * assume it's transient. */
356 flog(LOG_WARNING, "SIOCINQ return %s on socket %i", strerror(errno), sk->fd);
357 return;
358 }
359 dbuf = smalloc(sizeof(*dbuf));
360 dbuf->data = smalloc(inq);
361 dbuf->addr = smalloc(dbuf->addrlen = sizeof(struct sockaddr_storage));
362 ret = recvfrom(sk->fd, dbuf->data, inq, 0, dbuf->addr, &dbuf->addrlen);
363 if(ret < 0)
364 {
365 free(dbuf->addr);
366 free(dbuf->data);
367 free(dbuf);
368 if((errno == EINTR) || (errno == EAGAIN))
369 return;
370 if(sk->errcb != NULL)
371 sk->errcb(sk, errno, sk->data);
372 closesock(sk);
373 return;
374 }
375 /* On UDP/IPv[46], ret == 0 doesn't mean EOF (since UDP can't
376 * have EOF), but rather an empty packet. I don't know if any
377 * other potential DGRAM protocols might have an EOF
378 * condition, so let's play safe. */
379 if(ret == 0)
380 {
381 free(dbuf->addr);
382 free(dbuf->data);
383 free(dbuf);
384 if(!((sk->family == AF_INET) || (sk->family == AF_INET6)))
385 {
386 if(sk->errcb != NULL)
387 sk->errcb(sk, 0, sk->data);
388 closesock(sk);
389 }
390 return;
391 }
392 dbuf->addr = srealloc(dbuf->addr, dbuf->addrlen);
393 dbuf->data = srealloc(dbuf->data, dbuf->size = ret);
394 dbuf->next = NULL;
395 if(sk->inbuf.d.l != NULL)
396 sk->inbuf.d.l->next = dbuf;
397 else
398 sk->inbuf.d.f = dbuf;
399 sk->inbuf.d.l = dbuf;
400 if(sk->readcb != NULL)
401 sk->readcb(sk, sk->data);
402 break;
403 }
404}
405
406static void sockflush(struct socket *sk)
407{
408 int ret;
409 struct dgrambuf *dbuf;
410
411 switch(sk->type)
412 {
413 case SOCK_STREAM:
414 if(sk->isrealsocket)
415 ret = send(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize, MSG_DONTWAIT | MSG_NOSIGNAL);
416 else
417 ret = write(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize);
418 if(ret < 0)
419 {
420 /* For now, assume transient error, since
421 * the socket is polled for errors */
422 break;
423 }
424 if(ret > 0)
425 {
426 memmove(sk->outbuf.s.buf, ((char *)sk->outbuf.s.buf) + ret, sk->outbuf.s.datasize -= ret);
427 if(sk->writecb != NULL)
428 sk->writecb(sk, sk->data);
429 }
430 break;
431 case SOCK_DGRAM:
432 dbuf = sk->outbuf.d.f;
433 if((sk->outbuf.d.f = dbuf->next) == NULL)
434 sk->outbuf.d.l = NULL;
435 sendto(sk->fd, dbuf->data, dbuf->size, MSG_DONTWAIT | MSG_NOSIGNAL, dbuf->addr, dbuf->addrlen);
436 free(dbuf->data);
437 free(dbuf->addr);
438 free(dbuf);
439 if(sk->writecb != NULL)
440 sk->writecb(sk, sk->data);
441 break;
442 }
443}
444
445void closesock(struct socket *sk)
446{
447 sk->state = SOCK_STL;
448 close(sk->fd);
449 sk->fd = -1;
450 sk->close = 0;
451}
452
453void sockqueue(struct socket *sk, void *data, size_t size)
454{
455 struct dgrambuf *new;
456
457 if(sk->state == SOCK_STL)
458 return;
459 switch(sk->type)
460 {
461 case SOCK_STREAM:
462 sizebuf(&(sk->outbuf.s.buf), &(sk->outbuf.s.bufsize), sk->outbuf.s.datasize + size, 1, 1);
463 memcpy(sk->outbuf.s.buf + sk->outbuf.s.datasize, data, size);
464 sk->outbuf.s.datasize += size;
465 break;
466 case SOCK_DGRAM:
467 if(sk->remote == NULL)
468 return;
469 new = smalloc(sizeof(*new));
470 new->next = NULL;
471 memcpy(new->data = smalloc(size), data, new->size = size);
472 memcpy(new->addr = smalloc(sk->remotelen), sk->remote, new->addrlen = sk->remotelen);
473 if(sk->outbuf.d.l == NULL)
474 {
475 sk->outbuf.d.l = sk->outbuf.d.f = new;
476 } else {
477 sk->outbuf.d.l->next = new;
478 sk->outbuf.d.l = new;
479 }
480 break;
481 }
482}
483
484size_t sockgetdatalen(struct socket *sk)
485{
486 struct dgrambuf *b;
487 size_t ret;
488
489 switch(sk->type)
490 {
491 case SOCK_STREAM:
492 ret = sk->inbuf.s.datasize;
493 break;
494 case SOCK_DGRAM:
495 ret = 0;
496 for(b = sk->inbuf.d.f; b != NULL; b = b->next)
497 ret += b->size;
498 break;
499 }
500 return(ret);
501}
502
503size_t sockqueuesize(struct socket *sk)
504{
505 struct dgrambuf *b;
506 size_t ret;
507
508 switch(sk->type)
509 {
510 case SOCK_STREAM:
511 ret = sk->outbuf.s.datasize;
512 break;
513 case SOCK_DGRAM:
514 ret = 0;
515 for(b = sk->outbuf.d.f; b != NULL; b = b->next)
516 ret += b->size;
517 break;
518 }
519 return(ret);
520}
521
522struct socket *netcslisten(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
523{
524 struct socket *sk;
525 int intbuf;
526
527 if(confgetint("net", "mode") == 1)
528 {
529 errno = EOPNOTSUPP;
530 return(NULL);
531 }
532 /* I don't know if this is actually correct (it probably isn't),
533 * but since, at on least Linux systems, PF_* are specifically
534 * #define'd to their AF_* counterparts, it allows for a severely
535 * smoother implementation. If it breaks something on your
536 * platform, please tell me so.
537 */
538 if(confgetint("net", "mode") == 0)
539 {
540 if((sk = mksock(name->sa_family, type)) == NULL)
541 return(NULL);
542 sk->state = SOCK_LST;
543 if(confgetint("net", "reuseaddr"))
544 {
545 intbuf = 1;
546 setsockopt(sk->fd, SOL_SOCKET, SO_REUSEADDR, &intbuf, sizeof(intbuf));
547 }
548 if(bind(sk->fd, name, namelen) < 0)
549 {
550 putsock(sk);
551 return(NULL);
552 }
553 if(listen(sk->fd, 16) < 0)
554 {
555 putsock(sk);
556 return(NULL);
557 }
558 sk->acceptcb = func;
559 sk->data = data;
560 return(sk);
561 }
562 errno = EOPNOTSUPP;
563 return(NULL);
564}
565
566/*
567 * The difference between netcslisten() and netcslistenlocal() is that
568 * netcslistenlocal() always listens on the local host, instead of
569 * following proxy/passive mode directions. It is suitable for eg. the
570 * UI channel, while the file sharing networks should, naturally, use
571 * netcslisten() instead.
572*/
573
574struct socket *netcslistenlocal(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
575{
576 struct socket *sk;
577
578 /* I don't know if this is actually correct (it probably isn't),
579 * but since, at on least Linux systems, PF_* are specifically
580 * #define'd to their AF_* counterparts, it allows for a severely
581 * smoother implementation. If it breaks something on your
582 * platform, please tell me so.
583 */
584 if((sk = mksock(name->sa_family, type)) == NULL)
585 return(NULL);
586 sk->state = SOCK_LST;
587 if(bind(sk->fd, name, namelen) < 0)
588 {
589 putsock(sk);
590 return(NULL);
591 }
592 if(listen(sk->fd, 16) < 0)
593 {
594 putsock(sk);
595 return(NULL);
596 }
597 sk->acceptcb = func;
598 sk->data = data;
599 return(sk);
600}
601
602struct socket *netcsdgram(struct sockaddr *name, socklen_t namelen)
603{
604 struct socket *sk;
605 int mode;
606
607 mode = confgetint("net", "mode");
608 if((mode == 0) || (mode == 1))
609 {
610 if((sk = mksock(name->sa_family, SOCK_DGRAM)) == NULL)
611 return(NULL);
612 if(bind(sk->fd, name, namelen) < 0)
613 {
614 putsock(sk);
615 return(NULL);
616 }
617 sk->state = SOCK_EST;
618 return(sk);
619 }
620 errno = EOPNOTSUPP;
621 return(NULL);
622}
623
624struct socket *netdupsock(struct socket *sk)
625{
626 struct socket *newsk;
627
628 newsk = newsock(sk->type);
629 if((newsk->fd = dup(sk->fd)) < 0)
630 {
631 flog(LOG_WARNING, "could not dup() socket: %s", strerror(errno));
632 putsock(newsk);
633 return(NULL);
634 }
635 newsk->state = sk->state;
636 newsk->ignread = sk->ignread;
637 if(sk->remote != NULL)
638 memcpy(newsk->remote = smalloc(sk->remotelen), sk->remote, newsk->remotelen = sk->remotelen);
639 return(newsk);
640}
641
642void netdgramconn(struct socket *sk, struct sockaddr *addr, socklen_t addrlen)
643{
644 if(sk->remote != NULL)
645 free(sk->remote);
646 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
647 sk->ignread = 1;
648}
649
650struct socket *netcsconn(struct sockaddr *addr, socklen_t addrlen, void (*func)(struct socket *, int, void *), void *data)
651{
652 struct socket *sk;
653 int mode;
654
655 mode = confgetint("net", "mode");
656 if((mode == 0) || (mode == 1))
657 {
658 if((sk = mksock(addr->sa_family, SOCK_STREAM)) == NULL)
659 return(NULL);
660 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
661 if(!connect(sk->fd, addr, addrlen))
662 {
663 sk->state = SOCK_EST;
664 func(sk, 0, data);
665 return(sk);
666 }
667 if(errno == EINPROGRESS)
668 {
669 sk->state = SOCK_SYN;
670 sk->conncb = func;
671 sk->data = data;
672 return(sk);
673 }
674 putsock(sk);
675 return(NULL);
676 }
677 errno = EOPNOTSUPP;
678 return(NULL);
679}
680
681int pollsocks(int timeout)
682{
683 int i, num, ret, retlen;
684 int newfd;
685 struct pollfd *pfds;
686 struct socket *sk, *next, *newsk;
687 struct sockaddr_storage ss;
688 socklen_t sslen;
689
690 pfds = smalloc(sizeof(*pfds) * (num = numsocks));
691 for(i = 0, sk = sockets; i < num; sk = sk->next)
692 {
693 if(sk->state == SOCK_STL)
694 {
695 num--;
696 continue;
697 }
698 pfds[i].fd = sk->fd;
699 pfds[i].events = 0;
700 if(!sk->ignread)
701 pfds[i].events |= POLLIN;
702 if((sk->state == SOCK_SYN) || (sockqueuesize(sk) > 0))
703 pfds[i].events |= POLLOUT;
704 pfds[i].revents = 0;
705 i++;
706 }
707 ret = poll(pfds, num, timeout);
708 if(ret < 0)
709 {
710 if(errno != EINTR)
711 {
712 flog(LOG_CRIT, "pollsocks: poll errored out: %s", strerror(errno));
713 /* To avoid CPU hogging in case it's bad, which it
714 * probably is. */
715 sleep(1);
716 }
717 free(pfds);
718 return(1);
719 }
720 for(sk = sockets; sk != NULL; sk = next)
721 {
722 next = sk->next;
723 for(i = 0; i < num; i++)
724 {
725 if(pfds[i].fd == sk->fd)
726 break;
727 }
728 if(i == num)
729 continue;
730 switch(sk->state)
731 {
732 case SOCK_LST:
733 if(pfds[i].revents & POLLIN)
734 {
735 sslen = sizeof(ss);
736 if((newfd = accept(sk->fd, (struct sockaddr *)&ss, &sslen)) < 0)
737 {
738 if(sk->errcb != NULL)
739 sk->errcb(sk, errno, sk->data);
740 }
741 newsk = newsock(sk->type);
742 newsk->fd = newfd;
743 newsk->family = sk->family;
744 newsk->state = SOCK_EST;
745 memcpy(newsk->remote = smalloc(sslen), &ss, sslen);
746 newsk->remotelen = sslen;
747 putsock(newsk);
748 if(sk->acceptcb != NULL)
749 sk->acceptcb(sk, newsk, sk->data);
750 }
751 if(pfds[i].revents & POLLERR)
752 {
753 retlen = sizeof(ret);
754 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
755 if(sk->errcb != NULL)
756 sk->errcb(sk, ret, sk->data);
757 continue;
758 }
759 break;
760 case SOCK_SYN:
761 if(pfds[i].revents & POLLERR)
762 {
763 retlen = sizeof(ret);
764 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
765 if(sk->conncb != NULL)
766 sk->conncb(sk, ret, sk->data);
767 closesock(sk);
768 continue;
769 }
770 if(pfds[i].revents & (POLLIN | POLLOUT))
771 {
772 sk->state = SOCK_EST;
773 if(sk->conncb != NULL)
774 sk->conncb(sk, 0, sk->data);
775 }
776 break;
777 case SOCK_EST:
778 if(pfds[i].revents & POLLERR)
779 {
780 retlen = sizeof(ret);
781 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
782 if(sk->errcb != NULL)
783 sk->errcb(sk, ret, sk->data);
784 closesock(sk);
785 continue;
786 }
787 if(pfds[i].revents & POLLIN)
788 sockrecv(sk);
789 if(pfds[i].revents & POLLOUT)
790 {
791 if(sockqueuesize(sk) > 0)
792 sockflush(sk);
793 }
794 break;
795 }
796 if(pfds[i].revents & POLLNVAL)
797 {
798 flog(LOG_CRIT, "BUG: stale socket struct on fd %i", sk->fd);
799 sk->state = SOCK_STL;
800 unlinksock(sk);
801 continue;
802 }
803 if(pfds[i].revents & POLLHUP)
804 {
805 if(sk->errcb != NULL)
806 sk->errcb(sk, 0, sk->data);
807 closesock(sk);
808 unlinksock(sk);
809 continue;
810 }
811 }
812 free(pfds);
813 for(sk = sockets; sk != NULL; sk = next)
814 {
815 next = sk->next;
816 if(sk->refcount == 1 && (sockqueuesize(sk) == 0))
817 {
818 unlinksock(sk);
819 continue;
820 }
821 if(sk->close && (sockqueuesize(sk) == 0))
822 closesock(sk);
823 if(sk->state == SOCK_STL)
824 {
825 unlinksock(sk);
826 continue;
827 }
828 }
829 return(1);
830}
831
832int socksettos(struct socket *sk, int tos)
833{
834 if(sk->family == AF_INET)
835 {
836 if(setsockopt(sk->fd, SOL_IP, IP_TOS, &tos, sizeof(tos)) < 0)
837 {
838 flog(LOG_WARNING, "could not set sock TOS to %i: %s", tos, strerror(errno));
839 return(-1);
840 }
841 return(0);
842 }
843 /* XXX: How does the IPv6 traffic class work? */
844 flog(LOG_WARNING, "could not set TOS on sock of family %i", sk->family);
845 return(1);
846}
847
848struct resolvedata
849{
850 int fd;
851 void (*callback)(struct sockaddr *addr, int addrlen, void *data);
852 void *data;
853 struct sockaddr_storage addr;
854 int addrlen;
855};
856
857static void resolvecb(pid_t pid, int status, struct resolvedata *data)
858{
859 static char buf[80];
860 int ret;
861 struct sockaddr_in *ipv4;
862
863 if(!status)
864 {
865 if((ret = read(data->fd, buf, sizeof(buf))) != 4)
866 {
867 errno = ENONET;
868 data->callback(NULL, 0, data->data);
869 } else {
870 ipv4 = (struct sockaddr_in *)&data->addr;
871 memcpy(&ipv4->sin_addr, buf, 4);
872 data->callback((struct sockaddr *)ipv4, sizeof(*ipv4), data->data);
873 }
874 } else {
875 errno = ENONET;
876 data->callback(NULL, 0, data->data);
877 }
878 close(data->fd);
879 free(data);
880}
881
882int netresolve(char *addr, void (*callback)(struct sockaddr *addr, int addrlen, void *data), void *data)
883{
884 int i;
885 char *p;
886 int port;
887 int pfd[2];
888 pid_t child;
889 struct resolvedata *rdata;
890 struct sockaddr_in ipv4;
891 struct hostent *he;
892 sigset_t sigset;
893
894 /* IPv4 */
895 port = -1;
896 if((p = strchr(addr, ':')) != NULL)
897 {
898 *p = 0;
899 port = atoi(p + 1);
900 }
901 ipv4.sin_family = AF_INET;
902 ipv4.sin_port = htons(port);
903 if(inet_aton(addr, &ipv4.sin_addr))
904 {
905 callback((struct sockaddr *)&ipv4, sizeof(ipv4), data);
906 } else {
907 sigemptyset(&sigset);
908 sigaddset(&sigset, SIGCHLD);
909 sigprocmask(SIG_BLOCK, &sigset, NULL);
910 if((pipe(pfd) < 0) || ((child = fork()) < 0))
911 {
912 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
913 return(-1);
914 }
915 if(child == 0)
916 {
917 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
918 for(i = 3; i < FD_SETSIZE; i++)
919 {
920 if(i != pfd[1])
921 close(i);
922 }
923 signal(SIGALRM, SIG_DFL);
924 alarm(30);
925 if((he = gethostbyname(addr)) == NULL)
926 exit(1);
927 write(pfd[1], he->h_addr_list[0], 4);
928 exit(0);
929 } else {
930 close(pfd[1]);
931 fcntl(pfd[0], F_SETFL, fcntl(pfd[0], F_GETFL) | O_NONBLOCK);
932 rdata = smalloc(sizeof(*rdata));
933 rdata->fd = pfd[0];
934 rdata->callback = callback;
935 rdata->data = data;
936 memcpy(&rdata->addr, &ipv4, rdata->addrlen = sizeof(ipv4));
937 childcallback(child, (void (*)(pid_t, int, void *))resolvecb, rdata);
938 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
939 return(1);
940 }
941 }
942 return(0);
943}
944
945int sockgetlocalname(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
946{
947 socklen_t len;
948 struct sockaddr_storage name;
949
950 *namebuf = NULL;
951 if((sk->state == SOCK_STL) || (sk->fd < 0))
952 return(-1);
953 len = sizeof(name);
954 if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0)
955 {
956 flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetlocalname");
957 return(-1);
958 }
959 *namebuf = memcpy(smalloc(len), &name, len);
960 *lenbuf = len;
961 return(0);
962}
963
964int sockgetremotename(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
965{
966 socklen_t len;
967 struct sockaddr_storage name;
968 struct sockaddr_in *ipv4;
969 struct sockaddr *pname;
970 socklen_t pnamelen;
971
972 switch(confgetint("net", "mode"))
973 {
974 case 0:
975 *namebuf = NULL;
976 if((sk->state == SOCK_STL) || (sk->fd < 0))
977 return(-1);
978 len = sizeof(name);
979 if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0)
980 {
981 flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetremotename");
982 return(-1);
983 }
984 if(name.ss_family == AF_INET)
985 {
986 ipv4 = (struct sockaddr_in *)&name;
987 if(getpublicaddr(AF_INET, &pname, &pnamelen) < 0)
988 {
989 flog(LOG_WARNING, "could not determine public IP address - strange things may happen");
990 return(-1);
991 }
992 ipv4->sin_addr.s_addr = ((struct sockaddr_in *)pname)->sin_addr.s_addr;
993 free(pname);
994 }
995 *namebuf = memcpy(smalloc(len), &name, len);
996 *lenbuf = len;
997 return(0);
998 case 1:
999 errno = EOPNOTSUPP;
1000 return(-1);
1001 default:
1002 flog(LOG_CRIT, "unknown net mode %i active", confgetint("net", "mode"));
1003 errno = EOPNOTSUPP;
1004 return(-1);
1005 }
1006}
1007
1008char *formataddress(struct sockaddr *arg, socklen_t arglen)
1009{
1010 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1011 * lowercase letters to 1, so I do this
1012 * instead. */
1013 struct sockaddr_in *ipv4;
1014#ifdef HAVE_IPV6
1015 struct sockaddr_in6 *ipv6;
1016#endif
1017 static char *ret = NULL;
1018 char buf[1024];
1019
1020 if(ret != NULL)
1021 free(ret);
1022 ret = NULL;
1023 switch(arg->sa_family)
1024 {
1025 case AF_UNIX:
1026 UNIX = (struct sockaddr_un *)arg;
1027 ret = sprintf2("%s", UNIX->sun_path);
1028 break;
1029 case AF_INET:
1030 ipv4 = (struct sockaddr_in *)arg;
1031 if(inet_ntop(AF_INET, &ipv4->sin_addr, buf, sizeof(buf)) == NULL)
1032 return(NULL);
1033 ret = sprintf2("%s:%i", buf, (int)ntohs(ipv4->sin_port));
1034 break;
1035#ifdef HAVE_IPV6
1036 case AF_INET6:
1037 ipv6 = (struct sockaddr_in6 *)arg;
1038 if(inet_ntop(AF_INET6, &ipv6->sin6_addr, buf, sizeof(buf)) == NULL)
1039 return(NULL);
1040 ret = sprintf2("%s:%i", buf, (int)ntohs(ipv6->sin6_port));
1041 break;
1042#endif
1043 default:
1044 errno = EPFNOSUPPORT;
1045 break;
1046 }
1047 return(ret);
1048}
1049
1050#if 0
1051
1052/*
1053 * It was very nice to use this, but it seems
1054 * to mess things up, so I guess it has to go... :-(
1055 */
1056
1057static int formataddress(FILE *stream, const struct printf_info *info, const void *const *args)
1058{
1059 struct sockaddr *arg;
1060 socklen_t arglen;
1061 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1062 * lowercase letters to 1, so I do this
1063 * instead. */
1064 struct sockaddr_in *ipv4;
1065 int ret;
1066
1067 arg = *(struct sockaddr **)(args[0]);
1068 arglen = *(socklen_t *)(args[1]);
1069 switch(arg->sa_family)
1070 {
1071 case AF_UNIX:
1072 UNIX = (struct sockaddr_un *)arg;
1073 ret = fprintf(stream, "%s", UNIX->sun_path);
1074 break;
1075 case AF_INET:
1076 ipv4 = (struct sockaddr_in *)arg;
1077 ret = fprintf(stream, "%s:%i", inet_ntoa(ipv4->sin_addr), (int)ntohs(ipv4->sin_port));
1078 break;
1079 default:
1080 ret = -1;
1081 errno = EPFNOSUPPORT;
1082 break;
1083 }
1084 return(ret);
1085}
1086
1087static int formataddress_arginfo(const struct printf_info *info, size_t n, int *argtypes)
1088{
1089 if(n > 0)
1090 argtypes[0] = PA_POINTER;
1091 if(n > 1)
1092 argtypes[1] = PA_INT; /* Sources tell me that socklen_t _must_
1093 * be an int, so I guess this should be
1094 * safe. */
1095 return(2);
1096}
1097#endif
1098
1099static int init(int hup)
1100{
1101 if(!hup)
1102 {
1103 /*
1104 if(register_printf_function('N', formataddress, formataddress_arginfo))
1105 {
1106 flog(LOG_CRIT, "could not register printf handler %%N: %s", strerror(errno));
1107 return(1);
1108 }
1109 */
1110 }
1111 return(0);
1112}
1113
1114static void terminate(void)
1115{
1116 while(sockets != NULL)
1117 unlinksock(sockets);
1118}
1119
1120static struct module me =
1121{
1122 .name = "net",
1123 .conf =
1124 {
1125 .vars = myvars
1126 },
1127 .init = init,
1128 .terminate = terminate
1129};
1130
1131MODULE(me)