Macros | Functions | Variables
net_help.h File Reference

This file contains functions to perform network related tasks. More...

#include "util/log.h"

Macros

#define BIT_CD   0x0010
 DNS constants for uint16_t style flag manipulation. More...
 
#define BIT_AD   0x0020
 AD flag.
 
#define BIT_Z   0x0040
 Z flag.
 
#define BIT_RA   0x0080
 RA flag.
 
#define BIT_RD   0x0100
 RD flag.
 
#define BIT_TC   0x0200
 TC flag.
 
#define BIT_AA   0x0400
 AA flag.
 
#define BIT_QR   0x8000
 QR flag.
 
#define FLAGS_GET_RCODE(f)   ((f) & 0xf)
 get RCODE bits from uint16 flags
 
#define FLAGS_SET_RCODE(f, r)   (f = (((f) & 0xfff0) | (r)))
 set RCODE bits in uint16 flags
 
#define UDP_AUTH_QUERY_TIMEOUT   4
 timeout in seconds for UDP queries to auth servers. More...
 
#define TCP_AUTH_QUERY_TIMEOUT   30
 timeout in seconds for TCP queries to auth servers. More...
 
#define EDNS_ADVERTISED_VERSION   0
 Advertised version of EDNS capabilities.
 
#define EDNS_DO   0x8000 /* Dnssec Ok */
 bits for EDNS bitfield
 
#define INET_SIZE   4
 byte size of ip4 address
 
#define INET6_SIZE   16
 byte size of ip6 address
 
#define DNSKEY_BIT_ZSK   0x0100
 DNSKEY zone sign key flag.
 
#define DNSKEY_BIT_SEP   0x0001
 DNSKEY secure entry point, KSK flag.
 

Functions

int str_is_ip6 (const char *str)
 See if string is ip4 or ip6. More...
 
int fd_set_nonblock (int s)
 Set fd nonblocking. More...
 
int fd_set_block (int s)
 Set fd (back to) blocking. More...
 
int is_pow2 (size_t num)
 See if number is a power of 2. More...
 
void * memdup (void *data, size_t len)
 Allocate memory and copy over contents. More...
 
void log_addr (enum verbosity_value v, const char *str, struct sockaddr_storage *addr, socklen_t addrlen)
 Prints the sockaddr in readable format with log_info. More...
 
void log_name_addr (enum verbosity_value v, const char *str, uint8_t *zone, struct sockaddr_storage *addr, socklen_t addrlen)
 Prints zone name and sockaddr in readable format with log_info. More...
 
void log_err_addr (const char *str, const char *err, struct sockaddr_storage *addr, socklen_t addrlen)
 Log errno and addr. More...
 
int extstrtoaddr (const char *str, struct sockaddr_storage *addr, socklen_t *addrlen)
 Convert address string, with "@port" appendix, to sockaddr. More...
 
int ipstrtoaddr (const char *ip, int port, struct sockaddr_storage *addr, socklen_t *addrlen)
 Convert ip address string and port to sockaddr. More...
 
int netblockstrtoaddr (const char *ip, int port, struct sockaddr_storage *addr, socklen_t *addrlen, int *net)
 Convert ip netblock (ip/netsize) string and port to sockaddr. More...
 
void log_nametypeclass (enum verbosity_value v, const char *str, uint8_t *name, uint16_t type, uint16_t dclass)
 Print string with neat domain name, type and class. More...
 
int sockaddr_cmp (struct sockaddr_storage *addr1, socklen_t len1, struct sockaddr_storage *addr2, socklen_t len2)
 Compare two sockaddrs. More...
 
int sockaddr_cmp_addr (struct sockaddr_storage *addr1, socklen_t len1, struct sockaddr_storage *addr2, socklen_t len2)
 Compare two sockaddrs. More...
 
int addr_is_ip6 (struct sockaddr_storage *addr, socklen_t len)
 Checkout address family. More...
 
