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