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