xref: /freebsd/contrib/unbound/services/listen_dnsport.h (revision 46d2f61818f594174cafe31ee338c6e083fa1876)
1 /*
2  * services/listen_dnsport.h - listen on port 53 for incoming DNS queries.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file has functions to get queries from clients.
40  */
41 
42 #ifndef LISTEN_DNSPORT_H
43 #define LISTEN_DNSPORT_H
44 
45 #include "util/netevent.h"
46 #include "util/rbtree.h"
47 #include "util/locks.h"
48 #include "daemon/acl_list.h"
49 #ifdef HAVE_NGHTTP2_NGHTTP2_H
50 #include <nghttp2/nghttp2.h>
51 #endif
52 #ifdef HAVE_NGTCP2
53 #include <ngtcp2/ngtcp2.h>
54 #include <ngtcp2/ngtcp2_crypto.h>
55 #endif
56 struct listen_list;
57 struct config_file;
58 struct addrinfo;
59 struct sldns_buffer;
60 struct tcl_list;
61 
62 /**
63  * Listening for queries structure.
64  * Contains list of query-listen sockets.
65  */
66 struct listen_dnsport {
67 	/** Base for select calls */
68 	struct comm_base* base;
69 
70 	/** buffer shared by UDP connections, since there is only one
71 	    datagram at any time. */
72 	struct sldns_buffer* udp_buff;
73 #ifdef USE_DNSCRYPT
74 	struct sldns_buffer* dnscrypt_udp_buff;
75 #endif
76 	/** list of comm points used to get incoming events */
77 	struct listen_list* cps;
78 };
79 
80 /**
81  * Single linked list to store event points.
82  */
83 struct listen_list {
84 	/** next in list */
85 	struct listen_list* next;
86 	/** event info */
87 	struct comm_point* com;
88 };
89 
90 /**
91  * type of ports
92  */
93 enum listen_type {
94 	/** udp type */
95 	listen_type_udp,
96 	/** tcp type */
97 	listen_type_tcp,
98 	/** udp ipv6 (v4mapped) for use with ancillary data */
99 	listen_type_udpancil,
100 	/** ssl over tcp type */
101 	listen_type_ssl,
102 	/** udp type  + dnscrypt*/
103 	listen_type_udp_dnscrypt,
104 	/** tcp type + dnscrypt */
105 	listen_type_tcp_dnscrypt,
106 	/** udp ipv6 (v4mapped) for use with ancillary data + dnscrypt*/
107 	listen_type_udpancil_dnscrypt,
108 	/** HTTP(2) over TLS over TCP */
109 	listen_type_http,
110 	/** DNS over QUIC */
111 	listen_type_doq
112 };
113 
114 /*
115  * socket properties (just like NSD nsd_socket structure definition)
116  */
117 struct unbound_socket {
118 	/** the address of the socket */
119 	struct sockaddr* addr;
120 	/** length of the address */
121 	socklen_t addrlen;
122 	/** socket descriptor returned by socket() syscall */
123 	int s;
124 	/** address family (AF_INET/AF_INET6) */
125 	int fam;
126 	/** ACL on the socket (listening interface) */
127 	struct acl_addr* acl;
128 };
129 
130 /**
131  * Single linked list to store shared ports that have been
132  * opened for use by all threads.
133  */
134 struct listen_port {
135 	/** next in list */
136 	struct listen_port* next;
137 	/** file descriptor, open and ready for use */
138 	int fd;
139 	/** type of file descriptor, udp or tcp */
140 	enum listen_type ftype;
141 	/** if the port should support PROXYv2 */
142 	int pp2_enabled;
143 	/** fill in unbound_socket structure for every opened socket at
144 	 * Unbound startup */
145 	struct unbound_socket* socket;
146 };
147 
148 /**
149  * Create shared listening ports
150  * Getaddrinfo, create socket, bind and listen to zero or more
151  * interfaces for IP4 and/or IP6, for UDP and/or TCP.
152  * On the given port number. It creates the sockets.
153  * @param cfg: settings on what ports to open.
154  * @param ifs: interfaces to open, array of IP addresses, "ip[@port]".
155  * @param num_ifs: length of ifs.
156  * @param reuseport: set to true if you want reuseport, or NULL to not have it,
157  *   set to false on exit if reuseport failed to apply (because of no
158  *   kernel support).
159  * @return: linked list of ports or NULL on error.
160  */
161 struct listen_port* listening_ports_open(struct config_file* cfg,
162 	char** ifs, int num_ifs, int* reuseport);
163 
164 /**
165  * Close and delete the (list of) listening ports.
166  */
167 void listening_ports_free(struct listen_port* list);
168 
169 struct config_strlist;
170 /**
171  * Resolve interface names in config and store result IP addresses
172  * @param ifs: array of interfaces.  The list of interface names, if not NULL.
173  * @param num_ifs: length of ifs array.
174  * @param list: if not NULL, this is used as the list of interface names.
175  * @param resif: string array (malloced array of malloced strings) with
176  * 	result.  NULL if cfg has none.
177  * @param num_resif: length of resif.  Zero if cfg has zero num_ifs.
178  * @return 0 on failure.
179  */
180 int resolve_interface_names(char** ifs, int num_ifs,
181 	struct config_strlist* list, char*** resif, int* num_resif);
182 
183 /**
184  * Create commpoints with for this thread for the shared ports.
185  * @param base: the comm_base that provides event functionality.
186  *	for default all ifs.
187  * @param ports: the list of shared ports.
188  * @param bufsize: size of datagram buffer.
189  * @param tcp_accept_count: max number of simultaneous TCP connections
190  * 	from clients.
191  * @param tcp_idle_timeout: idle timeout for TCP connections in msec.
192  * @param harden_large_queries: whether query size should be limited.
193  * @param http_max_streams: maximum number of HTTP/2 streams per connection.
194  * @param http_endpoint: HTTP endpoint to service queries on
195  * @param http_notls: no TLS for http downstream
196  * @param tcp_conn_limit: TCP connection limit info.
197  * @param sslctx: nonNULL if ssl context.
198  * @param dtenv: nonNULL if dnstap enabled.
199  * @param doq_table: the doq connection table, with shared information.
200  * @param rnd: random state.
201  * @param ssl_service_key: the SSL service key file.
202  * @param ssl_service_pem: the SSL service pem file.
203  * @param cfg: config file struct.
204  * @param cb: callback function when a request arrives. It is passed
205  *	  the packet and user argument. Return true to send a reply.
206  * @param cb_arg: user data argument for callback function.
207  * @return: the malloced listening structure, ready for use. NULL on error.
208  */
209 struct listen_dnsport*
210 listen_create(struct comm_base* base, struct listen_port* ports,
211 	size_t bufsize, int tcp_accept_count, int tcp_idle_timeout,
212 	int harden_large_queries, uint32_t http_max_streams,
213 	char* http_endpoint, int http_notls, struct tcl_list* tcp_conn_limit,
214 	void* sslctx, struct dt_env* dtenv, struct doq_table* doq_table,
215 	struct ub_randstate* rnd, const char* ssl_service_key,
216 	const char* ssl_service_pem, struct config_file* cfg,
217 	comm_point_callback_type* cb, void *cb_arg);
218 
219 /**
220  * delete the listening structure
221  * @param listen: listening structure.
222  */
223 void listen_delete(struct listen_dnsport* listen);
224 
225 /** setup the locks for the listen ports */
226 void listen_setup_locks(void);
227 /** desetup the locks for the listen ports */
228 void listen_desetup_locks(void);
229 
230 /**
231  * delete listen_list of commpoints. Calls commpointdelete() on items.
232  * This may close the fds or not depending on flags.
233  * @param list: to delete.
234  */
235 void listen_list_delete(struct listen_list* list);
236 
237 /**
238  * get memory size used by the listening structs
239  * @param listen: listening structure.
240  * @return: size in bytes.
241  */
242 size_t listen_get_mem(struct listen_dnsport* listen);
243 
244 /**
245  * stop accept handlers for TCP (until enabled again)
246  * @param listen: listening structure.
247  */
248 void listen_stop_accept(struct listen_dnsport* listen);
249 
250 /**
251  * start accept handlers for TCP (was stopped before)
252  * @param listen: listening structure.
253  */
254 void listen_start_accept(struct listen_dnsport* listen);
255 
256 /**
257  * Create and bind nonblocking UDP socket
258  * @param family: for socket call.
259  * @param socktype: for socket call.
260  * @param addr: for bind call.
261  * @param addrlen: for bind call.
262  * @param v6only: if enabled, IP6 sockets get IP6ONLY option set.
263  * 	if enabled with value 2 IP6ONLY option is disabled.
264  * @param inuse: on error, this is set true if the port was in use.
265  * @param noproto: on error, this is set true if cause is that the
266 	IPv6 proto (family) is not available.
267  * @param rcv: set size on rcvbuf with socket option, if 0 it is not set.
268  * @param snd: set size on sndbuf with socket option, if 0 it is not set.
269  * @param listen: if true, this is a listening UDP port, eg port 53, and
270  * 	set SO_REUSEADDR on it.
271  * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on
272  * 	listening UDP port.  Set to false on return if it failed to do so.
273  * @param transparent: set IP_TRANSPARENT socket option.
274  * @param freebind: set IP_FREEBIND socket option.
275  * @param use_systemd: if true, fetch sockets from systemd.
276  * @param dscp: DSCP to use.
277  * @return: the socket. -1 on error.
278  */
279 int create_udp_sock(int family, int socktype, struct sockaddr* addr,
280 	socklen_t addrlen, int v6only, int* inuse, int* noproto, int rcv,
281 	int snd, int listen, int* reuseport, int transparent, int freebind, int use_systemd, int dscp);
282 
283 /**
284  * Create and bind TCP listening socket
285  * @param addr: address info ready to make socket.
286  * @param v6only: enable ip6 only flag on ip6 sockets.
287  * @param noproto: if error caused by lack of protocol support.
288  * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on
289  * 	listening UDP port.  Set to false on return if it failed to do so.
290  * @param transparent: set IP_TRANSPARENT socket option.
291  * @param mss: maximum segment size of the socket. if zero, leaves the default.
292  * @param nodelay: if true set TCP_NODELAY and TCP_QUICKACK socket options.
293  * @param freebind: set IP_FREEBIND socket option.
294  * @param use_systemd: if true, fetch sockets from systemd.
295  * @param dscp: DSCP to use.
296  * @param additional: additional log information for the socket type.
297  * @return: the socket. -1 on error.
298  */
299 int create_tcp_accept_sock(struct addrinfo *addr, int v6only, int* noproto,
300 	int* reuseport, int transparent, int mss, int nodelay, int freebind,
301 	int use_systemd, int dscp, const char* additional);
302 
303 /**
304  * Create and bind local listening socket
305  * @param path: path to the socket.
306  * @param noproto: on error, this is set true if cause is that local sockets
307  *	are not supported.
308  * @param use_systemd: if true, fetch sockets from systemd.
309  * @return: the socket. -1 on error.
310  */
311 int create_local_accept_sock(const char* path, int* noproto, int use_systemd);
312 
313 /**
314  * TCP request info.  List of requests outstanding on the channel, that
315  * are asked for but not yet answered back.
316  */
317 struct tcp_req_info {
318 	/** the TCP comm point for this.  Its buffer is used for read/write */
319 	struct comm_point* cp;
320 	/** the buffer to use to spool reply from mesh into,
321 	 * it can then be copied to the result list and written.
322 	 * it is a pointer to the shared udp buffer. */
323 	struct sldns_buffer* spool_buffer;
324 	/** are we in worker_handle function call (for recursion callback)*/
325 	int in_worker_handle;
326 	/** is the comm point dropped (by worker handle).
327 	 * That means we have to disconnect the channel. */
328 	int is_drop;
329 	/** is the comm point set to send_reply (by mesh new client in worker
330 	 * handle), if so answer is available in c.buffer */
331 	int is_reply;
332 	/** read channel has closed, just write pending results */
333 	int read_is_closed;
334 	/** read again */
335 	int read_again;
336 	/** number of outstanding requests */
337 	int num_open_req;
338 	/** list of outstanding requests */
339 	struct tcp_req_open_item* open_req_list;
340 	/** number of pending writeable results */
341 	int num_done_req;
342 	/** list of pending writable result packets, malloced one at a time */
343 	struct tcp_req_done_item* done_req_list;
344 };
345 
346 /**
347  * List of open items in TCP channel
348  */
349 struct tcp_req_open_item {
350 	/** next in list */
351 	struct tcp_req_open_item* next;
352 	/** the mesh area of the mesh_state */
353 	struct mesh_area* mesh;
354 	/** the mesh state */
355 	struct mesh_state* mesh_state;
356 };
357 
358 /**
359  * List of done items in TCP channel
360  */
361 struct tcp_req_done_item {
362 	/** next in list */
363 	struct tcp_req_done_item* next;
364 	/** the buffer with packet contents */
365 	uint8_t* buf;
366 	/** length of the buffer */
367 	size_t len;
368 };
369 
370 /**
371  * Create tcp request info structure that keeps track of open
372  * requests on the TCP channel that are resolved at the same time,
373  * and the pending results that have to get written back to that client.
374  * @param spoolbuf: shared buffer
375  * @return new structure or NULL on alloc failure.
376  */
377 struct tcp_req_info* tcp_req_info_create(struct sldns_buffer* spoolbuf);
378 
379 /**
380  * Delete tcp request structure.  Called by owning commpoint.
381  * Removes mesh entry references and stored results from the lists.
382  * @param req: the tcp request info
383  */
384 void tcp_req_info_delete(struct tcp_req_info* req);
385 
386 /**
387  * Clear tcp request structure.  Removes list entries, sets it up ready
388  * for the next connection.
389  * @param req: tcp request info structure.
390  */
391 void tcp_req_info_clear(struct tcp_req_info* req);
392 
393 /**
394  * Remove mesh state entry from list in tcp_req_info.
395  * caller has to manage the mesh state reply entry in the mesh state.
396  * @param req: the tcp req info that has the entry removed from the list.
397  * @param m: the state removed from the list.
398  */
399 void tcp_req_info_remove_mesh_state(struct tcp_req_info* req,
400 	struct mesh_state* m);
401 
402 /**
403  * Handle write done of the last result packet
404  * @param req: the tcp req info.
405  */
406 void tcp_req_info_handle_writedone(struct tcp_req_info* req);
407 
408 /**
409  * Handle read done of a new request from the client
410  * @param req: the tcp req info.
411  */
412 void tcp_req_info_handle_readdone(struct tcp_req_info* req);
413 
414 /**
415  * Add mesh state to the tcp req list of open requests.
416  * So the comm_reply can be removed off the mesh reply list when
417  * the tcp channel has to be closed (for other reasons then that that
418  * request was done, eg. channel closed by client or some format error).
419  * @param req: tcp req info structure.  It keeps track of the simultaneous
420  * 	requests and results on a tcp (or TLS) channel.
421  * @param mesh: mesh area for the state.
422  * @param m: mesh state to add.
423  * @return 0 on failure (malloc failure).
424  */
425 int tcp_req_info_add_meshstate(struct tcp_req_info* req,
426 	struct mesh_area* mesh, struct mesh_state* m);
427 
428 /**
429  * Send reply on tcp simultaneous answer channel.  May queue it up.
430  * @param req: request info structure.
431  */
432 void tcp_req_info_send_reply(struct tcp_req_info* req);
433 
434 /** the read channel has closed
435  * @param req: request. remaining queries are looked up and answered.
436  * @return zero if nothing to do, just close the tcp.
437  */
438 int tcp_req_info_handle_read_close(struct tcp_req_info* req);
439 
440 /** get the size of currently used tcp stream wait buffers (in bytes) */
441 size_t tcp_req_info_get_stream_buffer_size(void);
442 
443 /** get the size of currently used HTTP2 query buffers (in bytes) */
444 size_t http2_get_query_buffer_size(void);
445 /** get the size of currently used HTTP2 response buffers (in bytes) */
446 size_t http2_get_response_buffer_size(void);
447 
448 #ifdef HAVE_NGHTTP2
449 /**
450  * Create nghttp2 callbacks to handle HTTP2 requests.
451  * @return malloc'ed struct, NULL on failure
452  */
453 nghttp2_session_callbacks* http2_req_callbacks_create(void);
454 
455 /** Free http2 stream buffers and decrease buffer counters */
456 void http2_req_stream_clear(struct http2_stream* h2_stream);
457 
458 /**
459  * DNS response ready to be submitted to nghttp2, to be prepared for sending
460  * out. Response is stored in c->buffer. Copy to rbuffer because the c->buffer
461  * might be used before this will be send out.
462  * @param h2_session: http2 session, containing c->buffer which contains answer
463  * @param h2_stream: http2 stream, containing buffer to store answer in
464  * @return 0 on error, 1 otherwise
465  */
466 int http2_submit_dns_response(struct http2_session* h2_session);
467 #else
468 int http2_submit_dns_response(void* v);
469 #endif /* HAVE_NGHTTP2 */
470 
471 #ifdef HAVE_NGTCP2
472 struct doq_conid;
473 struct doq_server_socket;
474 
475 /**
476  * DoQ shared connection table. This is the connections for the host.
477  * And some config parameter values for connections. The host has to
478  * respond on that ip,port for those connections, so they are shared
479  * between threads.
480  */
481 struct doq_table {
482 	/** the lock on the tree and config elements. insert and deletion,
483 	 * also lookup in the tree needs to hold the lock. */
484 	lock_rw_type lock;
485 	/** rbtree of doq_conn, the connections to different destination
486 	 * addresses, and can be found by dcid. */
487 	struct rbtree_type* conn_tree;
488 	/** lock for the conid tree, needed for the conid tree and also
489 	 * the conid elements */
490 	lock_rw_type conid_lock;
491 	/** rbtree of doq_conid, connections can be found by their
492 	 * connection ids. Lookup by connection id, finds doq_conn. */
493 	struct rbtree_type* conid_tree;
494 	/** the server scid length */
495 	int sv_scidlen;
496 	/** the static secret for the server */
497 	uint8_t* static_secret;
498 	/** length of the static secret */
499 	size_t static_secret_len;
500 	/** the idle timeout in nanoseconds */
501 	uint64_t idle_timeout;
502 	/** the list of write interested connections, hold the doq_table.lock
503 	 * to change them */
504 	struct doq_conn* write_list_first, *write_list_last;
505 	/** rbtree of doq_timer. */
506 	struct rbtree_type* timer_tree;
507 	/** lock on the current_size counter. */
508 	lock_basic_type size_lock;
509 	/** current use, in bytes, of QUIC buffers.
510 	 * The doq_conn ngtcp2_conn structure, SSL structure and conid structs
511 	 * are not counted. */
512 	size_t current_size;
513 };
514 
515 /** create doq table */
516 struct doq_table* doq_table_create(struct config_file* cfg,
517 	struct ub_randstate* rnd);
518 
519 /** delete doq table */
520 void doq_table_delete(struct doq_table* table);
521 
522 /**
523  * Timer information for doq timer.
524  */
525 struct doq_timer {
526 	/** The rbnode in the tree sorted by timeout value. Key this struct. */
527 	struct rbnode_type node;
528 	/** The timeout value. Absolute time value. */
529 	struct timeval time;
530 	/** If the timer is in the time tree, with the node. */
531 	int timer_in_tree;
532 	/** If there are more timers with the exact same timeout value,
533 	 * they form a set of timers. The rbnode timer has a link to the list
534 	 * with the other timers in the set. The rbnode timer is not a
535 	 * member of the list with the other timers. The other timers are not
536 	 * linked into the tree. */
537 	struct doq_timer* setlist_first, *setlist_last;
538 	/** If the timer is on the setlist. */
539 	int timer_in_list;
540 	/** If in the setlist, the next and prev element. */
541 	struct doq_timer* setlist_next, *setlist_prev;
542 	/** The connection that is timeouted. */
543 	struct doq_conn* conn;
544 	/** The worker that is waiting for the timeout event.
545 	 * Set for the rbnode tree linked element. If a worker is waiting
546 	 * for the event. If NULL, no worker is waiting for this timeout. */
547 	struct doq_server_socket* worker_doq_socket;
548 };
549 
550 /**
551  * Key information that makes a doq_conn node in the tree lookup.
552  */
553 struct doq_conn_key {
554 	/** the remote endpoint and local endpoint and ifindex */
555 	struct doq_pkt_addr paddr;
556 	/** the doq connection dcid */
557 	uint8_t* dcid;
558 	/** length of dcid */
559 	size_t dcidlen;
560 };
561 
562 /**
563  * DoQ connection, for DNS over QUIC. One connection to a remote endpoint
564  * with a number of streams in it. Every stream is like a tcp stream with
565  * a uint16_t length, query read, and a uint16_t length and answer written.
566  */
567 struct doq_conn {
568 	/** rbtree node, key is addresses and dcid */
569 	struct rbnode_type node;
570 	/** lock on the connection */
571 	lock_basic_type lock;
572 	/** the key information, with dcid and address endpoint */
573 	struct doq_conn_key key;
574 	/** the doq server socket for inside callbacks */
575 	struct doq_server_socket* doq_socket;
576 	/** the doq table this connection is part of */
577 	struct doq_table* table;
578 	/** if the connection is about to be deleted. */
579 	uint8_t is_deleted;
580 	/** the version, the client chosen version of QUIC */
581 	uint32_t version;
582 	/** the ngtcp2 connection, a server connection */
583 	struct ngtcp2_conn* conn;
584 	/** the connection ids that are associated with this doq_conn.
585 	 * There can be a number, that can change. They are linked here,
586 	 * so that upon removal, the list of actually associated conid
587 	 * elements can be removed as well. */
588 	struct doq_conid* conid_list;
589 	/** the ngtcp2 last error for the connection */
590 #ifdef HAVE_NGTCP2_CCERR_DEFAULT
591 	struct ngtcp2_ccerr ccerr;
592 #else
593 	struct ngtcp2_connection_close_error last_error;
594 #endif
595 	/** the recent tls alert error code */
596 	uint8_t tls_alert;
597 	/** the ssl context, SSL* */
598 	void* ssl;
599 #ifdef HAVE_NGTCP2_CRYPTO_QUICTLS_CONFIGURE_SERVER_CONTEXT
600 	/** the connection reference for ngtcp2_conn and userdata in ssl */
601 	struct ngtcp2_crypto_conn_ref conn_ref;
602 #endif
603 	/** closure packet, if any */
604 	uint8_t* close_pkt;
605 	/** length of closure packet. */
606 	size_t close_pkt_len;
607 	/** closure ecn */
608 	uint32_t close_ecn;
609 	/** the streams for this connection, of type doq_stream */
610 	struct rbtree_type stream_tree;
611 	/** the streams that want write, they have something to write.
612 	 * The list is ordered, the last have to wait for the first to
613 	 * get their data written. */
614 	struct doq_stream* stream_write_first, *stream_write_last;
615 	/** the conn has write interest if true, no write interest if false. */
616 	uint8_t write_interest;
617 	/** if the conn is on the connection write list */
618 	uint8_t on_write_list;
619 	/** the connection write list prev and next, if on the write list */
620 	struct doq_conn* write_prev, *write_next;
621 	/** The timer for the connection. If unused, it is not in the tree
622 	 * and not in the list. It is alloced here, so that it is prealloced.
623 	 * It has to be set after every read and write on the connection, so
624 	 * this improves performance, but also the allocation does not fail. */
625 	struct doq_timer timer;
626 };
627 
628 /**
629  * Connection ID and the doq_conn that is that connection. A connection
630  * has an original dcid, and then more connection ids associated.
631  */
632 struct doq_conid {
633 	/** rbtree node, key is the connection id. */
634 	struct rbnode_type node;
635 	/** the next and prev in the list of conids for the doq_conn */
636 	struct doq_conid* next, *prev;
637 	/** key to the doq_conn that is the connection */
638 	struct doq_conn_key key;
639 	/** the connection id, byte string */
640 	uint8_t* cid;
641 	/** the length of cid */
642 	size_t cidlen;
643 };
644 
645 /**
646  * DoQ stream, for DNS over QUIC.
647  */
648 struct doq_stream {
649 	/** the rbtree node for the stream, key is the stream_id */
650 	rbnode_type node;
651 	/** the stream id */
652 	int64_t stream_id;
653 	/** if the stream is closed */
654 	uint8_t is_closed;
655 	/** if the query is complete */
656 	uint8_t is_query_complete;
657 	/** the number of bytes read on the stream, up to querylen+2. */
658 	size_t nread;
659 	/** the length of the input query bytes */
660 	size_t inlen;
661 	/** the input bytes */
662 	uint8_t* in;
663 	/** does the stream have an answer to send */
664 	uint8_t is_answer_available;
665 	/** the answer bytes sent, up to outlen+2. */
666 	size_t nwrite;
667 	/** the length of the output answer bytes */
668 	size_t outlen;
669 	/** the output length in network wireformat */
670 	uint16_t outlen_wire;
671 	/** the output packet bytes */
672 	uint8_t* out;
673 	/** if the stream is on the write list */
674 	uint8_t on_write_list;
675 	/** the prev and next on the write list, if on the list */
676 	struct doq_stream* write_prev, *write_next;
677 };
678 
679 /** doq application error code that is sent when a stream is closed */
680 #define DOQ_APP_ERROR_CODE 1
681 
682 /**
683  * Create the doq connection.
684  * @param c: the comm point for the listening doq socket.
685  * @param paddr: with remote and local address and ifindex for the
686  * 	connection destination. This is where packets are sent.
687  * @param dcid: the dcid, Destination Connection ID.
688  * @param dcidlen: length of dcid.
689  * @param version: client chosen version.
690  * @return new doq connection or NULL on allocation failure.
691  */
692 struct doq_conn* doq_conn_create(struct comm_point* c,
693 	struct doq_pkt_addr* paddr, const uint8_t* dcid, size_t dcidlen,
694 	uint32_t version);
695 
696 /**
697  * Delete the doq connection structure.
698  * @param conn: to delete.
699  * @param table: with memory size.
700  */
701 void doq_conn_delete(struct doq_conn* conn, struct doq_table* table);
702 
703 /** compare function of doq_conn */
704 int doq_conn_cmp(const void* key1, const void* key2);
705 
706 /** compare function of doq_conid */
707 int doq_conid_cmp(const void* key1, const void* key2);
708 
709 /** compare function of doq_timer */
710 int doq_timer_cmp(const void* key1, const void* key2);
711 
712 /** compare function of doq_stream */
713 int doq_stream_cmp(const void* key1, const void* key2);
714 
715 /** setup the doq_socket server tls context */
716 int doq_socket_setup_ctx(struct doq_server_socket* doq_socket);
717 
718 /** setup the doq connection callbacks, and settings. */
719 int doq_conn_setup(struct doq_conn* conn, uint8_t* scid, size_t scidlen,
720 	uint8_t* ocid, size_t ocidlen, const uint8_t* token, size_t tokenlen);
721 
722 /** fill a buffer with random data */
723 void doq_fill_rand(struct ub_randstate* rnd, uint8_t* buf, size_t len);
724 
725 /** delete a doq_conid */
726 void doq_conid_delete(struct doq_conid* conid);
727 
728 /** add a connection id to the doq_conn.
729  * caller must hold doq_table.conid_lock. */
730 int doq_conn_associate_conid(struct doq_conn* conn, uint8_t* data,
731 	size_t datalen);
732 
733 /** remove a connection id from the doq_conn.
734  * caller must hold doq_table.conid_lock. */
735 void doq_conn_dissociate_conid(struct doq_conn* conn, const uint8_t* data,
736 	size_t datalen);
737 
738 /** initial setup to link current connection ids to the doq_conn */
739 int doq_conn_setup_conids(struct doq_conn* conn);
740 
741 /** remove the connection ids from the doq_conn.
742  * caller must hold doq_table.conid_lock. */
743 void doq_conn_clear_conids(struct doq_conn* conn);
744 
745 /** find a conid in the doq_conn connection.
746  * caller must hold table.conid_lock. */
747 struct doq_conid* doq_conid_find(struct doq_table* doq_table,
748 	const uint8_t* data, size_t datalen);
749 
750 /** receive a packet for a connection */
751 int doq_conn_recv(struct comm_point* c, struct doq_pkt_addr* paddr,
752 	struct doq_conn* conn, struct ngtcp2_pkt_info* pi, int* err_retry,
753 	int* err_drop);
754 
755 /** send packets for a connection */
756 int doq_conn_write_streams(struct comm_point* c, struct doq_conn* conn,
757 	int* err_drop);
758 
759 /** send the close packet for the connection, perhaps again. */
760 int doq_conn_send_close(struct comm_point* c, struct doq_conn* conn);
761 
762 /** delete doq stream */
763 void doq_stream_delete(struct doq_stream* stream);
764 
765 /** doq read a connection key from repinfo. It is not malloced, but points
766  * into the repinfo for the dcid. */
767 void doq_conn_key_from_repinfo(struct doq_conn_key* key,
768 	struct comm_reply* repinfo);
769 
770 /** doq find a stream in the connection */
771 struct doq_stream* doq_stream_find(struct doq_conn* conn, int64_t stream_id);
772 
773 /** doq shutdown the stream. */
774 int doq_stream_close(struct doq_conn* conn, struct doq_stream* stream,
775 	int send_shutdown);
776 
777 /** send reply for a connection */
778 int doq_stream_send_reply(struct doq_conn* conn, struct doq_stream* stream,
779 	struct sldns_buffer* buf);
780 
781 /** the connection has write interest, wants to write packets */
782 void doq_conn_write_enable(struct doq_conn* conn);
783 
784 /** the connection has no write interest, does not want to write packets */
785 void doq_conn_write_disable(struct doq_conn* conn);
786 
787 /** set the connection on or off the write list, depending on write interest */
788 void doq_conn_set_write_list(struct doq_table* table, struct doq_conn* conn);
789 
790 /** doq remove the connection from the write list */
791 void doq_conn_write_list_remove(struct doq_table* table,
792 	struct doq_conn* conn);
793 
794 /** doq get the first conn from the write list, if any, popped from list.
795  * Locks the conn that is returned. */
796 struct doq_conn* doq_table_pop_first(struct doq_table* table);
797 
798 /**
799  * doq check if the timer for the conn needs to be changed.
800  * @param conn: connection, caller must hold lock on it.
801  * @param tv: time value, absolute time, returned.
802  * @return true if timer needs to be set to tv, false if no change is needed
803  * 	to the timer. The timer is already set to the right time in that case.
804  */
805 int doq_conn_check_timer(struct doq_conn* conn, struct timeval* tv);
806 
807 /** doq remove timer from tree */
808 void doq_timer_tree_remove(struct doq_table* table, struct doq_timer* timer);
809 
810 /** doq remove timer from list */
811 void doq_timer_list_remove(struct doq_table* table, struct doq_timer* timer);
812 
813 /** doq unset the timer if it was set. */
814 void doq_timer_unset(struct doq_table* table, struct doq_timer* timer);
815 
816 /** doq set the timer and add it. */
817 void doq_timer_set(struct doq_table* table, struct doq_timer* timer,
818 	struct doq_server_socket* worker_doq_socket, struct timeval* tv);
819 
820 /** doq find a timeout in the timer tree */
821 struct doq_timer* doq_timer_find_time(struct doq_table* table,
822 	struct timeval* tv);
823 
824 /** doq handle timeout for a connection. Pass conn locked. Returns false for
825  * deletion. */
826 int doq_conn_handle_timeout(struct doq_conn* conn);
827 
828 /** doq add size to the current quic buffer counter */
829 void doq_table_quic_size_add(struct doq_table* table, size_t add);
830 
831 /** doq subtract size from the current quic buffer counter */
832 void doq_table_quic_size_subtract(struct doq_table* table, size_t subtract);
833 
834 /** doq check if mem is available for quic. */
835 int doq_table_quic_size_available(struct doq_table* table,
836 	struct config_file* cfg, size_t mem);
837 
838 /** doq get the quic size value */
839 size_t doq_table_quic_size_get(struct doq_table* table);
840 #endif /* HAVE_NGTCP2 */
841 
842 char* set_ip_dscp(int socket, int addrfamily, int ds);
843 
844 /** for debug and profiling purposes only
845  * @param ub_sock: the structure containing created socket info we want to print or log for
846  */
847 void verbose_print_unbound_socket(struct unbound_socket* ub_sock);
848 
849 /** event callback for testcode/doqclient */
850 void doq_client_event_cb(int fd, short event, void* arg);
851 
852 /** timer event callback for testcode/doqclient */
853 void doq_client_timer_cb(int fd, short event, void* arg);
854 
855 #ifdef HAVE_NGTCP2
856 /** get a timestamp in nanoseconds */
857 ngtcp2_tstamp doq_get_timestamp_nanosec(void);
858 #endif
859 #endif /* LISTEN_DNSPORT_H */
860