unbound  0.1
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.
#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.
#define TCP_AUTH_QUERY_TIMEOUT   30
 timeout in seconds for TCP queries to auth servers.
#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.
int fd_set_nonblock (int s)
 Set fd nonblocking.
int fd_set_block (int s)
 Set fd (back to) blocking.
int is_pow2 (size_t num)
 See if number is a power of 2.
void * memdup (void *data, size_t len)
 Allocate memory and copy over contents.
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.
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.
int extstrtoaddr (const char *str, struct sockaddr_storage *addr, socklen_t *addrlen)
 Convert address string, with "@port" appendix, to sockaddr.
int ipstrtoaddr (const char *ip, int port, struct sockaddr_storage *addr, socklen_t *addrlen)
 Convert ip address string and port to sockaddr.
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.
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.
int sockaddr_cmp (struct sockaddr_storage *addr1, socklen_t len1, struct sockaddr_storage *addr2, socklen_t len2)
 Compare two sockaddrs.
int sockaddr_cmp_addr (struct sockaddr_storage *addr1, socklen_t len1, struct sockaddr_storage *addr2, socklen_t len2)
 Compare two sockaddrs.
int addr_is_ip6 (struct sockaddr_storage *addr, socklen_t len)
 Checkout address family.
void addr_mask (struct sockaddr_storage *addr, socklen_t len, int net)
 Make sure the sockaddr ends in zeroes.
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.
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.
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".
int addr_is_broadcast (struct sockaddr_storage *addr, socklen_t addrlen)
 See if sockaddr is 255.255.255.255.
int addr_is_any (struct sockaddr_storage *addr, socklen_t addrlen)
 See if sockaddr is 0.0.0.0 or ::0.
void sock_list_insert (struct sock_list **list, struct sockaddr_storage *addr, socklen_t len, struct regional *region)
 Insert new socket list item.
void sock_list_prepend (struct sock_list **list, struct sock_list *add)
 Append one list to another.
int sock_list_find (struct sock_list *list, struct sockaddr_storage *addr, socklen_t len)
 Find addr in list.
void sock_list_merge (struct sock_list **list, struct regional *region, struct sock_list *add)
 Merge socklist into another socket list.
void log_crypto_err (const char *str)
 Log libcrypto error with descriptive string.
void * listen_sslctx_create (char *key, char *pem, char *verifypem)
 create SSL listen context
void * connect_sslctx_create (char *key, char *pem, char *verifypem)
 create SSL connect context
void * incoming_ssl_fd (void *sslctx, int fd)
 accept a new fd and wrap it in a BIO in SSL
void * outgoing_ssl_fd (void *sslctx, int fd)
 connect a new fd and wrap it in a BIO in SSL

Variables

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

Detailed Description

This file contains functions to perform network related tasks.

Macro Definition Documentation

#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(), createResponse(), dns_cache_store(), error_encode(), generate_a_aaaa_check(), generate_dnskey_prefetch(), generate_ns_check(), generate_sub_request(), mesh_log_list(), mesh_send_reply(), mesh_state_compare(), mesh_state_create(), needs_validation(), prime_trust_anchor(), process_response(), processCollectClass(), processDLVLookup(), processFindKey(), processInitRequest3(), processQueryResponse(), processQueryTargets(), reply_info_answer_encode(), server_stats_insquery(), serviced_encode(), and val_new_getmsg().

#define UDP_AUTH_QUERY_TIMEOUT   4

timeout in seconds for UDP queries to auth servers.

#define TCP_AUTH_QUERY_TIMEOUT   30

timeout in seconds for TCP queries to auth servers.

Referenced by serviced_tcp_callback(), serviced_tcp_initiate(), and serviced_tcp_send().

Function Documentation

int str_is_ip6 ( const char *  str)

See if string is ip4 or ip6.

Parameters
str,:IP 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(), service(), and ub_ctx_hosts().

int fd_set_nonblock ( int  s)

Set fd nonblocking.

Parameters
s,:file descriptor.
Returns
: 0 on error (error is printed to log).

References log_err().

Referenced by comm_point_perform_accept(), create_tcp_accept_sock(), create_udp_sock(), find_create_proxy(), outnet_tcp_take_into_use(), service(), service_tcp_listen(), tube_create(), tube_read_msg(), and tube_write_msg().

int fd_set_block ( int  s)

Set fd (back to) blocking.

Parameters
s,:file descriptor.
Returns
: 0 on error (error is printed to log).

References log_err().

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

int is_pow2 ( size_t  num)

See if number is a power of 2.

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

Referenced by net_test().

void* memdup ( void *  data,
size_t  len 
)
void log_addr ( enum verbosity_value  v,
const char *  str,
struct sockaddr_storage *  addr,
socklen_t  addrlen 
)
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
v,:at what verbosity level to print this.
str,:descriptive string printed with it.
zone,:DNS domain name, uncompressed wireformat.
addr,:the sockaddr to print. Can be ip4 or ip6.
addrlen,:length of addr.

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

