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