unbound: services/outside_network.c File Reference

unbound 0.1

Macros | Functions
outside_network.c File Reference

This file has functions to send queries to authoritative servers and wait for the pending answer events. More...

`#include "config.h"`
`#include <ctype.h>`
`#include <sys/time.h>`
`#include "services/outside_network.h"`
`#include "services/listen_dnsport.h"`
`#include "services/cache/infra.h"`
`#include "iterator/iterator.h"`
`#include "util/data/msgparse.h"`
`#include "util/data/msgreply.h"`
`#include "util/data/msgencode.h"`
`#include "util/data/dname.h"`
`#include "util/netevent.h"`
`#include "util/log.h"`
`#include "util/net_help.h"`
`#include "util/random.h"`
`#include "util/fptr_wlist.h"`
`#include "util/edns.h"`
`#include "sldns/sbuffer.h"`
`#include "dnstap/dnstap.h"`
`#include <fcntl.h>`

## Macros
#define | MAX_ID_RETRY 1000
| number of times to retry making a random ID that is unique.
#define | MAX_PORT_RETRY 10000
| number of times to retry finding interface, port that can be opened.
#define | OUTBOUND_UDP_RETRY 1
| number of retries on outgoing UDP queries

## Functions
static void | serviced_tcp_initiate (struct serviced_query *sq, sldns_buffer *buff)
| initiate TCP transaction for serviced query
static int | randomize_and_send_udp (struct pending *pend, sldns_buffer *packet, int timeout)
| with a fd available, randomize and send UDP
static uint16_t | tcp_select_id (struct outside_network *outnet, struct reuse_tcp *reuse)
| select a DNS ID for a TCP stream
static int | serviced_udp_send (struct serviced_query *sq, sldns_buffer *buff)
| Perform serviced query UDP sending operation.
static int | serviced_tcp_send (struct serviced_query *sq, sldns_buffer *buff)
| Send serviced query over TCP return false on initial failure.
static void | serviced_callbacks (struct serviced_query *sq, int error, struct comm_point *c, struct comm_reply *rep)
| call the callbacks for a serviced query
int | pending_cmp (const void *key1, const void *key2)
| compare function of pending rbtree
int | serviced_cmp (const void *key1, const void *key2)
| compare function of serviced query rbtree
static int | reuse_cmp_addrportssl (const void *key1, const void *key2)
| compare if the reuse element has the same address, port and same ssl-is used-for-it characteristic
int | reuse_cmp (const void *key1, const void *key2)
| compare function of reuse_tcp rbtree in outside_network struct
int | reuse_id_cmp (const void *key1, const void *key2)
| compare function of reuse_tcp tree_by_id rbtree
static void | waiting_tcp_delete (struct waiting_tcp *w)
| delete waiting_tcp entry.
static int | pick_outgoing_tcp (struct pending_tcp *pend, struct waiting_tcp *w, int s)
| Pick random outgoing-interface of that family, and bind it.
int | outnet_get_tcp_fd (struct sockaddr_storage *addr, socklen_t addrlen, int tcp_mss, int dscp)
| get TCP file descriptor for address, returns -1 on failure, tcp_mss is 0 or maxseg size to set for TCP packets.
int | outnet_tcp_connect (int s, struct sockaddr_storage *addr, socklen_t addrlen)
| connect tcp connection to addr, 0 on failure
static void | log_reuse_tcp (enum verbosity_value v, const char *msg, struct reuse_tcp *reuse)
| log reuse item addr and ptr with message
struct waiting_tcp * | reuse_write_wait_pop (struct reuse_tcp *reuse)
| pop the first element from the writewait list
void | reuse_write_wait_remove (struct reuse_tcp *reuse, struct waiting_tcp *w)
| remove the element from the writewait list
void | reuse_write_wait_push_back (struct reuse_tcp *reuse, struct waiting_tcp *w)
| push the element after the last on the writewait list
void | reuse_tree_by_id_insert (struct reuse_tcp *reuse, struct waiting_tcp *w)
| insert element in tree by id
struct waiting_tcp * | reuse_tcp_by_id_find (struct reuse_tcp *reuse, uint16_t id)
| find element in tree by id
static uint16_t | tree_by_id_get_id (rbnode_type *node)
| return ID value of rbnode in tree_by_id
int | reuse_tcp_insert (struct outside_network *outnet, struct pending_tcp *pend_tcp)
| insert into reuse tcp tree and LRU, false on failure (duplicate)
static struct reuse_tcp * | reuse_tcp_find (struct outside_network *outnet, struct sockaddr_storage *addr, socklen_t addrlen, int use_ssl)
| find reuse tcp stream to destination for query, or NULL if none
static void | outnet_tcp_take_query_setup (int s, struct pending_tcp *pend, struct waiting_tcp *w)
| use the buffer to setup writing the query
static int | outnet_tcp_take_into_use (struct waiting_tcp *w)
| use next free buffer to service a tcp query
void | reuse_tcp_lru_touch (struct outside_network *outnet, struct reuse_tcp *reuse)
| Touch the lru of a reuse_tcp element, it is in use.
struct reuse_tcp * | reuse_tcp_lru_snip (struct outside_network *outnet)
| Snip the last reuse_tcp element off of the LRU list.
void | outnet_waiting_tcp_list_remove (struct outside_network *outnet, struct waiting_tcp *w)
| remove waiting tcp from the outnet waiting list
struct waiting_tcp * | outnet_waiting_tcp_list_pop (struct outside_network *outnet)
| pop the first waiting tcp from the outnet waiting list
void | outnet_waiting_tcp_list_add (struct outside_network *outnet, struct waiting_tcp *w, int set_timer)
| add waiting_tcp element to the outnet tcp waiting list
void | outnet_waiting_tcp_list_add_first (struct outside_network *outnet, struct waiting_tcp *w, int reset_timer)
| add waiting_tcp element as first to the outnet tcp waiting list
static void | waiting_tcp_callback (struct waiting_tcp *w, struct comm_point *c, int error, struct comm_reply *reply_info)
| call callback on waiting_tcp, if not NULL
static void | use_free_buffer (struct outside_network *outnet)
| see if buffers can be used to service TCP queries
static void | reuse_tree_by_id_delete (struct reuse_tcp *reuse, struct waiting_tcp *w)
| delete element from tree by id
static void | reuse_move_writewait_away (struct outside_network *outnet, struct pending_tcp *pend)
| move writewait list to go for another connection.
void | reuse_tcp_remove_tree_list (struct outside_network *outnet, struct reuse_tcp *reuse)
| remove reused element from tree and lru list
static void | reuse_del_readwait_elem (rbnode_type *node, void *ATTR_UNUSED(arg))
| helper function that deletes an element from the tree of readwait elements in tcp reuse structure
void | reuse_del_readwait (rbtree_type *tree_by_id)
| delete readwait waiting_tcp elements, deletes the elements in the list
static void | decommission_pending_tcp (struct outside_network *outnet, struct pending_tcp *pend)
| decommission a tcp buffer, closes commpoint and frees waiting_tcp entry
static void | reuse_cb_readwait_for_failure (rbtree_type *tree_by_id, int err)
| perform failure callbacks for waiting queries in reuse read rbtree
static void | mark_for_cb_and_decommission (rbnode_type *node, void *ATTR_UNUSED(arg))
| mark the entry for being in the cb_and_decommission stage
static void | reuse_cb_and_decommission (struct outside_network *outnet, struct pending_tcp *pend, int error)
| perform callbacks for failure and also decommission pending tcp.
static void | reuse_tcp_setup_timeout (struct pending_tcp *pend_tcp, int tcp_reuse_timeout)
| set timeout on tcp fd and setup read event to catch incoming dns msgs
static void | reuse_tcp_setup_read_and_timeout (struct pending_tcp *pend_tcp, int tcp_reuse_timeout)
| set timeout on tcp fd and setup read event to catch incoming dns msgs
int | outnet_tcp_cb (struct comm_point *c, void *arg, int error, struct comm_reply *reply_info)
| callback for pending tcp connections
static void | portcomm_loweruse (struct outside_network *outnet, struct port_comm *pc)
| lower use count on pc, see if it can be closed
static void | outnet_send_wait_udp (struct outside_network *outnet)
| try to send waiting UDP queries
int | outnet_udp_cb (struct comm_point *c, void *arg, int error, struct comm_reply *reply_info)
| callback for incoming udp answers from the network
static void | calc_num46 (char **ifs, int num_ifs, int do_ip4, int do_ip6, int *num_ip4, int *num_ip6)
| calculate number of ip4 and ip6 interfaces
void | pending_udp_timer_delay_cb (void *arg)
| callback for udp delay for timeout
void | pending_udp_timer_cb (void *arg)
| callback for udp timeout
static int | create_pending_tcp (struct outside_network *outnet, size_t bufsize)
| create pending_tcp buffers
static int | setup_if (struct port_if *pif, const char *addrstr, int *avail, int numavail, size_t numfd)
| setup an outgoing interface, ready address
struct outside_network * | outside_network_create (struct comm_base *base, size_t bufsize, size_t num_ports, char **ifs, int num_ifs, int do_ip4, int do_ip6, size_t num_tcp, int dscp, struct infra_cache *infra, struct ub_randstate *rnd, int use_caps_for_id, int *availports, int numavailports, size_t unwanted_threshold, int tcp_mss, void(*unwanted_action)(void *), void *unwanted_param, int do_udp, void *sslctx, int delayclose, int tls_use_sni, struct dt_env *dtenv, int udp_connect, int max_reuse_tcp_queries, int tcp_reuse_timeout, int tcp_auth_query_timeout)
| Create outside_network structure with N udp ports.
static void | pending_node_del (rbnode_type *node, void *arg)
| helper pending delete
static void | serviced_node_del (rbnode_type *node, void *ATTR_UNUSED(arg))
| helper serviced delete
void | outside_network_quit_prepare (struct outside_network *outnet)
| Prepare for quit.
void | outside_network_delete (struct outside_network *outnet)
| Delete outside_network structure.
void | pending_delete (struct outside_network *outnet, struct pending *p)
| Delete pending answer.
static void | sai6_putrandom (struct sockaddr_in6 *sa, int pfxlen, struct ub_randstate *rnd)
static int | udp_sockport (struct sockaddr_storage *addr, socklen_t addrlen, int pfxlen, int port, int *inuse, struct ub_randstate *rnd, int dscp)
| Try to open a UDP socket for outgoing communication.
static int | select_id (struct outside_network *outnet, struct pending *pend, sldns_buffer *packet)
| Select random ID.
static int | udp_connect_needs_log (int err, struct sockaddr_storage *addr, socklen_t addrlen)
| return true is UDP connect error needs to be logged
static int | select_ifport (struct outside_network *outnet, struct pending *pend, int num_if, struct port_if *ifs)
| Select random interface and port.
struct pending * | pending_udp_query (struct serviced_query *sq, struct sldns_buffer *packet, int timeout, comm_point_callback_type *cb, void *cb_arg)
| Send UDP query, create pending answer.
void | outnet_tcptimer (void *arg)
| callback for outgoing TCP timer event
static void | reuse_tcp_close_oldest (struct outside_network *outnet)
| close the oldest reuse_tcp connection to make a fd and struct pend available for a new stream connection
uint16_t | reuse_tcp_select_id (struct reuse_tcp *reuse, struct outside_network *outnet)
| find spare ID value for reuse tcp stream.
struct waiting_tcp * | pending_tcp_query (struct serviced_query *sq, sldns_buffer *packet, int timeout, comm_point_callback_type *callback, void *callback_arg)
| Send TCP query.
static void | serviced_gen_query (sldns_buffer *buff, uint8_t *qname, size_t qnamelen, uint16_t qtype, uint16_t qclass, uint16_t flags)
| create query for serviced queries
static struct serviced_query * | lookup_serviced (struct outside_network *outnet, sldns_buffer *buff, int dnssec, struct sockaddr_storage *addr, socklen_t addrlen, struct edns_option *opt_list)
| lookup serviced query in serviced query rbtree
void | serviced_timer_cb (void *arg)
| callback to send serviced queries
static struct serviced_query * | serviced_create (struct outside_network *outnet, sldns_buffer *buff, int dnssec, int want_dnssec, int nocaps, int tcp_upstream, int ssl_upstream, char *tls_auth_name, struct sockaddr_storage *addr, socklen_t addrlen, uint8_t *zone, size_t zonelen, int qtype, struct edns_option *opt_list, size_t pad_queries_block_size, struct alloc_cache *alloc, struct regional *region)
| Create new serviced entry.
static int | reuse_tcp_remove_serviced_keep (struct waiting_tcp *w, struct serviced_query *sq)
| reuse tcp stream, remove serviced query from stream, return true if the stream is kept, false if it is to be closed
static void | serviced_delete (struct serviced_query *sq)
| cleanup serviced query entry
static void | serviced_perturb_qname (struct ub_randstate *rnd, uint8_t *qbuf, size_t len)
| perturb a dname capitalization randomly
static uint16_t | serviced_query_udp_size (struct serviced_query *sq, enum serviced_query_status status)
static void | serviced_encode (struct serviced_query *sq, sldns_buffer *buff, int with_edns)
| put serviced query into a buffer
static int | serviced_check_qname (sldns_buffer *pkt, uint8_t *qbuf, size_t qbuflen)
| check that perturbed qname is identical
int | serviced_tcp_callback (struct comm_point *c, void *arg, int error, struct comm_reply *rep)
| TCP reply or error callback for serviced queries.
static int | packet_edns_malformed (struct sldns_buffer *buf, int qtype)
int | serviced_udp_callback (struct comm_point *c, void *arg, int error, struct comm_reply *rep)
| callback for serviced query UDP answers
struct serviced_query * | outnet_serviced_query (struct outside_network *outnet, struct query_info *qinfo, uint16_t flags, int dnssec, int want_dnssec, int nocaps, int check_ratelimit, int tcp_upstream, int ssl_upstream, char *tls_auth_name, struct sockaddr_storage *addr, socklen_t addrlen, uint8_t *zone, size_t zonelen, struct module_qstate *qstate, comm_point_callback_type *callback, void *callback_arg, sldns_buffer *buff, struct module_env *env, int *was_ratelimited)
| Perform a serviced query to the authoritative servers.
static void | callback_list_remove (struct serviced_query *sq, void *cb_arg)
| remove callback from list
void | outnet_serviced_query_stop (struct serviced_query *sq, void *cb_arg)
| Remove service query callback.
static int | fd_for_dest (struct outside_network *outnet, struct sockaddr_storage *to_addr, socklen_t to_addrlen)
| create fd to send to this destination
struct comm_point * | outnet_comm_point_for_udp (struct outside_network *outnet, comm_point_callback_type *cb, void *cb_arg, struct sockaddr_storage *to_addr, socklen_t to_addrlen)
| Create udp commpoint suitable for sending packets to the destination.
static int | setup_comm_ssl (struct comm_point *cp, struct outside_network *outnet, int fd, char *host)
| setup SSL for comm point
struct comm_point * | outnet_comm_point_for_tcp (struct outside_network *outnet, comm_point_callback_type *cb, void *cb_arg, struct sockaddr_storage *to_addr, socklen_t to_addrlen, sldns_buffer *query, int timeout, int ssl, char *host)
| Create tcp commpoint suitable for communication to the destination.
static void | setup_http_user_agent (sldns_buffer *buf, struct config_file *cfg)
| setup the User-Agent HTTP header based on http-user-agent configuration
static int | setup_http_request (sldns_buffer *buf, char *host, char *path, struct config_file *cfg)
| setup http request headers in buffer for sending query to destination
struct comm_point * | outnet_comm_point_for_http (struct outside_network *outnet, comm_point_callback_type *cb, void *cb_arg, struct sockaddr_storage *to_addr, socklen_t to_addrlen, int timeout, int ssl, char *host, char *path, struct config_file *cfg)
| Create http commpoint suitable for communication to the destination.
static size_t | waiting_tcp_get_mem (struct waiting_tcp *w)
| get memory used by waiting tcp entry (in use or not)
static size_t | if_get_mem (struct port_if *pif)
| get memory used by port if
static size_t | waiting_udp_get_mem (struct pending *w)
| get memory used by waiting udp
size_t | outnet_get_mem (struct outside_network *outnet)
| Get memory size in use by outside network.
size_t | serviced_get_mem (struct serviced_query *sq)
| Get memory size in use by serviced query while it is servicing callbacks.