void addr_mask (struct sockaddr_storage *addr, socklen_t len, int net)
 Make sure the sockaddr ends in zeroes. More...
 
int addr_in_common (struct sockaddr_storage *addr1, int net1, struct sockaddr_storage *addr2, int net2, socklen_t addrlen)
 See how many bits are shared, equal, between two addrs. More...
 
void addr_to_str (struct sockaddr_storage *addr, socklen_t addrlen, char *buf, size_t len)
 Put address into string, works for IPv4 and IPv6. More...
 
int addr_is_ip4mapped (struct sockaddr_storage *addr, socklen_t addrlen)
 See if sockaddr is an ipv6 mapped ipv4 address, "::ffff:0.0.0.0". More...
 
int addr_is_broadcast (struct sockaddr_storage *addr, socklen_t addrlen)
 See if sockaddr is 255.255.255.255. More...
 
int addr_is_any (struct sockaddr_storage *addr, socklen_t addrlen)
 See if sockaddr is 0.0.0.0 or ::0. More...
 
void sock_list_insert (struct sock_list **list, struct sockaddr_storage *addr, socklen_t len, struct regional *region)
 Insert new socket list item. More...
 
void sock_list_prepend (struct sock_list **list, struct sock_list *add)
 Append one list to another. More...
 
int sock_list_find (struct sock_list *list, struct sockaddr_storage *addr, socklen_t len)
 Find addr in list. More...
 
void sock_list_merge (struct sock_list **list, struct regional *region, struct sock_list *add)
 Merge socklist into another socket list. More...
 
void log_crypto_err (const char *str)
 Log libcrypto error with descriptive string. More...
 
int listen_sslctx_setup (void *ctxt)
 Set SSL_OP_NOxxx options on SSL context to disable bad crypto. More...
 
void listen_sslctx_setup_2 (void *ctxt)
 Further setup of listening SSL context, after keys loaded. More...
 
void * listen_sslctx_create (char *key, char *pem, char *verifypem)
 create SSL listen context More...
 
void * connect_sslctx_create (char *key, char *pem, char *verifypem)
 create SSL connect context More...
 
void * incoming_ssl_fd (void *sslctx, int fd)
 accept a new fd and wrap it in a BIO in SSL More...
 
void * outgoing_ssl_fd (void *sslctx, int fd)
 connect a new fd and wrap it in a BIO in SSL More...
 
int ub_openssl_lock_init (void)
 Initialize openssl locking for thread safety. More...
 
void ub_openssl_lock_delete (void)
 De-init the allocated openssl locks.
 

Variables

uint16_t EDNS_ADVERTISED_SIZE
 Advertised size of EDNS capabilities. More...
 
int MINIMAL_RESPONSES
 minimal responses when positive answer More...
 
int RRSET_ROUNDROBIN
 rrset order roundrobin More...
 

Detailed Description

This file contains functions to perform network related tasks.

Macro Definition Documentation

◆ BIT_CD

#define BIT_CD   0x0010

DNS constants for uint16_t style flag manipulation.

host byteorder. 1 1 1 1 1 1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 +–+–+–+–+–+–+–+–+–+–+–+–+–+–+–+–+ |QR| Opcode |AA|TC|RD|RA| Z|AD|CD| RCODE | +–+–+–+–+–+–+–+–+–+–+–+–+–+–+–+–+CD flag

Referenced by answer_from_cache(), answer_norec_from_cache(), causes_cycle(), dns_cache_store(), do_cache_remove(), error_encode(), generate_a_aaaa_check(), generate_ns_check(), generate_sub_request(), handle_event_moddone(), mesh_log_list(), mesh_new_callback(), mesh_new_client(), mesh_send_reply(), mesh_state_compare(), mesh_state_create(), pr_flags(), processCollectClass(), processInitRequest3(), query_info_hash(), reply_info_answer_encode(), and serviced_encode().

