1 /* 2 * util/netevent.h - event notification 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 contains event notification functions. 40 * 41 * There are three types of communication points 42 * o UDP socket - perthread buffer. 43 * o TCP-accept socket - array of TCP-sockets, socketcount. 44 * o TCP socket - own buffer, parent-TCPaccept, read/write state, 45 * number of bytes read/written, timeout. 46 * 47 * There are sockets aimed towards our clients and towards the internet. 48 * o frontside - aimed towards our clients, queries come in, answers back. 49 * o behind - aimed towards internet, to the authoritative DNS servers. 50 * 51 * Several event types are available: 52 * o comm_base - for thread safety of the comm points, one per thread. 53 * o comm_point - udp and tcp networking, with callbacks. 54 * o comm_timer - a timeout with callback. 55 * o comm_signal - callbacks when signal is caught. 56 * o comm_reply - holds reply info during networking callback. 57 * 58 */ 59 60 #ifndef NET_EVENT_H 61 #define NET_EVENT_H 62 63 #include <sys/time.h> 64 #include "dnscrypt/dnscrypt.h" 65 #ifdef HAVE_NGHTTP2_NGHTTP2_H 66 #include <nghttp2/nghttp2.h> 67 #endif 68 #ifdef HAVE_NGTCP2 69 #include <ngtcp2/ngtcp2.h> 70 #endif 71 72 struct sldns_buffer; 73 struct comm_point; 74 struct comm_reply; 75 struct tcl_list; 76 struct ub_event_base; 77 struct unbound_socket; 78 struct doq_server_socket; 79 struct doq_table; 80 struct doq_conn; 81 struct config_file; 82 struct ub_randstate; 83 84 struct mesh_state; 85 struct mesh_area; 86 87 /* internal event notification data storage structure. */ 88 struct internal_event; 89 struct internal_base; 90 struct internal_timer; /* A sub struct of the comm_timer super struct */ 91 92 enum listen_type; 93 94 /** callback from communication point function type */ 95 typedef int comm_point_callback_type(struct comm_point*, void*, int, 96 struct comm_reply*); 97 98 /** to pass no_error to callback function */ 99 #define NETEVENT_NOERROR 0 100 /** to pass closed connection to callback function */ 101 #define NETEVENT_CLOSED -1 102 /** to pass timeout happened to callback function */ 103 #define NETEVENT_TIMEOUT -2 104 /** to pass fallback from capsforID to callback function; 0x20 failed */ 105 #define NETEVENT_CAPSFAIL -3 106 /** to pass done transfer to callback function; http file is complete */ 107 #define NETEVENT_DONE -4 108 /** to pass write of the write packet is done to callback function 109 * used when tcp_write_and_read is enabled */ 110 #define NETEVENT_PKT_WRITTEN -5 111 112 /** timeout to slow accept calls when not possible, in msec. */ 113 #define NETEVENT_SLOW_ACCEPT_TIME 2000 114 /** timeout to slow accept calls when tcp queue is full, in msec. */ 115 #define NETEVENT_SLOW_ACCEPT_QUEUE_TIME 50 116 /** timeout to slow down log print, so it does not spam the logs, in sec */ 117 #define SLOW_LOG_TIME 10 118 /** for doq, the maximum dcid length, in ngtcp2 it is 20. */ 119 #define DOQ_MAX_CIDLEN 24 120 121 /** 122 * A communication point dispatcher. Thread specific. 123 */ 124 struct comm_base { 125 /** behind the scenes structure. with say libevent info. alloced */ 126 struct internal_base* eb; 127 /** callback to stop listening on accept sockets, 128 * performed when accept() will not function properly */ 129 void (*stop_accept)(void*); 130 /** callback to start listening on accept sockets, performed 131 * after stop_accept() then a timeout has passed. */ 132 void (*start_accept)(void*); 133 /** user argument for stop_accept and start_accept functions */ 134 void* cb_arg; 135 }; 136 137 /** 138 * Reply information for a communication point. 139 */ 140 struct comm_reply { 141 /** the comm_point with fd to send reply on to. */ 142 struct comm_point* c; 143 /** the address (for UDP based communication) */ 144 struct sockaddr_storage remote_addr; 145 /** length of address */ 146 socklen_t remote_addrlen; 147 /** return type 0 (none), 4(IP4), 6(IP6) 148 * used only with listen_type_udp_ancil* */ 149 int srctype; 150 /* DnsCrypt context */ 151 #ifdef USE_DNSCRYPT 152 uint8_t client_nonce[crypto_box_HALF_NONCEBYTES]; 153 uint8_t nmkey[crypto_box_BEFORENMBYTES]; 154 const dnsccert *dnsc_cert; 155 int is_dnscrypted; 156 #endif 157 /** the return source interface data */ 158 union { 159 #ifdef IPV6_PKTINFO 160 struct in6_pktinfo v6info; 161 #endif 162 #ifdef IP_PKTINFO 163 struct in_pktinfo v4info; 164 #elif defined(IP_RECVDSTADDR) 165 struct in_addr v4addr; 166 #endif 167 } 168 /** variable with return source data */ 169 pktinfo; 170 /** max udp size for udp packets */ 171 size_t max_udp_size; 172 /* if set, the request came through a proxy */ 173 int is_proxied; 174 /** the client address 175 * the same as remote_addr if not proxied */ 176 struct sockaddr_storage client_addr; 177 /** the original address length */ 178 socklen_t client_addrlen; 179 #ifdef HAVE_NGTCP2 180 /** the doq ifindex, together with addr and localaddr in pktinfo, 181 * and dcid makes the doq_conn_key to find the connection */ 182 int doq_ifindex; 183 /** the doq dcid, the connection id used to find the connection */ 184 uint8_t doq_dcid[DOQ_MAX_CIDLEN]; 185 /** the length of the doq dcid */ 186 size_t doq_dcidlen; 187 /** the doq stream id where the query came in on */ 188 int64_t doq_streamid; 189 /** port number for doq */ 190 int doq_srcport; 191 #endif /* HAVE_NGTCP2 */ 192 /** The doq stream to register mesh states to. */ 193 struct doq_stream* doq_stream; 194 }; 195 196 /** 197 * Communication point to the network 198 * These behaviours can be accomplished by setting the flags 199 * and passing return values from the callback. 200 * udp frontside: called after readdone. sendafter. 201 * tcp frontside: called readdone, sendafter. close. 202 * udp behind: called after readdone. No send after. 203 * tcp behind: write done, read done, then called. No send after. 204 */ 205 struct comm_point { 206 /** behind the scenes structure, with say libevent info. alloced. */ 207 struct internal_event* ev; 208 /** if the event is added or not */ 209 int event_added; 210 211 /** Reference to struct that is part of the listening ports, 212 * where for listening ports information is kept about the address. */ 213 struct unbound_socket* socket; 214 215 /** file descriptor for communication point */ 216 int fd; 217 218 /** timeout (NULL if it does not). Malloced. */ 219 struct timeval* timeout; 220 221 /** buffer pointer. Either to perthread, or own buffer or NULL */ 222 struct sldns_buffer* buffer; 223 224 /* -------- TCP Handler -------- */ 225 /** Read/Write state for TCP */ 226 int tcp_is_reading; 227 /** The current read/write count for TCP */ 228 size_t tcp_byte_count; 229 /** parent communication point (for TCP sockets) */ 230 struct comm_point* tcp_parent; 231 /** sockaddr from peer, for TCP handlers */ 232 struct comm_reply repinfo; 233 234 /* -------- TCP Accept -------- */ 235 /** the number of TCP handlers for this tcp-accept socket */ 236 int max_tcp_count; 237 /** current number of tcp handler in-use for this accept socket */ 238 int cur_tcp_count; 239 /** malloced array of tcp handlers for a tcp-accept, 240 of size max_tcp_count. */ 241 struct comm_point** tcp_handlers; 242 /** linked list of free tcp_handlers to use for new queries. 243 For tcp_accept the first entry, for tcp_handlers the next one. */ 244 struct comm_point* tcp_free; 245 /** Whether this struct is in its parent's tcp_free list */ 246 int is_in_tcp_free; 247 248 /* -------- SSL TCP DNS ------- */ 249 /** the SSL object with rw bio (owned) or for commaccept ctx ref */ 250 void* ssl; 251 /** handshake state for init and renegotiate */ 252 enum { 253 /** no handshake, it has been done */ 254 comm_ssl_shake_none = 0, 255 /** ssl initial handshake wants to read */ 256 comm_ssl_shake_read, 257 /** ssl initial handshake wants to write */ 258 comm_ssl_shake_write, 259 /** ssl_write wants to read */ 260 comm_ssl_shake_hs_read, 261 /** ssl_read wants to write */ 262 comm_ssl_shake_hs_write 263 } ssl_shake_state; 264 265 /* -------- HTTP ------- */ 266 /** Do not allow connection to use HTTP version lower than this. 0=no 267 * minimum. */ 268 enum { 269 http_version_none = 0, 270 http_version_2 = 2 271 } http_min_version; 272 /** http endpoint */ 273 char* http_endpoint; 274 /* -------- HTTP/1.1 ------- */ 275 /** Currently reading in http headers */ 276 int http_in_headers; 277 /** Currently reading in chunk headers, 0=not, 1=firstline, 2=unused 278 * (more lines), 3=trailer headers after chunk */ 279 int http_in_chunk_headers; 280 /** chunked transfer */ 281 int http_is_chunked; 282 /** http temp buffer (shared buffer for temporary work) */ 283 struct sldns_buffer* http_temp; 284 /** http stored content in buffer */ 285 size_t http_stored; 286 /* -------- HTTP/2 ------- */ 287 /** http2 session */ 288 struct http2_session* h2_session; 289 /** set to 1 if h2 is negotiated to be used (using alpn) */ 290 int use_h2; 291 /** stream currently being handled */ 292 struct http2_stream* h2_stream; 293 /** maximum allowed query buffer size, per stream */ 294 size_t http2_stream_max_qbuffer_size; 295 /** maximum number of HTTP/2 streams per connection. Send in HTTP/2 296 * SETTINGS frame. */ 297 uint32_t http2_max_streams; 298 /* -------- DoQ ------- */ 299 #ifdef HAVE_NGTCP2 300 /** the doq server socket, with list of doq connections */ 301 struct doq_server_socket* doq_socket; 302 #endif 303 304 /* -------- dnstap ------- */ 305 /** the dnstap environment */ 306 struct dt_env* dtenv; 307 308 /** is this a UDP, TCP-accept or TCP socket. */ 309 enum comm_point_type { 310 /** UDP socket - handle datagrams. */ 311 comm_udp, 312 /** TCP accept socket - only creates handlers if readable. */ 313 comm_tcp_accept, 314 /** TCP handler socket - handle byteperbyte readwrite. */ 315 comm_tcp, 316 /** HTTP handler socket */ 317 comm_http, 318 /** DOQ handler socket */ 319 comm_doq, 320 /** AF_UNIX socket - for internal commands. */ 321 comm_local, 322 /** raw - not DNS format - for pipe readers and writers */ 323 comm_raw 324 } 325 /** variable with type of socket, UDP,TCP-accept,TCP,pipe */ 326 type; 327 328 /* -------- PROXYv2 ------- */ 329 /** if set, PROXYv2 is expected on this connection */ 330 int pp2_enabled; 331 /** header state for the PROXYv2 header (for TCP) */ 332 enum { 333 /** no header encounter yet */ 334 pp2_header_none = 0, 335 /** read the static part of the header */ 336 pp2_header_init, 337 /** read the full header */ 338 pp2_header_done 339 } pp2_header_state; 340 341 /* ---------- Behaviour ----------- */ 342 /** if set the connection is NOT closed on delete. */ 343 int do_not_close; 344 345 /** if set, the connection is closed on error, on timeout, 346 and after read/write completes. No callback is done. */ 347 int tcp_do_close; 348 349 /** flag that indicates the stream is both written and read from. */ 350 int tcp_write_and_read; 351 352 /** byte count for written length over write channel, for when 353 * tcp_write_and_read is enabled. When tcp_write_and_read is enabled, 354 * this is the counter for writing, the one for reading is in the 355 * commpoint.buffer sldns buffer. The counter counts from 0 to 356 * 2+tcp_write_pkt_len, and includes the tcp length bytes. */ 357 size_t tcp_write_byte_count; 358 359 /** packet to write currently over the write channel. for when 360 * tcp_write_and_read is enabled. When tcp_write_and_read is enabled, 361 * this is the buffer for the written packet, the commpoint.buffer 362 * sldns buffer is the buffer for the received packet. */ 363 uint8_t* tcp_write_pkt; 364 /** length of tcp_write_pkt in bytes */ 365 size_t tcp_write_pkt_len; 366 367 /** if set try to read another packet again (over connection with 368 * multiple packets), once set, tries once, then zero again, 369 * so set it in the packet complete section. 370 * The pointer itself has to be set before the callback is invoked, 371 * when you set things up, and continue to exist also after the 372 * commpoint is closed and deleted in your callback. So that after 373 * the callback cleans up netevent can see what it has to do. 374 * Or leave NULL if it is not used at all. */ 375 int* tcp_more_read_again; 376 377 /** if set try to write another packet (over connection with 378 * multiple packets), once set, tries once, then zero again, 379 * so set it in the packet complete section. 380 * The pointer itself has to be set before the callback is invoked, 381 * when you set things up, and continue to exist also after the 382 * commpoint is closed and deleted in your callback. So that after 383 * the callback cleans up netevent can see what it has to do. 384 * Or leave NULL if it is not used at all. */ 385 int* tcp_more_write_again; 386 387 /** resume timer for tcp_more_read_again */ 388 struct comm_timer* tcp_more_read_again_timer; 389 390 /** if set, read/write completes: 391 read/write state of tcp is toggled. 392 buffer reset/bytecount reset. 393 this flag cleared. 394 So that when that is done the callback is called. */ 395 int tcp_do_toggle_rw; 396 397 /** timeout in msec for TCP wait times for this connection */ 398 int tcp_timeout_msec; 399 400 /** if set, tcp keepalive is enabled on this connection */ 401 int tcp_keepalive; 402 403 /** if set, checks for pending error from nonblocking connect() call.*/ 404 int tcp_check_nb_connect; 405 406 /** if set, check for connection limit on tcp accept. */ 407 struct tcl_list* tcp_conn_limit; 408 /** the entry for the connection. */ 409 struct tcl_addr* tcl_addr; 410 411 /** the structure to keep track of open requests on this channel */ 412 struct tcp_req_info* tcp_req_info; 413 414 #ifdef USE_MSG_FASTOPEN 415 /** used to track if the sendto() call should be done when using TFO. */ 416 int tcp_do_fastopen; 417 #endif 418 419 #ifdef USE_DNSCRYPT 420 /** Is this a dnscrypt channel */ 421 int dnscrypt; 422 /** encrypted buffer pointer. Either to perthread, or own buffer or NULL */ 423 struct sldns_buffer* dnscrypt_buffer; 424 #endif 425 /** number of queries outstanding on this socket, used by 426 * outside network for udp ports */ 427 int inuse; 428 /** the timestamp when the packet was received by the kernel */ 429 struct timeval recv_tv; 430 /** callback when done. 431 tcp_accept does not get called back, is NULL then. 432 If a timeout happens, callback with timeout=1 is called. 433 If an error happens, callback is called with error set 434 nonzero. If not NETEVENT_NOERROR, it is an errno value. 435 If the connection is closed (by remote end) then the 436 callback is called with error set to NETEVENT_CLOSED=-1. 437 If a timeout happens on the connection, the error is set to 438 NETEVENT_TIMEOUT=-2. 439 The reply_info can be copied if the reply needs to happen at a 440 later time. It consists of a struct with commpoint and address. 441 It can be passed to a msg send routine some time later. 442 Note the reply information is temporary and must be copied. 443 NULL is passed for_reply info, in cases where error happened. 444 445 declare as: 446 int my_callback(struct comm_point* c, void* my_arg, int error, 447 struct comm_reply *reply_info); 448 449 if the routine returns 0, nothing is done. 450 Notzero, the buffer will be sent back to client. 451 For UDP this is done without changing the commpoint. 452 In TCP it sets write state. 453 */ 454 comm_point_callback_type* callback; 455 /** argument to pass to callback. */ 456 void *cb_arg; 457 }; 458 459 /** 460 * Structure only for making timeout events. 461 */ 462 struct comm_timer { 463 /** the internal event stuff (derived) */ 464 struct internal_timer* ev_timer; 465 466 /** callback function, takes user arg only */ 467 void (*callback)(void*); 468 469 /** callback user argument */ 470 void* cb_arg; 471 }; 472 473 /** 474 * Structure only for signal events. 475 */ 476 struct comm_signal { 477 /** the communication base */ 478 struct comm_base* base; 479 480 /** the internal event stuff */ 481 struct internal_signal* ev_signal; 482 483 /** callback function, takes signal number and user arg */ 484 void (*callback)(int, void*); 485 486 /** callback user argument */ 487 void* cb_arg; 488 }; 489 490 /** 491 * Create a new comm base. 492 * @param sigs: if true it attempts to create a default loop for 493 * signal handling. 494 * @return: the new comm base. NULL on error. 495 */ 496 struct comm_base* comm_base_create(int sigs); 497 498 /** 499 * Create comm base that uses the given ub_event_base (underlying pluggable 500 * event mechanism pointer). 501 * @param base: underlying pluggable event base. 502 * @return: the new comm base. NULL on error. 503 */ 504 struct comm_base* comm_base_create_event(struct ub_event_base* base); 505 506 /** 507 * Delete comm base structure but not the underlying lib event base. 508 * All comm points must have been deleted. 509 * @param b: the base to delete. 510 */ 511 void comm_base_delete_no_base(struct comm_base* b); 512 513 /** 514 * Destroy a comm base. 515 * All comm points must have been deleted. 516 * @param b: the base to delete. 517 */ 518 void comm_base_delete(struct comm_base* b); 519 520 /** 521 * Obtain two pointers. The pointers never change (until base_delete()). 522 * The pointers point to time values that are updated regularly. 523 * @param b: the communication base that will update the time values. 524 * @param tt: pointer to time in seconds is returned. 525 * @param tv: pointer to time in microseconds is returned. 526 */ 527 void comm_base_timept(struct comm_base* b, time_t** tt, struct timeval** tv); 528 529 /** 530 * Dispatch the comm base events. 531 * @param b: the communication to perform. 532 */ 533 void comm_base_dispatch(struct comm_base* b); 534 535 /** 536 * Exit from dispatch loop. 537 * @param b: the communication base that is in dispatch(). 538 */ 539 void comm_base_exit(struct comm_base* b); 540 541 /** 542 * Set the slow_accept mode handlers. You can not provide these if you do 543 * not perform accept() calls. 544 * @param b: comm base 545 * @param stop_accept: function that stops listening to accept fds. 546 * @param start_accept: function that resumes listening to accept fds. 547 * @param arg: callback arg to pass to the functions. 548 */ 549 void comm_base_set_slow_accept_handlers(struct comm_base* b, 550 void (*stop_accept)(void*), void (*start_accept)(void*), void* arg); 551 552 /** 553 * Access internal data structure (for util/tube.c on windows) 554 * @param b: comm base 555 * @return ub_event_base. 556 */ 557 struct ub_event_base* comm_base_internal(struct comm_base* b); 558 559 /** 560 * Access internal event structure. It is for use with 561 * ub_winsock_tcp_wouldblock on windows. 562 * @param c: comm point. 563 * @return event. 564 */ 565 struct ub_event* comm_point_internal(struct comm_point* c); 566 567 /** 568 * Create an UDP comm point. Calls malloc. 569 * setups the structure with the parameters you provide. 570 * @param base: in which base to alloc the commpoint. 571 * @param fd: file descriptor of open UDP socket. 572 * @param buffer: shared buffer by UDP sockets from this thread. 573 * @param pp2_enabled: if the comm point will support PROXYv2. 574 * @param callback: callback function pointer. 575 * @param callback_arg: will be passed to your callback function. 576 * @param socket: and opened socket properties will be passed to your callback function. 577 * @return: returns the allocated communication point. NULL on error. 578 * Sets timeout to NULL. Turns off TCP options. 579 */ 580 struct comm_point* comm_point_create_udp(struct comm_base* base, 581 int fd, struct sldns_buffer* buffer, int pp2_enabled, 582 comm_point_callback_type* callback, void* callback_arg, struct unbound_socket* socket); 583 584 /** 585 * Create an UDP with ancillary data comm point. Calls malloc. 586 * Uses recvmsg instead of recv to get udp message. 587 * setups the structure with the parameters you provide. 588 * @param base: in which base to alloc the commpoint. 589 * @param fd: file descriptor of open UDP socket. 590 * @param buffer: shared buffer by UDP sockets from this thread. 591 * @param pp2_enabled: if the comm point will support PROXYv2. 592 * @param callback: callback function pointer. 593 * @param callback_arg: will be passed to your callback function. 594 * @param socket: and opened socket properties will be passed to your callback function. 595 * @return: returns the allocated communication point. NULL on error. 596 * Sets timeout to NULL. Turns off TCP options. 597 */ 598 struct comm_point* comm_point_create_udp_ancil(struct comm_base* base, 599 int fd, struct sldns_buffer* buffer, int pp2_enabled, 600 comm_point_callback_type* callback, void* callback_arg, struct unbound_socket* socket); 601 602 /** 603 * Create an UDP comm point for DoQ. Calls malloc. 604 * setups the structure with the parameters you provide. 605 * @param base: in which base to alloc the commpoint. 606 * @param fd : file descriptor of open UDP socket. 607 * @param buffer: shared buffer by UDP sockets from this thread. 608 * @param callback: callback function pointer. 609 * @param callback_arg: will be passed to your callback function. 610 * @param socket: and opened socket properties will be passed to your callback function. 611 * @param table: the doq connection table for the host. 612 * @param rnd: random generator to use. 613 * @param quic_sslctx: the quic ssl context. 614 * @param cfg: config file struct. 615 * @return: returns the allocated communication point. NULL on error. 616 * Sets timeout to NULL. Turns off TCP options. 617 */ 618 struct comm_point* comm_point_create_doq(struct comm_base* base, 619 int fd, struct sldns_buffer* buffer, 620 comm_point_callback_type* callback, void* callback_arg, 621 struct unbound_socket* socket, struct doq_table* table, 622 struct ub_randstate* rnd, const void* quic_sslctx, 623 struct config_file* cfg); 624 625 /** 626 * Create a TCP listener comm point. Calls malloc. 627 * Setups the structure with the parameters you provide. 628 * Also Creates TCP Handlers, pre allocated for you. 629 * Uses the parameters you provide. 630 * @param base: in which base to alloc the commpoint. 631 * @param fd: file descriptor of open TCP socket set to listen nonblocking. 632 * @param num: becomes max_tcp_count, the routine allocates that 633 * many tcp handler commpoints. 634 * @param idle_timeout: TCP idle timeout in ms. 635 * @param harden_large_queries: whether query size should be limited. 636 * @param http_max_streams: maximum number of HTTP/2 streams per connection. 637 * @param http_endpoint: HTTP endpoint to service queries on 638 * @param tcp_conn_limit: TCP connection limit info. 639 * @param bufsize: size of buffer to create for handlers. 640 * @param spoolbuf: shared spool buffer for tcp_req_info structures. 641 * or NULL to not create those structures in the tcp handlers. 642 * @param port_type: the type of port we are creating a TCP listener for. Used 643 * to select handler type to use. 644 * @param pp2_enabled: if the comm point will support PROXYv2. 645 * @param callback: callback function pointer for TCP handlers. 646 * @param callback_arg: will be passed to your callback function. 647 * @param socket: and opened socket properties will be passed to your callback function. 648 * @return: returns the TCP listener commpoint. You can find the 649 * TCP handlers in the array inside the listener commpoint. 650 * returns NULL on error. 651 * Inits timeout to NULL. All handlers are on the free list. 652 */ 653 struct comm_point* comm_point_create_tcp(struct comm_base* base, 654 int fd, int num, int idle_timeout, int harden_large_queries, 655 uint32_t http_max_streams, char* http_endpoint, 656 struct tcl_list* tcp_conn_limit, 657 size_t bufsize, struct sldns_buffer* spoolbuf, 658 enum listen_type port_type, int pp2_enabled, 659 comm_point_callback_type* callback, void* callback_arg, struct unbound_socket* socket); 660 661 /** 662 * Create an outgoing TCP commpoint. No file descriptor is opened, left at -1. 663 * @param base: in which base to alloc the commpoint. 664 * @param bufsize: size of buffer to create for handlers. 665 * @param callback: callback function pointer for the handler. 666 * @param callback_arg: will be passed to your callback function. 667 * @return: the commpoint or NULL on error. 668 */ 669 struct comm_point* comm_point_create_tcp_out(struct comm_base* base, 670 size_t bufsize, comm_point_callback_type* callback, void* callback_arg); 671 672 /** 673 * Create an outgoing HTTP commpoint. No file descriptor is opened, left at -1. 674 * @param base: in which base to alloc the commpoint. 675 * @param bufsize: size of buffer to create for handlers. 676 * @param callback: callback function pointer for the handler. 677 * @param callback_arg: will be passed to your callback function. 678 * @param temp: sldns buffer, shared between other http_out commpoints, for 679 * temporary data when performing callbacks. 680 * @return: the commpoint or NULL on error. 681 */ 682 struct comm_point* comm_point_create_http_out(struct comm_base* base, 683 size_t bufsize, comm_point_callback_type* callback, 684 void* callback_arg, struct sldns_buffer* temp); 685 686 /** 687 * Create commpoint to listen to a local domain file descriptor. 688 * @param base: in which base to alloc the commpoint. 689 * @param fd: file descriptor of open AF_UNIX socket set to listen nonblocking. 690 * @param bufsize: size of buffer to create for handlers. 691 * @param callback: callback function pointer for the handler. 692 * @param callback_arg: will be passed to your callback function. 693 * @return: the commpoint or NULL on error. 694 */ 695 struct comm_point* comm_point_create_local(struct comm_base* base, 696 int fd, size_t bufsize, 697 comm_point_callback_type* callback, void* callback_arg); 698 699 /** 700 * Create commpoint to listen to a local domain pipe descriptor. 701 * @param base: in which base to alloc the commpoint. 702 * @param fd: file descriptor. 703 * @param writing: true if you want to listen to writes, false for reads. 704 * @param callback: callback function pointer for the handler. 705 * @param callback_arg: will be passed to your callback function. 706 * @return: the commpoint or NULL on error. 707 */ 708 struct comm_point* comm_point_create_raw(struct comm_base* base, 709 int fd, int writing, 710 comm_point_callback_type* callback, void* callback_arg); 711 712 /** 713 * Close a comm point fd. 714 * @param c: comm point to close. 715 */ 716 void comm_point_close(struct comm_point* c); 717 718 /** 719 * Close and deallocate (free) the comm point. If the comm point is 720 * a tcp-accept point, also its tcp-handler points are deleted. 721 * @param c: comm point to delete. 722 */ 723 void comm_point_delete(struct comm_point* c); 724 725 /** 726 * Send reply. Put message into commpoint buffer. 727 * @param repinfo: The reply info copied from a commpoint callback call. 728 */ 729 void comm_point_send_reply(struct comm_reply* repinfo); 730 731 /** 732 * Drop reply. Cleans up. 733 * @param repinfo: The reply info copied from a commpoint callback call. 734 */ 735 void comm_point_drop_reply(struct comm_reply* repinfo); 736 737 /** 738 * Send an udp message over a commpoint. 739 * @param c: commpoint to send it from. 740 * @param packet: what to send. 741 * @param addr: where to send it to. If NULL, send is performed, 742 * for connected sockets, to the connected address. 743 * @param addrlen: length of addr. 744 * @param is_connected: if the UDP socket is connect()ed. 745 * @return: false on a failure. 746 */ 747 int comm_point_send_udp_msg(struct comm_point* c, struct sldns_buffer* packet, 748 struct sockaddr* addr, socklen_t addrlen,int is_connected); 749 750 /** 751 * Stop listening for input on the commpoint. No callbacks will happen. 752 * @param c: commpoint to disable. The fd is not closed. 753 */ 754 void comm_point_stop_listening(struct comm_point* c); 755 756 /** 757 * Start listening again for input on the comm point. 758 * @param c: commpoint to enable again. 759 * @param newfd: new fd, or -1 to leave fd be. 760 * @param msec: timeout in milliseconds, or -1 for no (change to the) timeout. 761 * So seconds*1000. 762 */ 763 void comm_point_start_listening(struct comm_point* c, int newfd, int msec); 764 765 /** 766 * Stop listening and start listening again for reading or writing. 767 * @param c: commpoint 768 * @param rd: if true, listens for reading. 769 * @param wr: if true, listens for writing. 770 */ 771 void comm_point_listen_for_rw(struct comm_point* c, int rd, int wr); 772 773 /** 774 * For TCP handlers that use c->tcp_timeout_msec, this routine adjusts 775 * it with the minimum. Otherwise, a 0 value advertised without the 776 * minimum applied moves to a 0 in comm_point_start_listening and that 777 * routine treats it as no timeout, listen forever, which is not wanted. 778 * @param c: comm point to use the tcp_timeout_msec of. 779 * @return adjusted tcp_timeout_msec value with the minimum if smaller. 780 */ 781 int adjusted_tcp_timeout(struct comm_point* c); 782 783 /** 784 * Get size of memory used by comm point. 785 * For TCP handlers this includes subhandlers. 786 * For UDP handlers, this does not include the (shared) UDP buffer. 787 * @param c: commpoint. 788 * @return size in bytes. 789 */ 790 size_t comm_point_get_mem(struct comm_point* c); 791 792 /** 793 * create timer. Not active upon creation. 794 * @param base: event handling base. 795 * @param cb: callback function: void myfunc(void* myarg); 796 * @param cb_arg: user callback argument. 797 * @return: the new timer or NULL on error. 798 */ 799 struct comm_timer* comm_timer_create(struct comm_base* base, 800 void (*cb)(void*), void* cb_arg); 801 802 /** 803 * disable timer. Stops callbacks from happening. 804 * @param timer: to disable. 805 */ 806 void comm_timer_disable(struct comm_timer* timer); 807 808 /** 809 * reset timevalue for timer. 810 * @param timer: timer to (re)set. 811 * @param tv: when the timer should activate. if NULL timer is disabled. 812 */ 813 void comm_timer_set(struct comm_timer* timer, struct timeval* tv); 814 815 /** 816 * delete timer. 817 * @param timer: to delete. 818 */ 819 void comm_timer_delete(struct comm_timer* timer); 820 821 /** 822 * see if timeout has been set to a value. 823 * @param timer: the timer to examine. 824 * @return: false if disabled or not set. 825 */ 826 int comm_timer_is_set(struct comm_timer* timer); 827 828 /** 829 * Get size of memory used by comm timer. 830 * @param timer: the timer to examine. 831 * @return size in bytes. 832 */ 833 size_t comm_timer_get_mem(struct comm_timer* timer); 834 835 /** 836 * Create a signal handler. Call signal_bind() later to bind to a signal. 837 * @param base: communication base to use. 838 * @param callback: called when signal is caught. 839 * @param cb_arg: user argument to callback 840 * @return: the signal struct or NULL on error. 841 */ 842 struct comm_signal* comm_signal_create(struct comm_base* base, 843 void (*callback)(int, void*), void* cb_arg); 844 845 /** 846 * Bind signal struct to catch a signal. A single comm_signal can be bound 847 * to multiple signals, calling comm_signal_bind multiple times. 848 * @param comsig: the communication point, with callback information. 849 * @param sig: signal number. 850 * @return: true on success. false on error. 851 */ 852 int comm_signal_bind(struct comm_signal* comsig, int sig); 853 854 /** 855 * Delete the signal communication point. 856 * @param comsig: to delete. 857 */ 858 void comm_signal_delete(struct comm_signal* comsig); 859 860 /** 861 * perform accept(2) with error checking. 862 * @param c: commpoint with accept fd. 863 * @param addr: remote end returned here. 864 * @param addrlen: length of remote end returned here. 865 * @return new fd, or -1 on error. 866 * if -1, error message has been printed if necessary, simply drop 867 * out of the reading handler. 868 */ 869 int comm_point_perform_accept(struct comm_point* c, 870 struct sockaddr_storage* addr, socklen_t* addrlen); 871 872 /**** internal routines ****/ 873 874 /** 875 * This routine is published for checks and tests, and is only used internally. 876 * handle libevent callback for udp comm point. 877 * @param fd: file descriptor. 878 * @param event: event bits from libevent: 879 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 880 * @param arg: the comm_point structure. 881 */ 882 void comm_point_udp_callback(int fd, short event, void* arg); 883 884 /** 885 * This routine is published for checks and tests, and is only used internally. 886 * handle libevent callback for udp ancillary data comm point. 887 * @param fd: file descriptor. 888 * @param event: event bits from libevent: 889 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 890 * @param arg: the comm_point structure. 891 */ 892 void comm_point_udp_ancil_callback(int fd, short event, void* arg); 893 894 /** 895 * This routine is published for checks and tests, and is only used internally. 896 * handle libevent callback for doq comm point. 897 * @param fd: file descriptor. 898 * @param event: event bits from libevent: 899 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 900 * @param arg: the comm_point structure. 901 */ 902 void comm_point_doq_callback(int fd, short event, void* arg); 903 904 /** 905 * This routine is published for checks and tests, and is only used internally. 906 * handle libevent callback for tcp accept comm point 907 * @param fd: file descriptor. 908 * @param event: event bits from libevent: 909 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 910 * @param arg: the comm_point structure. 911 */ 912 void comm_point_tcp_accept_callback(int fd, short event, void* arg); 913 914 /** 915 * This routine is published for checks and tests, and is only used internally. 916 * handle libevent callback for tcp data comm point 917 * @param fd: file descriptor. 918 * @param event: event bits from libevent: 919 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 920 * @param arg: the comm_point structure. 921 */ 922 void comm_point_tcp_handle_callback(int fd, short event, void* arg); 923 924 /** 925 * This routine is published for checks and tests, and is only used internally. 926 * handle libevent callback for tcp data comm point 927 * @param fd: file descriptor. 928 * @param event: event bits from libevent: 929 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 930 * @param arg: the comm_point structure. 931 */ 932 void comm_point_http_handle_callback(int fd, short event, void* arg); 933 934 /** 935 * HTTP2 session. HTTP2 related info per comm point. 936 */ 937 struct http2_session { 938 /** first item in list of streams */ 939 struct http2_stream* first_stream; 940 #ifdef HAVE_NGHTTP2 941 /** nghttp2 session */ 942 nghttp2_session *session; 943 /** store nghttp2 callbacks for easy reuse */ 944 nghttp2_session_callbacks* callbacks; 945 #endif 946 /** comm point containing buffer used to build answer in worker or 947 * module */ 948 struct comm_point* c; 949 /** count the number of consecutive reads on the session */ 950 uint32_t reads_count; 951 /** session is instructed to get dropped (comm port will be closed) */ 952 int is_drop; 953 /** postpone dropping the session, can be used to prevent dropping 954 * while being in a callback */ 955 int postpone_drop; 956 }; 957 958 /** enum of HTTP status */ 959 enum http_status { 960 HTTP_STATUS_OK = 200, 961 HTTP_STATUS_BAD_REQUEST = 400, 962 HTTP_STATUS_NOT_FOUND = 404, 963 HTTP_STATUS_PAYLOAD_TOO_LARGE = 413, 964 HTTP_STATUS_URI_TOO_LONG = 414, 965 HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE = 415, 966 HTTP_STATUS_NOT_IMPLEMENTED = 501 967 }; 968 969 /** 970 * HTTP stream. Part of list of HTTP2 streams per session. 971 */ 972 struct http2_stream { 973 /** next stream in list per session */ 974 struct http2_stream* next; 975 /** previous stream in list per session */ 976 struct http2_stream* prev; 977 /** HTTP2 stream ID is an unsigned 31-bit integer */ 978 int32_t stream_id; 979 /** HTTP method used for this stream */ 980 enum { 981 HTTP_METHOD_POST = 1, 982 HTTP_METHOD_GET, 983 HTTP_METHOD_UNSUPPORTED 984 } http_method; 985 /** message contains invalid content type */ 986 int invalid_content_type; 987 /** message body content type */ 988 size_t content_length; 989 /** HTTP response status */ 990 enum http_status status; 991 /** request for non existing endpoint */ 992 int invalid_endpoint; 993 /** query in request is too large */ 994 int query_too_large; 995 /** buffer to store query into. Can't use session shared buffer as query 996 * can arrive in parts, intertwined with frames for other queries. */ 997 struct sldns_buffer* qbuffer; 998 /** buffer to store response into. Can't use shared buffer as a next 999 * query read callback can overwrite it before it is send out. */ 1000 struct sldns_buffer* rbuffer; 1001 /** mesh area containing mesh state */ 1002 struct mesh_area* mesh; 1003 /** mesh state for query. Used to remove mesh reply before closing 1004 * stream. */ 1005 struct mesh_state* mesh_state; 1006 }; 1007 1008 #ifdef HAVE_NGHTTP2 1009 /** nghttp2 receive cb. Read from SSL connection into nghttp2 buffer */ 1010 ssize_t http2_recv_cb(nghttp2_session* session, uint8_t* buf, 1011 size_t len, int flags, void* cb_arg); 1012 /** nghttp2 send callback. Send from nghttp2 buffer to ssl socket */ 1013 ssize_t http2_send_cb(nghttp2_session* session, const uint8_t* buf, 1014 size_t len, int flags, void* cb_arg); 1015 /** nghttp2 callback on closing stream */ 1016 int http2_stream_close_cb(nghttp2_session* session, int32_t stream_id, 1017 uint32_t error_code, void* cb_arg); 1018 #endif 1019 1020 /** 1021 * Create new http2 stream 1022 * @param stream_id: ID for stream to create. 1023 * @return malloc'ed stream, NULL on error 1024 */ 1025 struct http2_stream* http2_stream_create(int32_t stream_id); 1026 1027 /** 1028 * Add new stream to session linked list 1029 * @param h2_session: http2 session to add stream to 1030 * @param h2_stream: stream to add to session list 1031 */ 1032 void http2_session_add_stream(struct http2_session* h2_session, 1033 struct http2_stream* h2_stream); 1034 1035 /** Add mesh state to stream. To be able to remove mesh reply on stream closure 1036 */ 1037 void http2_stream_add_meshstate(struct http2_stream* h2_stream, 1038 struct mesh_area* mesh, struct mesh_state* m); 1039 1040 /** Remove mesh state from stream. When the mesh state has been removed. */ 1041 void http2_stream_remove_mesh_state(struct http2_stream* h2_stream); 1042 1043 /** 1044 * DoQ socket address storage for IP4 or IP6 address. Smaller than 1045 * the sockaddr_storage because not with af_unix pathnames. 1046 */ 1047 struct doq_addr_storage { 1048 union { 1049 struct sockaddr_in in; 1050 #ifdef AF_INET6 1051 struct sockaddr_in6 in6; 1052 #endif 1053 } sockaddr; 1054 }; 1055 1056 /** 1057 * The DoQ server socket information, for DNS over QUIC. 1058 */ 1059 struct doq_server_socket { 1060 /** the doq connection table */ 1061 struct doq_table* table; 1062 /** random generator */ 1063 struct ub_randstate* rnd; 1064 /** if address validation is enabled */ 1065 uint8_t validate_addr; 1066 /** the server scid length */ 1067 int sv_scidlen; 1068 /** the idle timeout in nanoseconds */ 1069 uint64_t idle_timeout; 1070 /** the static secret for the server */ 1071 uint8_t* static_secret; 1072 /** length of the static secret */ 1073 size_t static_secret_len; 1074 /** ssl context, SSL_CTX* */ 1075 void* ctx; 1076 #ifndef HAVE_NGTCP2_CRYPTO_QUICTLS_CONFIGURE_SERVER_CONTEXT 1077 /** quic method functions, SSL_QUIC_METHOD* */ 1078 void* quic_method; 1079 #endif 1080 /** the comm point for this doq server socket */ 1081 struct comm_point* cp; 1082 /** the buffer for packets, doq in and out */ 1083 struct sldns_buffer* pkt_buf; 1084 /** the current doq connection when we are in callbacks to worker, 1085 * so that we have the already locked structure at our disposal. */ 1086 struct doq_conn* current_conn; 1087 /** if the callback event on the fd has write flags */ 1088 uint8_t event_has_write; 1089 /** if there is a blocked packet in the blocked_pkt buffer */ 1090 int have_blocked_pkt; 1091 /** store blocked packet, a packet that could not be send on the 1092 * nonblocking socket. It has to be sent later, when the write on 1093 * the udp socket unblocks. */ 1094 struct sldns_buffer* blocked_pkt; 1095 #ifdef HAVE_NGTCP2 1096 /** the ecn info for the blocked packet, congestion information. */ 1097 struct ngtcp2_pkt_info blocked_pkt_pi; 1098 #endif 1099 /** the packet destination for the blocked packet. */ 1100 struct doq_pkt_addr* blocked_paddr; 1101 /** timer for this worker on this comm_point to wait on. */ 1102 struct comm_timer* timer; 1103 #ifdef HAVE_NGTCP2 1104 /** the timer that is marked by the doq_socket as waited on. */ 1105 ngtcp2_tstamp marked_time; 1106 #endif 1107 /** the current time for use by time functions, time_t. */ 1108 time_t* now_tt; 1109 /** the current time for use by time functions, timeval. */ 1110 struct timeval* now_tv; 1111 /** config file for the worker. */ 1112 struct config_file* cfg; 1113 }; 1114 1115 /** 1116 * DoQ packet address information. From pktinfo, stores local and remote 1117 * address and ifindex, so the packet can be sent there. 1118 */ 1119 struct doq_pkt_addr { 1120 /** the remote addr, and local addr */ 1121 struct doq_addr_storage addr, localaddr; 1122 /** length of addr and length of localaddr */ 1123 socklen_t addrlen, localaddrlen; 1124 /** interface index from pktinfo ancillary information */ 1125 int ifindex; 1126 }; 1127 1128 /** Initialize the pkt addr with lengths set to sizeof. That is ready for 1129 * a call to recv. */ 1130 void doq_pkt_addr_init(struct doq_pkt_addr* paddr); 1131 1132 /** send doq packet over UDP. */ 1133 void doq_send_pkt(struct comm_point* c, struct doq_pkt_addr* paddr, 1134 uint32_t ecn); 1135 1136 /** doq timer callback function. */ 1137 void doq_timer_cb(void* arg); 1138 1139 /** tcp read again callback function. For tcp req info listen. */ 1140 void tcp_read_again_cb(void* arg); 1141 1142 /** tcp more read again callback function. For outside network. */ 1143 void tcp_more_read_again_cb(void* arg); 1144 1145 /** 1146 * This routine is published for checks and tests, and is only used internally. 1147 * handle libevent callback for timer comm. 1148 * @param fd: file descriptor (always -1). 1149 * @param event: event bits from libevent: 1150 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 1151 * @param arg: the comm_timer structure. 1152 */ 1153 void comm_timer_callback(int fd, short event, void* arg); 1154 1155 /** 1156 * This routine is published for checks and tests, and is only used internally. 1157 * handle libevent callback for signal comm. 1158 * @param fd: file descriptor (used for the signal number). 1159 * @param event: event bits from libevent: 1160 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 1161 * @param arg: the internal commsignal structure. 1162 */ 1163 void comm_signal_callback(int fd, short event, void* arg); 1164 1165 /** 1166 * This routine is published for checks and tests, and is only used internally. 1167 * libevent callback for AF_UNIX fds 1168 * @param fd: file descriptor. 1169 * @param event: event bits from libevent: 1170 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 1171 * @param arg: the comm_point structure. 1172 */ 1173 void comm_point_local_handle_callback(int fd, short event, void* arg); 1174 1175 /** 1176 * This routine is published for checks and tests, and is only used internally. 1177 * libevent callback for raw fd access. 1178 * @param fd: file descriptor. 1179 * @param event: event bits from libevent: 1180 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 1181 * @param arg: the comm_point structure. 1182 */ 1183 void comm_point_raw_handle_callback(int fd, short event, void* arg); 1184 1185 /** 1186 * This routine is published for checks and tests, and is only used internally. 1187 * libevent callback for timeout on slow accept. 1188 * @param fd: file descriptor. 1189 * @param event: event bits from libevent: 1190 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 1191 * @param arg: the comm_point structure. 1192 */ 1193 void comm_base_handle_slow_accept(int fd, short event, void* arg); 1194 1195 #ifdef USE_WINSOCK 1196 /** 1197 * Callback for openssl BIO to on windows detect WSAEWOULDBLOCK and notify 1198 * the winsock_event of this for proper TCP nonblocking implementation. 1199 * @param c: comm_point, fd must be set its struct event is registered. 1200 * @param ssl: openssl SSL, fd must be set so it has a bio. 1201 */ 1202 void comm_point_tcp_win_bio_cb(struct comm_point* c, void* ssl); 1203 #endif 1204 1205 /** 1206 * See if errno for tcp connect has to be logged or not. This uses errno 1207 * @param addr: apart from checking errno, the addr is checked for ip4mapped 1208 * and broadcast type, hence passed. 1209 * @param addrlen: length of the addr parameter. 1210 * @return true if it needs to be logged. 1211 */ 1212 int tcp_connect_errno_needs_log(struct sockaddr* addr, socklen_t addrlen); 1213 1214 #ifdef HAVE_SSL 1215 /** 1216 * True if the ssl handshake error has to be squelched from the logs 1217 * @param err: the error returned by the openssl routine, ERR_get_error. 1218 * This is a packed structure with elements that are examined. 1219 * @return true if the error is squelched (not logged). 1220 */ 1221 int squelch_err_ssl_handshake(unsigned long err); 1222 #endif 1223 1224 #endif /* NET_EVENT_H */ 1225