Home | History | Annotate | Line # | Download | only in netmgr
      1 /*	$NetBSD: netmgr-int.h,v 1.15 2026/04/08 00:16:16 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  *
      6  * SPDX-License-Identifier: MPL-2.0
      7  *
      8  * This Source Code Form is subject to the terms of the Mozilla Public
      9  * License, v. 2.0. If a copy of the MPL was not distributed with this
     10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
     11  *
     12  * See the COPYRIGHT file distributed with this work for additional
     13  * information regarding copyright ownership.
     14  */
     15 
     16 #pragma once
     17 
     18 #include <unistd.h>
     19 
     20 #include <openssl/err.h>
     21 #include <openssl/ssl.h>
     22 
     23 #include <isc/atomic.h>
     24 #include <isc/barrier.h>
     25 #include <isc/buffer.h>
     26 #include <isc/condition.h>
     27 #include <isc/dnsstream.h>
     28 #include <isc/magic.h>
     29 #include <isc/mem.h>
     30 #include <isc/netmgr.h>
     31 #include <isc/proxy2.h>
     32 #include <isc/quota.h>
     33 #include <isc/random.h>
     34 #include <isc/refcount.h>
     35 #include <isc/region.h>
     36 #include <isc/result.h>
     37 #include <isc/sockaddr.h>
     38 #include <isc/stats.h>
     39 #include <isc/thread.h>
     40 #include <isc/tid.h>
     41 #include <isc/time.h>
     42 #include <isc/tls.h>
     43 #include <isc/util.h>
     44 #include <isc/uv.h>
     45 
     46 #include "../loop_p.h"
     47 
     48 #define ISC_NETMGR_TID_UNKNOWN -1
     49 
     50 /*
     51  * Receive buffers
     52  */
     53 #if HAVE_DECL_UV_UDP_MMSG_CHUNK
     54 /*
     55  * The value 20 here is UV__MMSG_MAXWIDTH taken from the current libuv source,
     56  * libuv will not receive more that 20 datagrams in a single recvmmsg call.
     57  */
     58 #define ISC_NETMGR_UDP_RECVBUF_SIZE (20 * UINT16_MAX)
     59 #else
     60 /*
     61  * A single DNS message size
     62  */
     63 #define ISC_NETMGR_UDP_RECVBUF_SIZE UINT16_MAX
     64 #endif
     65 #define ISC_NETMGR_UDP_SENDBUF_SIZE UINT16_MAX
     66 
     67 /*
     68  * The TCP send and receive buffers can fit one maximum sized DNS message plus
     69  * its size, the receive buffer here affects TCP, DoT and DoH.
     70  */
     71 #define ISC_NETMGR_TCP_SENDBUF_SIZE (sizeof(uint16_t) + UINT16_MAX)
     72 #define ISC_NETMGR_TCP_RECVBUF_SIZE (sizeof(uint16_t) + UINT16_MAX)
     73 
     74 /* Pick the larger buffer */
     75 #define ISC_NETMGR_RECVBUF_SIZE                                     \
     76 	(ISC_NETMGR_UDP_RECVBUF_SIZE >= ISC_NETMGR_TCP_RECVBUF_SIZE \
     77 		 ? ISC_NETMGR_UDP_RECVBUF_SIZE                      \
     78 		 : ISC_NETMGR_TCP_RECVBUF_SIZE)
     79 
     80 /*
     81  * Make sure our RECVBUF size is large enough
     82  */
     83 
     84 STATIC_ASSERT(ISC_NETMGR_UDP_RECVBUF_SIZE <= ISC_NETMGR_RECVBUF_SIZE,
     85 	      "UDP receive buffer size must be smaller or equal than worker "
     86 	      "receive buffer size");
     87 
     88 STATIC_ASSERT(ISC_NETMGR_TCP_RECVBUF_SIZE <= ISC_NETMGR_RECVBUF_SIZE,
     89 	      "TCP receive buffer size must be smaller or equal than worker "
     90 	      "receive buffer size");
     91 
     92 /*%
     93  * Maximum outstanding DNS message that we process in a single TCP read.
     94  */
     95 #define ISC_NETMGR_MAX_STREAM_CLIENTS_PER_CONN 23
     96 
     97 /*%
     98  * Regular TCP buffer size.
     99  */
    100 #define NM_REG_BUF 4096
    101 
    102 /*%
    103  * Larger buffer for when the regular one isn't enough; this will
    104  * hold two full DNS packets with lengths.  netmgr receives 64k at
    105  * most in TCPDNS or TLSDNS connections, so there's no risk of overrun
    106  * when using a buffer this size.
    107  */
    108 #define NM_BIG_BUF ISC_NETMGR_TCP_RECVBUF_SIZE * 2
    109 
    110 /*%
    111  * Maximum segment size (MSS) of TCP socket on which the server responds to
    112  * queries. Value lower than common MSS on Ethernet (1220, that is 1280 (IPv6
    113  * minimum link MTU) - 40 (IPv6 fixed header) - 20 (TCP fixed header)) will
    114  * address path MTU problem.
    115  */
    116 #define NM_MAXSEG (1280 - 20 - 40)
    117 
    118 /*%
    119  * How many isc_nmhandles and isc_nm_uvreqs will we be
    120  * caching for reuse in a socket.
    121  */
    122 #define ISC_NM_NMSOCKET_MAX  64
    123 #define ISC_NM_NMHANDLES_MAX 64
    124 #define ISC_NM_UVREQS_MAX    64
    125 
    126 /*% ISC_PROXY2_MIN_AF_UNIX_SIZE is the largest type when TLVs are not used */
    127 #define ISC_NM_PROXY2_DEFAULT_BUFFER_SIZE (ISC_PROXY2_MIN_AF_UNIX_SIZE)
    128 
    129 /*
    130  * Define ISC_NETMGR_TRACE to activate tracing of handles and sockets.
    131  * This will impair performance but enables us to quickly determine,
    132  * if netmgr resources haven't been cleaned up on shutdown, which ones
    133  * are still in use.
    134  */
    135 #if ISC_NETMGR_TRACE
    136 #define TRACE_SIZE 8
    137 
    138 #if defined(__linux__)
    139 #include <syscall.h>
    140 #define gettid() (uint64_t)syscall(SYS_gettid)
    141 #elif defined(__FreeBSD__)
    142 #include <pthread_np.h>
    143 #define gettid() (uint64_t)(pthread_getthreadid_np())
    144 #elif defined(__OpenBSD__)
    145 #include <unistd.h>
    146 #define gettid() (uint64_t)(getthrid())
    147 #elif defined(__NetBSD__)
    148 #include <lwp.h>
    149 #define gettid() (uint64_t)(_lwp_self())
    150 #elif defined(__DragonFly__)
    151 #include <unistd.h>
    152 #define gettid() (uint64_t)(lwp_gettid())
    153 #else
    154 #define gettid() (uint64_t)(pthread_self())
    155 #endif
    156 
    157 #define NETMGR_TRACE_LOG(format, ...)                                \
    158 	fprintf(stderr, "%" PRIu64 ":%d:%s:%u:%s:" format, gettid(), \
    159 		isc_tid(), file, line, func, __VA_ARGS__)
    160 
    161 #define FLARG                                                                 \
    162 	, const char *func ISC_ATTR_UNUSED, const char *file ISC_ATTR_UNUSED, \
    163 		unsigned int line ISC_ATTR_UNUSED
    164 
    165 #define FLARG_PASS , func, file, line
    166 #define isc__nm_uvreq_get(sock) \
    167 	isc___nm_uvreq_get(sock, __func__, __FILE__, __LINE__)
    168 #define isc__nm_uvreq_put(req) \
    169 	isc___nm_uvreq_put(req, __func__, __FILE__, __LINE__)
    170 #define isc__nmsocket_init(sock, mgr, type, iface, parent)            \
    171 	isc___nmsocket_init(sock, mgr, type, iface, parent, __func__, \
    172 			    __FILE__, __LINE__)
    173 #define isc__nmsocket_put(sockp) \
    174 	isc___nmsocket_put(sockp, __func__, __FILE__, __LINE__)
    175 #define isc__nmsocket_attach(sock, target) \
    176 	isc___nmsocket_attach(sock, target, __func__, __FILE__, __LINE__)
    177 #define isc__nmsocket_detach(socketp) \
    178 	isc___nmsocket_detach(socketp, __func__, __FILE__, __LINE__)
    179 #define isc__nmsocket_close(socketp) \
    180 	isc___nmsocket_close(socketp, __func__, __FILE__, __LINE__)
    181 #define isc__nmhandle_get(sock, peer, local) \
    182 	isc___nmhandle_get(sock, peer, local, __func__, __FILE__, __LINE__)
    183 #define isc__nmsocket_prep_destroy(sock) \
    184 	isc___nmsocket_prep_destroy(sock, __func__, __FILE__, __LINE__)
    185 #define isc__nm_get_read_req(sock, sockaddr) \
    186 	isc___nm_get_read_req(sock, sockaddr, __func__, __FILE__, __LINE__)
    187 #else
    188 #define NETMGR_TRACE_LOG(format, ...)
    189 
    190 #define FLARG
    191 #define FLARG_PASS
    192 #define isc__nm_uvreq_get(sock) isc___nm_uvreq_get(sock)
    193 #define isc__nm_uvreq_put(req)	isc___nm_uvreq_put(req)
    194 #define isc__nmsocket_init(sock, mgr, type, iface, parent) \
    195 	isc___nmsocket_init(sock, mgr, type, iface, parent)
    196 #define isc__nmsocket_put(sockp)	   isc___nmsocket_put(sockp)
    197 #define isc__nmsocket_attach(sock, target) isc___nmsocket_attach(sock, target)
    198 #define isc__nmsocket_detach(socketp)	   isc___nmsocket_detach(socketp)
    199 #define isc__nmsocket_close(socketp)	   isc___nmsocket_close(socketp)
    200 #define isc__nmhandle_get(sock, peer, local) \
    201 	isc___nmhandle_get(sock, peer, local)
    202 #define isc__nmsocket_prep_destroy(sock) isc___nmsocket_prep_destroy(sock)
    203 #define isc__nm_get_read_req(sock, sockaddr) \
    204 	isc___nm_get_read_req(sock, sockaddr)
    205 #endif
    206 
    207 typedef struct isc__nm_uvreq isc__nm_uvreq_t;
    208 
    209 /*
    210  * Single network event loop worker.
    211  */
    212 typedef struct isc__networker {
    213 	isc_mem_t *mctx;
    214 	isc_refcount_t references;
    215 	isc_loop_t *loop;
    216 	isc_nm_t *netmgr;
    217 	bool shuttingdown;
    218 
    219 	char *recvbuf;
    220 	bool recvbuf_inuse;
    221 
    222 	ISC_LIST(isc_nmsocket_t) active_sockets;
    223 
    224 	isc_mempool_t *nmsocket_pool;
    225 	isc_mempool_t *uvreq_pool;
    226 } isc__networker_t;
    227 
    228 ISC_REFCOUNT_DECL(isc__networker);
    229 
    230 #ifdef ISC_NETMGR_TRACE
    231 void
    232 isc__nm_dump_active(isc__networker_t *worker);
    233 
    234 void
    235 isc__nm_dump_active_manager(isc_nm_t *netmgr);
    236 #endif /* ISC_NETMGR_TRACE */
    237 
    238 /*
    239  * A general handle for a connection bound to a networker.  For UDP
    240  * connections we have peer address here, so both TCP and UDP can be
    241  * handled with a simple send-like function
    242  */
    243 #define NMHANDLE_MAGIC ISC_MAGIC('N', 'M', 'H', 'D')
    244 #define VALID_NMHANDLE(t)                      \
    245 	(ISC_MAGIC_VALID(t, NMHANDLE_MAGIC) && \
    246 	 atomic_load(&(t)->references) > 0)
    247 
    248 typedef void (*isc__nm_closecb)(isc_nmhandle_t *);
    249 typedef struct isc_nm_http_session isc_nm_http_session_t;
    250 
    251 struct isc_nmhandle {
    252 	int magic;
    253 	isc_refcount_t references;
    254 
    255 	/*
    256 	 * The socket is not 'attached' in the traditional
    257 	 * reference-counting sense. Instead, we keep all handles in an
    258 	 * array in the socket object.  This way, we don't have circular
    259 	 * dependencies and we can close all handles when we're destroying
    260 	 * the socket.
    261 	 */
    262 	isc_nmsocket_t *sock;
    263 
    264 	isc_nm_http_session_t *httpsession;
    265 
    266 	isc_sockaddr_t peer;
    267 	isc_sockaddr_t local;
    268 	bool proxy_is_unspec;
    269 	struct isc_nmhandle *proxy_udphandle;
    270 	isc_nm_opaquecb_t doreset; /* reset extra callback, external */
    271 	isc_nm_opaquecb_t dofree;  /* free extra callback, external */
    272 #if ISC_NETMGR_TRACE
    273 	void *backtrace[TRACE_SIZE];
    274 	int backtrace_size;
    275 #endif
    276 	LINK(isc_nmhandle_t) active_link;
    277 	LINK(isc_nmhandle_t) inactive_link;
    278 
    279 	void *opaque;
    280 
    281 	isc_job_t job;
    282 };
    283 
    284 typedef union {
    285 	isc_nm_recv_cb_t recv;
    286 	isc_nm_cb_t send;
    287 	isc_nm_cb_t connect;
    288 } isc__nm_cb_t;
    289 
    290 /*
    291  * Wrapper around uv_req_t with 'our' fields in it.  req->data should
    292  * always point to its parent.  Note that we always allocate more than
    293  * sizeof(struct) because we make room for different req types;
    294  */
    295 #define UVREQ_MAGIC    ISC_MAGIC('N', 'M', 'U', 'R')
    296 #define VALID_UVREQ(t) ISC_MAGIC_VALID(t, UVREQ_MAGIC)
    297 
    298 typedef struct isc__nm_uvreq isc__nm_uvreq_t;
    299 struct isc__nm_uvreq {
    300 	int magic;
    301 	isc_nmsocket_t *sock;
    302 	isc_nmhandle_t *handle;
    303 	char tcplen[2];	       /* The TCP DNS message length */
    304 	uv_buf_t uvbuf;	       /* translated isc_region_t, to be
    305 				* sent or received */
    306 	isc_sockaddr_t local;  /* local address */
    307 	isc_sockaddr_t peer;   /* peer address */
    308 	isc__nm_cb_t cb;       /* callback */
    309 	void *cbarg;	       /* callback argument */
    310 	isc_nm_timer_t *timer; /* TCP write timer */
    311 	int connect_tries;     /* connect retries */
    312 	isc_result_t result;
    313 
    314 	union {
    315 		uv_handle_t handle;
    316 		uv_write_t write;
    317 		uv_connect_t connect;
    318 		uv_udp_send_t udp_send;
    319 	} uv_req;
    320 	ISC_LINK(isc__nm_uvreq_t) link;
    321 	ISC_LINK(isc__nm_uvreq_t) active_link;
    322 
    323 	isc_job_t job;
    324 };
    325 
    326 /*
    327  * Network manager
    328  */
    329 #define NM_MAGIC    ISC_MAGIC('N', 'E', 'T', 'M')
    330 #define VALID_NM(t) ISC_MAGIC_VALID(t, NM_MAGIC)
    331 
    332 struct isc_nm {
    333 	int magic;
    334 	isc_refcount_t references;
    335 	isc_mem_t *mctx;
    336 	isc_loopmgr_t *loopmgr;
    337 	uint32_t nloops;
    338 	isc__networker_t *workers;
    339 
    340 	isc_stats_t *stats;
    341 
    342 	atomic_uint_fast32_t maxudp;
    343 
    344 	bool load_balance_sockets;
    345 
    346 	/*
    347 	 * Active connections are being closed and new connections are
    348 	 * no longer allowed.
    349 	 */
    350 	atomic_bool shuttingdown;
    351 
    352 	/*
    353 	 * Timeout values for TCP connections, corresponding to
    354 	 * tcp-intiial-timeout, tcp-idle-timeout, tcp-keepalive-timeout,
    355 	 * and tcp-advertised-timeout. Note that these are stored in
    356 	 * milliseconds so they can be used directly with the libuv timer,
    357 	 * but they are configured in tenths of seconds.
    358 	 */
    359 	atomic_uint_fast32_t init;
    360 	atomic_uint_fast32_t idle;
    361 	atomic_uint_fast32_t keepalive;
    362 	atomic_uint_fast32_t advertised;
    363 
    364 	/*
    365 	 * Socket SO_RCVBUF and SO_SNDBUF values
    366 	 */
    367 	atomic_int_fast32_t recv_udp_buffer_size;
    368 	atomic_int_fast32_t send_udp_buffer_size;
    369 	atomic_int_fast32_t recv_tcp_buffer_size;
    370 	atomic_int_fast32_t send_tcp_buffer_size;
    371 
    372 	_Atomic(in_port_t) port_low4;
    373 	_Atomic(in_port_t) port_high4;
    374 	_Atomic(in_port_t) port_low6;
    375 	_Atomic(in_port_t) port_high6;
    376 };
    377 
    378 /*%
    379  * A universal structure for either a single socket or a group of
    380  * dup'd/SO_REUSE_PORT-using sockets listening on the same interface.
    381  */
    382 #define NMSOCK_MAGIC	ISC_MAGIC('N', 'M', 'S', 'K')
    383 #define VALID_NMSOCK(t) ISC_MAGIC_VALID(t, NMSOCK_MAGIC)
    384 
    385 /*%
    386  * Index into socket stat counter arrays.
    387  */
    388 typedef enum {
    389 	STATID_OPEN = 0,
    390 	STATID_OPENFAIL = 1,
    391 	STATID_CLOSE = 2,
    392 	STATID_BINDFAIL = 3,
    393 	STATID_CONNECTFAIL = 4,
    394 	STATID_CONNECT = 5,
    395 	STATID_ACCEPTFAIL = 6,
    396 	STATID_ACCEPT = 7,
    397 	STATID_SENDFAIL = 8,
    398 	STATID_RECVFAIL = 9,
    399 	STATID_ACTIVE = 10,
    400 	STATID_CLIENTS = 11,
    401 	STATID_MAX = 12,
    402 } isc__nm_statid_t;
    403 
    404 typedef struct isc_nmsocket_tls_send_req {
    405 	isc_nmsocket_t *tlssock;
    406 	isc_buffer_t data;
    407 	isc_nm_cb_t cb;
    408 	void *cbarg;
    409 	isc_nmhandle_t *handle;
    410 	bool finish;
    411 	uint8_t smallbuf[512];
    412 } isc_nmsocket_tls_send_req_t;
    413 
    414 #if HAVE_LIBNGHTTP2
    415 
    416 typedef enum isc_http_request_type {
    417 	ISC_HTTP_REQ_GET,
    418 	ISC_HTTP_REQ_POST,
    419 	ISC_HTTP_REQ_UNSUPPORTED
    420 } isc_http_request_type_t;
    421 
    422 typedef enum isc_http_scheme_type {
    423 	ISC_HTTP_SCHEME_HTTP,
    424 	ISC_HTTP_SCHEME_HTTP_SECURE,
    425 	ISC_HTTP_SCHEME_UNSUPPORTED
    426 } isc_http_scheme_type_t;
    427 
    428 typedef struct isc_nm_httphandler {
    429 	int magic;
    430 	char *path;
    431 	isc_nm_recv_cb_t cb;
    432 	void *cbarg;
    433 	LINK(struct isc_nm_httphandler) link;
    434 } isc_nm_httphandler_t;
    435 
    436 struct isc_nm_http_endpoints {
    437 	uint32_t magic;
    438 	isc_mem_t *mctx;
    439 
    440 	ISC_LIST(isc_nm_httphandler_t) handlers;
    441 
    442 	isc_refcount_t references;
    443 	atomic_bool in_use;
    444 };
    445 
    446 typedef struct isc_nmsocket_h2 {
    447 	isc_nmsocket_t *psock; /* owner of the structure */
    448 	char *request_path;
    449 	char *query_data;
    450 	size_t query_data_len;
    451 	bool query_too_large;
    452 
    453 	isc_buffer_t rbuf;
    454 	isc_buffer_t wbuf;
    455 
    456 	int32_t stream_id;
    457 	isc_nm_http_session_t *session;
    458 
    459 	/* maximum concurrent streams (server-side) */
    460 	atomic_uint_fast32_t max_concurrent_streams;
    461 
    462 	uint32_t min_ttl; /* used to set "max-age" in responses */
    463 
    464 	isc_http_request_type_t request_type;
    465 	isc_http_scheme_type_t request_scheme;
    466 
    467 	size_t content_length;
    468 	char clenbuf[128];
    469 
    470 	char cache_control_buf[128];
    471 
    472 	int headers_error_code;
    473 	size_t headers_data_processed;
    474 
    475 	isc_nm_recv_cb_t cb;
    476 	void *cbarg;
    477 	LINK(struct isc_nmsocket_h2) link;
    478 
    479 	isc_nm_http_endpoints_t **listener_endpoints;
    480 	size_t n_listener_endpoints;
    481 
    482 	isc_nm_http_endpoints_t *peer_endpoints;
    483 
    484 	bool request_received;
    485 	bool response_submitted;
    486 	struct {
    487 		char *uri;
    488 		bool post;
    489 		isc_tlsctx_t *tlsctx;
    490 		isc_sockaddr_t local_interface;
    491 		void *cstream;
    492 		const char *tls_peer_verify_string;
    493 	} connect;
    494 } isc_nmsocket_h2_t;
    495 #endif /* HAVE_LIBNGHTTP2 */
    496 
    497 typedef void (*isc_nm_closehandlecb_t)(void *arg);
    498 /*%<
    499  * Opaque callback function, used for isc_nmhandle 'reset' and 'free'
    500  * callbacks.
    501  */
    502 
    503 struct isc_nmsocket {
    504 	/*% Unlocked, RO */
    505 	int magic;
    506 	uint32_t tid;
    507 	isc_refcount_t references;
    508 	isc_nmsocket_type type;
    509 	isc__networker_t *worker;
    510 
    511 	isc_barrier_t listen_barrier;
    512 	isc_barrier_t stop_barrier;
    513 
    514 	/*% Parent socket for multithreaded listeners */
    515 	isc_nmsocket_t *parent;
    516 
    517 	/*% TLS stuff */
    518 	struct tlsstream {
    519 		bool server;
    520 		BIO *bio_in;
    521 		BIO *bio_out;
    522 		isc_tls_t *tls;
    523 		isc_tlsctx_t *ctx;
    524 		isc_tlsctx_t **listener_tls_ctx; /*%< A context reference per
    525 						    worker */
    526 		size_t n_listener_tls_ctx;
    527 		char *sni_hostname;
    528 		isc_tlsctx_client_session_cache_t *client_sess_cache;
    529 		bool client_session_saved;
    530 		isc_nmsocket_t *tlslistener;
    531 		isc_nmsocket_t *tlssocket;
    532 		atomic_bool result_updated;
    533 		enum {
    534 			TLS_INIT,
    535 			TLS_HANDSHAKE,
    536 			TLS_IO,
    537 			TLS_CLOSED
    538 		} state; /*%< The order of these is significant */
    539 		size_t nsending;
    540 		bool tcp_nodelay_value;
    541 		isc_nmsocket_tls_send_req_t *send_req; /*%< Send req to reuse */
    542 		bool reading;
    543 	} tlsstream;
    544 
    545 #if HAVE_LIBNGHTTP2
    546 	isc_nmsocket_h2_t *h2;
    547 #endif /* HAVE_LIBNGHTTP2 */
    548 
    549 	struct {
    550 		isc_dnsstream_assembler_t *input;
    551 		bool reading;
    552 		isc_nmsocket_t *listener;
    553 		isc_nmsocket_t *sock;
    554 		size_t nsending;
    555 		void *send_req;
    556 		bool dot_alpn_negotiated;
    557 		const char *tls_verify_error;
    558 	} streamdns;
    559 
    560 	struct {
    561 		isc_nmsocket_t *sock;
    562 		bool reading;
    563 		size_t nsending;
    564 		void *send_req;
    565 		union {
    566 			isc_proxy2_handler_t *handler; /* server */
    567 			isc_buffer_t *outbuf;	       /* client */
    568 		} proxy2;
    569 		bool header_processed;
    570 		bool extra_processed; /* data arrived past header processed */
    571 		isc_nmsocket_t **udp_server_socks; /* UDP sockets */
    572 		size_t udp_server_socks_num;
    573 	} proxy;
    574 
    575 	/*%
    576 	 * pquota is a non-attached pointer to the TCP client quota, stored in
    577 	 * listening sockets.
    578 	 */
    579 	isc_quota_t *pquota;
    580 	isc_job_t quotacb;
    581 
    582 	/*%
    583 	 * Socket statistics
    584 	 */
    585 	const isc_statscounter_t *statsindex;
    586 
    587 	/*%
    588 	 * TCP read/connect timeout timers.
    589 	 */
    590 	uv_timer_t read_timer;
    591 	uint64_t read_timeout;
    592 	uint64_t connect_timeout;
    593 
    594 	/*%
    595 	 * TCP write timeout timer.
    596 	 */
    597 	uint64_t write_timeout;
    598 
    599 	/*
    600 	 * Reading was throttled over TCP as the peer does not read the
    601 	 * data we are sending back.
    602 	 */
    603 	bool reading_throttled;
    604 
    605 	/*% outer socket is for 'wrapped' sockets - e.g. tcpdns in tcp */
    606 	isc_nmsocket_t *outer;
    607 
    608 	/*% server socket for connections */
    609 	isc_nmsocket_t *server;
    610 
    611 	/*% client socket for connections */
    612 	isc_nmsocket_t *listener;
    613 
    614 	/*% Child sockets for multi-socket setups */
    615 	isc_nmsocket_t *children;
    616 	uint_fast32_t nchildren;
    617 	isc_sockaddr_t iface;
    618 	isc_nmhandle_t *statichandle;
    619 	isc_nmhandle_t *outerhandle;
    620 
    621 	/*% TCP backlog */
    622 	int backlog;
    623 
    624 	/*% libuv data */
    625 	uv_os_sock_t fd;
    626 	union uv_any_handle uv_handle;
    627 
    628 	/*% Peer address */
    629 	isc_sockaddr_t peer;
    630 
    631 	/*%
    632 	 * Socket is active if it's listening, working, etc. If it's
    633 	 * closing, then it doesn't make a sense, for example, to
    634 	 * push handles or reqs for reuse.
    635 	 */
    636 	bool active;
    637 	bool destroying;
    638 
    639 	bool route_sock;
    640 
    641 	/*%
    642 	 * Socket is closed if it's not active and all the possible
    643 	 * callbacks were fired, there are no active handles, etc.
    644 	 * If active==false but closed==false, that means the socket
    645 	 * is closing.
    646 	 */
    647 	bool closing;
    648 	bool closed;
    649 	bool connecting;
    650 	bool connected;
    651 	bool accepting;
    652 	bool reading;
    653 	bool timedout;
    654 
    655 	/*%
    656 	 * A timestamp of when the connection acceptance was delayed due
    657 	 * to quota.
    658 	 */
    659 	isc_nanosecs_t quota_accept_ts;
    660 
    661 	/*%
    662 	 * Established an outgoing connection, as client not server.
    663 	 */
    664 	bool client;
    665 
    666 	/*%
    667 	 * The socket is processing read callback, this is guard to not read
    668 	 * data before the readcb is back.
    669 	 */
    670 	bool processing;
    671 
    672 	/*%
    673 	 * A TCP or TCPDNS socket has been set to use the keepalive
    674 	 * timeout instead of the default idle timeout.
    675 	 */
    676 	bool keepalive;
    677 
    678 	/*%
    679 	 * 'spare' handles for that can be reused to avoid allocations, for UDP.
    680 	 */
    681 	ISC_LIST(isc_nmhandle_t) inactive_handles;
    682 
    683 	size_t inactive_handles_cur;
    684 	size_t inactive_handles_max;
    685 
    686 	/*%
    687 	 * 'active' handles and uvreqs, mostly for debugging purposes.
    688 	 */
    689 	ISC_LIST(isc_nmhandle_t) active_handles;
    690 	ISC_LIST(isc__nm_uvreq_t) active_uvreqs;
    691 
    692 	size_t active_handles_cur;
    693 	size_t active_handles_max;
    694 
    695 	/*%
    696 	 * Used to pass a result back from listen or connect events.
    697 	 */
    698 	isc_result_t result;
    699 
    700 	/*%
    701 	 * This function will be called with handle->sock
    702 	 * as the argument whenever a handle's references drop
    703 	 * to zero, after its reset callback has been called.
    704 	 */
    705 	isc_nm_closehandlecb_t closehandle_cb;
    706 
    707 	isc_nmhandle_t *recv_handle;
    708 	isc_nm_recv_cb_t recv_cb;
    709 	void *recv_cbarg;
    710 
    711 	isc_nm_cb_t connect_cb;
    712 	void *connect_cbarg;
    713 
    714 	isc_nm_accept_cb_t accept_cb;
    715 	void *accept_cbarg;
    716 
    717 	bool barriers_initialised;
    718 	bool manual_read_timer;
    719 #if ISC_NETMGR_TRACE
    720 	void *backtrace[TRACE_SIZE];
    721 	int backtrace_size;
    722 #endif
    723 	LINK(isc_nmsocket_t) active_link;
    724 
    725 	isc_job_t job;
    726 };
    727 
    728 void
    729 isc__nm_free_uvbuf(isc_nmsocket_t *sock, const uv_buf_t *buf);
    730 /*%<
    731  * Free a buffer allocated for a receive operation.
    732  *
    733  * Note that as currently implemented, this doesn't actually
    734  * free anything, marks the isc__networker's UDP receive buffer
    735  * as "not in use".
    736  */
    737 
    738 isc_nmhandle_t *
    739 isc___nmhandle_get(isc_nmsocket_t *sock, isc_sockaddr_t const *peer,
    740 		   isc_sockaddr_t const *local FLARG);
    741 /*%<
    742  * Get a handle for the socket 'sock', allocating a new one
    743  * if there isn't one available in 'sock->inactivehandles'.
    744  *
    745  * If 'peer' is not NULL, set the handle's peer address to 'peer',
    746  * otherwise set it to 'sock->peer'.
    747  *
    748  * If 'local' is not NULL, set the handle's local address to 'local',
    749  * otherwise set it to 'sock->iface->addr'.
    750  *
    751  * 'sock' will be attached to 'handle->sock'. The caller may need
    752  * to detach the socket afterward.
    753  */
    754 
    755 isc__nm_uvreq_t *
    756 isc___nm_uvreq_get(isc_nmsocket_t *sock FLARG);
    757 /*%<
    758  * Get a UV request structure for the socket 'sock', allocating a
    759  * new one if there isn't one available in 'sock->inactivereqs'.
    760  */
    761 
    762 void
    763 isc___nm_uvreq_put(isc__nm_uvreq_t **req FLARG);
    764 /*%<
    765  * Completes the use of a UV request structure, setting '*req' to NULL.
    766  *
    767  * The UV request is pushed onto the 'sock->inactivereqs' stack or,
    768  * if that doesn't work, freed.
    769  */
    770 
    771 void
    772 isc___nmsocket_init(isc_nmsocket_t *sock, isc__networker_t *worker,
    773 		    isc_nmsocket_type type, isc_sockaddr_t *iface,
    774 		    isc_nmsocket_t *parent FLARG);
    775 /*%<
    776  * Initialize socket 'sock', attach it to 'mgr', and set it to type 'type'
    777  * and its interface to 'iface'.
    778  */
    779 
    780 void
    781 isc___nmsocket_attach(isc_nmsocket_t *sock, isc_nmsocket_t **target FLARG);
    782 /*%<
    783  * Attach to a socket, increasing refcount
    784  */
    785 
    786 void
    787 isc___nmsocket_detach(isc_nmsocket_t **socketp FLARG);
    788 /*%<
    789  * Detach from socket, decreasing refcount and possibly destroying the
    790  * socket if it's no longer referenced.
    791  */
    792 
    793 void
    794 isc___nmsocket_prep_destroy(isc_nmsocket_t *sock FLARG);
    795 /*%<
    796  * Market 'sock' as inactive, close it if necessary, and destroy it
    797  * if there are no remaining references or active handles.
    798  */
    799 
    800 void
    801 isc__nmsocket_shutdown(isc_nmsocket_t *sock);
    802 /*%<
    803  * Initiate the socket shutdown which actively calls the active
    804  * callbacks.
    805  */
    806 
    807 void
    808 isc__nmsocket_reset(isc_nmsocket_t *sock);
    809 /*%<
    810  * Reset and close the socket.
    811  */
    812 
    813 bool
    814 isc__nmsocket_active(isc_nmsocket_t *sock);
    815 /*%<
    816  * Determine whether 'sock' is active by checking 'sock->active'
    817  * or, for child sockets, 'sock->parent->active'.
    818  */
    819 
    820 void
    821 isc__nmsocket_clearcb(isc_nmsocket_t *sock);
    822 /*%<
    823  * Clear the recv and accept callbacks in 'sock'.
    824  */
    825 
    826 void
    827 isc__nmsocket_timer_stop(isc_nmsocket_t *sock);
    828 void
    829 isc__nmsocket_timer_start(isc_nmsocket_t *sock);
    830 void
    831 isc__nmsocket_timer_restart(isc_nmsocket_t *sock);
    832 bool
    833 isc__nmsocket_timer_running(isc_nmsocket_t *sock);
    834 /*%<
    835  * Start/stop/restart/check the timeout on the socket
    836  */
    837 
    838 void
    839 isc__nm_connectcb(isc_nmsocket_t *sock, isc__nm_uvreq_t *uvreq,
    840 		  isc_result_t eresult, bool async);
    841 
    842 void
    843 isc__nm_readcb(isc_nmsocket_t *sock, isc__nm_uvreq_t *uvreq,
    844 	       isc_result_t eresult, bool async);
    845 /*%<
    846  * Issue a read callback on the socket, used to call the callback
    847  * on failed conditions when the event can't be scheduled on the uv loop.
    848  *
    849  */
    850 
    851 void
    852 isc__nm_sendcb(isc_nmsocket_t *sock, isc__nm_uvreq_t *uvreq,
    853 	       isc_result_t eresult, bool async);
    854 /*%<
    855  * Issue a write callback on the socket, used to call the callback
    856  * on failed conditions when the event can't be scheduled on the uv loop.
    857  */
    858 
    859 void
    860 isc__nm_udp_send(isc_nmhandle_t *handle, const isc_region_t *region,
    861 		 isc_nm_cb_t cb, void *cbarg);
    862 /*%<
    863  * Back-end implementation of isc_nm_send() for UDP handles.
    864  */
    865 
    866 void
    867 isc__nm_udp_read(isc_nmhandle_t *handle, isc_nm_recv_cb_t cb, void *cbarg);
    868 /*
    869  * Back-end implementation of isc_nm_read() for UDP handles.
    870  */
    871 
    872 void
    873 isc__nm_udp_close(isc_nmsocket_t *sock);
    874 /*%<
    875  * Close a UDP socket.
    876  */
    877 
    878 void
    879 isc__nm_udp_shutdown(isc_nmsocket_t *sock);
    880 /*%<
    881  * Called during the shutdown process to close and clean up connected
    882  * sockets.
    883  */
    884 
    885 void
    886 isc__nm_udp_stoplistening(isc_nmsocket_t *sock);
    887 /*%<
    888  * Stop listening on 'sock'.
    889  */
    890 
    891 void
    892 isc__nm_udp_settimeout(isc_nmhandle_t *handle, uint32_t timeout);
    893 /*%<
    894  * Set or clear the recv timeout for the UDP socket associated with 'handle'.
    895  */
    896 
    897 void
    898 isc__nm_tcp_send(isc_nmhandle_t *handle, const isc_region_t *region,
    899 		 isc_nm_cb_t cb, void *cbarg);
    900 /*%<
    901  * Back-end implementation of isc_nm_send() for TCP handles.
    902  */
    903 
    904 void
    905 isc__nm_tcp_read(isc_nmhandle_t *handle, isc_nm_recv_cb_t cb, void *cbarg);
    906 /*
    907  * Start reading on this handle.
    908  */
    909 
    910 void
    911 isc__nm_tcp_close(isc_nmsocket_t *sock);
    912 /*%<
    913  * Close a TCP socket.
    914  */
    915 void
    916 isc__nm_tcp_read_stop(isc_nmhandle_t *handle);
    917 /*%<
    918  * Stop reading on this handle.
    919  */
    920 
    921 void
    922 isc__nm_tcp_shutdown(isc_nmsocket_t *sock);
    923 /*%<
    924  * Called during the shutdown process to close and clean up connected
    925  * sockets.
    926  */
    927 
    928 void
    929 isc__nm_tcp_stoplistening(isc_nmsocket_t *sock);
    930 /*%<
    931  * Stop listening on 'sock'.
    932  */
    933 
    934 void
    935 isc__nm_tcp_settimeout(isc_nmhandle_t *handle, uint32_t timeout);
    936 /*%<
    937  * Set the read timeout for the TCP socket associated with 'handle'.
    938  */
    939 
    940 void
    941 isc__nmhandle_tcp_set_manual_timer(isc_nmhandle_t *handle, const bool manual);
    942 
    943 void
    944 isc__nm_tcp_senddns(isc_nmhandle_t *handle, const isc_region_t *region,
    945 		    isc_nm_cb_t cb, void *cbarg);
    946 /*%<
    947  * The same as 'isc__nm_tcp_send()', but with data length sent
    948  * ahead of data (two bytes (16 bit) in big-endian format).
    949  */
    950 
    951 void
    952 isc__nm_tls_send(isc_nmhandle_t *handle, const isc_region_t *region,
    953 		 isc_nm_cb_t cb, void *cbarg);
    954 
    955 /*%<
    956  * Back-end implementation of isc_nm_send() for TLSDNS handles.
    957  */
    958 
    959 void
    960 isc__nm_tls_senddns(isc_nmhandle_t *handle, const isc_region_t *region,
    961 		    isc_nm_cb_t cb, void *cbarg);
    962 /*%<
    963  * The same as 'isc__nm_tls_send()', but with data length sent
    964  * ahead of data (two bytes (16 bit) in big-endian format).
    965  */
    966 
    967 void
    968 isc__nm_tls_read(isc_nmhandle_t *handle, isc_nm_recv_cb_t cb, void *cbarg);
    969 /*%<
    970  * Start reading on the TLS handle.
    971  */
    972 
    973 void
    974 isc__nm_tls_close(isc_nmsocket_t *sock);
    975 /*%<
    976  * Close a TLS socket.
    977  */
    978 
    979 void
    980 isc__nm_tls_read_stop(isc_nmhandle_t *handle);
    981 /*%<
    982  * Stop reading on the TLS handle.
    983  */
    984 
    985 void
    986 isc__nm_tls_cleanup_data(isc_nmsocket_t *sock);
    987 
    988 void
    989 isc__nm_tls_stoplistening(isc_nmsocket_t *sock);
    990 
    991 void
    992 isc__nm_tls_settimeout(isc_nmhandle_t *handle, uint32_t timeout);
    993 void
    994 isc__nm_tls_cleartimeout(isc_nmhandle_t *handle);
    995 /*%<
    996  * Set the read timeout and reset the timer for the socket
    997  * associated with 'handle', and the TCP socket it wraps
    998  * around.
    999  */
   1000 
   1001 void
   1002 isc__nmsocket_tls_reset(isc_nmsocket_t *sock);
   1003 
   1004 void
   1005 isc__nmhandle_tls_set_manual_timer(isc_nmhandle_t *handle, const bool manual);
   1006 
   1007 const char *
   1008 isc__nm_tls_verify_tls_peer_result_string(const isc_nmhandle_t *handle);
   1009 
   1010 void
   1011 isc__nmhandle_tls_keepalive(isc_nmhandle_t *handle, bool value);
   1012 /*%<
   1013  * Set the keepalive value on the underlying TCP handle.
   1014  */
   1015 
   1016 void
   1017 isc__nm_async_tls_set_tlsctx(isc_nmsocket_t *listener, isc_tlsctx_t *tlsctx,
   1018 			     const int tid);
   1019 
   1020 void
   1021 isc__nmhandle_tls_setwritetimeout(isc_nmhandle_t *handle,
   1022 				  uint64_t write_timeout);
   1023 
   1024 bool
   1025 isc__nmsocket_tls_timer_running(isc_nmsocket_t *sock);
   1026 
   1027 void
   1028 isc__nmsocket_tls_timer_restart(isc_nmsocket_t *sock);
   1029 
   1030 void
   1031 isc__nmsocket_tls_timer_stop(isc_nmsocket_t *sock);
   1032 
   1033 void
   1034 isc__nm_tls_failed_read_cb(isc_nmsocket_t *sock, isc_result_t result,
   1035 			   bool async);
   1036 
   1037 void
   1038 isc__nmhandle_tls_get_selected_alpn(isc_nmhandle_t *handle,
   1039 				    const unsigned char **alpn,
   1040 				    unsigned int *alpnlen);
   1041 
   1042 isc_result_t
   1043 isc__nmhandle_tls_set_tcp_nodelay(isc_nmhandle_t *handle, const bool value);
   1044 
   1045 #if HAVE_LIBNGHTTP2
   1046 
   1047 void
   1048 isc__nm_http_stoplistening(isc_nmsocket_t *sock);
   1049 
   1050 void
   1051 isc__nm_http_settimeout(isc_nmhandle_t *handle, uint32_t timeout);
   1052 void
   1053 isc__nm_http_cleartimeout(isc_nmhandle_t *handle);
   1054 /*%<
   1055  * Set the read timeout and reset the timer for the socket
   1056  * associated with 'handle', and the TLS/TCP socket it wraps
   1057  * around.
   1058  */
   1059 
   1060 void
   1061 isc__nmhandle_http_keepalive(isc_nmhandle_t *handle, bool value);
   1062 /*%<
   1063  * Set the keepalive value on the underlying session handle
   1064  */
   1065 
   1066 void
   1067 isc__nm_http_cleanup_data(isc_nmsocket_t *sock);
   1068 
   1069 isc_result_t
   1070 isc__nm_http_request(isc_nmhandle_t *handle, isc_region_t *region,
   1071 		     isc_nm_recv_cb_t reply_cb, void *cbarg);
   1072 
   1073 void
   1074 isc__nm_http_send(isc_nmhandle_t *handle, const isc_region_t *region,
   1075 		  isc_nm_cb_t cb, void *cbarg);
   1076 
   1077 void
   1078 isc__nm_http_read(isc_nmhandle_t *handle, isc_nm_recv_cb_t cb, void *cbarg);
   1079 
   1080 void
   1081 isc__nm_http_close(isc_nmsocket_t *sock);
   1082 
   1083 void
   1084 isc__nm_http_bad_request(isc_nmhandle_t *handle);
   1085 /*%<
   1086  * Respond to the request with 400 "Bad Request" status.
   1087  *
   1088  * Requires:
   1089  * \li 'handle' is a valid HTTP netmgr handle object, referencing a server-side
   1090  * socket
   1091  */
   1092 
   1093 bool
   1094 isc__nm_http_has_encryption(const isc_nmhandle_t *handle);
   1095 
   1096 void
   1097 isc__nm_http_set_maxage(isc_nmhandle_t *handle, const uint32_t ttl);
   1098 
   1099 const char *
   1100 isc__nm_http_verify_tls_peer_result_string(const isc_nmhandle_t *handle);
   1101 
   1102 bool
   1103 isc__nm_parse_httpquery(const char *query_string, const char **start,
   1104 			size_t *len);
   1105 
   1106 char *
   1107 isc__nm_base64url_to_base64(isc_mem_t *mem, const char *base64url,
   1108 			    const size_t base64url_len, size_t *res_len);
   1109 
   1110 char *
   1111 isc__nm_base64_to_base64url(isc_mem_t *mem, const char *base64,
   1112 			    const size_t base64_len, size_t *res_len);
   1113 
   1114 void
   1115 isc__nm_httpsession_attach(isc_nm_http_session_t *source,
   1116 			   isc_nm_http_session_t **targetp);
   1117 void
   1118 isc__nm_httpsession_detach(isc_nm_http_session_t **sessionp);
   1119 
   1120 isc_nmhandle_t *
   1121 isc__nm_httpsession_handle(isc_nm_http_session_t *session);
   1122 
   1123 void
   1124 isc__nm_http_set_tlsctx(isc_nmsocket_t *sock, isc_tlsctx_t *tlsctx);
   1125 
   1126 void
   1127 isc__nm_http_set_max_streams(isc_nmsocket_t *listener,
   1128 			     const uint32_t max_concurrent_streams);
   1129 
   1130 #endif
   1131 
   1132 void
   1133 isc__nm_streamdns_read(isc_nmhandle_t *handle, isc_nm_recv_cb_t cb,
   1134 		       void *cbarg);
   1135 
   1136 void
   1137 isc__nm_streamdns_send(isc_nmhandle_t *handle, const isc_region_t *region,
   1138 		       isc_nm_cb_t cb, void *cbarg);
   1139 
   1140 void
   1141 isc__nm_streamdns_close(isc_nmsocket_t *sock);
   1142 
   1143 void
   1144 isc__nm_streamdns_stoplistening(isc_nmsocket_t *sock);
   1145 
   1146 void
   1147 isc__nm_streamdns_cleanup_data(isc_nmsocket_t *sock);
   1148 
   1149 void
   1150 isc__nmhandle_streamdns_cleartimeout(isc_nmhandle_t *handle);
   1151 
   1152 void
   1153 isc__nmhandle_streamdns_settimeout(isc_nmhandle_t *handle, uint32_t timeout);
   1154 
   1155 void
   1156 isc__nmhandle_streamdns_keepalive(isc_nmhandle_t *handle, bool value);
   1157 
   1158 void
   1159 isc__nmhandle_streamdns_setwritetimeout(isc_nmhandle_t *handle,
   1160 					uint32_t timeout);
   1161 
   1162 bool
   1163 isc__nm_streamdns_has_encryption(const isc_nmhandle_t *handle);
   1164 
   1165 const char *
   1166 isc__nm_streamdns_verify_tls_peer_result_string(const isc_nmhandle_t *handle);
   1167 
   1168 void
   1169 isc__nm_streamdns_set_tlsctx(isc_nmsocket_t *listener, isc_tlsctx_t *tlsctx);
   1170 
   1171 isc_result_t
   1172 isc__nm_streamdns_xfr_checkperm(isc_nmsocket_t *sock);
   1173 
   1174 void
   1175 isc__nmsocket_streamdns_reset(isc_nmsocket_t *sock);
   1176 
   1177 bool
   1178 isc__nmsocket_streamdns_timer_running(isc_nmsocket_t *sock);
   1179 
   1180 void
   1181 isc__nmsocket_streamdns_timer_stop(isc_nmsocket_t *sock);
   1182 
   1183 void
   1184 isc__nmsocket_streamdns_timer_restart(isc_nmsocket_t *sock);
   1185 
   1186 void
   1187 isc__nm_streamdns_failed_read_cb(isc_nmsocket_t *sock, isc_result_t result,
   1188 				 bool async);
   1189 
   1190 bool
   1191 isc__nm_valid_proxy_addresses(const isc_sockaddr_t *src,
   1192 			      const isc_sockaddr_t *dst);
   1193 
   1194 void
   1195 isc__nm_proxystream_failed_read_cb(isc_nmsocket_t *sock, isc_result_t result,
   1196 				   bool async);
   1197 
   1198 void
   1199 isc__nm_proxystream_stoplistening(isc_nmsocket_t *sock);
   1200 
   1201 void
   1202 isc__nm_proxystream_cleanup_data(isc_nmsocket_t *sock);
   1203 
   1204 void
   1205 isc__nmhandle_proxystream_cleartimeout(isc_nmhandle_t *handle);
   1206 
   1207 void
   1208 isc__nmhandle_proxystream_settimeout(isc_nmhandle_t *handle, uint32_t timeout);
   1209 
   1210 void
   1211 isc__nmhandle_proxystream_keepalive(isc_nmhandle_t *handle, bool value);
   1212 
   1213 void
   1214 isc__nmhandle_proxystream_setwritetimeout(isc_nmhandle_t *handle,
   1215 					  uint64_t write_timeout);
   1216 
   1217 void
   1218 isc__nmsocket_proxystream_reset(isc_nmsocket_t *sock);
   1219 
   1220 bool
   1221 isc__nmsocket_proxystream_timer_running(isc_nmsocket_t *sock);
   1222 
   1223 void
   1224 isc__nmsocket_proxystream_timer_restart(isc_nmsocket_t *sock);
   1225 
   1226 void
   1227 isc__nmsocket_proxystream_timer_stop(isc_nmsocket_t *sock);
   1228 
   1229 void
   1230 isc__nmhandle_proxystream_set_manual_timer(isc_nmhandle_t *handle,
   1231 					   const bool manual);
   1232 
   1233 isc_result_t
   1234 isc__nmhandle_proxystream_set_tcp_nodelay(isc_nmhandle_t *handle,
   1235 					  const bool value);
   1236 
   1237 void
   1238 isc__nm_proxystream_read_stop(isc_nmhandle_t *handle);
   1239 
   1240 void
   1241 isc__nm_proxystream_close(isc_nmsocket_t *sock);
   1242 
   1243 void
   1244 isc__nm_proxystream_read(isc_nmhandle_t *handle, isc_nm_recv_cb_t cb,
   1245 			 void *cbarg);
   1246 
   1247 void
   1248 isc__nm_proxystream_send(isc_nmhandle_t *handle, isc_region_t *region,
   1249 			 isc_nm_cb_t cb, void *cbarg);
   1250 
   1251 void
   1252 isc__nm_proxystream_senddns(isc_nmhandle_t *handle, isc_region_t *region,
   1253 			    isc_nm_cb_t cb, void *cbarg);
   1254 
   1255 void
   1256 isc__nm_proxystream_set_tlsctx(isc_nmsocket_t *listener, isc_tlsctx_t *tlsctx);
   1257 
   1258 bool
   1259 isc__nm_proxystream_has_encryption(const isc_nmhandle_t *handle);
   1260 
   1261 const char *
   1262 isc__nm_proxystream_verify_tls_peer_result_string(const isc_nmhandle_t *handle);
   1263 
   1264 void
   1265 isc__nmhandle_proxystream_get_selected_alpn(isc_nmhandle_t *handle,
   1266 					    const unsigned char **alpn,
   1267 					    unsigned int *alpnlen);
   1268 
   1269 void
   1270 isc__nm_proxyudp_failed_read_cb(isc_nmsocket_t *sock, const isc_result_t result,
   1271 				const bool async);
   1272 
   1273 void
   1274 isc__nm_proxyudp_stoplistening(isc_nmsocket_t *listener);
   1275 
   1276 void
   1277 isc__nm_proxyudp_cleanup_data(isc_nmsocket_t *sock);
   1278 
   1279 void
   1280 isc__nmhandle_proxyudp_cleartimeout(isc_nmhandle_t *handle);
   1281 
   1282 void
   1283 isc__nmhandle_proxyudp_settimeout(isc_nmhandle_t *handle, uint32_t timeout);
   1284 
   1285 void
   1286 isc__nmhandle_proxyudp_setwritetimeout(isc_nmhandle_t *handle,
   1287 				       uint64_t write_timeout);
   1288 
   1289 bool
   1290 isc__nmsocket_proxyudp_timer_running(isc_nmsocket_t *sock);
   1291 
   1292 void
   1293 isc__nmsocket_proxyudp_timer_restart(isc_nmsocket_t *sock);
   1294 
   1295 void
   1296 isc__nmsocket_proxyudp_timer_stop(isc_nmsocket_t *sock);
   1297 
   1298 void
   1299 isc__nm_proxyudp_close(isc_nmsocket_t *sock);
   1300 
   1301 void
   1302 isc__nm_proxyudp_read(isc_nmhandle_t *handle, isc_nm_recv_cb_t cb, void *cbarg);
   1303 
   1304 void
   1305 isc__nm_proxyudp_send(isc_nmhandle_t *handle, isc_region_t *region,
   1306 		      isc_nm_cb_t cb, void *cbarg);
   1307 
   1308 void
   1309 isc__nm_incstats(isc_nmsocket_t *sock, isc__nm_statid_t id);
   1310 /*%<
   1311  * Increment socket-related statistics counters.
   1312  */
   1313 
   1314 void
   1315 isc__nm_decstats(isc_nmsocket_t *sock, isc__nm_statid_t id);
   1316 /*%<
   1317  * Decrement socket-related statistics counters.
   1318  */
   1319 
   1320 isc_result_t
   1321 isc__nm_socket(int domain, int type, int protocol, uv_os_sock_t *sockp);
   1322 /*%<
   1323  * Platform independent socket() version
   1324  */
   1325 
   1326 void
   1327 isc__nm_closesocket(uv_os_sock_t sock);
   1328 /*%<
   1329  * Platform independent closesocket() version
   1330  */
   1331 
   1332 isc_result_t
   1333 isc__nm_socket_reuse(uv_os_sock_t fd, int val);
   1334 /*%<
   1335  * Set the SO_REUSEADDR or SO_REUSEPORT (or equivalent) socket option on the fd
   1336  */
   1337 
   1338 isc_result_t
   1339 isc__nm_socket_reuse_lb(uv_os_sock_t fd);
   1340 /*%<
   1341  * Set the SO_REUSEPORT_LB (or equivalent) socket option on the fd
   1342  */
   1343 
   1344 isc_result_t
   1345 isc__nm_socket_disable_pmtud(uv_os_sock_t fd, sa_family_t sa_family);
   1346 /*%<
   1347  * Disable the Path MTU Discovery, either by disabling IP(V6)_DONTFRAG socket
   1348  * option, or setting the IP(V6)_MTU_DISCOVER socket option to IP_PMTUDISC_OMIT
   1349  */
   1350 
   1351 isc_result_t
   1352 isc__nm_socket_v6only(uv_os_sock_t fd, sa_family_t sa_family);
   1353 /*%<
   1354  * Restrict the socket to sending and receiving IPv6 packets only
   1355  */
   1356 
   1357 isc_result_t
   1358 isc__nm_socket_connectiontimeout(uv_os_sock_t fd, int timeout_ms);
   1359 /*%<
   1360  * Set the connection timeout in milliseconds, on non-Linux platforms,
   1361  * the minimum value must be at least 1000 (1 second).
   1362  */
   1363 
   1364 isc_result_t
   1365 isc__nm_socket_tcp_nodelay(const uv_os_sock_t fd, bool value);
   1366 /*%<
   1367  * Disables/Enables Nagle's algorithm on a TCP socket (sets TCP_NODELAY if
   1368  * 'value' equals 'true' or vice versa).
   1369  */
   1370 
   1371 isc_result_t
   1372 isc__nm_socket_tcp_maxseg(uv_os_sock_t fd, int size);
   1373 /*%<
   1374  * Set the TCP maximum segment size
   1375  */
   1376 
   1377 isc_result_t
   1378 isc__nm_socket_min_mtu(uv_os_sock_t fd, sa_family_t sa_family);
   1379 /*%<
   1380  * Use minimum MTU on IPv6 sockets
   1381  */
   1382 
   1383 isc_result_t
   1384 isc__nm_socket_max_port_range(uv_os_sock_t fd ISC_ATTR_UNUSED,
   1385 			      sa_family_t sa_family ISC_ATTR_UNUSED,
   1386 			      in_port_t port_low, in_port_t port_high);
   1387 /*%<
   1388  * Set IP_BIND_ADDRESS_NO_PORT and IP_LOCAL_PORT_RANGE on the socket
   1389  * (Linux only).
   1390  */
   1391 
   1392 void
   1393 isc__nm_set_network_buffers(isc_nm_t *nm, uv_handle_t *handle);
   1394 /*%>
   1395  * Sets the pre-configured network buffers size on the handle.
   1396  */
   1397 
   1398 void
   1399 isc__nmsocket_barrier_init(isc_nmsocket_t *listener);
   1400 /*%>
   1401  * Initialise the socket synchronisation barrier according to the
   1402  * number of children.
   1403  */
   1404 
   1405 void
   1406 isc__nmsocket_stop(isc_nmsocket_t *listener);
   1407 /*%>
   1408  * Broadcast "stop" event for a listener socket across all workers and
   1409  * wait its processing completion - then, stop and close the underlying
   1410  * transport listener socket.
   1411  *
   1412  * The primitive is used in multi-layer transport listener sockets to
   1413  * implement shutdown properly: after the broadcasted events has been
   1414  * processed it is safe to destroy the shared data within the listener
   1415  * socket (including shutting down the underlying transport listener
   1416  * socket).
   1417  */
   1418 
   1419 void
   1420 isc__nm_udp_failed_read_cb(isc_nmsocket_t *sock, isc_result_t result,
   1421 			   bool async);
   1422 void
   1423 isc__nm_tcp_failed_read_cb(isc_nmsocket_t *sock, isc_result_t result,
   1424 			   bool async);
   1425 
   1426 isc__nm_uvreq_t *
   1427 isc___nm_get_read_req(isc_nmsocket_t *sock, isc_sockaddr_t *sockaddr FLARG);
   1428 
   1429 void
   1430 isc__nm_alloc_cb(uv_handle_t *handle, size_t size, uv_buf_t *buf);
   1431 
   1432 void
   1433 isc__nm_udp_read_cb(uv_udp_t *handle, ssize_t nrecv, const uv_buf_t *buf,
   1434 		    const struct sockaddr *addr, unsigned int flags);
   1435 void
   1436 isc__nm_tcp_read_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf);
   1437 
   1438 isc_result_t
   1439 isc__nm_start_reading(isc_nmsocket_t *sock);
   1440 void
   1441 isc__nm_stop_reading(isc_nmsocket_t *sock);
   1442 bool
   1443 isc__nmsocket_closing(isc_nmsocket_t *sock);
   1444 bool
   1445 isc__nm_closing(isc__networker_t *worker);
   1446 
   1447 void
   1448 isc__nm_failed_send_cb(isc_nmsocket_t *sock, isc__nm_uvreq_t *req,
   1449 		       isc_result_t eresult, bool async);
   1450 void
   1451 isc__nm_failed_connect_cb(isc_nmsocket_t *sock, isc__nm_uvreq_t *req,
   1452 			  isc_result_t eresult, bool async);
   1453 void
   1454 isc__nm_failed_read_cb(isc_nmsocket_t *sock, isc_result_t result, bool async);
   1455 
   1456 void
   1457 isc__nm_accept_connection_log(isc_nmsocket_t *sock, isc_result_t result,
   1458 			      bool can_log_quota);
   1459 
   1460 /*
   1461  * Timeout callbacks
   1462  */
   1463 void
   1464 isc__nmsocket_connecttimeout_cb(uv_timer_t *timer);
   1465 void
   1466 isc__nmsocket_readtimeout_cb(uv_timer_t *timer);
   1467 void
   1468 isc__nmsocket_writetimeout_cb(void *data, isc_result_t eresult);
   1469 
   1470 /*
   1471  * Bind to the socket, but allow binding to IPv6 tentative addresses reported by
   1472  * the route socket by setting IP_FREEBIND (or equivalent).
   1473  */
   1474 int
   1475 isc__nm_udp_freebind(uv_udp_t *handle, const struct sockaddr *addr,
   1476 		     unsigned int flags);
   1477 
   1478 int
   1479 isc__nm_tcp_freebind(uv_tcp_t *handle, const struct sockaddr *addr,
   1480 		     unsigned int flags);
   1481 
   1482 void
   1483 isc__nmsocket_log_tls_session_reuse(isc_nmsocket_t *sock, isc_tls_t *tls);
   1484 
   1485 /*
   1486  * Logging helpers
   1487  */
   1488 void
   1489 isc__netmgr_log(const isc_nm_t *netmgr, int level, const char *fmt, ...)
   1490 	ISC_FORMAT_PRINTF(3, 4);
   1491 void
   1492 isc__nmsocket_log(const isc_nmsocket_t *sock, int level, const char *fmt, ...)
   1493 	ISC_FORMAT_PRINTF(3, 4);
   1494 void
   1495 isc__nmhandle_log(const isc_nmhandle_t *handle, int level, const char *fmt, ...)
   1496 	ISC_FORMAT_PRINTF(3, 4);
   1497 
   1498 void
   1499 isc__nm_received_proxy_header_log(isc_nmhandle_t *handle,
   1500 				  const isc_proxy2_command_t cmd,
   1501 				  const int socktype,
   1502 				  const isc_sockaddr_t *restrict src_addr,
   1503 				  const isc_sockaddr_t *restrict dst_addr,
   1504 				  const isc_region_t *restrict tlvs);
   1505 
   1506 void
   1507 isc__nmhandle_set_manual_timer(isc_nmhandle_t *handle, const bool manual);
   1508 /*
   1509  * Set manual read timer control mode - so that it will not get reset
   1510  * automatically on read nor get started when read is initiated.
   1511  */
   1512 
   1513 void
   1514 isc__nmhandle_get_selected_alpn(isc_nmhandle_t *handle,
   1515 				const unsigned char **alpn,
   1516 				unsigned int *alpnlen);
   1517 /*
   1518  * Returns a non zero terminated ALPN identifier via 'alpn'. The
   1519  * length of the identifier is returned via 'alpnlen'. If after the
   1520  * call either 'alpn == NULL' or 'alpnlen == 0', then identifier was
   1521  * not negotiated of the underlying protocol of the connection
   1522  * represented via the given handle does not support ALPN.
   1523  */
   1524 
   1525 void
   1526 isc__nm_senddns(isc_nmhandle_t *handle, isc_region_t *region, isc_nm_cb_t cb,
   1527 		void *cbarg);
   1528 /*%<
   1529  * The same as 'isc_nm_send()', but with data length sent
   1530  * ahead of data (two bytes (16 bit) in big-endian format).
   1531  */
   1532