◆ UDP_AUTH_QUERY_TIMEOUT

#define UDP_AUTH_QUERY_TIMEOUT   4

timeout in seconds for UDP queries to auth servers.

◆ TCP_AUTH_QUERY_TIMEOUT

#define TCP_AUTH_QUERY_TIMEOUT   30

timeout in seconds for TCP queries to auth servers.

Referenced by serviced_tcp_initiate(), and serviced_tcp_send().

Function Documentation

◆ str_is_ip6()

int str_is_ip6 ( const char *  str)

See if string is ip4 or ip6.

Parameters
strIP specification.
Returns
: true if string addr is an ip6 specced address.

Referenced by calc_num46(), ipstrtoaddr(), listening_ports_open(), net_test(), netblockstrtoaddr(), outside_network_create(), and ub_ctx_hosts().

◆ fd_set_nonblock()

int fd_set_nonblock ( int  s)

◆ fd_set_block()

int fd_set_block ( int  s)

Set fd (back to) blocking.

Parameters
sfile descriptor.
Returns
: 0 on error (error is printed to log).

References log_err(), and verbosity.

Referenced by comm_point_send_udp_msg(), comm_point_send_udp_msg_if(), handle_req(), tube_read_msg(), and tube_write_msg().

◆ is_pow2()

int is_pow2 ( size_t  num)

See if number is a power of 2.

Parameters
numthe value.
Returns
: true if the number is a power of 2.

Referenced by net_test().

◆ memdup()

void* memdup ( void *  data,
size_t  len 
)

◆ log_addr()

void log_addr ( enum verbosity_value  v,
const char *  str,
struct sockaddr_storage *  addr,
socklen_t  addrlen 
)

Prints the sockaddr in readable format with log_info.

Debug helper.

Parameters
vat what verbosity level to print this.
strdescriptive string printed with it.
addrthe sockaddr to print. Can be ip4 or ip6.
addrlenlength of addr.

References verbose(), and verbosity.

Referenced by comm_point_send_udp_msg(), comm_point_send_udp_msg_if(), delegpt_log(), do_proxy(), iter_filter_unsuitable(), iter_merge_retry_counts(), pending_find_match(), pick_outgoing_tcp(), service_tcp_relay(), serviced_tcp_callback(), sock_list_logentry(), tcp_callback_reader(), tcp_proxy_delete(), and worker_handle_request().

◆ log_name_addr()

void log_name_addr ( enum verbosity_value  v,
const char *  str,
uint8_t *  zone,
struct sockaddr_storage *  addr,
socklen_t  addrlen 
)

Prints zone name and sockaddr in readable format with log_info.

Debug.

Parameters
vat what verbosity level to print this.
strdescriptive string printed with it.
zoneDNS domain name, uncompressed wireformat.
addrthe sockaddr to print. Can be ip4 or ip6.
addrlenlength of addr.

References dname_str(), LDNS_MAX_DOMAINLEN, verbose(), and verbosity.

Referenced by remove_rr(), and serviced_udp_callback().

◆ log_err_addr()

void log_err_addr ( const char *  str,
const char *  err,
struct sockaddr_storage *  addr,
socklen_t  addrlen 
)

Log errno and addr.

Parameters
strdescriptive string printed with it.
errerrno string to print, i.e. strerror(errno).
addrthe sockaddr to print. Can be ip4 or ip6.
addrlenlength of addr.

References log_err(), and verbosity.

Referenced by comm_point_perform_accept(), comm_point_tcp_handle_read(), comm_point_tcp_handle_write(), create_tcp_accept_sock(), create_udp_sock(), outnet_tcp_take_into_use(), and verbose_print_addr().

◆ extstrtoaddr()

int extstrtoaddr ( const char *  str,
struct sockaddr_storage *  addr,
socklen_t *  addrlen 
)

