2 * services/listen_dnsport.c - listen on port 53 for incoming DNS queries.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This file has functions to get queries from clients.
42 #ifdef HAVE_SYS_TYPES_H
43 # include <sys/types.h>
46 #include "services/listen_dnsport.h"
47 #include "services/outside_network.h"
48 #include "util/netevent.h"
50 #include "util/config_file.h"
51 #include "util/net_help.h"
52 #include "sldns/sbuffer.h"
63 /** number of queued TCP connections for listen() */
64 #define TCP_BACKLOG 256
67 * Debug print of the getaddrinfo returned address.
68 * @param addr: the address returned.
71 verbose_print_addr(struct addrinfo *addr)
73 if(verbosity >= VERB_ALGO) {
75 void* sinaddr = &((struct sockaddr_in*)addr->ai_addr)->sin_addr;
77 if(addr->ai_family == AF_INET6)
78 sinaddr = &((struct sockaddr_in6*)addr->ai_addr)->
81 if(inet_ntop(addr->ai_family, sinaddr, buf,
82 (socklen_t)sizeof(buf)) == 0) {
83 (void)strlcpy(buf, "(null)", sizeof(buf));
85 buf[sizeof(buf)-1] = 0;
86 verbose(VERB_ALGO, "creating %s%s socket %s %d",
87 addr->ai_socktype==SOCK_DGRAM?"udp":
88 addr->ai_socktype==SOCK_STREAM?"tcp":"otherproto",
89 addr->ai_family==AF_INET?"4":
90 addr->ai_family==AF_INET6?"6":
92 ntohs(((struct sockaddr_in*)addr->ai_addr)->sin_port));
97 create_udp_sock(int family, int socktype, struct sockaddr* addr,
98 socklen_t addrlen, int v6only, int* inuse, int* noproto,
99 int rcv, int snd, int listen, int* reuseport, int transparent)
102 #if defined(SO_REUSEADDR) || defined(SO_REUSEPORT) || defined(IPV6_USE_MIN_MTU) || defined(IP_TRANSPARENT) || defined(IP_BINDANY)
106 int mtu = IPV6_MIN_MTU;
108 #if !defined(SO_RCVBUFFORCE) && !defined(SO_RCVBUF)
111 #if !defined(SO_SNDBUFFORCE) && !defined(SO_SNDBUF)
117 #if !defined(IP_TRANSPARENT) && !defined(IP_BINDANY)
120 if((s = socket(family, socktype, 0)) == -1) {
123 if(errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
127 log_err("can't create socket: %s", strerror(errno));
129 if(WSAGetLastError() == WSAEAFNOSUPPORT ||
130 WSAGetLastError() == WSAEPROTONOSUPPORT) {
134 log_err("can't create socket: %s",
135 wsa_strerror(WSAGetLastError()));
142 if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*)&on,
143 (socklen_t)sizeof(on)) < 0) {
145 log_err("setsockopt(.. SO_REUSEADDR ..) failed: %s",
147 if(errno != ENOSYS) {
154 log_err("setsockopt(.. SO_REUSEADDR ..) failed: %s",
155 wsa_strerror(WSAGetLastError()));
162 #endif /* SO_REUSEADDR */
164 /* try to set SO_REUSEPORT so that incoming
165 * queries are distributed evenly among the receiving threads.
166 * Each thread must have its own socket bound to the same port,
167 * with SO_REUSEPORT set on each socket.
169 if (reuseport && *reuseport &&
170 setsockopt(s, SOL_SOCKET, SO_REUSEPORT, (void*)&on,
171 (socklen_t)sizeof(on)) < 0) {
173 if(errno != ENOPROTOOPT || verbosity >= 3)
174 log_warn("setsockopt(.. SO_REUSEPORT ..) failed: %s",
177 /* this option is not essential, we can continue */
182 #endif /* defined(SO_REUSEPORT) */
183 #ifdef IP_TRANSPARENT
185 setsockopt(s, IPPROTO_IP, IP_TRANSPARENT, (void*)&on,
186 (socklen_t)sizeof(on)) < 0) {
187 log_warn("setsockopt(.. IP_TRANSPARENT ..) failed: %s",
190 #elif defined(IP_BINDANY)
192 setsockopt(s, (family==AF_INET6? IPPROTO_IPV6:IPPROTO_IP),
193 IP_BINDANY, (void*)&on, (socklen_t)sizeof(on)) < 0) {
194 log_warn("setsockopt(.. IP_BINDANY ..) failed: %s",
197 #endif /* IP_TRANSPARENT || IP_BINDANY */
202 socklen_t slen = (socklen_t)sizeof(got);
203 # ifdef SO_RCVBUFFORCE
204 /* Linux specific: try to use root permission to override
205 * system limits on rcvbuf. The limit is stored in
206 * /proc/sys/net/core/rmem_max or sysctl net.core.rmem_max */
207 if(setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, (void*)&rcv,
208 (socklen_t)sizeof(rcv)) < 0) {
211 log_err("setsockopt(..., SO_RCVBUFFORCE, "
212 "...) failed: %s", strerror(errno));
215 log_err("setsockopt(..., SO_RCVBUFFORCE, "
217 wsa_strerror(WSAGetLastError()));
224 # endif /* SO_RCVBUFFORCE */
225 if(setsockopt(s, SOL_SOCKET, SO_RCVBUF, (void*)&rcv,
226 (socklen_t)sizeof(rcv)) < 0) {
228 log_err("setsockopt(..., SO_RCVBUF, "
229 "...) failed: %s", strerror(errno));
232 log_err("setsockopt(..., SO_RCVBUF, "
234 wsa_strerror(WSAGetLastError()));
241 /* check if we got the right thing or if system
242 * reduced to some system max. Warn if so */
243 if(getsockopt(s, SOL_SOCKET, SO_RCVBUF, (void*)&got,
244 &slen) >= 0 && got < rcv/2) {
245 log_warn("so-rcvbuf %u was not granted. "
246 "Got %u. To fix: start with "
247 "root permissions(linux) or sysctl "
248 "bigger net.core.rmem_max(linux) or "
249 "kern.ipc.maxsockbuf(bsd) values.",
250 (unsigned)rcv, (unsigned)got);
252 # ifdef SO_RCVBUFFORCE
255 #endif /* SO_RCVBUF */
257 /* first do RCVBUF as the receive buffer is more important */
261 socklen_t slen = (socklen_t)sizeof(got);
262 # ifdef SO_SNDBUFFORCE
263 /* Linux specific: try to use root permission to override
264 * system limits on sndbuf. The limit is stored in
265 * /proc/sys/net/core/wmem_max or sysctl net.core.wmem_max */
266 if(setsockopt(s, SOL_SOCKET, SO_SNDBUFFORCE, (void*)&snd,
267 (socklen_t)sizeof(snd)) < 0) {
270 log_err("setsockopt(..., SO_SNDBUFFORCE, "
271 "...) failed: %s", strerror(errno));
274 log_err("setsockopt(..., SO_SNDBUFFORCE, "
276 wsa_strerror(WSAGetLastError()));
283 # endif /* SO_SNDBUFFORCE */
284 if(setsockopt(s, SOL_SOCKET, SO_SNDBUF, (void*)&snd,
285 (socklen_t)sizeof(snd)) < 0) {
287 log_err("setsockopt(..., SO_SNDBUF, "
288 "...) failed: %s", strerror(errno));
291 log_err("setsockopt(..., SO_SNDBUF, "
293 wsa_strerror(WSAGetLastError()));
300 /* check if we got the right thing or if system
301 * reduced to some system max. Warn if so */
302 if(getsockopt(s, SOL_SOCKET, SO_SNDBUF, (void*)&got,
303 &slen) >= 0 && got < snd/2) {
304 log_warn("so-sndbuf %u was not granted. "
305 "Got %u. To fix: start with "
306 "root permissions(linux) or sysctl "
307 "bigger net.core.wmem_max(linux) or "
308 "kern.ipc.maxsockbuf(bsd) values.",
309 (unsigned)snd, (unsigned)got);
311 # ifdef SO_SNDBUFFORCE
314 #endif /* SO_SNDBUF */
316 if(family == AF_INET6) {
317 # if defined(IPV6_V6ONLY)
319 int val=(v6only==2)?0:1;
320 if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
321 (void*)&val, (socklen_t)sizeof(val)) < 0) {
323 log_err("setsockopt(..., IPV6_V6ONLY"
324 ", ...) failed: %s", strerror(errno));
327 log_err("setsockopt(..., IPV6_V6ONLY"
329 wsa_strerror(WSAGetLastError()));
338 # if defined(IPV6_USE_MIN_MTU)
340 * There is no fragmentation of IPv6 datagrams
341 * during forwarding in the network. Therefore
342 * we do not send UDP datagrams larger than
343 * the minimum IPv6 MTU of 1280 octets. The
344 * EDNS0 message length can be larger if the
345 * network stack supports IPV6_USE_MIN_MTU.
347 if (setsockopt(s, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
348 (void*)&on, (socklen_t)sizeof(on)) < 0) {
350 log_err("setsockopt(..., IPV6_USE_MIN_MTU, "
351 "...) failed: %s", strerror(errno));
354 log_err("setsockopt(..., IPV6_USE_MIN_MTU, "
356 wsa_strerror(WSAGetLastError()));
363 # elif defined(IPV6_MTU)
365 * On Linux, to send no larger than 1280, the PMTUD is
366 * disabled by default for datagrams anyway, so we set
369 if (setsockopt(s, IPPROTO_IPV6, IPV6_MTU,
370 (void*)&mtu, (socklen_t)sizeof(mtu)) < 0) {
372 log_err("setsockopt(..., IPV6_MTU, ...) failed: %s",
376 log_err("setsockopt(..., IPV6_MTU, ...) failed: %s",
377 wsa_strerror(WSAGetLastError()));
384 # endif /* IPv6 MTU */
385 } else if(family == AF_INET) {
386 # if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
387 /* linux 3.15 has IP_PMTUDISC_OMIT, Hannes Frederic Sowa made it so that
388 * PMTU information is not accepted, but fragmentation is allowed
389 * if and only if the packet size exceeds the outgoing interface MTU
390 * (and also uses the interface mtu to determine the size of the packets).
391 * So there won't be any EMSGSIZE error. Against DNS fragmentation attacks.
392 * FreeBSD already has same semantics without setting the option. */
395 # if defined(IP_PMTUDISC_OMIT)
396 action = IP_PMTUDISC_OMIT;
397 if (setsockopt(s, IPPROTO_IP, IP_MTU_DISCOVER,
398 &action, (socklen_t)sizeof(action)) < 0) {
400 if (errno != EINVAL) {
401 log_err("setsockopt(..., IP_MTU_DISCOVER, IP_PMTUDISC_OMIT...) failed: %s",
420 action = IP_PMTUDISC_DONT;
421 if (setsockopt(s, IPPROTO_IP, IP_MTU_DISCOVER,
422 &action, (socklen_t)sizeof(action)) < 0) {
423 log_err("setsockopt(..., IP_MTU_DISCOVER, IP_PMTUDISC_DONT...) failed: %s",
435 # elif defined(IP_DONTFRAG)
437 if (setsockopt(s, IPPROTO_IP, IP_DONTFRAG,
438 &off, (socklen_t)sizeof(off)) < 0) {
439 log_err("setsockopt(..., IP_DONTFRAG, ...) failed: %s",
450 # endif /* IPv4 MTU */
452 if(bind(s, (struct sockaddr*)addr, addrlen) != 0) {
457 *inuse = (errno == EADDRINUSE);
458 /* detect freebsd jail with no ipv6 permission */
459 if(family==AF_INET6 && errno==EINVAL)
461 else if(errno != EADDRINUSE) {
462 log_err_addr("can't bind socket", strerror(errno),
463 (struct sockaddr_storage*)addr, addrlen);
465 #endif /* EADDRINUSE */
467 #else /* USE_WINSOCK */
468 if(WSAGetLastError() != WSAEADDRINUSE &&
469 WSAGetLastError() != WSAEADDRNOTAVAIL) {
470 log_err_addr("can't bind socket",
471 wsa_strerror(WSAGetLastError()),
472 (struct sockaddr_storage*)addr, addrlen);
478 if(!fd_set_nonblock(s)) {
492 create_tcp_accept_sock(struct addrinfo *addr, int v6only, int* noproto,
493 int* reuseport, int transparent, int mss)
496 #if defined(SO_REUSEADDR) || defined(SO_REUSEPORT) || defined(IPV6_V6ONLY) || defined(IP_TRANSPARENT)
499 #ifndef IP_TRANSPARENT
502 verbose_print_addr(addr);
504 if((s = socket(addr->ai_family, addr->ai_socktype, 0)) == -1) {
506 if(errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
510 log_err("can't create socket: %s", strerror(errno));
512 if(WSAGetLastError() == WSAEAFNOSUPPORT ||
513 WSAGetLastError() == WSAEPROTONOSUPPORT) {
517 log_err("can't create socket: %s",
518 wsa_strerror(WSAGetLastError()));
523 #if defined(IPPROTO_TCP) && defined(TCP_MAXSEG)
524 if(setsockopt(s, IPPROTO_TCP, TCP_MAXSEG, (void*)&mss,
525 (socklen_t)sizeof(mss)) < 0) {
527 log_err(" setsockopt(.. TCP_MAXSEG ..) failed: %s",
530 log_err(" setsockopt(.. TCP_MAXSEG ..) failed: %s",
531 wsa_strerror(WSAGetLastError()));
535 " tcp socket mss set to %d", mss);
538 log_warn(" setsockopt(TCP_MAXSEG) unsupported");
539 #endif /* defined(IPPROTO_TCP) && defined(TCP_MAXSEG) */
542 if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*)&on,
543 (socklen_t)sizeof(on)) < 0) {
545 log_err("setsockopt(.. SO_REUSEADDR ..) failed: %s",
549 log_err("setsockopt(.. SO_REUSEADDR ..) failed: %s",
550 wsa_strerror(WSAGetLastError()));
555 #endif /* SO_REUSEADDR */
557 /* try to set SO_REUSEPORT so that incoming
558 * connections are distributed evenly among the receiving threads.
559 * Each thread must have its own socket bound to the same port,
560 * with SO_REUSEPORT set on each socket.
562 if (reuseport && *reuseport &&
563 setsockopt(s, SOL_SOCKET, SO_REUSEPORT, (void*)&on,
564 (socklen_t)sizeof(on)) < 0) {
566 if(errno != ENOPROTOOPT || verbosity >= 3)
567 log_warn("setsockopt(.. SO_REUSEPORT ..) failed: %s",
570 /* this option is not essential, we can continue */
575 #endif /* defined(SO_REUSEPORT) */
576 #if defined(IPV6_V6ONLY)
577 if(addr->ai_family == AF_INET6 && v6only) {
578 if(setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
579 (void*)&on, (socklen_t)sizeof(on)) < 0) {
581 log_err("setsockopt(..., IPV6_V6ONLY, ...) failed: %s",
585 log_err("setsockopt(..., IPV6_V6ONLY, ...) failed: %s",
586 wsa_strerror(WSAGetLastError()));
594 #endif /* IPV6_V6ONLY */
595 #ifdef IP_TRANSPARENT
597 setsockopt(s, IPPROTO_IP, IP_TRANSPARENT, (void*)&on,
598 (socklen_t)sizeof(on)) < 0) {
599 log_warn("setsockopt(.. IP_TRANSPARENT ..) failed: %s",
602 #endif /* IP_TRANSPARENT */
603 if(bind(s, addr->ai_addr, addr->ai_addrlen) != 0) {
605 /* detect freebsd jail with no ipv6 permission */
606 if(addr->ai_family==AF_INET6 && errno==EINVAL)
609 log_err_addr("can't bind socket", strerror(errno),
610 (struct sockaddr_storage*)addr->ai_addr,
615 log_err_addr("can't bind socket",
616 wsa_strerror(WSAGetLastError()),
617 (struct sockaddr_storage*)addr->ai_addr,
623 if(!fd_set_nonblock(s)) {
631 if(listen(s, TCP_BACKLOG) == -1) {
633 log_err("can't listen: %s", strerror(errno));
636 log_err("can't listen: %s", wsa_strerror(WSAGetLastError()));
645 create_local_accept_sock(const char *path, int* noproto)
649 struct sockaddr_un usock;
651 verbose(VERB_ALGO, "creating unix socket %s", path);
652 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
653 /* this member exists on BSDs, not Linux */
654 usock.sun_len = (socklen_t)sizeof(usock);
656 usock.sun_family = AF_LOCAL;
657 /* length is 92-108, 104 on FreeBSD */
658 (void)strlcpy(usock.sun_path, path, sizeof(usock.sun_path));
660 if ((s = socket(AF_LOCAL, SOCK_STREAM, 0)) == -1) {
661 log_err("Cannot create local socket %s (%s)",
662 path, strerror(errno));
666 if (unlink(path) && errno != ENOENT) {
667 /* The socket already exists and cannot be removed */
668 log_err("Cannot remove old local socket %s (%s)",
669 path, strerror(errno));
673 if (bind(s, (struct sockaddr *)&usock,
674 (socklen_t)sizeof(struct sockaddr_un)) == -1) {
675 log_err("Cannot bind local socket %s (%s)",
676 path, strerror(errno));
680 if (!fd_set_nonblock(s)) {
681 log_err("Cannot set non-blocking mode");
685 if (listen(s, TCP_BACKLOG) == -1) {
686 log_err("can't listen: %s", strerror(errno));
690 (void)noproto; /*unused*/
694 log_err("Local sockets are not supported");
702 * Create socket from getaddrinfo results
705 make_sock(int stype, const char* ifname, const char* port,
706 struct addrinfo *hints, int v6only, int* noip6, size_t rcv, size_t snd,
707 int* reuseport, int transparent, int tcp_mss)
709 struct addrinfo *res = NULL;
710 int r, s, inuse, noproto;
711 hints->ai_socktype = stype;
713 if((r=getaddrinfo(ifname, port, hints, &res)) != 0 || !res) {
715 if(r == EAI_NONAME && hints->ai_family == AF_INET6){
716 *noip6 = 1; /* 'Host not found' for IP6 on winXP */
720 log_err("node %s:%s getaddrinfo: %s %s",
721 ifname?ifname:"default", port, gai_strerror(r),
723 r==EAI_SYSTEM?(char*)strerror(errno):""
730 if(stype == SOCK_DGRAM) {
731 verbose_print_addr(res);
732 s = create_udp_sock(res->ai_family, res->ai_socktype,
733 (struct sockaddr*)res->ai_addr, res->ai_addrlen,
734 v6only, &inuse, &noproto, (int)rcv, (int)snd, 1,
735 reuseport, transparent);
736 if(s == -1 && inuse) {
737 log_err("bind: address already in use");
738 } else if(s == -1 && noproto && hints->ai_family == AF_INET6){
742 s = create_tcp_accept_sock(res, v6only, &noproto, reuseport,
743 transparent, tcp_mss);
744 if(s == -1 && noproto && hints->ai_family == AF_INET6){
752 /** make socket and first see if ifname contains port override info */
754 make_sock_port(int stype, const char* ifname, const char* port,
755 struct addrinfo *hints, int v6only, int* noip6, size_t rcv, size_t snd,
756 int* reuseport, int transparent, int tcp_mss)
758 char* s = strchr(ifname, '@');
760 /* override port with ifspec@port */
763 if((size_t)(s-ifname) >= sizeof(newif)) {
764 log_err("ifname too long: %s", ifname);
768 if(strlen(s+1) >= sizeof(p)) {
769 log_err("portnumber too long: %s", ifname);
773 (void)strlcpy(newif, ifname, sizeof(newif));
775 (void)strlcpy(p, s+1, sizeof(p));
777 return make_sock(stype, newif, p, hints, v6only, noip6,
778 rcv, snd, reuseport, transparent, tcp_mss);
780 return make_sock(stype, ifname, port, hints, v6only, noip6, rcv, snd,
781 reuseport, transparent, tcp_mss);
785 * Add port to open ports list.
786 * @param list: list head. changed.
788 * @param ftype: if fd is UDP.
789 * @return false on failure. list in unchanged then.
792 port_insert(struct listen_port** list, int s, enum listen_type ftype)
794 struct listen_port* item = (struct listen_port*)malloc(
795 sizeof(struct listen_port));
805 /** set fd to receive source address packet info */
807 set_recvpktinfo(int s, int family)
809 #if defined(IPV6_RECVPKTINFO) || defined(IPV6_PKTINFO) || (defined(IP_RECVDSTADDR) && defined(IP_SENDSRCADDR)) || defined(IP_PKTINFO)
814 if(family == AF_INET6) {
815 # ifdef IPV6_RECVPKTINFO
816 if(setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO,
817 (void*)&on, (socklen_t)sizeof(on)) < 0) {
818 log_err("setsockopt(..., IPV6_RECVPKTINFO, ...) failed: %s",
822 # elif defined(IPV6_PKTINFO)
823 if(setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO,
824 (void*)&on, (socklen_t)sizeof(on)) < 0) {
825 log_err("setsockopt(..., IPV6_PKTINFO, ...) failed: %s",
830 log_err("no IPV6_RECVPKTINFO and no IPV6_PKTINFO option, please "
831 "disable interface-automatic in config");
833 # endif /* defined IPV6_RECVPKTINFO */
835 } else if(family == AF_INET) {
837 if(setsockopt(s, IPPROTO_IP, IP_PKTINFO,
838 (void*)&on, (socklen_t)sizeof(on)) < 0) {
839 log_err("setsockopt(..., IP_PKTINFO, ...) failed: %s",
843 # elif defined(IP_RECVDSTADDR) && defined(IP_SENDSRCADDR)
844 if(setsockopt(s, IPPROTO_IP, IP_RECVDSTADDR,
845 (void*)&on, (socklen_t)sizeof(on)) < 0) {
846 log_err("setsockopt(..., IP_RECVDSTADDR, ...) failed: %s",
851 log_err("no IP_SENDSRCADDR or IP_PKTINFO option, please disable "
852 "interface-automatic in config");
854 # endif /* IP_PKTINFO */
861 * Helper for ports_open. Creates one interface (or NULL for default).
862 * @param ifname: The interface ip address.
863 * @param do_auto: use automatic interface detection.
864 * If enabled, then ifname must be the wildcard name.
865 * @param do_udp: if udp should be used.
866 * @param do_tcp: if udp should be used.
867 * @param hints: for getaddrinfo. family and flags have to be set by caller.
868 * @param port: Port number to use (as string).
869 * @param list: list of open ports, appended to, changed to point to list head.
870 * @param rcv: receive buffer size for UDP
871 * @param snd: send buffer size for UDP
872 * @param ssl_port: ssl service port number
873 * @param reuseport: try to set SO_REUSEPORT if nonNULL and true.
874 * set to false on exit if reuseport failed due to no kernel support.
875 * @param transparent: set IP_TRANSPARENT socket option.
876 * @param tcp_mss: maximum segment size of tcp socket. default if zero.
877 * @return: returns false on error.
880 ports_create_if(const char* ifname, int do_auto, int do_udp, int do_tcp,
881 struct addrinfo *hints, const char* port, struct listen_port** list,
882 size_t rcv, size_t snd, int ssl_port, int* reuseport, int transparent,
886 if(!do_udp && !do_tcp)
889 if((s = make_sock_port(SOCK_DGRAM, ifname, port, hints, 1,
890 &noip6, rcv, snd, reuseport, transparent,
893 log_warn("IPv6 protocol not available");
898 /* getting source addr packet info is highly non-portable */
899 if(!set_recvpktinfo(s, hints->ai_family)) {
907 if(!port_insert(list, s, listen_type_udpancil)) {
916 /* regular udp socket */
917 if((s = make_sock_port(SOCK_DGRAM, ifname, port, hints, 1,
918 &noip6, rcv, snd, reuseport, transparent,
921 log_warn("IPv6 protocol not available");
926 if(!port_insert(list, s, listen_type_udp)) {
936 int is_ssl = ((strchr(ifname, '@') &&
937 atoi(strchr(ifname, '@')+1) == ssl_port) ||
938 (!strchr(ifname, '@') && atoi(port) == ssl_port));
939 if((s = make_sock_port(SOCK_STREAM, ifname, port, hints, 1,
940 &noip6, 0, 0, reuseport, transparent, tcp_mss)) == -1) {
942 /*log_warn("IPv6 protocol not available");*/
948 verbose(VERB_ALGO, "setup TCP for SSL service");
949 if(!port_insert(list, s, is_ssl?listen_type_ssl:
963 * Add items to commpoint list in front.
964 * @param c: commpoint to add.
965 * @param front: listen struct.
966 * @return: false on failure.
969 listen_cp_insert(struct comm_point* c, struct listen_dnsport* front)
971 struct listen_list* item = (struct listen_list*)malloc(
972 sizeof(struct listen_list));
976 item->next = front->cps;
981 struct listen_dnsport*
982 listen_create(struct comm_base* base, struct listen_port* ports,
983 size_t bufsize, int tcp_accept_count, void* sslctx,
984 struct dt_env* dtenv, comm_point_callback_t* cb, void *cb_arg)
986 struct listen_dnsport* front = (struct listen_dnsport*)
987 malloc(sizeof(struct listen_dnsport));
991 front->udp_buff = sldns_buffer_new(bufsize);
992 if(!front->udp_buff) {
997 /* create comm points as needed */
999 struct comm_point* cp = NULL;
1000 if(ports->ftype == listen_type_udp)
1001 cp = comm_point_create_udp(base, ports->fd,
1002 front->udp_buff, cb, cb_arg);
1003 else if(ports->ftype == listen_type_tcp)
1004 cp = comm_point_create_tcp(base, ports->fd,
1005 tcp_accept_count, bufsize, cb, cb_arg);
1006 else if(ports->ftype == listen_type_ssl) {
1007 cp = comm_point_create_tcp(base, ports->fd,
1008 tcp_accept_count, bufsize, cb, cb_arg);
1010 } else if(ports->ftype == listen_type_udpancil)
1011 cp = comm_point_create_udp_ancil(base, ports->fd,
1012 front->udp_buff, cb, cb_arg);
1014 log_err("can't create commpoint");
1015 listen_delete(front);
1019 cp->do_not_close = 1;
1020 if(!listen_cp_insert(cp, front)) {
1021 log_err("malloc failed");
1022 comm_point_delete(cp);
1023 listen_delete(front);
1026 ports = ports->next;
1029 log_err("Could not open sockets to accept queries.");
1030 listen_delete(front);
1038 listen_list_delete(struct listen_list* list)
1040 struct listen_list *p = list, *pn;
1043 comm_point_delete(p->com);
1050 listen_delete(struct listen_dnsport* front)
1054 listen_list_delete(front->cps);
1055 sldns_buffer_free(front->udp_buff);
1060 listening_ports_open(struct config_file* cfg, int* reuseport)
1062 struct listen_port* list = NULL;
1063 struct addrinfo hints;
1064 int i, do_ip4, do_ip6;
1065 int do_tcp, do_auto;
1067 snprintf(portbuf, sizeof(portbuf), "%d", cfg->port);
1068 do_ip4 = cfg->do_ip4;
1069 do_ip6 = cfg->do_ip6;
1070 do_tcp = cfg->do_tcp;
1071 do_auto = cfg->if_automatic && cfg->do_udp;
1072 if(cfg->incoming_num_tcp == 0)
1076 memset(&hints, 0, sizeof(hints));
1077 hints.ai_flags = AI_PASSIVE;
1078 /* no name lookups on our listening ports */
1079 if(cfg->num_ifs > 0)
1080 hints.ai_flags |= AI_NUMERICHOST;
1081 hints.ai_family = AF_UNSPEC;
1085 if(!do_ip4 && !do_ip6) {
1088 /* create ip4 and ip6 ports so that return addresses are nice. */
1089 if(do_auto || cfg->num_ifs == 0) {
1091 hints.ai_family = AF_INET6;
1092 if(!ports_create_if(do_auto?"::0":"::1",
1093 do_auto, cfg->do_udp, do_tcp,
1094 &hints, portbuf, &list,
1095 cfg->so_rcvbuf, cfg->so_sndbuf,
1096 cfg->ssl_port, reuseport,
1097 cfg->ip_transparent,
1099 listening_ports_free(list);
1104 hints.ai_family = AF_INET;
1105 if(!ports_create_if(do_auto?"0.0.0.0":"127.0.0.1",
1106 do_auto, cfg->do_udp, do_tcp,
1107 &hints, portbuf, &list,
1108 cfg->so_rcvbuf, cfg->so_sndbuf,
1109 cfg->ssl_port, reuseport,
1110 cfg->ip_transparent,
1112 listening_ports_free(list);
1116 } else for(i = 0; i<cfg->num_ifs; i++) {
1117 if(str_is_ip6(cfg->ifs[i])) {
1120 hints.ai_family = AF_INET6;
1121 if(!ports_create_if(cfg->ifs[i], 0, cfg->do_udp,
1122 do_tcp, &hints, portbuf, &list,
1123 cfg->so_rcvbuf, cfg->so_sndbuf,
1124 cfg->ssl_port, reuseport,
1125 cfg->ip_transparent,
1127 listening_ports_free(list);
1133 hints.ai_family = AF_INET;
1134 if(!ports_create_if(cfg->ifs[i], 0, cfg->do_udp,
1135 do_tcp, &hints, portbuf, &list,
1136 cfg->so_rcvbuf, cfg->so_sndbuf,
1137 cfg->ssl_port, reuseport,
1138 cfg->ip_transparent,
1140 listening_ports_free(list);
1148 void listening_ports_free(struct listen_port* list)
1150 struct listen_port* nx;
1153 if(list->fd != -1) {
1157 closesocket(list->fd);
1165 size_t listen_get_mem(struct listen_dnsport* listen)
1167 size_t s = sizeof(*listen) + sizeof(*listen->base) +
1168 sizeof(*listen->udp_buff) +
1169 sldns_buffer_capacity(listen->udp_buff);
1170 struct listen_list* p;
1171 for(p = listen->cps; p; p = p->next) {
1173 s += comm_point_get_mem(p->com);
1178 void listen_stop_accept(struct listen_dnsport* listen)
1180 /* do not stop the ones that have no tcp_free list
1181 * (they have already stopped listening) */
1182 struct listen_list* p;
1183 for(p=listen->cps; p; p=p->next) {
1184 if(p->com->type == comm_tcp_accept &&
1185 p->com->tcp_free != NULL) {
1186 comm_point_stop_listening(p->com);
1191 void listen_start_accept(struct listen_dnsport* listen)
1193 /* do not start the ones that have no tcp_free list, it is no
1194 * use to listen to them because they have no free tcp handlers */
1195 struct listen_list* p;
1196 for(p=listen->cps; p; p=p->next) {
1197 if(p->com->type == comm_tcp_accept &&
1198 p->com->tcp_free != NULL) {
1199 comm_point_start_listening(p->com, -1, -1);