## Detailed Description

This file has functions to send queries to authoritative servers and wait for the pending answer events.

## Function Documentation

## ◆ serviced_udp_send()

static int serviced_udp_send | ( | struct serviced_query * | sq,
|  | sldns_buffer * | buff )
| static

Perform serviced query UDP sending operation.

Sends UDP with EDNS, unless infra host marked non EDNS.

Parameters

  sq | query to send.
  buff | buffer scratch space.

Returns
  0 on error.

References serviced_query::addr, serviced_query::addrlen, serviced_query::edns_lame_known, outside_network::infra, infra_host(), serviced_query::last_rtt, serviced_query::last_sent_time, outside_network::now_secs, outside_network::now_tv, serviced_query::outnet, serviced_query::pending, pending_udp_query(), serviced_encode(), serviced_udp_callback(), serviced_query::status, VERB_ALGO, verbose(), serviced_query::zone, and serviced_query::zonelen.

Referenced by serviced_timer_cb(), and serviced_udp_callback().

## ◆ waiting_tcp_delete()

static void waiting_tcp_delete | ( | struct waiting_tcp * | w | ) |
| static

delete waiting_tcp entry.

Does not unlink from waiting list.

Parameters

  w | to delete.

References comm_timer_delete(), and waiting_tcp::timer.

