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 "ldns/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)
102 #if defined(SO_REUSEADDR) || defined(SO_REUSEPORT) || defined(IPV6_USE_MIN_MTU)
106 int mtu = IPV6_MIN_MTU;
108 #if !defined(SO_RCVBUFFORCE) && !defined(SO_RCVBUF)
111 #if !defined(SO_SNDBUFFORCE) && !defined(SO_SNDBUF)
117 if((s = socket(family, socktype, 0)) == -1) {
120 if(errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
124 log_err("can't create socket: %s", strerror(errno));
126 if(WSAGetLastError() == WSAEAFNOSUPPORT ||
127 WSAGetLastError() == WSAEPROTONOSUPPORT) {
131 log_err("can't create socket: %s",
132 wsa_strerror(WSAGetLastError()));
139 if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*)&on,
140 (socklen_t)sizeof(on)) < 0) {
142 log_err("setsockopt(.. SO_REUSEADDR ..) failed: %s",
144 if(errno != ENOSYS) {
151 log_err("setsockopt(.. SO_REUSEADDR ..) failed: %s",
152 wsa_strerror(WSAGetLastError()));
159 #endif /* SO_REUSEADDR */
161 /* try to set SO_REUSEPORT so that incoming
162 * queries are distributed evenly among the receiving threads.
163 * Each thread must have its own socket bound to the same port,
164 * with SO_REUSEPORT set on each socket.
166 if (reuseport && *reuseport &&
167 setsockopt(s, SOL_SOCKET, SO_REUSEPORT, (void*)&on,
168 (socklen_t)sizeof(on)) < 0) {
170 if(errno != ENOPROTOOPT || verbosity >= 3)
171 log_warn("setsockopt(.. SO_REUSEPORT ..) failed: %s",
174 /* this option is not essential, we can continue */
179 #endif /* defined(SO_REUSEPORT) */
184 socklen_t slen = (socklen_t)sizeof(got);
185 # ifdef SO_RCVBUFFORCE
186 /* Linux specific: try to use root permission to override
187 * system limits on rcvbuf. The limit is stored in
188 * /proc/sys/net/core/rmem_max or sysctl net.core.rmem_max */
189 if(setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, (void*)&rcv,
190 (socklen_t)sizeof(rcv)) < 0) {
193 log_err("setsockopt(..., SO_RCVBUFFORCE, "
194 "...) failed: %s", strerror(errno));
197 log_err("setsockopt(..., SO_RCVBUFFORCE, "
199 wsa_strerror(WSAGetLastError()));
206 # endif /* SO_RCVBUFFORCE */
207 if(setsockopt(s, SOL_SOCKET, SO_RCVBUF, (void*)&rcv,
208 (socklen_t)sizeof(rcv)) < 0) {
210 log_err("setsockopt(..., SO_RCVBUF, "
211 "...) failed: %s", strerror(errno));
214 log_err("setsockopt(..., SO_RCVBUF, "
216 wsa_strerror(WSAGetLastError()));
223 /* check if we got the right thing or if system
224 * reduced to some system max. Warn if so */
225 if(getsockopt(s, SOL_SOCKET, SO_RCVBUF, (void*)&got,
226 &slen) >= 0 && got < rcv/2) {
227 log_warn("so-rcvbuf %u was not granted. "
228 "Got %u. To fix: start with "
229 "root permissions(linux) or sysctl "
230 "bigger net.core.rmem_max(linux) or "
231 "kern.ipc.maxsockbuf(bsd) values.",
232 (unsigned)rcv, (unsigned)got);
234 # ifdef SO_RCVBUFFORCE
237 #endif /* SO_RCVBUF */
239 /* first do RCVBUF as the receive buffer is more important */
243 socklen_t slen = (socklen_t)sizeof(got);
244 # ifdef SO_SNDBUFFORCE
245 /* Linux specific: try to use root permission to override
246 * system limits on sndbuf. The limit is stored in
247 * /proc/sys/net/core/wmem_max or sysctl net.core.wmem_max */
248 if(setsockopt(s, SOL_SOCKET, SO_SNDBUFFORCE, (void*)&snd,
249 (socklen_t)sizeof(snd)) < 0) {
252 log_err("setsockopt(..., SO_SNDBUFFORCE, "
253 "...) failed: %s", strerror(errno));
256 log_err("setsockopt(..., SO_SNDBUFFORCE, "
258 wsa_strerror(WSAGetLastError()));
265 # endif /* SO_SNDBUFFORCE */
266 if(setsockopt(s, SOL_SOCKET, SO_SNDBUF, (void*)&snd,
267 (socklen_t)sizeof(snd)) < 0) {
269 log_err("setsockopt(..., SO_SNDBUF, "
270 "...) failed: %s", strerror(errno));
273 log_err("setsockopt(..., SO_SNDBUF, "
275 wsa_strerror(WSAGetLastError()));
282 /* check if we got the right thing or if system
283 * reduced to some system max. Warn if so */
284 if(getsockopt(s, SOL_SOCKET, SO_SNDBUF, (void*)&got,
285 &slen) >= 0 && got < snd/2) {
286 log_warn("so-sndbuf %u was not granted. "
287 "Got %u. To fix: start with "
288 "root permissions(linux) or sysctl "
289 "bigger net.core.wmem_max(linux) or "
290 "kern.ipc.maxsockbuf(bsd) values.",
291 (unsigned)snd, (unsigned)got);
293 # ifdef SO_SNDBUFFORCE
296 #endif /* SO_SNDBUF */
298 if(family == AF_INET6) {
299 # if defined(IPV6_V6ONLY)
301 int val=(v6only==2)?0:1;
302 if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
303 (void*)&val, (socklen_t)sizeof(val)) < 0) {
305 log_err("setsockopt(..., IPV6_V6ONLY"
306 ", ...) failed: %s", strerror(errno));
309 log_err("setsockopt(..., IPV6_V6ONLY"
311 wsa_strerror(WSAGetLastError()));
320 # if defined(IPV6_USE_MIN_MTU)
322 * There is no fragmentation of IPv6 datagrams
323 * during forwarding in the network. Therefore
324 * we do not send UDP datagrams larger than
325 * the minimum IPv6 MTU of 1280 octets. The
326 * EDNS0 message length can be larger if the
327 * network stack supports IPV6_USE_MIN_MTU.
329 if (setsockopt(s, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
330 (void*)&on, (socklen_t)sizeof(on)) < 0) {
332 log_err("setsockopt(..., IPV6_USE_MIN_MTU, "
333 "...) failed: %s", strerror(errno));
336 log_err("setsockopt(..., IPV6_USE_MIN_MTU, "
338 wsa_strerror(WSAGetLastError()));
345 # elif defined(IPV6_MTU)
347 * On Linux, to send no larger than 1280, the PMTUD is
348 * disabled by default for datagrams anyway, so we set
351 if (setsockopt(s, IPPROTO_IPV6, IPV6_MTU,
352 (void*)&mtu, (socklen_t)sizeof(mtu)) < 0) {
354 log_err("setsockopt(..., IPV6_MTU, ...) failed: %s",
358 log_err("setsockopt(..., IPV6_MTU, ...) failed: %s",
359 wsa_strerror(WSAGetLastError()));
366 # endif /* IPv6 MTU */
367 } else if(family == AF_INET) {
368 # if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
369 /* linux 3.15 has IP_PMTUDISC_OMIT, Hannes Frederic Sowa made it so that
370 * PMTU information is not accepted, but fragmentation is allowed
371 * if and only if the packet size exceeds the outgoing interface MTU
372 * (and also uses the interface mtu to determine the size of the packets).
373 * So there won't be any EMSGSIZE error. Against DNS fragmentation attacks.
374 * FreeBSD already has same semantics without setting the option. */
375 # if defined(IP_PMTUDISC_OMIT)
376 int action = IP_PMTUDISC_OMIT;
378 int action = IP_PMTUDISC_DONT;
380 if (setsockopt(s, IPPROTO_IP, IP_MTU_DISCOVER,
381 &action, (socklen_t)sizeof(action)) < 0) {
382 log_err("setsockopt(..., IP_MTU_DISCOVER, "
383 # if defined(IP_PMTUDISC_OMIT)
399 # elif defined(IP_DONTFRAG)
401 if (setsockopt(s, IPPROTO_IP, IP_DONTFRAG,
402 &off, (socklen_t)sizeof(off)) < 0) {
403 log_err("setsockopt(..., IP_DONTFRAG, ...) failed: %s",
414 # endif /* IPv4 MTU */
416 if(bind(s, (struct sockaddr*)addr, addrlen) != 0) {
421 *inuse = (errno == EADDRINUSE);
422 /* detect freebsd jail with no ipv6 permission */
423 if(family==AF_INET6 && errno==EINVAL)
425 else if(errno != EADDRINUSE) {
426 log_err_addr("can't bind socket", strerror(errno),
427 (struct sockaddr_storage*)addr, addrlen);
429 #endif /* EADDRINUSE */
431 #else /* USE_WINSOCK */
432 if(WSAGetLastError() != WSAEADDRINUSE &&
433 WSAGetLastError() != WSAEADDRNOTAVAIL) {
434 log_err_addr("can't bind socket",
435 wsa_strerror(WSAGetLastError()),
436 (struct sockaddr_storage*)addr, addrlen);
442 if(!fd_set_nonblock(s)) {
456 create_tcp_accept_sock(struct addrinfo *addr, int v6only, int* noproto,
460 #if defined(SO_REUSEADDR) || defined(SO_REUSEPORT) || defined(IPV6_V6ONLY)
462 #endif /* SO_REUSEADDR || IPV6_V6ONLY */
463 verbose_print_addr(addr);
465 if((s = socket(addr->ai_family, addr->ai_socktype, 0)) == -1) {
467 if(errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
471 log_err("can't create socket: %s", strerror(errno));
473 if(WSAGetLastError() == WSAEAFNOSUPPORT ||
474 WSAGetLastError() == WSAEPROTONOSUPPORT) {
478 log_err("can't create socket: %s",
479 wsa_strerror(WSAGetLastError()));
484 if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*)&on,
485 (socklen_t)sizeof(on)) < 0) {
487 log_err("setsockopt(.. SO_REUSEADDR ..) failed: %s",
491 log_err("setsockopt(.. SO_REUSEADDR ..) failed: %s",
492 wsa_strerror(WSAGetLastError()));
497 #endif /* SO_REUSEADDR */
499 /* try to set SO_REUSEPORT so that incoming
500 * connections are distributed evenly among the receiving threads.
501 * Each thread must have its own socket bound to the same port,
502 * with SO_REUSEPORT set on each socket.
504 if (reuseport && *reuseport &&
505 setsockopt(s, SOL_SOCKET, SO_REUSEPORT, (void*)&on,
506 (socklen_t)sizeof(on)) < 0) {
508 if(errno != ENOPROTOOPT || verbosity >= 3)
509 log_warn("setsockopt(.. SO_REUSEPORT ..) failed: %s",
512 /* this option is not essential, we can continue */
517 #endif /* defined(SO_REUSEPORT) */
518 #if defined(IPV6_V6ONLY)
519 if(addr->ai_family == AF_INET6 && v6only) {
520 if(setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
521 (void*)&on, (socklen_t)sizeof(on)) < 0) {
523 log_err("setsockopt(..., IPV6_V6ONLY, ...) failed: %s",
527 log_err("setsockopt(..., IPV6_V6ONLY, ...) failed: %s",
528 wsa_strerror(WSAGetLastError()));
536 #endif /* IPV6_V6ONLY */
537 if(bind(s, addr->ai_addr, addr->ai_addrlen) != 0) {
539 /* detect freebsd jail with no ipv6 permission */
540 if(addr->ai_family==AF_INET6 && errno==EINVAL)
543 log_err_addr("can't bind socket", strerror(errno),
544 (struct sockaddr_storage*)addr->ai_addr,
549 log_err_addr("can't bind socket",
550 wsa_strerror(WSAGetLastError()),
551 (struct sockaddr_storage*)addr->ai_addr,
557 if(!fd_set_nonblock(s)) {
565 if(listen(s, TCP_BACKLOG) == -1) {
567 log_err("can't listen: %s", strerror(errno));
570 log_err("can't listen: %s", wsa_strerror(WSAGetLastError()));
579 create_local_accept_sock(char *path, int* noproto)
583 struct sockaddr_un sun;
585 sun.sun_len = sizeof(sun);
586 sun.sun_family = AF_LOCAL;
587 strlcpy(sun.sun_path, path, 104);
589 if ((s = socket(PF_LOCAL, SOCK_STREAM, 0)) == -1) {
590 log_err("Cannot create local socket %s (%s)",
591 path, strerror(errno));
595 if (unlink(path) && errno != ENOENT) {
596 /* The socket already exists and cannot be removed */
597 log_err("Cannot remove old local socket %s (%s)",
598 path, strerror(errno));
602 if (bind(s, (struct sockaddr *)&sun,
603 sizeof(struct sockaddr_un)) == -1) {
604 log_err("Cannot bind local socket %s (%s)",
605 path, strerror(errno));
609 if (!fd_set_nonblock(s)) {
610 log_err("Cannot set non-blocking mode");
614 if (listen(s, TCP_BACKLOG) == -1) {
615 log_err("can't listen: %s", strerror(errno));
621 log_err("Local sockets are not supported");
629 * Create socket from getaddrinfo results
632 make_sock(int stype, const char* ifname, const char* port,
633 struct addrinfo *hints, int v6only, int* noip6, size_t rcv, size_t snd,
636 struct addrinfo *res = NULL;
637 int r, s, inuse, noproto;
638 hints->ai_socktype = stype;
640 if((r=getaddrinfo(ifname, port, hints, &res)) != 0 || !res) {
642 if(r == EAI_NONAME && hints->ai_family == AF_INET6){
643 *noip6 = 1; /* 'Host not found' for IP6 on winXP */
647 log_err("node %s:%s getaddrinfo: %s %s",
648 ifname?ifname:"default", port, gai_strerror(r),
650 r==EAI_SYSTEM?(char*)strerror(errno):""
657 if(stype == SOCK_DGRAM) {
658 verbose_print_addr(res);
659 s = create_udp_sock(res->ai_family, res->ai_socktype,
660 (struct sockaddr*)res->ai_addr, res->ai_addrlen,
661 v6only, &inuse, &noproto, (int)rcv, (int)snd, 1,
663 if(s == -1 && inuse) {
664 log_err("bind: address already in use");
665 } else if(s == -1 && noproto && hints->ai_family == AF_INET6){
669 s = create_tcp_accept_sock(res, v6only, &noproto, reuseport);
670 if(s == -1 && noproto && hints->ai_family == AF_INET6){
678 /** make socket and first see if ifname contains port override info */
680 make_sock_port(int stype, const char* ifname, const char* port,
681 struct addrinfo *hints, int v6only, int* noip6, size_t rcv, size_t snd,
684 char* s = strchr(ifname, '@');
686 /* override port with ifspec@port */
689 if((size_t)(s-ifname) >= sizeof(newif)) {
690 log_err("ifname too long: %s", ifname);
694 if(strlen(s+1) >= sizeof(p)) {
695 log_err("portnumber too long: %s", ifname);
699 (void)strlcpy(newif, ifname, sizeof(newif));
701 (void)strlcpy(p, s+1, sizeof(p));
703 return make_sock(stype, newif, p, hints, v6only, noip6,
704 rcv, snd, reuseport);
706 return make_sock(stype, ifname, port, hints, v6only, noip6, rcv, snd,
711 * Add port to open ports list.
712 * @param list: list head. changed.
714 * @param ftype: if fd is UDP.
715 * @return false on failure. list in unchanged then.
718 port_insert(struct listen_port** list, int s, enum listen_type ftype)
720 struct listen_port* item = (struct listen_port*)malloc(
721 sizeof(struct listen_port));
731 /** set fd to receive source address packet info */
733 set_recvpktinfo(int s, int family)
735 #if defined(IPV6_RECVPKTINFO) || defined(IPV6_PKTINFO) || (defined(IP_RECVDSTADDR) && defined(IP_SENDSRCADDR)) || defined(IP_PKTINFO)
740 if(family == AF_INET6) {
741 # ifdef IPV6_RECVPKTINFO
742 if(setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO,
743 (void*)&on, (socklen_t)sizeof(on)) < 0) {
744 log_err("setsockopt(..., IPV6_RECVPKTINFO, ...) failed: %s",
748 # elif defined(IPV6_PKTINFO)
749 if(setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO,
750 (void*)&on, (socklen_t)sizeof(on)) < 0) {
751 log_err("setsockopt(..., IPV6_PKTINFO, ...) failed: %s",
756 log_err("no IPV6_RECVPKTINFO and no IPV6_PKTINFO option, please "
757 "disable interface-automatic in config");
759 # endif /* defined IPV6_RECVPKTINFO */
761 } else if(family == AF_INET) {
763 if(setsockopt(s, IPPROTO_IP, IP_PKTINFO,
764 (void*)&on, (socklen_t)sizeof(on)) < 0) {
765 log_err("setsockopt(..., IP_PKTINFO, ...) failed: %s",
769 # elif defined(IP_RECVDSTADDR) && defined(IP_SENDSRCADDR)
770 if(setsockopt(s, IPPROTO_IP, IP_RECVDSTADDR,
771 (void*)&on, (socklen_t)sizeof(on)) < 0) {
772 log_err("setsockopt(..., IP_RECVDSTADDR, ...) failed: %s",
777 log_err("no IP_SENDSRCADDR or IP_PKTINFO option, please disable "
778 "interface-automatic in config");
780 # endif /* IP_PKTINFO */
787 * Helper for ports_open. Creates one interface (or NULL for default).
788 * @param ifname: The interface ip address.
789 * @param do_auto: use automatic interface detection.
790 * If enabled, then ifname must be the wildcard name.
791 * @param do_udp: if udp should be used.
792 * @param do_tcp: if udp should be used.
793 * @param hints: for getaddrinfo. family and flags have to be set by caller.
794 * @param port: Port number to use (as string).
795 * @param list: list of open ports, appended to, changed to point to list head.
796 * @param rcv: receive buffer size for UDP
797 * @param snd: send buffer size for UDP
798 * @param ssl_port: ssl service port number
799 * @param reuseport: try to set SO_REUSEPORT if nonNULL and true.
800 * set to false on exit if reuseport failed due to no kernel support.
801 * @return: returns false on error.
804 ports_create_if(const char* ifname, int do_auto, int do_udp, int do_tcp,
805 struct addrinfo *hints, const char* port, struct listen_port** list,
806 size_t rcv, size_t snd, int ssl_port, int* reuseport)
809 if(!do_udp && !do_tcp)
812 if((s = make_sock_port(SOCK_DGRAM, ifname, port, hints, 1,
813 &noip6, rcv, snd, reuseport)) == -1) {
815 log_warn("IPv6 protocol not available");
820 /* getting source addr packet info is highly non-portable */
821 if(!set_recvpktinfo(s, hints->ai_family)) {
829 if(!port_insert(list, s, listen_type_udpancil)) {
838 /* regular udp socket */
839 if((s = make_sock_port(SOCK_DGRAM, ifname, port, hints, 1,
840 &noip6, rcv, snd, reuseport)) == -1) {
842 log_warn("IPv6 protocol not available");
847 if(!port_insert(list, s, listen_type_udp)) {
857 int is_ssl = ((strchr(ifname, '@') &&
858 atoi(strchr(ifname, '@')+1) == ssl_port) ||
859 (!strchr(ifname, '@') && atoi(port) == ssl_port));
860 if((s = make_sock_port(SOCK_STREAM, ifname, port, hints, 1,
861 &noip6, 0, 0, reuseport)) == -1) {
863 /*log_warn("IPv6 protocol not available");*/
869 verbose(VERB_ALGO, "setup TCP for SSL service");
870 if(!port_insert(list, s, is_ssl?listen_type_ssl:
884 * Add items to commpoint list in front.
885 * @param c: commpoint to add.
886 * @param front: listen struct.
887 * @return: false on failure.
890 listen_cp_insert(struct comm_point* c, struct listen_dnsport* front)
892 struct listen_list* item = (struct listen_list*)malloc(
893 sizeof(struct listen_list));
897 item->next = front->cps;
902 struct listen_dnsport*
903 listen_create(struct comm_base* base, struct listen_port* ports,
904 size_t bufsize, int tcp_accept_count, void* sslctx,
905 struct dt_env* dtenv, comm_point_callback_t* cb, void *cb_arg)
907 struct listen_dnsport* front = (struct listen_dnsport*)
908 malloc(sizeof(struct listen_dnsport));
912 front->udp_buff = sldns_buffer_new(bufsize);
913 if(!front->udp_buff) {
918 /* create comm points as needed */
920 struct comm_point* cp = NULL;
921 if(ports->ftype == listen_type_udp)
922 cp = comm_point_create_udp(base, ports->fd,
923 front->udp_buff, cb, cb_arg);
924 else if(ports->ftype == listen_type_tcp)
925 cp = comm_point_create_tcp(base, ports->fd,
926 tcp_accept_count, bufsize, cb, cb_arg);
927 else if(ports->ftype == listen_type_ssl) {
928 cp = comm_point_create_tcp(base, ports->fd,
929 tcp_accept_count, bufsize, cb, cb_arg);
931 } else if(ports->ftype == listen_type_udpancil)
932 cp = comm_point_create_udp_ancil(base, ports->fd,
933 front->udp_buff, cb, cb_arg);
935 log_err("can't create commpoint");
936 listen_delete(front);
940 cp->do_not_close = 1;
941 if(!listen_cp_insert(cp, front)) {
942 log_err("malloc failed");
943 comm_point_delete(cp);
944 listen_delete(front);
950 log_err("Could not open sockets to accept queries.");
951 listen_delete(front);
959 listen_list_delete(struct listen_list* list)
961 struct listen_list *p = list, *pn;
964 comm_point_delete(p->com);
971 listen_delete(struct listen_dnsport* front)
975 listen_list_delete(front->cps);
976 sldns_buffer_free(front->udp_buff);
981 listening_ports_open(struct config_file* cfg, int* reuseport)
983 struct listen_port* list = NULL;
984 struct addrinfo hints;
985 int i, do_ip4, do_ip6;
988 snprintf(portbuf, sizeof(portbuf), "%d", cfg->port);
989 do_ip4 = cfg->do_ip4;
990 do_ip6 = cfg->do_ip6;
991 do_tcp = cfg->do_tcp;
992 do_auto = cfg->if_automatic && cfg->do_udp;
993 if(cfg->incoming_num_tcp == 0)
997 memset(&hints, 0, sizeof(hints));
998 hints.ai_flags = AI_PASSIVE;
999 /* no name lookups on our listening ports */
1000 if(cfg->num_ifs > 0)
1001 hints.ai_flags |= AI_NUMERICHOST;
1002 hints.ai_family = AF_UNSPEC;
1006 if(!do_ip4 && !do_ip6) {
1009 /* create ip4 and ip6 ports so that return addresses are nice. */
1010 if(do_auto || cfg->num_ifs == 0) {
1012 hints.ai_family = AF_INET6;
1013 if(!ports_create_if(do_auto?"::0":"::1",
1014 do_auto, cfg->do_udp, do_tcp,
1015 &hints, portbuf, &list,
1016 cfg->so_rcvbuf, cfg->so_sndbuf,
1017 cfg->ssl_port, reuseport)) {
1018 listening_ports_free(list);
1023 hints.ai_family = AF_INET;
1024 if(!ports_create_if(do_auto?"0.0.0.0":"127.0.0.1",
1025 do_auto, cfg->do_udp, do_tcp,
1026 &hints, portbuf, &list,
1027 cfg->so_rcvbuf, cfg->so_sndbuf,
1028 cfg->ssl_port, reuseport)) {
1029 listening_ports_free(list);
1033 } else for(i = 0; i<cfg->num_ifs; i++) {
1034 if(str_is_ip6(cfg->ifs[i])) {
1037 hints.ai_family = AF_INET6;
1038 if(!ports_create_if(cfg->ifs[i], 0, cfg->do_udp,
1039 do_tcp, &hints, portbuf, &list,
1040 cfg->so_rcvbuf, cfg->so_sndbuf,
1041 cfg->ssl_port, reuseport)) {
1042 listening_ports_free(list);
1048 hints.ai_family = AF_INET;
1049 if(!ports_create_if(cfg->ifs[i], 0, cfg->do_udp,
1050 do_tcp, &hints, portbuf, &list,
1051 cfg->so_rcvbuf, cfg->so_sndbuf,
1052 cfg->ssl_port, reuseport)) {
1053 listening_ports_free(list);
1061 void listening_ports_free(struct listen_port* list)
1063 struct listen_port* nx;
1066 if(list->fd != -1) {
1070 closesocket(list->fd);
1078 size_t listen_get_mem(struct listen_dnsport* listen)
1080 size_t s = sizeof(*listen) + sizeof(*listen->base) +
1081 sizeof(*listen->udp_buff) +
1082 sldns_buffer_capacity(listen->udp_buff);
1083 struct listen_list* p;
1084 for(p = listen->cps; p; p = p->next) {
1086 s += comm_point_get_mem(p->com);
1091 void listen_stop_accept(struct listen_dnsport* listen)
1093 /* do not stop the ones that have no tcp_free list
1094 * (they have already stopped listening) */
1095 struct listen_list* p;
1096 for(p=listen->cps; p; p=p->next) {
1097 if(p->com->type == comm_tcp_accept &&
1098 p->com->tcp_free != NULL) {
1099 comm_point_stop_listening(p->com);
1104 void listen_start_accept(struct listen_dnsport* listen)
1106 /* do not start the ones that have no tcp_free list, it is no
1107 * use to listen to them because they have no free tcp handlers */
1108 struct listen_list* p;
1109 for(p=listen->cps; p; p=p->next) {
1110 if(p->com->type == comm_tcp_accept &&
1111 p->com->tcp_free != NULL) {
1112 comm_point_start_listening(p->com, -1, -1);