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