Referenced by outnet_tcp_cb(), outnet_tcptimer(), outside_network_delete(), pending_tcp_query(), reuse_del_readwait_elem(), serviced_delete(), and use_free_buffer().

## ◆ pick_outgoing_tcp()

static int pick_outgoing_tcp | ( | struct pending_tcp * | pend,
|  | struct waiting_tcp * | w,
|  | int | s )
| static

Pick random outgoing-interface of that family, and bind it.

port set to 0 so OS picks a port number for us. if it is the ANY address, do not bind.

Parameters

  pend | pending tcp structure, for storing the local address choice.
  w | tcp structure with destination address.
  s | socket fd.

Returns
  false on error, socket closed.

References port_if::addr, waiting_tcp::addr, addr_is_any(), addr_is_ip6(), port_if::addrlen, waiting_tcp::addrlen, outside_network::ip4_ifs, outside_network::ip6_ifs, log_addr(), log_assert, log_err(), outside_network::num_ip4, outside_network::num_ip6, waiting_tcp::outnet, pending_tcp::pi, outside_network::rnd, sock_close(), sock_strerror(), ub_random_max(), VERB_ALGO, VERB_OPS, and verbosity.

Referenced by outnet_tcp_take_into_use().

## ◆ reuse_tcp_insert()

int reuse_tcp_insert | ( | struct outside_network * | outnet,
|  | struct pending_tcp * | pend_tcp )

