1b7579f77SDag-Erling Smørgrav /* 2b7579f77SDag-Erling Smørgrav * services/outside_network.h - listen to answers from the network 3b7579f77SDag-Erling Smørgrav * 4b7579f77SDag-Erling Smørgrav * Copyright (c) 2007, NLnet Labs. All rights reserved. 5b7579f77SDag-Erling Smørgrav * 6b7579f77SDag-Erling Smørgrav * This software is open source. 7b7579f77SDag-Erling Smørgrav * 8b7579f77SDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without 9b7579f77SDag-Erling Smørgrav * modification, are permitted provided that the following conditions 10b7579f77SDag-Erling Smørgrav * are met: 11b7579f77SDag-Erling Smørgrav * 12b7579f77SDag-Erling Smørgrav * Redistributions of source code must retain the above copyright notice, 13b7579f77SDag-Erling Smørgrav * this list of conditions and the following disclaimer. 14b7579f77SDag-Erling Smørgrav * 15b7579f77SDag-Erling Smørgrav * Redistributions in binary form must reproduce the above copyright notice, 16b7579f77SDag-Erling Smørgrav * this list of conditions and the following disclaimer in the documentation 17b7579f77SDag-Erling Smørgrav * and/or other materials provided with the distribution. 18b7579f77SDag-Erling Smørgrav * 19b7579f77SDag-Erling Smørgrav * Neither the name of the NLNET LABS nor the names of its contributors may 20b7579f77SDag-Erling Smørgrav * be used to endorse or promote products derived from this software without 21b7579f77SDag-Erling Smørgrav * specific prior written permission. 22b7579f77SDag-Erling Smørgrav * 23b7579f77SDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2417d15b25SDag-Erling Smørgrav * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2517d15b25SDag-Erling Smørgrav * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2617d15b25SDag-Erling Smørgrav * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2717d15b25SDag-Erling Smørgrav * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2817d15b25SDag-Erling Smørgrav * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 2917d15b25SDag-Erling Smørgrav * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 3017d15b25SDag-Erling Smørgrav * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 3117d15b25SDag-Erling Smørgrav * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 3217d15b25SDag-Erling Smørgrav * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 3317d15b25SDag-Erling Smørgrav * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34b7579f77SDag-Erling Smørgrav */ 35b7579f77SDag-Erling Smørgrav 36b7579f77SDag-Erling Smørgrav /** 37b7579f77SDag-Erling Smørgrav * \file 38b7579f77SDag-Erling Smørgrav * 39b7579f77SDag-Erling Smørgrav * This file has functions to send queries to authoritative servers, 40b7579f77SDag-Erling Smørgrav * and wait for the pending answer, with timeouts. 41b7579f77SDag-Erling Smørgrav */ 42b7579f77SDag-Erling Smørgrav 43b7579f77SDag-Erling Smørgrav #ifndef OUTSIDE_NETWORK_H 44b7579f77SDag-Erling Smørgrav #define OUTSIDE_NETWORK_H 45b7579f77SDag-Erling Smørgrav 469cf5bc93SCy Schubert #include "util/alloc.h" 47b7579f77SDag-Erling Smørgrav #include "util/rbtree.h" 489cf5bc93SCy Schubert #include "util/regional.h" 49b7579f77SDag-Erling Smørgrav #include "util/netevent.h" 50ff825849SDag-Erling Smørgrav #include "dnstap/dnstap_config.h" 51b7579f77SDag-Erling Smørgrav struct pending; 52b7579f77SDag-Erling Smørgrav struct pending_timeout; 53b7579f77SDag-Erling Smørgrav struct ub_randstate; 54b7579f77SDag-Erling Smørgrav struct pending_tcp; 55b7579f77SDag-Erling Smørgrav struct waiting_tcp; 56b7579f77SDag-Erling Smørgrav struct waiting_udp; 57369c6923SCy Schubert struct reuse_tcp; 58b7579f77SDag-Erling Smørgrav struct infra_cache; 59b7579f77SDag-Erling Smørgrav struct port_comm; 60b7579f77SDag-Erling Smørgrav struct port_if; 6117d15b25SDag-Erling Smørgrav struct sldns_buffer; 62ff825849SDag-Erling Smørgrav struct serviced_query; 63ff825849SDag-Erling Smørgrav struct dt_env; 64e2d15004SDag-Erling Smørgrav struct edns_option; 65bc892140SDag-Erling Smørgrav struct module_env; 66bc892140SDag-Erling Smørgrav struct module_qstate; 67bc892140SDag-Erling Smørgrav struct query_info; 685469a995SCy Schubert struct config_file; 69b7579f77SDag-Erling Smørgrav 70b7579f77SDag-Erling Smørgrav /** 71b7579f77SDag-Erling Smørgrav * Send queries to outside servers and wait for answers from servers. 72b7579f77SDag-Erling Smørgrav * Contains answer-listen sockets. 73b7579f77SDag-Erling Smørgrav */ 74b7579f77SDag-Erling Smørgrav struct outside_network { 75b7579f77SDag-Erling Smørgrav /** Base for select calls */ 76b7579f77SDag-Erling Smørgrav struct comm_base* base; 77b7579f77SDag-Erling Smørgrav /** pointer to time in seconds */ 7817d15b25SDag-Erling Smørgrav time_t* now_secs; 79b7579f77SDag-Erling Smørgrav /** pointer to time in microseconds */ 80b7579f77SDag-Erling Smørgrav struct timeval* now_tv; 81b7579f77SDag-Erling Smørgrav 82b7579f77SDag-Erling Smørgrav /** buffer shared by UDP connections, since there is only one 83b7579f77SDag-Erling Smørgrav datagram at any time. */ 8417d15b25SDag-Erling Smørgrav struct sldns_buffer* udp_buff; 85b7579f77SDag-Erling Smørgrav /** serviced_callbacks malloc overhead when processing multiple 86b7579f77SDag-Erling Smørgrav * identical serviced queries to the same server. */ 87b7579f77SDag-Erling Smørgrav size_t svcd_overhead; 88b7579f77SDag-Erling Smørgrav /** use x20 bits to encode additional ID random bits */ 89b7579f77SDag-Erling Smørgrav int use_caps_for_id; 90b7579f77SDag-Erling Smørgrav /** outside network wants to quit. Stop queued msgs from sent. */ 91b7579f77SDag-Erling Smørgrav int want_to_quit; 92b7579f77SDag-Erling Smørgrav 93b7579f77SDag-Erling Smørgrav /** number of unwanted replies received (for statistics) */ 94b7579f77SDag-Erling Smørgrav size_t unwanted_replies; 95b7579f77SDag-Erling Smørgrav /** cumulative total of unwanted replies (for defense) */ 96b7579f77SDag-Erling Smørgrav size_t unwanted_total; 97b7579f77SDag-Erling Smørgrav /** threshold when to take defensive action. If 0 then never. */ 98b7579f77SDag-Erling Smørgrav size_t unwanted_threshold; 99b7579f77SDag-Erling Smørgrav /** what action to take, called when defensive action is needed */ 100b7579f77SDag-Erling Smørgrav void (*unwanted_action)(void*); 101b7579f77SDag-Erling Smørgrav /** user param for action */ 102b7579f77SDag-Erling Smørgrav void* unwanted_param; 103b7579f77SDag-Erling Smørgrav 104b7579f77SDag-Erling Smørgrav /** linked list of available commpoints, unused file descriptors, 105b7579f77SDag-Erling Smørgrav * for use as outgoing UDP ports. cp.fd=-1 in them. */ 106b7579f77SDag-Erling Smørgrav struct port_comm* unused_fds; 107b7579f77SDag-Erling Smørgrav /** if udp is done */ 108b7579f77SDag-Erling Smørgrav int do_udp; 10917d15b25SDag-Erling Smørgrav /** if udp is delay-closed (delayed answers do not meet closed port)*/ 11017d15b25SDag-Erling Smørgrav int delayclose; 11117d15b25SDag-Erling Smørgrav /** timeout for delayclose */ 11217d15b25SDag-Erling Smørgrav struct timeval delay_tv; 113369c6923SCy Schubert /** if we perform udp-connect, connect() for UDP socket to mitigate 114369c6923SCy Schubert * ICMP side channel leakage */ 115369c6923SCy Schubert int udp_connect; 1160a92a9fcSCy Schubert /** number of udp packets sent. */ 1170a92a9fcSCy Schubert size_t num_udp_outgoing; 118b7579f77SDag-Erling Smørgrav 119b7579f77SDag-Erling Smørgrav /** array of outgoing IP4 interfaces */ 120b7579f77SDag-Erling Smørgrav struct port_if* ip4_ifs; 121b7579f77SDag-Erling Smørgrav /** number of outgoing IP4 interfaces */ 122b7579f77SDag-Erling Smørgrav int num_ip4; 123b7579f77SDag-Erling Smørgrav 124b7579f77SDag-Erling Smørgrav /** array of outgoing IP6 interfaces */ 125b7579f77SDag-Erling Smørgrav struct port_if* ip6_ifs; 126b7579f77SDag-Erling Smørgrav /** number of outgoing IP6 interfaces */ 127b7579f77SDag-Erling Smørgrav int num_ip6; 128b7579f77SDag-Erling Smørgrav 129b7579f77SDag-Erling Smørgrav /** pending udp queries waiting to be sent out, waiting for fd */ 130b7579f77SDag-Erling Smørgrav struct pending* udp_wait_first; 131b7579f77SDag-Erling Smørgrav /** last pending udp query in list */ 132b7579f77SDag-Erling Smørgrav struct pending* udp_wait_last; 133b7579f77SDag-Erling Smørgrav 134b7579f77SDag-Erling Smørgrav /** pending udp answers. sorted by id, addr */ 1353005e0a3SDag-Erling Smørgrav rbtree_type* pending; 136b7579f77SDag-Erling Smørgrav /** serviced queries, sorted by qbuf, addr, dnssec */ 1373005e0a3SDag-Erling Smørgrav rbtree_type* serviced; 138b7579f77SDag-Erling Smørgrav /** host cache, pointer but not owned by outnet. */ 139b7579f77SDag-Erling Smørgrav struct infra_cache* infra; 140b7579f77SDag-Erling Smørgrav /** where to get random numbers */ 141b7579f77SDag-Erling Smørgrav struct ub_randstate* rnd; 142b7579f77SDag-Erling Smørgrav /** ssl context to create ssl wrapped TCP with DNS connections */ 143b7579f77SDag-Erling Smørgrav void* sslctx; 14425039b37SCy Schubert /** if SNI will be used for TLS connections */ 14525039b37SCy Schubert int tls_use_sni; 146ff825849SDag-Erling Smørgrav #ifdef USE_DNSTAP 147ff825849SDag-Erling Smørgrav /** dnstap environment */ 148ff825849SDag-Erling Smørgrav struct dt_env* dtenv; 149ff825849SDag-Erling Smørgrav #endif 150f61ef7f6SDag-Erling Smørgrav /** maximum segment size of tcp socket */ 151f61ef7f6SDag-Erling Smørgrav int tcp_mss; 15225039b37SCy Schubert /** IP_TOS socket option requested on the sockets */ 15325039b37SCy Schubert int ip_dscp; 154b7579f77SDag-Erling Smørgrav 155b7579f77SDag-Erling Smørgrav /** 156b7579f77SDag-Erling Smørgrav * Array of tcp pending used for outgoing TCP connections. 157b7579f77SDag-Erling Smørgrav * Each can be used to establish a TCP connection with a server. 158b7579f77SDag-Erling Smørgrav * The file descriptors are -1 if they are free, and need to be 159b7579f77SDag-Erling Smørgrav * opened for the tcp connection. Can be used for ip4 and ip6. 160b7579f77SDag-Erling Smørgrav */ 161b7579f77SDag-Erling Smørgrav struct pending_tcp **tcp_conns; 162b7579f77SDag-Erling Smørgrav /** number of tcp communication points. */ 163b7579f77SDag-Erling Smørgrav size_t num_tcp; 164ff825849SDag-Erling Smørgrav /** number of tcp communication points in use. */ 165ff825849SDag-Erling Smørgrav size_t num_tcp_outgoing; 1665469a995SCy Schubert /** max number of queries on a reuse connection */ 1675469a995SCy Schubert size_t max_reuse_tcp_queries; 1685469a995SCy Schubert /** timeout for REUSE entries in milliseconds. */ 1695469a995SCy Schubert int tcp_reuse_timeout; 1705469a995SCy Schubert /** timeout in milliseconds for TCP queries to auth servers. */ 1715469a995SCy Schubert int tcp_auth_query_timeout; 172369c6923SCy Schubert /** 173369c6923SCy Schubert * tree of still-open and waiting tcp connections for reuse. 174369c6923SCy Schubert * can be closed and reopened to get a new tcp connection. 175369c6923SCy Schubert * or reused to the same destination again. with timeout to close. 176369c6923SCy Schubert * Entries are of type struct reuse_tcp. 177369c6923SCy Schubert * The entries are both active and empty connections. 178369c6923SCy Schubert */ 179369c6923SCy Schubert rbtree_type tcp_reuse; 180369c6923SCy Schubert /** max number of tcp_reuse entries we want to keep open */ 181369c6923SCy Schubert size_t tcp_reuse_max; 182369c6923SCy Schubert /** first and last(oldest) in lru list of reuse connections. 183369c6923SCy Schubert * the oldest can be closed to get a new free pending_tcp if needed 184369c6923SCy Schubert * The list contains empty connections, that wait for timeout or 185369c6923SCy Schubert * a new query that can use the existing connection. */ 186369c6923SCy Schubert struct reuse_tcp* tcp_reuse_first, *tcp_reuse_last; 187b7579f77SDag-Erling Smørgrav /** list of tcp comm points that are free for use */ 188b7579f77SDag-Erling Smørgrav struct pending_tcp* tcp_free; 189b7579f77SDag-Erling Smørgrav /** list of tcp queries waiting for a buffer */ 190b7579f77SDag-Erling Smørgrav struct waiting_tcp* tcp_wait_first; 191b7579f77SDag-Erling Smørgrav /** last of waiting query list */ 192b7579f77SDag-Erling Smørgrav struct waiting_tcp* tcp_wait_last; 193b7579f77SDag-Erling Smørgrav }; 194b7579f77SDag-Erling Smørgrav 195b7579f77SDag-Erling Smørgrav /** 196b7579f77SDag-Erling Smørgrav * Outgoing interface. Ports available and currently used are tracked 197b7579f77SDag-Erling Smørgrav * per interface 198b7579f77SDag-Erling Smørgrav */ 199b7579f77SDag-Erling Smørgrav struct port_if { 200b7579f77SDag-Erling Smørgrav /** address ready to allocate new socket (except port no). */ 201b7579f77SDag-Erling Smørgrav struct sockaddr_storage addr; 202b7579f77SDag-Erling Smørgrav /** length of addr field */ 203b7579f77SDag-Erling Smørgrav socklen_t addrlen; 204b7579f77SDag-Erling Smørgrav 205b5663de9SDag-Erling Smørgrav /** prefix length of network address (in bits), for randomisation. 206b5663de9SDag-Erling Smørgrav * if 0, no randomisation. */ 207b5663de9SDag-Erling Smørgrav int pfxlen; 208b5663de9SDag-Erling Smørgrav 20925039b37SCy Schubert #ifndef DISABLE_EXPLICIT_PORT_RANDOMISATION 210b7579f77SDag-Erling Smørgrav /** the available ports array. These are unused. 211b7579f77SDag-Erling Smørgrav * Only the first total-inuse part is filled. */ 212b7579f77SDag-Erling Smørgrav int* avail_ports; 213b7579f77SDag-Erling Smørgrav /** the total number of available ports (size of the array) */ 214b7579f77SDag-Erling Smørgrav int avail_total; 21525039b37SCy Schubert #endif 216b7579f77SDag-Erling Smørgrav 217b7579f77SDag-Erling Smørgrav /** array of the commpoints currently in use. 218b7579f77SDag-Erling Smørgrav * allocated for max number of fds, first part in use. */ 219b7579f77SDag-Erling Smørgrav struct port_comm** out; 220b7579f77SDag-Erling Smørgrav /** max number of fds, size of out array */ 221b7579f77SDag-Erling Smørgrav int maxout; 222b7579f77SDag-Erling Smørgrav /** number of commpoints (and thus also ports) in use */ 223b7579f77SDag-Erling Smørgrav int inuse; 224b7579f77SDag-Erling Smørgrav }; 225b7579f77SDag-Erling Smørgrav 226b7579f77SDag-Erling Smørgrav /** 227b7579f77SDag-Erling Smørgrav * Outgoing commpoint for UDP port. 228b7579f77SDag-Erling Smørgrav */ 229b7579f77SDag-Erling Smørgrav struct port_comm { 230b7579f77SDag-Erling Smørgrav /** next in free list */ 231b7579f77SDag-Erling Smørgrav struct port_comm* next; 232b7579f77SDag-Erling Smørgrav /** which port number (when in use) */ 233b7579f77SDag-Erling Smørgrav int number; 234b7579f77SDag-Erling Smørgrav /** interface it is used in */ 235b7579f77SDag-Erling Smørgrav struct port_if* pif; 236b7579f77SDag-Erling Smørgrav /** index in the out array of the interface */ 237b7579f77SDag-Erling Smørgrav int index; 238b7579f77SDag-Erling Smørgrav /** number of outstanding queries on this port */ 239b7579f77SDag-Erling Smørgrav int num_outstanding; 240b7579f77SDag-Erling Smørgrav /** UDP commpoint, fd=-1 if not in use */ 241b7579f77SDag-Erling Smørgrav struct comm_point* cp; 242b7579f77SDag-Erling Smørgrav }; 243b7579f77SDag-Erling Smørgrav 244b7579f77SDag-Erling Smørgrav /** 245369c6923SCy Schubert * Reuse TCP connection, still open can be used again. 246369c6923SCy Schubert */ 247369c6923SCy Schubert struct reuse_tcp { 248369c6923SCy Schubert /** rbtree node with links in tcp_reuse tree. key is NULL when not 249369c6923SCy Schubert * in tree. Both active and empty connections are in the tree. 250369c6923SCy Schubert * key is a pointer to this structure, the members used to compare 251369c6923SCy Schubert * are the sockaddr and and then is-ssl bool, and then ptr value is 252369c6923SCy Schubert * used in case the same address exists several times in the tree 253369c6923SCy Schubert * when there are multiple connections to the same destination to 254369c6923SCy Schubert * make the rbtree items unique. */ 255369c6923SCy Schubert rbnode_type node; 256369c6923SCy Schubert /** the key for the tcp_reuse tree. address of peer, ip4 or ip6, 257369c6923SCy Schubert * and port number of peer */ 258369c6923SCy Schubert struct sockaddr_storage addr; 259369c6923SCy Schubert /** length of addr */ 260369c6923SCy Schubert socklen_t addrlen; 261369c6923SCy Schubert /** also key for tcp_reuse tree, if ssl is used */ 262369c6923SCy Schubert int is_ssl; 263369c6923SCy Schubert /** lru chain, so that the oldest can be removed to get a new 264369c6923SCy Schubert * connection when all are in (re)use. oldest is last in list. 265369c6923SCy Schubert * The lru only contains empty connections waiting for reuse, 266369c6923SCy Schubert * the ones with active queries are not on the list because they 267369c6923SCy Schubert * do not need to be closed to make space for others. They already 268369c6923SCy Schubert * service a query so the close for another query does not help 269369c6923SCy Schubert * service a larger number of queries. */ 270369c6923SCy Schubert struct reuse_tcp* lru_next, *lru_prev; 271369c6923SCy Schubert /** true if the reuse_tcp item is on the lru list with empty items */ 272369c6923SCy Schubert int item_on_lru_list; 273369c6923SCy Schubert /** the connection to reuse, the fd is non-1 and is open. 274369c6923SCy Schubert * the addr and port determine where the connection is going, 275369c6923SCy Schubert * and is key to the rbtree. The SSL ptr determines if it is 276369c6923SCy Schubert * a TLS connection or a plain TCP connection there. And TLS 277369c6923SCy Schubert * or not is also part of the key to the rbtree. 278369c6923SCy Schubert * There is a timeout and read event on the fd, to close it. */ 279369c6923SCy Schubert struct pending_tcp* pending; 280369c6923SCy Schubert /** 281369c6923SCy Schubert * The more read again value pointed to by the commpoint 282369c6923SCy Schubert * tcp_more_read_again pointer, so that it exists after commpoint 283369c6923SCy Schubert * delete 284369c6923SCy Schubert */ 285369c6923SCy Schubert int cp_more_read_again; 286369c6923SCy Schubert /** 287369c6923SCy Schubert * The more write again value pointed to by the commpoint 288369c6923SCy Schubert * tcp_more_write_again pointer, so that it exists after commpoint 289369c6923SCy Schubert * delete 290369c6923SCy Schubert */ 291369c6923SCy Schubert int cp_more_write_again; 292369c6923SCy Schubert /** rbtree with other queries waiting on the connection, by ID number, 293369c6923SCy Schubert * of type struct waiting_tcp. It is for looking up received 294369c6923SCy Schubert * answers to the structure for callback. And also to see if ID 295369c6923SCy Schubert * numbers are unused and can be used for a new query. 296369c6923SCy Schubert * The write_wait elements are also in the tree, so that ID numbers 297369c6923SCy Schubert * can be looked up also for them. They are bool write_wait_queued. */ 298369c6923SCy Schubert rbtree_type tree_by_id; 299369c6923SCy Schubert /** list of queries waiting to be written on the channel, 300369c6923SCy Schubert * if NULL no queries are waiting to be written and the pending->query 301369c6923SCy Schubert * is the query currently serviced. The first is the next in line. 302369c6923SCy Schubert * They are also in the tree_by_id. Once written, the are removed 303369c6923SCy Schubert * from this list, but stay in the tree. */ 304369c6923SCy Schubert struct waiting_tcp* write_wait_first, *write_wait_last; 305369c6923SCy Schubert /** the outside network it is part of */ 306369c6923SCy Schubert struct outside_network* outnet; 307369c6923SCy Schubert }; 308369c6923SCy Schubert 309369c6923SCy Schubert /** 310b7579f77SDag-Erling Smørgrav * A query that has an answer pending for it. 311b7579f77SDag-Erling Smørgrav */ 312b7579f77SDag-Erling Smørgrav struct pending { 313b7579f77SDag-Erling Smørgrav /** redblacktree entry, key is the pending struct(id, addr). */ 3143005e0a3SDag-Erling Smørgrav rbnode_type node; 315b7579f77SDag-Erling Smørgrav /** the ID for the query. int so that a value out of range can 316b7579f77SDag-Erling Smørgrav * be used to signify a pending that is for certain not present in 317b7579f77SDag-Erling Smørgrav * the rbtree. (and for which deletion is safe). */ 318b7579f77SDag-Erling Smørgrav unsigned int id; 319b7579f77SDag-Erling Smørgrav /** remote address. */ 320b7579f77SDag-Erling Smørgrav struct sockaddr_storage addr; 321b7579f77SDag-Erling Smørgrav /** length of addr field in use. */ 322b7579f77SDag-Erling Smørgrav socklen_t addrlen; 323b7579f77SDag-Erling Smørgrav /** comm point it was sent on (and reply must come back on). */ 324b7579f77SDag-Erling Smørgrav struct port_comm* pc; 325b7579f77SDag-Erling Smørgrav /** timeout event */ 326b7579f77SDag-Erling Smørgrav struct comm_timer* timer; 327b7579f77SDag-Erling Smørgrav /** callback for the timeout, error or reply to the message */ 3283005e0a3SDag-Erling Smørgrav comm_point_callback_type* cb; 329b7579f77SDag-Erling Smørgrav /** callback user argument */ 330b7579f77SDag-Erling Smørgrav void* cb_arg; 331b7579f77SDag-Erling Smørgrav /** the outside network it is part of */ 332b7579f77SDag-Erling Smørgrav struct outside_network* outnet; 333ff825849SDag-Erling Smørgrav /** the corresponding serviced_query */ 334ff825849SDag-Erling Smørgrav struct serviced_query* sq; 335b7579f77SDag-Erling Smørgrav 336b7579f77SDag-Erling Smørgrav /*---- filled if udp pending is waiting -----*/ 337b7579f77SDag-Erling Smørgrav /** next in waiting list. */ 338b7579f77SDag-Erling Smørgrav struct pending* next_waiting; 339b7579f77SDag-Erling Smørgrav /** timeout in msec */ 340b7579f77SDag-Erling Smørgrav int timeout; 341b7579f77SDag-Erling Smørgrav /** The query itself, the query packet to send. */ 342b7579f77SDag-Erling Smørgrav uint8_t* pkt; 343b7579f77SDag-Erling Smørgrav /** length of query packet. */ 344b7579f77SDag-Erling Smørgrav size_t pkt_len; 345b7579f77SDag-Erling Smørgrav }; 346b7579f77SDag-Erling Smørgrav 347b7579f77SDag-Erling Smørgrav /** 348b7579f77SDag-Erling Smørgrav * Pending TCP query to server. 349b7579f77SDag-Erling Smørgrav */ 350b7579f77SDag-Erling Smørgrav struct pending_tcp { 351b7579f77SDag-Erling Smørgrav /** next in list of free tcp comm points, or NULL. */ 352b7579f77SDag-Erling Smørgrav struct pending_tcp* next_free; 3535469a995SCy Schubert /** port for of the outgoing interface that is used */ 3545469a995SCy Schubert struct port_if* pi; 355b7579f77SDag-Erling Smørgrav /** tcp comm point it was sent on (and reply must come back on). */ 356b7579f77SDag-Erling Smørgrav struct comm_point* c; 357b7579f77SDag-Erling Smørgrav /** the query being serviced, NULL if the pending_tcp is unused. */ 358b7579f77SDag-Erling Smørgrav struct waiting_tcp* query; 359369c6923SCy Schubert /** the pre-allocated reuse tcp structure. if ->pending is nonNULL 360369c6923SCy Schubert * it is in use and the connection is waiting for reuse. 361369c6923SCy Schubert * It is here for memory pre-allocation, and used to make this 362369c6923SCy Schubert * pending_tcp wait for reuse. */ 363369c6923SCy Schubert struct reuse_tcp reuse; 364b7579f77SDag-Erling Smørgrav }; 365b7579f77SDag-Erling Smørgrav 366b7579f77SDag-Erling Smørgrav /** 367b7579f77SDag-Erling Smørgrav * Query waiting for TCP buffer. 368b7579f77SDag-Erling Smørgrav */ 369b7579f77SDag-Erling Smørgrav struct waiting_tcp { 370b7579f77SDag-Erling Smørgrav /** 371b7579f77SDag-Erling Smørgrav * next in waiting list. 372369c6923SCy Schubert * if on_tcp_waiting_list==0, this points to the pending_tcp structure. 373b7579f77SDag-Erling Smørgrav */ 374b7579f77SDag-Erling Smørgrav struct waiting_tcp* next_waiting; 375369c6923SCy Schubert /** if true the item is on the tcp waiting list and next_waiting 376369c6923SCy Schubert * is used for that. If false, the next_waiting points to the 377369c6923SCy Schubert * pending_tcp */ 378369c6923SCy Schubert int on_tcp_waiting_list; 379369c6923SCy Schubert /** next and prev in query waiting list for stream connection */ 380369c6923SCy Schubert struct waiting_tcp* write_wait_prev, *write_wait_next; 381369c6923SCy Schubert /** true if the waiting_tcp structure is on the write_wait queue */ 382369c6923SCy Schubert int write_wait_queued; 383369c6923SCy Schubert /** entry in reuse.tree_by_id, if key is NULL, not in tree, otherwise, 384369c6923SCy Schubert * this struct is key and sorted by ID (from waiting_tcp.id). */ 385369c6923SCy Schubert rbnode_type id_node; 386369c6923SCy Schubert /** the ID for the query; checked in reply */ 387369c6923SCy Schubert uint16_t id; 388b7579f77SDag-Erling Smørgrav /** timeout event; timer keeps running whether the query is 389b7579f77SDag-Erling Smørgrav * waiting for a buffer or the tcp reply is pending */ 390b7579f77SDag-Erling Smørgrav struct comm_timer* timer; 391369c6923SCy Schubert /** timeout in msec */ 392369c6923SCy Schubert int timeout; 393b7579f77SDag-Erling Smørgrav /** the outside network it is part of */ 394b7579f77SDag-Erling Smørgrav struct outside_network* outnet; 395b7579f77SDag-Erling Smørgrav /** remote address. */ 396b7579f77SDag-Erling Smørgrav struct sockaddr_storage addr; 397b7579f77SDag-Erling Smørgrav /** length of addr field in use. */ 398b7579f77SDag-Erling Smørgrav socklen_t addrlen; 399b7579f77SDag-Erling Smørgrav /** 400b7579f77SDag-Erling Smørgrav * The query itself, the query packet to send. 401b7579f77SDag-Erling Smørgrav * allocated after the waiting_tcp structure. 402b7579f77SDag-Erling Smørgrav */ 403b7579f77SDag-Erling Smørgrav uint8_t* pkt; 404b7579f77SDag-Erling Smørgrav /** length of query packet. */ 405b7579f77SDag-Erling Smørgrav size_t pkt_len; 406369c6923SCy Schubert /** callback for the timeout, error or reply to the message, 407369c6923SCy Schubert * or NULL if no user is waiting. the entry uses an ID number. 408369c6923SCy Schubert * a query that was written is no longer needed, but the ID number 409369c6923SCy Schubert * and a reply will come back and can be ignored if NULL */ 4103005e0a3SDag-Erling Smørgrav comm_point_callback_type* cb; 411b7579f77SDag-Erling Smørgrav /** callback user argument */ 412b7579f77SDag-Erling Smørgrav void* cb_arg; 413b7579f77SDag-Erling Smørgrav /** if it uses ssl upstream */ 414b7579f77SDag-Erling Smørgrav int ssl_upstream; 4150fb34990SDag-Erling Smørgrav /** ref to the tls_auth_name from the serviced_query */ 4160fb34990SDag-Erling Smørgrav char* tls_auth_name; 417369c6923SCy Schubert /** the packet was involved in an error, to stop looping errors */ 418369c6923SCy Schubert int error_count; 4199cf5bc93SCy Schubert /** if true, the item is at the cb_and_decommission stage */ 4209cf5bc93SCy Schubert int in_cb_and_decommission; 4215469a995SCy Schubert #ifdef USE_DNSTAP 4225469a995SCy Schubert /** serviced query pointer for dnstap to get logging info, if nonNULL*/ 4235469a995SCy Schubert struct serviced_query* sq; 4245469a995SCy Schubert #endif 425b7579f77SDag-Erling Smørgrav }; 426b7579f77SDag-Erling Smørgrav 427b7579f77SDag-Erling Smørgrav /** 428b7579f77SDag-Erling Smørgrav * Callback to party interested in serviced query results. 429b7579f77SDag-Erling Smørgrav */ 430b7579f77SDag-Erling Smørgrav struct service_callback { 431b7579f77SDag-Erling Smørgrav /** next in callback list */ 432b7579f77SDag-Erling Smørgrav struct service_callback* next; 433b7579f77SDag-Erling Smørgrav /** callback function */ 4343005e0a3SDag-Erling Smørgrav comm_point_callback_type* cb; 435b7579f77SDag-Erling Smørgrav /** user argument for callback function */ 436b7579f77SDag-Erling Smørgrav void* cb_arg; 437b7579f77SDag-Erling Smørgrav }; 438b7579f77SDag-Erling Smørgrav 439b7579f77SDag-Erling Smørgrav /** fallback size for fragmentation for EDNS in IPv4 */ 4408ed2b524SDag-Erling Smørgrav #define EDNS_FRAG_SIZE_IP4 1472 441b7579f77SDag-Erling Smørgrav /** fallback size for EDNS in IPv6, fits one fragment with ip6-tunnel-ids */ 4428ed2b524SDag-Erling Smørgrav #define EDNS_FRAG_SIZE_IP6 1232 443b7579f77SDag-Erling Smørgrav 444b7579f77SDag-Erling Smørgrav /** 445b7579f77SDag-Erling Smørgrav * Query service record. 446b7579f77SDag-Erling Smørgrav * Contains query and destination. UDP, TCP, EDNS are all tried. 447b7579f77SDag-Erling Smørgrav * complete with retries and timeouts. A number of interested parties can 448b7579f77SDag-Erling Smørgrav * receive a callback. 449b7579f77SDag-Erling Smørgrav */ 450b7579f77SDag-Erling Smørgrav struct serviced_query { 451b7579f77SDag-Erling Smørgrav /** The rbtree node, key is this record */ 4523005e0a3SDag-Erling Smørgrav rbnode_type node; 453b7579f77SDag-Erling Smørgrav /** The query that needs to be answered. Starts with flags u16, 454b7579f77SDag-Erling Smørgrav * then qdcount, ..., including qname, qtype, qclass. Does not include 455b7579f77SDag-Erling Smørgrav * EDNS record. */ 456b7579f77SDag-Erling Smørgrav uint8_t* qbuf; 457b7579f77SDag-Erling Smørgrav /** length of qbuf. */ 458b7579f77SDag-Erling Smørgrav size_t qbuflen; 459b7579f77SDag-Erling Smørgrav /** If an EDNS section is included, the DO/CD bit will be turned on. */ 460b7579f77SDag-Erling Smørgrav int dnssec; 461b7579f77SDag-Erling Smørgrav /** We want signatures, or else the answer is likely useless */ 462b7579f77SDag-Erling Smørgrav int want_dnssec; 463ff825849SDag-Erling Smørgrav /** ignore capsforid */ 464ff825849SDag-Erling Smørgrav int nocaps; 465b7579f77SDag-Erling Smørgrav /** tcp upstream used, use tcp, or ssl_upstream for SSL */ 466b7579f77SDag-Erling Smørgrav int tcp_upstream, ssl_upstream; 4670fb34990SDag-Erling Smørgrav /** the name of the tls authentication name, eg. 'ns.example.com' 4680fb34990SDag-Erling Smørgrav * or NULL */ 4690fb34990SDag-Erling Smørgrav char* tls_auth_name; 470b7579f77SDag-Erling Smørgrav /** where to send it */ 471b7579f77SDag-Erling Smørgrav struct sockaddr_storage addr; 472b7579f77SDag-Erling Smørgrav /** length of addr field in use. */ 473b7579f77SDag-Erling Smørgrav socklen_t addrlen; 474b7579f77SDag-Erling Smørgrav /** zone name, uncompressed domain name in wireformat */ 475b7579f77SDag-Erling Smørgrav uint8_t* zone; 476b7579f77SDag-Erling Smørgrav /** length of zone name */ 477b7579f77SDag-Erling Smørgrav size_t zonelen; 478b7579f77SDag-Erling Smørgrav /** qtype */ 479b7579f77SDag-Erling Smørgrav int qtype; 480b7579f77SDag-Erling Smørgrav /** current status */ 481b7579f77SDag-Erling Smørgrav enum serviced_query_status { 482b7579f77SDag-Erling Smørgrav /** initial status */ 483b7579f77SDag-Erling Smørgrav serviced_initial, 484b7579f77SDag-Erling Smørgrav /** UDP with EDNS sent */ 485b7579f77SDag-Erling Smørgrav serviced_query_UDP_EDNS, 486b7579f77SDag-Erling Smørgrav /** UDP without EDNS sent */ 487b7579f77SDag-Erling Smørgrav serviced_query_UDP, 488b7579f77SDag-Erling Smørgrav /** TCP with EDNS sent */ 489b7579f77SDag-Erling Smørgrav serviced_query_TCP_EDNS, 490b7579f77SDag-Erling Smørgrav /** TCP without EDNS sent */ 491b7579f77SDag-Erling Smørgrav serviced_query_TCP, 492b7579f77SDag-Erling Smørgrav /** probe to test noEDNS0 (EDNS gives FORMERRorNOTIMP) */ 493b7579f77SDag-Erling Smørgrav serviced_query_UDP_EDNS_fallback, 494b7579f77SDag-Erling Smørgrav /** probe to test TCP noEDNS0 (EDNS gives FORMERRorNOTIMP) */ 495b7579f77SDag-Erling Smørgrav serviced_query_TCP_EDNS_fallback, 496b7579f77SDag-Erling Smørgrav /** send UDP query with EDNS1480 (or 1280) */ 497b7579f77SDag-Erling Smørgrav serviced_query_UDP_EDNS_FRAG 498b7579f77SDag-Erling Smørgrav } 499b7579f77SDag-Erling Smørgrav /** variable with current status */ 500b7579f77SDag-Erling Smørgrav status; 501b7579f77SDag-Erling Smørgrav /** true if serviced_query is scheduled for deletion already */ 502b7579f77SDag-Erling Smørgrav int to_be_deleted; 503b7579f77SDag-Erling Smørgrav /** number of UDP retries */ 504b7579f77SDag-Erling Smørgrav int retry; 505b7579f77SDag-Erling Smørgrav /** time last UDP was sent */ 506b7579f77SDag-Erling Smørgrav struct timeval last_sent_time; 5073bd4df0aSDag-Erling Smørgrav /** rtt of last message */ 508b7579f77SDag-Erling Smørgrav int last_rtt; 509b7579f77SDag-Erling Smørgrav /** do we know edns probe status already, for UDP_EDNS queries */ 510b7579f77SDag-Erling Smørgrav int edns_lame_known; 511e2d15004SDag-Erling Smørgrav /** edns options to use for sending upstream packet */ 512e2d15004SDag-Erling Smørgrav struct edns_option* opt_list; 513b7579f77SDag-Erling Smørgrav /** outside network this is part of */ 514b7579f77SDag-Erling Smørgrav struct outside_network* outnet; 515b7579f77SDag-Erling Smørgrav /** list of interested parties that need callback on results. */ 516b7579f77SDag-Erling Smørgrav struct service_callback* cblist; 517b7579f77SDag-Erling Smørgrav /** the UDP or TCP query that is pending, see status which */ 518b7579f77SDag-Erling Smørgrav void* pending; 519f44e67d1SCy Schubert /** block size with which to pad encrypted queries (default: 128) */ 520f44e67d1SCy Schubert size_t padding_block_size; 5219cf5bc93SCy Schubert /** region for this serviced query. Will be cleared when this 5229cf5bc93SCy Schubert * serviced_query will be deleted */ 5239cf5bc93SCy Schubert struct regional* region; 5249cf5bc93SCy Schubert /** allocation service for the region */ 5259cf5bc93SCy Schubert struct alloc_cache* alloc; 5269cf5bc93SCy Schubert /** flash timer to start the net I/O as a separate event */ 5279cf5bc93SCy Schubert struct comm_timer* timer; 5289cf5bc93SCy Schubert /** true if serviced_query is currently doing net I/O and may block */ 5299cf5bc93SCy Schubert int busy; 530b7579f77SDag-Erling Smørgrav }; 531b7579f77SDag-Erling Smørgrav 532b7579f77SDag-Erling Smørgrav /** 533b7579f77SDag-Erling Smørgrav * Create outside_network structure with N udp ports. 534b7579f77SDag-Erling Smørgrav * @param base: the communication base to use for event handling. 535b7579f77SDag-Erling Smørgrav * @param bufsize: size for network buffers. 536b7579f77SDag-Erling Smørgrav * @param num_ports: number of udp ports to open per interface. 537b7579f77SDag-Erling Smørgrav * @param ifs: interface names (or NULL for default interface). 538b7579f77SDag-Erling Smørgrav * These interfaces must be able to access all authoritative servers. 539b7579f77SDag-Erling Smørgrav * @param num_ifs: number of names in array ifs. 540b7579f77SDag-Erling Smørgrav * @param do_ip4: service IP4. 541b7579f77SDag-Erling Smørgrav * @param do_ip6: service IP6. 542b7579f77SDag-Erling Smørgrav * @param num_tcp: number of outgoing tcp buffers to preallocate. 54325039b37SCy Schubert * @param dscp: DSCP to use. 544b7579f77SDag-Erling Smørgrav * @param infra: pointer to infra cached used for serviced queries. 545b7579f77SDag-Erling Smørgrav * @param rnd: stored to create random numbers for serviced queries. 546b7579f77SDag-Erling Smørgrav * @param use_caps_for_id: enable to use 0x20 bits to encode id randomness. 547b7579f77SDag-Erling Smørgrav * @param availports: array of available ports. 548b7579f77SDag-Erling Smørgrav * @param numavailports: number of available ports in array. 549b7579f77SDag-Erling Smørgrav * @param unwanted_threshold: when to take defensive action. 550b7579f77SDag-Erling Smørgrav * @param unwanted_action: the action to take. 551b7579f77SDag-Erling Smørgrav * @param unwanted_param: user parameter to action. 552f61ef7f6SDag-Erling Smørgrav * @param tcp_mss: maximum segment size of tcp socket. 553b7579f77SDag-Erling Smørgrav * @param do_udp: if udp is done. 554b7579f77SDag-Erling Smørgrav * @param sslctx: context to create outgoing connections with (if enabled). 55517d15b25SDag-Erling Smørgrav * @param delayclose: if not 0, udp sockets are delayed before timeout closure. 55617d15b25SDag-Erling Smørgrav * msec to wait on timeouted udp sockets. 55725039b37SCy Schubert * @param tls_use_sni: if SNI is used for TLS connections. 558ff825849SDag-Erling Smørgrav * @param dtenv: environment to send dnstap events with (if enabled). 559369c6923SCy Schubert * @param udp_connect: if the udp_connect option is enabled. 5605469a995SCy Schubert * @param max_reuse_tcp_queries: max number of queries on a reuse connection. 5615469a995SCy Schubert * @param tcp_reuse_timeout: timeout for REUSE entries in milliseconds. 5625469a995SCy Schubert * @param tcp_auth_query_timeout: timeout in milliseconds for TCP queries to auth servers. 563b7579f77SDag-Erling Smørgrav * @return: the new structure (with no pending answers) or NULL on error. 564b7579f77SDag-Erling Smørgrav */ 565b7579f77SDag-Erling Smørgrav struct outside_network* outside_network_create(struct comm_base* base, 566b7579f77SDag-Erling Smørgrav size_t bufsize, size_t num_ports, char** ifs, int num_ifs, 56725039b37SCy Schubert int do_ip4, int do_ip6, size_t num_tcp, int dscp, struct infra_cache* infra, 568b7579f77SDag-Erling Smørgrav struct ub_randstate* rnd, int use_caps_for_id, int* availports, 569f61ef7f6SDag-Erling Smørgrav int numavailports, size_t unwanted_threshold, int tcp_mss, 570b7579f77SDag-Erling Smørgrav void (*unwanted_action)(void*), void* unwanted_param, int do_udp, 571369c6923SCy Schubert void* sslctx, int delayclose, int tls_use_sni, struct dt_env *dtenv, 5725469a995SCy Schubert int udp_connect, int max_reuse_tcp_queries, int tcp_reuse_timeout, 5735469a995SCy Schubert int tcp_auth_query_timeout); 574b7579f77SDag-Erling Smørgrav 575b7579f77SDag-Erling Smørgrav /** 576b7579f77SDag-Erling Smørgrav * Delete outside_network structure. 577b7579f77SDag-Erling Smørgrav * @param outnet: object to delete. 578b7579f77SDag-Erling Smørgrav */ 579b7579f77SDag-Erling Smørgrav void outside_network_delete(struct outside_network* outnet); 580b7579f77SDag-Erling Smørgrav 581b7579f77SDag-Erling Smørgrav /** 582b7579f77SDag-Erling Smørgrav * Prepare for quit. Sends no more queries, even if queued up. 583b7579f77SDag-Erling Smørgrav * @param outnet: object to prepare for removal 584b7579f77SDag-Erling Smørgrav */ 585b7579f77SDag-Erling Smørgrav void outside_network_quit_prepare(struct outside_network* outnet); 586b7579f77SDag-Erling Smørgrav 587b7579f77SDag-Erling Smørgrav /** 588b7579f77SDag-Erling Smørgrav * Send UDP query, create pending answer. 589b7579f77SDag-Erling Smørgrav * Changes the ID for the query to be random and unique for that destination. 590ff825849SDag-Erling Smørgrav * @param sq: serviced query. 591b7579f77SDag-Erling Smørgrav * @param packet: wireformat query to send to destination. 592b7579f77SDag-Erling Smørgrav * @param timeout: in milliseconds from now. 593b7579f77SDag-Erling Smørgrav * @param callback: function to call on error, timeout or reply. 594b7579f77SDag-Erling Smørgrav * @param callback_arg: user argument for callback function. 595b7579f77SDag-Erling Smørgrav * @return: NULL on error for malloc or socket. Else the pending query object. 596b7579f77SDag-Erling Smørgrav */ 597ff825849SDag-Erling Smørgrav struct pending* pending_udp_query(struct serviced_query* sq, 5983005e0a3SDag-Erling Smørgrav struct sldns_buffer* packet, int timeout, comm_point_callback_type* callback, 599b7579f77SDag-Erling Smørgrav void* callback_arg); 600b7579f77SDag-Erling Smørgrav 601b7579f77SDag-Erling Smørgrav /** 602b7579f77SDag-Erling Smørgrav * Send TCP query. May wait for TCP buffer. Selects ID to be random, and 603b7579f77SDag-Erling Smørgrav * checks id. 604ff825849SDag-Erling Smørgrav * @param sq: serviced query. 605b7579f77SDag-Erling Smørgrav * @param packet: wireformat query to send to destination. copied from. 6063bd4df0aSDag-Erling Smørgrav * @param timeout: in milliseconds from now. 607b7579f77SDag-Erling Smørgrav * Timer starts running now. Timer may expire if all buffers are used, 608b7579f77SDag-Erling Smørgrav * without any query been sent to the server yet. 609b7579f77SDag-Erling Smørgrav * @param callback: function to call on error, timeout or reply. 610b7579f77SDag-Erling Smørgrav * @param callback_arg: user argument for callback function. 611b7579f77SDag-Erling Smørgrav * @return: false on error for malloc or socket. Else the pending TCP object. 612b7579f77SDag-Erling Smørgrav */ 613ff825849SDag-Erling Smørgrav struct waiting_tcp* pending_tcp_query(struct serviced_query* sq, 6143005e0a3SDag-Erling Smørgrav struct sldns_buffer* packet, int timeout, comm_point_callback_type* callback, 615ff825849SDag-Erling Smørgrav void* callback_arg); 616b7579f77SDag-Erling Smørgrav 617b7579f77SDag-Erling Smørgrav /** 618b7579f77SDag-Erling Smørgrav * Delete pending answer. 619b7579f77SDag-Erling Smørgrav * @param outnet: outside network the pending query is part of. 620b7579f77SDag-Erling Smørgrav * Internal feature: if outnet is NULL, p is not unlinked from rbtree. 621b7579f77SDag-Erling Smørgrav * @param p: deleted 622b7579f77SDag-Erling Smørgrav */ 623b7579f77SDag-Erling Smørgrav void pending_delete(struct outside_network* outnet, struct pending* p); 624b7579f77SDag-Erling Smørgrav 625b7579f77SDag-Erling Smørgrav /** 626b7579f77SDag-Erling Smørgrav * Perform a serviced query to the authoritative servers. 627b7579f77SDag-Erling Smørgrav * Duplicate efforts are detected, and EDNS, TCP and UDP retry is performed. 628b7579f77SDag-Erling Smørgrav * @param outnet: outside network, with rbtree of serviced queries. 629bc892140SDag-Erling Smørgrav * @param qinfo: query info. 630b7579f77SDag-Erling Smørgrav * @param flags: flags u16 (host format), includes opcode, CD bit. 631b7579f77SDag-Erling Smørgrav * @param dnssec: if set, DO bit is set in EDNS queries. 632b7579f77SDag-Erling Smørgrav * If the value includes BIT_CD, CD bit is set when in EDNS queries. 633b7579f77SDag-Erling Smørgrav * If the value includes BIT_DO, DO bit is set when in EDNS queries. 634b7579f77SDag-Erling Smørgrav * @param want_dnssec: signatures are needed, without EDNS the answer is 635b7579f77SDag-Erling Smørgrav * likely to be useless. 636ff825849SDag-Erling Smørgrav * @param nocaps: ignore use_caps_for_id and use unperturbed qname. 6379cf5bc93SCy Schubert * @param check_ratelimit: if set, will check ratelimit before sending out. 638b7579f77SDag-Erling Smørgrav * @param tcp_upstream: use TCP for upstream queries. 639b7579f77SDag-Erling Smørgrav * @param ssl_upstream: use SSL for upstream queries. 6400fb34990SDag-Erling Smørgrav * @param tls_auth_name: when ssl_upstream is true, use this name to check 6410fb34990SDag-Erling Smørgrav * the server's peer certificate. 642b7579f77SDag-Erling Smørgrav * @param addr: to which server to send the query. 643b7579f77SDag-Erling Smørgrav * @param addrlen: length of addr. 644b7579f77SDag-Erling Smørgrav * @param zone: name of the zone of the delegation point. wireformat dname. 645b7579f77SDag-Erling Smørgrav This is the delegation point name for which the server is deemed 646b7579f77SDag-Erling Smørgrav authoritative. 647b7579f77SDag-Erling Smørgrav * @param zonelen: length of zone. 648bc892140SDag-Erling Smørgrav * @param qstate: module qstate. Mainly for inspecting the available 649bc892140SDag-Erling Smørgrav * edns_opts_lists. 650bc892140SDag-Erling Smørgrav * @param callback: callback function. 651bc892140SDag-Erling Smørgrav * @param callback_arg: user argument to callback function. 652b7579f77SDag-Erling Smørgrav * @param buff: scratch buffer to create query contents in. Empty on exit. 653bc892140SDag-Erling Smørgrav * @param env: the module environment. 6549cf5bc93SCy Schubert * @param was_ratelimited: it will signal back if the query failed to pass the 6559cf5bc93SCy Schubert * ratelimit check. 656b7579f77SDag-Erling Smørgrav * @return 0 on error, or pointer to serviced query that is used to answer 657b7579f77SDag-Erling Smørgrav * this serviced query may be shared with other callbacks as well. 658b7579f77SDag-Erling Smørgrav */ 659b7579f77SDag-Erling Smørgrav struct serviced_query* outnet_serviced_query(struct outside_network* outnet, 660bc892140SDag-Erling Smørgrav struct query_info* qinfo, uint16_t flags, int dnssec, int want_dnssec, 6619cf5bc93SCy Schubert int nocaps, int check_ratelimit, int tcp_upstream, int ssl_upstream, 6629cf5bc93SCy Schubert char* tls_auth_name, struct sockaddr_storage* addr, socklen_t addrlen, 6639cf5bc93SCy Schubert uint8_t* zone, size_t zonelen, struct module_qstate* qstate, 6643005e0a3SDag-Erling Smørgrav comm_point_callback_type* callback, void* callback_arg, 6659cf5bc93SCy Schubert struct sldns_buffer* buff, struct module_env* env, int* was_ratelimited); 666b7579f77SDag-Erling Smørgrav 667b7579f77SDag-Erling Smørgrav /** 668b7579f77SDag-Erling Smørgrav * Remove service query callback. 669b7579f77SDag-Erling Smørgrav * If that leads to zero callbacks, the query is completely cancelled. 670b7579f77SDag-Erling Smørgrav * @param sq: serviced query to adjust. 671b7579f77SDag-Erling Smørgrav * @param cb_arg: callback argument of callback that needs removal. 672b7579f77SDag-Erling Smørgrav * same as the callback_arg to outnet_serviced_query(). 673b7579f77SDag-Erling Smørgrav */ 674b7579f77SDag-Erling Smørgrav void outnet_serviced_query_stop(struct serviced_query* sq, void* cb_arg); 675b7579f77SDag-Erling Smørgrav 676b7579f77SDag-Erling Smørgrav /** 677b7579f77SDag-Erling Smørgrav * Get memory size in use by outside network. 678b7579f77SDag-Erling Smørgrav * Counts buffers and outstanding query (serviced queries) malloced data. 679b7579f77SDag-Erling Smørgrav * @param outnet: outside network structure. 680b7579f77SDag-Erling Smørgrav * @return size in bytes. 681b7579f77SDag-Erling Smørgrav */ 682b7579f77SDag-Erling Smørgrav size_t outnet_get_mem(struct outside_network* outnet); 683b7579f77SDag-Erling Smørgrav 684b7579f77SDag-Erling Smørgrav /** 685b7579f77SDag-Erling Smørgrav * Get memory size in use by serviced query while it is servicing callbacks. 686b7579f77SDag-Erling Smørgrav * This takes into account the pre-deleted status of it; it will be deleted 687b7579f77SDag-Erling Smørgrav * when the callbacks are done. 688b7579f77SDag-Erling Smørgrav * @param sq: serviced query. 689b7579f77SDag-Erling Smørgrav * @return size in bytes. 690b7579f77SDag-Erling Smørgrav */ 691b7579f77SDag-Erling Smørgrav size_t serviced_get_mem(struct serviced_query* sq); 692b7579f77SDag-Erling Smørgrav 693369c6923SCy Schubert /** Pick random ID value for a tcp stream, avoids existing IDs. */ 694369c6923SCy Schubert uint16_t reuse_tcp_select_id(struct reuse_tcp* reuse, 695369c6923SCy Schubert struct outside_network* outnet); 696369c6923SCy Schubert 697369c6923SCy Schubert /** find element in tree by id */ 698369c6923SCy Schubert struct waiting_tcp* reuse_tcp_by_id_find(struct reuse_tcp* reuse, uint16_t id); 699369c6923SCy Schubert 700369c6923SCy Schubert /** insert element in tree by id */ 701369c6923SCy Schubert void reuse_tree_by_id_insert(struct reuse_tcp* reuse, struct waiting_tcp* w); 702369c6923SCy Schubert 7035469a995SCy Schubert /** insert element in tcp_reuse tree and LRU list */ 7045469a995SCy Schubert int reuse_tcp_insert(struct outside_network* outnet, 7055469a995SCy Schubert struct pending_tcp* pend_tcp); 7065469a995SCy Schubert 7075469a995SCy Schubert /** touch the LRU of the element */ 7085469a995SCy Schubert void reuse_tcp_lru_touch(struct outside_network* outnet, 7095469a995SCy Schubert struct reuse_tcp* reuse); 7105469a995SCy Schubert 7115469a995SCy Schubert /** remove element from tree and LRU list */ 7125469a995SCy Schubert void reuse_tcp_remove_tree_list(struct outside_network* outnet, 7135469a995SCy Schubert struct reuse_tcp* reuse); 7145469a995SCy Schubert 7155469a995SCy Schubert /** snip the last reuse_tcp element off of the LRU list if any */ 7165469a995SCy Schubert struct reuse_tcp* reuse_tcp_lru_snip(struct outside_network* outnet); 7175469a995SCy Schubert 718369c6923SCy Schubert /** delete readwait waiting_tcp elements, deletes the elements in the list */ 719369c6923SCy Schubert void reuse_del_readwait(rbtree_type* tree_by_id); 720369c6923SCy Schubert 721*865f46b2SCy Schubert /** remove waiting tcp from the outnet waiting list */ 722*865f46b2SCy Schubert void outnet_waiting_tcp_list_remove(struct outside_network* outnet, 723*865f46b2SCy Schubert struct waiting_tcp* w); 724*865f46b2SCy Schubert 725*865f46b2SCy Schubert /** pop the first waiting tcp from the outnet waiting list */ 726*865f46b2SCy Schubert struct waiting_tcp* outnet_waiting_tcp_list_pop(struct outside_network* outnet); 727*865f46b2SCy Schubert 728*865f46b2SCy Schubert /** add waiting_tcp element to the outnet tcp waiting list */ 729*865f46b2SCy Schubert void outnet_waiting_tcp_list_add(struct outside_network* outnet, 730*865f46b2SCy Schubert struct waiting_tcp* w, int set_timer); 731*865f46b2SCy Schubert 732*865f46b2SCy Schubert /** add waiting_tcp element as first to the outnet tcp waiting list */ 733*865f46b2SCy Schubert void outnet_waiting_tcp_list_add_first(struct outside_network* outnet, 734*865f46b2SCy Schubert struct waiting_tcp* w, int reset_timer); 735*865f46b2SCy Schubert 736*865f46b2SCy Schubert /** pop the first element from the writewait list */ 737*865f46b2SCy Schubert struct waiting_tcp* reuse_write_wait_pop(struct reuse_tcp* reuse); 738*865f46b2SCy Schubert 739*865f46b2SCy Schubert /** remove the element from the writewait list */ 740*865f46b2SCy Schubert void reuse_write_wait_remove(struct reuse_tcp* reuse, struct waiting_tcp* w); 741*865f46b2SCy Schubert 742*865f46b2SCy Schubert /** push the element after the last on the writewait list */ 743*865f46b2SCy Schubert void reuse_write_wait_push_back(struct reuse_tcp* reuse, struct waiting_tcp* w); 744*865f46b2SCy Schubert 74557bddd21SDag-Erling Smørgrav /** get TCP file descriptor for address, returns -1 on failure, 74657bddd21SDag-Erling Smørgrav * tcp_mss is 0 or maxseg size to set for TCP packets. */ 7475469a995SCy Schubert int outnet_get_tcp_fd(struct sockaddr_storage* addr, socklen_t addrlen, 7485469a995SCy Schubert int tcp_mss, int dscp); 74957bddd21SDag-Erling Smørgrav 75057bddd21SDag-Erling Smørgrav /** 75157bddd21SDag-Erling Smørgrav * Create udp commpoint suitable for sending packets to the destination. 75257bddd21SDag-Erling Smørgrav * @param outnet: outside_network with the comm_base it is attached to, 75357bddd21SDag-Erling Smørgrav * with the outgoing interfaces chosen from, and rnd gen for random. 75457bddd21SDag-Erling Smørgrav * @param cb: callback function for the commpoint. 75557bddd21SDag-Erling Smørgrav * @param cb_arg: callback argument for cb. 75657bddd21SDag-Erling Smørgrav * @param to_addr: intended destination. 75757bddd21SDag-Erling Smørgrav * @param to_addrlen: length of to_addr. 75857bddd21SDag-Erling Smørgrav * @return commpoint that you can comm_point_send_udp_msg with, or NULL. 75957bddd21SDag-Erling Smørgrav */ 76057bddd21SDag-Erling Smørgrav struct comm_point* outnet_comm_point_for_udp(struct outside_network* outnet, 76157bddd21SDag-Erling Smørgrav comm_point_callback_type* cb, void* cb_arg, 76257bddd21SDag-Erling Smørgrav struct sockaddr_storage* to_addr, socklen_t to_addrlen); 76357bddd21SDag-Erling Smørgrav 76457bddd21SDag-Erling Smørgrav /** 76557bddd21SDag-Erling Smørgrav * Create tcp commpoint suitable for communication to the destination. 76657bddd21SDag-Erling Smørgrav * It also performs connect() to the to_addr. 76757bddd21SDag-Erling Smørgrav * @param outnet: outside_network with the comm_base it is attached to, 76857bddd21SDag-Erling Smørgrav * and the tcp_mss. 76957bddd21SDag-Erling Smørgrav * @param cb: callback function for the commpoint. 77057bddd21SDag-Erling Smørgrav * @param cb_arg: callback argument for cb. 77157bddd21SDag-Erling Smørgrav * @param to_addr: intended destination. 77257bddd21SDag-Erling Smørgrav * @param to_addrlen: length of to_addr. 77357bddd21SDag-Erling Smørgrav * @param query: initial packet to send writing, in buffer. It is copied 77457bddd21SDag-Erling Smørgrav * to the commpoint buffer that is created. 77557bddd21SDag-Erling Smørgrav * @param timeout: timeout for the TCP connection. 77657bddd21SDag-Erling Smørgrav * timeout in milliseconds, or -1 for no (change to the) timeout. 77757bddd21SDag-Erling Smørgrav * So seconds*1000. 778e86b9096SDag-Erling Smørgrav * @param ssl: set to true for TLS. 779e86b9096SDag-Erling Smørgrav * @param host: hostname for host name verification of TLS (or NULL if no TLS). 78057bddd21SDag-Erling Smørgrav * @return tcp_out commpoint, or NULL. 78157bddd21SDag-Erling Smørgrav */ 78257bddd21SDag-Erling Smørgrav struct comm_point* outnet_comm_point_for_tcp(struct outside_network* outnet, 78357bddd21SDag-Erling Smørgrav comm_point_callback_type* cb, void* cb_arg, 78457bddd21SDag-Erling Smørgrav struct sockaddr_storage* to_addr, socklen_t to_addrlen, 785e86b9096SDag-Erling Smørgrav struct sldns_buffer* query, int timeout, int ssl, char* host); 78657bddd21SDag-Erling Smørgrav 78757bddd21SDag-Erling Smørgrav /** 78857bddd21SDag-Erling Smørgrav * Create http commpoint suitable for communication to the destination. 78957bddd21SDag-Erling Smørgrav * Creates the http request buffer. It also performs connect() to the to_addr. 79057bddd21SDag-Erling Smørgrav * @param outnet: outside_network with the comm_base it is attached to, 79157bddd21SDag-Erling Smørgrav * and the tcp_mss. 79257bddd21SDag-Erling Smørgrav * @param cb: callback function for the commpoint. 79357bddd21SDag-Erling Smørgrav * @param cb_arg: callback argument for cb. 79457bddd21SDag-Erling Smørgrav * @param to_addr: intended destination. 79557bddd21SDag-Erling Smørgrav * @param to_addrlen: length of to_addr. 79657bddd21SDag-Erling Smørgrav * @param timeout: timeout for the TCP connection. 79757bddd21SDag-Erling Smørgrav * timeout in milliseconds, or -1 for no (change to the) timeout. 79857bddd21SDag-Erling Smørgrav * So seconds*1000. 79957bddd21SDag-Erling Smørgrav * @param ssl: set to true for https. 80057bddd21SDag-Erling Smørgrav * @param host: hostname to use for the destination. part of http request. 80157bddd21SDag-Erling Smørgrav * @param path: pathname to lookup, eg. name of the file on the destination. 8025469a995SCy Schubert * @param cfg: running configuration for User-Agent setup. 80357bddd21SDag-Erling Smørgrav * @return http_out commpoint, or NULL. 80457bddd21SDag-Erling Smørgrav */ 80557bddd21SDag-Erling Smørgrav struct comm_point* outnet_comm_point_for_http(struct outside_network* outnet, 80657bddd21SDag-Erling Smørgrav comm_point_callback_type* cb, void* cb_arg, 80757bddd21SDag-Erling Smørgrav struct sockaddr_storage* to_addr, socklen_t to_addrlen, int timeout, 8085469a995SCy Schubert int ssl, char* host, char* path, struct config_file* cfg); 80957bddd21SDag-Erling Smørgrav 81057bddd21SDag-Erling Smørgrav /** connect tcp connection to addr, 0 on failure */ 81157bddd21SDag-Erling Smørgrav int outnet_tcp_connect(int s, struct sockaddr_storage* addr, socklen_t addrlen); 81257bddd21SDag-Erling Smørgrav 813b7579f77SDag-Erling Smørgrav /** callback for incoming udp answers from the network */ 814b7579f77SDag-Erling Smørgrav int outnet_udp_cb(struct comm_point* c, void* arg, int error, 815b7579f77SDag-Erling Smørgrav struct comm_reply *reply_info); 816b7579f77SDag-Erling Smørgrav 817b7579f77SDag-Erling Smørgrav /** callback for pending tcp connections */ 818b7579f77SDag-Erling Smørgrav int outnet_tcp_cb(struct comm_point* c, void* arg, int error, 819b7579f77SDag-Erling Smørgrav struct comm_reply *reply_info); 820b7579f77SDag-Erling Smørgrav 821b7579f77SDag-Erling Smørgrav /** callback for udp timeout */ 822b7579f77SDag-Erling Smørgrav void pending_udp_timer_cb(void *arg); 823b7579f77SDag-Erling Smørgrav 82417d15b25SDag-Erling Smørgrav /** callback for udp delay for timeout */ 82517d15b25SDag-Erling Smørgrav void pending_udp_timer_delay_cb(void *arg); 82617d15b25SDag-Erling Smørgrav 827b7579f77SDag-Erling Smørgrav /** callback for outgoing TCP timer event */ 828b7579f77SDag-Erling Smørgrav void outnet_tcptimer(void* arg); 829b7579f77SDag-Erling Smørgrav 8309cf5bc93SCy Schubert /** callback to send serviced queries */ 8319cf5bc93SCy Schubert void serviced_timer_cb(void *arg); 8329cf5bc93SCy Schubert 833b7579f77SDag-Erling Smørgrav /** callback for serviced query UDP answers */ 834b7579f77SDag-Erling Smørgrav int serviced_udp_callback(struct comm_point* c, void* arg, int error, 835b7579f77SDag-Erling Smørgrav struct comm_reply* rep); 836b7579f77SDag-Erling Smørgrav 837b7579f77SDag-Erling Smørgrav /** TCP reply or error callback for serviced queries */ 838b7579f77SDag-Erling Smørgrav int serviced_tcp_callback(struct comm_point* c, void* arg, int error, 839b7579f77SDag-Erling Smørgrav struct comm_reply* rep); 840b7579f77SDag-Erling Smørgrav 841b7579f77SDag-Erling Smørgrav /** compare function of pending rbtree */ 842b7579f77SDag-Erling Smørgrav int pending_cmp(const void* key1, const void* key2); 843b7579f77SDag-Erling Smørgrav 844b7579f77SDag-Erling Smørgrav /** compare function of serviced query rbtree */ 845b7579f77SDag-Erling Smørgrav int serviced_cmp(const void* key1, const void* key2); 846b7579f77SDag-Erling Smørgrav 847369c6923SCy Schubert /** compare function of reuse_tcp rbtree in outside_network struct */ 848369c6923SCy Schubert int reuse_cmp(const void* key1, const void* key2); 849369c6923SCy Schubert 850369c6923SCy Schubert /** compare function of reuse_tcp tree_by_id rbtree */ 851369c6923SCy Schubert int reuse_id_cmp(const void* key1, const void* key2); 852369c6923SCy Schubert 853b7579f77SDag-Erling Smørgrav #endif /* OUTSIDE_NETWORK_H */ 854