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