Referenced by process_response(), processQueryTargets(), and serviced_udp_callback().

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
str,:the string
addr,:where to store sockaddr.
addrlen,:length of stored sockaddr is returned.
Returns
0 on error.

References ipstrtoaddr(), and MAX_ADDR_STRLEN.

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

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

Convert ip address string and port to sockaddr.

Parameters
ip,:ip4 or ip6 address string.
port,:port number, host format.
addr,:where to store sockaddr.
addrlen,:length of stored sockaddr is returned.
Returns
0 on error.

References MAX_ADDR_STRLEN, and str_is_ip6().

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

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
ip,:ip4 or ip6 address string.
port,:port number, host format.
addr,:where to store sockaddr.
addrlen,:length of stored sockaddr is returned.
net,:netblock size is returned.
Returns
0 on error.

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

Referenced by acl_list_str_cfg(), aclchecks(), donotq_str_cfg(), and read_addrs().

void log_nametypeclass ( enum verbosity_value  v,
const char *  str,
uint8_t *  name,
uint16_t  type,
uint16_t  dclass 
)
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
addr1,:address 1.
len1,:lengths of addr1.
addr2,:address 2.
len2,:lengths 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 answer_check_it(), find_create_proxy(), infra_compfunc(), infra_del_host(), pending_cmp(), pending_find_match(), pending_matches_current(), pending_udp_query(), and serviced_cmp().

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
addr1,:address 1.
len1,:lengths of addr1.
addr2,:address 2.
len2,:lengths 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().

int addr_is_ip6 ( struct sockaddr_storage *  addr,
socklen_t  len 
)
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
addr,:the ip4 or ip6 addr.
len,:length of addr.
net,:number of bits to leave untouched, the rest of the netblock address is zeroed.

References addr_is_ip6().

Referenced by net_test(), and netblockstrtoaddr().

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
addr1,:first addr.
net1,:netblock size of first addr.
addr2,:second addr.
net2,:netblock size of second addr.
addrlen,:length 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(), and net_test().

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
addr,:address
addrlen,:length of address
buf,:result string stored here
len,:length 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(), print_dp_details(), ssl_print_name_dp(), and worker_handle_request().

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
addr,:address
addrlen,:length of address
Returns
true if so

References addr_is_ip6().

Referenced by net_test(), and udp_send_errno_needs_log().

int addr_is_broadcast ( struct sockaddr_storage *  addr,
socklen_t  addrlen 
)

See if sockaddr is 255.255.255.255.

Parameters
addr,:address
addrlen,:length of address
Returns
true if so

Referenced by udp_send_errno_needs_log().

int addr_is_any ( struct sockaddr_storage *  addr,
socklen_t  addrlen 
)

See if sockaddr is 0.0.0.0 or ::0.

Parameters
addr,:address
addrlen,:length of address
Returns
true if so

Referenced by net_test(), and pick_outgoing_tcp().

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
list,:pointer to pointer to first item.
addr,:address or NULL if 'cache'.
len,:length of addr, or 0 if 'cache'.
region,:where 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(), mesh_new_prefetch(), processInitRequest(), processInitRequest3(), processQueryResponse(), sock_list_merge(), and val_blacklist().

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
list,:pointer to result list that is modified.
add,:item(s) to add. They are prepended to list.

References sock_list::next.

Referenced by val_blacklist().

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

Find addr in list.

Parameters
list,:to search in
addr,:address to look for.
len,:length. 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().

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
list,:the destination list (checked for duplicates)
region,:where to allocate
add,:the list of entries to add.

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

Referenced by generate_request(), and val_blacklist().

void log_crypto_err ( const char *  str)
void* listen_sslctx_create ( char *  key,
char *  pem,
char *  verifypem 
)

create SSL listen context

Parameters
key,:private key file.
pem,:public key cert.
verifypem,:if nonNULL, verifylocation file. return SSL_CTX* or NULL on failure (logged).

References log_crypto_err(), and log_err().

Referenced by perform_setup().

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

create SSL connect context

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

References log_crypto_err(), and log_err().

Referenced by libworker_setup(), perform_setup(), and send_em().

void* incoming_ssl_fd ( void *  sslctx,
int  fd 
)

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

Parameters
sslctx,:the SSL_CTX to use (from listen_sslctx_create()).
fd,:from accept, nonblocking.
Returns
SSL or NULL on alloc failure.

References log_crypto_err().

Referenced by comm_point_tcp_accept_callback().

void* outgoing_ssl_fd ( void *  sslctx,
int  fd 
)

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

Parameters
sslctx,:the SSL_CTX to use (from connect_sslctx_create())
fd,:from connect.
Returns
SSL or NULL on alloc failure

References log_crypto_err().

Referenced by outnet_tcp_take_into_use(), and send_em().