insert into reuse tcp tree and LRU, false on failure (duplicate)

insert element in tcp_reuse tree and LRU list

References reuse_tcp::item_on_lru_list, rbnode_type::key, log_assert, log_err(), log_reuse_tcp(), reuse_tcp::lru_next, reuse_tcp::node, waiting_tcp::outnet, reuse_tcp::pending, rbtree_insert(), pending_tcp::reuse, outside_network::tcp_reuse, outside_network::tcp_reuse_first, and VERB_CLIENT.

Referenced by check_removal(), check_snip(), outnet_tcp_cb(), outnet_tcp_take_into_use(), reuse_tcp_remove_serviced_keep(), and tcp_reuse_tree_list_test().

## ◆ reuse_tcp_lru_touch()

void reuse_tcp_lru_touch | ( | struct outside_network * | outnet,
|  | struct reuse_tcp * | reuse )

Touch the lru of a reuse_tcp element, it is in use.

touch the LRU of the element

This moves it to the front of the list, where it is not likely to be closed. Items at the back of the list are closed to make space.

References reuse_tcp::item_on_lru_list, log_assert, log_err(), reuse_tcp::lru_next, and outside_network::tcp_reuse_first.

Referenced by pending_tcp_query(), tcp_reuse_tree_list_test(), and use_free_buffer().

## ◆ reuse_tcp_lru_snip()

struct reuse_tcp * reuse_tcp_lru_snip | ( | struct outside_network * | outnet | ) |

Snip the last reuse_tcp element off of the LRU list.

snip the last reuse_tcp element off of the LRU list if any

References reuse_tcp::item_on_lru_list, log_assert, reuse_tcp::lru_next, reuse_tcp::outnet, and outside_network::tcp_reuse_first.

Referenced by check_snip(), and reuse_tcp_close_oldest().

## ◆ reuse_tcp_remove_tree_list()

void reuse_tcp_remove_tree_list | ( | struct outside_network * | outnet,
|  | struct reuse_tcp * | reuse )

remove reused element from tree and lru list

remove element from tree and LRU list

References reuse_tcp::addr, addr_to_str(), reuse_tcp::addrlen, reuse_tcp::is_ssl, reuse_tcp::item_on_lru_list, rbnode_type::key, log_assert, log_err(), reuse_tcp::lru_next, reuse_tcp::node, waiting_tcp::outnet, reuse_tcp::pending, rbtree_delete(), outside_network::tcp_reuse, outside_network::tcp_reuse_first, VERB_CLIENT, and verbose().

Referenced by check_removal(), check_snip(), decommission_pending_tcp(), empty_tree(), outnet_tcp_cb(), and outnet_tcp_take_into_use().

## ◆ reuse_cb_and_decommission()

static void reuse_cb_and_decommission | ( | struct outside_network * | outnet,
|  | struct pending_tcp * | pend,
|  | int | error )
| static

perform callbacks for failure and also decommission pending tcp.

the callbacks remove references in sq->pending to the waiting_tcp members of the tree_by_id in the pending tcp. The pending_tcp is removed before the callbacks, so that the callbacks do not modify the pending_tcp due to its reference in the outside_network reuse tree

