Implementation of net_help.h. More...
#include "config.h"#include <ldns/ldns.h>#include "util/net_help.h"#include "util/log.h"#include "util/data/dname.h"#include "util/module.h"#include "util/regional.h"#include <fcntl.h>#include <openssl/ssl.h>#include <openssl/err.h>Defines | |
| #define | MAX_ADDR_STRLEN 128 |
| max length of an IP address (the address portion) that we allow | |
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. | |
| 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 *str, 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. | |
| 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 | 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 = 4096 |
| default value for EDNS ADVERTISED size | |
Implementation of net_help.h.
| int str_is_ip6 | ( | const char * | str | ) |
See if string is ip4 or ip6.
| str,: | IP specification. |
Referenced by ub_ctx_hosts(), listening_ports_open(), calc_num46(), outside_network_create(), service(), net_test(), ipstrtoaddr(), and netblockstrtoaddr().
| int fd_set_nonblock | ( | int | s | ) |
Set fd nonblocking.
| s,: | file descriptor. |
References log_err().
Referenced by create_udp_sock(), create_tcp_accept_sock(), outnet_tcp_take_into_use(), find_create_proxy(), service_tcp_listen(), service(), comm_point_perform_accept(), tube_create(), tube_write_msg(), and tube_read_msg().
| int fd_set_block | ( | int | s | ) |
Set fd (back to) blocking.
| s,: | file descriptor. |
References log_err().
Referenced by handle_req(), tube_write_msg(), and tube_read_msg().
| int is_pow2 | ( | size_t | num | ) |
See if number is a power of 2.
| num,: | the value. |
Referenced by net_test().
| void* memdup | ( | void * | data, |
| size_t | len | ||
| ) |
Allocate memory and copy over contents.
| data,: | what to copy over. |
| len,: | length of data. |
Referenced by move_into_cache(), parse_arg_name(), worker_create(), context_deserialize_answer(), fill_res(), libworker_fillup_fg(), setup_qinfo_edns(), add_bg_result(), dns_cache_store(), new_entry(), parse_dname(), get_rr_content(), get_rr_nameclass(), lz_setup_implicit(), setup_if(), pending_udp_query(), serviced_create(), serviced_callbacks(), tcp_relay_read(), qlist_add_line(), write_q(), repinfo_copy_rrsets(), packed_rrset_copy_alloc(), ub_packed_rrset_heap_key(), autr_tp_create(), key_entry_copy(), neg_setup_zone_node(), neg_setup_data_node(), and neg_insert_data().
| 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.
| v,: | at what verbosity level to print this. |
| str,: | descriptive string printed with it. |
| addr,: | the sockaddr to print. Can be ip4 or ip6. |
| addrlen,: | length of addr. |
References verbosity, and verbose().
Referenced by remote_accept_callback(), remote_control_callback(), worker_handle_request(), delegpt_log(), iter_filter_unsuitable(), iter_merge_retry_counts(), processQueryTargets(), create_udp_sock(), create_tcp_accept_sock(), pick_outgoing_tcp(), outnet_tcp_take_into_use(), outnet_tcp_cb(), outnet_udp_cb(), serviced_callbacks(), serviced_tcp_callback(), serviced_udp_callback(), contact_server(), service_send(), do_proxy(), service_recv(), tcp_proxy_delete(), service_tcp_listen(), service_tcp_relay(), pending_find_match(), do_infra_rtt(), run_scenario(), comm_point_send_udp_msg(), comm_point_send_udp_msg_if(), comm_point_perform_accept(), ssl_handshake(), comm_point_tcp_handle_read(), comm_point_tcp_handle_write(), and sock_list_logentry().
| 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.
| str,: | the string |
| addr,: | where to store sockaddr. |
| addrlen,: | length of stored sockaddr is returned. |
References MAX_ADDR_STRLEN, and ipstrtoaddr().
Referenced by parse_delegpt(), read_fwds_addr(), ah(), read_stubs_addr(), ub_ctx_set_fwd(), warn_hosts(), interfacechecks(), contact_server(), service(), main(), replay_range_read(), replay_moment_read(), and open_svr().
| int ipstrtoaddr | ( | const char * | ip, |
| int | port, | ||
| struct sockaddr_storage * | addr, | ||
| socklen_t * | addrlen | ||
| ) |
Convert ip address string and port to sockaddr.
| ip,: | ip4 or ip6 address string. |
| port,: | port number, host format. |
| addr,: | where to store sockaddr. |
| addrlen,: | length of stored sockaddr is returned. |
References str_is_ip6(), and MAX_ADDR_STRLEN.
Referenced by do_flush_infra(), setup_if(), interfacechecks(), contact_server(), service(), net_test(), infra_test(), cfg_ptr_reverse(), extstrtoaddr(), and 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.
| 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. |
References str_is_ip6(), log_err(), ipstrtoaddr(), and addr_mask().
Referenced by acl_list_str_cfg(), donotq_str_cfg(), read_addrs(), and aclchecks().
| 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.
| v,: | at what verbosity level to print this. |
| str,: | string of message. |
| name,: | domain name uncompressed wireformat. |
| type,: | host format RR type. |
| dclass,: | host format RR class. |
References verbosity, dname_str(), and log_info().
Referenced by worker_handle_request(), remove_rrset(), scrub_message(), iter_mark_cycle_targets(), iter_mark_pside_cycle_targets(), iter_scrub_ds(), prime_stub(), generate_a_aaaa_check(), generate_ns_check(), generate_dnskey_prefetch(), generate_parentside_target_query(), generate_target_query(), processPrimeResponse(), processCollectClass(), cache_fill_missing(), lz_setup_implicit(), local_zone_out(), local_zones_print(), mesh_make_new_space(), outnet_serviced_query(), get_random_data(), add_item(), remove_item(), check_zone_invariants(), verifytest_rrset(), verifytest_file(), log_query_info(), log_rrset_key(), update_events(), autr_tp_remove(), autr_process_prime(), anchor_store_new_rr(), anchors_assemble_rrsets(), neg_insert_data(), val_neg_addreply(), val_neg_dlvlookup(), val_neg_addreferral(), val_nsec_check_dlv(), nsec3_prove_closest_encloser(), nsec3_do_prove_nameerror(), nsec3_prove_nameerror(), dnskey_verify_rrset_sig(), val_verify_rrset(), val_check_nonsecure(), validate_msg_signatures(), validate_positive_response(), validate_any_response(), validate_cname_response(), processInit(), processFindKey(), processValidate(), val_dlv_init(), processDLVLookup(), and primeResponseToKE().
| 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.
| 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 verbosity, dname_str(), and verbose().
Referenced by processQueryTargets(), process_response(), and serviced_udp_callback().
| 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.
| addr1,: | address 1. |
| len1,: | lengths of addr1. |
| addr2,: | address 2. |
| len2,: | lengths of addr2. |
References log_assert, INET_SIZE, and INET6_SIZE.
Referenced by infra_del_host(), infra_compfunc(), pending_cmp(), serviced_cmp(), find_create_proxy(), pending_matches_current(), pending_find_match(), answer_check_it(), pending_udp_query(), pending_tcp_query(), and outnet_serviced_query().
| 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.
| addr1,: | address 1. |
| len1,: | lengths of addr1. |
| addr2,: | address 2. |
| len2,: | lengths of addr2. |
References log_assert, INET_SIZE, and INET6_SIZE.
Referenced by delegpt_find_addr(), net_test(), sock_list_find(), and addr_tree_compare().
| int addr_is_ip6 | ( | struct sockaddr_storage * | addr, |
| socklen_t | len | ||
| ) |
Checkout address family.
| addr,: | the sockaddr to examine. |
| len,: | the length of addr. |
Referenced by server_stats_insquery(), delegpt_add_target(), iter_filter_unsuitable(), hash_addr(), pick_outgoing_tcp(), outnet_tcp_take_into_use(), udp_sockport(), randomize_and_send_udp(), serviced_encode(), warn_hosts(), contact_server(), service_recv(), service_tcp_listen(), proxy_list_clear(), perfsetup(), open_svr(), cfg_ptr_reverse(), addr_mask(), addr_in_common(), addr_to_str(), addr_is_ip4mapped(), and addr_tree_lookup().
| 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.
| 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.
| 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). |
References match(), addr_is_ip6(), and log_assert.
Referenced by net_test(), addr_tree_init_parents(), and addr_tree_lookup().
| 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.
| 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 print_dp_details(), ssl_print_name_dp(), get_mesh_status(), dump_infra_host(), worker_handle_request(), and errinf_origin().
| 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".
| addr,: | address |
| addrlen,: | length of address |
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.
| addr,: | address |
| addrlen,: | length of address |
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.
| addr,: | address |
| addrlen,: | length of address |
Referenced by pick_outgoing_tcp(), and net_test().
| 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.
| 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 regional_alloc(), sock_list::addr, sock_list::len, log_err(), log_assert, and sock_list::next.
Referenced by processInitRequest(), processInitRequest3(), generate_parentside_target_query(), processQueryResponse(), mesh_new_prefetch(), sock_list_merge(), and val_blacklist().
Append one list to another.
Must both be from same qstate(regional).
| 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.
| list,: | to search in |
| addr,: | address to look for. |
| len,: | length. Can be 0, look for 'cache entry'. |
References sock_list::len, sockaddr_cmp_addr(), sock_list::addr, and sock_list::next.
Referenced by iter_fill_rtt(), and sock_list_merge().
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.
| list,: | the destination list (checked for duplicates) |
| region,: | where to allocate |
| add,: | the list of entries to add. |
References sock_list::next, sock_list_find(), sock_list::addr, sock_list::len, and sock_list_insert().
Referenced by val_blacklist(), and generate_request().
| void log_crypto_err | ( | const char * | str | ) |
Log libcrypto error with descriptive string.
Calls log_err().
| str,: | what failed. |
References log_err().
Referenced by daemon_remote_create(), remote_accept_callback(), ssl_print_text(), ssl_read_line(), handle_req(), remote_control_callback(), write_q(), recv_one(), send_em(), listen_sslctx_create(), connect_sslctx_create(), incoming_ssl_fd(), outgoing_ssl_fd(), ssl_handshake(), ssl_handle_read(), and ssl_handle_write().
| void* listen_sslctx_create | ( | char * | key, |
| char * | pem, | ||
| char * | verifypem | ||
| ) |
create SSL listen context
| 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
| 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. |
References log_crypto_err(), and log_err().
Referenced by perform_setup(), libworker_setup(), and send_em().
| void* incoming_ssl_fd | ( | void * | sslctx, |
| int | fd | ||
| ) |
accept a new fd and wrap it in a BIO in SSL
| sslctx,: | the SSL_CTX to use (from listen_sslctx_create()). |
| fd,: | from accept, nonblocking. |
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
| sslctx,: | the SSL_CTX to use (from connect_sslctx_create()) |
| fd,: | from connect. |
References log_crypto_err().
Referenced by outnet_tcp_take_into_use(), and send_em().
| uint16_t EDNS_ADVERTISED_SIZE = 4096 |
default value for EDNS ADVERTISED size
Advertised size of EDNS capabilities.
Referenced by answer_norec_from_cache(), answer_from_cache(), chaos_replystr(), worker_handle_request(), local_encode(), mesh_do_callback(), mesh_send_reply(), serviced_encode(), outnet_serviced_query(), qlist_parse_line(), config_apply(), and error_encode().