Convert address string, with "@port" appendix, to sockaddr.

Uses DNS port by default.

Parameters
strthe string
addrwhere to store sockaddr.
addrlenlength of stored sockaddr is returned.
Returns
0 on error.

References ipstrtoaddr(), and MAX_ADDR_STRLEN.

Referenced by ah(), contact_server(), interfacechecks(), open_svr(), parse_delegpt(), read_fwds_addr(), read_stubs_addr(), replay_moment_read(), replay_range_read(), service(), ub_ctx_set_fwd(), ub_ctx_set_stub(), and warn_hosts().

◆ ipstrtoaddr()

int ipstrtoaddr ( const char *  ip,
int  port,
struct sockaddr_storage *  addr,
socklen_t *  addrlen 
)

Convert ip address string and port to sockaddr.

Parameters
ipip4 or ip6 address string.
portport number, host format.
addrwhere to store sockaddr.
addrlenlength of stored sockaddr is returned.
Returns
0 on error.

References MAX_ADDR_STRLEN, and str_is_ip6().

Referenced by cfg_ptr_reverse(), do_flush_infra(), extstrtoaddr(), infra_test(), net_test(), netblockstrtoaddr(), and setup_if().

◆ netblockstrtoaddr()

int netblockstrtoaddr ( const char *  ip,
int  port,
struct sockaddr_storage *  addr,
socklen_t *  addrlen,
int *  net 
)

Convert ip netblock (ip/netsize) string and port to sockaddr.

SLOW, does a malloc internally to avoid writing over 'ip' string.

Parameters
ipip4 or ip6 address string.
portport number, host format.
addrwhere to store sockaddr.
addrlenlength of stored sockaddr is returned.
netnetblock size is returned.
Returns
0 on error.

References addr_mask(), ipstrtoaddr(), log_err(), and str_is_ip6().

Referenced by acl_find_or_create(), acl_list_str_cfg(), aclchecks(), dns64_apply_cfg(), donotq_str_cfg(), lz_enter_override(), read_addrs(), respip_find_or_create(), setup_if(), verify_respip_set_actions(), and verify_rrset().

◆ log_nametypeclass()

void log_nametypeclass ( enum verbosity_value  v,
const char *  str,
uint8_t *  name,
uint16_t  type,
uint16_t  dclass 
)

◆ sockaddr_cmp()

int sockaddr_cmp ( struct sockaddr_storage *  addr1,
socklen_t  len1,
struct sockaddr_storage *  addr2,
socklen_t  len2 
)

Compare two sockaddrs.

Imposes an ordering on the addresses. Compares address and port.

Parameters
addr1address 1.
len1lengths of addr1.
addr2address 2.
len2lengths of addr2.
Returns
: 0 if addr1 == addr2. -1 if addr1 is smaller, +1 if larger.

References INET6_SIZE, INET_SIZE, and log_assert.

Referenced by find_create_proxy(), infra_compfunc(), infra_del_host(), pending_cmp(), pending_find_match(), pending_matches_current(), and serviced_cmp().

◆ sockaddr_cmp_addr()

int sockaddr_cmp_addr ( struct sockaddr_storage *  addr1,
socklen_t  len1,
struct sockaddr_storage *  addr2,
socklen_t  len2 
)

Compare two sockaddrs.

Compares address, not the port.

Parameters
addr1address 1.
len1lengths of addr1.
addr2address 2.
len2lengths of addr2.
Returns
: 0 if addr1 == addr2. -1 if addr1 is smaller, +1 if larger.

References INET6_SIZE, INET_SIZE, and log_assert.

Referenced by addr_tree_compare(), delegpt_find_addr(), net_test(), and sock_list_find().

◆ addr_is_ip6()

int addr_is_ip6 ( struct sockaddr_storage *  addr,
socklen_t  len 
)

◆ addr_mask()

void addr_mask ( struct sockaddr_storage *  addr,
socklen_t  len,
int  net 
)