References decommission_pending_tcp(), error(), mark_for_cb_and_decommission(), serviced_query::outnet, pending_tcp::query, rbtree_init(), RBTREE_NULL, pending_tcp::reuse, reuse_cb_readwait_for_failure(), reuse_del_readwait(), reuse_id_cmp(), rbtree_type::root, traverse_postorder(), reuse_tcp::tree_by_id, and reuse_tcp::write_wait_first.

Referenced by outnet_tcp_cb(), outnet_tcptimer(), reuse_tcp_close_oldest(), and serviced_delete().

## ◆ outside_network_create()

struct outside_network * outside_network_create | ( | struct comm_base * | base,
|  | size_t | bufsize,
|  | size_t | num_ports,
|  | char ** | ifs,
|  | int | num_ifs,
|  | int | do_ip4,
|  | int | do_ip6,
|  | size_t | num_tcp,
|  | int | dscp,
|  | struct infra_cache * | infra,
|  | struct ub_randstate * | rnd,
|  | int | use_caps_for_id,
|  | int * | availports,
|  | int | numavailports,
|  | size_t | unwanted_threshold,
|  | int | tcp_mss,
|  | void(* | unwanted_action )(void *),
|  | void * | unwanted_param,
|  | int | do_udp,
|  | void * | sslctx,
|  | int | delayclose,
|  | int | tls_use_sni,
|  | struct dt_env * | dtenv,
|  | int | udp_connect,
|  | int | max_reuse_tcp_queries,
|  | int | tcp_reuse_timeout,
|  | int | tcp_auth_query_timeout )

Create outside_network structure with N udp ports.

Parameters

  base | the communication base to use for event handling.
  bufsize | size for network buffers.
  num_ports | number of udp ports to open per interface.
  ifs | interface names (or NULL for default interface). These interfaces must be able to access all authoritative servers.
  num_ifs | number of names in array ifs.
  do_ip4 | service IP4.
  do_ip6 | service IP6.
  num_tcp | number of outgoing tcp buffers to preallocate.
  dscp | DSCP to use.
  infra | pointer to infra cached used for serviced queries.
  rnd | stored to create random numbers for serviced queries.
  use_caps_for_id | enable to use 0x20 bits to encode id randomness.
  availports | array of available ports.
  numavailports | number of available ports in array.
  unwanted_threshold | when to take defensive action.
  unwanted_action | the action to take.
  unwanted_param | user parameter to action.
  tcp_mss | maximum segment size of tcp socket.
  do_udp | if udp is done.
  sslctx | context to create outgoing connections with (if enabled).
  delayclose | if not 0, udp sockets are delayed before timeout closure. msec to wait on timeouted udp sockets.
  tls_use_sni | if SNI is used for TLS connections.
  dtenv | environment to send dnstap events with (if enabled).
  udp_connect | if the udp_connect option is enabled.
  max_reuse_tcp_queries | max number of queries on a reuse connection.
  tcp_reuse_timeout | timeout for REUSE entries in milliseconds.
  tcp_auth_query_timeout | timeout in milliseconds for TCP queries to auth servers.

Returns
  : the new structure (with no pending answers) or NULL on error.

References outside_network::base, calc_num46(), comm_base_timept(), port_comm::cp, create_pending_tcp(), outside_network::delay_tv, outside_network::delayclose, outside_network::do_udp, outside_network::infra, outside_network::ip4_ifs, outside_network::ip6_ifs, outside_network::ip_dscp, log_err(), outside_network::max_reuse_tcp_queries, port_comm::next, outside_network::now_secs, outside_network::now_tv, outside_network::num_ip4, outside_network::num_ip6, outside_network::num_tcp, outside_network::num_tcp_outgoing, outside_network::num_udp_outgoing, outnet_udp_cb(), outside_network_delete(), outside_network::pending, pending_cmp(), rbtree_create(), rbtree_init(), reuse_cmp(), outside_network::rnd, outside_network::serviced, serviced_cmp(), setup_if(), sldns_buffer_new(), outside_network::sslctx, str_is_ip6(), outside_network::svcd_overhead, outside_network::tcp_auth_query_timeout, outside_network::tcp_mss, outside_network::tcp_reuse, outside_network::tcp_reuse_max, outside_network::tcp_reuse_timeout, outside_network::tls_use_sni, outside_network::udp_buff, outside_network::udp_connect, outside_network::unused_fds, outside_network::unwanted_action, outside_network::unwanted_param, outside_network::unwanted_threshold, outside_network::use_caps_for_id, and outside_network::want_to_quit.

Referenced by libworker_setup(), and worker_init().

## ◆ outside_network_quit_prepare()

void outside_network_quit_prepare | ( | struct outside_network * | outnet | ) |

Prepare for quit.

Sends no more queries, even if queued up.

Parameters

  outnet | object to prepare for removal

References serviced_query::outnet, and outside_network::want_to_quit.

Referenced by libworker_delete_env(), and worker_delete().

## ◆ outside_network_delete()

void outside_network_delete | ( | struct outside_network * | outnet | ) |

Delete outside_network structure.

Parameters

  outnet | object to delete.

References port_if::avail_ports, pending_tcp::c, comm_point_delete(), port_comm::cp, decommission_pending_tcp(), port_if::inuse, outside_network::ip4_ifs, outside_network::ip6_ifs, reuse_tcp::item_on_lru_list, port_comm::next, pending::next_waiting, waiting_tcp::next_waiting, outside_network::num_ip4, outside_network::num_ip6, outside_network::num_tcp, port_if::out, pending::outnet, serviced_query::outnet, waiting_tcp::outnet, outside_network::pending, pending_delete(), pending_node_del(), rbtree_init(), pending_tcp::reuse, reuse_cmp(), outside_network::serviced, serviced_node_del(), sldns_buffer_free(), outside_network::tcp_conns, outside_network::tcp_reuse, outside_network::tcp_reuse_first, outside_network::tcp_wait_first, traverse_postorder(), outside_network::udp_buff, outside_network::udp_wait_first, outside_network::unused_fds, waiting_tcp_delete(), and outside_network::want_to_quit.

