1ae8c6e27Sflorian /*
2ae8c6e27Sflorian  * services/listen_dnsport.c - listen on port 53 for incoming DNS queries.
3ae8c6e27Sflorian  *
4ae8c6e27Sflorian  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5ae8c6e27Sflorian  *
6ae8c6e27Sflorian  * This software is open source.
7ae8c6e27Sflorian  *
8ae8c6e27Sflorian  * Redistribution and use in source and binary forms, with or without
9ae8c6e27Sflorian  * modification, are permitted provided that the following conditions
10ae8c6e27Sflorian  * are met:
11ae8c6e27Sflorian  *
12ae8c6e27Sflorian  * Redistributions of source code must retain the above copyright notice,
13ae8c6e27Sflorian  * this list of conditions and the following disclaimer.
14ae8c6e27Sflorian  *
15ae8c6e27Sflorian  * Redistributions in binary form must reproduce the above copyright notice,
16ae8c6e27Sflorian  * this list of conditions and the following disclaimer in the documentation
17ae8c6e27Sflorian  * and/or other materials provided with the distribution.
18ae8c6e27Sflorian  *
19ae8c6e27Sflorian  * Neither the name of the NLNET LABS nor the names of its contributors may
20ae8c6e27Sflorian  * be used to endorse or promote products derived from this software without
21ae8c6e27Sflorian  * specific prior written permission.
22ae8c6e27Sflorian  *
23ae8c6e27Sflorian  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24ae8c6e27Sflorian  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25ae8c6e27Sflorian  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26ae8c6e27Sflorian  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27ae8c6e27Sflorian  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28ae8c6e27Sflorian  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29ae8c6e27Sflorian  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30ae8c6e27Sflorian  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31ae8c6e27Sflorian  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32ae8c6e27Sflorian  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33ae8c6e27Sflorian  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34ae8c6e27Sflorian  */
35ae8c6e27Sflorian 
36ae8c6e27Sflorian /**
37ae8c6e27Sflorian  * \file
38ae8c6e27Sflorian  *
39ae8c6e27Sflorian  * This file has functions to get queries from clients.
40ae8c6e27Sflorian  */
41ae8c6e27Sflorian #include "config.h"
42ae8c6e27Sflorian #ifdef HAVE_SYS_TYPES_H
43ae8c6e27Sflorian #  include <sys/types.h>
44ae8c6e27Sflorian #endif
45ae8c6e27Sflorian #include <sys/time.h>
46853e076fSflorian #include <limits.h>
47ae8c6e27Sflorian #ifdef USE_TCP_FASTOPEN
48ae8c6e27Sflorian #include <netinet/tcp.h>
49ae8c6e27Sflorian #endif
507a05b9dfSflorian #include <ctype.h>
51ae8c6e27Sflorian #include "services/listen_dnsport.h"
52ae8c6e27Sflorian #include "services/outside_network.h"
53ae8c6e27Sflorian #include "util/netevent.h"
54ae8c6e27Sflorian #include "util/log.h"
55ae8c6e27Sflorian #include "util/config_file.h"
56ae8c6e27Sflorian #include "util/net_help.h"
57ae8c6e27Sflorian #include "sldns/sbuffer.h"
58f4f0f0ceSflorian #include "sldns/parseutil.h"
59e97c6e54Ssthen #include "services/mesh.h"
60e97c6e54Ssthen #include "util/fptr_wlist.h"
61e97c6e54Ssthen #include "util/locks.h"
62ae8c6e27Sflorian 
63ae8c6e27Sflorian #ifdef HAVE_NETDB_H
64ae8c6e27Sflorian #include <netdb.h>
65ae8c6e27Sflorian #endif
66ae8c6e27Sflorian #include <fcntl.h>
67ae8c6e27Sflorian 
68ae8c6e27Sflorian #ifdef HAVE_SYS_UN_H
69ae8c6e27Sflorian #include <sys/un.h>
70ae8c6e27Sflorian #endif
71ae8c6e27Sflorian 
72ae8c6e27Sflorian #ifdef HAVE_SYSTEMD
73ae8c6e27Sflorian #include <systemd/sd-daemon.h>
74ae8c6e27Sflorian #endif
75ae8c6e27Sflorian 
76f4f0f0ceSflorian #ifdef HAVE_IFADDRS_H
77f4f0f0ceSflorian #include <ifaddrs.h>
78f4f0f0ceSflorian #endif
79f4f0f0ceSflorian #ifdef HAVE_NET_IF_H
80f4f0f0ceSflorian #include <net/if.h>
81f4f0f0ceSflorian #endif
82d500c338Sflorian #ifdef HAVE_LINUX_NET_TSTAMP_H
83d500c338Sflorian #include <linux/net_tstamp.h>
84d500c338Sflorian #endif
85ae8c6e27Sflorian /** number of queued TCP connections for listen() */
86ae8c6e27Sflorian #define TCP_BACKLOG 256
87ae8c6e27Sflorian 
88e97c6e54Ssthen #ifndef THREADS_DISABLED
89e97c6e54Ssthen /** lock on the counter of stream buffer memory */
90e97c6e54Ssthen static lock_basic_type stream_wait_count_lock;
91f4f0f0ceSflorian /** lock on the counter of HTTP2 query buffer memory */
92f4f0f0ceSflorian static lock_basic_type http2_query_buffer_count_lock;
93f4f0f0ceSflorian /** lock on the counter of HTTP2 response buffer memory */
94f4f0f0ceSflorian static lock_basic_type http2_response_buffer_count_lock;
95e97c6e54Ssthen #endif
96e97c6e54Ssthen /** size (in bytes) of stream wait buffers */
97e97c6e54Ssthen static size_t stream_wait_count = 0;
98e97c6e54Ssthen /** is the lock initialised for stream wait buffers */
99e97c6e54Ssthen static int stream_wait_lock_inited = 0;
100f4f0f0ceSflorian /** size (in bytes) of HTTP2 query buffers */
101f4f0f0ceSflorian static size_t http2_query_buffer_count = 0;
102f4f0f0ceSflorian /** is the lock initialised for HTTP2 query buffers */
103f4f0f0ceSflorian static int http2_query_buffer_lock_inited = 0;
104f4f0f0ceSflorian /** size (in bytes) of HTTP2 response buffers */
105f4f0f0ceSflorian static size_t http2_response_buffer_count = 0;
106f4f0f0ceSflorian /** is the lock initialised for HTTP2 response buffers */
107f4f0f0ceSflorian static int http2_response_buffer_lock_inited = 0;
108e97c6e54Ssthen 
109ae8c6e27Sflorian /**
110ae8c6e27Sflorian  * Debug print of the getaddrinfo returned address.
111ae8c6e27Sflorian  * @param addr: the address returned.
112ae8c6e27Sflorian  */
113ae8c6e27Sflorian static void
verbose_print_addr(struct addrinfo * addr)114ae8c6e27Sflorian verbose_print_addr(struct addrinfo *addr)
115ae8c6e27Sflorian {
116ae8c6e27Sflorian 	if(verbosity >= VERB_ALGO) {
117ae8c6e27Sflorian 		char buf[100];
118ae8c6e27Sflorian 		void* sinaddr = &((struct sockaddr_in*)addr->ai_addr)->sin_addr;
119ae8c6e27Sflorian #ifdef INET6
120ae8c6e27Sflorian 		if(addr->ai_family == AF_INET6)
121ae8c6e27Sflorian 			sinaddr = &((struct sockaddr_in6*)addr->ai_addr)->
122ae8c6e27Sflorian 				sin6_addr;
123ae8c6e27Sflorian #endif /* INET6 */
124ae8c6e27Sflorian 		if(inet_ntop(addr->ai_family, sinaddr, buf,
125ae8c6e27Sflorian 			(socklen_t)sizeof(buf)) == 0) {
126ae8c6e27Sflorian 			(void)strlcpy(buf, "(null)", sizeof(buf));
127ae8c6e27Sflorian 		}
128ae8c6e27Sflorian 		buf[sizeof(buf)-1] = 0;
129ae8c6e27Sflorian 		verbose(VERB_ALGO, "creating %s%s socket %s %d",
130ae8c6e27Sflorian 			addr->ai_socktype==SOCK_DGRAM?"udp":
131ae8c6e27Sflorian 			addr->ai_socktype==SOCK_STREAM?"tcp":"otherproto",
132ae8c6e27Sflorian 			addr->ai_family==AF_INET?"4":
133ae8c6e27Sflorian 			addr->ai_family==AF_INET6?"6":
134ae8c6e27Sflorian 			"_otherfam", buf,
135ae8c6e27Sflorian 			ntohs(((struct sockaddr_in*)addr->ai_addr)->sin_port));
136ae8c6e27Sflorian 	}
137ae8c6e27Sflorian }
138ae8c6e27Sflorian 
139411c5950Sflorian void
verbose_print_unbound_socket(struct unbound_socket * ub_sock)140411c5950Sflorian verbose_print_unbound_socket(struct unbound_socket* ub_sock)
141411c5950Sflorian {
142411c5950Sflorian 	if(verbosity >= VERB_ALGO) {
143411c5950Sflorian 		log_info("listing of unbound_socket structure:");
144411c5950Sflorian 		verbose_print_addr(ub_sock->addr);
1455c45b740Sflorian 		log_info("s is: %d, fam is: %s, acl: %s", ub_sock->s,
1465c45b740Sflorian 			ub_sock->fam == AF_INET?"AF_INET":"AF_INET6",
1475c45b740Sflorian 			ub_sock->acl?"yes":"no");
148411c5950Sflorian 	}
149411c5950Sflorian }
150411c5950Sflorian 
151ae8c6e27Sflorian #ifdef HAVE_SYSTEMD
152ae8c6e27Sflorian static int
systemd_get_activated(int family,int socktype,int listen,struct sockaddr * addr,socklen_t addrlen,const char * path)153ae8c6e27Sflorian systemd_get_activated(int family, int socktype, int listen,
154ae8c6e27Sflorian 		      struct sockaddr *addr, socklen_t addrlen,
155ae8c6e27Sflorian 		      const char *path)
156ae8c6e27Sflorian {
157ae8c6e27Sflorian 	int i = 0;
158ae8c6e27Sflorian 	int r = 0;
159ae8c6e27Sflorian 	int s = -1;
160ae8c6e27Sflorian 	const char* listen_pid, *listen_fds;
161ae8c6e27Sflorian 
162ae8c6e27Sflorian 	/* We should use "listen" option only for stream protocols. For UDP it should be -1 */
163ae8c6e27Sflorian 
164ae8c6e27Sflorian 	if((r = sd_booted()) < 1) {
165ae8c6e27Sflorian 		if(r == 0)
166ae8c6e27Sflorian 			log_warn("systemd is not running");
167ae8c6e27Sflorian 		else
168ae8c6e27Sflorian 			log_err("systemd sd_booted(): %s", strerror(-r));
169ae8c6e27Sflorian 		return -1;
170ae8c6e27Sflorian 	}
171ae8c6e27Sflorian 
172ae8c6e27Sflorian 	listen_pid = getenv("LISTEN_PID");
173ae8c6e27Sflorian 	listen_fds = getenv("LISTEN_FDS");
174ae8c6e27Sflorian 
175ae8c6e27Sflorian 	if (!listen_pid) {
176ae8c6e27Sflorian 		log_warn("Systemd mandatory ENV variable is not defined: LISTEN_PID");
177ae8c6e27Sflorian 		return -1;
178ae8c6e27Sflorian 	}
179ae8c6e27Sflorian 
180ae8c6e27Sflorian 	if (!listen_fds) {
181ae8c6e27Sflorian 		log_warn("Systemd mandatory ENV variable is not defined: LISTEN_FDS");
182ae8c6e27Sflorian 		return -1;
183ae8c6e27Sflorian 	}
184ae8c6e27Sflorian 
185ae8c6e27Sflorian 	if((r = sd_listen_fds(0)) < 1) {
186ae8c6e27Sflorian 		if(r == 0)
187ae8c6e27Sflorian 			log_warn("systemd: did not return socket, check unit configuration");
188ae8c6e27Sflorian 		else
189ae8c6e27Sflorian 			log_err("systemd sd_listen_fds(): %s", strerror(-r));
190ae8c6e27Sflorian 		return -1;
191ae8c6e27Sflorian 	}
192ae8c6e27Sflorian 
193ae8c6e27Sflorian 	for(i = 0; i < r; i++) {
194ae8c6e27Sflorian 		if(sd_is_socket(SD_LISTEN_FDS_START + i, family, socktype, listen)) {
195ae8c6e27Sflorian 			s = SD_LISTEN_FDS_START + i;
196ae8c6e27Sflorian 			break;
197ae8c6e27Sflorian 		}
198ae8c6e27Sflorian 	}
199ae8c6e27Sflorian 	if (s == -1) {
200ae8c6e27Sflorian 		if (addr)
201ae8c6e27Sflorian 			log_err_addr("systemd sd_listen_fds()",
202ae8c6e27Sflorian 				     "no such socket",
203ae8c6e27Sflorian 				     (struct sockaddr_storage *)addr, addrlen);
204ae8c6e27Sflorian 		else
205ae8c6e27Sflorian 			log_err("systemd sd_listen_fds(): %s", path);
206ae8c6e27Sflorian 	}
207ae8c6e27Sflorian 	return s;
208ae8c6e27Sflorian }
209ae8c6e27Sflorian #endif
210ae8c6e27Sflorian 
211ae8c6e27Sflorian int
create_udp_sock(int family,int socktype,struct sockaddr * addr,socklen_t addrlen,int v6only,int * inuse,int * noproto,int rcv,int snd,int listen,int * reuseport,int transparent,int freebind,int use_systemd,int dscp)212ae8c6e27Sflorian create_udp_sock(int family, int socktype, struct sockaddr* addr,
213ae8c6e27Sflorian         socklen_t addrlen, int v6only, int* inuse, int* noproto,
214ae8c6e27Sflorian 	int rcv, int snd, int listen, int* reuseport, int transparent,
215e47fef9eSflorian 	int freebind, int use_systemd, int dscp)
216ae8c6e27Sflorian {
217ae8c6e27Sflorian 	int s;
218e47fef9eSflorian 	char* err;
219ae8c6e27Sflorian #if defined(SO_REUSEADDR) || defined(SO_REUSEPORT) || defined(IPV6_USE_MIN_MTU)  || defined(IP_TRANSPARENT) || defined(IP_BINDANY) || defined(IP_FREEBIND) || defined (SO_BINDANY)
220ae8c6e27Sflorian 	int on=1;
221ae8c6e27Sflorian #endif
222ae8c6e27Sflorian #ifdef IPV6_MTU
223ae8c6e27Sflorian 	int mtu = IPV6_MIN_MTU;
224ae8c6e27Sflorian #endif
225ae8c6e27Sflorian #if !defined(SO_RCVBUFFORCE) && !defined(SO_RCVBUF)
226ae8c6e27Sflorian 	(void)rcv;
227ae8c6e27Sflorian #endif
228ae8c6e27Sflorian #if !defined(SO_SNDBUFFORCE) && !defined(SO_SNDBUF)
229ae8c6e27Sflorian 	(void)snd;
230ae8c6e27Sflorian #endif
231ae8c6e27Sflorian #ifndef IPV6_V6ONLY
232ae8c6e27Sflorian 	(void)v6only;
233ae8c6e27Sflorian #endif
234ae8c6e27Sflorian #if !defined(IP_TRANSPARENT) && !defined(IP_BINDANY) && !defined(SO_BINDANY)
235ae8c6e27Sflorian 	(void)transparent;
236ae8c6e27Sflorian #endif
237ae8c6e27Sflorian #if !defined(IP_FREEBIND)
238ae8c6e27Sflorian 	(void)freebind;
239ae8c6e27Sflorian #endif
240ae8c6e27Sflorian #ifdef HAVE_SYSTEMD
241ae8c6e27Sflorian 	int got_fd_from_systemd = 0;
242ae8c6e27Sflorian 
243ae8c6e27Sflorian 	if (!use_systemd
244ae8c6e27Sflorian 	    || (use_systemd
245ae8c6e27Sflorian 		&& (s = systemd_get_activated(family, socktype, -1, addr,
246ae8c6e27Sflorian 					      addrlen, NULL)) == -1)) {
247ae8c6e27Sflorian #else
248ae8c6e27Sflorian 	(void)use_systemd;
249ae8c6e27Sflorian #endif
250ae8c6e27Sflorian 	if((s = socket(family, socktype, 0)) == -1) {
251ae8c6e27Sflorian 		*inuse = 0;
252ae8c6e27Sflorian #ifndef USE_WINSOCK
253ae8c6e27Sflorian 		if(errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
254ae8c6e27Sflorian 			*noproto = 1;
255ae8c6e27Sflorian 			return -1;
256ae8c6e27Sflorian 		}
257ae8c6e27Sflorian #else
258ae8c6e27Sflorian 		if(WSAGetLastError() == WSAEAFNOSUPPORT ||
259ae8c6e27Sflorian 			WSAGetLastError() == WSAEPROTONOSUPPORT) {
260ae8c6e27Sflorian 			*noproto = 1;
261ae8c6e27Sflorian 			return -1;
262ae8c6e27Sflorian 		}
263ae8c6e27Sflorian #endif
264f4f0f0ceSflorian 		log_err("can't create socket: %s", sock_strerror(errno));
265ae8c6e27Sflorian 		*noproto = 0;
266ae8c6e27Sflorian 		return -1;
267ae8c6e27Sflorian 	}
268ae8c6e27Sflorian #ifdef HAVE_SYSTEMD
269ae8c6e27Sflorian 	} else {
270ae8c6e27Sflorian 		got_fd_from_systemd = 1;
271ae8c6e27Sflorian 	}
272ae8c6e27Sflorian #endif
273ae8c6e27Sflorian 	if(listen) {
274ae8c6e27Sflorian #ifdef SO_REUSEADDR
275ae8c6e27Sflorian 		if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*)&on,
276ae8c6e27Sflorian 			(socklen_t)sizeof(on)) < 0) {
277ae8c6e27Sflorian 			log_err("setsockopt(.. SO_REUSEADDR ..) failed: %s",
278f4f0f0ceSflorian 				sock_strerror(errno));
279f4f0f0ceSflorian #ifndef USE_WINSOCK
280ae8c6e27Sflorian 			if(errno != ENOSYS) {
281ae8c6e27Sflorian 				close(s);
282ae8c6e27Sflorian 				*noproto = 0;
283ae8c6e27Sflorian 				*inuse = 0;
284ae8c6e27Sflorian 				return -1;
285ae8c6e27Sflorian 			}
286ae8c6e27Sflorian #else
287ae8c6e27Sflorian 			closesocket(s);
288ae8c6e27Sflorian 			*noproto = 0;
289ae8c6e27Sflorian 			*inuse = 0;
290ae8c6e27Sflorian 			return -1;
291ae8c6e27Sflorian #endif
292ae8c6e27Sflorian 		}
293ae8c6e27Sflorian #endif /* SO_REUSEADDR */
294ae8c6e27Sflorian #ifdef SO_REUSEPORT
295ae8c6e27Sflorian #  ifdef SO_REUSEPORT_LB
296ae8c6e27Sflorian 		/* on FreeBSD 12 we have SO_REUSEPORT_LB that does loadbalance
297ae8c6e27Sflorian 		 * like SO_REUSEPORT on Linux.  This is what the users want
298ae8c6e27Sflorian 		 * with the config option in unbound.conf; if we actually
299ae8c6e27Sflorian 		 * need local address and port reuse they'll also need to
300ae8c6e27Sflorian 		 * have SO_REUSEPORT set for them, assume it was _LB they want.
301ae8c6e27Sflorian 		 */
302ae8c6e27Sflorian 		if (reuseport && *reuseport &&
303ae8c6e27Sflorian 		    setsockopt(s, SOL_SOCKET, SO_REUSEPORT_LB, (void*)&on,
304ae8c6e27Sflorian 			(socklen_t)sizeof(on)) < 0) {
305ae8c6e27Sflorian #ifdef ENOPROTOOPT
306ae8c6e27Sflorian 			if(errno != ENOPROTOOPT || verbosity >= 3)
307ae8c6e27Sflorian 				log_warn("setsockopt(.. SO_REUSEPORT_LB ..) failed: %s",
308ae8c6e27Sflorian 					strerror(errno));
309ae8c6e27Sflorian #endif
310ae8c6e27Sflorian 			/* this option is not essential, we can continue */
311ae8c6e27Sflorian 			*reuseport = 0;
312ae8c6e27Sflorian 		}
313ae8c6e27Sflorian #  else /* no SO_REUSEPORT_LB */
314ae8c6e27Sflorian 
315ae8c6e27Sflorian 		/* try to set SO_REUSEPORT so that incoming
316ae8c6e27Sflorian 		 * queries are distributed evenly among the receiving threads.
317ae8c6e27Sflorian 		 * Each thread must have its own socket bound to the same port,
318ae8c6e27Sflorian 		 * with SO_REUSEPORT set on each socket.
319ae8c6e27Sflorian 		 */
320ae8c6e27Sflorian 		if (reuseport && *reuseport &&
321ae8c6e27Sflorian 		    setsockopt(s, SOL_SOCKET, SO_REUSEPORT, (void*)&on,
322ae8c6e27Sflorian 			(socklen_t)sizeof(on)) < 0) {
323ae8c6e27Sflorian #ifdef ENOPROTOOPT
324ae8c6e27Sflorian 			if(errno != ENOPROTOOPT || verbosity >= 3)
325ae8c6e27Sflorian 				log_warn("setsockopt(.. SO_REUSEPORT ..) failed: %s",
326ae8c6e27Sflorian 					strerror(errno));
327ae8c6e27Sflorian #endif
328ae8c6e27Sflorian 			/* this option is not essential, we can continue */
329ae8c6e27Sflorian 			*reuseport = 0;
330ae8c6e27Sflorian 		}
331ae8c6e27Sflorian #  endif /* SO_REUSEPORT_LB */
332ae8c6e27Sflorian #else
333ae8c6e27Sflorian 		(void)reuseport;
334ae8c6e27Sflorian #endif /* defined(SO_REUSEPORT) */
335ae8c6e27Sflorian #ifdef IP_TRANSPARENT
336ae8c6e27Sflorian 		if (transparent &&
337ae8c6e27Sflorian 		    setsockopt(s, IPPROTO_IP, IP_TRANSPARENT, (void*)&on,
338ae8c6e27Sflorian 		    (socklen_t)sizeof(on)) < 0) {
339ae8c6e27Sflorian 			log_warn("setsockopt(.. IP_TRANSPARENT ..) failed: %s",
340ae8c6e27Sflorian 			strerror(errno));
341ae8c6e27Sflorian 		}
342ae8c6e27Sflorian #elif defined(IP_BINDANY)
343ae8c6e27Sflorian 		if (transparent &&
344ae8c6e27Sflorian 		    setsockopt(s, (family==AF_INET6? IPPROTO_IPV6:IPPROTO_IP),
345ae8c6e27Sflorian 		    (family == AF_INET6? IPV6_BINDANY:IP_BINDANY),
346ae8c6e27Sflorian 		    (void*)&on, (socklen_t)sizeof(on)) < 0) {
347ae8c6e27Sflorian 			log_warn("setsockopt(.. IP%s_BINDANY ..) failed: %s",
348ae8c6e27Sflorian 			(family==AF_INET6?"V6":""), strerror(errno));
349ae8c6e27Sflorian 		}
350ae8c6e27Sflorian #elif defined(SO_BINDANY)
351ae8c6e27Sflorian 		if (transparent &&
352ae8c6e27Sflorian 		    setsockopt(s, SOL_SOCKET, SO_BINDANY, (void*)&on,
353ae8c6e27Sflorian 		    (socklen_t)sizeof(on)) < 0) {
354ae8c6e27Sflorian 			log_warn("setsockopt(.. SO_BINDANY ..) failed: %s",
355ae8c6e27Sflorian 			strerror(errno));
356ae8c6e27Sflorian 		}
357ae8c6e27Sflorian #endif /* IP_TRANSPARENT || IP_BINDANY || SO_BINDANY */
358ae8c6e27Sflorian 	}
359ae8c6e27Sflorian #ifdef IP_FREEBIND
360ae8c6e27Sflorian 	if(freebind &&
361ae8c6e27Sflorian 	    setsockopt(s, IPPROTO_IP, IP_FREEBIND, (void*)&on,
362ae8c6e27Sflorian 	    (socklen_t)sizeof(on)) < 0) {
363ae8c6e27Sflorian 		log_warn("setsockopt(.. IP_FREEBIND ..) failed: %s",
364ae8c6e27Sflorian 		strerror(errno));
365ae8c6e27Sflorian 	}
366ae8c6e27Sflorian #endif /* IP_FREEBIND */
367ae8c6e27Sflorian 	if(rcv) {
368ae8c6e27Sflorian #ifdef SO_RCVBUF
369ae8c6e27Sflorian 		int got;
370ae8c6e27Sflorian 		socklen_t slen = (socklen_t)sizeof(got);
371ae8c6e27Sflorian #  ifdef SO_RCVBUFFORCE
372ae8c6e27Sflorian 		/* Linux specific: try to use root permission to override
373ae8c6e27Sflorian 		 * system limits on rcvbuf. The limit is stored in
374ae8c6e27Sflorian 		 * /proc/sys/net/core/rmem_max or sysctl net.core.rmem_max */
375ae8c6e27Sflorian 		if(setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, (void*)&rcv,
376ae8c6e27Sflorian 			(socklen_t)sizeof(rcv)) < 0) {
377ae8c6e27Sflorian 			if(errno != EPERM) {
378ae8c6e27Sflorian 				log_err("setsockopt(..., SO_RCVBUFFORCE, "
379f4f0f0ceSflorian 					"...) failed: %s", sock_strerror(errno));
380f4f0f0ceSflorian 				sock_close(s);
381ae8c6e27Sflorian 				*noproto = 0;
382ae8c6e27Sflorian 				*inuse = 0;
383ae8c6e27Sflorian 				return -1;
384ae8c6e27Sflorian 			}
385ae8c6e27Sflorian #  endif /* SO_RCVBUFFORCE */
386ae8c6e27Sflorian 			if(setsockopt(s, SOL_SOCKET, SO_RCVBUF, (void*)&rcv,
387ae8c6e27Sflorian 				(socklen_t)sizeof(rcv)) < 0) {
388ae8c6e27Sflorian 				log_err("setsockopt(..., SO_RCVBUF, "
389f4f0f0ceSflorian 					"...) failed: %s", sock_strerror(errno));
390f4f0f0ceSflorian 				sock_close(s);
391ae8c6e27Sflorian 				*noproto = 0;
392ae8c6e27Sflorian 				*inuse = 0;
393ae8c6e27Sflorian 				return -1;
394ae8c6e27Sflorian 			}
395ae8c6e27Sflorian 			/* check if we got the right thing or if system
396ae8c6e27Sflorian 			 * reduced to some system max.  Warn if so */
397ae8c6e27Sflorian 			if(getsockopt(s, SOL_SOCKET, SO_RCVBUF, (void*)&got,
398ae8c6e27Sflorian 				&slen) >= 0 && got < rcv/2) {
399ae8c6e27Sflorian 				log_warn("so-rcvbuf %u was not granted. "
400ae8c6e27Sflorian 					"Got %u. To fix: start with "
401ae8c6e27Sflorian 					"root permissions(linux) or sysctl "
402ae8c6e27Sflorian 					"bigger net.core.rmem_max(linux) or "
403ae8c6e27Sflorian 					"kern.ipc.maxsockbuf(bsd) values.",
404ae8c6e27Sflorian 					(unsigned)rcv, (unsigned)got);
405ae8c6e27Sflorian 			}
406ae8c6e27Sflorian #  ifdef SO_RCVBUFFORCE
407ae8c6e27Sflorian 		}
408ae8c6e27Sflorian #  endif
409ae8c6e27Sflorian #endif /* SO_RCVBUF */
410ae8c6e27Sflorian 	}
411ae8c6e27Sflorian 	/* first do RCVBUF as the receive buffer is more important */
412ae8c6e27Sflorian 	if(snd) {
413ae8c6e27Sflorian #ifdef SO_SNDBUF
414ae8c6e27Sflorian 		int got;
415ae8c6e27Sflorian 		socklen_t slen = (socklen_t)sizeof(got);
416ae8c6e27Sflorian #  ifdef SO_SNDBUFFORCE
417ae8c6e27Sflorian 		/* Linux specific: try to use root permission to override
418ae8c6e27Sflorian 		 * system limits on sndbuf. The limit is stored in
419ae8c6e27Sflorian 		 * /proc/sys/net/core/wmem_max or sysctl net.core.wmem_max */
420ae8c6e27Sflorian 		if(setsockopt(s, SOL_SOCKET, SO_SNDBUFFORCE, (void*)&snd,
421ae8c6e27Sflorian 			(socklen_t)sizeof(snd)) < 0) {
422ae8c6e27Sflorian 			if(errno != EPERM) {
423ae8c6e27Sflorian 				log_err("setsockopt(..., SO_SNDBUFFORCE, "
424f4f0f0ceSflorian 					"...) failed: %s", sock_strerror(errno));
425f4f0f0ceSflorian 				sock_close(s);
426ae8c6e27Sflorian 				*noproto = 0;
427ae8c6e27Sflorian 				*inuse = 0;
428ae8c6e27Sflorian 				return -1;
429ae8c6e27Sflorian 			}
430ae8c6e27Sflorian #  endif /* SO_SNDBUFFORCE */
431ae8c6e27Sflorian 			if(setsockopt(s, SOL_SOCKET, SO_SNDBUF, (void*)&snd,
432ae8c6e27Sflorian 				(socklen_t)sizeof(snd)) < 0) {
433ae8c6e27Sflorian 				log_err("setsockopt(..., SO_SNDBUF, "
434f4f0f0ceSflorian 					"...) failed: %s", sock_strerror(errno));
435f4f0f0ceSflorian 				sock_close(s);
436ae8c6e27Sflorian 				*noproto = 0;
437ae8c6e27Sflorian 				*inuse = 0;
438ae8c6e27Sflorian 				return -1;
439ae8c6e27Sflorian 			}
440ae8c6e27Sflorian 			/* check if we got the right thing or if system
441ae8c6e27Sflorian 			 * reduced to some system max.  Warn if so */
442ae8c6e27Sflorian 			if(getsockopt(s, SOL_SOCKET, SO_SNDBUF, (void*)&got,
443ae8c6e27Sflorian 				&slen) >= 0 && got < snd/2) {
444ae8c6e27Sflorian 				log_warn("so-sndbuf %u was not granted. "
445ae8c6e27Sflorian 					"Got %u. To fix: start with "
446ae8c6e27Sflorian 					"root permissions(linux) or sysctl "
447ae8c6e27Sflorian 					"bigger net.core.wmem_max(linux) or "
448ae8c6e27Sflorian 					"kern.ipc.maxsockbuf(bsd) values.",
449ae8c6e27Sflorian 					(unsigned)snd, (unsigned)got);
450ae8c6e27Sflorian 			}
451ae8c6e27Sflorian #  ifdef SO_SNDBUFFORCE
452ae8c6e27Sflorian 		}
453ae8c6e27Sflorian #  endif
454ae8c6e27Sflorian #endif /* SO_SNDBUF */
455ae8c6e27Sflorian 	}
456e47fef9eSflorian 	err = set_ip_dscp(s, family, dscp);
457e47fef9eSflorian 	if(err != NULL)
458e47fef9eSflorian 		log_warn("error setting IP DiffServ codepoint %d on UDP socket: %s", dscp, err);
459ae8c6e27Sflorian 	if(family == AF_INET6) {
460411c5950Sflorian # if defined(IPV6_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
461411c5950Sflorian 		int omit6_set = 0;
462411c5950Sflorian 		int action;
463411c5950Sflorian # endif
464ae8c6e27Sflorian # if defined(IPV6_V6ONLY)
4655c45b740Sflorian 		if(v6only
4665c45b740Sflorian #   ifdef HAVE_SYSTEMD
4675c45b740Sflorian 			/* Systemd wants to control if the socket is v6 only
4685c45b740Sflorian 			 * or both, with BindIPv6Only=default, ipv6-only or
4695c45b740Sflorian 			 * both in systemd.socket, so it is not set here. */
4705c45b740Sflorian 			&& !got_fd_from_systemd
4715c45b740Sflorian #   endif
4725c45b740Sflorian 			) {
473ae8c6e27Sflorian 			int val=(v6only==2)?0:1;
474ae8c6e27Sflorian 			if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
475ae8c6e27Sflorian 				(void*)&val, (socklen_t)sizeof(val)) < 0) {
476ae8c6e27Sflorian 				log_err("setsockopt(..., IPV6_V6ONLY"
477f4f0f0ceSflorian 					", ...) failed: %s", sock_strerror(errno));
478f4f0f0ceSflorian 				sock_close(s);
479ae8c6e27Sflorian 				*noproto = 0;
480ae8c6e27Sflorian 				*inuse = 0;
481ae8c6e27Sflorian 				return -1;
482ae8c6e27Sflorian 			}
483ae8c6e27Sflorian 		}
484ae8c6e27Sflorian # endif
485ae8c6e27Sflorian # if defined(IPV6_USE_MIN_MTU)
486ae8c6e27Sflorian 		/*
487ae8c6e27Sflorian 		 * There is no fragmentation of IPv6 datagrams
488ae8c6e27Sflorian 		 * during forwarding in the network. Therefore
489ae8c6e27Sflorian 		 * we do not send UDP datagrams larger than
490ae8c6e27Sflorian 		 * the minimum IPv6 MTU of 1280 octets. The
491ae8c6e27Sflorian 		 * EDNS0 message length can be larger if the
492ae8c6e27Sflorian 		 * network stack supports IPV6_USE_MIN_MTU.
493ae8c6e27Sflorian 		 */
494ae8c6e27Sflorian 		if (setsockopt(s, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
495ae8c6e27Sflorian 			(void*)&on, (socklen_t)sizeof(on)) < 0) {
496ae8c6e27Sflorian 			log_err("setsockopt(..., IPV6_USE_MIN_MTU, "
497f4f0f0ceSflorian 				"...) failed: %s", sock_strerror(errno));
498f4f0f0ceSflorian 			sock_close(s);
499ae8c6e27Sflorian 			*noproto = 0;
500ae8c6e27Sflorian 			*inuse = 0;
501ae8c6e27Sflorian 			return -1;
502ae8c6e27Sflorian 		}
503ae8c6e27Sflorian # elif defined(IPV6_MTU)
5046d08cb1bSflorian #   ifndef USE_WINSOCK
505ae8c6e27Sflorian 		/*
506ae8c6e27Sflorian 		 * On Linux, to send no larger than 1280, the PMTUD is
507ae8c6e27Sflorian 		 * disabled by default for datagrams anyway, so we set
508ae8c6e27Sflorian 		 * the MTU to use.
509ae8c6e27Sflorian 		 */
510ae8c6e27Sflorian 		if (setsockopt(s, IPPROTO_IPV6, IPV6_MTU,
511ae8c6e27Sflorian 			(void*)&mtu, (socklen_t)sizeof(mtu)) < 0) {
512ae8c6e27Sflorian 			log_err("setsockopt(..., IPV6_MTU, ...) failed: %s",
513f4f0f0ceSflorian 				sock_strerror(errno));
514f4f0f0ceSflorian 			sock_close(s);
515ae8c6e27Sflorian 			*noproto = 0;
516ae8c6e27Sflorian 			*inuse = 0;
517ae8c6e27Sflorian 			return -1;
518ae8c6e27Sflorian 		}
5196d08cb1bSflorian #   elif defined(IPV6_USER_MTU)
5206d08cb1bSflorian 		/* As later versions of the mingw crosscompiler define
5216d08cb1bSflorian 		 * IPV6_MTU, do the same for windows but use IPV6_USER_MTU
5226d08cb1bSflorian 		 * instead which is writable; IPV6_MTU is readonly there. */
5236d08cb1bSflorian 		if (setsockopt(s, IPPROTO_IPV6, IPV6_USER_MTU,
5246d08cb1bSflorian 			(void*)&mtu, (socklen_t)sizeof(mtu)) < 0) {
5255c45b740Sflorian 			if (WSAGetLastError() != WSAENOPROTOOPT) {
5266d08cb1bSflorian 				log_err("setsockopt(..., IPV6_USER_MTU, ...) failed: %s",
5276d08cb1bSflorian 					wsa_strerror(WSAGetLastError()));
5286d08cb1bSflorian 				sock_close(s);
5296d08cb1bSflorian 				*noproto = 0;
5306d08cb1bSflorian 				*inuse = 0;
5316d08cb1bSflorian 				return -1;
5326d08cb1bSflorian 			}
5335c45b740Sflorian 		}
5346d08cb1bSflorian #   endif /* USE_WINSOCK */
535ae8c6e27Sflorian # endif /* IPv6 MTU */
536411c5950Sflorian # if defined(IPV6_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
537411c5950Sflorian #  if defined(IP_PMTUDISC_OMIT)
538411c5950Sflorian 		action = IP_PMTUDISC_OMIT;
539411c5950Sflorian 		if (setsockopt(s, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
540411c5950Sflorian 			&action, (socklen_t)sizeof(action)) < 0) {
541411c5950Sflorian 
542411c5950Sflorian 			if (errno != EINVAL) {
543411c5950Sflorian 				log_err("setsockopt(..., IPV6_MTU_DISCOVER, IP_PMTUDISC_OMIT...) failed: %s",
544411c5950Sflorian 					strerror(errno));
545411c5950Sflorian 				sock_close(s);
546411c5950Sflorian 				*noproto = 0;
547411c5950Sflorian 				*inuse = 0;
548411c5950Sflorian 				return -1;
549411c5950Sflorian 			}
550411c5950Sflorian 		}
551411c5950Sflorian 		else
552411c5950Sflorian 		{
553411c5950Sflorian 		    omit6_set = 1;
554411c5950Sflorian 		}
555411c5950Sflorian #  endif
556411c5950Sflorian 		if (omit6_set == 0) {
557411c5950Sflorian 			action = IP_PMTUDISC_DONT;
558411c5950Sflorian 			if (setsockopt(s, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
559411c5950Sflorian 				&action, (socklen_t)sizeof(action)) < 0) {
560411c5950Sflorian 				log_err("setsockopt(..., IPV6_MTU_DISCOVER, IP_PMTUDISC_DONT...) failed: %s",
561411c5950Sflorian 					strerror(errno));
562411c5950Sflorian 				sock_close(s);
563411c5950Sflorian 				*noproto = 0;
564411c5950Sflorian 				*inuse = 0;
565411c5950Sflorian 				return -1;
566411c5950Sflorian 			}
567411c5950Sflorian 		}
568411c5950Sflorian # endif /* IPV6_MTU_DISCOVER */
569ae8c6e27Sflorian 	} else if(family == AF_INET) {
570ae8c6e27Sflorian #  if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
571ae8c6e27Sflorian /* linux 3.15 has IP_PMTUDISC_OMIT, Hannes Frederic Sowa made it so that
572ae8c6e27Sflorian  * PMTU information is not accepted, but fragmentation is allowed
573ae8c6e27Sflorian  * if and only if the packet size exceeds the outgoing interface MTU
574ae8c6e27Sflorian  * (and also uses the interface mtu to determine the size of the packets).
575ae8c6e27Sflorian  * So there won't be any EMSGSIZE error.  Against DNS fragmentation attacks.
576ae8c6e27Sflorian  * FreeBSD already has same semantics without setting the option. */
577ae8c6e27Sflorian 		int omit_set = 0;
578ae8c6e27Sflorian 		int action;
579ae8c6e27Sflorian #   if defined(IP_PMTUDISC_OMIT)
580ae8c6e27Sflorian 		action = IP_PMTUDISC_OMIT;
581ae8c6e27Sflorian 		if (setsockopt(s, IPPROTO_IP, IP_MTU_DISCOVER,
582ae8c6e27Sflorian 			&action, (socklen_t)sizeof(action)) < 0) {
583ae8c6e27Sflorian 
584ae8c6e27Sflorian 			if (errno != EINVAL) {
585ae8c6e27Sflorian 				log_err("setsockopt(..., IP_MTU_DISCOVER, IP_PMTUDISC_OMIT...) failed: %s",
586ae8c6e27Sflorian 					strerror(errno));
587f4f0f0ceSflorian 				sock_close(s);
588ae8c6e27Sflorian 				*noproto = 0;
589ae8c6e27Sflorian 				*inuse = 0;
590ae8c6e27Sflorian 				return -1;
591ae8c6e27Sflorian 			}
592ae8c6e27Sflorian 		}
593ae8c6e27Sflorian 		else
594ae8c6e27Sflorian 		{
595ae8c6e27Sflorian 		    omit_set = 1;
596ae8c6e27Sflorian 		}
597ae8c6e27Sflorian #   endif
598ae8c6e27Sflorian 		if (omit_set == 0) {
599ae8c6e27Sflorian    			action = IP_PMTUDISC_DONT;
600ae8c6e27Sflorian 			if (setsockopt(s, IPPROTO_IP, IP_MTU_DISCOVER,
601ae8c6e27Sflorian 				&action, (socklen_t)sizeof(action)) < 0) {
602ae8c6e27Sflorian 				log_err("setsockopt(..., IP_MTU_DISCOVER, IP_PMTUDISC_DONT...) failed: %s",
603ae8c6e27Sflorian 					strerror(errno));
604f4f0f0ceSflorian 				sock_close(s);
605ae8c6e27Sflorian 				*noproto = 0;
606ae8c6e27Sflorian 				*inuse = 0;
607ae8c6e27Sflorian 				return -1;
608ae8c6e27Sflorian 			}
609ae8c6e27Sflorian 		}
610853e076fSflorian #  elif defined(IP_DONTFRAG) && !defined(__APPLE__)
611853e076fSflorian 		/* the IP_DONTFRAG option if defined in the 11.0 OSX headers,
612853e076fSflorian 		 * but does not work on that version, so we exclude it */
613ae8c6e27Sflorian 		int off = 0;
614ae8c6e27Sflorian 		if (setsockopt(s, IPPROTO_IP, IP_DONTFRAG,
615ae8c6e27Sflorian 			&off, (socklen_t)sizeof(off)) < 0) {
616ae8c6e27Sflorian 			log_err("setsockopt(..., IP_DONTFRAG, ...) failed: %s",
617ae8c6e27Sflorian 				strerror(errno));
618f4f0f0ceSflorian 			sock_close(s);
619ae8c6e27Sflorian 			*noproto = 0;
620ae8c6e27Sflorian 			*inuse = 0;
621ae8c6e27Sflorian 			return -1;
622ae8c6e27Sflorian 		}
623ae8c6e27Sflorian #  endif /* IPv4 MTU */
624ae8c6e27Sflorian 	}
625ae8c6e27Sflorian 	if(
626ae8c6e27Sflorian #ifdef HAVE_SYSTEMD
627ae8c6e27Sflorian 		!got_fd_from_systemd &&
628ae8c6e27Sflorian #endif
629ae8c6e27Sflorian 		bind(s, (struct sockaddr*)addr, addrlen) != 0) {
630ae8c6e27Sflorian 		*noproto = 0;
631ae8c6e27Sflorian 		*inuse = 0;
632ae8c6e27Sflorian #ifndef USE_WINSOCK
633ae8c6e27Sflorian #ifdef EADDRINUSE
634ae8c6e27Sflorian 		*inuse = (errno == EADDRINUSE);
635ae8c6e27Sflorian 		/* detect freebsd jail with no ipv6 permission */
636ae8c6e27Sflorian 		if(family==AF_INET6 && errno==EINVAL)
637ae8c6e27Sflorian 			*noproto = 1;
638ae8c6e27Sflorian 		else if(errno != EADDRINUSE &&
639ae8c6e27Sflorian 			!(errno == EACCES && verbosity < 4 && !listen)
640ae8c6e27Sflorian #ifdef EADDRNOTAVAIL
641ae8c6e27Sflorian 			&& !(errno == EADDRNOTAVAIL && verbosity < 4 && !listen)
642ae8c6e27Sflorian #endif
643ae8c6e27Sflorian 			) {
644ae8c6e27Sflorian 			log_err_addr("can't bind socket", strerror(errno),
645ae8c6e27Sflorian 				(struct sockaddr_storage*)addr, addrlen);
646ae8c6e27Sflorian 		}
647ae8c6e27Sflorian #endif /* EADDRINUSE */
648ae8c6e27Sflorian #else /* USE_WINSOCK */
649ae8c6e27Sflorian 		if(WSAGetLastError() != WSAEADDRINUSE &&
650ae8c6e27Sflorian 			WSAGetLastError() != WSAEADDRNOTAVAIL &&
651ae8c6e27Sflorian 			!(WSAGetLastError() == WSAEACCES && verbosity < 4 && !listen)) {
652ae8c6e27Sflorian 			log_err_addr("can't bind socket",
653ae8c6e27Sflorian 				wsa_strerror(WSAGetLastError()),
654ae8c6e27Sflorian 				(struct sockaddr_storage*)addr, addrlen);
655ae8c6e27Sflorian 		}
656ae8c6e27Sflorian #endif /* USE_WINSOCK */
657f4f0f0ceSflorian 		sock_close(s);
658ae8c6e27Sflorian 		return -1;
659ae8c6e27Sflorian 	}
660ae8c6e27Sflorian 	if(!fd_set_nonblock(s)) {
661ae8c6e27Sflorian 		*noproto = 0;
662ae8c6e27Sflorian 		*inuse = 0;
663f4f0f0ceSflorian 		sock_close(s);
664ae8c6e27Sflorian 		return -1;
665ae8c6e27Sflorian 	}
666ae8c6e27Sflorian 	return s;
667ae8c6e27Sflorian }
668ae8c6e27Sflorian 
669ae8c6e27Sflorian int
create_tcp_accept_sock(struct addrinfo * addr,int v6only,int * noproto,int * reuseport,int transparent,int mss,int nodelay,int freebind,int use_systemd,int dscp)670ae8c6e27Sflorian create_tcp_accept_sock(struct addrinfo *addr, int v6only, int* noproto,
671f4f0f0ceSflorian 	int* reuseport, int transparent, int mss, int nodelay, int freebind,
672f4f0f0ceSflorian 	int use_systemd, int dscp)
673ae8c6e27Sflorian {
674ae8c6e27Sflorian 	int s;
675e47fef9eSflorian 	char* err;
676ae8c6e27Sflorian #if defined(SO_REUSEADDR) || defined(SO_REUSEPORT) || defined(IPV6_V6ONLY) || defined(IP_TRANSPARENT) || defined(IP_BINDANY) || defined(IP_FREEBIND) || defined(SO_BINDANY)
677ae8c6e27Sflorian 	int on = 1;
678ae8c6e27Sflorian #endif
679ae8c6e27Sflorian #ifdef HAVE_SYSTEMD
680ae8c6e27Sflorian 	int got_fd_from_systemd = 0;
681ae8c6e27Sflorian #endif
682ae8c6e27Sflorian #ifdef USE_TCP_FASTOPEN
683ae8c6e27Sflorian 	int qlen;
684ae8c6e27Sflorian #endif
685ae8c6e27Sflorian #if !defined(IP_TRANSPARENT) && !defined(IP_BINDANY) && !defined(SO_BINDANY)
686ae8c6e27Sflorian 	(void)transparent;
687ae8c6e27Sflorian #endif
688ae8c6e27Sflorian #if !defined(IP_FREEBIND)
689ae8c6e27Sflorian 	(void)freebind;
690ae8c6e27Sflorian #endif
691ae8c6e27Sflorian 	verbose_print_addr(addr);
692ae8c6e27Sflorian 	*noproto = 0;
693ae8c6e27Sflorian #ifdef HAVE_SYSTEMD
694ae8c6e27Sflorian 	if (!use_systemd ||
695ae8c6e27Sflorian 	    (use_systemd
696ae8c6e27Sflorian 	     && (s = systemd_get_activated(addr->ai_family, addr->ai_socktype, 1,
697ae8c6e27Sflorian 					   addr->ai_addr, addr->ai_addrlen,
698ae8c6e27Sflorian 					   NULL)) == -1)) {
699ae8c6e27Sflorian #else
700ae8c6e27Sflorian 	(void)use_systemd;
701ae8c6e27Sflorian #endif
702ae8c6e27Sflorian 	if((s = socket(addr->ai_family, addr->ai_socktype, 0)) == -1) {
703ae8c6e27Sflorian #ifndef USE_WINSOCK
704ae8c6e27Sflorian 		if(errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
705ae8c6e27Sflorian 			*noproto = 1;
706ae8c6e27Sflorian 			return -1;
707ae8c6e27Sflorian 		}
708ae8c6e27Sflorian #else
709ae8c6e27Sflorian 		if(WSAGetLastError() == WSAEAFNOSUPPORT ||
710ae8c6e27Sflorian 			WSAGetLastError() == WSAEPROTONOSUPPORT) {
711ae8c6e27Sflorian 			*noproto = 1;
712ae8c6e27Sflorian 			return -1;
713ae8c6e27Sflorian 		}
714f4f0f0ceSflorian #endif
715f4f0f0ceSflorian 		log_err("can't create socket: %s", sock_strerror(errno));
716f4f0f0ceSflorian 		return -1;
717f4f0f0ceSflorian 	}
718f4f0f0ceSflorian 	if(nodelay) {
719f4f0f0ceSflorian #if defined(IPPROTO_TCP) && defined(TCP_NODELAY)
720f4f0f0ceSflorian 		if(setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (void*)&on,
721f4f0f0ceSflorian 			(socklen_t)sizeof(on)) < 0) {
722f4f0f0ceSflorian 			#ifndef USE_WINSOCK
723f4f0f0ceSflorian 			log_err(" setsockopt(.. TCP_NODELAY ..) failed: %s",
724f4f0f0ceSflorian 				strerror(errno));
725f4f0f0ceSflorian 			#else
726f4f0f0ceSflorian 			log_err(" setsockopt(.. TCP_NODELAY ..) failed: %s",
727ae8c6e27Sflorian 				wsa_strerror(WSAGetLastError()));
728ae8c6e27Sflorian 			#endif
729f4f0f0ceSflorian 		}
730f4f0f0ceSflorian #else
731f4f0f0ceSflorian 		log_warn(" setsockopt(TCP_NODELAY) unsupported");
732f4f0f0ceSflorian #endif /* defined(IPPROTO_TCP) && defined(TCP_NODELAY) */
733ae8c6e27Sflorian 	}
734ae8c6e27Sflorian 	if (mss > 0) {
735ae8c6e27Sflorian #if defined(IPPROTO_TCP) && defined(TCP_MAXSEG)
736ae8c6e27Sflorian 		if(setsockopt(s, IPPROTO_TCP, TCP_MAXSEG, (void*)&mss,
737ae8c6e27Sflorian 			(socklen_t)sizeof(mss)) < 0) {
738ae8c6e27Sflorian 			log_err(" setsockopt(.. TCP_MAXSEG ..) failed: %s",
739f4f0f0ceSflorian 				sock_strerror(errno));
740ae8c6e27Sflorian 		} else {
741ae8c6e27Sflorian 			verbose(VERB_ALGO,
742ae8c6e27Sflorian 				" tcp socket mss set to %d", mss);
743ae8c6e27Sflorian 		}
744ae8c6e27Sflorian #else
745ae8c6e27Sflorian 		log_warn(" setsockopt(TCP_MAXSEG) unsupported");
746ae8c6e27Sflorian #endif /* defined(IPPROTO_TCP) && defined(TCP_MAXSEG) */
747ae8c6e27Sflorian 	}
748ae8c6e27Sflorian #ifdef HAVE_SYSTEMD
749ae8c6e27Sflorian 	} else {
750ae8c6e27Sflorian 		got_fd_from_systemd = 1;
751ae8c6e27Sflorian     }
752ae8c6e27Sflorian #endif
753ae8c6e27Sflorian #ifdef SO_REUSEADDR
754ae8c6e27Sflorian 	if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*)&on,
755ae8c6e27Sflorian 		(socklen_t)sizeof(on)) < 0) {
756ae8c6e27Sflorian 		log_err("setsockopt(.. SO_REUSEADDR ..) failed: %s",
757f4f0f0ceSflorian 			sock_strerror(errno));
758f4f0f0ceSflorian 		sock_close(s);
759ae8c6e27Sflorian 		return -1;
760ae8c6e27Sflorian 	}
761ae8c6e27Sflorian #endif /* SO_REUSEADDR */
762ae8c6e27Sflorian #ifdef IP_FREEBIND
763ae8c6e27Sflorian 	if (freebind && setsockopt(s, IPPROTO_IP, IP_FREEBIND, (void*)&on,
764ae8c6e27Sflorian 	    (socklen_t)sizeof(on)) < 0) {
765ae8c6e27Sflorian 		log_warn("setsockopt(.. IP_FREEBIND ..) failed: %s",
766ae8c6e27Sflorian 		strerror(errno));
767ae8c6e27Sflorian 	}
768ae8c6e27Sflorian #endif /* IP_FREEBIND */
769ae8c6e27Sflorian #ifdef SO_REUSEPORT
770ae8c6e27Sflorian 	/* try to set SO_REUSEPORT so that incoming
771ae8c6e27Sflorian 	 * connections are distributed evenly among the receiving threads.
772ae8c6e27Sflorian 	 * Each thread must have its own socket bound to the same port,
773ae8c6e27Sflorian 	 * with SO_REUSEPORT set on each socket.
774ae8c6e27Sflorian 	 */
775ae8c6e27Sflorian 	if (reuseport && *reuseport &&
776ae8c6e27Sflorian 		setsockopt(s, SOL_SOCKET, SO_REUSEPORT, (void*)&on,
777ae8c6e27Sflorian 		(socklen_t)sizeof(on)) < 0) {
778ae8c6e27Sflorian #ifdef ENOPROTOOPT
779ae8c6e27Sflorian 		if(errno != ENOPROTOOPT || verbosity >= 3)
780ae8c6e27Sflorian 			log_warn("setsockopt(.. SO_REUSEPORT ..) failed: %s",
781ae8c6e27Sflorian 				strerror(errno));
782ae8c6e27Sflorian #endif
783ae8c6e27Sflorian 		/* this option is not essential, we can continue */
784ae8c6e27Sflorian 		*reuseport = 0;
785ae8c6e27Sflorian 	}
786ae8c6e27Sflorian #else
787ae8c6e27Sflorian 	(void)reuseport;
788ae8c6e27Sflorian #endif /* defined(SO_REUSEPORT) */
789ae8c6e27Sflorian #if defined(IPV6_V6ONLY)
7905c45b740Sflorian 	if(addr->ai_family == AF_INET6 && v6only
7915c45b740Sflorian #  ifdef HAVE_SYSTEMD
7925c45b740Sflorian 		/* Systemd wants to control if the socket is v6 only
7935c45b740Sflorian 		 * or both, with BindIPv6Only=default, ipv6-only or
7945c45b740Sflorian 		 * both in systemd.socket, so it is not set here. */
7955c45b740Sflorian 		&& !got_fd_from_systemd
7965c45b740Sflorian #  endif
7975c45b740Sflorian 		) {
798ae8c6e27Sflorian 		if(setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
799ae8c6e27Sflorian 			(void*)&on, (socklen_t)sizeof(on)) < 0) {
800ae8c6e27Sflorian 			log_err("setsockopt(..., IPV6_V6ONLY, ...) failed: %s",
801f4f0f0ceSflorian 				sock_strerror(errno));
802f4f0f0ceSflorian 			sock_close(s);
803ae8c6e27Sflorian 			return -1;
804ae8c6e27Sflorian 		}
805ae8c6e27Sflorian 	}
806ae8c6e27Sflorian #else
807ae8c6e27Sflorian 	(void)v6only;
808ae8c6e27Sflorian #endif /* IPV6_V6ONLY */
809ae8c6e27Sflorian #ifdef IP_TRANSPARENT
810ae8c6e27Sflorian 	if (transparent &&
811ae8c6e27Sflorian 	    setsockopt(s, IPPROTO_IP, IP_TRANSPARENT, (void*)&on,
812ae8c6e27Sflorian 	    (socklen_t)sizeof(on)) < 0) {
813ae8c6e27Sflorian 		log_warn("setsockopt(.. IP_TRANSPARENT ..) failed: %s",
814ae8c6e27Sflorian 			strerror(errno));
815ae8c6e27Sflorian 	}
816ae8c6e27Sflorian #elif defined(IP_BINDANY)
817ae8c6e27Sflorian 	if (transparent &&
818ae8c6e27Sflorian 	    setsockopt(s, (addr->ai_family==AF_INET6? IPPROTO_IPV6:IPPROTO_IP),
819ae8c6e27Sflorian 	    (addr->ai_family == AF_INET6? IPV6_BINDANY:IP_BINDANY),
820ae8c6e27Sflorian 	    (void*)&on, (socklen_t)sizeof(on)) < 0) {
821ae8c6e27Sflorian 		log_warn("setsockopt(.. IP%s_BINDANY ..) failed: %s",
822ae8c6e27Sflorian 		(addr->ai_family==AF_INET6?"V6":""), strerror(errno));
823ae8c6e27Sflorian 	}
824ae8c6e27Sflorian #elif defined(SO_BINDANY)
825ae8c6e27Sflorian 	if (transparent &&
826ae8c6e27Sflorian 	    setsockopt(s, SOL_SOCKET, SO_BINDANY, (void*)&on, (socklen_t)
827ae8c6e27Sflorian 	    sizeof(on)) < 0) {
828ae8c6e27Sflorian 		log_warn("setsockopt(.. SO_BINDANY ..) failed: %s",
829ae8c6e27Sflorian 		strerror(errno));
830ae8c6e27Sflorian 	}
831ae8c6e27Sflorian #endif /* IP_TRANSPARENT || IP_BINDANY || SO_BINDANY */
832e47fef9eSflorian 	err = set_ip_dscp(s, addr->ai_family, dscp);
833e47fef9eSflorian 	if(err != NULL)
834e47fef9eSflorian 		log_warn("error setting IP DiffServ codepoint %d on TCP socket: %s", dscp, err);
835ae8c6e27Sflorian 	if(
836ae8c6e27Sflorian #ifdef HAVE_SYSTEMD
837ae8c6e27Sflorian 		!got_fd_from_systemd &&
838ae8c6e27Sflorian #endif
839ae8c6e27Sflorian         bind(s, addr->ai_addr, addr->ai_addrlen) != 0) {
840ae8c6e27Sflorian #ifndef USE_WINSOCK
841ae8c6e27Sflorian 		/* detect freebsd jail with no ipv6 permission */
842ae8c6e27Sflorian 		if(addr->ai_family==AF_INET6 && errno==EINVAL)
843ae8c6e27Sflorian 			*noproto = 1;
844ae8c6e27Sflorian 		else {
845ae8c6e27Sflorian 			log_err_addr("can't bind socket", strerror(errno),
846ae8c6e27Sflorian 				(struct sockaddr_storage*)addr->ai_addr,
847ae8c6e27Sflorian 				addr->ai_addrlen);
848ae8c6e27Sflorian 		}
849ae8c6e27Sflorian #else
850ae8c6e27Sflorian 		log_err_addr("can't bind socket",
851ae8c6e27Sflorian 			wsa_strerror(WSAGetLastError()),
852ae8c6e27Sflorian 			(struct sockaddr_storage*)addr->ai_addr,
853ae8c6e27Sflorian 			addr->ai_addrlen);
854ae8c6e27Sflorian #endif
855f4f0f0ceSflorian 		sock_close(s);
856ae8c6e27Sflorian 		return -1;
857ae8c6e27Sflorian 	}
858ae8c6e27Sflorian 	if(!fd_set_nonblock(s)) {
859f4f0f0ceSflorian 		sock_close(s);
860ae8c6e27Sflorian 		return -1;
861ae8c6e27Sflorian 	}
862ae8c6e27Sflorian 	if(listen(s, TCP_BACKLOG) == -1) {
863f4f0f0ceSflorian 		log_err("can't listen: %s", sock_strerror(errno));
864f4f0f0ceSflorian 		sock_close(s);
865ae8c6e27Sflorian 		return -1;
866ae8c6e27Sflorian 	}
867ae8c6e27Sflorian #ifdef USE_TCP_FASTOPEN
868ae8c6e27Sflorian 	/* qlen specifies how many outstanding TFO requests to allow. Limit is a defense
869ae8c6e27Sflorian 	   against IP spoofing attacks as suggested in RFC7413 */
870ae8c6e27Sflorian #ifdef __APPLE__
871ae8c6e27Sflorian 	/* OS X implementation only supports qlen of 1 via this call. Actual
872ae8c6e27Sflorian 	   value is configured by the net.inet.tcp.fastopen_backlog kernel parm. */
873ae8c6e27Sflorian 	qlen = 1;
874ae8c6e27Sflorian #else
875ae8c6e27Sflorian 	/* 5 is recommended on linux */
876ae8c6e27Sflorian 	qlen = 5;
877ae8c6e27Sflorian #endif
878ae8c6e27Sflorian 	if ((setsockopt(s, IPPROTO_TCP, TCP_FASTOPEN, &qlen,
879ae8c6e27Sflorian 		  sizeof(qlen))) == -1 ) {
880ae8c6e27Sflorian #ifdef ENOPROTOOPT
881ae8c6e27Sflorian 		/* squelch ENOPROTOOPT: freebsd server mode with kernel support
882ae8c6e27Sflorian 		   disabled, except when verbosity enabled for debugging */
8839b465e50Sflorian 		if(errno != ENOPROTOOPT || verbosity >= 3) {
884ae8c6e27Sflorian #endif
885e97c6e54Ssthen 		  if(errno == EPERM) {
886e97c6e54Ssthen 		  	log_warn("Setting TCP Fast Open as server failed: %s ; this could likely be because sysctl net.inet.tcp.fastopen.enabled, net.inet.tcp.fastopen.server_enable, or net.ipv4.tcp_fastopen is disabled", strerror(errno));
887e97c6e54Ssthen 		  } else {
888ae8c6e27Sflorian 		  	log_err("Setting TCP Fast Open as server failed: %s", strerror(errno));
889ae8c6e27Sflorian 		  }
8909b465e50Sflorian #ifdef ENOPROTOOPT
8919b465e50Sflorian 		}
8929b465e50Sflorian #endif
893e97c6e54Ssthen 	}
894ae8c6e27Sflorian #endif
895ae8c6e27Sflorian 	return s;
896ae8c6e27Sflorian }
897ae8c6e27Sflorian 
898e47fef9eSflorian char*
set_ip_dscp(int socket,int addrfamily,int dscp)899e47fef9eSflorian set_ip_dscp(int socket, int addrfamily, int dscp)
900e47fef9eSflorian {
901e47fef9eSflorian 	int ds;
902e47fef9eSflorian 
903e47fef9eSflorian 	if(dscp == 0)
904e47fef9eSflorian 		return NULL;
905e47fef9eSflorian 	ds = dscp << 2;
906e47fef9eSflorian 	switch(addrfamily) {
907e47fef9eSflorian 	case AF_INET6:
908a1a7ba80Sflorian 	#ifdef IPV6_TCLASS
909a1a7ba80Sflorian 		if(setsockopt(socket, IPPROTO_IPV6, IPV6_TCLASS, (void*)&ds,
910a1a7ba80Sflorian 			sizeof(ds)) < 0)
911e47fef9eSflorian 			return sock_strerror(errno);
912e47fef9eSflorian 		break;
913a1a7ba80Sflorian 	#else
914a1a7ba80Sflorian 		return "IPV6_TCLASS not defined on this system";
915a1a7ba80Sflorian 	#endif
916e47fef9eSflorian 	default:
917e47fef9eSflorian 		if(setsockopt(socket, IPPROTO_IP, IP_TOS, (void*)&ds, sizeof(ds)) < 0)
918e47fef9eSflorian 			return sock_strerror(errno);
919e47fef9eSflorian 		break;
920e47fef9eSflorian 	}
921e47fef9eSflorian 	return NULL;
922e47fef9eSflorian }
923e47fef9eSflorian 
924ae8c6e27Sflorian int
create_local_accept_sock(const char * path,int * noproto,int use_systemd)925ae8c6e27Sflorian create_local_accept_sock(const char *path, int* noproto, int use_systemd)
926ae8c6e27Sflorian {
927ae8c6e27Sflorian #ifdef HAVE_SYSTEMD
928ae8c6e27Sflorian 	int ret;
929ae8c6e27Sflorian 
930ae8c6e27Sflorian 	if (use_systemd && (ret = systemd_get_activated(AF_LOCAL, SOCK_STREAM, 1, NULL, 0, path)) != -1)
931ae8c6e27Sflorian 		return ret;
932ae8c6e27Sflorian 	else {
933ae8c6e27Sflorian #endif
934ae8c6e27Sflorian #ifdef HAVE_SYS_UN_H
935ae8c6e27Sflorian 	int s;
936ae8c6e27Sflorian 	struct sockaddr_un usock;
937ae8c6e27Sflorian #ifndef HAVE_SYSTEMD
938ae8c6e27Sflorian 	(void)use_systemd;
939ae8c6e27Sflorian #endif
940ae8c6e27Sflorian 
941ae8c6e27Sflorian 	verbose(VERB_ALGO, "creating unix socket %s", path);
942ae8c6e27Sflorian #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
943ae8c6e27Sflorian 	/* this member exists on BSDs, not Linux */
944ae8c6e27Sflorian 	usock.sun_len = (unsigned)sizeof(usock);
945ae8c6e27Sflorian #endif
946ae8c6e27Sflorian 	usock.sun_family = AF_LOCAL;
947ae8c6e27Sflorian 	/* length is 92-108, 104 on FreeBSD */
948ae8c6e27Sflorian 	(void)strlcpy(usock.sun_path, path, sizeof(usock.sun_path));
949ae8c6e27Sflorian 
950ae8c6e27Sflorian 	if ((s = socket(AF_LOCAL, SOCK_STREAM, 0)) == -1) {
951ae8c6e27Sflorian 		log_err("Cannot create local socket %s (%s)",
952ae8c6e27Sflorian 			path, strerror(errno));
953ae8c6e27Sflorian 		return -1;
954ae8c6e27Sflorian 	}
955ae8c6e27Sflorian 
956ae8c6e27Sflorian 	if (unlink(path) && errno != ENOENT) {
957ae8c6e27Sflorian 		/* The socket already exists and cannot be removed */
958ae8c6e27Sflorian 		log_err("Cannot remove old local socket %s (%s)",
959ae8c6e27Sflorian 			path, strerror(errno));
960ae8c6e27Sflorian 		goto err;
961ae8c6e27Sflorian 	}
962ae8c6e27Sflorian 
963ae8c6e27Sflorian 	if (bind(s, (struct sockaddr *)&usock,
964ae8c6e27Sflorian 		(socklen_t)sizeof(struct sockaddr_un)) == -1) {
965ae8c6e27Sflorian 		log_err("Cannot bind local socket %s (%s)",
966ae8c6e27Sflorian 			path, strerror(errno));
967ae8c6e27Sflorian 		goto err;
968ae8c6e27Sflorian 	}
969ae8c6e27Sflorian 
970ae8c6e27Sflorian 	if (!fd_set_nonblock(s)) {
971ae8c6e27Sflorian 		log_err("Cannot set non-blocking mode");
972ae8c6e27Sflorian 		goto err;
973ae8c6e27Sflorian 	}
974ae8c6e27Sflorian 
975ae8c6e27Sflorian 	if (listen(s, TCP_BACKLOG) == -1) {
976ae8c6e27Sflorian 		log_err("can't listen: %s", strerror(errno));
977ae8c6e27Sflorian 		goto err;
978ae8c6e27Sflorian 	}
979ae8c6e27Sflorian 
980ae8c6e27Sflorian 	(void)noproto; /*unused*/
981ae8c6e27Sflorian 	return s;
982ae8c6e27Sflorian 
983ae8c6e27Sflorian err:
984f4f0f0ceSflorian 	sock_close(s);
985ae8c6e27Sflorian 	return -1;
986ae8c6e27Sflorian 
987ae8c6e27Sflorian #ifdef HAVE_SYSTEMD
988ae8c6e27Sflorian 	}
989ae8c6e27Sflorian #endif
990ae8c6e27Sflorian #else
991ae8c6e27Sflorian 	(void)use_systemd;
992ae8c6e27Sflorian 	(void)path;
993ae8c6e27Sflorian 	log_err("Local sockets are not supported");
994ae8c6e27Sflorian 	*noproto = 1;
995ae8c6e27Sflorian 	return -1;
996ae8c6e27Sflorian #endif
997ae8c6e27Sflorian }
998ae8c6e27Sflorian 
999ae8c6e27Sflorian 
1000ae8c6e27Sflorian /**
1001ae8c6e27Sflorian  * Create socket from getaddrinfo results
1002ae8c6e27Sflorian  */
1003ae8c6e27Sflorian static int
make_sock(int stype,const char * ifname,const char * port,struct addrinfo * hints,int v6only,int * noip6,size_t rcv,size_t snd,int * reuseport,int transparent,int tcp_mss,int nodelay,int freebind,int use_systemd,int dscp,struct unbound_socket * ub_sock)1004ae8c6e27Sflorian make_sock(int stype, const char* ifname, const char* port,
1005ae8c6e27Sflorian 	struct addrinfo *hints, int v6only, int* noip6, size_t rcv, size_t snd,
1006f4f0f0ceSflorian 	int* reuseport, int transparent, int tcp_mss, int nodelay, int freebind,
1007411c5950Sflorian 	int use_systemd, int dscp, struct unbound_socket* ub_sock)
1008ae8c6e27Sflorian {
1009ae8c6e27Sflorian 	struct addrinfo *res = NULL;
1010ae8c6e27Sflorian 	int r, s, inuse, noproto;
1011ae8c6e27Sflorian 	hints->ai_socktype = stype;
1012ae8c6e27Sflorian 	*noip6 = 0;
1013ae8c6e27Sflorian 	if((r=getaddrinfo(ifname, port, hints, &res)) != 0 || !res) {
1014ae8c6e27Sflorian #ifdef USE_WINSOCK
1015ae8c6e27Sflorian 		if(r == EAI_NONAME && hints->ai_family == AF_INET6){
1016ae8c6e27Sflorian 			*noip6 = 1; /* 'Host not found' for IP6 on winXP */
1017ae8c6e27Sflorian 			return -1;
1018ae8c6e27Sflorian 		}
1019ae8c6e27Sflorian #endif
1020ae8c6e27Sflorian 		log_err("node %s:%s getaddrinfo: %s %s",
1021ae8c6e27Sflorian 			ifname?ifname:"default", port, gai_strerror(r),
1022ae8c6e27Sflorian #ifdef EAI_SYSTEM
1023d500c338Sflorian 			(r==EAI_SYSTEM?(char*)strerror(errno):"")
1024ae8c6e27Sflorian #else
1025ae8c6e27Sflorian 			""
1026ae8c6e27Sflorian #endif
1027ae8c6e27Sflorian 		);
1028ae8c6e27Sflorian 		return -1;
1029ae8c6e27Sflorian 	}
1030ae8c6e27Sflorian 	if(stype == SOCK_DGRAM) {
1031ae8c6e27Sflorian 		verbose_print_addr(res);
1032ae8c6e27Sflorian 		s = create_udp_sock(res->ai_family, res->ai_socktype,
1033ae8c6e27Sflorian 			(struct sockaddr*)res->ai_addr, res->ai_addrlen,
1034ae8c6e27Sflorian 			v6only, &inuse, &noproto, (int)rcv, (int)snd, 1,
1035e47fef9eSflorian 			reuseport, transparent, freebind, use_systemd, dscp);
1036ae8c6e27Sflorian 		if(s == -1 && inuse) {
1037ae8c6e27Sflorian 			log_err("bind: address already in use");
1038ae8c6e27Sflorian 		} else if(s == -1 && noproto && hints->ai_family == AF_INET6){
1039ae8c6e27Sflorian 			*noip6 = 1;
1040ae8c6e27Sflorian 		}
1041ae8c6e27Sflorian 	} else	{
1042ae8c6e27Sflorian 		s = create_tcp_accept_sock(res, v6only, &noproto, reuseport,
1043f4f0f0ceSflorian 			transparent, tcp_mss, nodelay, freebind, use_systemd,
1044f4f0f0ceSflorian 			dscp);
1045ae8c6e27Sflorian 		if(s == -1 && noproto && hints->ai_family == AF_INET6){
1046ae8c6e27Sflorian 			*noip6 = 1;
1047ae8c6e27Sflorian 		}
1048ae8c6e27Sflorian 	}
1049411c5950Sflorian 
1050411c5950Sflorian 	ub_sock->addr = res;
1051411c5950Sflorian 	ub_sock->s = s;
1052411c5950Sflorian 	ub_sock->fam = hints->ai_family;
10535c45b740Sflorian 	ub_sock->acl = NULL;
1054411c5950Sflorian 
1055ae8c6e27Sflorian 	return s;
1056ae8c6e27Sflorian }
1057ae8c6e27Sflorian 
1058ae8c6e27Sflorian /** make socket and first see if ifname contains port override info */
1059ae8c6e27Sflorian static int
make_sock_port(int stype,const char * ifname,const char * port,struct addrinfo * hints,int v6only,int * noip6,size_t rcv,size_t snd,int * reuseport,int transparent,int tcp_mss,int nodelay,int freebind,int use_systemd,int dscp,struct unbound_socket * ub_sock)1060ae8c6e27Sflorian make_sock_port(int stype, const char* ifname, const char* port,
1061ae8c6e27Sflorian 	struct addrinfo *hints, int v6only, int* noip6, size_t rcv, size_t snd,
1062f4f0f0ceSflorian 	int* reuseport, int transparent, int tcp_mss, int nodelay, int freebind,
1063411c5950Sflorian 	int use_systemd, int dscp, struct unbound_socket* ub_sock)
1064ae8c6e27Sflorian {
1065ae8c6e27Sflorian 	char* s = strchr(ifname, '@');
1066ae8c6e27Sflorian 	if(s) {
1067ae8c6e27Sflorian 		/* override port with ifspec@port */
1068ae8c6e27Sflorian 		char p[16];
1069ae8c6e27Sflorian 		char newif[128];
1070ae8c6e27Sflorian 		if((size_t)(s-ifname) >= sizeof(newif)) {
1071ae8c6e27Sflorian 			log_err("ifname too long: %s", ifname);
1072ae8c6e27Sflorian 			*noip6 = 0;
1073ae8c6e27Sflorian 			return -1;
1074ae8c6e27Sflorian 		}
1075ae8c6e27Sflorian 		if(strlen(s+1) >= sizeof(p)) {
1076ae8c6e27Sflorian 			log_err("portnumber too long: %s", ifname);
1077ae8c6e27Sflorian 			*noip6 = 0;
1078ae8c6e27Sflorian 			return -1;
1079ae8c6e27Sflorian 		}
1080ae8c6e27Sflorian 		(void)strlcpy(newif, ifname, sizeof(newif));
1081ae8c6e27Sflorian 		newif[s-ifname] = 0;
1082ae8c6e27Sflorian 		(void)strlcpy(p, s+1, sizeof(p));
1083ae8c6e27Sflorian 		p[strlen(s+1)]=0;
1084f4f0f0ceSflorian 		return make_sock(stype, newif, p, hints, v6only, noip6, rcv,
1085f4f0f0ceSflorian 			snd, reuseport, transparent, tcp_mss, nodelay, freebind,
1086411c5950Sflorian 			use_systemd, dscp, ub_sock);
1087ae8c6e27Sflorian 	}
1088ae8c6e27Sflorian 	return make_sock(stype, ifname, port, hints, v6only, noip6, rcv, snd,
1089f4f0f0ceSflorian 		reuseport, transparent, tcp_mss, nodelay, freebind, use_systemd,
1090411c5950Sflorian 		dscp, ub_sock);
1091ae8c6e27Sflorian }
1092ae8c6e27Sflorian 
1093ae8c6e27Sflorian /**
1094ae8c6e27Sflorian  * Add port to open ports list.
1095ae8c6e27Sflorian  * @param list: list head. changed.
1096ae8c6e27Sflorian  * @param s: fd.
1097ae8c6e27Sflorian  * @param ftype: if fd is UDP.
10985c45b740Sflorian  * @param pp2_enabled: if PROXYv2 is enabled for this port.
1099411c5950Sflorian  * @param ub_sock: socket with address.
1100ae8c6e27Sflorian  * @return false on failure. list in unchanged then.
1101ae8c6e27Sflorian  */
1102ae8c6e27Sflorian static int
port_insert(struct listen_port ** list,int s,enum listen_type ftype,int pp2_enabled,struct unbound_socket * ub_sock)11035c45b740Sflorian port_insert(struct listen_port** list, int s, enum listen_type ftype,
11045c45b740Sflorian 	int pp2_enabled, struct unbound_socket* ub_sock)
1105ae8c6e27Sflorian {
1106ae8c6e27Sflorian 	struct listen_port* item = (struct listen_port*)malloc(
1107ae8c6e27Sflorian 		sizeof(struct listen_port));
1108ae8c6e27Sflorian 	if(!item)
1109ae8c6e27Sflorian 		return 0;
1110ae8c6e27Sflorian 	item->next = *list;
1111ae8c6e27Sflorian 	item->fd = s;
1112ae8c6e27Sflorian 	item->ftype = ftype;
11135c45b740Sflorian 	item->pp2_enabled = pp2_enabled;
1114411c5950Sflorian 	item->socket = ub_sock;
1115ae8c6e27Sflorian 	*list = item;
1116ae8c6e27Sflorian 	return 1;
1117ae8c6e27Sflorian }
1118ae8c6e27Sflorian 
1119d500c338Sflorian /** set fd to receive software timestamps */
1120d500c338Sflorian static int
set_recvtimestamp(int s)1121d500c338Sflorian set_recvtimestamp(int s)
1122d500c338Sflorian {
1123d500c338Sflorian #ifdef HAVE_LINUX_NET_TSTAMP_H
1124d500c338Sflorian 	int opt = SOF_TIMESTAMPING_RX_SOFTWARE | SOF_TIMESTAMPING_SOFTWARE;
1125d500c338Sflorian 	if (setsockopt(s, SOL_SOCKET, SO_TIMESTAMPNS, (void*)&opt, (socklen_t)sizeof(opt)) < 0) {
1126d500c338Sflorian 		log_err("setsockopt(..., SO_TIMESTAMPNS, ...) failed: %s",
1127d500c338Sflorian 			strerror(errno));
1128d500c338Sflorian 		return 0;
1129d500c338Sflorian 	}
1130d500c338Sflorian 	return 1;
1131d500c338Sflorian #else
1132d500c338Sflorian 	log_err("packets timestamping is not supported on this platform");
1133d500c338Sflorian 	(void)s;
1134d500c338Sflorian 	return 0;
1135d500c338Sflorian #endif
1136d500c338Sflorian }
1137d500c338Sflorian 
1138ae8c6e27Sflorian /** set fd to receive source address packet info */
1139ae8c6e27Sflorian static int
set_recvpktinfo(int s,int family)1140ae8c6e27Sflorian set_recvpktinfo(int s, int family)
1141ae8c6e27Sflorian {
1142ae8c6e27Sflorian #if defined(IPV6_RECVPKTINFO) || defined(IPV6_PKTINFO) || (defined(IP_RECVDSTADDR) && defined(IP_SENDSRCADDR)) || defined(IP_PKTINFO)
1143ae8c6e27Sflorian 	int on = 1;
1144ae8c6e27Sflorian #else
1145ae8c6e27Sflorian 	(void)s;
1146ae8c6e27Sflorian #endif
1147ae8c6e27Sflorian 	if(family == AF_INET6) {
1148ae8c6e27Sflorian #           ifdef IPV6_RECVPKTINFO
1149ae8c6e27Sflorian 		if(setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO,
1150ae8c6e27Sflorian 			(void*)&on, (socklen_t)sizeof(on)) < 0) {
1151ae8c6e27Sflorian 			log_err("setsockopt(..., IPV6_RECVPKTINFO, ...) failed: %s",
1152ae8c6e27Sflorian 				strerror(errno));
1153ae8c6e27Sflorian 			return 0;
1154ae8c6e27Sflorian 		}
1155ae8c6e27Sflorian #           elif defined(IPV6_PKTINFO)
1156ae8c6e27Sflorian 		if(setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO,
1157ae8c6e27Sflorian 			(void*)&on, (socklen_t)sizeof(on)) < 0) {
1158ae8c6e27Sflorian 			log_err("setsockopt(..., IPV6_PKTINFO, ...) failed: %s",
1159ae8c6e27Sflorian 				strerror(errno));
1160ae8c6e27Sflorian 			return 0;
1161ae8c6e27Sflorian 		}
1162ae8c6e27Sflorian #           else
1163411c5950Sflorian 		log_err("no IPV6_RECVPKTINFO and IPV6_PKTINFO options, please "
1164ae8c6e27Sflorian 			"disable interface-automatic or do-ip6 in config");
1165ae8c6e27Sflorian 		return 0;
1166ae8c6e27Sflorian #           endif /* defined IPV6_RECVPKTINFO */
1167ae8c6e27Sflorian 
1168ae8c6e27Sflorian 	} else if(family == AF_INET) {
1169ae8c6e27Sflorian #           ifdef IP_PKTINFO
1170ae8c6e27Sflorian 		if(setsockopt(s, IPPROTO_IP, IP_PKTINFO,
1171ae8c6e27Sflorian 			(void*)&on, (socklen_t)sizeof(on)) < 0) {
1172ae8c6e27Sflorian 			log_err("setsockopt(..., IP_PKTINFO, ...) failed: %s",
1173ae8c6e27Sflorian 				strerror(errno));
1174ae8c6e27Sflorian 			return 0;
1175ae8c6e27Sflorian 		}
1176ae8c6e27Sflorian #           elif defined(IP_RECVDSTADDR) && defined(IP_SENDSRCADDR)
1177ae8c6e27Sflorian 		if(setsockopt(s, IPPROTO_IP, IP_RECVDSTADDR,
1178ae8c6e27Sflorian 			(void*)&on, (socklen_t)sizeof(on)) < 0) {
1179ae8c6e27Sflorian 			log_err("setsockopt(..., IP_RECVDSTADDR, ...) failed: %s",
1180ae8c6e27Sflorian 				strerror(errno));
1181ae8c6e27Sflorian 			return 0;
1182ae8c6e27Sflorian 		}
1183ae8c6e27Sflorian #           else
1184ae8c6e27Sflorian 		log_err("no IP_SENDSRCADDR or IP_PKTINFO option, please disable "
1185ae8c6e27Sflorian 			"interface-automatic or do-ip4 in config");
1186ae8c6e27Sflorian 		return 0;
1187ae8c6e27Sflorian #           endif /* IP_PKTINFO */
1188ae8c6e27Sflorian 
1189ae8c6e27Sflorian 	}
1190ae8c6e27Sflorian 	return 1;
1191ae8c6e27Sflorian }
1192ae8c6e27Sflorian 
1193ae8c6e27Sflorian /** see if interface is ssl, its port number == the ssl port number */
1194ae8c6e27Sflorian static int
if_is_ssl(const char * ifname,const char * port,int ssl_port,struct config_strlist * tls_additional_port)1195ae8c6e27Sflorian if_is_ssl(const char* ifname, const char* port, int ssl_port,
1196ae8c6e27Sflorian 	struct config_strlist* tls_additional_port)
1197ae8c6e27Sflorian {
1198ae8c6e27Sflorian 	struct config_strlist* s;
1199ae8c6e27Sflorian 	char* p = strchr(ifname, '@');
1200ae8c6e27Sflorian 	if(!p && atoi(port) == ssl_port)
1201ae8c6e27Sflorian 		return 1;
1202ae8c6e27Sflorian 	if(p && atoi(p+1) == ssl_port)
1203ae8c6e27Sflorian 		return 1;
1204ae8c6e27Sflorian 	for(s = tls_additional_port; s; s = s->next) {
1205ae8c6e27Sflorian 		if(p && atoi(p+1) == atoi(s->str))
1206ae8c6e27Sflorian 			return 1;
1207ae8c6e27Sflorian 		if(!p && atoi(port) == atoi(s->str))
1208ae8c6e27Sflorian 			return 1;
1209ae8c6e27Sflorian 	}
1210ae8c6e27Sflorian 	return 0;
1211ae8c6e27Sflorian }
1212ae8c6e27Sflorian 
1213ae8c6e27Sflorian /**
1214ae8c6e27Sflorian  * Helper for ports_open. Creates one interface (or NULL for default).
1215ae8c6e27Sflorian  * @param ifname: The interface ip address.
1216ae8c6e27Sflorian  * @param do_auto: use automatic interface detection.
1217ae8c6e27Sflorian  * 	If enabled, then ifname must be the wildcard name.
1218ae8c6e27Sflorian  * @param do_udp: if udp should be used.
12197a05b9dfSflorian  * @param do_tcp: if tcp should be used.
1220ae8c6e27Sflorian  * @param hints: for getaddrinfo. family and flags have to be set by caller.
1221ae8c6e27Sflorian  * @param port: Port number to use (as string).
1222ae8c6e27Sflorian  * @param list: list of open ports, appended to, changed to point to list head.
1223ae8c6e27Sflorian  * @param rcv: receive buffer size for UDP
1224ae8c6e27Sflorian  * @param snd: send buffer size for UDP
1225ae8c6e27Sflorian  * @param ssl_port: ssl service port number
1226ae8c6e27Sflorian  * @param tls_additional_port: list of additional ssl service port numbers.
1227f4f0f0ceSflorian  * @param https_port: DoH service port number
12285c45b740Sflorian  * @param proxy_protocol_port: list of PROXYv2 port numbers.
1229ae8c6e27Sflorian  * @param reuseport: try to set SO_REUSEPORT if nonNULL and true.
1230ae8c6e27Sflorian  * 	set to false on exit if reuseport failed due to no kernel support.
1231ae8c6e27Sflorian  * @param transparent: set IP_TRANSPARENT socket option.
1232ae8c6e27Sflorian  * @param tcp_mss: maximum segment size of tcp socket. default if zero.
1233ae8c6e27Sflorian  * @param freebind: set IP_FREEBIND socket option.
1234f4f0f0ceSflorian  * @param http2_nodelay: set TCP_NODELAY on HTTP/2 connection
1235ae8c6e27Sflorian  * @param use_systemd: if true, fetch sockets from systemd.
1236ae8c6e27Sflorian  * @param dnscrypt_port: dnscrypt service port number
1237e47fef9eSflorian  * @param dscp: DSCP to use.
1238d500c338Sflorian  * @param sock_queue_timeout: the sock_queue_timeout from config. Seconds to
1239d500c338Sflorian  * 	wait to discard if UDP packets have waited for long in the socket
1240d500c338Sflorian  * 	buffer.
1241ae8c6e27Sflorian  * @return: returns false on error.
1242ae8c6e27Sflorian  */
1243ae8c6e27Sflorian static int
ports_create_if(const char * ifname,int do_auto,int do_udp,int do_tcp,struct addrinfo * hints,const char * port,struct listen_port ** list,size_t rcv,size_t snd,int ssl_port,struct config_strlist * tls_additional_port,int https_port,struct config_strlist * proxy_protocol_port,int * reuseport,int transparent,int tcp_mss,int freebind,int http2_nodelay,int use_systemd,int dnscrypt_port,int dscp,int sock_queue_timeout)1244ae8c6e27Sflorian ports_create_if(const char* ifname, int do_auto, int do_udp, int do_tcp,
1245ae8c6e27Sflorian 	struct addrinfo *hints, const char* port, struct listen_port** list,
1246ae8c6e27Sflorian 	size_t rcv, size_t snd, int ssl_port,
1247f4f0f0ceSflorian 	struct config_strlist* tls_additional_port, int https_port,
12485c45b740Sflorian 	struct config_strlist* proxy_protocol_port,
1249f4f0f0ceSflorian 	int* reuseport, int transparent, int tcp_mss, int freebind,
1250d500c338Sflorian 	int http2_nodelay, int use_systemd, int dnscrypt_port, int dscp,
1251d500c338Sflorian 	int sock_queue_timeout)
1252ae8c6e27Sflorian {
1253ae8c6e27Sflorian 	int s, noip6=0;
1254f4f0f0ceSflorian 	int is_https = if_is_https(ifname, port, https_port);
12555c45b740Sflorian 	int is_dnscrypt = if_is_dnscrypt(ifname, port, dnscrypt_port);
12565c45b740Sflorian 	int is_pp2 = if_is_pp2(ifname, port, proxy_protocol_port);
1257f4f0f0ceSflorian 	int nodelay = is_https && http2_nodelay;
1258411c5950Sflorian 	struct unbound_socket* ub_sock;
1259ae8c6e27Sflorian 
1260ae8c6e27Sflorian 	if(!do_udp && !do_tcp)
1261ae8c6e27Sflorian 		return 0;
1262411c5950Sflorian 
12635c45b740Sflorian 	if(is_pp2) {
12645c45b740Sflorian 		if(is_dnscrypt) {
12655c45b740Sflorian 			fatal_exit("PROXYv2 and DNSCrypt combination not "
12665c45b740Sflorian 				"supported!");
12675c45b740Sflorian 		} else if(is_https) {
12685c45b740Sflorian 			fatal_exit("PROXYv2 and DoH combination not "
12695c45b740Sflorian 				"supported!");
12705c45b740Sflorian 		}
12715c45b740Sflorian 	}
12725c45b740Sflorian 
1273ae8c6e27Sflorian 	if(do_auto) {
1274411c5950Sflorian 		ub_sock = calloc(1, sizeof(struct unbound_socket));
1275411c5950Sflorian 		if(!ub_sock)
1276411c5950Sflorian 			return 0;
1277ae8c6e27Sflorian 		if((s = make_sock_port(SOCK_DGRAM, ifname, port, hints, 1,
1278ae8c6e27Sflorian 			&noip6, rcv, snd, reuseport, transparent,
1279411c5950Sflorian 			tcp_mss, nodelay, freebind, use_systemd, dscp, ub_sock)) == -1) {
1280d500c338Sflorian 			if(ub_sock->addr)
1281411c5950Sflorian 				freeaddrinfo(ub_sock->addr);
1282411c5950Sflorian 			free(ub_sock);
1283ae8c6e27Sflorian 			if(noip6) {
1284ae8c6e27Sflorian 				log_warn("IPv6 protocol not available");
1285ae8c6e27Sflorian 				return 1;
1286ae8c6e27Sflorian 			}
1287ae8c6e27Sflorian 			return 0;
1288ae8c6e27Sflorian 		}
1289ae8c6e27Sflorian 		/* getting source addr packet info is highly non-portable */
1290ae8c6e27Sflorian 		if(!set_recvpktinfo(s, hints->ai_family)) {
1291f4f0f0ceSflorian 			sock_close(s);
1292d500c338Sflorian 			if(ub_sock->addr)
1293411c5950Sflorian 				freeaddrinfo(ub_sock->addr);
1294411c5950Sflorian 			free(ub_sock);
1295ae8c6e27Sflorian 			return 0;
1296ae8c6e27Sflorian 		}
1297d500c338Sflorian 		if (sock_queue_timeout && !set_recvtimestamp(s)) {
1298d500c338Sflorian 			log_warn("socket timestamping is not available");
1299d500c338Sflorian 		}
13005c45b740Sflorian 		if(!port_insert(list, s, is_dnscrypt
13015c45b740Sflorian 			?listen_type_udpancil_dnscrypt:listen_type_udpancil,
13025c45b740Sflorian 			is_pp2, ub_sock)) {
1303f4f0f0ceSflorian 			sock_close(s);
1304d500c338Sflorian 			if(ub_sock->addr)
1305411c5950Sflorian 				freeaddrinfo(ub_sock->addr);
1306411c5950Sflorian 			free(ub_sock);
1307ae8c6e27Sflorian 			return 0;
1308ae8c6e27Sflorian 		}
1309ae8c6e27Sflorian 	} else if(do_udp) {
1310411c5950Sflorian 		ub_sock = calloc(1, sizeof(struct unbound_socket));
1311411c5950Sflorian 		if(!ub_sock)
1312411c5950Sflorian 			return 0;
1313ae8c6e27Sflorian 		/* regular udp socket */
1314ae8c6e27Sflorian 		if((s = make_sock_port(SOCK_DGRAM, ifname, port, hints, 1,
1315ae8c6e27Sflorian 			&noip6, rcv, snd, reuseport, transparent,
1316411c5950Sflorian 			tcp_mss, nodelay, freebind, use_systemd, dscp, ub_sock)) == -1) {
1317d500c338Sflorian 			if(ub_sock->addr)
1318411c5950Sflorian 				freeaddrinfo(ub_sock->addr);
1319411c5950Sflorian 			free(ub_sock);
1320ae8c6e27Sflorian 			if(noip6) {
1321ae8c6e27Sflorian 				log_warn("IPv6 protocol not available");
1322ae8c6e27Sflorian 				return 1;
1323ae8c6e27Sflorian 			}
1324ae8c6e27Sflorian 			return 0;
1325ae8c6e27Sflorian 		}
1326d500c338Sflorian 		if (sock_queue_timeout && !set_recvtimestamp(s)) {
1327d500c338Sflorian 			log_warn("socket timestamping is not available");
1328d500c338Sflorian 		}
13295c45b740Sflorian 		if(!port_insert(list, s, is_dnscrypt
1330*54cc57acSflorian 			?listen_type_udp_dnscrypt :
1331*54cc57acSflorian 			(sock_queue_timeout ?
1332*54cc57acSflorian 				listen_type_udpancil:listen_type_udp),
13335c45b740Sflorian 			is_pp2, ub_sock)) {
1334f4f0f0ceSflorian 			sock_close(s);
1335d500c338Sflorian 			if(ub_sock->addr)
1336411c5950Sflorian 				freeaddrinfo(ub_sock->addr);
1337411c5950Sflorian 			free(ub_sock);
1338ae8c6e27Sflorian 			return 0;
1339ae8c6e27Sflorian 		}
1340ae8c6e27Sflorian 	}
1341ae8c6e27Sflorian 	if(do_tcp) {
1342ae8c6e27Sflorian 		int is_ssl = if_is_ssl(ifname, port, ssl_port,
1343ae8c6e27Sflorian 			tls_additional_port);
1344f4f0f0ceSflorian 		enum listen_type port_type;
1345411c5950Sflorian 		ub_sock = calloc(1, sizeof(struct unbound_socket));
1346411c5950Sflorian 		if(!ub_sock)
1347411c5950Sflorian 			return 0;
1348f4f0f0ceSflorian 		if(is_ssl)
1349f4f0f0ceSflorian 			port_type = listen_type_ssl;
1350f4f0f0ceSflorian 		else if(is_https)
1351f4f0f0ceSflorian 			port_type = listen_type_http;
1352f4f0f0ceSflorian 		else if(is_dnscrypt)
1353f4f0f0ceSflorian 			port_type = listen_type_tcp_dnscrypt;
1354f4f0f0ceSflorian 		else
1355f4f0f0ceSflorian 			port_type = listen_type_tcp;
1356ae8c6e27Sflorian 		if((s = make_sock_port(SOCK_STREAM, ifname, port, hints, 1,
1357f4f0f0ceSflorian 			&noip6, 0, 0, reuseport, transparent, tcp_mss, nodelay,
1358411c5950Sflorian 			freebind, use_systemd, dscp, ub_sock)) == -1) {
1359d500c338Sflorian 			if(ub_sock->addr)
1360411c5950Sflorian 				freeaddrinfo(ub_sock->addr);
1361411c5950Sflorian 			free(ub_sock);
1362ae8c6e27Sflorian 			if(noip6) {
1363ae8c6e27Sflorian 				/*log_warn("IPv6 protocol not available");*/
1364ae8c6e27Sflorian 				return 1;
1365ae8c6e27Sflorian 			}
1366ae8c6e27Sflorian 			return 0;
1367ae8c6e27Sflorian 		}
1368ae8c6e27Sflorian 		if(is_ssl)
1369ae8c6e27Sflorian 			verbose(VERB_ALGO, "setup TCP for SSL service");
13705c45b740Sflorian 		if(!port_insert(list, s, port_type, is_pp2, ub_sock)) {
1371f4f0f0ceSflorian 			sock_close(s);
1372d500c338Sflorian 			if(ub_sock->addr)
1373411c5950Sflorian 				freeaddrinfo(ub_sock->addr);
1374411c5950Sflorian 			free(ub_sock);
1375ae8c6e27Sflorian 			return 0;
1376ae8c6e27Sflorian 		}
1377ae8c6e27Sflorian 	}
1378ae8c6e27Sflorian 	return 1;
1379ae8c6e27Sflorian }
1380ae8c6e27Sflorian 
1381ae8c6e27Sflorian /**
1382ae8c6e27Sflorian  * Add items to commpoint list in front.
1383ae8c6e27Sflorian  * @param c: commpoint to add.
1384ae8c6e27Sflorian  * @param front: listen struct.
1385ae8c6e27Sflorian  * @return: false on failure.
1386ae8c6e27Sflorian  */
1387ae8c6e27Sflorian static int
listen_cp_insert(struct comm_point * c,struct listen_dnsport * front)1388ae8c6e27Sflorian listen_cp_insert(struct comm_point* c, struct listen_dnsport* front)
1389ae8c6e27Sflorian {
1390ae8c6e27Sflorian 	struct listen_list* item = (struct listen_list*)malloc(
1391ae8c6e27Sflorian 		sizeof(struct listen_list));
1392ae8c6e27Sflorian 	if(!item)
1393ae8c6e27Sflorian 		return 0;
1394ae8c6e27Sflorian 	item->com = c;
1395ae8c6e27Sflorian 	item->next = front->cps;
1396ae8c6e27Sflorian 	front->cps = item;
1397ae8c6e27Sflorian 	return 1;
1398ae8c6e27Sflorian }
1399ae8c6e27Sflorian 
listen_setup_locks(void)1400a1a7ba80Sflorian void listen_setup_locks(void)
1401a1a7ba80Sflorian {
1402a1a7ba80Sflorian 	if(!stream_wait_lock_inited) {
1403a1a7ba80Sflorian 		lock_basic_init(&stream_wait_count_lock);
1404a1a7ba80Sflorian 		stream_wait_lock_inited = 1;
1405a1a7ba80Sflorian 	}
1406a1a7ba80Sflorian 	if(!http2_query_buffer_lock_inited) {
1407a1a7ba80Sflorian 		lock_basic_init(&http2_query_buffer_count_lock);
1408a1a7ba80Sflorian 		http2_query_buffer_lock_inited = 1;
1409a1a7ba80Sflorian 	}
1410a1a7ba80Sflorian 	if(!http2_response_buffer_lock_inited) {
1411a1a7ba80Sflorian 		lock_basic_init(&http2_response_buffer_count_lock);
1412a1a7ba80Sflorian 		http2_response_buffer_lock_inited = 1;
1413a1a7ba80Sflorian 	}
1414a1a7ba80Sflorian }
1415a1a7ba80Sflorian 
listen_desetup_locks(void)1416a1a7ba80Sflorian void listen_desetup_locks(void)
1417a1a7ba80Sflorian {
1418a1a7ba80Sflorian 	if(stream_wait_lock_inited) {
1419a1a7ba80Sflorian 		stream_wait_lock_inited = 0;
1420a1a7ba80Sflorian 		lock_basic_destroy(&stream_wait_count_lock);
1421a1a7ba80Sflorian 	}
1422a1a7ba80Sflorian 	if(http2_query_buffer_lock_inited) {
1423a1a7ba80Sflorian 		http2_query_buffer_lock_inited = 0;
1424a1a7ba80Sflorian 		lock_basic_destroy(&http2_query_buffer_count_lock);
1425a1a7ba80Sflorian 	}
1426a1a7ba80Sflorian 	if(http2_response_buffer_lock_inited) {
1427a1a7ba80Sflorian 		http2_response_buffer_lock_inited = 0;
1428a1a7ba80Sflorian 		lock_basic_destroy(&http2_response_buffer_count_lock);
1429a1a7ba80Sflorian 	}
1430a1a7ba80Sflorian }
1431a1a7ba80Sflorian 
1432ae8c6e27Sflorian struct listen_dnsport*
listen_create(struct comm_base * base,struct listen_port * ports,size_t bufsize,int tcp_accept_count,int tcp_idle_timeout,int harden_large_queries,uint32_t http_max_streams,char * http_endpoint,int http_notls,struct tcl_list * tcp_conn_limit,void * sslctx,struct dt_env * dtenv,comm_point_callback_type * cb,void * cb_arg)1433ae8c6e27Sflorian listen_create(struct comm_base* base, struct listen_port* ports,
1434ae8c6e27Sflorian 	size_t bufsize, int tcp_accept_count, int tcp_idle_timeout,
1435f4f0f0ceSflorian 	int harden_large_queries, uint32_t http_max_streams,
1436853e076fSflorian 	char* http_endpoint, int http_notls, struct tcl_list* tcp_conn_limit,
1437853e076fSflorian 	void* sslctx, struct dt_env* dtenv, comm_point_callback_type* cb,
1438853e076fSflorian 	void *cb_arg)
1439ae8c6e27Sflorian {
1440ae8c6e27Sflorian 	struct listen_dnsport* front = (struct listen_dnsport*)
1441ae8c6e27Sflorian 		malloc(sizeof(struct listen_dnsport));
1442ae8c6e27Sflorian 	if(!front)
1443ae8c6e27Sflorian 		return NULL;
1444ae8c6e27Sflorian 	front->cps = NULL;
1445ae8c6e27Sflorian 	front->udp_buff = sldns_buffer_new(bufsize);
1446ae8c6e27Sflorian #ifdef USE_DNSCRYPT
1447ae8c6e27Sflorian 	front->dnscrypt_udp_buff = NULL;
1448ae8c6e27Sflorian #endif
1449ae8c6e27Sflorian 	if(!front->udp_buff) {
1450ae8c6e27Sflorian 		free(front);
1451ae8c6e27Sflorian 		return NULL;
1452ae8c6e27Sflorian 	}
1453ae8c6e27Sflorian 
1454ae8c6e27Sflorian 	/* create comm points as needed */
1455ae8c6e27Sflorian 	while(ports) {
1456ae8c6e27Sflorian 		struct comm_point* cp = NULL;
1457ae8c6e27Sflorian 		if(ports->ftype == listen_type_udp ||
1458a1a7ba80Sflorian 		   ports->ftype == listen_type_udp_dnscrypt) {
1459ae8c6e27Sflorian 			cp = comm_point_create_udp(base, ports->fd,
14605c45b740Sflorian 				front->udp_buff, ports->pp2_enabled, cb,
14615c45b740Sflorian 				cb_arg, ports->socket);
1462a1a7ba80Sflorian 		} else if(ports->ftype == listen_type_tcp ||
1463a1a7ba80Sflorian 				ports->ftype == listen_type_tcp_dnscrypt) {
1464ae8c6e27Sflorian 			cp = comm_point_create_tcp(base, ports->fd,
1465ae8c6e27Sflorian 				tcp_accept_count, tcp_idle_timeout,
1466f4f0f0ceSflorian 				harden_large_queries, 0, NULL,
1467e97c6e54Ssthen 				tcp_conn_limit, bufsize, front->udp_buff,
14685c45b740Sflorian 				ports->ftype, ports->pp2_enabled, cb, cb_arg,
14695c45b740Sflorian 				ports->socket);
1470a1a7ba80Sflorian 		} else if(ports->ftype == listen_type_ssl ||
1471f4f0f0ceSflorian 			ports->ftype == listen_type_http) {
1472ae8c6e27Sflorian 			cp = comm_point_create_tcp(base, ports->fd,
1473ae8c6e27Sflorian 				tcp_accept_count, tcp_idle_timeout,
1474f4f0f0ceSflorian 				harden_large_queries,
1475f4f0f0ceSflorian 				http_max_streams, http_endpoint,
1476e97c6e54Ssthen 				tcp_conn_limit, bufsize, front->udp_buff,
14775c45b740Sflorian 				ports->ftype, ports->pp2_enabled, cb, cb_arg,
14785c45b740Sflorian 				ports->socket);
1479f4f0f0ceSflorian 			if(ports->ftype == listen_type_http) {
1480853e076fSflorian 				if(!sslctx && !http_notls) {
1481a1a7ba80Sflorian 					log_warn("HTTPS port configured, but "
1482a1a7ba80Sflorian 						"no TLS tls-service-key or "
1483a1a7ba80Sflorian 						"tls-service-pem set");
1484f4f0f0ceSflorian 				}
1485f4f0f0ceSflorian #ifndef HAVE_SSL_CTX_SET_ALPN_SELECT_CB
1486a1a7ba80Sflorian 				if(!http_notls) {
1487a1a7ba80Sflorian 					log_warn("Unbound is not compiled "
1488a1a7ba80Sflorian 						"with an OpenSSL version "
1489a1a7ba80Sflorian 						"supporting ALPN "
1490a1a7ba80Sflorian 						"(OpenSSL >= 1.0.2). This "
1491a1a7ba80Sflorian 						"is required to use "
1492a1a7ba80Sflorian 						"DNS-over-HTTPS");
1493a1a7ba80Sflorian 				}
1494f4f0f0ceSflorian #endif
1495f4f0f0ceSflorian #ifndef HAVE_NGHTTP2_NGHTTP2_H
1496f4f0f0ceSflorian 				log_warn("Unbound is not compiled with "
1497f4f0f0ceSflorian 					"nghttp2. This is required to use "
1498f4f0f0ceSflorian 					"DNS-over-HTTPS.");
1499f4f0f0ceSflorian #endif
1500f4f0f0ceSflorian 			}
1501ae8c6e27Sflorian 		} else if(ports->ftype == listen_type_udpancil ||
1502a1a7ba80Sflorian 				  ports->ftype == listen_type_udpancil_dnscrypt) {
1503*54cc57acSflorian #if defined(AF_INET6) && defined(IPV6_PKTINFO) && defined(HAVE_RECVMSG)
1504ae8c6e27Sflorian 			cp = comm_point_create_udp_ancil(base, ports->fd,
15055c45b740Sflorian 				front->udp_buff, ports->pp2_enabled, cb,
15065c45b740Sflorian 				cb_arg, ports->socket);
1507*54cc57acSflorian #else
1508*54cc57acSflorian 			log_warn("This system does not support UDP ancilliary data.");
1509*54cc57acSflorian #endif
1510a1a7ba80Sflorian 		}
1511ae8c6e27Sflorian 		if(!cp) {
1512ae8c6e27Sflorian 			log_err("can't create commpoint");
1513ae8c6e27Sflorian 			listen_delete(front);
1514ae8c6e27Sflorian 			return NULL;
1515ae8c6e27Sflorian 		}
1516a1a7ba80Sflorian 		if((http_notls && ports->ftype == listen_type_http) ||
1517a1a7ba80Sflorian 			(ports->ftype == listen_type_tcp) ||
1518a1a7ba80Sflorian 			(ports->ftype == listen_type_udp) ||
1519a1a7ba80Sflorian 			(ports->ftype == listen_type_udpancil) ||
1520a1a7ba80Sflorian 			(ports->ftype == listen_type_tcp_dnscrypt) ||
1521a1a7ba80Sflorian 			(ports->ftype == listen_type_udp_dnscrypt) ||
1522a1a7ba80Sflorian 			(ports->ftype == listen_type_udpancil_dnscrypt))
1523a1a7ba80Sflorian 			cp->ssl = NULL;
1524a1a7ba80Sflorian 		else
1525a1a7ba80Sflorian 			cp->ssl = sslctx;
1526ae8c6e27Sflorian 		cp->dtenv = dtenv;
1527ae8c6e27Sflorian 		cp->do_not_close = 1;
1528ae8c6e27Sflorian #ifdef USE_DNSCRYPT
1529ae8c6e27Sflorian 		if (ports->ftype == listen_type_udp_dnscrypt ||
1530ae8c6e27Sflorian 			ports->ftype == listen_type_tcp_dnscrypt ||
1531ae8c6e27Sflorian 			ports->ftype == listen_type_udpancil_dnscrypt) {
1532ae8c6e27Sflorian 			cp->dnscrypt = 1;
1533ae8c6e27Sflorian 			cp->dnscrypt_buffer = sldns_buffer_new(bufsize);
1534ae8c6e27Sflorian 			if(!cp->dnscrypt_buffer) {
1535ae8c6e27Sflorian 				log_err("can't alloc dnscrypt_buffer");
1536ae8c6e27Sflorian 				comm_point_delete(cp);
1537ae8c6e27Sflorian 				listen_delete(front);
1538ae8c6e27Sflorian 				return NULL;
1539ae8c6e27Sflorian 			}
1540ae8c6e27Sflorian 			front->dnscrypt_udp_buff = cp->dnscrypt_buffer;
1541ae8c6e27Sflorian 		}
1542ae8c6e27Sflorian #endif
1543ae8c6e27Sflorian 		if(!listen_cp_insert(cp, front)) {
1544ae8c6e27Sflorian 			log_err("malloc failed");
1545ae8c6e27Sflorian 			comm_point_delete(cp);
1546ae8c6e27Sflorian 			listen_delete(front);
1547ae8c6e27Sflorian 			return NULL;
1548ae8c6e27Sflorian 		}
1549ae8c6e27Sflorian 		ports = ports->next;
1550ae8c6e27Sflorian 	}
1551ae8c6e27Sflorian 	if(!front->cps) {
1552ae8c6e27Sflorian 		log_err("Could not open sockets to accept queries.");
1553ae8c6e27Sflorian 		listen_delete(front);
1554ae8c6e27Sflorian 		return NULL;
1555ae8c6e27Sflorian 	}
1556ae8c6e27Sflorian 
1557ae8c6e27Sflorian 	return front;
1558ae8c6e27Sflorian }
1559ae8c6e27Sflorian 
1560ae8c6e27Sflorian void
listen_list_delete(struct listen_list * list)1561ae8c6e27Sflorian listen_list_delete(struct listen_list* list)
1562ae8c6e27Sflorian {
1563ae8c6e27Sflorian 	struct listen_list *p = list, *pn;
1564ae8c6e27Sflorian 	while(p) {
1565ae8c6e27Sflorian 		pn = p->next;
1566ae8c6e27Sflorian 		comm_point_delete(p->com);
1567ae8c6e27Sflorian 		free(p);
1568ae8c6e27Sflorian 		p = pn;
1569ae8c6e27Sflorian 	}
1570ae8c6e27Sflorian }
1571ae8c6e27Sflorian 
1572ae8c6e27Sflorian void
listen_delete(struct listen_dnsport * front)1573ae8c6e27Sflorian listen_delete(struct listen_dnsport* front)
1574ae8c6e27Sflorian {
1575ae8c6e27Sflorian 	if(!front)
1576ae8c6e27Sflorian 		return;
1577ae8c6e27Sflorian 	listen_list_delete(front->cps);
1578ae8c6e27Sflorian #ifdef USE_DNSCRYPT
1579ae8c6e27Sflorian 	if(front->dnscrypt_udp_buff &&
1580ae8c6e27Sflorian 		front->udp_buff != front->dnscrypt_udp_buff) {
1581ae8c6e27Sflorian 		sldns_buffer_free(front->dnscrypt_udp_buff);
1582ae8c6e27Sflorian 	}
1583ae8c6e27Sflorian #endif
1584ae8c6e27Sflorian 	sldns_buffer_free(front->udp_buff);
1585ae8c6e27Sflorian 	free(front);
1586f4f0f0ceSflorian }
1587f4f0f0ceSflorian 
1588f4f0f0ceSflorian #ifdef HAVE_GETIFADDRS
1589f4f0f0ceSflorian static int
resolve_ifa_name(struct ifaddrs * ifas,const char * search_ifa,char *** ip_addresses,int * ip_addresses_size)1590f4f0f0ceSflorian resolve_ifa_name(struct ifaddrs *ifas, const char *search_ifa, char ***ip_addresses, int *ip_addresses_size)
1591f4f0f0ceSflorian {
1592f4f0f0ceSflorian 	struct ifaddrs *ifa;
1593853e076fSflorian 	void *tmpbuf;
1594f4f0f0ceSflorian 	int last_ip_addresses_size = *ip_addresses_size;
1595f4f0f0ceSflorian 
1596f4f0f0ceSflorian 	for(ifa = ifas; ifa != NULL; ifa = ifa->ifa_next) {
1597f4f0f0ceSflorian 		sa_family_t family;
1598f4f0f0ceSflorian 		const char* atsign;
1599f4f0f0ceSflorian #ifdef INET6      /* |   address ip    | % |  ifa name  | @ |  port  | nul */
1600f4f0f0ceSflorian 		char addr_buf[INET6_ADDRSTRLEN + 1 + IF_NAMESIZE + 1 + 16 + 1];
1601f4f0f0ceSflorian #else
1602f4f0f0ceSflorian 		char addr_buf[INET_ADDRSTRLEN + 1 + 16 + 1];
1603f4f0f0ceSflorian #endif
1604f4f0f0ceSflorian 
1605f4f0f0ceSflorian 		if((atsign=strrchr(search_ifa, '@')) != NULL) {
1606f4f0f0ceSflorian 			if(strlen(ifa->ifa_name) != (size_t)(atsign-search_ifa)
1607f4f0f0ceSflorian 			   || strncmp(ifa->ifa_name, search_ifa,
1608f4f0f0ceSflorian 			   atsign-search_ifa) != 0)
1609f4f0f0ceSflorian 				continue;
1610f4f0f0ceSflorian 		} else {
1611f4f0f0ceSflorian 			if(strcmp(ifa->ifa_name, search_ifa) != 0)
1612f4f0f0ceSflorian 				continue;
1613f4f0f0ceSflorian 			atsign = "";
1614f4f0f0ceSflorian 		}
1615f4f0f0ceSflorian 
1616f4f0f0ceSflorian 		if(ifa->ifa_addr == NULL)
1617f4f0f0ceSflorian 			continue;
1618f4f0f0ceSflorian 
1619f4f0f0ceSflorian 		family = ifa->ifa_addr->sa_family;
1620f4f0f0ceSflorian 		if(family == AF_INET) {
1621f4f0f0ceSflorian 			char a4[INET_ADDRSTRLEN + 1];
1622f4f0f0ceSflorian 			struct sockaddr_in *in4 = (struct sockaddr_in *)
1623f4f0f0ceSflorian 				ifa->ifa_addr;
1624f4f0f0ceSflorian 			if(!inet_ntop(family, &in4->sin_addr, a4, sizeof(a4))) {
1625f4f0f0ceSflorian 				log_err("inet_ntop failed");
1626f4f0f0ceSflorian 				return 0;
1627f4f0f0ceSflorian 			}
1628f4f0f0ceSflorian 			snprintf(addr_buf, sizeof(addr_buf), "%s%s",
1629f4f0f0ceSflorian 				a4, atsign);
1630f4f0f0ceSflorian 		}
1631f4f0f0ceSflorian #ifdef INET6
1632f4f0f0ceSflorian 		else if(family == AF_INET6) {
1633f4f0f0ceSflorian 			struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)
1634f4f0f0ceSflorian 				ifa->ifa_addr;
1635f4f0f0ceSflorian 			char a6[INET6_ADDRSTRLEN + 1];
1636f4f0f0ceSflorian 			char if_index_name[IF_NAMESIZE + 1];
1637f4f0f0ceSflorian 			if_index_name[0] = 0;
1638f4f0f0ceSflorian 			if(!inet_ntop(family, &in6->sin6_addr, a6, sizeof(a6))) {
1639f4f0f0ceSflorian 				log_err("inet_ntop failed");
1640f4f0f0ceSflorian 				return 0;
1641f4f0f0ceSflorian 			}
1642a8eaceedSflorian 			(void)if_indextoname(in6->sin6_scope_id,
1643f4f0f0ceSflorian 				(char *)if_index_name);
1644f4f0f0ceSflorian 			if (strlen(if_index_name) != 0) {
1645f4f0f0ceSflorian 				snprintf(addr_buf, sizeof(addr_buf),
1646f4f0f0ceSflorian 					"%s%%%s%s", a6, if_index_name, atsign);
1647f4f0f0ceSflorian 			} else {
1648f4f0f0ceSflorian 				snprintf(addr_buf, sizeof(addr_buf), "%s%s",
1649f4f0f0ceSflorian 					a6, atsign);
1650f4f0f0ceSflorian 			}
1651f4f0f0ceSflorian 		}
1652f4f0f0ceSflorian #endif
1653f4f0f0ceSflorian 		else {
1654f4f0f0ceSflorian 			continue;
1655f4f0f0ceSflorian 		}
1656f4f0f0ceSflorian 		verbose(4, "interface %s has address %s", search_ifa, addr_buf);
1657f4f0f0ceSflorian 
1658853e076fSflorian 		tmpbuf = realloc(*ip_addresses, sizeof(char *) * (*ip_addresses_size + 1));
1659853e076fSflorian 		if(!tmpbuf) {
1660f4f0f0ceSflorian 			log_err("realloc failed: out of memory");
1661f4f0f0ceSflorian 			return 0;
1662853e076fSflorian 		} else {
1663853e076fSflorian 			*ip_addresses = tmpbuf;
1664f4f0f0ceSflorian 		}
1665f4f0f0ceSflorian 		(*ip_addresses)[*ip_addresses_size] = strdup(addr_buf);
1666f4f0f0ceSflorian 		if(!(*ip_addresses)[*ip_addresses_size]) {
1667f4f0f0ceSflorian 			log_err("strdup failed: out of memory");
1668f4f0f0ceSflorian 			return 0;
1669f4f0f0ceSflorian 		}
1670f4f0f0ceSflorian 		(*ip_addresses_size)++;
1671f4f0f0ceSflorian 	}
1672f4f0f0ceSflorian 
1673f4f0f0ceSflorian 	if (*ip_addresses_size == last_ip_addresses_size) {
1674853e076fSflorian 		tmpbuf = realloc(*ip_addresses, sizeof(char *) * (*ip_addresses_size + 1));
1675853e076fSflorian 		if(!tmpbuf) {
1676f4f0f0ceSflorian 			log_err("realloc failed: out of memory");
1677f4f0f0ceSflorian 			return 0;
1678853e076fSflorian 		} else {
1679853e076fSflorian 			*ip_addresses = tmpbuf;
1680f4f0f0ceSflorian 		}
1681f4f0f0ceSflorian 		(*ip_addresses)[*ip_addresses_size] = strdup(search_ifa);
1682f4f0f0ceSflorian 		if(!(*ip_addresses)[*ip_addresses_size]) {
1683f4f0f0ceSflorian 			log_err("strdup failed: out of memory");
1684f4f0f0ceSflorian 			return 0;
1685f4f0f0ceSflorian 		}
1686f4f0f0ceSflorian 		(*ip_addresses_size)++;
1687f4f0f0ceSflorian 	}
1688f4f0f0ceSflorian 	return 1;
1689f4f0f0ceSflorian }
1690f4f0f0ceSflorian #endif /* HAVE_GETIFADDRS */
1691f4f0f0ceSflorian 
resolve_interface_names(char ** ifs,int num_ifs,struct config_strlist * list,char *** resif,int * num_resif)1692411c5950Sflorian int resolve_interface_names(char** ifs, int num_ifs,
1693411c5950Sflorian 	struct config_strlist* list, char*** resif, int* num_resif)
1694f4f0f0ceSflorian {
1695f4f0f0ceSflorian #ifdef HAVE_GETIFADDRS
1696f4f0f0ceSflorian 	struct ifaddrs *addrs = NULL;
1697411c5950Sflorian 	if(num_ifs == 0 && list == NULL) {
1698f4f0f0ceSflorian 		*resif = NULL;
1699f4f0f0ceSflorian 		*num_resif = 0;
1700f4f0f0ceSflorian 		return 1;
1701f4f0f0ceSflorian 	}
1702f4f0f0ceSflorian 	if(getifaddrs(&addrs) == -1) {
1703f4f0f0ceSflorian 		log_err("failed to list interfaces: getifaddrs: %s",
1704f4f0f0ceSflorian 			strerror(errno));
1705f4f0f0ceSflorian 		freeifaddrs(addrs);
1706f4f0f0ceSflorian 		return 0;
1707f4f0f0ceSflorian 	}
1708411c5950Sflorian 	if(ifs) {
1709411c5950Sflorian 		int i;
1710411c5950Sflorian 		for(i=0; i<num_ifs; i++) {
1711411c5950Sflorian 			if(!resolve_ifa_name(addrs, ifs[i], resif, num_resif)) {
1712f4f0f0ceSflorian 				freeifaddrs(addrs);
1713f4f0f0ceSflorian 				config_del_strarray(*resif, *num_resif);
1714f4f0f0ceSflorian 				*resif = NULL;
1715f4f0f0ceSflorian 				*num_resif = 0;
1716f4f0f0ceSflorian 				return 0;
1717f4f0f0ceSflorian 			}
1718f4f0f0ceSflorian 		}
1719411c5950Sflorian 	}
1720411c5950Sflorian 	if(list) {
1721411c5950Sflorian 		struct config_strlist* p;
1722411c5950Sflorian 		for(p = list; p; p = p->next) {
1723411c5950Sflorian 			if(!resolve_ifa_name(addrs, p->str, resif, num_resif)) {
1724411c5950Sflorian 				freeifaddrs(addrs);
1725411c5950Sflorian 				config_del_strarray(*resif, *num_resif);
1726411c5950Sflorian 				*resif = NULL;
1727411c5950Sflorian 				*num_resif = 0;
1728411c5950Sflorian 				return 0;
1729411c5950Sflorian 			}
1730411c5950Sflorian }
1731411c5950Sflorian 	}
1732f4f0f0ceSflorian 	freeifaddrs(addrs);
1733f4f0f0ceSflorian 	return 1;
1734f4f0f0ceSflorian #else
1735411c5950Sflorian 	struct config_strlist* p;
1736411c5950Sflorian 	if(num_ifs == 0 && list == NULL) {
1737f4f0f0ceSflorian 		*resif = NULL;
1738f4f0f0ceSflorian 		*num_resif = 0;
1739f4f0f0ceSflorian 		return 1;
1740f4f0f0ceSflorian 	}
1741411c5950Sflorian 	*num_resif = num_ifs;
1742411c5950Sflorian 	for(p = list; p; p = p->next) {
1743411c5950Sflorian 		(*num_resif)++;
1744411c5950Sflorian 	}
1745f4f0f0ceSflorian 	*resif = calloc(*num_resif, sizeof(**resif));
1746f4f0f0ceSflorian 	if(!*resif) {
1747f4f0f0ceSflorian 		log_err("out of memory");
1748f4f0f0ceSflorian 		return 0;
1749f4f0f0ceSflorian 	}
1750411c5950Sflorian 	if(ifs) {
1751411c5950Sflorian 		int i;
1752411c5950Sflorian 		for(i=0; i<num_ifs; i++) {
1753411c5950Sflorian 			(*resif)[i] = strdup(ifs[i]);
1754f4f0f0ceSflorian 			if(!((*resif)[i])) {
1755f4f0f0ceSflorian 				log_err("out of memory");
1756f4f0f0ceSflorian 				config_del_strarray(*resif, *num_resif);
1757f4f0f0ceSflorian 				*resif = NULL;
1758f4f0f0ceSflorian 				*num_resif = 0;
1759f4f0f0ceSflorian 				return 0;
1760f4f0f0ceSflorian 			}
1761f4f0f0ceSflorian 		}
1762411c5950Sflorian 	}
1763411c5950Sflorian 	if(list) {
1764411c5950Sflorian 		int idx = num_ifs;
1765411c5950Sflorian 		for(p = list; p; p = p->next) {
1766411c5950Sflorian 			(*resif)[idx] = strdup(p->str);
1767411c5950Sflorian 			if(!((*resif)[idx])) {
1768411c5950Sflorian 				log_err("out of memory");
1769411c5950Sflorian 				config_del_strarray(*resif, *num_resif);
1770411c5950Sflorian 				*resif = NULL;
1771411c5950Sflorian 				*num_resif = 0;
1772411c5950Sflorian 				return 0;
1773411c5950Sflorian 			}
1774411c5950Sflorian 			idx++;
1775411c5950Sflorian 		}
1776411c5950Sflorian 	}
1777f4f0f0ceSflorian 	return 1;
1778f4f0f0ceSflorian #endif /* HAVE_GETIFADDRS */
1779ae8c6e27Sflorian }
1780ae8c6e27Sflorian 
1781ae8c6e27Sflorian struct listen_port*
listening_ports_open(struct config_file * cfg,char ** ifs,int num_ifs,int * reuseport)1782f4f0f0ceSflorian listening_ports_open(struct config_file* cfg, char** ifs, int num_ifs,
1783f4f0f0ceSflorian 	int* reuseport)
1784ae8c6e27Sflorian {
1785ae8c6e27Sflorian 	struct listen_port* list = NULL;
1786ae8c6e27Sflorian 	struct addrinfo hints;
1787ae8c6e27Sflorian 	int i, do_ip4, do_ip6;
1788ae8c6e27Sflorian 	int do_tcp, do_auto;
1789ae8c6e27Sflorian 	char portbuf[32];
1790ae8c6e27Sflorian 	snprintf(portbuf, sizeof(portbuf), "%d", cfg->port);
1791ae8c6e27Sflorian 	do_ip4 = cfg->do_ip4;
1792ae8c6e27Sflorian 	do_ip6 = cfg->do_ip6;
1793ae8c6e27Sflorian 	do_tcp = cfg->do_tcp;
1794ae8c6e27Sflorian 	do_auto = cfg->if_automatic && cfg->do_udp;
1795ae8c6e27Sflorian 	if(cfg->incoming_num_tcp == 0)
1796ae8c6e27Sflorian 		do_tcp = 0;
1797ae8c6e27Sflorian 
1798ae8c6e27Sflorian 	/* getaddrinfo */
1799ae8c6e27Sflorian 	memset(&hints, 0, sizeof(hints));
1800ae8c6e27Sflorian 	hints.ai_flags = AI_PASSIVE;
1801ae8c6e27Sflorian 	/* no name lookups on our listening ports */
1802f4f0f0ceSflorian 	if(num_ifs > 0)
1803ae8c6e27Sflorian 		hints.ai_flags |= AI_NUMERICHOST;
1804ae8c6e27Sflorian 	hints.ai_family = AF_UNSPEC;
1805ae8c6e27Sflorian #ifndef INET6
1806ae8c6e27Sflorian 	do_ip6 = 0;
1807ae8c6e27Sflorian #endif
1808ae8c6e27Sflorian 	if(!do_ip4 && !do_ip6) {
1809ae8c6e27Sflorian 		return NULL;
1810ae8c6e27Sflorian 	}
1811ae8c6e27Sflorian 	/* create ip4 and ip6 ports so that return addresses are nice. */
1812f4f0f0ceSflorian 	if(do_auto || num_ifs == 0) {
18137a05b9dfSflorian 		if(do_auto && cfg->if_automatic_ports &&
18147a05b9dfSflorian 			cfg->if_automatic_ports[0]!=0) {
18157a05b9dfSflorian 			char* now = cfg->if_automatic_ports;
18167a05b9dfSflorian 			while(now && *now) {
18177a05b9dfSflorian 				char* after;
18187a05b9dfSflorian 				int extraport;
18197a05b9dfSflorian 				while(isspace((unsigned char)*now))
18207a05b9dfSflorian 					now++;
18217a05b9dfSflorian 				if(!*now)
18227a05b9dfSflorian 					break;
18237a05b9dfSflorian 				after = now;
18247a05b9dfSflorian 				extraport = (int)strtol(now, &after, 10);
18257a05b9dfSflorian 				if(extraport < 0 || extraport > 65535) {
18267a05b9dfSflorian 					log_err("interface-automatic-ports port number out of range, at position %d of '%s'", (int)(now-cfg->if_automatic_ports)+1, cfg->if_automatic_ports);
18277a05b9dfSflorian 					listening_ports_free(list);
18287a05b9dfSflorian 					return NULL;
18297a05b9dfSflorian 				}
18307a05b9dfSflorian 				if(extraport == 0 && now == after) {
18317a05b9dfSflorian 					log_err("interface-automatic-ports could not be parsed, at position %d of '%s'", (int)(now-cfg->if_automatic_ports)+1, cfg->if_automatic_ports);
18327a05b9dfSflorian 					listening_ports_free(list);
18337a05b9dfSflorian 					return NULL;
18347a05b9dfSflorian 				}
18357a05b9dfSflorian 				now = after;
18367a05b9dfSflorian 				snprintf(portbuf, sizeof(portbuf), "%d", extraport);
18377a05b9dfSflorian 				if(do_ip6) {
18387a05b9dfSflorian 					hints.ai_family = AF_INET6;
18397a05b9dfSflorian 					if(!ports_create_if("::0",
18407a05b9dfSflorian 						do_auto, cfg->do_udp, do_tcp,
18417a05b9dfSflorian 						&hints, portbuf, &list,
18427a05b9dfSflorian 						cfg->so_rcvbuf, cfg->so_sndbuf,
18437a05b9dfSflorian 						cfg->ssl_port, cfg->tls_additional_port,
18445c45b740Sflorian 						cfg->https_port,
18455c45b740Sflorian 						cfg->proxy_protocol_port,
18465c45b740Sflorian 						reuseport, cfg->ip_transparent,
18477a05b9dfSflorian 						cfg->tcp_mss, cfg->ip_freebind,
18487a05b9dfSflorian 						cfg->http_nodelay, cfg->use_systemd,
1849d500c338Sflorian 						cfg->dnscrypt_port, cfg->ip_dscp, cfg->sock_queue_timeout)) {
18507a05b9dfSflorian 						listening_ports_free(list);
18517a05b9dfSflorian 						return NULL;
18527a05b9dfSflorian 					}
18537a05b9dfSflorian 				}
18547a05b9dfSflorian 				if(do_ip4) {
18557a05b9dfSflorian 					hints.ai_family = AF_INET;
18567a05b9dfSflorian 					if(!ports_create_if("0.0.0.0",
18577a05b9dfSflorian 						do_auto, cfg->do_udp, do_tcp,
18587a05b9dfSflorian 						&hints, portbuf, &list,
18597a05b9dfSflorian 						cfg->so_rcvbuf, cfg->so_sndbuf,
18607a05b9dfSflorian 						cfg->ssl_port, cfg->tls_additional_port,
18615c45b740Sflorian 						cfg->https_port,
18625c45b740Sflorian 						cfg->proxy_protocol_port,
18635c45b740Sflorian 						reuseport, cfg->ip_transparent,
18647a05b9dfSflorian 						cfg->tcp_mss, cfg->ip_freebind,
18657a05b9dfSflorian 						cfg->http_nodelay, cfg->use_systemd,
1866d500c338Sflorian 						cfg->dnscrypt_port, cfg->ip_dscp, cfg->sock_queue_timeout)) {
18677a05b9dfSflorian 						listening_ports_free(list);
18687a05b9dfSflorian 						return NULL;
18697a05b9dfSflorian 					}
18707a05b9dfSflorian 				}
18717a05b9dfSflorian 			}
18727a05b9dfSflorian 			return list;
18737a05b9dfSflorian 		}
1874ae8c6e27Sflorian 		if(do_ip6) {
1875ae8c6e27Sflorian 			hints.ai_family = AF_INET6;
1876ae8c6e27Sflorian 			if(!ports_create_if(do_auto?"::0":"::1",
1877ae8c6e27Sflorian 				do_auto, cfg->do_udp, do_tcp,
1878ae8c6e27Sflorian 				&hints, portbuf, &list,
1879ae8c6e27Sflorian 				cfg->so_rcvbuf, cfg->so_sndbuf,
1880ae8c6e27Sflorian 				cfg->ssl_port, cfg->tls_additional_port,
18815c45b740Sflorian 				cfg->https_port, cfg->proxy_protocol_port,
18825c45b740Sflorian 				reuseport, cfg->ip_transparent,
1883f4f0f0ceSflorian 				cfg->tcp_mss, cfg->ip_freebind,
1884f4f0f0ceSflorian 				cfg->http_nodelay, cfg->use_systemd,
1885d500c338Sflorian 				cfg->dnscrypt_port, cfg->ip_dscp, cfg->sock_queue_timeout)) {
1886ae8c6e27Sflorian 				listening_ports_free(list);
1887ae8c6e27Sflorian 				return NULL;
1888ae8c6e27Sflorian 			}
1889ae8c6e27Sflorian 		}
1890ae8c6e27Sflorian 		if(do_ip4) {
1891ae8c6e27Sflorian 			hints.ai_family = AF_INET;
1892ae8c6e27Sflorian 			if(!ports_create_if(do_auto?"0.0.0.0":"127.0.0.1",
1893ae8c6e27Sflorian 				do_auto, cfg->do_udp, do_tcp,
1894ae8c6e27Sflorian 				&hints, portbuf, &list,
1895ae8c6e27Sflorian 				cfg->so_rcvbuf, cfg->so_sndbuf,
1896ae8c6e27Sflorian 				cfg->ssl_port, cfg->tls_additional_port,
18975c45b740Sflorian 				cfg->https_port, cfg->proxy_protocol_port,
18985c45b740Sflorian 				reuseport, cfg->ip_transparent,
1899f4f0f0ceSflorian 				cfg->tcp_mss, cfg->ip_freebind,
1900f4f0f0ceSflorian 				cfg->http_nodelay, cfg->use_systemd,
1901d500c338Sflorian 				cfg->dnscrypt_port, cfg->ip_dscp, cfg->sock_queue_timeout)) {
1902ae8c6e27Sflorian 				listening_ports_free(list);
1903ae8c6e27Sflorian 				return NULL;
1904ae8c6e27Sflorian 			}
1905ae8c6e27Sflorian 		}
1906f4f0f0ceSflorian 	} else for(i = 0; i<num_ifs; i++) {
1907f4f0f0ceSflorian 		if(str_is_ip6(ifs[i])) {
1908ae8c6e27Sflorian 			if(!do_ip6)
1909ae8c6e27Sflorian 				continue;
1910ae8c6e27Sflorian 			hints.ai_family = AF_INET6;
1911f4f0f0ceSflorian 			if(!ports_create_if(ifs[i], 0, cfg->do_udp,
1912ae8c6e27Sflorian 				do_tcp, &hints, portbuf, &list,
1913ae8c6e27Sflorian 				cfg->so_rcvbuf, cfg->so_sndbuf,
1914ae8c6e27Sflorian 				cfg->ssl_port, cfg->tls_additional_port,
19155c45b740Sflorian 				cfg->https_port, cfg->proxy_protocol_port,
19165c45b740Sflorian 				reuseport, cfg->ip_transparent,
1917f4f0f0ceSflorian 				cfg->tcp_mss, cfg->ip_freebind,
1918f4f0f0ceSflorian 				cfg->http_nodelay, cfg->use_systemd,
1919d500c338Sflorian 				cfg->dnscrypt_port, cfg->ip_dscp, cfg->sock_queue_timeout)) {
1920ae8c6e27Sflorian 				listening_ports_free(list);
1921ae8c6e27Sflorian 				return NULL;
1922ae8c6e27Sflorian 			}
1923ae8c6e27Sflorian 		} else {
1924ae8c6e27Sflorian 			if(!do_ip4)
1925ae8c6e27Sflorian 				continue;
1926ae8c6e27Sflorian 			hints.ai_family = AF_INET;
1927f4f0f0ceSflorian 			if(!ports_create_if(ifs[i], 0, cfg->do_udp,
1928ae8c6e27Sflorian 				do_tcp, &hints, portbuf, &list,
1929ae8c6e27Sflorian 				cfg->so_rcvbuf, cfg->so_sndbuf,
1930ae8c6e27Sflorian 				cfg->ssl_port, cfg->tls_additional_port,
19315c45b740Sflorian 				cfg->https_port, cfg->proxy_protocol_port,
19325c45b740Sflorian 				reuseport, cfg->ip_transparent,
1933f4f0f0ceSflorian 				cfg->tcp_mss, cfg->ip_freebind,
1934f4f0f0ceSflorian 				cfg->http_nodelay, cfg->use_systemd,
1935d500c338Sflorian 				cfg->dnscrypt_port, cfg->ip_dscp, cfg->sock_queue_timeout)) {
1936ae8c6e27Sflorian 				listening_ports_free(list);
1937ae8c6e27Sflorian 				return NULL;
1938ae8c6e27Sflorian 			}
1939ae8c6e27Sflorian 		}
1940ae8c6e27Sflorian 	}
1941411c5950Sflorian 
1942ae8c6e27Sflorian 	return list;
1943ae8c6e27Sflorian }
1944ae8c6e27Sflorian 
listening_ports_free(struct listen_port * list)1945ae8c6e27Sflorian void listening_ports_free(struct listen_port* list)
1946ae8c6e27Sflorian {
1947ae8c6e27Sflorian 	struct listen_port* nx;
1948ae8c6e27Sflorian 	while(list) {
1949ae8c6e27Sflorian 		nx = list->next;
1950ae8c6e27Sflorian 		if(list->fd != -1) {
1951f4f0f0ceSflorian 			sock_close(list->fd);
1952ae8c6e27Sflorian 		}
1953411c5950Sflorian 		/* rc_ports don't have ub_socket */
1954411c5950Sflorian 		if(list->socket) {
1955d500c338Sflorian 			if(list->socket->addr)
1956411c5950Sflorian 				freeaddrinfo(list->socket->addr);
1957411c5950Sflorian 			free(list->socket);
1958411c5950Sflorian 		}
1959ae8c6e27Sflorian 		free(list);
1960ae8c6e27Sflorian 		list = nx;
1961ae8c6e27Sflorian 	}
1962ae8c6e27Sflorian }
1963ae8c6e27Sflorian 
listen_get_mem(struct listen_dnsport * listen)1964ae8c6e27Sflorian size_t listen_get_mem(struct listen_dnsport* listen)
1965ae8c6e27Sflorian {
1966ae8c6e27Sflorian 	struct listen_list* p;
1967ae8c6e27Sflorian 	size_t s = sizeof(*listen) + sizeof(*listen->base) +
1968ae8c6e27Sflorian 		sizeof(*listen->udp_buff) +
1969ae8c6e27Sflorian 		sldns_buffer_capacity(listen->udp_buff);
1970ae8c6e27Sflorian #ifdef USE_DNSCRYPT
1971ae8c6e27Sflorian 	s += sizeof(*listen->dnscrypt_udp_buff);
1972ae8c6e27Sflorian 	if(listen->udp_buff != listen->dnscrypt_udp_buff){
1973ae8c6e27Sflorian 		s += sldns_buffer_capacity(listen->dnscrypt_udp_buff);
1974ae8c6e27Sflorian 	}
1975ae8c6e27Sflorian #endif
1976ae8c6e27Sflorian 	for(p = listen->cps; p; p = p->next) {
1977ae8c6e27Sflorian 		s += sizeof(*p);
1978ae8c6e27Sflorian 		s += comm_point_get_mem(p->com);
1979ae8c6e27Sflorian 	}
1980ae8c6e27Sflorian 	return s;
1981ae8c6e27Sflorian }
1982ae8c6e27Sflorian 
listen_stop_accept(struct listen_dnsport * listen)1983ae8c6e27Sflorian void listen_stop_accept(struct listen_dnsport* listen)
1984ae8c6e27Sflorian {
1985ae8c6e27Sflorian 	/* do not stop the ones that have no tcp_free list
1986ae8c6e27Sflorian 	 * (they have already stopped listening) */
1987ae8c6e27Sflorian 	struct listen_list* p;
1988ae8c6e27Sflorian 	for(p=listen->cps; p; p=p->next) {
1989ae8c6e27Sflorian 		if(p->com->type == comm_tcp_accept &&
1990ae8c6e27Sflorian 			p->com->tcp_free != NULL) {
1991ae8c6e27Sflorian 			comm_point_stop_listening(p->com);
1992ae8c6e27Sflorian 		}
1993ae8c6e27Sflorian 	}
1994ae8c6e27Sflorian }
1995ae8c6e27Sflorian 
listen_start_accept(struct listen_dnsport * listen)1996ae8c6e27Sflorian void listen_start_accept(struct listen_dnsport* listen)
1997ae8c6e27Sflorian {
1998ae8c6e27Sflorian 	/* do not start the ones that have no tcp_free list, it is no
1999ae8c6e27Sflorian 	 * use to listen to them because they have no free tcp handlers */
2000ae8c6e27Sflorian 	struct listen_list* p;
2001ae8c6e27Sflorian 	for(p=listen->cps; p; p=p->next) {
2002ae8c6e27Sflorian 		if(p->com->type == comm_tcp_accept &&
2003ae8c6e27Sflorian 			p->com->tcp_free != NULL) {
2004ae8c6e27Sflorian 			comm_point_start_listening(p->com, -1, -1);
2005ae8c6e27Sflorian 		}
2006ae8c6e27Sflorian 	}
2007ae8c6e27Sflorian }
2008ae8c6e27Sflorian 
2009e97c6e54Ssthen struct tcp_req_info*
tcp_req_info_create(struct sldns_buffer * spoolbuf)2010e97c6e54Ssthen tcp_req_info_create(struct sldns_buffer* spoolbuf)
2011e97c6e54Ssthen {
2012e97c6e54Ssthen 	struct tcp_req_info* req = (struct tcp_req_info*)malloc(sizeof(*req));
2013e97c6e54Ssthen 	if(!req) {
2014e97c6e54Ssthen 		log_err("malloc failure for new stream outoforder processing structure");
2015e97c6e54Ssthen 		return NULL;
2016e97c6e54Ssthen 	}
2017e97c6e54Ssthen 	memset(req, 0, sizeof(*req));
2018e97c6e54Ssthen 	req->spool_buffer = spoolbuf;
2019e97c6e54Ssthen 	return req;
2020e97c6e54Ssthen }
2021e97c6e54Ssthen 
2022e97c6e54Ssthen void
tcp_req_info_delete(struct tcp_req_info * req)2023e97c6e54Ssthen tcp_req_info_delete(struct tcp_req_info* req)
2024e97c6e54Ssthen {
2025e97c6e54Ssthen 	if(!req) return;
2026e97c6e54Ssthen 	tcp_req_info_clear(req);
2027e97c6e54Ssthen 	/* cp is pointer back to commpoint that owns this struct and
2028e97c6e54Ssthen 	 * called delete on us */
2029e97c6e54Ssthen 	/* spool_buffer is shared udp buffer, not deleted here */
2030e97c6e54Ssthen 	free(req);
2031e97c6e54Ssthen }
2032e97c6e54Ssthen 
tcp_req_info_clear(struct tcp_req_info * req)2033e97c6e54Ssthen void tcp_req_info_clear(struct tcp_req_info* req)
2034e97c6e54Ssthen {
2035e97c6e54Ssthen 	struct tcp_req_open_item* open, *nopen;
2036e97c6e54Ssthen 	struct tcp_req_done_item* item, *nitem;
2037e97c6e54Ssthen 	if(!req) return;
2038e97c6e54Ssthen 
2039e97c6e54Ssthen 	/* free outstanding request mesh reply entries */
2040e97c6e54Ssthen 	open = req->open_req_list;
2041e97c6e54Ssthen 	while(open) {
2042e97c6e54Ssthen 		nopen = open->next;
2043e97c6e54Ssthen 		mesh_state_remove_reply(open->mesh, open->mesh_state, req->cp);
2044e97c6e54Ssthen 		free(open);
2045e97c6e54Ssthen 		open = nopen;
2046e97c6e54Ssthen 	}
2047e97c6e54Ssthen 	req->open_req_list = NULL;
2048e97c6e54Ssthen 	req->num_open_req = 0;
2049e97c6e54Ssthen 
2050e97c6e54Ssthen 	/* free pending writable result packets */
2051e97c6e54Ssthen 	item = req->done_req_list;
2052e97c6e54Ssthen 	while(item) {
2053e97c6e54Ssthen 		nitem = item->next;
2054e97c6e54Ssthen 		lock_basic_lock(&stream_wait_count_lock);
2055e97c6e54Ssthen 		stream_wait_count -= (sizeof(struct tcp_req_done_item)
2056e97c6e54Ssthen 			+item->len);
2057e97c6e54Ssthen 		lock_basic_unlock(&stream_wait_count_lock);
2058e97c6e54Ssthen 		free(item->buf);
2059e97c6e54Ssthen 		free(item);
2060e97c6e54Ssthen 		item = nitem;
2061e97c6e54Ssthen 	}
2062e97c6e54Ssthen 	req->done_req_list = NULL;
2063e97c6e54Ssthen 	req->num_done_req = 0;
2064e97c6e54Ssthen 	req->read_is_closed = 0;
2065e97c6e54Ssthen }
2066e97c6e54Ssthen 
2067e97c6e54Ssthen void
tcp_req_info_remove_mesh_state(struct tcp_req_info * req,struct mesh_state * m)2068e97c6e54Ssthen tcp_req_info_remove_mesh_state(struct tcp_req_info* req, struct mesh_state* m)
2069e97c6e54Ssthen {
2070e97c6e54Ssthen 	struct tcp_req_open_item* open, *prev = NULL;
2071e97c6e54Ssthen 	if(!req || !m) return;
2072e97c6e54Ssthen 	open = req->open_req_list;
2073e97c6e54Ssthen 	while(open) {
2074e97c6e54Ssthen 		if(open->mesh_state == m) {
2075e97c6e54Ssthen 			struct tcp_req_open_item* next;
2076e97c6e54Ssthen 			if(prev) prev->next = open->next;
2077e97c6e54Ssthen 			else req->open_req_list = open->next;
2078e97c6e54Ssthen 			/* caller has to manage the mesh state reply entry */
2079e97c6e54Ssthen 			next = open->next;
2080e97c6e54Ssthen 			free(open);
2081e97c6e54Ssthen 			req->num_open_req --;
2082e97c6e54Ssthen 
2083e97c6e54Ssthen 			/* prev = prev; */
2084e97c6e54Ssthen 			open = next;
2085e97c6e54Ssthen 			continue;
2086e97c6e54Ssthen 		}
2087e97c6e54Ssthen 		prev = open;
2088e97c6e54Ssthen 		open = open->next;
2089e97c6e54Ssthen 	}
2090e97c6e54Ssthen }
2091e97c6e54Ssthen 
2092e97c6e54Ssthen /** setup listening for read or write */
2093e97c6e54Ssthen static void
tcp_req_info_setup_listen(struct tcp_req_info * req)2094e97c6e54Ssthen tcp_req_info_setup_listen(struct tcp_req_info* req)
2095e97c6e54Ssthen {
2096e97c6e54Ssthen 	int wr = 0;
2097e97c6e54Ssthen 	int rd = 0;
2098e97c6e54Ssthen 
2099e97c6e54Ssthen 	if(req->cp->tcp_byte_count != 0) {
2100e97c6e54Ssthen 		/* cannot change, halfway through */
2101e97c6e54Ssthen 		return;
2102e97c6e54Ssthen 	}
2103e97c6e54Ssthen 
2104e97c6e54Ssthen 	if(!req->cp->tcp_is_reading)
2105e97c6e54Ssthen 		wr = 1;
2106853e076fSflorian 	if(!req->read_is_closed)
2107e97c6e54Ssthen 		rd = 1;
2108e97c6e54Ssthen 
2109e97c6e54Ssthen 	if(wr) {
2110e97c6e54Ssthen 		req->cp->tcp_is_reading = 0;
2111988ebc2dSflorian 		comm_point_stop_listening(req->cp);
2112e97c6e54Ssthen 		comm_point_start_listening(req->cp, -1,
2113a8eaceedSflorian 			adjusted_tcp_timeout(req->cp));
2114e97c6e54Ssthen 	} else if(rd) {
2115e97c6e54Ssthen 		req->cp->tcp_is_reading = 1;
2116988ebc2dSflorian 		comm_point_stop_listening(req->cp);
2117e97c6e54Ssthen 		comm_point_start_listening(req->cp, -1,
2118a8eaceedSflorian 			adjusted_tcp_timeout(req->cp));
2119e97c6e54Ssthen 		/* and also read it (from SSL stack buffers), so
2120e97c6e54Ssthen 		 * no event read event is expected since the remainder of
2121e97c6e54Ssthen 		 * the TLS frame is sitting in the buffers. */
2122e97c6e54Ssthen 		req->read_again = 1;
2123e97c6e54Ssthen 	} else {
2124988ebc2dSflorian 		comm_point_stop_listening(req->cp);
2125e97c6e54Ssthen 		comm_point_start_listening(req->cp, -1,
2126a8eaceedSflorian 			adjusted_tcp_timeout(req->cp));
2127e97c6e54Ssthen 		comm_point_listen_for_rw(req->cp, 0, 0);
2128e97c6e54Ssthen 	}
2129e97c6e54Ssthen }
2130e97c6e54Ssthen 
2131e97c6e54Ssthen /** remove first item from list of pending results */
2132e97c6e54Ssthen static struct tcp_req_done_item*
tcp_req_info_pop_done(struct tcp_req_info * req)2133e97c6e54Ssthen tcp_req_info_pop_done(struct tcp_req_info* req)
2134e97c6e54Ssthen {
2135e97c6e54Ssthen 	struct tcp_req_done_item* item;
2136e97c6e54Ssthen 	log_assert(req->num_done_req > 0 && req->done_req_list);
2137e97c6e54Ssthen 	item = req->done_req_list;
2138e97c6e54Ssthen 	lock_basic_lock(&stream_wait_count_lock);
2139e97c6e54Ssthen 	stream_wait_count -= (sizeof(struct tcp_req_done_item)+item->len);
2140e97c6e54Ssthen 	lock_basic_unlock(&stream_wait_count_lock);
2141e97c6e54Ssthen 	req->done_req_list = req->done_req_list->next;
2142e97c6e54Ssthen 	req->num_done_req --;
2143e97c6e54Ssthen 	return item;
2144e97c6e54Ssthen }
2145e97c6e54Ssthen 
2146e97c6e54Ssthen /** Send given buffer and setup to write */
2147e97c6e54Ssthen static void
tcp_req_info_start_write_buf(struct tcp_req_info * req,uint8_t * buf,size_t len)2148e97c6e54Ssthen tcp_req_info_start_write_buf(struct tcp_req_info* req, uint8_t* buf,
2149e97c6e54Ssthen 	size_t len)
2150e97c6e54Ssthen {
2151e97c6e54Ssthen 	sldns_buffer_clear(req->cp->buffer);
2152e97c6e54Ssthen 	sldns_buffer_write(req->cp->buffer, buf, len);
2153e97c6e54Ssthen 	sldns_buffer_flip(req->cp->buffer);
2154e97c6e54Ssthen 
2155e97c6e54Ssthen 	req->cp->tcp_is_reading = 0; /* we are now writing */
2156e97c6e54Ssthen }
2157e97c6e54Ssthen 
2158e97c6e54Ssthen /** pick up the next result and start writing it to the channel */
2159e97c6e54Ssthen static void
tcp_req_pickup_next_result(struct tcp_req_info * req)2160e97c6e54Ssthen tcp_req_pickup_next_result(struct tcp_req_info* req)
2161e97c6e54Ssthen {
2162e97c6e54Ssthen 	if(req->num_done_req > 0) {
2163e97c6e54Ssthen 		/* unlist the done item from the list of pending results */
2164e97c6e54Ssthen 		struct tcp_req_done_item* item = tcp_req_info_pop_done(req);
2165e97c6e54Ssthen 		tcp_req_info_start_write_buf(req, item->buf, item->len);
2166e97c6e54Ssthen 		free(item->buf);
2167e97c6e54Ssthen 		free(item);
2168e97c6e54Ssthen 	}
2169e97c6e54Ssthen }
2170e97c6e54Ssthen 
2171e97c6e54Ssthen /** the read channel has closed */
2172e97c6e54Ssthen int
tcp_req_info_handle_read_close(struct tcp_req_info * req)2173e97c6e54Ssthen tcp_req_info_handle_read_close(struct tcp_req_info* req)
2174e97c6e54Ssthen {
2175e97c6e54Ssthen 	verbose(VERB_ALGO, "tcp channel read side closed %d", req->cp->fd);
2176e97c6e54Ssthen 	/* reset byte count for (potential) partial read */
2177e97c6e54Ssthen 	req->cp->tcp_byte_count = 0;
2178e97c6e54Ssthen 	/* if we still have results to write, pick up next and write it */
2179e97c6e54Ssthen 	if(req->num_done_req != 0) {
2180e97c6e54Ssthen 		tcp_req_pickup_next_result(req);
2181e97c6e54Ssthen 		tcp_req_info_setup_listen(req);
2182e97c6e54Ssthen 		return 1;
2183e97c6e54Ssthen 	}
2184e97c6e54Ssthen 	/* if nothing to do, this closes the connection */
2185e97c6e54Ssthen 	if(req->num_open_req == 0 && req->num_done_req == 0)
2186e97c6e54Ssthen 		return 0;
2187e97c6e54Ssthen 	/* otherwise, we must be waiting for dns resolve, wait with timeout */
2188e97c6e54Ssthen 	req->read_is_closed = 1;
2189e97c6e54Ssthen 	tcp_req_info_setup_listen(req);
2190e97c6e54Ssthen 	return 1;
2191e97c6e54Ssthen }
2192e97c6e54Ssthen 
2193e97c6e54Ssthen void
tcp_req_info_handle_writedone(struct tcp_req_info * req)2194e97c6e54Ssthen tcp_req_info_handle_writedone(struct tcp_req_info* req)
2195e97c6e54Ssthen {
2196e97c6e54Ssthen 	/* back to reading state, we finished this write event */
2197e97c6e54Ssthen 	sldns_buffer_clear(req->cp->buffer);
2198e97c6e54Ssthen 	if(req->num_done_req == 0 && req->read_is_closed) {
2199e97c6e54Ssthen 		/* no more to write and nothing to read, close it */
2200e97c6e54Ssthen 		comm_point_drop_reply(&req->cp->repinfo);
2201e97c6e54Ssthen 		return;
2202e97c6e54Ssthen 	}
2203e97c6e54Ssthen 	req->cp->tcp_is_reading = 1;
2204e97c6e54Ssthen 	/* see if another result needs writing */
2205e97c6e54Ssthen 	tcp_req_pickup_next_result(req);
2206e97c6e54Ssthen 
2207e97c6e54Ssthen 	/* see if there is more to write, if not stop_listening for writing */
2208e97c6e54Ssthen 	/* see if new requests are allowed, if so, start_listening
2209e97c6e54Ssthen 	 * for reading */
2210e97c6e54Ssthen 	tcp_req_info_setup_listen(req);
2211e97c6e54Ssthen }
2212e97c6e54Ssthen 
2213e97c6e54Ssthen void
tcp_req_info_handle_readdone(struct tcp_req_info * req)2214e97c6e54Ssthen tcp_req_info_handle_readdone(struct tcp_req_info* req)
2215e97c6e54Ssthen {
2216e97c6e54Ssthen 	struct comm_point* c = req->cp;
2217e97c6e54Ssthen 
2218e97c6e54Ssthen 	/* we want to read up several requests, unless there are
2219e97c6e54Ssthen 	 * pending answers */
2220e97c6e54Ssthen 
2221e97c6e54Ssthen 	req->is_drop = 0;
2222e97c6e54Ssthen 	req->is_reply = 0;
2223e97c6e54Ssthen 	req->in_worker_handle = 1;
22249b465e50Sflorian 	sldns_buffer_set_limit(req->spool_buffer, 0);
2225e97c6e54Ssthen 	/* handle the current request */
2226e97c6e54Ssthen 	/* this calls the worker handle request routine that could give
2227e97c6e54Ssthen 	 * a cache response, or localdata response, or drop the reply,
2228e97c6e54Ssthen 	 * or schedule a mesh entry for later */
2229e97c6e54Ssthen 	fptr_ok(fptr_whitelist_comm_point(c->callback));
2230e97c6e54Ssthen 	if( (*c->callback)(c, c->cb_arg, NETEVENT_NOERROR, &c->repinfo) ) {
2231e97c6e54Ssthen 		req->in_worker_handle = 0;
2232e97c6e54Ssthen 		/* there is an answer, put it up.  It is already in the
2233e97c6e54Ssthen 		 * c->buffer, just send it. */
2234e97c6e54Ssthen 		/* since we were just reading a query, the channel is
2235e97c6e54Ssthen 		 * clear to write to */
2236e97c6e54Ssthen 	send_it:
2237e97c6e54Ssthen 		c->tcp_is_reading = 0;
2238988ebc2dSflorian 		comm_point_stop_listening(c);
2239a8eaceedSflorian 		comm_point_start_listening(c, -1, adjusted_tcp_timeout(c));
2240e97c6e54Ssthen 		return;
2241e97c6e54Ssthen 	}
2242e97c6e54Ssthen 	req->in_worker_handle = 0;
2243e97c6e54Ssthen 	/* it should be waiting in the mesh for recursion.
2244e97c6e54Ssthen 	 * If mesh failed to add a new entry and called commpoint_drop_reply.
2245e97c6e54Ssthen 	 * Then the mesh state has been cleared. */
2246e97c6e54Ssthen 	if(req->is_drop) {
22479b465e50Sflorian 		/* the reply has been dropped, stream has been closed. */
2248e97c6e54Ssthen 		return;
2249e97c6e54Ssthen 	}
2250e97c6e54Ssthen 	/* If mesh failed(mallocfail) and called commpoint_send_reply with
2251e97c6e54Ssthen 	 * something like servfail then we pick up that reply below. */
2252e97c6e54Ssthen 	if(req->is_reply) {
2253e97c6e54Ssthen 		goto send_it;
2254e97c6e54Ssthen 	}
2255e97c6e54Ssthen 
2256e97c6e54Ssthen 	sldns_buffer_clear(c->buffer);
2257e97c6e54Ssthen 	/* if pending answers, pick up an answer and start sending it */
2258e97c6e54Ssthen 	tcp_req_pickup_next_result(req);
2259e97c6e54Ssthen 
2260e97c6e54Ssthen 	/* if answers pending, start sending answers */
2261e97c6e54Ssthen 	/* read more requests if we can have more requests */
2262e97c6e54Ssthen 	tcp_req_info_setup_listen(req);
2263e97c6e54Ssthen }
2264e97c6e54Ssthen 
2265e97c6e54Ssthen int
tcp_req_info_add_meshstate(struct tcp_req_info * req,struct mesh_area * mesh,struct mesh_state * m)2266e97c6e54Ssthen tcp_req_info_add_meshstate(struct tcp_req_info* req,
2267e97c6e54Ssthen 	struct mesh_area* mesh, struct mesh_state* m)
2268e97c6e54Ssthen {
2269e97c6e54Ssthen 	struct tcp_req_open_item* item;
2270e97c6e54Ssthen 	log_assert(req && mesh && m);
2271e97c6e54Ssthen 	item = (struct tcp_req_open_item*)malloc(sizeof(*item));
2272e97c6e54Ssthen 	if(!item) return 0;
2273e97c6e54Ssthen 	item->next = req->open_req_list;
2274e97c6e54Ssthen 	item->mesh = mesh;
2275e97c6e54Ssthen 	item->mesh_state = m;
2276e97c6e54Ssthen 	req->open_req_list = item;
2277e97c6e54Ssthen 	req->num_open_req++;
2278e97c6e54Ssthen 	return 1;
2279e97c6e54Ssthen }
2280e97c6e54Ssthen 
2281e97c6e54Ssthen /** Add a result to the result list.  At the end. */
2282e97c6e54Ssthen static int
tcp_req_info_add_result(struct tcp_req_info * req,uint8_t * buf,size_t len)2283e97c6e54Ssthen tcp_req_info_add_result(struct tcp_req_info* req, uint8_t* buf, size_t len)
2284e97c6e54Ssthen {
2285e97c6e54Ssthen 	struct tcp_req_done_item* last = NULL;
2286e97c6e54Ssthen 	struct tcp_req_done_item* item;
2287e97c6e54Ssthen 	size_t space;
2288e97c6e54Ssthen 
2289e97c6e54Ssthen 	/* see if we have space */
2290e97c6e54Ssthen 	space = sizeof(struct tcp_req_done_item) + len;
2291e97c6e54Ssthen 	lock_basic_lock(&stream_wait_count_lock);
2292e97c6e54Ssthen 	if(stream_wait_count + space > stream_wait_max) {
2293e97c6e54Ssthen 		lock_basic_unlock(&stream_wait_count_lock);
2294e97c6e54Ssthen 		verbose(VERB_ALGO, "drop stream reply, no space left, in stream-wait-size");
2295e97c6e54Ssthen 		return 0;
2296e97c6e54Ssthen 	}
2297e97c6e54Ssthen 	stream_wait_count += space;
2298e97c6e54Ssthen 	lock_basic_unlock(&stream_wait_count_lock);
2299e97c6e54Ssthen 
2300e97c6e54Ssthen 	/* find last element */
2301e97c6e54Ssthen 	last = req->done_req_list;
2302e97c6e54Ssthen 	while(last && last->next)
2303e97c6e54Ssthen 		last = last->next;
2304e97c6e54Ssthen 
2305e97c6e54Ssthen 	/* create new element */
2306e97c6e54Ssthen 	item = (struct tcp_req_done_item*)malloc(sizeof(*item));
2307e97c6e54Ssthen 	if(!item) {
2308e97c6e54Ssthen 		log_err("malloc failure, for stream result list");
2309e97c6e54Ssthen 		return 0;
2310e97c6e54Ssthen 	}
2311e97c6e54Ssthen 	item->next = NULL;
2312e97c6e54Ssthen 	item->len = len;
2313e97c6e54Ssthen 	item->buf = memdup(buf, len);
2314e97c6e54Ssthen 	if(!item->buf) {
2315e97c6e54Ssthen 		free(item);
2316e97c6e54Ssthen 		log_err("malloc failure, adding reply to stream result list");
2317e97c6e54Ssthen 		return 0;
2318e97c6e54Ssthen 	}
2319e97c6e54Ssthen 
2320e97c6e54Ssthen 	/* link in */
2321e97c6e54Ssthen 	if(last) last->next = item;
2322e97c6e54Ssthen 	else req->done_req_list = item;
2323e97c6e54Ssthen 	req->num_done_req++;
2324e97c6e54Ssthen 	return 1;
2325e97c6e54Ssthen }
2326e97c6e54Ssthen 
2327e97c6e54Ssthen void
tcp_req_info_send_reply(struct tcp_req_info * req)2328e97c6e54Ssthen tcp_req_info_send_reply(struct tcp_req_info* req)
2329e97c6e54Ssthen {
2330e97c6e54Ssthen 	if(req->in_worker_handle) {
23319b465e50Sflorian 		/* reply from mesh is in the spool_buffer */
23329b465e50Sflorian 		/* copy now, so that the spool buffer is free for other tasks
23339b465e50Sflorian 		 * before the callback is done */
23349b465e50Sflorian 		sldns_buffer_clear(req->cp->buffer);
23359b465e50Sflorian 		sldns_buffer_write(req->cp->buffer,
23369b465e50Sflorian 			sldns_buffer_begin(req->spool_buffer),
23379b465e50Sflorian 			sldns_buffer_limit(req->spool_buffer));
23389b465e50Sflorian 		sldns_buffer_flip(req->cp->buffer);
2339e97c6e54Ssthen 		req->is_reply = 1;
2340e97c6e54Ssthen 		return;
2341e97c6e54Ssthen 	}
2342e97c6e54Ssthen 	/* now that the query has been handled, that mesh_reply entry
2343e97c6e54Ssthen 	 * should be removed, from the tcp_req_info list,
2344e97c6e54Ssthen 	 * the mesh state cleanup removes then with region_cleanup and
2345e97c6e54Ssthen 	 * replies_sent true. */
2346e97c6e54Ssthen 	/* see if we can send it straight away (we are not doing
2347e97c6e54Ssthen 	 * anything else).  If so, copy to buffer and start */
2348e97c6e54Ssthen 	if(req->cp->tcp_is_reading && req->cp->tcp_byte_count == 0) {
2349e97c6e54Ssthen 		/* buffer is free, and was ready to read new query into,
2350e97c6e54Ssthen 		 * but we are now going to use it to send this answer */
2351e97c6e54Ssthen 		tcp_req_info_start_write_buf(req,
2352e97c6e54Ssthen 			sldns_buffer_begin(req->spool_buffer),
2353e97c6e54Ssthen 			sldns_buffer_limit(req->spool_buffer));
2354e97c6e54Ssthen 		/* switch to listen to write events */
2355e97c6e54Ssthen 		comm_point_stop_listening(req->cp);
2356e97c6e54Ssthen 		comm_point_start_listening(req->cp, -1,
2357a8eaceedSflorian 			adjusted_tcp_timeout(req->cp));
2358e97c6e54Ssthen 		return;
2359e97c6e54Ssthen 	}
2360e97c6e54Ssthen 	/* queue up the answer behind the others already pending */
2361e97c6e54Ssthen 	if(!tcp_req_info_add_result(req, sldns_buffer_begin(req->spool_buffer),
2362e97c6e54Ssthen 		sldns_buffer_limit(req->spool_buffer))) {
2363e97c6e54Ssthen 		/* drop the connection, we are out of resources */
2364e97c6e54Ssthen 		comm_point_drop_reply(&req->cp->repinfo);
2365e97c6e54Ssthen 	}
2366e97c6e54Ssthen }
2367e97c6e54Ssthen 
tcp_req_info_get_stream_buffer_size(void)2368e97c6e54Ssthen size_t tcp_req_info_get_stream_buffer_size(void)
2369e97c6e54Ssthen {
2370e97c6e54Ssthen 	size_t s;
2371e97c6e54Ssthen 	if(!stream_wait_lock_inited)
2372e97c6e54Ssthen 		return stream_wait_count;
2373e97c6e54Ssthen 	lock_basic_lock(&stream_wait_count_lock);
2374e97c6e54Ssthen 	s = stream_wait_count;
2375e97c6e54Ssthen 	lock_basic_unlock(&stream_wait_count_lock);
2376e97c6e54Ssthen 	return s;
2377e97c6e54Ssthen }
2378f4f0f0ceSflorian 
http2_get_query_buffer_size(void)2379f4f0f0ceSflorian size_t http2_get_query_buffer_size(void)
2380f4f0f0ceSflorian {
2381f4f0f0ceSflorian 	size_t s;
2382f4f0f0ceSflorian 	if(!http2_query_buffer_lock_inited)
2383f4f0f0ceSflorian 		return http2_query_buffer_count;
2384f4f0f0ceSflorian 	lock_basic_lock(&http2_query_buffer_count_lock);
2385f4f0f0ceSflorian 	s = http2_query_buffer_count;
2386f4f0f0ceSflorian 	lock_basic_unlock(&http2_query_buffer_count_lock);
2387f4f0f0ceSflorian 	return s;
2388f4f0f0ceSflorian }
2389f4f0f0ceSflorian 
http2_get_response_buffer_size(void)2390f4f0f0ceSflorian size_t http2_get_response_buffer_size(void)
2391f4f0f0ceSflorian {
2392f4f0f0ceSflorian 	size_t s;
2393f4f0f0ceSflorian 	if(!http2_response_buffer_lock_inited)
2394f4f0f0ceSflorian 		return http2_response_buffer_count;
2395f4f0f0ceSflorian 	lock_basic_lock(&http2_response_buffer_count_lock);
2396f4f0f0ceSflorian 	s = http2_response_buffer_count;
2397f4f0f0ceSflorian 	lock_basic_unlock(&http2_response_buffer_count_lock);
2398f4f0f0ceSflorian 	return s;
2399f4f0f0ceSflorian }
2400f4f0f0ceSflorian 
2401f4f0f0ceSflorian #ifdef HAVE_NGHTTP2
2402f4f0f0ceSflorian /** nghttp2 callback. Used to copy response from rbuffer to nghttp2 session */
http2_submit_response_read_callback(nghttp2_session * ATTR_UNUSED (session),int32_t stream_id,uint8_t * buf,size_t length,uint32_t * data_flags,nghttp2_data_source * source,void * ATTR_UNUSED (cb_arg))2403f4f0f0ceSflorian static ssize_t http2_submit_response_read_callback(
2404f4f0f0ceSflorian 	nghttp2_session* ATTR_UNUSED(session),
2405f4f0f0ceSflorian 	int32_t stream_id, uint8_t* buf, size_t length, uint32_t* data_flags,
2406f4f0f0ceSflorian 	nghttp2_data_source* source, void* ATTR_UNUSED(cb_arg))
2407f4f0f0ceSflorian {
2408f4f0f0ceSflorian 	struct http2_stream* h2_stream;
2409f4f0f0ceSflorian 	struct http2_session* h2_session = source->ptr;
2410f4f0f0ceSflorian 	size_t copylen = length;
2411f4f0f0ceSflorian 	if(!(h2_stream = nghttp2_session_get_stream_user_data(
2412f4f0f0ceSflorian 		h2_session->session, stream_id))) {
2413f4f0f0ceSflorian 		verbose(VERB_QUERY, "http2: cannot get stream data, closing "
2414f4f0f0ceSflorian 			"stream");
2415f4f0f0ceSflorian 		return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
2416f4f0f0ceSflorian 	}
2417f4f0f0ceSflorian 	if(!h2_stream->rbuffer ||
2418f4f0f0ceSflorian 		sldns_buffer_remaining(h2_stream->rbuffer) == 0) {
2419f4f0f0ceSflorian 		verbose(VERB_QUERY, "http2: cannot submit buffer. No data "
2420f4f0f0ceSflorian 			"available in rbuffer");
2421f4f0f0ceSflorian 		/* rbuffer will be free'd in frame close cb */
2422f4f0f0ceSflorian 		return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
2423f4f0f0ceSflorian 	}
2424f4f0f0ceSflorian 
2425f4f0f0ceSflorian 	if(copylen > sldns_buffer_remaining(h2_stream->rbuffer))
2426f4f0f0ceSflorian 		copylen = sldns_buffer_remaining(h2_stream->rbuffer);
2427f4f0f0ceSflorian 	if(copylen > SSIZE_MAX)
2428f4f0f0ceSflorian 		copylen = SSIZE_MAX; /* will probably never happen */
2429f4f0f0ceSflorian 
2430f4f0f0ceSflorian 	memcpy(buf, sldns_buffer_current(h2_stream->rbuffer), copylen);
2431f4f0f0ceSflorian 	sldns_buffer_skip(h2_stream->rbuffer, copylen);
2432f4f0f0ceSflorian 
2433f4f0f0ceSflorian 	if(sldns_buffer_remaining(h2_stream->rbuffer) == 0) {
2434f4f0f0ceSflorian 		*data_flags |= NGHTTP2_DATA_FLAG_EOF;
2435f4f0f0ceSflorian 		lock_basic_lock(&http2_response_buffer_count_lock);
2436f4f0f0ceSflorian 		http2_response_buffer_count -=
2437f4f0f0ceSflorian 			sldns_buffer_capacity(h2_stream->rbuffer);
2438f4f0f0ceSflorian 		lock_basic_unlock(&http2_response_buffer_count_lock);
2439f4f0f0ceSflorian 		sldns_buffer_free(h2_stream->rbuffer);
2440f4f0f0ceSflorian 		h2_stream->rbuffer = NULL;
2441f4f0f0ceSflorian 	}
2442f4f0f0ceSflorian 
2443f4f0f0ceSflorian 	return copylen;
2444f4f0f0ceSflorian }
2445f4f0f0ceSflorian 
2446f4f0f0ceSflorian /**
2447f4f0f0ceSflorian  * Send RST_STREAM frame for stream.
2448f4f0f0ceSflorian  * @param h2_session: http2 session to submit frame to
2449f4f0f0ceSflorian  * @param h2_stream: http2 stream containing frame ID to use in RST_STREAM
2450f4f0f0ceSflorian  * @return 0 on error, 1 otherwise
2451f4f0f0ceSflorian  */
http2_submit_rst_stream(struct http2_session * h2_session,struct http2_stream * h2_stream)2452f4f0f0ceSflorian static int http2_submit_rst_stream(struct http2_session* h2_session,
2453f4f0f0ceSflorian 		struct http2_stream* h2_stream)
2454f4f0f0ceSflorian {
2455f4f0f0ceSflorian 	int ret = nghttp2_submit_rst_stream(h2_session->session,
2456f4f0f0ceSflorian 		NGHTTP2_FLAG_NONE, h2_stream->stream_id,
2457f4f0f0ceSflorian 		NGHTTP2_INTERNAL_ERROR);
2458f4f0f0ceSflorian 	if(ret) {
2459f4f0f0ceSflorian 		verbose(VERB_QUERY, "http2: nghttp2_submit_rst_stream failed, "
2460f4f0f0ceSflorian 			"error: %s", nghttp2_strerror(ret));
2461f4f0f0ceSflorian 		return 0;
2462f4f0f0ceSflorian 	}
2463f4f0f0ceSflorian 	return 1;
2464f4f0f0ceSflorian }
2465f4f0f0ceSflorian 
2466f4f0f0ceSflorian /**
2467f4f0f0ceSflorian  * DNS response ready to be submitted to nghttp2, to be prepared for sending
2468f4f0f0ceSflorian  * out. Response is stored in c->buffer. Copy to rbuffer because the c->buffer
2469f4f0f0ceSflorian  * might be used before this will be sent out.
2470f4f0f0ceSflorian  * @param h2_session: http2 session, containing c->buffer which contains answer
2471f4f0f0ceSflorian  * @return 0 on error, 1 otherwise
2472f4f0f0ceSflorian  */
http2_submit_dns_response(struct http2_session * h2_session)2473f4f0f0ceSflorian int http2_submit_dns_response(struct http2_session* h2_session)
2474f4f0f0ceSflorian {
2475f4f0f0ceSflorian 	int ret;
2476f4f0f0ceSflorian 	nghttp2_data_provider data_prd;
2477f4f0f0ceSflorian 	char status[4];
2478853e076fSflorian 	nghttp2_nv headers[3];
2479f4f0f0ceSflorian 	struct http2_stream* h2_stream = h2_session->c->h2_stream;
2480f4f0f0ceSflorian 	size_t rlen;
2481853e076fSflorian 	char rlen_str[32];
2482f4f0f0ceSflorian 
2483f4f0f0ceSflorian 	if(h2_stream->rbuffer) {
2484f4f0f0ceSflorian 		log_err("http2 submit response error: rbuffer already "
2485f4f0f0ceSflorian 			"exists");
2486f4f0f0ceSflorian 		return 0;
2487f4f0f0ceSflorian 	}
2488f4f0f0ceSflorian 	if(sldns_buffer_remaining(h2_session->c->buffer) == 0) {
2489f4f0f0ceSflorian 		log_err("http2 submit response error: c->buffer not complete");
2490f4f0f0ceSflorian 		return 0;
2491f4f0f0ceSflorian 	}
2492f4f0f0ceSflorian 
2493f4f0f0ceSflorian 	if(snprintf(status, 4, "%d", h2_stream->status) != 3) {
2494f4f0f0ceSflorian 		verbose(VERB_QUERY, "http2: submit response error: "
2495f4f0f0ceSflorian 			"invalid status");
2496f4f0f0ceSflorian 		return 0;
2497f4f0f0ceSflorian 	}
2498f4f0f0ceSflorian 
2499f4f0f0ceSflorian 	rlen = sldns_buffer_remaining(h2_session->c->buffer);
2500853e076fSflorian 	snprintf(rlen_str, sizeof(rlen_str), "%u", (unsigned)rlen);
2501853e076fSflorian 
2502f4f0f0ceSflorian 	lock_basic_lock(&http2_response_buffer_count_lock);
2503f4f0f0ceSflorian 	if(http2_response_buffer_count + rlen > http2_response_buffer_max) {
2504f4f0f0ceSflorian 		lock_basic_unlock(&http2_response_buffer_count_lock);
2505f4f0f0ceSflorian 		verbose(VERB_ALGO, "reset HTTP2 stream, no space left, "
2506f4f0f0ceSflorian 			"in https-response-buffer-size");
2507f4f0f0ceSflorian 		return http2_submit_rst_stream(h2_session, h2_stream);
2508f4f0f0ceSflorian 	}
2509f4f0f0ceSflorian 	http2_response_buffer_count += rlen;
2510f4f0f0ceSflorian 	lock_basic_unlock(&http2_response_buffer_count_lock);
2511f4f0f0ceSflorian 
2512f4f0f0ceSflorian 	if(!(h2_stream->rbuffer = sldns_buffer_new(rlen))) {
2513f4f0f0ceSflorian 		lock_basic_lock(&http2_response_buffer_count_lock);
2514f4f0f0ceSflorian 		http2_response_buffer_count -= rlen;
2515f4f0f0ceSflorian 		lock_basic_unlock(&http2_response_buffer_count_lock);
2516f4f0f0ceSflorian 		log_err("http2 submit response error: malloc failure");
2517f4f0f0ceSflorian 		return 0;
2518f4f0f0ceSflorian 	}
2519f4f0f0ceSflorian 
2520f4f0f0ceSflorian 	headers[0].name = (uint8_t*)":status";
2521f4f0f0ceSflorian 	headers[0].namelen = 7;
2522f4f0f0ceSflorian 	headers[0].value = (uint8_t*)status;
2523f4f0f0ceSflorian 	headers[0].valuelen = 3;
2524f4f0f0ceSflorian 	headers[0].flags = NGHTTP2_NV_FLAG_NONE;
2525f4f0f0ceSflorian 
2526f4f0f0ceSflorian 	headers[1].name = (uint8_t*)"content-type";
2527f4f0f0ceSflorian 	headers[1].namelen = 12;
2528f4f0f0ceSflorian 	headers[1].value = (uint8_t*)"application/dns-message";
2529f4f0f0ceSflorian 	headers[1].valuelen = 23;
2530f4f0f0ceSflorian 	headers[1].flags = NGHTTP2_NV_FLAG_NONE;
2531f4f0f0ceSflorian 
2532f4f0f0ceSflorian 	headers[2].name = (uint8_t*)"content-length";
2533f4f0f0ceSflorian 	headers[2].namelen = 14;
2534853e076fSflorian 	headers[2].value = (uint8_t*)rlen_str;
2535853e076fSflorian 	headers[2].valuelen = strlen(rlen_str);
2536f4f0f0ceSflorian 	headers[2].flags = NGHTTP2_NV_FLAG_NONE;
2537f4f0f0ceSflorian 
2538f4f0f0ceSflorian 	sldns_buffer_write(h2_stream->rbuffer,
2539f4f0f0ceSflorian 		sldns_buffer_current(h2_session->c->buffer),
2540f4f0f0ceSflorian 		sldns_buffer_remaining(h2_session->c->buffer));
2541f4f0f0ceSflorian 	sldns_buffer_flip(h2_stream->rbuffer);
2542f4f0f0ceSflorian 
2543f4f0f0ceSflorian 	data_prd.source.ptr = h2_session;
2544f4f0f0ceSflorian 	data_prd.read_callback = http2_submit_response_read_callback;
2545f4f0f0ceSflorian 	ret = nghttp2_submit_response(h2_session->session, h2_stream->stream_id,
2546853e076fSflorian 		headers, 3, &data_prd);
2547f4f0f0ceSflorian 	if(ret) {
2548f4f0f0ceSflorian 		verbose(VERB_QUERY, "http2: set_stream_user_data failed, "
2549f4f0f0ceSflorian 			"error: %s", nghttp2_strerror(ret));
2550f4f0f0ceSflorian 		return 0;
2551f4f0f0ceSflorian 	}
2552f4f0f0ceSflorian 	return 1;
2553f4f0f0ceSflorian }
2554f4f0f0ceSflorian #else
http2_submit_dns_response(void * ATTR_UNUSED (v))2555f4f0f0ceSflorian int http2_submit_dns_response(void* ATTR_UNUSED(v))
2556f4f0f0ceSflorian {
2557f4f0f0ceSflorian 	return 0;
2558f4f0f0ceSflorian }
2559f4f0f0ceSflorian #endif
2560f4f0f0ceSflorian 
2561f4f0f0ceSflorian #ifdef HAVE_NGHTTP2
2562f4f0f0ceSflorian /** HTTP status to descriptive string */
http_status_to_str(enum http_status s)2563f4f0f0ceSflorian static char* http_status_to_str(enum http_status s)
2564f4f0f0ceSflorian {
2565f4f0f0ceSflorian 	switch(s) {
2566f4f0f0ceSflorian 		case HTTP_STATUS_OK:
2567f4f0f0ceSflorian 			return "OK";
2568f4f0f0ceSflorian 		case HTTP_STATUS_BAD_REQUEST:
2569f4f0f0ceSflorian 			return "Bad Request";
2570f4f0f0ceSflorian 		case HTTP_STATUS_NOT_FOUND:
2571f4f0f0ceSflorian 			return "Not Found";
2572f4f0f0ceSflorian 		case HTTP_STATUS_PAYLOAD_TOO_LARGE:
2573f4f0f0ceSflorian 			return "Payload Too Large";
2574f4f0f0ceSflorian 		case HTTP_STATUS_URI_TOO_LONG:
2575f4f0f0ceSflorian 			return "URI Too Long";
2576f4f0f0ceSflorian 		case HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE:
2577f4f0f0ceSflorian 			return "Unsupported Media Type";
2578f4f0f0ceSflorian 		case HTTP_STATUS_NOT_IMPLEMENTED:
2579f4f0f0ceSflorian 			return "Not Implemented";
2580f4f0f0ceSflorian 	}
2581f4f0f0ceSflorian 	return "Status Unknown";
2582f4f0f0ceSflorian }
2583f4f0f0ceSflorian 
2584f4f0f0ceSflorian /** nghttp2 callback. Used to copy error message to nghttp2 session */
http2_submit_error_read_callback(nghttp2_session * ATTR_UNUSED (session),int32_t stream_id,uint8_t * buf,size_t length,uint32_t * data_flags,nghttp2_data_source * source,void * ATTR_UNUSED (cb_arg))2585f4f0f0ceSflorian static ssize_t http2_submit_error_read_callback(
2586f4f0f0ceSflorian 	nghttp2_session* ATTR_UNUSED(session),
2587f4f0f0ceSflorian 	int32_t stream_id, uint8_t* buf, size_t length, uint32_t* data_flags,
2588f4f0f0ceSflorian 	nghttp2_data_source* source, void* ATTR_UNUSED(cb_arg))
2589f4f0f0ceSflorian {
2590f4f0f0ceSflorian 	struct http2_stream* h2_stream;
2591f4f0f0ceSflorian 	struct http2_session* h2_session = source->ptr;
2592f4f0f0ceSflorian 	char* msg;
2593f4f0f0ceSflorian 	if(!(h2_stream = nghttp2_session_get_stream_user_data(
2594f4f0f0ceSflorian 		h2_session->session, stream_id))) {
2595f4f0f0ceSflorian 		verbose(VERB_QUERY, "http2: cannot get stream data, closing "
2596f4f0f0ceSflorian 			"stream");
2597f4f0f0ceSflorian 		return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
2598f4f0f0ceSflorian 	}
2599f4f0f0ceSflorian 	*data_flags |= NGHTTP2_DATA_FLAG_EOF;
2600f4f0f0ceSflorian 	msg = http_status_to_str(h2_stream->status);
2601f4f0f0ceSflorian 	if(length < strlen(msg))
2602f4f0f0ceSflorian 		return 0; /* not worth trying over multiple frames */
2603f4f0f0ceSflorian 	memcpy(buf, msg, strlen(msg));
2604f4f0f0ceSflorian 	return strlen(msg);
2605f4f0f0ceSflorian 
2606f4f0f0ceSflorian }
2607f4f0f0ceSflorian 
2608f4f0f0ceSflorian /**
2609f4f0f0ceSflorian  * HTTP error response ready to be submitted to nghttp2, to be prepared for
2610f4f0f0ceSflorian  * sending out. Message body will contain descriptive string for HTTP status.
2611f4f0f0ceSflorian  * @param h2_session: http2 session to submit to
2612f4f0f0ceSflorian  * @param h2_stream: http2 stream containing HTTP status to use for error
2613f4f0f0ceSflorian  * @return 0 on error, 1 otherwise
2614f4f0f0ceSflorian  */
http2_submit_error(struct http2_session * h2_session,struct http2_stream * h2_stream)2615f4f0f0ceSflorian static int http2_submit_error(struct http2_session* h2_session,
2616f4f0f0ceSflorian 	struct http2_stream* h2_stream)
2617f4f0f0ceSflorian {
2618f4f0f0ceSflorian 	int ret;
2619f4f0f0ceSflorian 	char status[4];
2620f4f0f0ceSflorian 	nghttp2_data_provider data_prd;
2621f4f0f0ceSflorian 	nghttp2_nv headers[1]; /* will be copied by nghttp */
2622f4f0f0ceSflorian 	if(snprintf(status, 4, "%d", h2_stream->status) != 3) {
2623f4f0f0ceSflorian 		verbose(VERB_QUERY, "http2: submit error failed, "
2624f4f0f0ceSflorian 			"invalid status");
2625f4f0f0ceSflorian 		return 0;
2626f4f0f0ceSflorian 	}
2627f4f0f0ceSflorian 	headers[0].name = (uint8_t*)":status";
2628f4f0f0ceSflorian 	headers[0].namelen = 7;
2629f4f0f0ceSflorian 	headers[0].value = (uint8_t*)status;
2630f4f0f0ceSflorian 	headers[0].valuelen = 3;
2631f4f0f0ceSflorian 	headers[0].flags = NGHTTP2_NV_FLAG_NONE;
2632f4f0f0ceSflorian 
2633f4f0f0ceSflorian 	data_prd.source.ptr = h2_session;
2634f4f0f0ceSflorian 	data_prd.read_callback = http2_submit_error_read_callback;
2635f4f0f0ceSflorian 
2636f4f0f0ceSflorian 	ret = nghttp2_submit_response(h2_session->session, h2_stream->stream_id,
2637f4f0f0ceSflorian 		headers, 1, &data_prd);
2638f4f0f0ceSflorian 	if(ret) {
2639f4f0f0ceSflorian 		verbose(VERB_QUERY, "http2: submit error failed, "
2640f4f0f0ceSflorian 			"error: %s", nghttp2_strerror(ret));
2641f4f0f0ceSflorian 		return 0;
2642f4f0f0ceSflorian 	}
2643f4f0f0ceSflorian 	return 1;
2644f4f0f0ceSflorian }
2645f4f0f0ceSflorian 
2646f4f0f0ceSflorian /**
2647f4f0f0ceSflorian  * Start query handling. Query is stored in the stream, and will be free'd here.
2648f4f0f0ceSflorian  * @param h2_session: http2 session, containing comm point
2649f4f0f0ceSflorian  * @param h2_stream: stream containing buffered query
2650f4f0f0ceSflorian  * @return: -1 on error, 1 if answer is stored in c->buffer, 0 if there is no
2651f4f0f0ceSflorian  * reply available (yet).
2652f4f0f0ceSflorian  */
http2_query_read_done(struct http2_session * h2_session,struct http2_stream * h2_stream)2653f4f0f0ceSflorian static int http2_query_read_done(struct http2_session* h2_session,
2654f4f0f0ceSflorian 	struct http2_stream* h2_stream)
2655f4f0f0ceSflorian {
2656f4f0f0ceSflorian 	log_assert(h2_stream->qbuffer);
2657f4f0f0ceSflorian 
2658f4f0f0ceSflorian 	if(h2_session->c->h2_stream) {
2659f4f0f0ceSflorian 		verbose(VERB_ALGO, "http2_query_read_done failure: shared "
2660f4f0f0ceSflorian 			"buffer already assigned to stream");
2661f4f0f0ceSflorian 		return -1;
2662f4f0f0ceSflorian 	}
2663411c5950Sflorian 
2664411c5950Sflorian     /* the c->buffer might be used by mesh_send_reply and no be cleard
2665411c5950Sflorian 	 * need to be cleared before use */
2666411c5950Sflorian 	sldns_buffer_clear(h2_session->c->buffer);
2667f4f0f0ceSflorian 	if(sldns_buffer_remaining(h2_session->c->buffer) <
2668f4f0f0ceSflorian 		sldns_buffer_remaining(h2_stream->qbuffer)) {
2669f4f0f0ceSflorian 		/* qbuffer will be free'd in frame close cb */
2670f4f0f0ceSflorian 		sldns_buffer_clear(h2_session->c->buffer);
2671f4f0f0ceSflorian 		verbose(VERB_ALGO, "http2_query_read_done failure: can't fit "
2672f4f0f0ceSflorian 			"qbuffer in c->buffer");
2673f4f0f0ceSflorian 		return -1;
2674f4f0f0ceSflorian 	}
2675f4f0f0ceSflorian 
2676f4f0f0ceSflorian 	sldns_buffer_write(h2_session->c->buffer,
2677f4f0f0ceSflorian 		sldns_buffer_current(h2_stream->qbuffer),
2678f4f0f0ceSflorian 		sldns_buffer_remaining(h2_stream->qbuffer));
2679f4f0f0ceSflorian 
2680f4f0f0ceSflorian 	lock_basic_lock(&http2_query_buffer_count_lock);
2681f4f0f0ceSflorian 	http2_query_buffer_count -= sldns_buffer_capacity(h2_stream->qbuffer);
2682f4f0f0ceSflorian 	lock_basic_unlock(&http2_query_buffer_count_lock);
2683f4f0f0ceSflorian 	sldns_buffer_free(h2_stream->qbuffer);
2684f4f0f0ceSflorian 	h2_stream->qbuffer = NULL;
2685f4f0f0ceSflorian 
2686f4f0f0ceSflorian 	sldns_buffer_flip(h2_session->c->buffer);
2687f4f0f0ceSflorian 	h2_session->c->h2_stream = h2_stream;
2688f4f0f0ceSflorian 	fptr_ok(fptr_whitelist_comm_point(h2_session->c->callback));
2689f4f0f0ceSflorian 	if((*h2_session->c->callback)(h2_session->c, h2_session->c->cb_arg,
2690f4f0f0ceSflorian 		NETEVENT_NOERROR, &h2_session->c->repinfo)) {
2691f4f0f0ceSflorian 		return 1; /* answer in c->buffer */
2692f4f0f0ceSflorian 	}
2693f4f0f0ceSflorian 	sldns_buffer_clear(h2_session->c->buffer);
2694f4f0f0ceSflorian 	h2_session->c->h2_stream = NULL;
2695f4f0f0ceSflorian 	return 0; /* mesh state added, or dropped */
2696f4f0f0ceSflorian }
2697f4f0f0ceSflorian 
2698f4f0f0ceSflorian /** nghttp2 callback. Used to check if the received frame indicates the end of a
2699f4f0f0ceSflorian  * stream. Gather collected request data and start query handling. */
http2_req_frame_recv_cb(nghttp2_session * session,const nghttp2_frame * frame,void * cb_arg)2700f4f0f0ceSflorian static int http2_req_frame_recv_cb(nghttp2_session* session,
2701f4f0f0ceSflorian 	const nghttp2_frame* frame, void* cb_arg)
2702f4f0f0ceSflorian {
2703f4f0f0ceSflorian 	struct http2_session* h2_session = (struct http2_session*)cb_arg;
2704f4f0f0ceSflorian 	struct http2_stream* h2_stream;
2705f4f0f0ceSflorian 	int query_read_done;
2706f4f0f0ceSflorian 
2707f4f0f0ceSflorian 	if((frame->hd.type != NGHTTP2_DATA &&
2708f4f0f0ceSflorian 		frame->hd.type != NGHTTP2_HEADERS) ||
2709f4f0f0ceSflorian 		!(frame->hd.flags & NGHTTP2_FLAG_END_STREAM)) {
2710f4f0f0ceSflorian 			return 0;
2711f4f0f0ceSflorian 	}
2712f4f0f0ceSflorian 
2713f4f0f0ceSflorian 	if(!(h2_stream = nghttp2_session_get_stream_user_data(
2714f4f0f0ceSflorian 		session, frame->hd.stream_id)))
2715f4f0f0ceSflorian 		return 0;
2716f4f0f0ceSflorian 
2717f4f0f0ceSflorian 	if(h2_stream->invalid_endpoint) {
2718f4f0f0ceSflorian 		h2_stream->status = HTTP_STATUS_NOT_FOUND;
2719f4f0f0ceSflorian 		goto submit_http_error;
2720f4f0f0ceSflorian 	}
2721f4f0f0ceSflorian 
2722f4f0f0ceSflorian 	if(h2_stream->invalid_content_type) {
2723f4f0f0ceSflorian 		h2_stream->status = HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE;
2724f4f0f0ceSflorian 		goto submit_http_error;
2725f4f0f0ceSflorian 	}
2726f4f0f0ceSflorian 
2727f4f0f0ceSflorian 	if(h2_stream->http_method != HTTP_METHOD_GET &&
2728f4f0f0ceSflorian 		h2_stream->http_method != HTTP_METHOD_POST) {
2729f4f0f0ceSflorian 		h2_stream->status = HTTP_STATUS_NOT_IMPLEMENTED;
2730f4f0f0ceSflorian 		goto submit_http_error;
2731f4f0f0ceSflorian 	}
2732f4f0f0ceSflorian 
2733f4f0f0ceSflorian 	if(h2_stream->query_too_large) {
2734f4f0f0ceSflorian 		if(h2_stream->http_method == HTTP_METHOD_POST)
2735f4f0f0ceSflorian 			h2_stream->status = HTTP_STATUS_PAYLOAD_TOO_LARGE;
2736f4f0f0ceSflorian 		else
2737f4f0f0ceSflorian 			h2_stream->status = HTTP_STATUS_URI_TOO_LONG;
2738f4f0f0ceSflorian 		goto submit_http_error;
2739f4f0f0ceSflorian 	}
2740f4f0f0ceSflorian 
2741f4f0f0ceSflorian 	if(!h2_stream->qbuffer) {
2742f4f0f0ceSflorian 		h2_stream->status = HTTP_STATUS_BAD_REQUEST;
2743f4f0f0ceSflorian 		goto submit_http_error;
2744f4f0f0ceSflorian 	}
2745f4f0f0ceSflorian 
2746f4f0f0ceSflorian 	if(h2_stream->status) {
2747f4f0f0ceSflorian submit_http_error:
2748f4f0f0ceSflorian 		verbose(VERB_QUERY, "http2 request invalid, returning :status="
2749f4f0f0ceSflorian 			"%d", h2_stream->status);
2750f4f0f0ceSflorian 		if(!http2_submit_error(h2_session, h2_stream)) {
2751f4f0f0ceSflorian 			return NGHTTP2_ERR_CALLBACK_FAILURE;
2752f4f0f0ceSflorian 		}
2753f4f0f0ceSflorian 		return 0;
2754f4f0f0ceSflorian 	}
2755f4f0f0ceSflorian 	h2_stream->status = HTTP_STATUS_OK;
2756f4f0f0ceSflorian 
2757f4f0f0ceSflorian 	sldns_buffer_flip(h2_stream->qbuffer);
2758f4f0f0ceSflorian 	h2_session->postpone_drop = 1;
2759f4f0f0ceSflorian 	query_read_done = http2_query_read_done(h2_session, h2_stream);
2760f4f0f0ceSflorian 	if(query_read_done < 0)
2761f4f0f0ceSflorian 		return NGHTTP2_ERR_CALLBACK_FAILURE;
2762f4f0f0ceSflorian 	else if(!query_read_done) {
2763f4f0f0ceSflorian 		if(h2_session->is_drop) {
2764f4f0f0ceSflorian 			/* connection needs to be closed. Return failure to make
2765f4f0f0ceSflorian 			 * sure no other action are taken anymore on comm point.
2766f4f0f0ceSflorian 			 * failure will result in reclaiming (and closing)
2767f4f0f0ceSflorian 			 * of comm point. */
2768f4f0f0ceSflorian 			verbose(VERB_QUERY, "http2 query dropped in worker cb");
2769f4f0f0ceSflorian 			h2_session->postpone_drop = 0;
2770f4f0f0ceSflorian 			return NGHTTP2_ERR_CALLBACK_FAILURE;
2771f4f0f0ceSflorian 		}
2772f4f0f0ceSflorian 		/* nothing to submit right now, query added to mesh. */
2773f4f0f0ceSflorian 		h2_session->postpone_drop = 0;
2774f4f0f0ceSflorian 		return 0;
2775f4f0f0ceSflorian 	}
2776f4f0f0ceSflorian 	if(!http2_submit_dns_response(h2_session)) {
2777f4f0f0ceSflorian 		sldns_buffer_clear(h2_session->c->buffer);
2778f4f0f0ceSflorian 		h2_session->c->h2_stream = NULL;
2779f4f0f0ceSflorian 		return NGHTTP2_ERR_CALLBACK_FAILURE;
2780f4f0f0ceSflorian 	}
2781f4f0f0ceSflorian 	verbose(VERB_QUERY, "http2 query submitted to session");
2782f4f0f0ceSflorian 	sldns_buffer_clear(h2_session->c->buffer);
2783f4f0f0ceSflorian 	h2_session->c->h2_stream = NULL;
2784f4f0f0ceSflorian 	return 0;
2785f4f0f0ceSflorian }
2786f4f0f0ceSflorian 
2787f4f0f0ceSflorian /** nghttp2 callback. Used to detect start of new streams. */
http2_req_begin_headers_cb(nghttp2_session * session,const nghttp2_frame * frame,void * cb_arg)2788f4f0f0ceSflorian static int http2_req_begin_headers_cb(nghttp2_session* session,
2789f4f0f0ceSflorian 	const nghttp2_frame* frame, void* cb_arg)
2790f4f0f0ceSflorian {
2791f4f0f0ceSflorian 	struct http2_session* h2_session = (struct http2_session*)cb_arg;
2792f4f0f0ceSflorian 	struct http2_stream* h2_stream;
2793f4f0f0ceSflorian 	int ret;
2794f4f0f0ceSflorian 	if(frame->hd.type != NGHTTP2_HEADERS ||
2795f4f0f0ceSflorian 		frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
2796a1a7ba80Sflorian 		/* only interested in request headers */
2797f4f0f0ceSflorian 		return 0;
2798f4f0f0ceSflorian 	}
2799f4f0f0ceSflorian 	if(!(h2_stream = http2_stream_create(frame->hd.stream_id))) {
2800f4f0f0ceSflorian 		log_err("malloc failure while creating http2 stream");
2801f4f0f0ceSflorian 		return NGHTTP2_ERR_CALLBACK_FAILURE;
2802f4f0f0ceSflorian 	}
2803f4f0f0ceSflorian 	http2_session_add_stream(h2_session, h2_stream);
2804f4f0f0ceSflorian 	ret = nghttp2_session_set_stream_user_data(session,
2805f4f0f0ceSflorian 		frame->hd.stream_id, h2_stream);
2806f4f0f0ceSflorian 	if(ret) {
2807f4f0f0ceSflorian 		/* stream does not exist */
2808f4f0f0ceSflorian 		verbose(VERB_QUERY, "http2: set_stream_user_data failed, "
2809f4f0f0ceSflorian 			"error: %s", nghttp2_strerror(ret));
2810f4f0f0ceSflorian 		return NGHTTP2_ERR_CALLBACK_FAILURE;
2811f4f0f0ceSflorian 	}
2812f4f0f0ceSflorian 
2813f4f0f0ceSflorian 	return 0;
2814f4f0f0ceSflorian }
2815f4f0f0ceSflorian 
2816f4f0f0ceSflorian /**
2817f4f0f0ceSflorian  * base64url decode, store in qbuffer
2818f4f0f0ceSflorian  * @param h2_session: http2 session
2819f4f0f0ceSflorian  * @param h2_stream: http2 stream
2820f4f0f0ceSflorian  * @param start: start of the base64 string
2821f4f0f0ceSflorian  * @param length: length of the base64 string
2822f4f0f0ceSflorian  * @return: 0 on error, 1 otherwise. query will be stored in h2_stream->qbuffer,
2823f4f0f0ceSflorian  * buffer will be NULL is unparseble.
2824f4f0f0ceSflorian  */
http2_buffer_uri_query(struct http2_session * h2_session,struct http2_stream * h2_stream,const uint8_t * start,size_t length)2825f4f0f0ceSflorian static int http2_buffer_uri_query(struct http2_session* h2_session,
2826f4f0f0ceSflorian 	struct http2_stream* h2_stream, const uint8_t* start, size_t length)
2827f4f0f0ceSflorian {
2828f4f0f0ceSflorian 	size_t expectb64len;
2829f4f0f0ceSflorian 	int b64len;
2830f4f0f0ceSflorian 	if(h2_stream->http_method == HTTP_METHOD_POST)
2831f4f0f0ceSflorian 		return 1;
2832f4f0f0ceSflorian 	if(length == 0)
2833f4f0f0ceSflorian 		return 1;
2834f4f0f0ceSflorian 	if(h2_stream->qbuffer) {
2835f4f0f0ceSflorian 		verbose(VERB_ALGO, "http2_req_header fail, "
2836f4f0f0ceSflorian 			"qbuffer already set");
2837f4f0f0ceSflorian 		return 0;
2838f4f0f0ceSflorian 	}
2839f4f0f0ceSflorian 
2840f4f0f0ceSflorian 	/* calculate size, might be a bit bigger than the real
2841f4f0f0ceSflorian 	 * decoded buffer size */
2842f4f0f0ceSflorian 	expectb64len = sldns_b64_pton_calculate_size(length);
2843f4f0f0ceSflorian 	log_assert(expectb64len > 0);
2844f4f0f0ceSflorian 	if(expectb64len >
2845f4f0f0ceSflorian 		h2_session->c->http2_stream_max_qbuffer_size) {
2846f4f0f0ceSflorian 		h2_stream->query_too_large = 1;
2847f4f0f0ceSflorian 		return 1;
2848f4f0f0ceSflorian 	}
2849f4f0f0ceSflorian 
2850f4f0f0ceSflorian 	lock_basic_lock(&http2_query_buffer_count_lock);
2851f4f0f0ceSflorian 	if(http2_query_buffer_count + expectb64len > http2_query_buffer_max) {
2852f4f0f0ceSflorian 		lock_basic_unlock(&http2_query_buffer_count_lock);
2853f4f0f0ceSflorian 		verbose(VERB_ALGO, "reset HTTP2 stream, no space left, "
2854f4f0f0ceSflorian 			"in http2-query-buffer-size");
2855f4f0f0ceSflorian 		return http2_submit_rst_stream(h2_session, h2_stream);
2856f4f0f0ceSflorian 	}
2857f4f0f0ceSflorian 	http2_query_buffer_count += expectb64len;
2858f4f0f0ceSflorian 	lock_basic_unlock(&http2_query_buffer_count_lock);
2859f4f0f0ceSflorian 	if(!(h2_stream->qbuffer = sldns_buffer_new(expectb64len))) {
2860f4f0f0ceSflorian 		lock_basic_lock(&http2_query_buffer_count_lock);
2861f4f0f0ceSflorian 		http2_query_buffer_count -= expectb64len;
2862f4f0f0ceSflorian 		lock_basic_unlock(&http2_query_buffer_count_lock);
2863f4f0f0ceSflorian 		log_err("http2_req_header fail, qbuffer "
2864f4f0f0ceSflorian 			"malloc failure");
2865f4f0f0ceSflorian 		return 0;
2866f4f0f0ceSflorian 	}
2867f4f0f0ceSflorian 
2868411c5950Sflorian 	if(sldns_b64_contains_nonurl((char const*)start, length)) {
2869411c5950Sflorian 		char buf[65536+4];
2870411c5950Sflorian 		verbose(VERB_ALGO, "HTTP2 stream contains wrong b64 encoding");
2871411c5950Sflorian 		/* copy to the scratch buffer temporarily to terminate the
2872411c5950Sflorian 		 * string with a zero */
2873411c5950Sflorian 		if(length+1 > sizeof(buf)) {
2874411c5950Sflorian 			/* too long */
2875411c5950Sflorian 			lock_basic_lock(&http2_query_buffer_count_lock);
2876411c5950Sflorian 			http2_query_buffer_count -= expectb64len;
2877411c5950Sflorian 			lock_basic_unlock(&http2_query_buffer_count_lock);
2878411c5950Sflorian 			sldns_buffer_free(h2_stream->qbuffer);
2879411c5950Sflorian 			h2_stream->qbuffer = NULL;
2880411c5950Sflorian 			return 1;
2881411c5950Sflorian 		}
2882411c5950Sflorian 		memmove(buf, start, length);
2883411c5950Sflorian 		buf[length] = 0;
2884411c5950Sflorian 		if(!(b64len = sldns_b64_pton(buf, sldns_buffer_current(
2885411c5950Sflorian 			h2_stream->qbuffer), expectb64len)) || b64len < 0) {
2886411c5950Sflorian 			lock_basic_lock(&http2_query_buffer_count_lock);
2887411c5950Sflorian 			http2_query_buffer_count -= expectb64len;
2888411c5950Sflorian 			lock_basic_unlock(&http2_query_buffer_count_lock);
2889411c5950Sflorian 			sldns_buffer_free(h2_stream->qbuffer);
2890411c5950Sflorian 			h2_stream->qbuffer = NULL;
2891411c5950Sflorian 			return 1;
2892411c5950Sflorian 		}
2893411c5950Sflorian 	} else {
2894f4f0f0ceSflorian 		if(!(b64len = sldns_b64url_pton(
2895f4f0f0ceSflorian 			(char const *)start, length,
2896f4f0f0ceSflorian 			sldns_buffer_current(h2_stream->qbuffer),
2897f4f0f0ceSflorian 			expectb64len)) || b64len < 0) {
2898f4f0f0ceSflorian 			lock_basic_lock(&http2_query_buffer_count_lock);
2899f4f0f0ceSflorian 			http2_query_buffer_count -= expectb64len;
2900f4f0f0ceSflorian 			lock_basic_unlock(&http2_query_buffer_count_lock);
2901f4f0f0ceSflorian 			sldns_buffer_free(h2_stream->qbuffer);
2902f4f0f0ceSflorian 			h2_stream->qbuffer = NULL;
2903f4f0f0ceSflorian 			/* return without error, method can be an
2904f4f0f0ceSflorian 			 * unknown POST */
2905f4f0f0ceSflorian 			return 1;
2906f4f0f0ceSflorian 		}
2907411c5950Sflorian 	}
2908f4f0f0ceSflorian 	sldns_buffer_skip(h2_stream->qbuffer, (size_t)b64len);
2909f4f0f0ceSflorian 	return 1;
2910f4f0f0ceSflorian }
2911f4f0f0ceSflorian 
2912f4f0f0ceSflorian /** nghttp2 callback. Used to parse headers from HEADER frames. */
http2_req_header_cb(nghttp2_session * session,const nghttp2_frame * frame,const uint8_t * name,size_t namelen,const uint8_t * value,size_t valuelen,uint8_t ATTR_UNUSED (flags),void * cb_arg)2913f4f0f0ceSflorian static int http2_req_header_cb(nghttp2_session* session,
2914f4f0f0ceSflorian 	const nghttp2_frame* frame, const uint8_t* name, size_t namelen,
2915f4f0f0ceSflorian 	const uint8_t* value, size_t valuelen, uint8_t ATTR_UNUSED(flags),
2916f4f0f0ceSflorian 	void* cb_arg)
2917f4f0f0ceSflorian {
2918f4f0f0ceSflorian 	struct http2_stream* h2_stream = NULL;
2919f4f0f0ceSflorian 	struct http2_session* h2_session = (struct http2_session*)cb_arg;
2920f4f0f0ceSflorian 	/* nghttp2 deals with CONTINUATION frames and provides them as part of
2921f4f0f0ceSflorian 	 * the HEADER */
2922f4f0f0ceSflorian 	if(frame->hd.type != NGHTTP2_HEADERS ||
2923f4f0f0ceSflorian 		frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
2924a1a7ba80Sflorian 		/* only interested in request headers */
2925f4f0f0ceSflorian 		return 0;
2926f4f0f0ceSflorian 	}
2927f4f0f0ceSflorian 	if(!(h2_stream = nghttp2_session_get_stream_user_data(session,
2928f4f0f0ceSflorian 		frame->hd.stream_id)))
2929f4f0f0ceSflorian 		return 0;
2930f4f0f0ceSflorian 
2931f4f0f0ceSflorian 	/* earlier checks already indicate we can stop handling this query */
2932f4f0f0ceSflorian 	if(h2_stream->http_method == HTTP_METHOD_UNSUPPORTED ||
2933f4f0f0ceSflorian 		h2_stream->invalid_content_type ||
2934f4f0f0ceSflorian 		h2_stream->invalid_endpoint)
2935f4f0f0ceSflorian 		return 0;
2936f4f0f0ceSflorian 
2937f4f0f0ceSflorian 
2938f4f0f0ceSflorian 	/* nghttp2 performs some sanity checks in the headers, including:
2939f4f0f0ceSflorian 	 * name and value are guaranteed to be null terminated
2940f4f0f0ceSflorian 	 * name is guaranteed to be lowercase
2941f4f0f0ceSflorian 	 * content-length value is guaranteed to contain digits
2942f4f0f0ceSflorian 	 */
2943f4f0f0ceSflorian 
2944f4f0f0ceSflorian 	if(!h2_stream->http_method && namelen == 7 &&
2945f4f0f0ceSflorian 		memcmp(":method", name, namelen) == 0) {
2946f4f0f0ceSflorian 		/* Case insensitive check on :method value to be on the safe
2947f4f0f0ceSflorian 		 * side. I failed to find text about case sensitivity in specs.
2948f4f0f0ceSflorian 		 */
2949f4f0f0ceSflorian 		if(valuelen == 3 && strcasecmp("GET", (const char*)value) == 0)
2950f4f0f0ceSflorian 			h2_stream->http_method = HTTP_METHOD_GET;
2951f4f0f0ceSflorian 		else if(valuelen == 4 &&
2952f4f0f0ceSflorian 			strcasecmp("POST", (const char*)value) == 0) {
2953f4f0f0ceSflorian 			h2_stream->http_method = HTTP_METHOD_POST;
2954f4f0f0ceSflorian 			if(h2_stream->qbuffer) {
2955f4f0f0ceSflorian 				/* POST method uses query from DATA frames */
2956f4f0f0ceSflorian 				lock_basic_lock(&http2_query_buffer_count_lock);
2957f4f0f0ceSflorian 				http2_query_buffer_count -=
2958f4f0f0ceSflorian 					sldns_buffer_capacity(h2_stream->qbuffer);
2959f4f0f0ceSflorian 				lock_basic_unlock(&http2_query_buffer_count_lock);
2960f4f0f0ceSflorian 				sldns_buffer_free(h2_stream->qbuffer);
2961f4f0f0ceSflorian 				h2_stream->qbuffer = NULL;
2962f4f0f0ceSflorian 			}
2963f4f0f0ceSflorian 		} else
2964f4f0f0ceSflorian 			h2_stream->http_method = HTTP_METHOD_UNSUPPORTED;
2965f4f0f0ceSflorian 		return 0;
2966f4f0f0ceSflorian 	}
2967f4f0f0ceSflorian 	if(namelen == 5 && memcmp(":path", name, namelen) == 0) {
2968f4f0f0ceSflorian 		/* :path may contain DNS query, depending on method. Method might
2969f4f0f0ceSflorian 		 * not be known yet here, so check after finishing receiving
2970f4f0f0ceSflorian 		 * stream. */
2971f4f0f0ceSflorian #define	HTTP_QUERY_PARAM "?dns="
2972f4f0f0ceSflorian 		size_t el = strlen(h2_session->c->http_endpoint);
2973f4f0f0ceSflorian 		size_t qpl = strlen(HTTP_QUERY_PARAM);
2974f4f0f0ceSflorian 
2975f4f0f0ceSflorian 		if(valuelen < el || memcmp(h2_session->c->http_endpoint,
2976f4f0f0ceSflorian 			value, el) != 0) {
2977f4f0f0ceSflorian 			h2_stream->invalid_endpoint = 1;
2978f4f0f0ceSflorian 			return 0;
2979f4f0f0ceSflorian 		}
2980f4f0f0ceSflorian 		/* larger than endpoint only allowed if it is for the query
2981f4f0f0ceSflorian 		 * parameter */
2982f4f0f0ceSflorian 		if(valuelen <= el+qpl ||
2983f4f0f0ceSflorian 			memcmp(HTTP_QUERY_PARAM, value+el, qpl) != 0) {
2984f4f0f0ceSflorian 			if(valuelen != el)
2985f4f0f0ceSflorian 				h2_stream->invalid_endpoint = 1;
2986f4f0f0ceSflorian 			return 0;
2987f4f0f0ceSflorian 		}
2988f4f0f0ceSflorian 
2989f4f0f0ceSflorian 		if(!http2_buffer_uri_query(h2_session, h2_stream,
2990f4f0f0ceSflorian 			value+(el+qpl), valuelen-(el+qpl))) {
2991f4f0f0ceSflorian 			return NGHTTP2_ERR_CALLBACK_FAILURE;
2992f4f0f0ceSflorian 		}
2993f4f0f0ceSflorian 		return 0;
2994f4f0f0ceSflorian 	}
2995f4f0f0ceSflorian 	/* Content type is a SHOULD (rfc7231#section-3.1.1.5) when using POST,
2996f4f0f0ceSflorian 	 * and not needed when using GET. Don't enfore.
2997f4f0f0ceSflorian 	 * If set only allow lowercase "application/dns-message".
2998f4f0f0ceSflorian 	 *
2999f4f0f0ceSflorian 	 * Clients SHOULD (rfc8484#section-4.1) set an accept header, but MUST
3000f4f0f0ceSflorian 	 * be able to handle "application/dns-message". Since that is the only
3001f4f0f0ceSflorian 	 * content-type supported we can ignore the accept header.
3002f4f0f0ceSflorian 	 */
3003f4f0f0ceSflorian 	if((namelen == 12 && memcmp("content-type", name, namelen) == 0)) {
3004f4f0f0ceSflorian 		if(valuelen != 23 || memcmp("application/dns-message", value,
3005f4f0f0ceSflorian 			valuelen) != 0) {
3006f4f0f0ceSflorian 			h2_stream->invalid_content_type = 1;
3007f4f0f0ceSflorian 		}
3008f4f0f0ceSflorian 	}
3009f4f0f0ceSflorian 
3010f4f0f0ceSflorian 	/* Only interested in content-lentg for POST (on not yet known) method.
3011f4f0f0ceSflorian 	 */
3012f4f0f0ceSflorian 	if((!h2_stream->http_method ||
3013f4f0f0ceSflorian 		h2_stream->http_method == HTTP_METHOD_POST) &&
3014f4f0f0ceSflorian 		!h2_stream->content_length && namelen  == 14 &&
3015f4f0f0ceSflorian 		memcmp("content-length", name, namelen) == 0) {
3016f4f0f0ceSflorian 		if(valuelen > 5) {
3017f4f0f0ceSflorian 			h2_stream->query_too_large = 1;
3018f4f0f0ceSflorian 			return 0;
3019f4f0f0ceSflorian 		}
3020a1a7ba80Sflorian 		/* guaranteed to only contain digits and be null terminated */
3021f4f0f0ceSflorian 		h2_stream->content_length = atoi((const char*)value);
3022f4f0f0ceSflorian 		if(h2_stream->content_length >
3023f4f0f0ceSflorian 			h2_session->c->http2_stream_max_qbuffer_size) {
3024f4f0f0ceSflorian 			h2_stream->query_too_large = 1;
3025f4f0f0ceSflorian 			return 0;
3026f4f0f0ceSflorian 		}
3027f4f0f0ceSflorian 	}
3028f4f0f0ceSflorian 	return 0;
3029f4f0f0ceSflorian }
3030f4f0f0ceSflorian 
3031f4f0f0ceSflorian /** nghttp2 callback. Used to get data from DATA frames, which can contain
3032f4f0f0ceSflorian  * queries in POST requests. */
http2_req_data_chunk_recv_cb(nghttp2_session * ATTR_UNUSED (session),uint8_t ATTR_UNUSED (flags),int32_t stream_id,const uint8_t * data,size_t len,void * cb_arg)3033f4f0f0ceSflorian static int http2_req_data_chunk_recv_cb(nghttp2_session* ATTR_UNUSED(session),
3034f4f0f0ceSflorian 	uint8_t ATTR_UNUSED(flags), int32_t stream_id, const uint8_t* data,
3035f4f0f0ceSflorian 	size_t len, void* cb_arg)
3036f4f0f0ceSflorian {
3037f4f0f0ceSflorian 	struct http2_session* h2_session = (struct http2_session*)cb_arg;
3038f4f0f0ceSflorian 	struct http2_stream* h2_stream;
3039f4f0f0ceSflorian 	size_t qlen = 0;
3040f4f0f0ceSflorian 
3041f4f0f0ceSflorian 	if(!(h2_stream = nghttp2_session_get_stream_user_data(
3042f4f0f0ceSflorian 		h2_session->session, stream_id))) {
3043f4f0f0ceSflorian 		return 0;
3044f4f0f0ceSflorian 	}
3045f4f0f0ceSflorian 
3046f4f0f0ceSflorian 	if(h2_stream->query_too_large)
3047f4f0f0ceSflorian 		return 0;
3048f4f0f0ceSflorian 
3049f4f0f0ceSflorian 	if(!h2_stream->qbuffer) {
3050f4f0f0ceSflorian 		if(h2_stream->content_length) {
3051f4f0f0ceSflorian 			if(h2_stream->content_length < len)
3052f4f0f0ceSflorian 				/* getting more data in DATA frame than
3053f4f0f0ceSflorian 				 * advertised in content-length header. */
3054f4f0f0ceSflorian 				return NGHTTP2_ERR_CALLBACK_FAILURE;
3055f4f0f0ceSflorian 			qlen = h2_stream->content_length;
3056f4f0f0ceSflorian 		} else if(len <= h2_session->c->http2_stream_max_qbuffer_size) {
3057f4f0f0ceSflorian 			/* setting this to msg-buffer-size can result in a lot
3058f4f0f0ceSflorian 			 * of memory consuption. Most queries should fit in a
3059f4f0f0ceSflorian 			 * single DATA frame, and most POST queries will
3060a1a7ba80Sflorian 			 * contain content-length which does not impose this
3061f4f0f0ceSflorian 			 * limit. */
3062f4f0f0ceSflorian 			qlen = len;
3063f4f0f0ceSflorian 		}
3064f4f0f0ceSflorian 	}
3065f4f0f0ceSflorian 	if(!h2_stream->qbuffer && qlen) {
3066f4f0f0ceSflorian 		lock_basic_lock(&http2_query_buffer_count_lock);
3067f4f0f0ceSflorian 		if(http2_query_buffer_count + qlen > http2_query_buffer_max) {
3068f4f0f0ceSflorian 			lock_basic_unlock(&http2_query_buffer_count_lock);
3069f4f0f0ceSflorian 			verbose(VERB_ALGO, "reset HTTP2 stream, no space left, "
3070f4f0f0ceSflorian 				"in http2-query-buffer-size");
3071f4f0f0ceSflorian 			return http2_submit_rst_stream(h2_session, h2_stream);
3072f4f0f0ceSflorian 		}
3073f4f0f0ceSflorian 		http2_query_buffer_count += qlen;
3074f4f0f0ceSflorian 		lock_basic_unlock(&http2_query_buffer_count_lock);
3075f4f0f0ceSflorian 		if(!(h2_stream->qbuffer = sldns_buffer_new(qlen))) {
3076f4f0f0ceSflorian 			lock_basic_lock(&http2_query_buffer_count_lock);
3077f4f0f0ceSflorian 			http2_query_buffer_count -= qlen;
3078f4f0f0ceSflorian 			lock_basic_unlock(&http2_query_buffer_count_lock);
3079f4f0f0ceSflorian 		}
3080f4f0f0ceSflorian 	}
3081f4f0f0ceSflorian 
3082f4f0f0ceSflorian 	if(!h2_stream->qbuffer ||
3083f4f0f0ceSflorian 		sldns_buffer_remaining(h2_stream->qbuffer) < len) {
3084f4f0f0ceSflorian 		verbose(VERB_ALGO, "http2 data_chunck_recv failed. Not enough "
3085f4f0f0ceSflorian 			"buffer space for POST query. Can happen on multi "
3086f4f0f0ceSflorian 			"frame requests without content-length header");
3087f4f0f0ceSflorian 		h2_stream->query_too_large = 1;
3088f4f0f0ceSflorian 		return 0;
3089f4f0f0ceSflorian 	}
3090f4f0f0ceSflorian 
3091f4f0f0ceSflorian 	sldns_buffer_write(h2_stream->qbuffer, data, len);
3092f4f0f0ceSflorian 
3093f4f0f0ceSflorian 	return 0;
3094f4f0f0ceSflorian }
3095f4f0f0ceSflorian 
http2_req_stream_clear(struct http2_stream * h2_stream)3096f4f0f0ceSflorian void http2_req_stream_clear(struct http2_stream* h2_stream)
3097f4f0f0ceSflorian {
3098f4f0f0ceSflorian 	if(h2_stream->qbuffer) {
3099f4f0f0ceSflorian 		lock_basic_lock(&http2_query_buffer_count_lock);
3100f4f0f0ceSflorian 		http2_query_buffer_count -=
3101f4f0f0ceSflorian 			sldns_buffer_capacity(h2_stream->qbuffer);
3102f4f0f0ceSflorian 		lock_basic_unlock(&http2_query_buffer_count_lock);
3103f4f0f0ceSflorian 		sldns_buffer_free(h2_stream->qbuffer);
3104f4f0f0ceSflorian 		h2_stream->qbuffer = NULL;
3105f4f0f0ceSflorian 	}
3106f4f0f0ceSflorian 	if(h2_stream->rbuffer) {
3107f4f0f0ceSflorian 		lock_basic_lock(&http2_response_buffer_count_lock);
3108f4f0f0ceSflorian 		http2_response_buffer_count -=
3109f4f0f0ceSflorian 			sldns_buffer_capacity(h2_stream->rbuffer);
3110f4f0f0ceSflorian 		lock_basic_unlock(&http2_response_buffer_count_lock);
3111f4f0f0ceSflorian 		sldns_buffer_free(h2_stream->rbuffer);
3112f4f0f0ceSflorian 		h2_stream->rbuffer = NULL;
3113f4f0f0ceSflorian 	}
3114f4f0f0ceSflorian }
3115f4f0f0ceSflorian 
http2_req_callbacks_create(void)3116a8eaceedSflorian nghttp2_session_callbacks* http2_req_callbacks_create(void)
3117f4f0f0ceSflorian {
3118f4f0f0ceSflorian 	nghttp2_session_callbacks *callbacks;
3119f4f0f0ceSflorian 	if(nghttp2_session_callbacks_new(&callbacks) == NGHTTP2_ERR_NOMEM) {
3120f4f0f0ceSflorian 		log_err("failed to initialize nghttp2 callback");
3121f4f0f0ceSflorian 		return NULL;
3122f4f0f0ceSflorian 	}
3123f4f0f0ceSflorian 	/* reception of header block started, used to create h2_stream */
3124f4f0f0ceSflorian 	nghttp2_session_callbacks_set_on_begin_headers_callback(callbacks,
3125f4f0f0ceSflorian 		http2_req_begin_headers_cb);
3126f4f0f0ceSflorian 	/* complete frame received, used to get data from stream if frame
3127f4f0f0ceSflorian 	 * has end stream flag, and start processing query */
3128f4f0f0ceSflorian 	nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks,
3129f4f0f0ceSflorian 		http2_req_frame_recv_cb);
3130f4f0f0ceSflorian 	/* get request info from headers */
3131f4f0f0ceSflorian 	nghttp2_session_callbacks_set_on_header_callback(callbacks,
3132f4f0f0ceSflorian 		http2_req_header_cb);
3133f4f0f0ceSflorian 	/* get data from DATA frames, containing POST query */
3134f4f0f0ceSflorian 	nghttp2_session_callbacks_set_on_data_chunk_recv_callback(callbacks,
3135f4f0f0ceSflorian 		http2_req_data_chunk_recv_cb);
3136f4f0f0ceSflorian 
3137f4f0f0ceSflorian 	/* generic HTTP2 callbacks */
3138f4f0f0ceSflorian 	nghttp2_session_callbacks_set_recv_callback(callbacks, http2_recv_cb);
3139f4f0f0ceSflorian 	nghttp2_session_callbacks_set_send_callback(callbacks, http2_send_cb);
3140f4f0f0ceSflorian 	nghttp2_session_callbacks_set_on_stream_close_callback(callbacks,
3141f4f0f0ceSflorian 		http2_stream_close_cb);
3142f4f0f0ceSflorian 
3143f4f0f0ceSflorian 	return callbacks;
3144f4f0f0ceSflorian }
3145f4f0f0ceSflorian #endif /* HAVE_NGHTTP2 */
3146