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