Referenced by libworker_delete_env(), outside_network_create(), and worker_delete().

## ◆ pending_delete()

void pending_delete | ( | struct outside_network * | outnet,
|  | struct pending * | p )

Delete pending answer.

Parameters

  outnet | outside network the pending query is part of. Internal feature: if outnet is NULL, p is not unlinked from rbtree.
  p | deleted

References comm_timer_delete(), rbnode_type::key, log_assert, pending::next_waiting, pending::node, pending::outnet, outside_network::pending, pending::pkt, rbtree_delete(), pending::timer, outside_network::udp_wait_first, and outside_network::udp_wait_last.

Referenced by outnet_send_wait_udp(), outnet_udp_cb(), outside_network_delete(), pending_node_del(), pending_udp_query(), pending_udp_timer_cb(), pending_udp_timer_delay_cb(), and serviced_delete().

## ◆ udp_sockport()

static int udp_sockport | ( | struct sockaddr_storage * | addr,
|  | socklen_t | addrlen,
|  | int | pfxlen,
|  | int | port,
|  | int * | inuse,
|  | struct ub_randstate * | rnd,
|  | int | dscp )
| static

Try to open a UDP socket for outgoing communication.

Sets sockets options as needed.

Parameters

  addr | socket address.
  addrlen | length of address.
  pfxlen | length of network prefix (for address randomisation).
  port | port override for addr.
  inuse | if -1 is returned, this bool means the port was in use.
  rnd | random state (for address randomisation).
  dscp | DSCP to use.

Returns
  fd or -1

References pending::addr, addr_is_ip6(), pending::addrlen, and create_udp_sock().

Referenced by select_ifport().

## ◆ pending_udp_query()

struct pending * pending_udp_query | ( | struct serviced_query * | sq,
|  | struct sldns_buffer * | packet,
|  | int | timeout,
|  | comm_point_callback_type * | callback,
|  | void * | callback_arg )

Send UDP query, create pending answer.

Changes the ID for the query to be random and unique for that destination.

Parameters

  sq | serviced query.
  packet | wireformat query to send to destination.
  timeout | in milliseconds from now.
  callback | function to call on error, timeout or reply.
  callback_arg | user argument for callback function.

Returns
  : NULL on error for malloc or socket. Else the pending query object.

References pending::addr, serviced_query::addr, pending::addrlen, serviced_query::addrlen, outside_network::base, serviced_query::busy, pending::cb, pending::cb_arg, comm_timer_create(), comm_timer_delete(), rbnode_type::key, log_assert, memdup(), pending::next_waiting, pending::node, pending::outnet, serviced_query::outnet, pending_delete(), pending_udp_timer_cb(), pending::pkt, pending::pkt_len, randomize_and_send_udp(), sldns_buffer_begin(), sldns_buffer_limit(), pending::sq, pending::timeout, pending::timer, outside_network::udp_wait_first, outside_network::udp_wait_last, outside_network::unused_fds, VERB_ALGO, and verbose().

Referenced by serviced_udp_send().

## ◆ reuse_tcp_select_id()

uint16_t reuse_tcp_select_id | ( | struct reuse_tcp * | reuse,
|  | struct outside_network * | outnet )

find spare ID value for reuse tcp stream.

Pick random ID value for a tcp stream, avoids existing IDs.

That is random and also does not collide with an existing query ID that is in use or waiting

References rbtree_type::count, GET_RANDOM_ID, log_assert, reuse_tcp::node, reuse_tcp::outnet, rbtree_first(), rbtree_last(), rbtree_next(), RBTREE_NULL, reuse_tcp_by_id_find(), outside_network::rnd, reuse_tcp::tree_by_id, tree_by_id_get_id(), and ub_random_max().

Referenced by tcp_select_id(), and tcpid_addmore().

## ◆ pending_tcp_query()

struct waiting_tcp * pending_tcp_query | ( | struct serviced_query * | sq,
|  | struct sldns_buffer * | packet,
|  | int | timeout,
|  | comm_point_callback_type * | callback,
|  | void * | callback_arg )

Send TCP query.

May wait for TCP buffer. Selects ID to be random, and checks id.

Parameters

  sq | serviced query.
  packet | wireformat query to send to destination. copied from.
  timeout | in milliseconds from now. Timer starts running now. Timer may expire if all buffers are used, without any query been sent to the server yet.
  callback | function to call on error, timeout or reply.
  callback_arg | user argument for callback function.

Returns
  : false on error for malloc or socket. Else the pending TCP object.

