1 /* 2 * services/outside_network.h - listen to answers from the network 3 * 4 * Copyright (c) 2007, NLnet Labs. All rights reserved. 5 * 6 * This software is open source. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * Redistributions of source code must retain the above copyright notice, 13 * this list of conditions and the following disclaimer. 14 * 15 * Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 19 * Neither the name of the NLNET LABS nor the names of its contributors may 20 * be used to endorse or promote products derived from this software without 21 * specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 /** 37 * \file 38 * 39 * This file has functions to send queries to authoritative servers, 40 * and wait for the pending answer, with timeouts. 41 */ 42 43 #ifndef OUTSIDE_NETWORK_H 44 #define OUTSIDE_NETWORK_H 45 46 #include "util/alloc.h" 47 #include "util/rbtree.h" 48 #include "util/regional.h" 49 #include "util/netevent.h" 50 #include "dnstap/dnstap_config.h" 51 #ifdef __QNX__ 52 /* For struct timeval */ 53 #include <sys/time.h> 54 #endif /* __QNX__ */ 55 struct pending; 56 struct pending_timeout; 57 struct ub_randstate; 58 struct pending_tcp; 59 struct waiting_tcp; 60 struct waiting_udp; 61 struct reuse_tcp; 62 struct infra_cache; 63 struct port_comm; 64 struct port_if; 65 struct sldns_buffer; 66 struct serviced_query; 67 struct dt_env; 68 struct edns_option; 69 struct module_env; 70 struct module_qstate; 71 struct query_info; 72 struct config_file; 73 struct shared_ports; 74 struct shared_ports_if; 75 76 /** 77 * Send queries to outside servers and wait for answers from servers. 78 * Contains answer-listen sockets. 79 */ 80 struct outside_network { 81 /** Base for select calls */ 82 struct comm_base* base; 83 /** pointer to time in seconds */ 84 time_t* now_secs; 85 /** pointer to time in microseconds */ 86 struct timeval* now_tv; 87 88 /** buffer shared by UDP connections, since there is only one 89 datagram at any time. */ 90 struct sldns_buffer* udp_buff; 91 /** serviced_callbacks malloc overhead when processing multiple 92 * identical serviced queries to the same server. */ 93 size_t svcd_overhead; 94 /** use x20 bits to encode additional ID random bits */ 95 int use_caps_for_id; 96 /** outside network wants to quit. Stop queued msgs from sent. */ 97 int want_to_quit; 98 99 /** number of unwanted replies received (for statistics) */ 100 size_t unwanted_replies; 101 /** cumulative total of unwanted replies (for defense) */ 102 size_t unwanted_total; 103 /** threshold when to take defensive action. If 0 then never. */ 104 size_t unwanted_threshold; 105 /** what action to take, called when defensive action is needed */ 106 void (*unwanted_action)(void*); 107 /** user param for action */ 108 void* unwanted_param; 109 110 /** linked list of available commpoints, unused file descriptors, 111 * for use as outgoing UDP ports. cp.fd=-1 in them. */ 112 struct port_comm* unused_fds; 113 /** if udp is done */ 114 int do_udp; 115 /** if udp is delay-closed (delayed answers do not meet closed port)*/ 116 int delayclose; 117 /** timeout for delayclose */ 118 struct timeval delay_tv; 119 /** if we perform udp-connect, connect() for UDP socket to mitigate 120 * ICMP side channel leakage */ 121 int udp_connect; 122 /** number of udp packets sent. */ 123 size_t num_udp_outgoing; 124 /** the shared ports structure, with random ports numbers. 125 * This is a reference to the member in the daemon structure. */ 126 struct shared_ports* shared_ports; 127 128 /** array of outgoing IP4 interfaces */ 129 struct port_if* ip4_ifs; 130 /** number of outgoing IP4 interfaces */ 131 int num_ip4; 132 133 /** array of outgoing IP6 interfaces */ 134 struct port_if* ip6_ifs; 135 /** number of outgoing IP6 interfaces */ 136 int num_ip6; 137 138 /** pending udp queries waiting to be sent out, waiting for fd */ 139 struct pending* udp_wait_first; 140 /** last pending udp query in list */ 141 struct pending* udp_wait_last; 142 143 /** pending udp answers. sorted by id, addr */ 144 rbtree_type* pending; 145 /** serviced queries, sorted by qbuf, addr, dnssec */ 146 rbtree_type* serviced; 147 /** host cache, pointer but not owned by outnet. */ 148 struct infra_cache* infra; 149 /** where to get random numbers */ 150 struct ub_randstate* rnd; 151 /** ssl context to create ssl wrapped TCP with DNS connections */ 152 void* sslctx; 153 /** if SNI will be used for TLS connections */ 154 int tls_use_sni; 155 #ifdef USE_DNSTAP 156 /** dnstap environment */ 157 struct dt_env* dtenv; 158 #endif 159 /** maximum segment size of tcp socket */ 160 int tcp_mss; 161 /** IP_TOS socket option requested on the sockets */ 162 int ip_dscp; 163 164 /** 165 * Array of tcp pending used for outgoing TCP connections. 166 * Each can be used to establish a TCP connection with a server. 167 * The file descriptors are -1 if they are free, and need to be 168 * opened for the tcp connection. Can be used for ip4 and ip6. 169 */ 170 struct pending_tcp **tcp_conns; 171 /** number of tcp communication points. */ 172 size_t num_tcp; 173 /** number of tcp communication points in use. */ 174 size_t num_tcp_outgoing; 175 /** max number of queries on a reuse connection */ 176 size_t max_reuse_tcp_queries; 177 /** timeout for REUSE entries in milliseconds. */ 178 int tcp_reuse_timeout; 179 /** timeout in milliseconds for TCP queries to auth servers. */ 180 int tcp_auth_query_timeout; 181 /** 182 * tree of still-open and waiting tcp connections for reuse. 183 * can be closed and reopened to get a new tcp connection. 184 * or reused to the same destination again. with timeout to close. 185 * Entries are of type struct reuse_tcp. 186 * The entries are both active and empty connections. 187 */ 188 rbtree_type tcp_reuse; 189 /** max number of tcp_reuse entries we want to keep open */ 190 size_t tcp_reuse_max; 191 /** first and last(oldest) in lru list of reuse connections. 192 * the oldest can be closed to get a new free pending_tcp if needed 193 * The list contains empty connections, that wait for timeout or 194 * a new query that can use the existing connection. */ 195 struct reuse_tcp* tcp_reuse_first, *tcp_reuse_last; 196 /** list of tcp comm points that are free for use */ 197 struct pending_tcp* tcp_free; 198 /** list of tcp queries waiting for a buffer */ 199 struct waiting_tcp* tcp_wait_first; 200 /** last of waiting query list */ 201 struct waiting_tcp* tcp_wait_last; 202 }; 203 204 /** 205 * Outgoing interface. Ports available and currently used are tracked 206 * per interface 207 */ 208 struct port_if { 209 /** address ready to allocate new socket (except port no). */ 210 struct sockaddr_storage addr; 211 /** length of addr field */ 212 socklen_t addrlen; 213 214 /** prefix length of network address (in bits), for randomisation. 215 * if 0, no randomisation. */ 216 int pfxlen; 217 218 #ifndef DISABLE_EXPLICIT_PORT_RANDOMISATION 219 /** the shared port numbers for this interface. */ 220 struct shared_ports_if* shpif; 221 #endif 222 223 /** array of the commpoints currently in use. 224 * allocated for max number of fds, first part in use. */ 225 struct port_comm** out; 226 /** max number of fds, size of out array */ 227 int maxout; 228 /** number of commpoints (and thus also ports) in use */ 229 int inuse; 230 }; 231 232 /** 233 * Outgoing commpoint for UDP port. 234 */ 235 struct port_comm { 236 /** next in free list */ 237 struct port_comm* next; 238 /** which port number (when in use) */ 239 int number; 240 /** interface it is used in */ 241 struct port_if* pif; 242 /** index in the out array of the interface */ 243 int index; 244 /** number of outstanding queries on this port */ 245 int num_outstanding; 246 /** UDP commpoint, fd=-1 if not in use */ 247 struct comm_point* cp; 248 }; 249 250 /** 251 * Shared ports, the list of ports shared across threads 252 */ 253 struct shared_ports { 254 /** mutex on the ports */ 255 lock_basic_type lock; 256 /** array of IP4 interfaces */ 257 struct shared_ports_if* ip4_ifs; 258 /** number of outgoing IP4 interfaces */ 259 int num_ip4; 260 /** array of IP6 interfaces */ 261 struct shared_ports_if* ip6_ifs; 262 /** number of outgoing IP6 interfaces */ 263 int num_ip6; 264 }; 265 266 /** 267 * Shared ports for an interface. 268 */ 269 struct shared_ports_if { 270 /** address ready to allocate new socket (except port no). */ 271 struct sockaddr_storage addr; 272 /** length of addr field */ 273 socklen_t addrlen; 274 /** if a netblock, the prefix */ 275 int pfxlen; 276 277 /** the available ports array. These are unused. 278 * Only the first total-inuse part is filled. */ 279 int* avail_ports; 280 /** the total number of available ports (size of the array) */ 281 int avail_total; 282 /** the number in use. */ 283 int inuse; 284 }; 285 286 /** 287 * Reuse TCP connection, still open can be used again. 288 */ 289 struct reuse_tcp { 290 /** rbtree node with links in tcp_reuse tree. key is NULL when not 291 * in tree. Both active and empty connections are in the tree. 292 * key is a pointer to this structure, the members used to compare 293 * are the sockaddr and and then is-ssl bool, and then ptr value is 294 * used in case the same address exists several times in the tree 295 * when there are multiple connections to the same destination to 296 * make the rbtree items unique. */ 297 rbnode_type node; 298 /** the key for the tcp_reuse tree. address of peer, ip4 or ip6, 299 * and port number of peer */ 300 struct sockaddr_storage addr; 301 /** length of addr */ 302 socklen_t addrlen; 303 /** also key for tcp_reuse tree, if ssl is used */ 304 int is_ssl; 305 /** If is_ssl is enabled, tls_auth_name is part of the key for 306 * tcp_reuse tree. If the string is NULL, it without a tls_auth_name */ 307 char* tls_auth_name; 308 /** lru chain, so that the oldest can be removed to get a new 309 * connection when all are in (re)use. oldest is last in list. 310 * The lru only contains empty connections waiting for reuse, 311 * the ones with active queries are not on the list because they 312 * do not need to be closed to make space for others. They already 313 * service a query so the close for another query does not help 314 * service a larger number of queries. */ 315 struct reuse_tcp* lru_next, *lru_prev; 316 /** true if the reuse_tcp item is on the lru list with empty items */ 317 int item_on_lru_list; 318 /** the connection to reuse, the fd is non-1 and is open. 319 * the addr and port determine where the connection is going, 320 * and is key to the rbtree. The SSL ptr determines if it is 321 * a TLS connection or a plain TCP connection there. And TLS 322 * or not is also part of the key to the rbtree. 323 * There is a timeout and read event on the fd, to close it. */ 324 struct pending_tcp* pending; 325 /** 326 * The more read again value pointed to by the commpoint 327 * tcp_more_read_again pointer, so that it exists after commpoint 328 * delete 329 */ 330 int cp_more_read_again; 331 /** 332 * The more write again value pointed to by the commpoint 333 * tcp_more_write_again pointer, so that it exists after commpoint 334 * delete 335 */ 336 int cp_more_write_again; 337 /** rbtree with other queries waiting on the connection, by ID number, 338 * of type struct waiting_tcp. It is for looking up received 339 * answers to the structure for callback. And also to see if ID 340 * numbers are unused and can be used for a new query. 341 * The write_wait elements are also in the tree, so that ID numbers 342 * can be looked up also for them. They are bool write_wait_queued. */ 343 rbtree_type tree_by_id; 344 /** list of queries waiting to be written on the channel, 345 * if NULL no queries are waiting to be written and the pending->query 346 * is the query currently serviced. The first is the next in line. 347 * They are also in the tree_by_id. Once written, the are removed 348 * from this list, but stay in the tree. */ 349 struct waiting_tcp* write_wait_first, *write_wait_last; 350 /** the outside network it is part of */ 351 struct outside_network* outnet; 352 }; 353 354 /** 355 * A query that has an answer pending for it. 356 */ 357 struct pending { 358 /** redblacktree entry, key is the pending struct(id, addr). */ 359 rbnode_type node; 360 /** the ID for the query. int so that a value out of range can 361 * be used to signify a pending that is for certain not present in 362 * the rbtree. (and for which deletion is safe). */ 363 unsigned int id; 364 /** remote address. */ 365 struct sockaddr_storage addr; 366 /** length of addr field in use. */ 367 socklen_t addrlen; 368 /** comm point it was sent on (and reply must come back on). */ 369 struct port_comm* pc; 370 /** timeout event */ 371 struct comm_timer* timer; 372 /** callback for the timeout, error or reply to the message */ 373 comm_point_callback_type* cb; 374 /** callback user argument */ 375 void* cb_arg; 376 /** the outside network it is part of */ 377 struct outside_network* outnet; 378 /** the corresponding serviced_query */ 379 struct serviced_query* sq; 380 381 /*---- filled if udp pending is waiting -----*/ 382 /** next in waiting list. */ 383 struct pending* next_waiting; 384 /** timeout in msec */ 385 int timeout; 386 /** The query itself, the query packet to send. */ 387 uint8_t* pkt; 388 /** length of query packet. */ 389 size_t pkt_len; 390 }; 391 392 /** 393 * Pending TCP query to server. 394 */ 395 struct pending_tcp { 396 /** next in list of free tcp comm points, or NULL. */ 397 struct pending_tcp* next_free; 398 /** port for of the outgoing interface that is used */ 399 struct port_if* pi; 400 /** tcp comm point it was sent on (and reply must come back on). */ 401 struct comm_point* c; 402 /** the query being serviced, NULL if the pending_tcp is unused. */ 403 struct waiting_tcp* query; 404 /** the pre-allocated reuse tcp structure. if ->pending is nonNULL 405 * it is in use and the connection is waiting for reuse. 406 * It is here for memory pre-allocation, and used to make this 407 * pending_tcp wait for reuse. */ 408 struct reuse_tcp reuse; 409 }; 410 411 /** 412 * Query waiting for TCP buffer. 413 */ 414 struct waiting_tcp { 415 /** 416 * next in waiting list. 417 * if on_tcp_waiting_list==0, this points to the pending_tcp structure. 418 */ 419 struct waiting_tcp* next_waiting; 420 /** if true the item is on the tcp waiting list and next_waiting 421 * is used for that. If false, the next_waiting points to the 422 * pending_tcp */ 423 int on_tcp_waiting_list; 424 /** next and prev in query waiting list for stream connection */ 425 struct waiting_tcp* write_wait_prev, *write_wait_next; 426 /** true if the waiting_tcp structure is on the write_wait queue */ 427 int write_wait_queued; 428 /** entry in reuse.tree_by_id, if key is NULL, not in tree, otherwise, 429 * this struct is key and sorted by ID (from waiting_tcp.id). */ 430 rbnode_type id_node; 431 /** the ID for the query; checked in reply */ 432 uint16_t id; 433 /** timeout event; timer keeps running whether the query is 434 * waiting for a buffer or the tcp reply is pending */ 435 struct comm_timer* timer; 436 /** timeout in msec */ 437 int timeout; 438 /** the outside network it is part of */ 439 struct outside_network* outnet; 440 /** remote address. */ 441 struct sockaddr_storage addr; 442 /** length of addr field in use. */ 443 socklen_t addrlen; 444 /** 445 * The query itself, the query packet to send. 446 * allocated after the waiting_tcp structure. 447 */ 448 uint8_t* pkt; 449 /** length of query packet. */ 450 size_t pkt_len; 451 /** callback for the timeout, error or reply to the message, 452 * or NULL if no user is waiting. the entry uses an ID number. 453 * a query that was written is no longer needed, but the ID number 454 * and a reply will come back and can be ignored if NULL */ 455 comm_point_callback_type* cb; 456 /** callback user argument */ 457 void* cb_arg; 458 /** if it uses ssl upstream */ 459 int ssl_upstream; 460 /** owned copy of the tls_auth_name (malloced) */ 461 char* tls_auth_name; 462 /** the packet was involved in an error, to stop looping errors */ 463 int error_count; 464 /** if true, the item is at the cb_and_decommission stage */ 465 int in_cb_and_decommission; 466 #ifdef USE_DNSTAP 467 /** serviced query pointer for dnstap to get logging info, if nonNULL*/ 468 struct serviced_query* sq; 469 #endif 470 }; 471 472 /** 473 * Callback to party interested in serviced query results. 474 */ 475 struct service_callback { 476 /** next in callback list */ 477 struct service_callback* next; 478 /** callback function */ 479 comm_point_callback_type* cb; 480 /** user argument for callback function */ 481 void* cb_arg; 482 }; 483 484 /** fallback size for fragmentation for EDNS in IPv4 */ 485 #define EDNS_FRAG_SIZE_IP4 1472 486 /** fallback size for EDNS in IPv6, fits one fragment with ip6-tunnel-ids */ 487 #define EDNS_FRAG_SIZE_IP6 1232 488 489 /** 490 * Query service record. 491 * Contains query and destination. UDP, TCP, EDNS are all tried. 492 * complete with retries and timeouts. A number of interested parties can 493 * receive a callback. 494 */ 495 struct serviced_query { 496 /** The rbtree node, key is this record */ 497 rbnode_type node; 498 /** The query that needs to be answered. Starts with flags u16, 499 * then qdcount, ..., including qname, qtype, qclass. Does not include 500 * EDNS record. */ 501 uint8_t* qbuf; 502 /** length of qbuf. */ 503 size_t qbuflen; 504 /** If an EDNS section is included, the DO/CD bit will be turned on. */ 505 int dnssec; 506 /** We want signatures, or else the answer is likely useless */ 507 int want_dnssec; 508 /** ignore capsforid */ 509 int nocaps; 510 /** tcp upstream used, use tcp, or ssl_upstream for SSL */ 511 int tcp_upstream, ssl_upstream; 512 /** the name of the tls authentication name, eg. 'ns.example.com' 513 * or NULL */ 514 char* tls_auth_name; 515 /** where to send it */ 516 struct sockaddr_storage addr; 517 /** length of addr field in use. */ 518 socklen_t addrlen; 519 /** zone name, uncompressed domain name in wireformat */ 520 uint8_t* zone; 521 /** length of zone name */ 522 size_t zonelen; 523 /** qtype */ 524 int qtype; 525 /** current status */ 526 enum serviced_query_status { 527 /** initial status */ 528 serviced_initial, 529 /** UDP with EDNS sent */ 530 serviced_query_UDP_EDNS, 531 /** UDP without EDNS sent */ 532 serviced_query_UDP, 533 /** TCP with EDNS sent */ 534 serviced_query_TCP_EDNS, 535 /** TCP without EDNS sent */ 536 serviced_query_TCP, 537 /** probe to test noEDNS0 (EDNS gives FORMERRorNOTIMP) */ 538 serviced_query_UDP_EDNS_fallback, 539 /** probe to test TCP noEDNS0 (EDNS gives FORMERRorNOTIMP) */ 540 serviced_query_TCP_EDNS_fallback, 541 /** send UDP query with EDNS1472 (or 1232) */ 542 serviced_query_UDP_EDNS_FRAG 543 } 544 /** variable with current status */ 545 status; 546 /** true if serviced_query is scheduled for deletion already */ 547 int to_be_deleted; 548 /** number of UDP retries */ 549 int retry; 550 /** time last UDP was sent */ 551 struct timeval last_sent_time; 552 /** rtt of last message */ 553 int last_rtt; 554 /** do we know edns probe status already, for UDP_EDNS queries */ 555 int edns_lame_known; 556 /** edns options to use for sending upstream packet */ 557 struct edns_option* opt_list; 558 /** outside network this is part of */ 559 struct outside_network* outnet; 560 /** list of interested parties that need callback on results. */ 561 struct service_callback* cblist; 562 /** the UDP or TCP query that is pending, see status which */ 563 void* pending; 564 /** block size with which to pad encrypted queries (default: 128) */ 565 size_t padding_block_size; 566 /** region for this serviced query. Will be cleared when this 567 * serviced_query will be deleted */ 568 struct regional* region; 569 /** allocation service for the region */ 570 struct alloc_cache* alloc; 571 /** flash timer to start the net I/O as a separate event */ 572 struct comm_timer* timer; 573 /** true if serviced_query is currently doing net I/O and may block */ 574 int busy; 575 }; 576 577 /** 578 * Create outside_network structure with N udp ports. 579 * @param base: the communication base to use for event handling. 580 * @param bufsize: size for network buffers. 581 * @param num_ports: number of udp ports to open per interface. 582 * @param ifs: interface names (or NULL for default interface). 583 * These interfaces must be able to access all authoritative servers. 584 * @param num_ifs: number of names in array ifs. 585 * @param do_ip4: service IP4. 586 * @param do_ip6: service IP6. 587 * @param num_tcp: number of outgoing tcp buffers to preallocate. 588 * @param dscp: DSCP to use. 589 * @param infra: pointer to infra cached used for serviced queries. 590 * @param rnd: stored to create random numbers for serviced queries. 591 * @param use_caps_for_id: enable to use 0x20 bits to encode id randomness. 592 * @param unwanted_threshold: when to take defensive action. 593 * @param unwanted_action: the action to take. 594 * @param unwanted_param: user parameter to action. 595 * @param tcp_mss: maximum segment size of tcp socket. 596 * @param do_udp: if udp is done. 597 * @param sslctx: context to create outgoing connections with (if enabled). 598 * @param delayclose: if not 0, udp sockets are delayed before timeout closure. 599 * msec to wait on timeouted udp sockets. 600 * @param tls_use_sni: if SNI is used for TLS connections. 601 * @param dtenv: environment to send dnstap events with (if enabled). 602 * @param udp_connect: if the udp_connect option is enabled. 603 * @param max_reuse_tcp_queries: max number of queries on a reuse connection. 604 * @param tcp_reuse_timeout: timeout for REUSE entries in milliseconds. 605 * @param tcp_auth_query_timeout: timeout in milliseconds for TCP queries to auth servers. 606 * @param shared_ports: the shared_ports structure. 607 * @return: the new structure (with no pending answers) or NULL on error. 608 */ 609 struct outside_network* outside_network_create(struct comm_base* base, 610 size_t bufsize, size_t num_ports, char** ifs, int num_ifs, 611 int do_ip4, int do_ip6, size_t num_tcp, int dscp, struct infra_cache* infra, 612 struct ub_randstate* rnd, int use_caps_for_id, 613 size_t unwanted_threshold, int tcp_mss, 614 void (*unwanted_action)(void*), void* unwanted_param, int do_udp, 615 void* sslctx, int delayclose, int tls_use_sni, struct dt_env *dtenv, 616 int udp_connect, int max_reuse_tcp_queries, int tcp_reuse_timeout, 617 int tcp_auth_query_timeout, struct shared_ports* shared_ports); 618 619 /** 620 * Delete outside_network structure. 621 * @param outnet: object to delete. 622 */ 623 void outside_network_delete(struct outside_network* outnet); 624 625 /** 626 * Prepare for quit. Sends no more queries, even if queued up. 627 * @param outnet: object to prepare for removal 628 */ 629 void outside_network_quit_prepare(struct outside_network* outnet); 630 631 /** 632 * Send UDP query, create pending answer. 633 * Changes the ID for the query to be random and unique for that destination. 634 * @param sq: serviced query. 635 * @param packet: wireformat query to send to destination. 636 * @param timeout: in milliseconds from now. 637 * @param callback: function to call on error, timeout or reply. 638 * @param callback_arg: user argument for callback function. 639 * @return: NULL on error for malloc or socket. Else the pending query object. 640 */ 641 struct pending* pending_udp_query(struct serviced_query* sq, 642 struct sldns_buffer* packet, int timeout, comm_point_callback_type* callback, 643 void* callback_arg); 644 645 /** 646 * Send TCP query. May wait for TCP buffer. Selects ID to be random, and 647 * checks id. 648 * @param sq: serviced query. 649 * @param packet: wireformat query to send to destination. copied from. 650 * @param timeout: in milliseconds from now. 651 * Timer starts running now. Timer may expire if all buffers are used, 652 * without any query been sent to the server yet. 653 * @param callback: function to call on error, timeout or reply. 654 * @param callback_arg: user argument for callback function. 655 * @return: false on error for malloc or socket. Else the pending TCP object. 656 */ 657 struct waiting_tcp* pending_tcp_query(struct serviced_query* sq, 658 struct sldns_buffer* packet, int timeout, comm_point_callback_type* callback, 659 void* callback_arg); 660 661 /** 662 * Delete pending answer. 663 * @param outnet: outside network the pending query is part of. 664 * Internal feature: if outnet is NULL, p is not unlinked from rbtree. 665 * @param p: deleted 666 */ 667 void pending_delete(struct outside_network* outnet, struct pending* p); 668 669 /** 670 * Perform a serviced query to the authoritative servers. 671 * Duplicate efforts are detected, and EDNS, TCP and UDP retry is performed. 672 * @param outnet: outside network, with rbtree of serviced queries. 673 * @param qinfo: query info. 674 * @param flags: flags u16 (host format), includes opcode, CD bit. 675 * @param dnssec: if set, DO bit is set in EDNS queries. 676 * If the value includes BIT_CD, CD bit is set when in EDNS queries. 677 * If the value includes BIT_DO, DO bit is set when in EDNS queries. 678 * @param want_dnssec: signatures are needed, without EDNS the answer is 679 * likely to be useless. 680 * @param nocaps: ignore use_caps_for_id and use unperturbed qname. 681 * @param check_ratelimit: if set, will check ratelimit before sending out. 682 * @param tcp_upstream: use TCP for upstream queries. 683 * @param ssl_upstream: use SSL for upstream queries. 684 * @param tls_auth_name: when ssl_upstream is true, use this name to check 685 * the server's peer certificate. 686 * @param addr: to which server to send the query. 687 * @param addrlen: length of addr. 688 * @param zone: name of the zone of the delegation point. wireformat dname. 689 This is the delegation point name for which the server is deemed 690 authoritative. 691 * @param zonelen: length of zone. 692 * @param qstate: module qstate. Mainly for inspecting the available 693 * edns_opts_lists. 694 * @param callback: callback function. 695 * @param callback_arg: user argument to callback function. 696 * @param buff: scratch buffer to create query contents in. Empty on exit. 697 * @param env: the module environment. 698 * @param was_ratelimited: it will signal back if the query failed to pass the 699 * ratelimit check. 700 * @param ratelimit_incremented: set to true if the ratelimit counter 701 * was increased. 702 * @return 0 on error, or pointer to serviced query that is used to answer 703 * this serviced query may be shared with other callbacks as well. 704 */ 705 struct serviced_query* outnet_serviced_query(struct outside_network* outnet, 706 struct query_info* qinfo, uint16_t flags, int dnssec, int want_dnssec, 707 int nocaps, int check_ratelimit, int tcp_upstream, int ssl_upstream, 708 char* tls_auth_name, struct sockaddr_storage* addr, socklen_t addrlen, 709 uint8_t* zone, size_t zonelen, struct module_qstate* qstate, 710 comm_point_callback_type* callback, void* callback_arg, 711 struct sldns_buffer* buff, struct module_env* env, int* was_ratelimited, 712 int* ratelimit_incremented); 713 714 /** 715 * Remove service query callback. 716 * If that leads to zero callbacks, the query is completely cancelled. 717 * @param sq: serviced query to adjust. 718 * @param cb_arg: callback argument of callback that needs removal. 719 * same as the callback_arg to outnet_serviced_query(). 720 */ 721 void outnet_serviced_query_stop(struct serviced_query* sq, void* cb_arg); 722 723 /** 724 * Get memory size in use by outside network. 725 * Counts buffers and outstanding query (serviced queries) malloced data. 726 * @param outnet: outside network structure. 727 * @return size in bytes. 728 */ 729 size_t outnet_get_mem(struct outside_network* outnet); 730 731 /** 732 * Get memory size in use by serviced query while it is servicing callbacks. 733 * This takes into account the pre-deleted status of it; it will be deleted 734 * when the callbacks are done. 735 * @param sq: serviced query. 736 * @return size in bytes. 737 */ 738 size_t serviced_get_mem(struct serviced_query* sq); 739 740 /** Pick random ID value for a tcp stream, avoids existing IDs. */ 741 uint16_t reuse_tcp_select_id(struct reuse_tcp* reuse, 742 struct outside_network* outnet); 743 744 /** find element in tree by id */ 745 struct waiting_tcp* reuse_tcp_by_id_find(struct reuse_tcp* reuse, uint16_t id); 746 747 /** insert element in tree by id */ 748 void reuse_tree_by_id_insert(struct reuse_tcp* reuse, struct waiting_tcp* w); 749 750 /** insert element in tcp_reuse tree and LRU list */ 751 int reuse_tcp_insert(struct outside_network* outnet, 752 struct pending_tcp* pend_tcp); 753 754 /** touch the LRU of the element */ 755 void reuse_tcp_lru_touch(struct outside_network* outnet, 756 struct reuse_tcp* reuse); 757 758 /** remove element from tree and LRU list */ 759 void reuse_tcp_remove_tree_list(struct outside_network* outnet, 760 struct reuse_tcp* reuse); 761 762 /** snip the last reuse_tcp element off of the LRU list if any */ 763 struct reuse_tcp* reuse_tcp_lru_snip(struct outside_network* outnet); 764 765 /** delete readwait waiting_tcp elements, deletes the elements in the list */ 766 void reuse_del_readwait(rbtree_type* tree_by_id); 767 768 /** remove waiting tcp from the outnet waiting list */ 769 void outnet_waiting_tcp_list_remove(struct outside_network* outnet, 770 struct waiting_tcp* w); 771 772 /** pop the first waiting tcp from the outnet waiting list */ 773 struct waiting_tcp* outnet_waiting_tcp_list_pop(struct outside_network* outnet); 774 775 /** add waiting_tcp element to the outnet tcp waiting list */ 776 void outnet_waiting_tcp_list_add(struct outside_network* outnet, 777 struct waiting_tcp* w, int set_timer); 778 779 /** add waiting_tcp element as first to the outnet tcp waiting list */ 780 void outnet_waiting_tcp_list_add_first(struct outside_network* outnet, 781 struct waiting_tcp* w, int reset_timer); 782 783 /** pop the first element from the writewait list */ 784 struct waiting_tcp* reuse_write_wait_pop(struct reuse_tcp* reuse); 785 786 /** remove the element from the writewait list */ 787 void reuse_write_wait_remove(struct reuse_tcp* reuse, struct waiting_tcp* w); 788 789 /** push the element after the last on the writewait list */ 790 void reuse_write_wait_push_back(struct reuse_tcp* reuse, struct waiting_tcp* w); 791 792 /** get TCP file descriptor for address, returns -1 on failure, 793 * tcp_mss is 0 or maxseg size to set for TCP packets, 794 * nodelay (TCP_NODELAY) should be set for TLS connections to speed up the TLS 795 * handshake.*/ 796 int outnet_get_tcp_fd(struct sockaddr_storage* addr, socklen_t addrlen, 797 int tcp_mss, int dscp, int nodelay); 798 799 /** 800 * Create udp commpoint suitable for sending packets to the destination. 801 * @param outnet: outside_network with the comm_base it is attached to, 802 * with the outgoing interfaces chosen from, and rnd gen for random. 803 * @param cb: callback function for the commpoint. 804 * @param cb_arg: callback argument for cb. 805 * @param to_addr: intended destination. 806 * @param to_addrlen: length of to_addr. 807 * @return commpoint that you can comm_point_send_udp_msg with, or NULL. 808 */ 809 struct comm_point* outnet_comm_point_for_udp(struct outside_network* outnet, 810 comm_point_callback_type* cb, void* cb_arg, 811 struct sockaddr_storage* to_addr, socklen_t to_addrlen); 812 813 /** 814 * Create tcp commpoint suitable for communication to the destination. 815 * It also performs connect() to the to_addr. 816 * @param outnet: outside_network with the comm_base it is attached to, 817 * and the tcp_mss. 818 * @param cb: callback function for the commpoint. 819 * @param cb_arg: callback argument for cb. 820 * @param to_addr: intended destination. 821 * @param to_addrlen: length of to_addr. 822 * @param query: initial packet to send writing, in buffer. It is copied 823 * to the commpoint buffer that is created. 824 * @param timeout: timeout for the TCP connection. 825 * timeout in milliseconds, or -1 for no (change to the) timeout. 826 * So seconds*1000. 827 * @param ssl: set to true for TLS. 828 * @param host: hostname for host name verification of TLS (or NULL if no TLS). 829 * @return tcp_out commpoint, or NULL. 830 */ 831 struct comm_point* outnet_comm_point_for_tcp(struct outside_network* outnet, 832 comm_point_callback_type* cb, void* cb_arg, 833 struct sockaddr_storage* to_addr, socklen_t to_addrlen, 834 struct sldns_buffer* query, int timeout, int ssl, char* host); 835 836 /** 837 * Create http commpoint suitable for communication to the destination. 838 * Creates the http request buffer. It also performs connect() to the to_addr. 839 * @param outnet: outside_network with the comm_base it is attached to, 840 * and the tcp_mss. 841 * @param cb: callback function for the commpoint. 842 * @param cb_arg: callback argument for cb. 843 * @param to_addr: intended destination. 844 * @param to_addrlen: length of to_addr. 845 * @param timeout: timeout for the TCP connection. 846 * timeout in milliseconds, or -1 for no (change to the) timeout. 847 * So seconds*1000. 848 * @param ssl: set to true for https. 849 * @param host: hostname to use for the destination. part of http request. 850 * @param path: pathname to lookup, eg. name of the file on the destination. 851 * @param cfg: running configuration for User-Agent setup. 852 * @return http_out commpoint, or NULL. 853 */ 854 struct comm_point* outnet_comm_point_for_http(struct outside_network* outnet, 855 comm_point_callback_type* cb, void* cb_arg, 856 struct sockaddr_storage* to_addr, socklen_t to_addrlen, int timeout, 857 int ssl, char* host, char* path, struct config_file* cfg); 858 859 /** connect tcp connection to addr, 0 on failure */ 860 int outnet_tcp_connect(int s, struct sockaddr_storage* addr, socklen_t addrlen); 861 862 /** 863 * Create new shared ports structure. 864 * @param ifs: interface names (or NULL for default interface). 865 * These interfaces must be able to access all authoritative servers. 866 * @param num_ifs: number of names in array ifs. 867 * @param do_ip4: service IP4. 868 * @param do_ip6: service IP6. 869 * @param availports: array of available ports. 870 * @param numavailports: number of available ports in array. 871 * @return new, or NULL on failure. 872 */ 873 struct shared_ports* shared_ports_create(char** ifs, int num_ifs, int do_ip4, 874 int do_ip6, int* availports, int numavailports); 875 876 /** 877 * Delete shared ports structure. 878 * @param shp: shared ports structure. 879 */ 880 void shared_ports_delete(struct shared_ports* shp); 881 882 /** Find interface in shared ports. */ 883 struct shared_ports_if* shared_ports_find_if(struct shared_ports* shp, 884 struct sockaddr_storage* addr, socklen_t addrlen, int pfxlen); 885 886 /** 887 * Get a shared port from the list of random ports. 888 * @param shp: shared ports structure. 889 * @param shpif: the shared ports interface. 890 * @param rnd: used to make random numbers. 891 * @param udp_connect: set to true if no reuse is possible. 892 * @param reusenum: number of ports that can be reused (already open). 893 * @param port: the port number is returned. 894 * @param reused: if the port numer is reused, returned. 895 * @return false on failure. That can mean no more free ports to use. 896 */ 897 int shared_ports_fetch_random(struct shared_ports* shp, 898 struct shared_ports_if* shpif, struct ub_randstate* rnd, 899 int udp_connect, int reusenum, int* port, int* reused); 900 901 /** 902 * Return a shared port to the list of random ports. 903 * @param shp: shared ports structure. 904 * @param shpif: the shared ports interface. 905 * @param port: port number to return to be used again. 906 */ 907 void shared_ports_return_port(struct shared_ports* shp, 908 struct shared_ports_if* shpif, int port); 909 910 /** callback for incoming udp answers from the network */ 911 int outnet_udp_cb(struct comm_point* c, void* arg, int error, 912 struct comm_reply *reply_info); 913 914 /** callback for pending tcp connections */ 915 int outnet_tcp_cb(struct comm_point* c, void* arg, int error, 916 struct comm_reply *reply_info); 917 918 /** callback for udp timeout */ 919 void pending_udp_timer_cb(void *arg); 920 921 /** callback for udp delay for timeout */ 922 void pending_udp_timer_delay_cb(void *arg); 923 924 /** callback for outgoing TCP timer event */ 925 void outnet_tcptimer(void* arg); 926 927 /** callback to send serviced queries */ 928 void serviced_timer_cb(void *arg); 929 930 /** callback for serviced query UDP answers */ 931 int serviced_udp_callback(struct comm_point* c, void* arg, int error, 932 struct comm_reply* rep); 933 934 /** TCP reply or error callback for serviced queries */ 935 int serviced_tcp_callback(struct comm_point* c, void* arg, int error, 936 struct comm_reply* rep); 937 938 /** compare function of pending rbtree */ 939 int pending_cmp(const void* key1, const void* key2); 940 941 /** compare function of serviced query rbtree */ 942 int serviced_cmp(const void* key1, const void* key2); 943 944 /** compare function of reuse_tcp rbtree in outside_network struct */ 945 int reuse_cmp(const void* key1, const void* key2); 946 947 /** compare function of reuse_tcp tree_by_id rbtree */ 948 int reuse_id_cmp(const void* key1, const void* key2); 949 950 #endif /* OUTSIDE_NETWORK_H */ 951