1933707f3Ssthen /* 2933707f3Ssthen * util/netevent.h - event notification 3933707f3Ssthen * 4933707f3Ssthen * Copyright (c) 2007, NLnet Labs. All rights reserved. 5933707f3Ssthen * 6933707f3Ssthen * This software is open source. 7933707f3Ssthen * 8933707f3Ssthen * Redistribution and use in source and binary forms, with or without 9933707f3Ssthen * modification, are permitted provided that the following conditions 10933707f3Ssthen * are met: 11933707f3Ssthen * 12933707f3Ssthen * Redistributions of source code must retain the above copyright notice, 13933707f3Ssthen * this list of conditions and the following disclaimer. 14933707f3Ssthen * 15933707f3Ssthen * Redistributions in binary form must reproduce the above copyright notice, 16933707f3Ssthen * this list of conditions and the following disclaimer in the documentation 17933707f3Ssthen * and/or other materials provided with the distribution. 18933707f3Ssthen * 19933707f3Ssthen * Neither the name of the NLNET LABS nor the names of its contributors may 20933707f3Ssthen * be used to endorse or promote products derived from this software without 21933707f3Ssthen * specific prior written permission. 22933707f3Ssthen * 23933707f3Ssthen * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 240b68ff31Ssthen * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 250b68ff31Ssthen * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 260b68ff31Ssthen * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 270b68ff31Ssthen * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 280b68ff31Ssthen * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 290b68ff31Ssthen * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 300b68ff31Ssthen * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 310b68ff31Ssthen * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 320b68ff31Ssthen * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 330b68ff31Ssthen * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34933707f3Ssthen */ 35933707f3Ssthen 36933707f3Ssthen /** 37933707f3Ssthen * \file 38933707f3Ssthen * 39933707f3Ssthen * This file contains event notification functions. 40933707f3Ssthen * 41933707f3Ssthen * There are three types of communication points 42933707f3Ssthen * o UDP socket - perthread buffer. 43933707f3Ssthen * o TCP-accept socket - array of TCP-sockets, socketcount. 44933707f3Ssthen * o TCP socket - own buffer, parent-TCPaccept, read/write state, 45933707f3Ssthen * number of bytes read/written, timeout. 46933707f3Ssthen * 47933707f3Ssthen * There are sockets aimed towards our clients and towards the internet. 48933707f3Ssthen * o frontside - aimed towards our clients, queries come in, answers back. 49933707f3Ssthen * o behind - aimed towards internet, to the authoritative DNS servers. 50933707f3Ssthen * 51933707f3Ssthen * Several event types are available: 52933707f3Ssthen * o comm_base - for thread safety of the comm points, one per thread. 53933707f3Ssthen * o comm_point - udp and tcp networking, with callbacks. 54933707f3Ssthen * o comm_timer - a timeout with callback. 55933707f3Ssthen * o comm_signal - callbacks when signal is caught. 56933707f3Ssthen * o comm_reply - holds reply info during networking callback. 57933707f3Ssthen * 58933707f3Ssthen */ 59933707f3Ssthen 60933707f3Ssthen #ifndef NET_EVENT_H 61933707f3Ssthen #define NET_EVENT_H 62933707f3Ssthen 638b7325afSsthen #include <sys/time.h> 642be9e038Ssthen #include "dnscrypt/dnscrypt.h" 652c144df0Ssthen #ifdef HAVE_NGHTTP2_NGHTTP2_H 662c144df0Ssthen #include <nghttp2/nghttp2.h> 672c144df0Ssthen #endif 682be9e038Ssthen 690b68ff31Ssthen struct sldns_buffer; 70933707f3Ssthen struct comm_point; 71933707f3Ssthen struct comm_reply; 722308e98cSsthen struct tcl_list; 732ee382b6Ssthen struct ub_event_base; 74191f22c6Ssthen struct unbound_socket; 75933707f3Ssthen 762c144df0Ssthen struct mesh_state; 772c144df0Ssthen struct mesh_area; 782c144df0Ssthen 79933707f3Ssthen /* internal event notification data storage structure. */ 80933707f3Ssthen struct internal_event; 81933707f3Ssthen struct internal_base; 822ee382b6Ssthen struct internal_timer; /* A sub struct of the comm_timer super struct */ 83933707f3Ssthen 842c144df0Ssthen enum listen_type; 852c144df0Ssthen 86933707f3Ssthen /** callback from communication point function type */ 8777079be7Ssthen typedef int comm_point_callback_type(struct comm_point*, void*, int, 88933707f3Ssthen struct comm_reply*); 89933707f3Ssthen 90933707f3Ssthen /** to pass no_error to callback function */ 91933707f3Ssthen #define NETEVENT_NOERROR 0 92933707f3Ssthen /** to pass closed connection to callback function */ 93933707f3Ssthen #define NETEVENT_CLOSED -1 94933707f3Ssthen /** to pass timeout happened to callback function */ 95933707f3Ssthen #define NETEVENT_TIMEOUT -2 96933707f3Ssthen /** to pass fallback from capsforID to callback function; 0x20 failed */ 97933707f3Ssthen #define NETEVENT_CAPSFAIL -3 98938a3a5eSflorian /** to pass done transfer to callback function; http file is complete */ 99938a3a5eSflorian #define NETEVENT_DONE -4 100eba819a2Ssthen /** to pass write of the write packet is done to callback function 101eba819a2Ssthen * used when tcp_write_and_read is enabled */ 102eba819a2Ssthen #define NETEVENT_PKT_WRITTEN -5 103933707f3Ssthen 104af4988b1Ssthen /** timeout to slow accept calls when not possible, in msec. */ 105af4988b1Ssthen #define NETEVENT_SLOW_ACCEPT_TIME 2000 10645872187Ssthen /** timeout to slow down log print, so it does not spam the logs, in sec */ 10745872187Ssthen #define SLOW_LOG_TIME 10 108af4988b1Ssthen 109933707f3Ssthen /** 110933707f3Ssthen * A communication point dispatcher. Thread specific. 111933707f3Ssthen */ 112933707f3Ssthen struct comm_base { 113933707f3Ssthen /** behind the scenes structure. with say libevent info. alloced */ 114933707f3Ssthen struct internal_base* eb; 115af4988b1Ssthen /** callback to stop listening on accept sockets, 116af4988b1Ssthen * performed when accept() will not function properly */ 117af4988b1Ssthen void (*stop_accept)(void*); 118af4988b1Ssthen /** callback to start listening on accept sockets, performed 119af4988b1Ssthen * after stop_accept() then a timeout has passed. */ 120af4988b1Ssthen void (*start_accept)(void*); 121af4988b1Ssthen /** user argument for stop_accept and start_accept functions */ 122af4988b1Ssthen void* cb_arg; 123933707f3Ssthen }; 124933707f3Ssthen 125933707f3Ssthen /** 126933707f3Ssthen * Reply information for a communication point. 127933707f3Ssthen */ 128933707f3Ssthen struct comm_reply { 129933707f3Ssthen /** the comm_point with fd to send reply on to. */ 130933707f3Ssthen struct comm_point* c; 131933707f3Ssthen /** the address (for UDP based communication) */ 13245872187Ssthen struct sockaddr_storage remote_addr; 133933707f3Ssthen /** length of address */ 13445872187Ssthen socklen_t remote_addrlen; 13545872187Ssthen /** return type 0 (none), 4(IP4), 6(IP6) 13645872187Ssthen * used only with listen_type_udp_ancil* */ 137933707f3Ssthen int srctype; 1382be9e038Ssthen /* DnsCrypt context */ 1392be9e038Ssthen #ifdef USE_DNSCRYPT 1402be9e038Ssthen uint8_t client_nonce[crypto_box_HALF_NONCEBYTES]; 1412be9e038Ssthen uint8_t nmkey[crypto_box_BEFORENMBYTES]; 1422be9e038Ssthen const dnsccert *dnsc_cert; 1432be9e038Ssthen int is_dnscrypted; 1442be9e038Ssthen #endif 145933707f3Ssthen /** the return source interface data */ 146933707f3Ssthen union { 147933707f3Ssthen #ifdef IPV6_PKTINFO 148933707f3Ssthen struct in6_pktinfo v6info; 149933707f3Ssthen #endif 150933707f3Ssthen #ifdef IP_PKTINFO 151933707f3Ssthen struct in_pktinfo v4info; 152933707f3Ssthen #elif defined(IP_RECVDSTADDR) 153933707f3Ssthen struct in_addr v4addr; 154933707f3Ssthen #endif 155933707f3Ssthen } 156933707f3Ssthen /** variable with return source data */ 157933707f3Ssthen pktinfo; 1582be9e038Ssthen /** max udp size for udp packets */ 1592be9e038Ssthen size_t max_udp_size; 16045872187Ssthen /* if set, the request came through a proxy */ 16145872187Ssthen int is_proxied; 16245872187Ssthen /** the client address 16345872187Ssthen * the same as remote_addr if not proxied */ 16445872187Ssthen struct sockaddr_storage client_addr; 16545872187Ssthen /** the original address length */ 16645872187Ssthen socklen_t client_addrlen; 167933707f3Ssthen }; 168933707f3Ssthen 169933707f3Ssthen /** 170933707f3Ssthen * Communication point to the network 171933707f3Ssthen * These behaviours can be accomplished by setting the flags 172933707f3Ssthen * and passing return values from the callback. 173933707f3Ssthen * udp frontside: called after readdone. sendafter. 174933707f3Ssthen * tcp frontside: called readdone, sendafter. close. 175933707f3Ssthen * udp behind: called after readdone. No send after. 176933707f3Ssthen * tcp behind: write done, read done, then called. No send after. 177933707f3Ssthen */ 178933707f3Ssthen struct comm_point { 179933707f3Ssthen /** behind the scenes structure, with say libevent info. alloced. */ 180933707f3Ssthen struct internal_event* ev; 1819982a05dSsthen /** if the event is added or not */ 1829982a05dSsthen int event_added; 183933707f3Ssthen 1842bdc0ed1Ssthen /** Reference to struct that is part of the listening ports, 1852bdc0ed1Ssthen * where for listening ports information is kept about the address. */ 186191f22c6Ssthen struct unbound_socket* socket; 187191f22c6Ssthen 188933707f3Ssthen /** file descriptor for communication point */ 189933707f3Ssthen int fd; 190933707f3Ssthen 191933707f3Ssthen /** timeout (NULL if it does not). Malloced. */ 192933707f3Ssthen struct timeval* timeout; 193933707f3Ssthen 194933707f3Ssthen /** buffer pointer. Either to perthread, or own buffer or NULL */ 1950b68ff31Ssthen struct sldns_buffer* buffer; 196933707f3Ssthen 197933707f3Ssthen /* -------- TCP Handler -------- */ 198933707f3Ssthen /** Read/Write state for TCP */ 199933707f3Ssthen int tcp_is_reading; 200933707f3Ssthen /** The current read/write count for TCP */ 201933707f3Ssthen size_t tcp_byte_count; 202933707f3Ssthen /** parent communication point (for TCP sockets) */ 203933707f3Ssthen struct comm_point* tcp_parent; 204933707f3Ssthen /** sockaddr from peer, for TCP handlers */ 205933707f3Ssthen struct comm_reply repinfo; 206933707f3Ssthen 207933707f3Ssthen /* -------- TCP Accept -------- */ 208933707f3Ssthen /** the number of TCP handlers for this tcp-accept socket */ 209933707f3Ssthen int max_tcp_count; 210a58bff56Ssthen /** current number of tcp handler in-use for this accept socket */ 211a58bff56Ssthen int cur_tcp_count; 212933707f3Ssthen /** malloced array of tcp handlers for a tcp-accept, 213933707f3Ssthen of size max_tcp_count. */ 214933707f3Ssthen struct comm_point** tcp_handlers; 215933707f3Ssthen /** linked list of free tcp_handlers to use for new queries. 216933707f3Ssthen For tcp_accept the first entry, for tcp_handlers the next one. */ 217933707f3Ssthen struct comm_point* tcp_free; 218933707f3Ssthen 219933707f3Ssthen /* -------- SSL TCP DNS ------- */ 220933707f3Ssthen /** the SSL object with rw bio (owned) or for commaccept ctx ref */ 221933707f3Ssthen void* ssl; 222933707f3Ssthen /** handshake state for init and renegotiate */ 223933707f3Ssthen enum { 224933707f3Ssthen /** no handshake, it has been done */ 225933707f3Ssthen comm_ssl_shake_none = 0, 226933707f3Ssthen /** ssl initial handshake wants to read */ 227933707f3Ssthen comm_ssl_shake_read, 228933707f3Ssthen /** ssl initial handshake wants to write */ 229933707f3Ssthen comm_ssl_shake_write, 230933707f3Ssthen /** ssl_write wants to read */ 231933707f3Ssthen comm_ssl_shake_hs_read, 232933707f3Ssthen /** ssl_read wants to write */ 233933707f3Ssthen comm_ssl_shake_hs_write 234933707f3Ssthen } ssl_shake_state; 235933707f3Ssthen 236938a3a5eSflorian /* -------- HTTP ------- */ 2372c144df0Ssthen /** Do not allow connection to use HTTP version lower than this. 0=no 2382c144df0Ssthen * minimum. */ 2392c144df0Ssthen enum { 2402c144df0Ssthen http_version_none = 0, 2412c144df0Ssthen http_version_2 = 2 2422c144df0Ssthen } http_min_version; 2432c144df0Ssthen /** http endpoint */ 2442c144df0Ssthen char* http_endpoint; 2452c144df0Ssthen /* -------- HTTP/1.1 ------- */ 246938a3a5eSflorian /** Currently reading in http headers */ 247938a3a5eSflorian int http_in_headers; 248938a3a5eSflorian /** Currently reading in chunk headers, 0=not, 1=firstline, 2=unused 249938a3a5eSflorian * (more lines), 3=trailer headers after chunk */ 250938a3a5eSflorian int http_in_chunk_headers; 251938a3a5eSflorian /** chunked transfer */ 252938a3a5eSflorian int http_is_chunked; 253938a3a5eSflorian /** http temp buffer (shared buffer for temporary work) */ 254938a3a5eSflorian struct sldns_buffer* http_temp; 255938a3a5eSflorian /** http stored content in buffer */ 256938a3a5eSflorian size_t http_stored; 2572c144df0Ssthen /* -------- HTTP/2 ------- */ 2582c144df0Ssthen /** http2 session */ 2592c144df0Ssthen struct http2_session* h2_session; 2602c144df0Ssthen /** set to 1 if h2 is negotiated to be used (using alpn) */ 2612c144df0Ssthen int use_h2; 2622c144df0Ssthen /** stream currently being handled */ 2632c144df0Ssthen struct http2_stream* h2_stream; 2642c144df0Ssthen /** maximum allowed query buffer size, per stream */ 2652c144df0Ssthen size_t http2_stream_max_qbuffer_size; 2662c144df0Ssthen /** maximum number of HTTP/2 streams per connection. Send in HTTP/2 2672c144df0Ssthen * SETTINGS frame. */ 2682c144df0Ssthen uint32_t http2_max_streams; 269938a3a5eSflorian 270e10d3884Sbrad /* -------- dnstap ------- */ 271e10d3884Sbrad /** the dnstap environment */ 272e10d3884Sbrad struct dt_env* dtenv; 273e10d3884Sbrad 274933707f3Ssthen /** is this a UDP, TCP-accept or TCP socket. */ 275933707f3Ssthen enum comm_point_type { 276933707f3Ssthen /** UDP socket - handle datagrams. */ 277933707f3Ssthen comm_udp, 278933707f3Ssthen /** TCP accept socket - only creates handlers if readable. */ 279933707f3Ssthen comm_tcp_accept, 280933707f3Ssthen /** TCP handler socket - handle byteperbyte readwrite. */ 281933707f3Ssthen comm_tcp, 282938a3a5eSflorian /** HTTP handler socket */ 283938a3a5eSflorian comm_http, 284933707f3Ssthen /** AF_UNIX socket - for internal commands. */ 285933707f3Ssthen comm_local, 286933707f3Ssthen /** raw - not DNS format - for pipe readers and writers */ 287933707f3Ssthen comm_raw 288933707f3Ssthen } 289933707f3Ssthen /** variable with type of socket, UDP,TCP-accept,TCP,pipe */ 290933707f3Ssthen type; 291933707f3Ssthen 29245872187Ssthen /* -------- PROXYv2 ------- */ 29345872187Ssthen /** if set, PROXYv2 is expected on this connection */ 29445872187Ssthen int pp2_enabled; 29545872187Ssthen /** header state for the PROXYv2 header (for TCP) */ 29645872187Ssthen enum { 29745872187Ssthen /** no header encounter yet */ 29845872187Ssthen pp2_header_none = 0, 29945872187Ssthen /** read the static part of the header */ 30045872187Ssthen pp2_header_init, 30145872187Ssthen /** read the full header */ 30245872187Ssthen pp2_header_done 30345872187Ssthen } pp2_header_state; 30445872187Ssthen 305933707f3Ssthen /* ---------- Behaviour ----------- */ 306933707f3Ssthen /** if set the connection is NOT closed on delete. */ 307933707f3Ssthen int do_not_close; 308933707f3Ssthen 309933707f3Ssthen /** if set, the connection is closed on error, on timeout, 310933707f3Ssthen and after read/write completes. No callback is done. */ 311933707f3Ssthen int tcp_do_close; 312933707f3Ssthen 313eba819a2Ssthen /** flag that indicates the stream is both written and read from. */ 314eba819a2Ssthen int tcp_write_and_read; 315eba819a2Ssthen 316eba819a2Ssthen /** byte count for written length over write channel, for when 317eba819a2Ssthen * tcp_write_and_read is enabled. When tcp_write_and_read is enabled, 318eba819a2Ssthen * this is the counter for writing, the one for reading is in the 319eba819a2Ssthen * commpoint.buffer sldns buffer. The counter counts from 0 to 320eba819a2Ssthen * 2+tcp_write_pkt_len, and includes the tcp length bytes. */ 321eba819a2Ssthen size_t tcp_write_byte_count; 322eba819a2Ssthen 323eba819a2Ssthen /** packet to write currently over the write channel. for when 324eba819a2Ssthen * tcp_write_and_read is enabled. When tcp_write_and_read is enabled, 325eba819a2Ssthen * this is the buffer for the written packet, the commpoint.buffer 326eba819a2Ssthen * sldns buffer is the buffer for the received packet. */ 327eba819a2Ssthen uint8_t* tcp_write_pkt; 328eba819a2Ssthen /** length of tcp_write_pkt in bytes */ 329eba819a2Ssthen size_t tcp_write_pkt_len; 330eba819a2Ssthen 331eba819a2Ssthen /** if set try to read another packet again (over connection with 332eba819a2Ssthen * multiple packets), once set, tries once, then zero again, 333eba819a2Ssthen * so set it in the packet complete section. 334eba819a2Ssthen * The pointer itself has to be set before the callback is invoked, 335eba819a2Ssthen * when you set things up, and continue to exist also after the 336eba819a2Ssthen * commpoint is closed and deleted in your callback. So that after 337eba819a2Ssthen * the callback cleans up netevent can see what it has to do. 338eba819a2Ssthen * Or leave NULL if it is not used at all. */ 339eba819a2Ssthen int* tcp_more_read_again; 340eba819a2Ssthen 341eba819a2Ssthen /** if set try to write another packet (over connection with 342eba819a2Ssthen * multiple packets), once set, tries once, then zero again, 343eba819a2Ssthen * so set it in the packet complete section. 344eba819a2Ssthen * The pointer itself has to be set before the callback is invoked, 345eba819a2Ssthen * when you set things up, and continue to exist also after the 346eba819a2Ssthen * commpoint is closed and deleted in your callback. So that after 347eba819a2Ssthen * the callback cleans up netevent can see what it has to do. 348eba819a2Ssthen * Or leave NULL if it is not used at all. */ 349eba819a2Ssthen int* tcp_more_write_again; 350eba819a2Ssthen 351933707f3Ssthen /** if set, read/write completes: 352933707f3Ssthen read/write state of tcp is toggled. 353933707f3Ssthen buffer reset/bytecount reset. 354933707f3Ssthen this flag cleared. 355933707f3Ssthen So that when that is done the callback is called. */ 356933707f3Ssthen int tcp_do_toggle_rw; 357933707f3Ssthen 35877079be7Ssthen /** timeout in msec for TCP wait times for this connection */ 35977079be7Ssthen int tcp_timeout_msec; 36077079be7Ssthen 3612308e98cSsthen /** if set, tcp keepalive is enabled on this connection */ 3622308e98cSsthen int tcp_keepalive; 3632308e98cSsthen 364933707f3Ssthen /** if set, checks for pending error from nonblocking connect() call.*/ 365933707f3Ssthen int tcp_check_nb_connect; 366933707f3Ssthen 3672308e98cSsthen /** if set, check for connection limit on tcp accept. */ 3682308e98cSsthen struct tcl_list* tcp_conn_limit; 3692308e98cSsthen /** the entry for the connection. */ 3702308e98cSsthen struct tcl_addr* tcl_addr; 3712308e98cSsthen 372f6b99bafSsthen /** the structure to keep track of open requests on this channel */ 373f6b99bafSsthen struct tcp_req_info* tcp_req_info; 374f6b99bafSsthen 37577079be7Ssthen #ifdef USE_MSG_FASTOPEN 37677079be7Ssthen /** used to track if the sendto() call should be done when using TFO. */ 37777079be7Ssthen int tcp_do_fastopen; 37877079be7Ssthen #endif 37977079be7Ssthen 3802be9e038Ssthen #ifdef USE_DNSCRYPT 3812be9e038Ssthen /** Is this a dnscrypt channel */ 3822be9e038Ssthen int dnscrypt; 3832be9e038Ssthen /** encrypted buffer pointer. Either to perthread, or own buffer or NULL */ 3842be9e038Ssthen struct sldns_buffer* dnscrypt_buffer; 3852be9e038Ssthen #endif 386933707f3Ssthen /** number of queries outstanding on this socket, used by 387933707f3Ssthen * outside network for udp ports */ 388933707f3Ssthen int inuse; 3898b7325afSsthen /** the timestamp when the packet was received by the kernel */ 3908b7325afSsthen struct timeval recv_tv; 391933707f3Ssthen /** callback when done. 392933707f3Ssthen tcp_accept does not get called back, is NULL then. 393933707f3Ssthen If a timeout happens, callback with timeout=1 is called. 394933707f3Ssthen If an error happens, callback is called with error set 395933707f3Ssthen nonzero. If not NETEVENT_NOERROR, it is an errno value. 396933707f3Ssthen If the connection is closed (by remote end) then the 397933707f3Ssthen callback is called with error set to NETEVENT_CLOSED=-1. 398933707f3Ssthen If a timeout happens on the connection, the error is set to 399933707f3Ssthen NETEVENT_TIMEOUT=-2. 400933707f3Ssthen The reply_info can be copied if the reply needs to happen at a 401933707f3Ssthen later time. It consists of a struct with commpoint and address. 402933707f3Ssthen It can be passed to a msg send routine some time later. 403933707f3Ssthen Note the reply information is temporary and must be copied. 404933707f3Ssthen NULL is passed for_reply info, in cases where error happened. 405933707f3Ssthen 406933707f3Ssthen declare as: 407933707f3Ssthen int my_callback(struct comm_point* c, void* my_arg, int error, 408933707f3Ssthen struct comm_reply *reply_info); 409933707f3Ssthen 410933707f3Ssthen if the routine returns 0, nothing is done. 411933707f3Ssthen Notzero, the buffer will be sent back to client. 412933707f3Ssthen For UDP this is done without changing the commpoint. 413933707f3Ssthen In TCP it sets write state. 414933707f3Ssthen */ 41577079be7Ssthen comm_point_callback_type* callback; 416933707f3Ssthen /** argument to pass to callback. */ 417933707f3Ssthen void *cb_arg; 418933707f3Ssthen }; 419933707f3Ssthen 420933707f3Ssthen /** 421933707f3Ssthen * Structure only for making timeout events. 422933707f3Ssthen */ 423933707f3Ssthen struct comm_timer { 4242ee382b6Ssthen /** the internal event stuff (derived) */ 425933707f3Ssthen struct internal_timer* ev_timer; 426933707f3Ssthen 427933707f3Ssthen /** callback function, takes user arg only */ 428933707f3Ssthen void (*callback)(void*); 429933707f3Ssthen 430933707f3Ssthen /** callback user argument */ 431933707f3Ssthen void* cb_arg; 432933707f3Ssthen }; 433933707f3Ssthen 434933707f3Ssthen /** 435933707f3Ssthen * Structure only for signal events. 436933707f3Ssthen */ 437933707f3Ssthen struct comm_signal { 438933707f3Ssthen /** the communication base */ 439933707f3Ssthen struct comm_base* base; 440933707f3Ssthen 441933707f3Ssthen /** the internal event stuff */ 442933707f3Ssthen struct internal_signal* ev_signal; 443933707f3Ssthen 444933707f3Ssthen /** callback function, takes signal number and user arg */ 445933707f3Ssthen void (*callback)(int, void*); 446933707f3Ssthen 447933707f3Ssthen /** callback user argument */ 448933707f3Ssthen void* cb_arg; 449933707f3Ssthen }; 450933707f3Ssthen 451933707f3Ssthen /** 452933707f3Ssthen * Create a new comm base. 453933707f3Ssthen * @param sigs: if true it attempts to create a default loop for 454933707f3Ssthen * signal handling. 455933707f3Ssthen * @return: the new comm base. NULL on error. 456933707f3Ssthen */ 457933707f3Ssthen struct comm_base* comm_base_create(int sigs); 458933707f3Ssthen 459933707f3Ssthen /** 4602ee382b6Ssthen * Create comm base that uses the given ub_event_base (underlying pluggable 4612ee382b6Ssthen * event mechanism pointer). 4622ee382b6Ssthen * @param base: underlying pluggable event base. 4630b68ff31Ssthen * @return: the new comm base. NULL on error. 4640b68ff31Ssthen */ 4652ee382b6Ssthen struct comm_base* comm_base_create_event(struct ub_event_base* base); 4660b68ff31Ssthen 4670b68ff31Ssthen /** 4680b68ff31Ssthen * Delete comm base structure but not the underlying lib event base. 4690b68ff31Ssthen * All comm points must have been deleted. 4700b68ff31Ssthen * @param b: the base to delete. 4710b68ff31Ssthen */ 4720b68ff31Ssthen void comm_base_delete_no_base(struct comm_base* b); 4730b68ff31Ssthen 4740b68ff31Ssthen /** 475933707f3Ssthen * Destroy a comm base. 476933707f3Ssthen * All comm points must have been deleted. 477933707f3Ssthen * @param b: the base to delete. 478933707f3Ssthen */ 479933707f3Ssthen void comm_base_delete(struct comm_base* b); 480933707f3Ssthen 481933707f3Ssthen /** 482933707f3Ssthen * Obtain two pointers. The pointers never change (until base_delete()). 483933707f3Ssthen * The pointers point to time values that are updated regularly. 484933707f3Ssthen * @param b: the communication base that will update the time values. 485933707f3Ssthen * @param tt: pointer to time in seconds is returned. 486933707f3Ssthen * @param tv: pointer to time in microseconds is returned. 487933707f3Ssthen */ 488e9c7b4efSsthen void comm_base_timept(struct comm_base* b, time_t** tt, struct timeval** tv); 489933707f3Ssthen 490933707f3Ssthen /** 491933707f3Ssthen * Dispatch the comm base events. 492933707f3Ssthen * @param b: the communication to perform. 493933707f3Ssthen */ 494933707f3Ssthen void comm_base_dispatch(struct comm_base* b); 495933707f3Ssthen 496933707f3Ssthen /** 497933707f3Ssthen * Exit from dispatch loop. 498933707f3Ssthen * @param b: the communication base that is in dispatch(). 499933707f3Ssthen */ 500933707f3Ssthen void comm_base_exit(struct comm_base* b); 501933707f3Ssthen 502933707f3Ssthen /** 503af4988b1Ssthen * Set the slow_accept mode handlers. You can not provide these if you do 504af4988b1Ssthen * not perform accept() calls. 505af4988b1Ssthen * @param b: comm base 506af4988b1Ssthen * @param stop_accept: function that stops listening to accept fds. 507af4988b1Ssthen * @param start_accept: function that resumes listening to accept fds. 508af4988b1Ssthen * @param arg: callback arg to pass to the functions. 509af4988b1Ssthen */ 510af4988b1Ssthen void comm_base_set_slow_accept_handlers(struct comm_base* b, 511af4988b1Ssthen void (*stop_accept)(void*), void (*start_accept)(void*), void* arg); 512af4988b1Ssthen 513af4988b1Ssthen /** 514933707f3Ssthen * Access internal data structure (for util/tube.c on windows) 515933707f3Ssthen * @param b: comm base 5162ee382b6Ssthen * @return ub_event_base. 517933707f3Ssthen */ 5182ee382b6Ssthen struct ub_event_base* comm_base_internal(struct comm_base* b); 519933707f3Ssthen 520933707f3Ssthen /** 521933707f3Ssthen * Create an UDP comm point. Calls malloc. 522933707f3Ssthen * setups the structure with the parameters you provide. 523933707f3Ssthen * @param base: in which base to alloc the commpoint. 524933707f3Ssthen * @param fd: file descriptor of open UDP socket. 525933707f3Ssthen * @param buffer: shared buffer by UDP sockets from this thread. 52645872187Ssthen * @param pp2_enabled: if the comm point will support PROXYv2. 527933707f3Ssthen * @param callback: callback function pointer. 528933707f3Ssthen * @param callback_arg: will be passed to your callback function. 529191f22c6Ssthen * @param socket: and opened socket properties will be passed to your callback function. 530933707f3Ssthen * @return: returns the allocated communication point. NULL on error. 531933707f3Ssthen * Sets timeout to NULL. Turns off TCP options. 532933707f3Ssthen */ 533933707f3Ssthen struct comm_point* comm_point_create_udp(struct comm_base* base, 53445872187Ssthen int fd, struct sldns_buffer* buffer, int pp2_enabled, 535191f22c6Ssthen comm_point_callback_type* callback, void* callback_arg, struct unbound_socket* socket); 536933707f3Ssthen 537933707f3Ssthen /** 538933707f3Ssthen * Create an UDP with ancillary data comm point. Calls malloc. 539933707f3Ssthen * Uses recvmsg instead of recv to get udp message. 540933707f3Ssthen * setups the structure with the parameters you provide. 541933707f3Ssthen * @param base: in which base to alloc the commpoint. 542933707f3Ssthen * @param fd: file descriptor of open UDP socket. 543933707f3Ssthen * @param buffer: shared buffer by UDP sockets from this thread. 54445872187Ssthen * @param pp2_enabled: if the comm point will support PROXYv2. 545933707f3Ssthen * @param callback: callback function pointer. 546933707f3Ssthen * @param callback_arg: will be passed to your callback function. 547191f22c6Ssthen * @param socket: and opened socket properties will be passed to your callback function. 548933707f3Ssthen * @return: returns the allocated communication point. NULL on error. 549933707f3Ssthen * Sets timeout to NULL. Turns off TCP options. 550933707f3Ssthen */ 551933707f3Ssthen struct comm_point* comm_point_create_udp_ancil(struct comm_base* base, 55245872187Ssthen int fd, struct sldns_buffer* buffer, int pp2_enabled, 553191f22c6Ssthen comm_point_callback_type* callback, void* callback_arg, struct unbound_socket* socket); 554933707f3Ssthen 555933707f3Ssthen /** 556933707f3Ssthen * Create a TCP listener comm point. Calls malloc. 557933707f3Ssthen * Setups the structure with the parameters you provide. 558933707f3Ssthen * Also Creates TCP Handlers, pre allocated for you. 559933707f3Ssthen * Uses the parameters you provide. 560933707f3Ssthen * @param base: in which base to alloc the commpoint. 561933707f3Ssthen * @param fd: file descriptor of open TCP socket set to listen nonblocking. 562933707f3Ssthen * @param num: becomes max_tcp_count, the routine allocates that 563933707f3Ssthen * many tcp handler commpoints. 5642308e98cSsthen * @param idle_timeout: TCP idle timeout in ms. 5652c144df0Ssthen * @param harden_large_queries: whether query size should be limited. 5662c144df0Ssthen * @param http_max_streams: maximum number of HTTP/2 streams per connection. 5672c144df0Ssthen * @param http_endpoint: HTTP endpoint to service queries on 5682308e98cSsthen * @param tcp_conn_limit: TCP connection limit info. 569933707f3Ssthen * @param bufsize: size of buffer to create for handlers. 570f6b99bafSsthen * @param spoolbuf: shared spool buffer for tcp_req_info structures. 571f6b99bafSsthen * or NULL to not create those structures in the tcp handlers. 5722c144df0Ssthen * @param port_type: the type of port we are creating a TCP listener for. Used 5732c144df0Ssthen * to select handler type to use. 57445872187Ssthen * @param pp2_enabled: if the comm point will support PROXYv2. 575933707f3Ssthen * @param callback: callback function pointer for TCP handlers. 576933707f3Ssthen * @param callback_arg: will be passed to your callback function. 577191f22c6Ssthen * @param socket: and opened socket properties will be passed to your callback function. 578933707f3Ssthen * @return: returns the TCP listener commpoint. You can find the 579933707f3Ssthen * TCP handlers in the array inside the listener commpoint. 580933707f3Ssthen * returns NULL on error. 581933707f3Ssthen * Inits timeout to NULL. All handlers are on the free list. 582933707f3Ssthen */ 583933707f3Ssthen struct comm_point* comm_point_create_tcp(struct comm_base* base, 5842c144df0Ssthen int fd, int num, int idle_timeout, int harden_large_queries, 5852c144df0Ssthen uint32_t http_max_streams, char* http_endpoint, 5862c144df0Ssthen struct tcl_list* tcp_conn_limit, 587f6b99bafSsthen size_t bufsize, struct sldns_buffer* spoolbuf, 58845872187Ssthen enum listen_type port_type, int pp2_enabled, 589191f22c6Ssthen comm_point_callback_type* callback, void* callback_arg, struct unbound_socket* socket); 590933707f3Ssthen 591933707f3Ssthen /** 592933707f3Ssthen * Create an outgoing TCP commpoint. No file descriptor is opened, left at -1. 593933707f3Ssthen * @param base: in which base to alloc the commpoint. 594933707f3Ssthen * @param bufsize: size of buffer to create for handlers. 595933707f3Ssthen * @param callback: callback function pointer for the handler. 596933707f3Ssthen * @param callback_arg: will be passed to your callback function. 597933707f3Ssthen * @return: the commpoint or NULL on error. 598933707f3Ssthen */ 599933707f3Ssthen struct comm_point* comm_point_create_tcp_out(struct comm_base* base, 60077079be7Ssthen size_t bufsize, comm_point_callback_type* callback, void* callback_arg); 601933707f3Ssthen 602933707f3Ssthen /** 603938a3a5eSflorian * Create an outgoing HTTP commpoint. No file descriptor is opened, left at -1. 604938a3a5eSflorian * @param base: in which base to alloc the commpoint. 605938a3a5eSflorian * @param bufsize: size of buffer to create for handlers. 606938a3a5eSflorian * @param callback: callback function pointer for the handler. 607938a3a5eSflorian * @param callback_arg: will be passed to your callback function. 608938a3a5eSflorian * @param temp: sldns buffer, shared between other http_out commpoints, for 609938a3a5eSflorian * temporary data when performing callbacks. 610938a3a5eSflorian * @return: the commpoint or NULL on error. 611938a3a5eSflorian */ 612938a3a5eSflorian struct comm_point* comm_point_create_http_out(struct comm_base* base, 613938a3a5eSflorian size_t bufsize, comm_point_callback_type* callback, 614938a3a5eSflorian void* callback_arg, struct sldns_buffer* temp); 615938a3a5eSflorian 616938a3a5eSflorian /** 617933707f3Ssthen * Create commpoint to listen to a local domain file descriptor. 618933707f3Ssthen * @param base: in which base to alloc the commpoint. 619933707f3Ssthen * @param fd: file descriptor of open AF_UNIX socket set to listen nonblocking. 620933707f3Ssthen * @param bufsize: size of buffer to create for handlers. 621933707f3Ssthen * @param callback: callback function pointer for the handler. 622933707f3Ssthen * @param callback_arg: will be passed to your callback function. 623933707f3Ssthen * @return: the commpoint or NULL on error. 624933707f3Ssthen */ 625933707f3Ssthen struct comm_point* comm_point_create_local(struct comm_base* base, 626933707f3Ssthen int fd, size_t bufsize, 62777079be7Ssthen comm_point_callback_type* callback, void* callback_arg); 628933707f3Ssthen 629933707f3Ssthen /** 630933707f3Ssthen * Create commpoint to listen to a local domain pipe descriptor. 631933707f3Ssthen * @param base: in which base to alloc the commpoint. 632933707f3Ssthen * @param fd: file descriptor. 633933707f3Ssthen * @param writing: true if you want to listen to writes, false for reads. 634933707f3Ssthen * @param callback: callback function pointer for the handler. 635933707f3Ssthen * @param callback_arg: will be passed to your callback function. 636933707f3Ssthen * @return: the commpoint or NULL on error. 637933707f3Ssthen */ 638933707f3Ssthen struct comm_point* comm_point_create_raw(struct comm_base* base, 639933707f3Ssthen int fd, int writing, 64077079be7Ssthen comm_point_callback_type* callback, void* callback_arg); 641933707f3Ssthen 642933707f3Ssthen /** 643933707f3Ssthen * Close a comm point fd. 644933707f3Ssthen * @param c: comm point to close. 645933707f3Ssthen */ 646933707f3Ssthen void comm_point_close(struct comm_point* c); 647933707f3Ssthen 648933707f3Ssthen /** 649933707f3Ssthen * Close and deallocate (free) the comm point. If the comm point is 650933707f3Ssthen * a tcp-accept point, also its tcp-handler points are deleted. 651933707f3Ssthen * @param c: comm point to delete. 652933707f3Ssthen */ 653933707f3Ssthen void comm_point_delete(struct comm_point* c); 654933707f3Ssthen 655933707f3Ssthen /** 656933707f3Ssthen * Send reply. Put message into commpoint buffer. 657933707f3Ssthen * @param repinfo: The reply info copied from a commpoint callback call. 658933707f3Ssthen */ 659933707f3Ssthen void comm_point_send_reply(struct comm_reply* repinfo); 660933707f3Ssthen 661933707f3Ssthen /** 662933707f3Ssthen * Drop reply. Cleans up. 663933707f3Ssthen * @param repinfo: The reply info copied from a commpoint callback call. 664933707f3Ssthen */ 665933707f3Ssthen void comm_point_drop_reply(struct comm_reply* repinfo); 666933707f3Ssthen 667933707f3Ssthen /** 668933707f3Ssthen * Send an udp message over a commpoint. 669933707f3Ssthen * @param c: commpoint to send it from. 670933707f3Ssthen * @param packet: what to send. 671eba819a2Ssthen * @param addr: where to send it to. If NULL, send is performed, 672eba819a2Ssthen * for connected sockets, to the connected address. 673933707f3Ssthen * @param addrlen: length of addr. 6749982a05dSsthen * @param is_connected: if the UDP socket is connect()ed. 675933707f3Ssthen * @return: false on a failure. 676933707f3Ssthen */ 6770b68ff31Ssthen int comm_point_send_udp_msg(struct comm_point* c, struct sldns_buffer* packet, 6789982a05dSsthen struct sockaddr* addr, socklen_t addrlen,int is_connected); 679933707f3Ssthen 680933707f3Ssthen /** 681933707f3Ssthen * Stop listening for input on the commpoint. No callbacks will happen. 682933707f3Ssthen * @param c: commpoint to disable. The fd is not closed. 683933707f3Ssthen */ 684933707f3Ssthen void comm_point_stop_listening(struct comm_point* c); 685933707f3Ssthen 686933707f3Ssthen /** 687933707f3Ssthen * Start listening again for input on the comm point. 688933707f3Ssthen * @param c: commpoint to enable again. 689933707f3Ssthen * @param newfd: new fd, or -1 to leave fd be. 69077079be7Ssthen * @param msec: timeout in milliseconds, or -1 for no (change to the) timeout. 69177079be7Ssthen * So seconds*1000. 692933707f3Ssthen */ 69377079be7Ssthen void comm_point_start_listening(struct comm_point* c, int newfd, int msec); 694933707f3Ssthen 695933707f3Ssthen /** 696933707f3Ssthen * Stop listening and start listening again for reading or writing. 697933707f3Ssthen * @param c: commpoint 698933707f3Ssthen * @param rd: if true, listens for reading. 699933707f3Ssthen * @param wr: if true, listens for writing. 700933707f3Ssthen */ 701933707f3Ssthen void comm_point_listen_for_rw(struct comm_point* c, int rd, int wr); 702933707f3Ssthen 703933707f3Ssthen /** 7049982a05dSsthen * For TCP handlers that use c->tcp_timeout_msec, this routine adjusts 7059982a05dSsthen * it with the minimum. Otherwise, a 0 value advertised without the 7069982a05dSsthen * minimum applied moves to a 0 in comm_point_start_listening and that 7079982a05dSsthen * routine treats it as no timeout, listen forever, which is not wanted. 7089982a05dSsthen * @param c: comm point to use the tcp_timeout_msec of. 7099982a05dSsthen * @return adjusted tcp_timeout_msec value with the minimum if smaller. 7109982a05dSsthen */ 7119982a05dSsthen int adjusted_tcp_timeout(struct comm_point* c); 7129982a05dSsthen 7139982a05dSsthen /** 714933707f3Ssthen * Get size of memory used by comm point. 715933707f3Ssthen * For TCP handlers this includes subhandlers. 716933707f3Ssthen * For UDP handlers, this does not include the (shared) UDP buffer. 717933707f3Ssthen * @param c: commpoint. 718933707f3Ssthen * @return size in bytes. 719933707f3Ssthen */ 720933707f3Ssthen size_t comm_point_get_mem(struct comm_point* c); 721933707f3Ssthen 722933707f3Ssthen /** 723933707f3Ssthen * create timer. Not active upon creation. 724933707f3Ssthen * @param base: event handling base. 725933707f3Ssthen * @param cb: callback function: void myfunc(void* myarg); 726933707f3Ssthen * @param cb_arg: user callback argument. 727933707f3Ssthen * @return: the new timer or NULL on error. 728933707f3Ssthen */ 729933707f3Ssthen struct comm_timer* comm_timer_create(struct comm_base* base, 730933707f3Ssthen void (*cb)(void*), void* cb_arg); 731933707f3Ssthen 732933707f3Ssthen /** 733933707f3Ssthen * disable timer. Stops callbacks from happening. 734933707f3Ssthen * @param timer: to disable. 735933707f3Ssthen */ 736933707f3Ssthen void comm_timer_disable(struct comm_timer* timer); 737933707f3Ssthen 738933707f3Ssthen /** 739933707f3Ssthen * reset timevalue for timer. 740933707f3Ssthen * @param timer: timer to (re)set. 741933707f3Ssthen * @param tv: when the timer should activate. if NULL timer is disabled. 742933707f3Ssthen */ 743933707f3Ssthen void comm_timer_set(struct comm_timer* timer, struct timeval* tv); 744933707f3Ssthen 745933707f3Ssthen /** 746933707f3Ssthen * delete timer. 747933707f3Ssthen * @param timer: to delete. 748933707f3Ssthen */ 749933707f3Ssthen void comm_timer_delete(struct comm_timer* timer); 750933707f3Ssthen 751933707f3Ssthen /** 752933707f3Ssthen * see if timeout has been set to a value. 753933707f3Ssthen * @param timer: the timer to examine. 754933707f3Ssthen * @return: false if disabled or not set. 755933707f3Ssthen */ 756933707f3Ssthen int comm_timer_is_set(struct comm_timer* timer); 757933707f3Ssthen 758933707f3Ssthen /** 759933707f3Ssthen * Get size of memory used by comm timer. 760933707f3Ssthen * @param timer: the timer to examine. 761933707f3Ssthen * @return size in bytes. 762933707f3Ssthen */ 763933707f3Ssthen size_t comm_timer_get_mem(struct comm_timer* timer); 764933707f3Ssthen 765933707f3Ssthen /** 766933707f3Ssthen * Create a signal handler. Call signal_bind() later to bind to a signal. 767933707f3Ssthen * @param base: communication base to use. 768933707f3Ssthen * @param callback: called when signal is caught. 769933707f3Ssthen * @param cb_arg: user argument to callback 770933707f3Ssthen * @return: the signal struct or NULL on error. 771933707f3Ssthen */ 772933707f3Ssthen struct comm_signal* comm_signal_create(struct comm_base* base, 773933707f3Ssthen void (*callback)(int, void*), void* cb_arg); 774933707f3Ssthen 775933707f3Ssthen /** 776e21c60efSsthen * Bind signal struct to catch a signal. A single comm_signal can be bound 777933707f3Ssthen * to multiple signals, calling comm_signal_bind multiple times. 778933707f3Ssthen * @param comsig: the communication point, with callback information. 779933707f3Ssthen * @param sig: signal number. 780933707f3Ssthen * @return: true on success. false on error. 781933707f3Ssthen */ 782933707f3Ssthen int comm_signal_bind(struct comm_signal* comsig, int sig); 783933707f3Ssthen 784933707f3Ssthen /** 785933707f3Ssthen * Delete the signal communication point. 786933707f3Ssthen * @param comsig: to delete. 787933707f3Ssthen */ 788933707f3Ssthen void comm_signal_delete(struct comm_signal* comsig); 789933707f3Ssthen 790933707f3Ssthen /** 791933707f3Ssthen * perform accept(2) with error checking. 792933707f3Ssthen * @param c: commpoint with accept fd. 793933707f3Ssthen * @param addr: remote end returned here. 794933707f3Ssthen * @param addrlen: length of remote end returned here. 795933707f3Ssthen * @return new fd, or -1 on error. 796933707f3Ssthen * if -1, error message has been printed if necessary, simply drop 797933707f3Ssthen * out of the reading handler. 798933707f3Ssthen */ 799933707f3Ssthen int comm_point_perform_accept(struct comm_point* c, 800933707f3Ssthen struct sockaddr_storage* addr, socklen_t* addrlen); 801933707f3Ssthen 802933707f3Ssthen /**** internal routines ****/ 803933707f3Ssthen 804933707f3Ssthen /** 805933707f3Ssthen * This routine is published for checks and tests, and is only used internally. 806933707f3Ssthen * handle libevent callback for udp comm point. 807933707f3Ssthen * @param fd: file descriptor. 808933707f3Ssthen * @param event: event bits from libevent: 809933707f3Ssthen * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 810933707f3Ssthen * @param arg: the comm_point structure. 811933707f3Ssthen */ 812933707f3Ssthen void comm_point_udp_callback(int fd, short event, void* arg); 813933707f3Ssthen 814933707f3Ssthen /** 815933707f3Ssthen * This routine is published for checks and tests, and is only used internally. 816933707f3Ssthen * handle libevent callback for udp ancillary data comm point. 817933707f3Ssthen * @param fd: file descriptor. 818933707f3Ssthen * @param event: event bits from libevent: 819933707f3Ssthen * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 820933707f3Ssthen * @param arg: the comm_point structure. 821933707f3Ssthen */ 822933707f3Ssthen void comm_point_udp_ancil_callback(int fd, short event, void* arg); 823933707f3Ssthen 824933707f3Ssthen /** 825933707f3Ssthen * This routine is published for checks and tests, and is only used internally. 826933707f3Ssthen * handle libevent callback for tcp accept comm point 827933707f3Ssthen * @param fd: file descriptor. 828933707f3Ssthen * @param event: event bits from libevent: 829933707f3Ssthen * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 830933707f3Ssthen * @param arg: the comm_point structure. 831933707f3Ssthen */ 832933707f3Ssthen void comm_point_tcp_accept_callback(int fd, short event, void* arg); 833933707f3Ssthen 834933707f3Ssthen /** 835933707f3Ssthen * This routine is published for checks and tests, and is only used internally. 836933707f3Ssthen * handle libevent callback for tcp data comm point 837933707f3Ssthen * @param fd: file descriptor. 838933707f3Ssthen * @param event: event bits from libevent: 839933707f3Ssthen * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 840933707f3Ssthen * @param arg: the comm_point structure. 841933707f3Ssthen */ 842933707f3Ssthen void comm_point_tcp_handle_callback(int fd, short event, void* arg); 843933707f3Ssthen 844933707f3Ssthen /** 845933707f3Ssthen * This routine is published for checks and tests, and is only used internally. 846938a3a5eSflorian * handle libevent callback for tcp data comm point 847938a3a5eSflorian * @param fd: file descriptor. 848938a3a5eSflorian * @param event: event bits from libevent: 849938a3a5eSflorian * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 850938a3a5eSflorian * @param arg: the comm_point structure. 851938a3a5eSflorian */ 852938a3a5eSflorian void comm_point_http_handle_callback(int fd, short event, void* arg); 853938a3a5eSflorian 854938a3a5eSflorian /** 8552c144df0Ssthen * HTTP2 session. HTTP2 related info per comm point. 8562c144df0Ssthen */ 8572c144df0Ssthen struct http2_session { 8582c144df0Ssthen /** first item in list of streams */ 8592c144df0Ssthen struct http2_stream* first_stream; 8602c144df0Ssthen #ifdef HAVE_NGHTTP2 8612c144df0Ssthen /** nghttp2 session */ 8622c144df0Ssthen nghttp2_session *session; 8632c144df0Ssthen /** store nghttp2 callbacks for easy reuse */ 8642c144df0Ssthen nghttp2_session_callbacks* callbacks; 8652c144df0Ssthen #endif 8662c144df0Ssthen /** comm point containing buffer used to build answer in worker or 8672c144df0Ssthen * module */ 8682c144df0Ssthen struct comm_point* c; 8692c144df0Ssthen /** session is instructed to get dropped (comm port will be closed) */ 8702c144df0Ssthen int is_drop; 8712c144df0Ssthen /** postpone dropping the session, can be used to prevent dropping 8722c144df0Ssthen * while being in a callback */ 8732c144df0Ssthen int postpone_drop; 8742c144df0Ssthen }; 8752c144df0Ssthen 8762c144df0Ssthen /** enum of HTTP status */ 8772c144df0Ssthen enum http_status { 8782c144df0Ssthen HTTP_STATUS_OK = 200, 8792c144df0Ssthen HTTP_STATUS_BAD_REQUEST = 400, 8802c144df0Ssthen HTTP_STATUS_NOT_FOUND = 404, 8812c144df0Ssthen HTTP_STATUS_PAYLOAD_TOO_LARGE = 413, 8822c144df0Ssthen HTTP_STATUS_URI_TOO_LONG = 414, 8832c144df0Ssthen HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE = 415, 8842c144df0Ssthen HTTP_STATUS_NOT_IMPLEMENTED = 501 8852c144df0Ssthen }; 8862c144df0Ssthen 8872c144df0Ssthen /** 8882c144df0Ssthen * HTTP stream. Part of list of HTTP2 streams per session. 8892c144df0Ssthen */ 8902c144df0Ssthen struct http2_stream { 8912c144df0Ssthen /** next stream in list per session */ 8922c144df0Ssthen struct http2_stream* next; 8932c144df0Ssthen /** previous stream in list per session */ 8942c144df0Ssthen struct http2_stream* prev; 8952c144df0Ssthen /** HTTP2 stream ID is an unsigned 31-bit integer */ 8962c144df0Ssthen int32_t stream_id; 8972c144df0Ssthen /** HTTP method used for this stream */ 8982c144df0Ssthen enum { 8992c144df0Ssthen HTTP_METHOD_POST = 1, 9002c144df0Ssthen HTTP_METHOD_GET, 9012c144df0Ssthen HTTP_METHOD_UNSUPPORTED 9022c144df0Ssthen } http_method; 9032c144df0Ssthen /** message contains invalid content type */ 9042c144df0Ssthen int invalid_content_type; 9052c144df0Ssthen /** message body content type */ 9062c144df0Ssthen size_t content_length; 9072c144df0Ssthen /** HTTP response status */ 9082c144df0Ssthen enum http_status status; 9092c144df0Ssthen /** request for non existing endpoint */ 9102c144df0Ssthen int invalid_endpoint; 9112c144df0Ssthen /** query in request is too large */ 9122c144df0Ssthen int query_too_large; 9132c144df0Ssthen /** buffer to store query into. Can't use session shared buffer as query 9142c144df0Ssthen * can arrive in parts, intertwined with frames for other queries. */ 9152c144df0Ssthen struct sldns_buffer* qbuffer; 9162c144df0Ssthen /** buffer to store response into. Can't use shared buffer as a next 9172c144df0Ssthen * query read callback can overwrite it before it is send out. */ 9182c144df0Ssthen struct sldns_buffer* rbuffer; 9192c144df0Ssthen /** mesh area containing mesh state */ 9202c144df0Ssthen struct mesh_area* mesh; 9212c144df0Ssthen /** mesh state for query. Used to remove mesh reply before closing 9222c144df0Ssthen * stream. */ 9232c144df0Ssthen struct mesh_state* mesh_state; 9242c144df0Ssthen }; 9252c144df0Ssthen 9262c144df0Ssthen #ifdef HAVE_NGHTTP2 9272c144df0Ssthen /** nghttp2 receive cb. Read from SSL connection into nghttp2 buffer */ 9282c144df0Ssthen ssize_t http2_recv_cb(nghttp2_session* session, uint8_t* buf, 9292c144df0Ssthen size_t len, int flags, void* cb_arg); 9302c144df0Ssthen /** nghttp2 send callback. Send from nghttp2 buffer to ssl socket */ 9312c144df0Ssthen ssize_t http2_send_cb(nghttp2_session* session, const uint8_t* buf, 9322c144df0Ssthen size_t len, int flags, void* cb_arg); 9332c144df0Ssthen /** nghttp2 callback on closing stream */ 9342c144df0Ssthen int http2_stream_close_cb(nghttp2_session* session, int32_t stream_id, 9352c144df0Ssthen uint32_t error_code, void* cb_arg); 9362c144df0Ssthen #endif 9372c144df0Ssthen 9382c144df0Ssthen /** 9392c144df0Ssthen * Create new http2 stream 9402c144df0Ssthen * @param stream_id: ID for stream to create. 9412c144df0Ssthen * @return malloc'ed stream, NULL on error 9422c144df0Ssthen */ 9432c144df0Ssthen struct http2_stream* http2_stream_create(int32_t stream_id); 9442c144df0Ssthen 9452c144df0Ssthen /** 9462c144df0Ssthen * Add new stream to session linked list 9472c144df0Ssthen * @param h2_session: http2 session to add stream to 9482c144df0Ssthen * @param h2_stream: stream to add to session list 9492c144df0Ssthen */ 9502c144df0Ssthen void http2_session_add_stream(struct http2_session* h2_session, 9512c144df0Ssthen struct http2_stream* h2_stream); 9522c144df0Ssthen 9532c144df0Ssthen /** Add mesh state to stream. To be able to remove mesh reply on stream closure 9542c144df0Ssthen */ 9552c144df0Ssthen void http2_stream_add_meshstate(struct http2_stream* h2_stream, 9562c144df0Ssthen struct mesh_area* mesh, struct mesh_state* m); 9572c144df0Ssthen 958*98bc733bSsthen /** Remove mesh state from stream. When the mesh state has been removed. */ 959*98bc733bSsthen void http2_stream_remove_mesh_state(struct http2_stream* h2_stream); 960*98bc733bSsthen 9612c144df0Ssthen /** 962938a3a5eSflorian * This routine is published for checks and tests, and is only used internally. 963933707f3Ssthen * handle libevent callback for timer comm. 964933707f3Ssthen * @param fd: file descriptor (always -1). 965933707f3Ssthen * @param event: event bits from libevent: 966933707f3Ssthen * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 967933707f3Ssthen * @param arg: the comm_timer structure. 968933707f3Ssthen */ 969933707f3Ssthen void comm_timer_callback(int fd, short event, void* arg); 970933707f3Ssthen 971933707f3Ssthen /** 972933707f3Ssthen * This routine is published for checks and tests, and is only used internally. 973933707f3Ssthen * handle libevent callback for signal comm. 974933707f3Ssthen * @param fd: file descriptor (used for the signal number). 975933707f3Ssthen * @param event: event bits from libevent: 976933707f3Ssthen * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 977933707f3Ssthen * @param arg: the internal commsignal structure. 978933707f3Ssthen */ 979933707f3Ssthen void comm_signal_callback(int fd, short event, void* arg); 980933707f3Ssthen 981933707f3Ssthen /** 982933707f3Ssthen * This routine is published for checks and tests, and is only used internally. 983933707f3Ssthen * libevent callback for AF_UNIX fds 984933707f3Ssthen * @param fd: file descriptor. 985933707f3Ssthen * @param event: event bits from libevent: 986933707f3Ssthen * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 987933707f3Ssthen * @param arg: the comm_point structure. 988933707f3Ssthen */ 989933707f3Ssthen void comm_point_local_handle_callback(int fd, short event, void* arg); 990933707f3Ssthen 991933707f3Ssthen /** 992933707f3Ssthen * This routine is published for checks and tests, and is only used internally. 993933707f3Ssthen * libevent callback for raw fd access. 994933707f3Ssthen * @param fd: file descriptor. 995933707f3Ssthen * @param event: event bits from libevent: 996933707f3Ssthen * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 997933707f3Ssthen * @param arg: the comm_point structure. 998933707f3Ssthen */ 999933707f3Ssthen void comm_point_raw_handle_callback(int fd, short event, void* arg); 1000933707f3Ssthen 1001af4988b1Ssthen /** 1002af4988b1Ssthen * This routine is published for checks and tests, and is only used internally. 1003af4988b1Ssthen * libevent callback for timeout on slow accept. 1004af4988b1Ssthen * @param fd: file descriptor. 1005af4988b1Ssthen * @param event: event bits from libevent: 1006af4988b1Ssthen * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 1007af4988b1Ssthen * @param arg: the comm_point structure. 1008af4988b1Ssthen */ 1009af4988b1Ssthen void comm_base_handle_slow_accept(int fd, short event, void* arg); 1010af4988b1Ssthen 1011933707f3Ssthen #ifdef USE_WINSOCK 1012933707f3Ssthen /** 1013933707f3Ssthen * Callback for openssl BIO to on windows detect WSAEWOULDBLOCK and notify 1014933707f3Ssthen * the winsock_event of this for proper TCP nonblocking implementation. 1015933707f3Ssthen * @param c: comm_point, fd must be set its struct event is registered. 1016933707f3Ssthen * @param ssl: openssl SSL, fd must be set so it has a bio. 1017933707f3Ssthen */ 1018933707f3Ssthen void comm_point_tcp_win_bio_cb(struct comm_point* c, void* ssl); 1019933707f3Ssthen #endif 1020933707f3Ssthen 1021a3167c07Ssthen /** 1022a3167c07Ssthen * See if errno for tcp connect has to be logged or not. This uses errno 1023a3167c07Ssthen * @param addr: apart from checking errno, the addr is checked for ip4mapped 1024a3167c07Ssthen * and broadcast type, hence passed. 1025a3167c07Ssthen * @param addrlen: length of the addr parameter. 1026a3167c07Ssthen * @return true if it needs to be logged. 1027a3167c07Ssthen */ 1028163a4143Ssthen int tcp_connect_errno_needs_log(struct sockaddr* addr, socklen_t addrlen); 1029163a4143Ssthen 1030a3167c07Ssthen #ifdef HAVE_SSL 1031a3167c07Ssthen /** 1032a3167c07Ssthen * True if the ssl handshake error has to be squelched from the logs 1033a3167c07Ssthen * @param err: the error returned by the openssl routine, ERR_get_error. 1034a3167c07Ssthen * This is a packed structure with elements that are examined. 1035a3167c07Ssthen * @return true if the error is squelched (not logged). 1036a3167c07Ssthen */ 1037a3167c07Ssthen int squelch_err_ssl_handshake(unsigned long err); 1038a3167c07Ssthen #endif 1039a3167c07Ssthen 1040933707f3Ssthen #endif /* NET_EVENT_H */ 1041