References port_if::addr, reuse_tcp::addr, serviced_query::addr, waiting_tcp::addr, reuse_tcp::addrlen, serviced_query::addrlen, waiting_tcp::addrlen, outside_network::base, pending_tcp::c, waiting_tcp::cb, waiting_tcp::cb_arg, comm_timer_create(), waiting_tcp::error_count, comm_point::fd, waiting_tcp::id, waiting_tcp::id_node, waiting_tcp::in_cb_and_decommission, rbnode_type::key, log_assert, log_reuse_tcp(), waiting_tcp::next_waiting, waiting_tcp::on_tcp_waiting_list, serviced_query::outnet, waiting_tcp::outnet, outnet_tcp_take_into_use(), outnet_tcp_take_query_setup(), outnet_tcptimer(), outnet_waiting_tcp_list_add(), reuse_tcp::pending, pending_tcp::pi, waiting_tcp::pkt, waiting_tcp::pkt_len, pending_tcp::query, rbtree_init(), pending_tcp::reuse, reuse_id_cmp(), reuse_tcp_close_oldest(), reuse_tcp_find(), reuse_tcp_lru_touch(), reuse_tree_by_id_insert(), reuse_write_wait_push_back(), sldns_buffer_begin(), sldns_buffer_init_frm_data(), sldns_buffer_limit(), waiting_tcp::ssl_upstream, outside_network::tcp_free, tcp_select_id(), waiting_tcp::timeout, waiting_tcp::timer, serviced_query::tls_auth_name, waiting_tcp::tls_auth_name, reuse_tcp::tree_by_id, VERB_ALGO, VERB_CLIENT, verbose(), waiting_tcp_delete(), waiting_tcp::write_wait_prev, waiting_tcp::write_wait_queued, serviced_query::zone, and serviced_query::zonelen.

Referenced by serviced_tcp_initiate(), and serviced_tcp_send().

## ◆ outnet_serviced_query()

struct serviced_query * outnet_serviced_query | ( | struct outside_network * | outnet,
|  | struct query_info * | qinfo,
|  | uint16_t | flags,
|  | int | dnssec,
|  | int | want_dnssec,
|  | int | nocaps,
|  | int | check_ratelimit,
|  | int | tcp_upstream,
|  | int | ssl_upstream,
|  | char * | tls_auth_name,
|  | struct sockaddr_storage * | addr,
|  | socklen_t | addrlen,
|  | uint8_t * | zone,
|  | size_t | zonelen,
|  | struct module_qstate * | qstate,
|  | comm_point_callback_type * | callback,
|  | void * | callback_arg,
|  | struct sldns_buffer * | buff,
|  | struct module_env * | env,
|  | int * | was_ratelimited )

Perform a serviced query to the authoritative servers.

Duplicate efforts are detected, and EDNS, TCP and UDP retry is performed.

Parameters

  outnet | outside network, with rbtree of serviced queries.
  qinfo | query info.
  flags | flags u16 (host format), includes opcode, CD bit.
  dnssec | if set, DO bit is set in EDNS queries. If the value includes BIT_CD, CD bit is set when in EDNS queries. If the value includes BIT_DO, DO bit is set when in EDNS queries.
  want_dnssec | signatures are needed, without EDNS the answer is likely to be useless.
  nocaps | ignore use_caps_for_id and use unperturbed qname.
  check_ratelimit | if set, will check ratelimit before sending out.
  tcp_upstream | use TCP for upstream queries.
  ssl_upstream | use SSL for upstream queries.
  tls_auth_name | when ssl_upstream is true, use this name to check the server's peer certificate.
  addr | to which server to send the query.
  addrlen | length of addr.
  zone | name of the zone of the delegation point. wireformat dname. This is the delegation point name for which the server is deemed authoritative.
  zonelen | length of zone.
  qstate | module qstate. Mainly for inspecting the available edns_opts_lists.
  callback | callback function.
  callback_arg | user argument to callback function.
  buff | scratch buffer to create query contents in. Empty on exit.
  env | the module environment.
  was_ratelimited | it will signal back if the query failed to pass the ratelimit check.

Returns
  0 on error, or pointer to serviced query that is used to answer this serviced query may be shared with other callbacks as well.

References module_env::alloc, alloc_reg_obtain(), alloc_reg_release(), service_callback::cb, service_callback::cb_arg, serviced_query::cblist, module_env::cfg, edns_strings::client_string_opcode, edns_strings::client_strings, edns_opt_copy_region(), edns_opt_list_append(), module_qstate::edns_opts_back_out, edns_string_addr_lookup(), module_env::edns_strings, module_env::infra_cache, infra_ratelimit_dec(), infra_ratelimit_inc(), inplace_cb_query_call(), LDNS_RR_CLASS_IN, LDNS_RR_TYPE_NS, log_nametypeclass(), lookup_serviced(), module_qstate::mesh_info, service_callback::next, serviced_query::node, module_env::now, config_file::pad_queries, config_file::pad_queries_block_size, query_info::qclass, module_qstate::qinfo, query_info::qname, query_info::qname_len, query_info::qtype, mesh_reply::query_reply, config_file::ratelimit_backoff, config_file::ratelimit_factor, rbtree_delete(), serviced_query::region, regional_alloc(), mesh_state::reply_list, module_env::rnd, outside_network::serviced, serviced_create(), serviced_gen_query(), serviced_node_del(), edns_string_addr::string, edns_string_addr::string_len, ub_random_max(), and VERB_ALGO.

Referenced by libworker_send_query(), and worker_send_query().

## ◆ outnet_serviced_query_stop()

void outnet_serviced_query_stop | ( | struct serviced_query * | sq,
|  | void * | cb_arg )

Remove service query callback.

If that leads to zero callbacks, the query is completely cancelled.

Parameters

  sq | serviced query to adjust.
  cb_arg | callback argument of callback that needs removal. same as the callback_arg to outnet_serviced_query().

References serviced_query::busy, callback_list_remove(), service_callback::cb_arg, serviced_query::cblist, serviced_query::outnet, rbtree_delete(), outside_network::serviced, serviced_delete(), and serviced_query::to_be_deleted.

Referenced by outbound_list_clear(), and outbound_list_remove().

## ◆ outnet_comm_point_for_udp()

struct comm_point * outnet_comm_point_for_udp | ( | struct outside_network * | outnet,
|  | comm_point_callback_type * | cb,
|  | void * | cb_arg,
|  | struct sockaddr_storage * | to_addr,
|  | socklen_t | to_addrlen )

