New dc_connect implementation
[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>
7ab36fbd 36#include <sys/stat.h> /* For rebindunix() */
d3372da9 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{
d9f89ef5 52 /** The network mode to use. Currently supported values are 0 for
53 * active mode and 1 for passive mode. In the future, SOCKS5 proxy
54 * support may be added. */
d3372da9 55 {CONF_VAR_INT, "mode", {.num = 0}},
d9f89ef5 56 /** Set the SO_REUSEADDR socket option on listening sockets, so
57 * that dead TCP connections waiting for timeout are ignored. */
347d6d76 58 {CONF_VAR_BOOL, "reuseaddr", {.num = 0}},
d9f89ef5 59 /** Overrides the IPv4 address reported to other clients in active
60 * mode. Useful for servers behind NAT routers. If both this and
61 * net.publicif are unspecified the address of the hub connection
62 * is used. */
d3372da9 63 {CONF_VAR_IPV4, "visibleipv4", {.ipv4 = {0}}},
d9f89ef5 64 /** Specifies an interface name from which to fetch the IPv4
65 * address reported to other clients in active mode. If both this
66 * and net.visibleipv4 are unspecified the address of the hub
67 * connection is used. */
d3372da9 68 {CONF_VAR_STRING, "publicif", {.str = L""}},
b020fb3d 69 /* Diffserv should be supported on IPv4, too, but I don't know the
70 * API to do that. */
d9f89ef5 71 /** The Diffserv value to use on IPv6 connections when the
72 * minimize cost TOS value is used (see the TOS VALUES
73 * section). */
b020fb3d 74 {CONF_VAR_INT, "diffserv-mincost", {.num = 0}},
d9f89ef5 75 /** The Diffserv value to use on IPv6 connections when the
76 * maximize reliability TOS value is used (see the TOS VALUES
77 * section). */
b020fb3d 78 {CONF_VAR_INT, "diffserv-maxrel", {.num = 0}},
d9f89ef5 79 /** The Diffserv value to use on IPv6 connections when the
80 * maximize throughput TOS value is used (see the TOS VALUES
81 * section). */
b020fb3d 82 {CONF_VAR_INT, "diffserv-maxtp", {.num = 0}},
d9f89ef5 83 /** The Diffserv value to use on IPv6 connections when the
84 * minimize delay TOS value is used (see the TOS VALUES
85 * section). */
b020fb3d 86 {CONF_VAR_INT, "diffserv-mindelay", {.num = 0}},
d3372da9 87 {CONF_VAR_END}
88};
89
90static struct socket *sockets = NULL;
91int numsocks = 0;
92
93/* XXX: Get autoconf for all this... */
94int getpublicaddr(int af, struct sockaddr **addr, socklen_t *lenbuf)
95{
96 struct sockaddr_in *ipv4;
97 struct configvar *var;
98 void *bufend;
99 int sock;
100 struct ifconf conf;
101 struct ifreq *ifr, req;
102 char *pif;
103
104 if(af == AF_INET)
105 {
106 var = confgetvar("net", "visibleipv4");
107 if(var->val.ipv4.s_addr != 0)
108 {
109 ipv4 = smalloc(sizeof(*ipv4));
110 ipv4->sin_family = AF_INET;
111 ipv4->sin_addr.s_addr = var->val.ipv4.s_addr;
112 *addr = (struct sockaddr *)ipv4;
113 *lenbuf = sizeof(*ipv4);
114 return(0);
115 }
bcb73bb3 116 if((pif = icswcstombs(confgetstr("net", "publicif"), NULL, NULL)) == NULL)
d3372da9 117 {
118 flog(LOG_ERR, "could not convert net.publicif into local charset: %s", strerror(errno));
119 return(-1);
120 }
6fb0c6ac 121 if(!strcmp(pif, ""))
122 return(1);
d3372da9 123 if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
124 return(-1);
125 conf.ifc_buf = smalloc(conf.ifc_len = 65536);
126 if(ioctl(sock, SIOCGIFCONF, &conf) < 0)
127 {
128 free(conf.ifc_buf);
129 close(sock);
130 return(-1);
131 }
132 bufend = ((char *)conf.ifc_buf) + conf.ifc_len;
133 ipv4 = NULL;
134 for(ifr = conf.ifc_ifcu.ifcu_req; (void *)ifr < bufend; ifr++)
135 {
1315d02a 136 if(strcmp(ifr->ifr_name, pif))
6fb0c6ac 137 continue;
d3372da9 138 memset(&req, 0, sizeof(req));
139 memcpy(req.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name));
140 if(ioctl(sock, SIOCGIFFLAGS, &req) < 0)
6fb0c6ac 141 break;
142 if(!(req.ifr_flags & IFF_UP))
d3372da9 143 {
6fb0c6ac 144 flog(LOG_WARNING, "public interface is down");
145 break;
d3372da9 146 }
6fb0c6ac 147 if(ifr->ifr_addr.sa_family != AF_INET)
d3372da9 148 {
6fb0c6ac 149 flog(LOG_WARNING, "address of the public interface is not AF_INET");
150 break;
d3372da9 151 }
6fb0c6ac 152 ipv4 = smalloc(sizeof(*ipv4));
153 memcpy(ipv4, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
154 break;
d3372da9 155 }
bcb73bb3 156 free(conf.ifc_buf);
d3372da9 157 close(sock);
158 if(ipv4 != NULL)
159 {
160 *addr = (struct sockaddr *)ipv4;
161 *lenbuf = sizeof(*ipv4);
162 return(0);
163 }
164 errno = ENETDOWN;
165 return(-1);
166 }
6fb0c6ac 167 return(1);
d3372da9 168}
169
170static struct socket *newsock(int type)
171{
172 struct socket *new;
173
174 new = smalloc(sizeof(*new));
175 new->refcount = 2;
176 new->fd = -1;
177 new->isrealsocket = 1;
178 new->family = -1;
179 new->tos = 0;
180 new->type = type;
181 new->state = -1;
182 new->ignread = 0;
183 new->close = 0;
184 new->remote = NULL;
185 new->remotelen = 0;
186 switch(type)
187 {
188 case SOCK_STREAM:
189 new->outbuf.s.buf = NULL;
190 new->outbuf.s.bufsize = 0;
191 new->outbuf.s.datasize = 0;
192 new->inbuf.s.buf = NULL;
193 new->inbuf.s.bufsize = 0;
194 new->inbuf.s.datasize = 0;
195 break;
196 case SOCK_DGRAM:
197 new->outbuf.d.f = new->outbuf.d.l = NULL;
198 new->inbuf.d.f = new->inbuf.d.l = NULL;
199 break;
200 }
cab0b442 201 new->conncb = NULL;
202 new->errcb = NULL;
203 new->readcb = NULL;
204 new->writecb = NULL;
205 new->acceptcb = NULL;
d3372da9 206 new->next = sockets;
207 new->prev = NULL;
208 if(sockets != NULL)
209 sockets->prev = new;
210 sockets = new;
211 numsocks++;
212 return(new);
213}
214
215static struct socket *mksock(int domain, int type)
216{
217 int fd;
218 struct socket *new;
219
220 if((fd = socket(domain, type, 0)) < 0)
221 {
222 flog(LOG_CRIT, "could not create socket: %s", strerror(errno));
223 return(NULL);
224 }
225 new = newsock(type);
226 new->fd = fd;
227 new->family = domain;
228 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
229 return(new);
230}
231
232struct socket *wrapsock(int fd)
233{
234 struct socket *new;
235
236 new = newsock(SOCK_STREAM);
237 new->fd = fd;
238 new->state = SOCK_EST;
239 new->isrealsocket = 0;
240 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
241 return(new);
242}
243
244static void unlinksock(struct socket *sk)
245{
246 if(sk->prev != NULL)
247 sk->prev->next = sk->next;
248 if(sk->next != NULL)
249 sk->next->prev = sk->prev;
250 if(sk == sockets)
251 sockets = sk->next;
252 putsock(sk);
253 numsocks--;
254}
255
256void getsock(struct socket *sk)
257{
258 sk->refcount++;
259}
260
261void putsock(struct socket *sk)
262{
263 struct dgrambuf *buf;
264
265 if(--(sk->refcount) == 0)
266 {
267 switch(sk->type)
268 {
269 case SOCK_STREAM:
270 if(sk->outbuf.s.buf != NULL)
271 free(sk->outbuf.s.buf);
272 if(sk->inbuf.s.buf != NULL)
273 free(sk->inbuf.s.buf);
274 break;
275 case SOCK_DGRAM:
276 while((buf = sk->outbuf.d.f) != NULL)
277 {
278 sk->outbuf.d.f = buf->next;
279 free(buf->data);
161604ea 280 free(buf->addr);
d3372da9 281 free(buf);
282 }
283 while((buf = sk->inbuf.d.f) != NULL)
284 {
285 sk->inbuf.d.f = buf->next;
286 free(buf->data);
161604ea 287 free(buf->addr);
d3372da9 288 free(buf);
289 }
290 break;
291 }
b870805e 292 closesock(sk);
d3372da9 293 if(sk->remote != NULL)
294 free(sk->remote);
295 free(sk);
296 }
297}
298
336539c2 299void sockpushdata(struct socket *sk, void *buf, size_t size)
300{
301 switch(sk->type)
302 {
303 case SOCK_STREAM:
304 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + size, 1, 1);
305 memmove(sk->inbuf.s.buf + size, sk->inbuf.s.buf, sk->inbuf.s.datasize);
306 memcpy(sk->inbuf.s.buf, buf, size);
307 sk->inbuf.s.datasize += size;
308 break;
309 case SOCK_DGRAM:
310 /* XXX */
311 break;
312 }
313 return;
314}
315
d3372da9 316void *sockgetinbuf(struct socket *sk, size_t *size)
317{
318 void *buf;
319 struct dgrambuf *dbuf;
320
321 switch(sk->type)
322 {
323 case SOCK_STREAM:
324 if((sk->inbuf.s.buf == NULL) || (sk->inbuf.s.datasize == 0))
325 {
326 *size = 0;
327 return(NULL);
328 }
329 buf = sk->inbuf.s.buf;
330 *size = sk->inbuf.s.datasize;
331 sk->inbuf.s.buf = NULL;
332 sk->inbuf.s.bufsize = sk->inbuf.s.datasize = 0;
333 return(buf);
334 case SOCK_DGRAM:
335 if((dbuf = sk->inbuf.d.f) == NULL)
336 return(NULL);
337 sk->inbuf.d.f = dbuf->next;
338 if(dbuf->next == NULL)
339 sk->inbuf.d.l = NULL;
340 buf = dbuf->data;
341 *size = dbuf->size;
342 free(dbuf->addr);
343 free(dbuf);
344 return(buf);
345 }
346 return(NULL);
347}
348
349static void sockrecv(struct socket *sk)
350{
351 int ret, inq;
352 struct dgrambuf *dbuf;
353
354 switch(sk->type)
355 {
356 case SOCK_STREAM:
357#if defined(HAVE_LINUX_SOCKIOS_H) && defined(SIOCINQ)
358 /* SIOCINQ is Linux-specific AFAIK, but I really have no idea
359 * how to read the inqueue size on other OSs */
360 if(ioctl(sk->fd, SIOCINQ, &inq))
361 {
362 /* I don't really know what could go wrong here, so let's
363 * assume it's transient. */
364 flog(LOG_WARNING, "SIOCINQ return %s on socket %i, falling back to 2048 bytes", strerror(errno), sk->fd);
365 inq = 2048;
366 }
367#else
368 inq = 2048;
369#endif
370 if(inq > 65536)
371 inq = 65536;
372 sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + inq, 1, 1);
373 ret = read(sk->fd, sk->inbuf.s.buf + sk->inbuf.s.datasize, inq);
374 if(ret < 0)
375 {
376 if((errno == EINTR) || (errno == EAGAIN))
377 return;
cab0b442 378 if(sk->errcb != NULL)
379 sk->errcb(sk, errno, sk->data);
d3372da9 380 closesock(sk);
381 return;
382 }
383 if(ret == 0)
384 {
cab0b442 385 if(sk->errcb != NULL)
386 sk->errcb(sk, 0, sk->data);
d3372da9 387 closesock(sk);
388 return;
389 }
390 sk->inbuf.s.datasize += ret;
cab0b442 391 if(sk->readcb != NULL)
392 sk->readcb(sk, sk->data);
d3372da9 393 break;
394 case SOCK_DGRAM:
395 if(ioctl(sk->fd, SIOCINQ, &inq))
396 {
397 /* I don't really know what could go wrong here, so let's
398 * assume it's transient. */
399 flog(LOG_WARNING, "SIOCINQ return %s on socket %i", strerror(errno), sk->fd);
400 return;
401 }
402 dbuf = smalloc(sizeof(*dbuf));
403 dbuf->data = smalloc(inq);
404 dbuf->addr = smalloc(dbuf->addrlen = sizeof(struct sockaddr_storage));
405 ret = recvfrom(sk->fd, dbuf->data, inq, 0, dbuf->addr, &dbuf->addrlen);
406 if(ret < 0)
407 {
408 free(dbuf->addr);
409 free(dbuf->data);
410 free(dbuf);
411 if((errno == EINTR) || (errno == EAGAIN))
412 return;
cab0b442 413 if(sk->errcb != NULL)
414 sk->errcb(sk, errno, sk->data);
d3372da9 415 closesock(sk);
416 return;
417 }
418 /* On UDP/IPv[46], ret == 0 doesn't mean EOF (since UDP can't
419 * have EOF), but rather an empty packet. I don't know if any
420 * other potential DGRAM protocols might have an EOF
421 * condition, so let's play safe. */
422 if(ret == 0)
423 {
424 free(dbuf->addr);
425 free(dbuf->data);
426 free(dbuf);
427 if(!((sk->family == AF_INET) || (sk->family == AF_INET6)))
428 {
cab0b442 429 if(sk->errcb != NULL)
430 sk->errcb(sk, 0, sk->data);
d3372da9 431 closesock(sk);
432 }
433 return;
434 }
435 dbuf->addr = srealloc(dbuf->addr, dbuf->addrlen);
436 dbuf->data = srealloc(dbuf->data, dbuf->size = ret);
437 dbuf->next = NULL;
438 if(sk->inbuf.d.l != NULL)
439 sk->inbuf.d.l->next = dbuf;
440 else
441 sk->inbuf.d.f = dbuf;
442 sk->inbuf.d.l = dbuf;
cab0b442 443 if(sk->readcb != NULL)
444 sk->readcb(sk, sk->data);
d3372da9 445 break;
446 }
447}
448
449static void sockflush(struct socket *sk)
450{
451 int ret;
452 struct dgrambuf *dbuf;
453
454 switch(sk->type)
455 {
456 case SOCK_STREAM:
457 if(sk->isrealsocket)
458 ret = send(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize, MSG_DONTWAIT | MSG_NOSIGNAL);
459 else
460 ret = write(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize);
461 if(ret < 0)
462 {
463 /* For now, assume transient error, since
464 * the socket is polled for errors */
465 break;
466 }
467 if(ret > 0)
468 {
469 memmove(sk->outbuf.s.buf, ((char *)sk->outbuf.s.buf) + ret, sk->outbuf.s.datasize -= ret);
cab0b442 470 if(sk->writecb != NULL)
471 sk->writecb(sk, sk->data);
d3372da9 472 }
473 break;
474 case SOCK_DGRAM:
475 dbuf = sk->outbuf.d.f;
476 if((sk->outbuf.d.f = dbuf->next) == NULL)
477 sk->outbuf.d.l = NULL;
478 sendto(sk->fd, dbuf->data, dbuf->size, MSG_DONTWAIT | MSG_NOSIGNAL, dbuf->addr, dbuf->addrlen);
479 free(dbuf->data);
480 free(dbuf->addr);
481 free(dbuf);
cab0b442 482 if(sk->writecb != NULL)
483 sk->writecb(sk, sk->data);
d3372da9 484 break;
485 }
486}
487
488void closesock(struct socket *sk)
489{
b870805e 490 struct sockaddr_un *un;
491
492 if((sk->family == AF_UNIX) && !sockgetlocalname(sk, (struct sockaddr **)&un, NULL) && (un->sun_family == PF_UNIX))
493 {
7ab36fbd 494 if((sk->state == SOCK_LST) && strchr(un->sun_path, '/'))
b870805e 495 {
496 if(unlink(un->sun_path))
7ab36fbd 497 flog(LOG_WARNING, "could not unlink Unix socket %s: %s", un->sun_path, strerror(errno));
b870805e 498 }
499 }
d3372da9 500 sk->state = SOCK_STL;
501 close(sk->fd);
502 sk->fd = -1;
503 sk->close = 0;
504}
505
506void sockqueue(struct socket *sk, void *data, size_t size)
507{
508 struct dgrambuf *new;
509
510 if(sk->state == SOCK_STL)
511 return;
512 switch(sk->type)
513 {
514 case SOCK_STREAM:
515 sizebuf(&(sk->outbuf.s.buf), &(sk->outbuf.s.bufsize), sk->outbuf.s.datasize + size, 1, 1);
516 memcpy(sk->outbuf.s.buf + sk->outbuf.s.datasize, data, size);
517 sk->outbuf.s.datasize += size;
518 break;
519 case SOCK_DGRAM:
520 if(sk->remote == NULL)
521 return;
522 new = smalloc(sizeof(*new));
523 new->next = NULL;
524 memcpy(new->data = smalloc(size), data, new->size = size);
525 memcpy(new->addr = smalloc(sk->remotelen), sk->remote, new->addrlen = sk->remotelen);
526 if(sk->outbuf.d.l == NULL)
527 {
528 sk->outbuf.d.l = sk->outbuf.d.f = new;
529 } else {
530 sk->outbuf.d.l->next = new;
531 sk->outbuf.d.l = new;
532 }
533 break;
534 }
535}
536
537size_t sockgetdatalen(struct socket *sk)
538{
539 struct dgrambuf *b;
540 size_t ret;
541
542 switch(sk->type)
543 {
544 case SOCK_STREAM:
545 ret = sk->inbuf.s.datasize;
546 break;
547 case SOCK_DGRAM:
548 ret = 0;
549 for(b = sk->inbuf.d.f; b != NULL; b = b->next)
550 ret += b->size;
551 break;
552 }
553 return(ret);
554}
555
556size_t sockqueuesize(struct socket *sk)
557{
558 struct dgrambuf *b;
559 size_t ret;
560
561 switch(sk->type)
562 {
563 case SOCK_STREAM:
564 ret = sk->outbuf.s.datasize;
565 break;
566 case SOCK_DGRAM:
567 ret = 0;
568 for(b = sk->outbuf.d.f; b != NULL; b = b->next)
569 ret += b->size;
570 break;
571 }
572 return(ret);
573}
574
d3372da9 575/*
7ab36fbd 576 * Seriously, I don't know if it's naughty or not to remove
577 * pre-existing Unix sockets.
578 */
579static int rebindunix(struct socket *sk, struct sockaddr *name, socklen_t namelen)
580{
581 struct sockaddr_un *un;
582 struct stat sb;
583
584 if((sk->family != AF_UNIX) || (name->sa_family != PF_UNIX))
585 return(-1);
586 un = (struct sockaddr_un *)name;
587 if(stat(un->sun_path, &sb))
588 return(-1);
589 if(!S_ISSOCK(sb.st_mode))
590 return(-1);
591 if(unlink(un->sun_path))
592 return(-1);
593 if(bind(sk->fd, name, namelen) < 0)
594 return(-1);
595 return(0);
596}
597
598/*
d3372da9 599 * The difference between netcslisten() and netcslistenlocal() is that
600 * netcslistenlocal() always listens on the local host, instead of
601 * following proxy/passive mode directions. It is suitable for eg. the
602 * UI channel, while the file sharing networks should, naturally, use
603 * netcslisten() instead.
604*/
605
cab0b442 606struct socket *netcslistenlocal(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
d3372da9 607{
608 struct socket *sk;
66c517d2 609 int intbuf;
d3372da9 610
611 /* I don't know if this is actually correct (it probably isn't),
612 * but since, at on least Linux systems, PF_* are specifically
613 * #define'd to their AF_* counterparts, it allows for a severely
614 * smoother implementation. If it breaks something on your
615 * platform, please tell me so.
616 */
617 if((sk = mksock(name->sa_family, type)) == NULL)
618 return(NULL);
619 sk->state = SOCK_LST;
687b2ee2 620 if(confgetint("net", "reuseaddr"))
621 {
622 intbuf = 1;
623 setsockopt(sk->fd, SOL_SOCKET, SO_REUSEADDR, &intbuf, sizeof(intbuf));
624 }
7ab36fbd 625 if((bind(sk->fd, name, namelen) < 0) && ((errno != EADDRINUSE) || (rebindunix(sk, name, namelen) < 0)))
d3372da9 626 {
627 putsock(sk);
628 return(NULL);
629 }
630 if(listen(sk->fd, 16) < 0)
631 {
632 putsock(sk);
633 return(NULL);
634 }
cab0b442 635 sk->acceptcb = func;
636 sk->data = data;
d3372da9 637 return(sk);
638}
639
cab0b442 640struct socket *netcslisten(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data)
c23acc61 641{
642 if(confgetint("net", "mode") == 1)
643 {
644 errno = EOPNOTSUPP;
645 return(NULL);
646 }
c23acc61 647 if(confgetint("net", "mode") == 0)
648 return(netcslistenlocal(type, name, namelen, func, data));
649 errno = EOPNOTSUPP;
650 return(NULL);
651}
652
cab0b442 653struct socket *netcstcplisten(int port, int local, void (*func)(struct socket *, struct socket *, void *), void *data)
0a1bc5b1 654{
655 struct sockaddr_in addr;
656#ifdef HAVE_IPV6
657 struct sockaddr_in6 addr6;
658#endif
cab0b442 659 struct socket *(*csfunc)(int, struct sockaddr *, socklen_t, void (*)(struct socket *, struct socket *, void *), void *);
0a1bc5b1 660 struct socket *ret;
661
662 if(local)
663 csfunc = netcslistenlocal;
664 else
665 csfunc = netcslisten;
666#ifdef HAVE_IPV6
667 memset(&addr6, 0, sizeof(addr6));
668 addr6.sin6_family = AF_INET6;
669 addr6.sin6_port = htons(port);
670 addr6.sin6_addr = in6addr_any;
671 if((ret = csfunc(SOCK_STREAM, (struct sockaddr *)&addr6, sizeof(addr6), func, data)) != NULL)
672 return(ret);
673 if((ret == NULL) && (errno != EAFNOSUPPORT))
674 return(NULL);
675#endif
676 memset(&addr, 0, sizeof(addr));
677 addr.sin_family = AF_INET;
678 addr.sin_port = htons(port);
679 return(csfunc(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), func, data));
680}
681
d3372da9 682struct socket *netcsdgram(struct sockaddr *name, socklen_t namelen)
683{
684 struct socket *sk;
685 int mode;
686
687 mode = confgetint("net", "mode");
688 if((mode == 0) || (mode == 1))
689 {
690 if((sk = mksock(name->sa_family, SOCK_DGRAM)) == NULL)
691 return(NULL);
692 if(bind(sk->fd, name, namelen) < 0)
693 {
694 putsock(sk);
695 return(NULL);
696 }
697 sk->state = SOCK_EST;
698 return(sk);
699 }
700 errno = EOPNOTSUPP;
701 return(NULL);
702}
703
704struct socket *netdupsock(struct socket *sk)
705{
706 struct socket *newsk;
707
708 newsk = newsock(sk->type);
709 if((newsk->fd = dup(sk->fd)) < 0)
710 {
711 flog(LOG_WARNING, "could not dup() socket: %s", strerror(errno));
712 putsock(newsk);
713 return(NULL);
714 }
715 newsk->state = sk->state;
716 newsk->ignread = sk->ignread;
717 if(sk->remote != NULL)
718 memcpy(newsk->remote = smalloc(sk->remotelen), sk->remote, newsk->remotelen = sk->remotelen);
719 return(newsk);
720}
721
722void netdgramconn(struct socket *sk, struct sockaddr *addr, socklen_t addrlen)
723{
724 if(sk->remote != NULL)
725 free(sk->remote);
726 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
727 sk->ignread = 1;
728}
729
cab0b442 730struct socket *netcsconn(struct sockaddr *addr, socklen_t addrlen, void (*func)(struct socket *, int, void *), void *data)
d3372da9 731{
732 struct socket *sk;
733 int mode;
734
735 mode = confgetint("net", "mode");
736 if((mode == 0) || (mode == 1))
737 {
738 if((sk = mksock(addr->sa_family, SOCK_STREAM)) == NULL)
739 return(NULL);
740 memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen);
741 if(!connect(sk->fd, addr, addrlen))
742 {
743 sk->state = SOCK_EST;
744 func(sk, 0, data);
745 return(sk);
746 }
747 if(errno == EINPROGRESS)
748 {
749 sk->state = SOCK_SYN;
cab0b442 750 sk->conncb = func;
751 sk->data = data;
d3372da9 752 return(sk);
753 }
754 putsock(sk);
755 return(NULL);
756 }
757 errno = EOPNOTSUPP;
758 return(NULL);
759}
760
761int pollsocks(int timeout)
762{
eaa35963 763 int i, num, ret;
764 socklen_t retlen;
d3372da9 765 int newfd;
766 struct pollfd *pfds;
767 struct socket *sk, *next, *newsk;
768 struct sockaddr_storage ss;
769 socklen_t sslen;
770
771 pfds = smalloc(sizeof(*pfds) * (num = numsocks));
772 for(i = 0, sk = sockets; i < num; sk = sk->next)
773 {
774 if(sk->state == SOCK_STL)
775 {
776 num--;
777 continue;
778 }
779 pfds[i].fd = sk->fd;
780 pfds[i].events = 0;
781 if(!sk->ignread)
782 pfds[i].events |= POLLIN;
783 if((sk->state == SOCK_SYN) || (sockqueuesize(sk) > 0))
784 pfds[i].events |= POLLOUT;
785 pfds[i].revents = 0;
786 i++;
787 }
788 ret = poll(pfds, num, timeout);
789 if(ret < 0)
790 {
791 if(errno != EINTR)
792 {
793 flog(LOG_CRIT, "pollsocks: poll errored out: %s", strerror(errno));
794 /* To avoid CPU hogging in case it's bad, which it
795 * probably is. */
796 sleep(1);
797 }
798 free(pfds);
799 return(1);
800 }
801 for(sk = sockets; sk != NULL; sk = next)
802 {
803 next = sk->next;
804 for(i = 0; i < num; i++)
805 {
806 if(pfds[i].fd == sk->fd)
807 break;
808 }
809 if(i == num)
810 continue;
811 switch(sk->state)
812 {
813 case SOCK_LST:
814 if(pfds[i].revents & POLLIN)
815 {
816 sslen = sizeof(ss);
817 if((newfd = accept(sk->fd, (struct sockaddr *)&ss, &sslen)) < 0)
cab0b442 818 {
819 if(sk->errcb != NULL)
820 sk->errcb(sk, errno, sk->data);
821 }
d3372da9 822 newsk = newsock(sk->type);
823 newsk->fd = newfd;
824 newsk->family = sk->family;
825 newsk->state = SOCK_EST;
826 memcpy(newsk->remote = smalloc(sslen), &ss, sslen);
827 newsk->remotelen = sslen;
828 putsock(newsk);
cab0b442 829 if(sk->acceptcb != NULL)
830 sk->acceptcb(sk, newsk, sk->data);
d3372da9 831 }
832 if(pfds[i].revents & POLLERR)
833 {
834 retlen = sizeof(ret);
835 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
cab0b442 836 if(sk->errcb != NULL)
837 sk->errcb(sk, ret, sk->data);
d3372da9 838 continue;
839 }
840 break;
841 case SOCK_SYN:
842 if(pfds[i].revents & POLLERR)
843 {
844 retlen = sizeof(ret);
845 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
cab0b442 846 if(sk->conncb != NULL)
847 sk->conncb(sk, ret, sk->data);
d3372da9 848 closesock(sk);
849 continue;
850 }
851 if(pfds[i].revents & (POLLIN | POLLOUT))
852 {
853 sk->state = SOCK_EST;
cab0b442 854 if(sk->conncb != NULL)
855 sk->conncb(sk, 0, sk->data);
d3372da9 856 }
857 break;
858 case SOCK_EST:
859 if(pfds[i].revents & POLLERR)
860 {
861 retlen = sizeof(ret);
862 getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen);
cab0b442 863 if(sk->errcb != NULL)
864 sk->errcb(sk, ret, sk->data);
d3372da9 865 closesock(sk);
866 continue;
867 }
868 if(pfds[i].revents & POLLIN)
869 sockrecv(sk);
870 if(pfds[i].revents & POLLOUT)
871 {
872 if(sockqueuesize(sk) > 0)
873 sockflush(sk);
874 }
875 break;
876 }
877 if(pfds[i].revents & POLLNVAL)
878 {
879 flog(LOG_CRIT, "BUG: stale socket struct on fd %i", sk->fd);
880 sk->state = SOCK_STL;
881 unlinksock(sk);
882 continue;
883 }
884 if(pfds[i].revents & POLLHUP)
885 {
cab0b442 886 if(sk->errcb != NULL)
887 sk->errcb(sk, 0, sk->data);
d3372da9 888 closesock(sk);
889 unlinksock(sk);
890 continue;
891 }
892 }
893 free(pfds);
894 for(sk = sockets; sk != NULL; sk = next)
895 {
896 next = sk->next;
897 if(sk->refcount == 1 && (sockqueuesize(sk) == 0))
898 {
899 unlinksock(sk);
900 continue;
901 }
902 if(sk->close && (sockqueuesize(sk) == 0))
903 closesock(sk);
904 if(sk->state == SOCK_STL)
905 {
906 unlinksock(sk);
907 continue;
908 }
909 }
910 return(1);
911}
912
913int socksettos(struct socket *sk, int tos)
914{
b020fb3d 915 int buf;
916
7ab36fbd 917 if(sk->family == AF_UNIX)
918 return(0); /* Unix sockets are always perfect. :) */
d3372da9 919 if(sk->family == AF_INET)
920 {
b020fb3d 921 switch(tos)
922 {
b198bed6 923 case 0:
924 buf = 0;
925 break;
b020fb3d 926 case SOCK_TOS_MINCOST:
18c1ae1d 927 buf = 0x02;
b020fb3d 928 break;
929 case SOCK_TOS_MAXREL:
18c1ae1d 930 buf = 0x04;
b020fb3d 931 break;
932 case SOCK_TOS_MAXTP:
18c1ae1d 933 buf = 0x08;
b020fb3d 934 break;
935 case SOCK_TOS_MINDELAY:
18c1ae1d 936 buf = 0x10;
b020fb3d 937 break;
938 default:
939 flog(LOG_WARNING, "attempted to set unknown TOS value %i to IPv4 sock", tos);
940 return(-1);
941 }
942 if(setsockopt(sk->fd, SOL_IP, IP_TOS, &buf, sizeof(buf)) < 0)
d3372da9 943 {
944 flog(LOG_WARNING, "could not set sock TOS to %i: %s", tos, strerror(errno));
945 return(-1);
946 }
947 return(0);
948 }
b020fb3d 949 if(sk->family == AF_INET6)
950 {
951 switch(tos)
952 {
b198bed6 953 case 0:
954 buf = 0;
b020fb3d 955 case SOCK_TOS_MINCOST:
956 buf = confgetint("net", "diffserv-mincost");
957 break;
958 case SOCK_TOS_MAXREL:
959 buf = confgetint("net", "diffserv-maxrel");
960 break;
961 case SOCK_TOS_MAXTP:
962 buf = confgetint("net", "diffserv-maxtp");
963 break;
964 case SOCK_TOS_MINDELAY:
965 buf = confgetint("net", "diffserv-mindelay");
966 break;
967 default:
968 flog(LOG_WARNING, "attempted to set unknown TOS value %i to IPv4 sock", tos);
969 return(-1);
970 }
971 /*
972 On Linux, the API IPv6 flow label management doesn't seem to
973 be entirely complete, so I guess this will have to wait.
974
975 if(setsockopt(...) < 0)
976 {
977 flog(LOG_WARNING, "could not set sock traffic class to %i: %s", tos, strerror(errno));
978 return(-1);
979 }
980 */
981 return(0);
982 }
d3372da9 983 flog(LOG_WARNING, "could not set TOS on sock of family %i", sk->family);
984 return(1);
985}
986
987struct resolvedata
988{
989 int fd;
990 void (*callback)(struct sockaddr *addr, int addrlen, void *data);
991 void *data;
992 struct sockaddr_storage addr;
993 int addrlen;
994};
995
996static void resolvecb(pid_t pid, int status, struct resolvedata *data)
997{
998 static char buf[80];
999 int ret;
1000 struct sockaddr_in *ipv4;
1001
1002 if(!status)
1003 {
1004 if((ret = read(data->fd, buf, sizeof(buf))) != 4)
1005 {
1006 errno = ENONET;
1007 data->callback(NULL, 0, data->data);
1008 } else {
1009 ipv4 = (struct sockaddr_in *)&data->addr;
1010 memcpy(&ipv4->sin_addr, buf, 4);
1011 data->callback((struct sockaddr *)ipv4, sizeof(*ipv4), data->data);
1012 }
1013 } else {
1014 errno = ENONET;
1015 data->callback(NULL, 0, data->data);
1016 }
1017 close(data->fd);
1018 free(data);
1019}
1020
1021int netresolve(char *addr, void (*callback)(struct sockaddr *addr, int addrlen, void *data), void *data)
1022{
1023 int i;
1024 char *p;
1025 int port;
1026 int pfd[2];
1027 pid_t child;
1028 struct resolvedata *rdata;
1029 struct sockaddr_in ipv4;
1030 struct hostent *he;
1031 sigset_t sigset;
1032
1033 /* IPv4 */
1034 port = -1;
1035 if((p = strchr(addr, ':')) != NULL)
1036 {
1037 *p = 0;
1038 port = atoi(p + 1);
1039 }
1040 ipv4.sin_family = AF_INET;
1041 ipv4.sin_port = htons(port);
1042 if(inet_aton(addr, &ipv4.sin_addr))
1043 {
1044 callback((struct sockaddr *)&ipv4, sizeof(ipv4), data);
1045 } else {
1046 sigemptyset(&sigset);
1047 sigaddset(&sigset, SIGCHLD);
1048 sigprocmask(SIG_BLOCK, &sigset, NULL);
1049 if((pipe(pfd) < 0) || ((child = fork()) < 0))
1050 {
1051 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1052 return(-1);
1053 }
1054 if(child == 0)
1055 {
1056 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1057 for(i = 3; i < FD_SETSIZE; i++)
1058 {
1059 if(i != pfd[1])
1060 close(i);
1061 }
1062 signal(SIGALRM, SIG_DFL);
1063 alarm(30);
1064 if((he = gethostbyname(addr)) == NULL)
1065 exit(1);
1066 write(pfd[1], he->h_addr_list[0], 4);
1067 exit(0);
1068 } else {
1069 close(pfd[1]);
1070 fcntl(pfd[0], F_SETFL, fcntl(pfd[0], F_GETFL) | O_NONBLOCK);
1071 rdata = smalloc(sizeof(*rdata));
1072 rdata->fd = pfd[0];
1073 rdata->callback = callback;
1074 rdata->data = data;
1075 memcpy(&rdata->addr, &ipv4, rdata->addrlen = sizeof(ipv4));
1076 childcallback(child, (void (*)(pid_t, int, void *))resolvecb, rdata);
1077 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
1078 return(1);
1079 }
1080 }
1081 return(0);
1082}
1083
1084int sockgetlocalname(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
1085{
1086 socklen_t len;
1087 struct sockaddr_storage name;
1088
1089 *namebuf = NULL;
1090 if((sk->state == SOCK_STL) || (sk->fd < 0))
1091 return(-1);
1092 len = sizeof(name);
1093 if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0)
1094 {
6fb0c6ac 1095 flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetlocalname (%s)", strerror(errno));
d3372da9 1096 return(-1);
1097 }
1098 *namebuf = memcpy(smalloc(len), &name, len);
b870805e 1099 if(lenbuf != NULL)
1100 *lenbuf = len;
d3372da9 1101 return(0);
1102}
1103
eaa35963 1104static void sethostaddr(struct sockaddr *dst, struct sockaddr *src)
1105{
1106 if(dst->sa_family != src->sa_family)
1107 {
1108 flog(LOG_ERR, "BUG: non-matching socket families in sethostaddr (%i -> %i)", src->sa_family, dst->sa_family);
1109 return;
1110 }
1111 switch(src->sa_family)
1112 {
1113 case AF_INET:
1114 ((struct sockaddr_in *)dst)->sin_addr = ((struct sockaddr_in *)src)->sin_addr;
1115 break;
1116 case AF_INET6:
1117 ((struct sockaddr_in6 *)dst)->sin6_addr = ((struct sockaddr_in6 *)src)->sin6_addr;
1118 break;
1119 default:
1120 flog(LOG_WARNING, "sethostaddr unimplemented for family %i", src->sa_family);
1121 break;
1122 }
1123}
1124
1125static int makepublic(struct sockaddr *addr)
d3372da9 1126{
6fb0c6ac 1127 int ret;
eaa35963 1128 socklen_t plen;
1129 struct sockaddr *pname;
1130
1131 if((ret = getpublicaddr(addr->sa_family, &pname, &plen)) < 0)
1132 {
1133 flog(LOG_ERR, "could not get public address: %s", strerror(errno));
1134 return(-1);
1135 }
1136 if(ret)
1137 return(0);
1138 sethostaddr(addr, pname);
1139 free(pname);
1140 return(0);
1141}
1142
1143int sockgetremotename(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf)
1144{
d3372da9 1145 socklen_t len;
6fb0c6ac 1146 struct sockaddr *name;
d3372da9 1147
1148 switch(confgetint("net", "mode"))
1149 {
1150 case 0:
1151 *namebuf = NULL;
1152 if((sk->state == SOCK_STL) || (sk->fd < 0))
d3372da9 1153 {
eaa35963 1154 errno = EBADF;
d3372da9 1155 return(-1);
1156 }
6fb0c6ac 1157 if(!sockgetlocalname(sk, &name, &len))
1158 {
1159 *namebuf = name;
1160 *lenbuf = len;
eaa35963 1161 makepublic(name);
6fb0c6ac 1162 return(0);
1163 }
1164 flog(LOG_ERR, "could not get remotely accessible name by any means");
1165 return(-1);
d3372da9 1166 case 1:
1167 errno = EOPNOTSUPP;
1168 return(-1);
1169 default:
1170 flog(LOG_CRIT, "unknown net mode %i active", confgetint("net", "mode"));
1171 errno = EOPNOTSUPP;
1172 return(-1);
1173 }
1174}
1175
d29853b1 1176int sockgetremotename2(struct socket *sk, struct socket *sk2, struct sockaddr **namebuf, socklen_t *lenbuf)
1177{
1178 struct sockaddr *name1, *name2;
1179 socklen_t len1, len2;
1180
1181 if(sk->family != sk2->family)
1182 {
1183 flog(LOG_ERR, "using sockgetremotename2 with sockets of differing family: %i %i", sk->family, sk2->family);
1184 return(-1);
1185 }
3804379d 1186 if(sockgetremotename(sk, &name1, &len1))
d29853b1 1187 return(-1);
3804379d 1188 if(sockgetremotename(sk2, &name2, &len2)) {
d29853b1 1189 free(name1);
1190 return(-1);
1191 }
1192 sethostaddr(name1, name2);
1193 free(name2);
1194 *namebuf = name1;
1195 *lenbuf = len1;
1196 return(0);
1197}
1198
99a28d47 1199int addreq(struct sockaddr *x, struct sockaddr *y)
1200{
1201 struct sockaddr_un *u1, *u2;
1202 struct sockaddr_in *n1, *n2;
1203#ifdef HAVE_IPV6
1204 struct sockaddr_in6 *s1, *s2;
1205#endif
1206
1207 if(x->sa_family != y->sa_family)
1208 return(0);
1209 switch(x->sa_family) {
1210 case AF_UNIX:
1211 u1 = (struct sockaddr_un *)x; u2 = (struct sockaddr_un *)y;
1212 if(strncmp(u1->sun_path, u2->sun_path, sizeof(u1->sun_path)))
1213 return(0);
1214 break;
1215 case AF_INET:
1216 n1 = (struct sockaddr_in *)x; n2 = (struct sockaddr_in *)y;
1217 if(n1->sin_port != n2->sin_port)
1218 return(0);
1219 if(n1->sin_addr.s_addr != n2->sin_addr.s_addr)
1220 return(0);
1221 break;
c8a729d7 1222#ifdef HAVE_IPV6
99a28d47 1223 case AF_INET6:
1224 s1 = (struct sockaddr_in6 *)x; s2 = (struct sockaddr_in6 *)y;
1225 if(s1->sin6_port != s2->sin6_port)
1226 return(0);
1227 if(memcmp(s1->sin6_addr.s6_addr, s2->sin6_addr.s6_addr, sizeof(s1->sin6_addr.s6_addr)))
1228 return(0);
1229 break;
c8a729d7 1230#endif
99a28d47 1231 }
1232 return(1);
1233}
1234
d3372da9 1235char *formataddress(struct sockaddr *arg, socklen_t arglen)
1236{
1237 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1238 * lowercase letters to 1, so I do this
1239 * instead. */
1240 struct sockaddr_in *ipv4;
1241#ifdef HAVE_IPV6
1242 struct sockaddr_in6 *ipv6;
1243#endif
1244 static char *ret = NULL;
1245 char buf[1024];
1246
1247 if(ret != NULL)
1248 free(ret);
1249 ret = NULL;
1250 switch(arg->sa_family)
1251 {
1252 case AF_UNIX:
1253 UNIX = (struct sockaddr_un *)arg;
1254 ret = sprintf2("%s", UNIX->sun_path);
1255 break;
1256 case AF_INET:
1257 ipv4 = (struct sockaddr_in *)arg;
1258 if(inet_ntop(AF_INET, &ipv4->sin_addr, buf, sizeof(buf)) == NULL)
1259 return(NULL);
1260 ret = sprintf2("%s:%i", buf, (int)ntohs(ipv4->sin_port));
1261 break;
1262#ifdef HAVE_IPV6
1263 case AF_INET6:
1264 ipv6 = (struct sockaddr_in6 *)arg;
1265 if(inet_ntop(AF_INET6, &ipv6->sin6_addr, buf, sizeof(buf)) == NULL)
1266 return(NULL);
fc7f7735 1267 ret = sprintf2("[%s]:%i", buf, (int)ntohs(ipv6->sin6_port));
d3372da9 1268 break;
1269#endif
1270 default:
1271 errno = EPFNOSUPPORT;
1272 break;
1273 }
1274 return(ret);
1275}
1276
1277#if 0
1278
1279/*
1280 * It was very nice to use this, but it seems
1281 * to mess things up, so I guess it has to go... :-(
1282 */
1283
1284static int formataddress(FILE *stream, const struct printf_info *info, const void *const *args)
1285{
1286 struct sockaddr *arg;
1287 socklen_t arglen;
1288 struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
1289 * lowercase letters to 1, so I do this
1290 * instead. */
1291 struct sockaddr_in *ipv4;
1292 int ret;
1293
1294 arg = *(struct sockaddr **)(args[0]);
1295 arglen = *(socklen_t *)(args[1]);
1296 switch(arg->sa_family)
1297 {
1298 case AF_UNIX:
1299 UNIX = (struct sockaddr_un *)arg;
1300 ret = fprintf(stream, "%s", UNIX->sun_path);
1301 break;
1302 case AF_INET:
1303 ipv4 = (struct sockaddr_in *)arg;
1304 ret = fprintf(stream, "%s:%i", inet_ntoa(ipv4->sin_addr), (int)ntohs(ipv4->sin_port));
1305 break;
1306 default:
1307 ret = -1;
1308 errno = EPFNOSUPPORT;
1309 break;
1310 }
1311 return(ret);
1312}
1313
1314static int formataddress_arginfo(const struct printf_info *info, size_t n, int *argtypes)
1315{
1316 if(n > 0)
1317 argtypes[0] = PA_POINTER;
1318 if(n > 1)
1319 argtypes[1] = PA_INT; /* Sources tell me that socklen_t _must_
1320 * be an int, so I guess this should be
1321 * safe. */
1322 return(2);
1323}
1324#endif
1325
1326static int init(int hup)
1327{
1328 if(!hup)
1329 {
1330 /*
1331 if(register_printf_function('N', formataddress, formataddress_arginfo))
1332 {
1333 flog(LOG_CRIT, "could not register printf handler %%N: %s", strerror(errno));
1334 return(1);
1335 }
1336 */
1337 }
1338 return(0);
1339}
1340
1341static void terminate(void)
1342{
1343 while(sockets != NULL)
1344 unlinksock(sockets);
1345}
1346
1347static struct module me =
1348{
1349 .name = "net",
1350 .conf =
1351 {
1352 .vars = myvars
1353 },
1354 .init = init,
1355 .terminate = terminate
1356};
1357
1358MODULE(me)