Make sure the sockaddr ends in zeroes.

For tree insertion and subsequent comparison.

Parameters
addrthe ip4 or ip6 addr.
lenlength of addr.
netnumber of bits to leave untouched, the rest of the netblock address is zeroed.

References addr_is_ip6().

Referenced by net_test(), and netblockstrtoaddr().

◆ addr_in_common()

int addr_in_common ( struct sockaddr_storage *  addr1,
int  net1,
struct sockaddr_storage *  addr2,
int  net2,
socklen_t  addrlen 
)

See how many bits are shared, equal, between two addrs.

Parameters
addr1first addr.
net1netblock size of first addr.
addr2second addr.
net2netblock size of second addr.
addrlenlength of first addr and of second addr. They must be of the same length (i.e. same type IP4, IP6).
Returns
: number of bits the same.

References addr_is_ip6(), log_assert, and match().

Referenced by addr_tree_init_parents(), addr_tree_lookup(), handle_ipv6_ptr(), and net_test().

◆ addr_to_str()

void addr_to_str ( struct sockaddr_storage *  addr,
socklen_t  addrlen,
char *  buf,
size_t  len 
)

Put address into string, works for IPv4 and IPv6.

Parameters
addraddress
addrlenlength of address
bufresult string stored here
lenlength of buf. On failure a string with "error" is stored inside.

References addr_is_ip6().

Referenced by dump_infra_host(), errinf_origin(), get_mesh_status(), infra_ip_ratelimit_inc(), log_reply_info(), lz_inform_print(), print_dp_details(), respip_inform_print(), and ssl_print_name_dp().

◆ addr_is_ip4mapped()

int addr_is_ip4mapped ( struct sockaddr_storage *  addr,
socklen_t  addrlen 
)

See if sockaddr is an ipv6 mapped ipv4 address, "::ffff:0.0.0.0".

Parameters
addraddress
addrlenlength of address
Returns
true if so

References addr_is_ip6().

Referenced by net_test(), and udp_send_errno_needs_log().

◆ addr_is_broadcast()

int addr_is_broadcast ( struct sockaddr_storage *  addr,
socklen_t  addrlen 
)

See if sockaddr is 255.255.255.255.

Parameters
addraddress
addrlenlength of address
Returns
true if so

Referenced by udp_send_errno_needs_log().

◆ addr_is_any()

int addr_is_any ( struct sockaddr_storage *  addr,
socklen_t  addrlen 
)

See if sockaddr is 0.0.0.0 or ::0.

Parameters
addraddress
addrlenlength of address
Returns
true if so

Referenced by net_test(), and pick_outgoing_tcp().

◆ sock_list_insert()

void sock_list_insert ( struct sock_list **  list,
struct sockaddr_storage *  addr,
socklen_t  len,
struct regional region 
)

Insert new socket list item.

If fails logs error.

Parameters
listpointer to pointer to first item.
addraddress or NULL if 'cache'.
lenlength of addr, or 0 if 'cache'.
regionwhere to allocate

References sock_list::addr, sock_list::len, log_assert, log_err(), sock_list::next, and regional_alloc().

Referenced by generate_parentside_target_query(), processInitRequest3(), sock_list_merge(), and val_blacklist().

◆ sock_list_prepend()

void sock_list_prepend ( struct sock_list **  list,
struct sock_list add 
)

Append one list to another.

Must both be from same qstate(regional).

Parameters
listpointer to result list that is modified.
additem(s) to add. They are prepended to list.

References sock_list::next.

Referenced by val_blacklist().

◆ sock_list_find()

int sock_list_find ( struct sock_list list,
struct sockaddr_storage *  addr,
socklen_t  len 
)

Find addr in list.

Parameters
listto search in
addraddress to look for.
lenlength. Can be 0, look for 'cache entry'.
Returns
true if found.

