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