Create udp commpoint suitable for sending packets to the destination.

Parameters

  outnet | outside_network with the comm_base it is attached to, with the outgoing interfaces chosen from, and rnd gen for random.
  cb | callback function for the commpoint.
  cb_arg | callback argument for cb.
  to_addr | intended destination.
  to_addrlen | length of to_addr.

Returns
  commpoint that you can comm_point_send_udp_msg with, or NULL.

References outside_network::base, comm_point::cb_arg, comm_point_create_udp(), comm_point::fd, fd_for_dest(), log_err(), and outside_network::udp_buff.

Referenced by xfr_probe_send_probe().

## ◆ outnet_comm_point_for_tcp()

struct comm_point * outnet_comm_point_for_tcp | ( | struct outside_network * | outnet,
|  | comm_point_callback_type * | cb,
|  | void * | cb_arg,
|  | struct sockaddr_storage * | to_addr,
|  | socklen_t | to_addrlen,
|  | struct sldns_buffer * | query,
|  | int | timeout,
|  | int | ssl,
|  | char * | host )

Create tcp commpoint suitable for communication to the destination.

It also performs connect() to the to_addr.

Parameters

  outnet | outside_network with the comm_base it is attached to, and the tcp_mss.
  cb | callback function for the commpoint.
  cb_arg | callback argument for cb.
  to_addr | intended destination.
  to_addrlen | length of to_addr.
  query | initial packet to send writing, in buffer. It is copied to the commpoint buffer that is created.
  timeout | timeout for the TCP connection. timeout in milliseconds, or -1 for no (change to the) timeout. So seconds*1000.
  ssl | set to true for TLS.
  host | hostname for host name verification of TLS (or NULL if no TLS).

Returns
  tcp_out commpoint, or NULL.

References outside_network::base, comm_point::buffer, comm_point::cb_arg, comm_point_create_tcp_out(), comm_point_delete(), comm_point_start_listening(), comm_point::fd, fd_set_nonblock(), outside_network::ip_dscp, log_err(), outnet_get_tcp_fd(), outnet_tcp_connect(), comm_reply::remote_addr, comm_reply::remote_addrlen, comm_point::repinfo, setup_comm_ssl(), sldns_buffer_copy(), comm_point::ssl, outside_network::tcp_mss, and comm_point::timeout.

Referenced by xfr_transfer_init_fetch().

## ◆ outnet_comm_point_for_http()

struct comm_point * outnet_comm_point_for_http | ( | struct outside_network * | outnet,
|  | comm_point_callback_type * | cb,
|  | void * | cb_arg,
|  | struct sockaddr_storage * | to_addr,
|  | socklen_t | to_addrlen,
|  | int | timeout,
|  | int | ssl,
|  | char * | host,
|  | char * | path,
|  | struct config_file * | cfg )

Create http commpoint suitable for communication to the destination.

Creates the http request buffer. It also performs connect() to the to_addr.

Parameters

  outnet | outside_network with the comm_base it is attached to, and the tcp_mss.
  cb | callback function for the commpoint.
  cb_arg | callback argument for cb.
  to_addr | intended destination.
  to_addrlen | length of to_addr.
  timeout | timeout for the TCP connection. timeout in milliseconds, or -1 for no (change to the) timeout. So seconds*1000.
  ssl | set to true for https.
  host | hostname to use for the destination. part of http request.
  path | pathname to lookup, eg. name of the file on the destination.
  cfg | running configuration for User-Agent setup.

Returns
  http_out commpoint, or NULL.

References outside_network::base, comm_point::buffer, comm_point::cb_arg, comm_point_create_http_out(), comm_point_delete(), comm_point_start_listening(), comm_point::fd, fd_set_nonblock(), outside_network::ip_dscp, log_err(), outnet_get_tcp_fd(), outnet_tcp_connect(), comm_reply::remote_addr, comm_reply::remote_addrlen, comm_point::repinfo, setup_comm_ssl(), setup_http_request(), comm_point::ssl, outside_network::tcp_mss, comm_point::timeout, and outside_network::udp_buff.

Referenced by xfr_transfer_init_fetch().

## ◆ outnet_get_mem()

size_t outnet_get_mem | ( | struct outside_network * | outnet | ) |

Get memory size in use by outside network.

Counts buffers and outstanding query (serviced queries) malloced data.

Parameters

  outnet | outside network structure.

Returns
  size in bytes.

References outside_network::base, pending_tcp::c, serviced_query::cblist, rbtree_type::count, port_comm::cp, if_get_mem(), outside_network::ip4_ifs, outside_network::ip6_ifs, port_comm::next, service_callback::next, pending::next_waiting, waiting_tcp::next_waiting, outside_network::num_ip4, outside_network::num_ip6, outside_network::num_tcp, pending::outnet, outside_network::pending, serviced_query::qbuflen, pending_tcp::query, RBTREE_FOR, outside_network::serviced, sldns_buffer_capacity(), pending::sq, outside_network::svcd_overhead, outside_network::tcp_conns, outside_network::tcp_wait_first, outside_network::udp_buff, outside_network::udp_wait_first, outside_network::unused_fds, waiting_tcp_get_mem(), and waiting_udp_get_mem().

Referenced by worker_mem_report().

## ◆ serviced_get_mem()

size_t serviced_get_mem | ( | struct serviced_query * | sq | ) |

Get memory size in use by serviced query while it is servicing callbacks.

This takes into account the pre-deleted status of it; it will be deleted when the callbacks are done.

Parameters

  sq | serviced query.

Returns
  size in bytes.

References serviced_query::cblist, service_callback::next, serviced_query::qbuflen, and serviced_query::status.

Referenced by worker_mem_report().