References sock_list::addr, sock_list::len, sock_list::next, and sockaddr_cmp_addr().

Referenced by iter_fill_rtt(), and sock_list_merge().

◆ sock_list_merge()

void sock_list_merge ( struct sock_list **  list,
struct regional region,
struct sock_list add 
)

Merge socklist into another socket list.

Allocates the new entries freshly and copies them over, so also performs a region switchover. Allocation failures are logged.

Parameters
listthe destination list (checked for duplicates)
regionwhere to allocate
addthe list of entries to add.

References sock_list::addr, sock_list::len, sock_list::next, sock_list_find(), and sock_list_insert().

Referenced by val_blacklist().

◆ log_crypto_err()

void log_crypto_err ( const char *  str)

◆ listen_sslctx_setup()

int listen_sslctx_setup ( void *  ctxt)

Set SSL_OP_NOxxx options on SSL context to disable bad crypto.

Parameters
ctxtSSL_CTX*
Returns
false on failure.

References log_crypto_err().

Referenced by daemon_remote_create(), and listen_sslctx_create().

◆ listen_sslctx_setup_2()

void listen_sslctx_setup_2 ( void *  ctxt)

Further setup of listening SSL context, after keys loaded.

Parameters
ctxtSSL_CTX*

References log_crypto_err().

Referenced by listen_sslctx_create().

◆ listen_sslctx_create()

void* listen_sslctx_create ( char *  key,
char *  pem,
char *  verifypem 
)

create SSL listen context

Parameters
keyprivate key file.
pempublic key cert.
verifypemif nonNULL, verifylocation file. return SSL_CTX* or NULL on failure (logged).

References listen_sslctx_setup(), listen_sslctx_setup_2(), log_crypto_err(), and log_err().

◆ connect_sslctx_create()

void* connect_sslctx_create ( char *  key,
char *  pem,
char *  verifypem 
)

create SSL connect context

Parameters
keyif nonNULL (also pem nonNULL), the client private key.
pemclient public key (or NULL if key is NULL).
verifypemif nonNULL used for verifylocation file.
Returns
SSL_CTX* or NULL on failure (logged).

References log_crypto_err(), and log_err().

Referenced by libworker_setup().

◆ incoming_ssl_fd()

void* incoming_ssl_fd ( void *  sslctx,
int  fd 
)

accept a new fd and wrap it in a BIO in SSL

Parameters
sslctxthe SSL_CTX to use (from listen_sslctx_create()).
fdfrom accept, nonblocking.
Returns
SSL or NULL on alloc failure.

References log_crypto_err().

Referenced by comm_point_tcp_accept_callback().

◆ outgoing_ssl_fd()

void* outgoing_ssl_fd ( void *  sslctx,
int  fd 
)

connect a new fd and wrap it in a BIO in SSL

Parameters
sslctxthe SSL_CTX to use (from connect_sslctx_create())
fdfrom connect.
Returns
SSL or NULL on alloc failure

References log_crypto_err(), and log_thread_get().

Referenced by outnet_tcp_take_into_use().

◆ ub_openssl_lock_init()

int ub_openssl_lock_init ( void  )

Initialize openssl locking for thread safety.

Returns
false on failure (alloc failure).

Variable Documentation

◆ EDNS_ADVERTISED_SIZE

uint16_t EDNS_ADVERTISED_SIZE

Advertised size of EDNS capabilities.

Advertised size of EDNS capabilities.

Referenced by answer_from_cache(), answer_norec_from_cache(), config_apply(), error_encode(), local_encode(), local_error_encode(), and serviced_encode().

◆ MINIMAL_RESPONSES

int MINIMAL_RESPONSES

minimal responses when positive answer

minimal responses when positive answer

Referenced by config_apply().

◆ RRSET_ROUNDROBIN

int RRSET_ROUNDROBIN

rrset order roundrobin

rrset order roundrobin

Referenced by config_apply().