unbound
0.1
|
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 <ldns/wire2host.h>
#include "services/outside_network.h"
#include "services/listen_dnsport.h"
#include "services/cache/infra.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 <openssl/ssl.h>
#include <fcntl.h>
Defines | |
#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 outside_network *outnet, struct serviced_query *sq, ldns_buffer *buff) |
initiate TCP transaction for serviced query | |
static int | randomize_and_send_udp (struct outside_network *outnet, struct pending *pend, ldns_buffer *packet, int timeout) |
with a fd available, randomize and send UDP | |
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 void | waiting_tcp_delete (struct waiting_tcp *w) |
delete waiting_tcp entry. | |
static int | pick_outgoing_tcp (struct waiting_tcp *w, int s) |
Pick random outgoing-interface of that family, and bind it. | |
static int | outnet_tcp_take_into_use (struct waiting_tcp *w, uint8_t *pkt, size_t pkt_len) |
use next free buffer to service a tcp query | |
static void | use_free_buffer (struct outside_network *outnet) |
see if buffers can be used to service TCP queries | |
static void | decomission_pending_tcp (struct outside_network *outnet, struct pending_tcp *pend) |
decomission a tcp buffer, closes commpoint and frees waiting_tcp entry | |
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_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, struct infra_cache *infra, struct ub_randstate *rnd, int use_caps_for_id, int *availports, int numavailports, size_t unwanted_threshold, void(*unwanted_action)(void *), void *unwanted_param, int do_udp, void *sslctx) |
Create outside_network structure with N udp ports. | |
static void | pending_node_del (rbnode_t *node, void *arg) |
helper pending delete | |
static void | serviced_node_del (rbnode_t *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 int | udp_sockport (struct sockaddr_storage *addr, socklen_t addrlen, int port, int *inuse) |
Try to open a UDP socket for outgoing communication. | |
static int | select_id (struct outside_network *outnet, struct pending *pend, ldns_buffer *packet) |
Select random ID. | |
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 outside_network *outnet, ldns_buffer *packet, struct sockaddr_storage *addr, socklen_t addrlen, int timeout, comm_point_callback_t *cb, void *cb_arg) |
Send UDP query, create pending answer. | |
void | outnet_tcptimer (void *arg) |
callback for outgoing TCP timer event | |
struct waiting_tcp * | pending_tcp_query (struct outside_network *outnet, ldns_buffer *packet, struct sockaddr_storage *addr, socklen_t addrlen, int timeout, comm_point_callback_t *callback, void *callback_arg, int ssl_upstream) |
Send TCP query. | |
static void | serviced_gen_query (ldns_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, ldns_buffer *buff, int dnssec, struct sockaddr_storage *addr, socklen_t addrlen) |
lookup serviced query in serviced query rbtree | |
static struct serviced_query * | serviced_create (struct outside_network *outnet, ldns_buffer *buff, int dnssec, int want_dnssec, int tcp_upstream, int ssl_upstream, struct sockaddr_storage *addr, socklen_t addrlen, uint8_t *zone, size_t zonelen) |
Create new serviced entry. | |
static void | waiting_list_remove (struct outside_network *outnet, struct waiting_tcp *w) |
remove waiting tcp from the outnet waiting list | |
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 void | serviced_encode (struct serviced_query *sq, ldns_buffer *buff, int with_edns) |
put serviced query into a buffer | |
static int | serviced_udp_send (struct serviced_query *sq, ldns_buffer *buff) |
Perform serviced query UDP sending operation. | |
static int | serviced_check_qname (ldns_buffer *pkt, uint8_t *qbuf, size_t qbuflen) |
check that perturbed qname is identical | |
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 | 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 | serviced_tcp_send (struct serviced_query *sq, ldns_buffer *buff) |
Send serviced query over TCP return false on initial failure. | |
int | serviced_udp_callback (struct comm_point *c, void *arg, int error, struct comm_reply *rep) |
callback for serviced query UDP answers | |
static struct service_callback * | callback_list_find (struct serviced_query *sq, void *cb_arg, int(*arg_compare)(void *, void *)) |
find callback in list | |
struct serviced_query * | outnet_serviced_query (struct outside_network *outnet, uint8_t *qname, size_t qnamelen, uint16_t qtype, uint16_t qclass, uint16_t flags, int dnssec, int want_dnssec, int tcp_upstream, int ssl_upstream, struct sockaddr_storage *addr, socklen_t addrlen, uint8_t *zone, size_t zonelen, comm_point_callback_t *callback, void *callback_arg, ldns_buffer *buff, int(*arg_compare)(void *, void *)) |
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 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. |
This file has functions to send queries to authoritative servers and wait for the pending answer events.
#define MAX_ID_RETRY 1000 |
number of times to retry making a random ID that is unique.
Referenced by select_id().
#define MAX_PORT_RETRY 10000 |
number of times to retry finding interface, port that can be opened.
Referenced by select_ifport().
static void waiting_tcp_delete | ( | struct waiting_tcp * | w | ) | [static] |
delete waiting_tcp entry.
Does not unlink from waiting list.
w,: | to delete. |
References comm_timer_delete(), and waiting_tcp::timer.
Referenced by decomission_pending_tcp(), outnet_tcptimer(), outside_network_delete(), pending_tcp_query(), serviced_delete(), and use_free_buffer().
static int pick_outgoing_tcp | ( | 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.
w,: | tcp structure with destination address. |
s,: | socket fd. |
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, outside_network::rnd, ub_random_max(), VERB_ALGO, and VERB_OPS.
Referenced by outnet_tcp_take_into_use().
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, | ||
struct infra_cache * | infra, | ||
struct ub_randstate * | rnd, | ||
int | use_caps_for_id, | ||
int * | availports, | ||
int | numavailports, | ||
size_t | unwanted_threshold, | ||
void(*)(void *) | unwanted_action, | ||
void * | unwanted_param, | ||
int | do_udp, | ||
void * | sslctx | ||
) | [read] |
Create outside_network structure with N udp ports.
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. |
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. |
do_udp,: | if udp is done. |
sslctx,: | context to create outgoing connections with (if enabled). |
References outside_network::base, calc_num46(), comm_base_timept(), comm_point_create_udp(), port_comm::cp, create_pending_tcp(), outside_network::do_udp, outside_network::infra, outside_network::ip4_ifs, outside_network::ip6_ifs, log_err(), port_comm::next, outside_network::now_secs, outside_network::now_tv, outside_network::num_ip4, outside_network::num_ip6, outside_network::num_tcp, outnet_udp_cb(), outside_network_delete(), outside_network::pending, pending_cmp(), rbtree_create(), outside_network::rnd, outside_network::serviced, serviced_cmp(), setup_if(), outside_network::sslctx, str_is_ip6(), outside_network::svcd_overhead, outside_network::udp_buff, 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().
void outside_network_quit_prepare | ( | struct outside_network * | outnet | ) |
Prepare for quit.
Sends no more queries, even if queued up.
outnet,: | object to prepare for removal |
References outside_network::want_to_quit.
Referenced by libworker_delete(), and worker_delete().
void outside_network_delete | ( | struct outside_network * | outnet | ) |
Delete outside_network structure.
outnet,: | object to delete. |
Referenced by libworker_delete(), outside_network_create(), and worker_delete().
void pending_delete | ( | struct outside_network * | outnet, |
struct pending * | p | ||
) |
Delete pending answer.
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_t::key, log_assert, pending::next_waiting, pending::node, 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(), and serviced_delete().
static int udp_sockport | ( | struct sockaddr_storage * | addr, |
socklen_t | addrlen, | ||
int | port, | ||
int * | inuse | ||
) | [static] |
Try to open a UDP socket for outgoing communication.
Sets sockets options as needed.
addr,: | socket address. |
addrlen,: | length of address. |
port,: | port override for addr. |
inuse,: | if -1 is returned, this bool means the port was in use. |
References addr_is_ip6(), and create_udp_sock().
Referenced by select_ifport().
struct pending* pending_udp_query | ( | struct outside_network * | outnet, |
ldns_buffer * | packet, | ||
struct sockaddr_storage * | addr, | ||
socklen_t | addrlen, | ||
int | timeout, | ||
comm_point_callback_t * | callback, | ||
void * | callback_arg | ||
) | [read] |
Send UDP query, create pending answer.
Changes the ID for the query to be random and unique for that destination.
outnet,: | provides the event handling |
packet,: | wireformat query to send to destination. |
addr,: | address to send to. |
addrlen,: | length of addr. |
timeout,: | in milliseconds from now. |
callback,: | function to call on error, timeout or reply. |
callback_arg,: | user argument for callback function. |
Referenced by serviced_udp_send().
struct waiting_tcp* pending_tcp_query | ( | struct outside_network * | outnet, |
ldns_buffer * | packet, | ||
struct sockaddr_storage * | addr, | ||
socklen_t | addrlen, | ||
int | timeout, | ||
comm_point_callback_t * | callback, | ||
void * | callback_arg, | ||
int | ssl_upstream | ||
) | [read] |
Send TCP query.
May wait for TCP buffer. Selects ID to be random, and checks id.
outnet,: | provides the event handling. |
packet,: | wireformat query to send to destination. copied from. |
addr,: | address to send to. |
addrlen,: | length of addr. |
timeout,: | in seconds 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. |
ssl_upstream,: | if the tcp connection must use SSL. |
References waiting_tcp::addr, waiting_tcp::addrlen, outside_network::base, waiting_tcp::cb, waiting_tcp::cb_arg, comm_timer_create(), comm_timer_set(), waiting_tcp::next_waiting, waiting_tcp::outnet, outnet_tcp_take_into_use(), outnet_tcptimer(), waiting_tcp::pkt, waiting_tcp::pkt_len, outside_network::rnd, waiting_tcp::ssl_upstream, outside_network::tcp_free, outside_network::tcp_wait_first, outside_network::tcp_wait_last, waiting_tcp::timer, ub_random(), and waiting_tcp_delete().
Referenced by serviced_tcp_initiate(), and serviced_tcp_send().
static int serviced_udp_send | ( | struct serviced_query * | sq, |
ldns_buffer * | buff | ||
) | [static] |
Perform serviced query UDP sending operation.
Sends UDP with EDNS, unless infra host marked non EDNS.
sq,: | query to send. |
buff,: | buffer scratch space. |
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 outnet_serviced_query(), and serviced_udp_callback().
struct serviced_query* outnet_serviced_query | ( | struct outside_network * | outnet, |
uint8_t * | qname, | ||
size_t | qnamelen, | ||
uint16_t | qtype, | ||
uint16_t | qclass, | ||
uint16_t | flags, | ||
int | dnssec, | ||
int | want_dnssec, | ||
int | tcp_upstream, | ||
int | ssl_upstream, | ||
struct sockaddr_storage * | addr, | ||
socklen_t | addrlen, | ||
uint8_t * | zone, | ||
size_t | zonelen, | ||
comm_point_callback_t * | callback, | ||
void * | callback_arg, | ||
ldns_buffer * | buff, | ||
int(*)(void *, void *) | arg_compare | ||
) | [read] |
Perform a serviced query to the authoritative servers.
Duplicate efforts are detected, and EDNS, TCP and UDP retry is performed.
outnet,: | outside network, with rbtree of serviced queries. |
qname,: | what qname to query. |
qnamelen,: | length of qname in octets including 0 root label. |
qtype,: | rrset type to query (host format) |
qclass,: | query class. (host format) |
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. |
tcp_upstream,: | use TCP for upstream queries. |
ssl_upstream,: | use SSL for upstream queries. |
callback,: | callback function. |
callback_arg,: | user argument to callback function. |
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. |
buff,: | scratch buffer to create query contents in. Empty on exit. |
arg_compare,: | function to compare callback args, return true if identical. It is given the callback_arg and args that are listed. |
References callback_list_find(), service_callback::cb, service_callback::cb_arg, serviced_query::cblist, outside_network::do_udp, lookup_serviced(), service_callback::next, serviced_query::qbuf, rbtree_delete(), outside_network::serviced, serviced_create(), serviced_gen_query(), serviced_tcp_send(), serviced_udp_send(), and serviced_query::zone.
Referenced by libworker_send_query(), and worker_send_query().
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.
sq,: | serviced query to adjust. |
cb_arg,: | callback argument of callback that needs removal. same as the callback_arg to outnet_serviced_query(). |
Referenced by outbound_list_clear(), and outbound_list_remove().
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.
outnet,: | outside network structure. |
References outside_network::base, pending_tcp::c, serviced_query::cblist, rbtree_t::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, outside_network::pending, serviced_query::qbuflen, pending_tcp::query, RBTREE_FOR, outside_network::serviced, 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().
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.
sq,: | serviced query. |
References serviced_query::cblist, comm_timer_get_mem(), service_callback::next, serviced_query::qbuflen, and serviced_query::status.
Referenced by worker_mem_report().