xref: /netbsd-src/external/bsd/unbound/dist/util/net_help.h (revision 91f7d55fb697b5e0475da4718fa34c3a3ebeac85)
13b6c3722Schristos /*
23b6c3722Schristos  * util/net_help.h - network help functions
33b6c3722Schristos  *
43b6c3722Schristos  * Copyright (c) 2007, NLnet Labs. All rights reserved.
53b6c3722Schristos  *
63b6c3722Schristos  * This software is open source.
73b6c3722Schristos  *
83b6c3722Schristos  * Redistribution and use in source and binary forms, with or without
93b6c3722Schristos  * modification, are permitted provided that the following conditions
103b6c3722Schristos  * are met:
113b6c3722Schristos  *
123b6c3722Schristos  * Redistributions of source code must retain the above copyright notice,
133b6c3722Schristos  * this list of conditions and the following disclaimer.
143b6c3722Schristos  *
153b6c3722Schristos  * Redistributions in binary form must reproduce the above copyright notice,
163b6c3722Schristos  * this list of conditions and the following disclaimer in the documentation
173b6c3722Schristos  * and/or other materials provided with the distribution.
183b6c3722Schristos  *
193b6c3722Schristos  * Neither the name of the NLNET LABS nor the names of its contributors may
203b6c3722Schristos  * be used to endorse or promote products derived from this software without
213b6c3722Schristos  * specific prior written permission.
223b6c3722Schristos  *
233b6c3722Schristos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
243b6c3722Schristos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
253b6c3722Schristos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
263b6c3722Schristos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
273b6c3722Schristos  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
283b6c3722Schristos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
293b6c3722Schristos  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
303b6c3722Schristos  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
313b6c3722Schristos  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
323b6c3722Schristos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
333b6c3722Schristos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
343b6c3722Schristos  */
353b6c3722Schristos 
363b6c3722Schristos /**
373b6c3722Schristos  * \file
383b6c3722Schristos  *
393b6c3722Schristos  * This file contains functions to perform network related tasks.
403b6c3722Schristos  */
413b6c3722Schristos 
423b6c3722Schristos #ifndef NET_HELP_H
433b6c3722Schristos #define NET_HELP_H
443b6c3722Schristos #include "util/log.h"
457a540f2bSchristos #include "util/random.h"
463b6c3722Schristos struct sock_list;
473b6c3722Schristos struct regional;
48f42d8de7Schristos struct config_strlist;
493b6c3722Schristos 
503b6c3722Schristos /** DNS constants for uint16_t style flag manipulation. host byteorder.
513b6c3722Schristos  *                                1  1  1  1  1  1
523b6c3722Schristos  *  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
533b6c3722Schristos  * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
543b6c3722Schristos  * |QR|   Opcode  |AA|TC|RD|RA| Z|AD|CD|   RCODE   |
553b6c3722Schristos  * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
563b6c3722Schristos  */
573b6c3722Schristos /** CD flag */
583b6c3722Schristos #define BIT_CD 0x0010
593b6c3722Schristos /** AD flag */
603b6c3722Schristos #define BIT_AD 0x0020
613b6c3722Schristos /** Z flag */
623b6c3722Schristos #define BIT_Z  0x0040
633b6c3722Schristos /** RA flag */
643b6c3722Schristos #define BIT_RA 0x0080
653b6c3722Schristos /** RD flag */
663b6c3722Schristos #define BIT_RD 0x0100
673b6c3722Schristos /** TC flag */
683b6c3722Schristos #define BIT_TC 0x0200
693b6c3722Schristos /** AA flag */
703b6c3722Schristos #define BIT_AA 0x0400
713b6c3722Schristos /** QR flag */
723b6c3722Schristos #define BIT_QR 0x8000
733b6c3722Schristos /** get RCODE bits from uint16 flags */
743b6c3722Schristos #define FLAGS_GET_RCODE(f) ((f) & 0xf)
753b6c3722Schristos /** set RCODE bits in uint16 flags */
763b6c3722Schristos #define FLAGS_SET_RCODE(f, r) (f = (((f) & 0xfff0) | (r)))
773b6c3722Schristos 
787cd94d69Schristos /** timeout in milliseconds for UDP queries to auth servers. */
797cd94d69Schristos #define UDP_AUTH_QUERY_TIMEOUT 3000
803b6c3722Schristos /** Advertised version of EDNS capabilities */
813b6c3722Schristos #define EDNS_ADVERTISED_VERSION         0
823b6c3722Schristos /** Advertised size of EDNS capabilities */
833b6c3722Schristos extern uint16_t EDNS_ADVERTISED_SIZE;
843b6c3722Schristos /** bits for EDNS bitfield */
853b6c3722Schristos #define EDNS_DO 0x8000 /* Dnssec Ok */
863b6c3722Schristos /** byte size of ip4 address */
873b6c3722Schristos #define INET_SIZE 4
883b6c3722Schristos /** byte size of ip6 address */
893b6c3722Schristos #define INET6_SIZE 16
903b6c3722Schristos 
913b6c3722Schristos /** DNSKEY zone sign key flag */
923b6c3722Schristos #define DNSKEY_BIT_ZSK 0x0100
933b6c3722Schristos /** DNSKEY secure entry point, KSK flag */
943b6c3722Schristos #define DNSKEY_BIT_SEP 0x0001
953b6c3722Schristos 
967a540f2bSchristos /** return a random 16-bit number given a random source */
977a540f2bSchristos #define GET_RANDOM_ID(rnd) (((unsigned)ub_random(rnd)>>8) & 0xffff)
987a540f2bSchristos 
99*91f7d55fSchristos /** define MSG_DONTWAIT for unsupported platforms */
100*91f7d55fSchristos #ifndef MSG_DONTWAIT
101*91f7d55fSchristos #define MSG_DONTWAIT 0
102*91f7d55fSchristos #endif
103*91f7d55fSchristos 
1043b6c3722Schristos /** minimal responses when positive answer */
1053b6c3722Schristos extern int MINIMAL_RESPONSES;
1063b6c3722Schristos 
1073b6c3722Schristos /** rrset order roundrobin */
1083b6c3722Schristos extern int RRSET_ROUNDROBIN;
1093b6c3722Schristos 
110f42d8de7Schristos /** log tag queries with name instead of 'info' for filtering */
111f42d8de7Schristos extern int LOG_TAG_QUERYREPLY;
112f42d8de7Schristos 
1133b6c3722Schristos /**
1143b6c3722Schristos  * See if string is ip4 or ip6.
1153b6c3722Schristos  * @param str: IP specification.
1163b6c3722Schristos  * @return: true if string addr is an ip6 specced address.
1173b6c3722Schristos  */
1183b6c3722Schristos int str_is_ip6(const char* str);
1193b6c3722Schristos 
1203b6c3722Schristos /**
1213b6c3722Schristos  * Set fd nonblocking.
1223b6c3722Schristos  * @param s: file descriptor.
1233b6c3722Schristos  * @return: 0 on error (error is printed to log).
1243b6c3722Schristos  */
1253b6c3722Schristos int fd_set_nonblock(int s);
1263b6c3722Schristos 
1273b6c3722Schristos /**
1283b6c3722Schristos  * Set fd (back to) blocking.
1293b6c3722Schristos  * @param s: file descriptor.
1303b6c3722Schristos  * @return: 0 on error (error is printed to log).
1313b6c3722Schristos  */
1323b6c3722Schristos int fd_set_block(int s);
1333b6c3722Schristos 
1343b6c3722Schristos /**
1353b6c3722Schristos  * See if number is a power of 2.
1363b6c3722Schristos  * @param num: the value.
1373b6c3722Schristos  * @return: true if the number is a power of 2.
1383b6c3722Schristos  */
1393b6c3722Schristos int is_pow2(size_t num);
1403b6c3722Schristos 
1413b6c3722Schristos /**
1423b6c3722Schristos  * Allocate memory and copy over contents.
1433b6c3722Schristos  * @param data: what to copy over.
1443b6c3722Schristos  * @param len: length of data.
1453b6c3722Schristos  * @return: NULL on malloc failure, or newly malloced data.
1463b6c3722Schristos  */
1473b6c3722Schristos void* memdup(void* data, size_t len);
1483b6c3722Schristos 
1493b6c3722Schristos /**
1503b6c3722Schristos  * Prints the sockaddr in readable format with log_info. Debug helper.
1513b6c3722Schristos  * @param v: at what verbosity level to print this.
1523b6c3722Schristos  * @param str: descriptive string printed with it.
1533b6c3722Schristos  * @param addr: the sockaddr to print. Can be ip4 or ip6.
1543b6c3722Schristos  * @param addrlen: length of addr.
1553b6c3722Schristos  */
1563b6c3722Schristos void log_addr(enum verbosity_value v, const char* str,
1573b6c3722Schristos 	struct sockaddr_storage* addr, socklen_t addrlen);
1583b6c3722Schristos 
1593b6c3722Schristos /**
1603b6c3722Schristos  * Prints zone name and sockaddr in readable format with log_info. Debug.
1613b6c3722Schristos  * @param v: at what verbosity level to print this.
1623b6c3722Schristos  * @param str: descriptive string printed with it.
1633b6c3722Schristos  * @param zone: DNS domain name, uncompressed wireformat.
1643b6c3722Schristos  * @param addr: the sockaddr to print. Can be ip4 or ip6.
1653b6c3722Schristos  * @param addrlen: length of addr.
1663b6c3722Schristos  */
1673b6c3722Schristos void log_name_addr(enum verbosity_value v, const char* str, uint8_t* zone,
1683b6c3722Schristos 	struct sockaddr_storage* addr, socklen_t addrlen);
1693b6c3722Schristos 
1703b6c3722Schristos /**
1713b6c3722Schristos  * Log errno and addr.
1723b6c3722Schristos  * @param str: descriptive string printed with it.
1733b6c3722Schristos  * @param err: errno string to print, i.e. strerror(errno).
1743b6c3722Schristos  * @param addr: the sockaddr to print. Can be ip4 or ip6.
1753b6c3722Schristos  * @param addrlen: length of addr.
1763b6c3722Schristos  */
1773b6c3722Schristos void log_err_addr(const char* str, const char* err,
1783b6c3722Schristos 	struct sockaddr_storage* addr, socklen_t addrlen);
1793b6c3722Schristos 
1803b6c3722Schristos /**
1813b6c3722Schristos  * Convert address string, with "@port" appendix, to sockaddr.
1823b6c3722Schristos  * Uses DNS port by default.
1833b6c3722Schristos  * @param str: the string
1843b6c3722Schristos  * @param addr: where to store sockaddr.
1853b6c3722Schristos  * @param addrlen: length of stored sockaddr is returned.
186*91f7d55fSchristos  * @param port: default port.
1873b6c3722Schristos  * @return 0 on error.
1883b6c3722Schristos  */
1893b6c3722Schristos int extstrtoaddr(const char* str, struct sockaddr_storage* addr,
190*91f7d55fSchristos 	socklen_t* addrlen, int port);
1913b6c3722Schristos 
1923b6c3722Schristos /**
1933b6c3722Schristos  * Convert ip address string and port to sockaddr.
1943b6c3722Schristos  * @param ip: ip4 or ip6 address string.
1953b6c3722Schristos  * @param port: port number, host format.
1963b6c3722Schristos  * @param addr: where to store sockaddr.
1973b6c3722Schristos  * @param addrlen: length of stored sockaddr is returned.
1983b6c3722Schristos  * @return 0 on error.
1993b6c3722Schristos  */
2003b6c3722Schristos int ipstrtoaddr(const char* ip, int port, struct sockaddr_storage* addr,
2013b6c3722Schristos 	socklen_t* addrlen);
2023b6c3722Schristos 
2033b6c3722Schristos /**
2043b6c3722Schristos  * Convert ip netblock (ip/netsize) string and port to sockaddr.
2057cd94d69Schristos  * performs a copy internally to avoid writing over 'ip' string.
2063b6c3722Schristos  * @param ip: ip4 or ip6 address string.
2073b6c3722Schristos  * @param port: port number, host format.
2083b6c3722Schristos  * @param addr: where to store sockaddr.
2093b6c3722Schristos  * @param addrlen: length of stored sockaddr is returned.
2103b6c3722Schristos  * @param net: netblock size is returned.
2113b6c3722Schristos  * @return 0 on error.
2123b6c3722Schristos  */
2133b6c3722Schristos int netblockstrtoaddr(const char* ip, int port, struct sockaddr_storage* addr,
2143b6c3722Schristos 	socklen_t* addrlen, int* net);
2153b6c3722Schristos 
2163b6c3722Schristos /**
2177cd94d69Schristos  * Convert address string, with "@port" appendix, to sockaddr.
2187cd94d69Schristos  * It can also have an "#tls-auth-name" appendix (after the port).
2197a540f2bSchristos  * The returned auth_name string is a pointer into the input string.
2207a540f2bSchristos  * Uses DNS port by default; TLS port when a "#tls-auth-name" is configured.
2217cd94d69Schristos  * @param str: the string
2227cd94d69Schristos  * @param addr: where to store sockaddr.
2237cd94d69Schristos  * @param addrlen: length of stored sockaddr is returned.
2247cd94d69Schristos  * @param auth_name: returned pointer to tls_auth_name, or NULL if none.
2257cd94d69Schristos  * @return 0 on error.
2267cd94d69Schristos  */
2277cd94d69Schristos int authextstrtoaddr(char* str, struct sockaddr_storage* addr,
2287cd94d69Schristos 	socklen_t* addrlen, char** auth_name);
2297cd94d69Schristos 
2307cd94d69Schristos /**
2317a540f2bSchristos  * Convert domain string, with "@port" appendix, to dname.
2327a540f2bSchristos  * It can also have an "#tls-auth-name" appendix (after the port).
2337a540f2bSchristos  * The return port is the parsed port.
2347a540f2bSchristos  * Uses DNS port by default; TLS port when a "#tls-auth-name" is configured.
2357a540f2bSchristos  * The returned auth_name string is a pointer into the input string.
2367a540f2bSchristos  * @param str: the string
2377a540f2bSchristos  * @param port: pointer to be assigned the parsed port value.
2387a540f2bSchristos  * @param auth_name: returned pointer to tls_auth_name, or NULL if none.
2397a540f2bSchristos  * @return pointer to the dname.
2407a540f2bSchristos  */
2417a540f2bSchristos uint8_t* authextstrtodname(char* str, int* port, char** auth_name);
2427a540f2bSchristos 
2437a540f2bSchristos /**
2447cd94d69Schristos  * Store port number into sockaddr structure
2457cd94d69Schristos  * @param addr: sockaddr structure, ip4 or ip6.
2467cd94d69Schristos  * @param addrlen: length of addr.
2477cd94d69Schristos  * @param port: port number to put into the addr.
2487cd94d69Schristos  */
2497cd94d69Schristos void sockaddr_store_port(struct sockaddr_storage* addr, socklen_t addrlen,
2507cd94d69Schristos 	int port);
2517cd94d69Schristos 
2527cd94d69Schristos /**
2533b6c3722Schristos  * Print string with neat domain name, type and class.
2543b6c3722Schristos  * @param v: at what verbosity level to print this.
2553b6c3722Schristos  * @param str: string of message.
2563b6c3722Schristos  * @param name: domain name uncompressed wireformat.
2573b6c3722Schristos  * @param type: host format RR type.
2583b6c3722Schristos  * @param dclass: host format RR class.
2593b6c3722Schristos  */
2603b6c3722Schristos void log_nametypeclass(enum verbosity_value v, const char* str,
2613b6c3722Schristos 	uint8_t* name, uint16_t type, uint16_t dclass);
2623b6c3722Schristos 
2633b6c3722Schristos /**
264f42d8de7Schristos  * Like log_nametypeclass, but logs with log_query for query logging
265f42d8de7Schristos  */
266f42d8de7Schristos void log_query_in(const char* str, uint8_t* name, uint16_t type,
267f42d8de7Schristos 	uint16_t dclass);
268f42d8de7Schristos 
269f42d8de7Schristos /**
2703b6c3722Schristos  * Compare two sockaddrs. Imposes an ordering on the addresses.
2713b6c3722Schristos  * Compares address and port.
2723b6c3722Schristos  * @param addr1: address 1.
2733b6c3722Schristos  * @param len1: lengths of addr1.
2743b6c3722Schristos  * @param addr2: address 2.
2753b6c3722Schristos  * @param len2: lengths of addr2.
2763b6c3722Schristos  * @return: 0 if addr1 == addr2. -1 if addr1 is smaller, +1 if larger.
2773b6c3722Schristos  */
2783b6c3722Schristos int sockaddr_cmp(struct sockaddr_storage* addr1, socklen_t len1,
2793b6c3722Schristos 	struct sockaddr_storage* addr2, socklen_t len2);
2803b6c3722Schristos 
2813b6c3722Schristos /**
2823b6c3722Schristos  * Compare two sockaddrs. Compares address, not the port.
2833b6c3722Schristos  * @param addr1: address 1.
2843b6c3722Schristos  * @param len1: lengths of addr1.
2853b6c3722Schristos  * @param addr2: address 2.
2863b6c3722Schristos  * @param len2: lengths of addr2.
2873b6c3722Schristos  * @return: 0 if addr1 == addr2. -1 if addr1 is smaller, +1 if larger.
2883b6c3722Schristos  */
2893b6c3722Schristos int sockaddr_cmp_addr(struct sockaddr_storage* addr1, socklen_t len1,
2903b6c3722Schristos 	struct sockaddr_storage* addr2, socklen_t len2);
2913b6c3722Schristos 
2923b6c3722Schristos /**
2933b6c3722Schristos  * Checkout address family.
2943b6c3722Schristos  * @param addr: the sockaddr to examine.
2953b6c3722Schristos  * @param len: the length of addr.
2963b6c3722Schristos  * @return: true if sockaddr is ip6.
2973b6c3722Schristos  */
2983b6c3722Schristos int addr_is_ip6(struct sockaddr_storage* addr, socklen_t len);
2993b6c3722Schristos 
3003b6c3722Schristos /**
3013b6c3722Schristos  * Make sure the sockaddr ends in zeroes. For tree insertion and subsequent
3023b6c3722Schristos  * comparison.
3033b6c3722Schristos  * @param addr: the ip4 or ip6 addr.
3043b6c3722Schristos  * @param len: length of addr.
3053b6c3722Schristos  * @param net: number of bits to leave untouched, the rest of the netblock
3063b6c3722Schristos  * 	address is zeroed.
3073b6c3722Schristos  */
3083b6c3722Schristos void addr_mask(struct sockaddr_storage* addr, socklen_t len, int net);
3093b6c3722Schristos 
3103b6c3722Schristos /**
3113b6c3722Schristos  * See how many bits are shared, equal, between two addrs.
3123b6c3722Schristos  * @param addr1: first addr.
3133b6c3722Schristos  * @param net1: netblock size of first addr.
3143b6c3722Schristos  * @param addr2: second addr.
3153b6c3722Schristos  * @param net2: netblock size of second addr.
3163b6c3722Schristos  * @param addrlen: length of first addr and of second addr.
3173b6c3722Schristos  * 	They must be of the same length (i.e. same type IP4, IP6).
3183b6c3722Schristos  * @return: number of bits the same.
3193b6c3722Schristos  */
3203b6c3722Schristos int addr_in_common(struct sockaddr_storage* addr1, int net1,
3213b6c3722Schristos 	struct sockaddr_storage* addr2, int net2, socklen_t addrlen);
3223b6c3722Schristos 
3233b6c3722Schristos /**
3243b6c3722Schristos  * Put address into string, works for IPv4 and IPv6.
3253b6c3722Schristos  * @param addr: address
3263b6c3722Schristos  * @param addrlen: length of address
3273b6c3722Schristos  * @param buf: result string stored here
3283b6c3722Schristos  * @param len: length of buf.
3293b6c3722Schristos  * On failure a string with "error" is stored inside.
3303b6c3722Schristos  */
3313b6c3722Schristos void addr_to_str(struct sockaddr_storage* addr, socklen_t addrlen,
3323b6c3722Schristos 	char* buf, size_t len);
3333b6c3722Schristos 
3343b6c3722Schristos /**
335*91f7d55fSchristos  * Check if the prefix network length is one of the allowed 32, 40, 48, 56, 64,
336*91f7d55fSchristos  * or 96.
337*91f7d55fSchristos  * @param prefixnet: prefix network length to check.
338*91f7d55fSchristos  * @return 1 on success, 0 on failure.
339*91f7d55fSchristos  */
340*91f7d55fSchristos int prefixnet_is_nat64(int prefixnet);
341*91f7d55fSchristos 
342*91f7d55fSchristos /**
343*91f7d55fSchristos  * Create a NAT64 address from a given address (needs to be IPv4) and a given
344*91f7d55fSchristos  * NAT64 prefix. The NAT64 prefix net needs to be one of 32, 40, 48, 56, 64, 96.
345*91f7d55fSchristos  * @param addr: IPv4 address.
346*91f7d55fSchristos  * @param nat64_prefix: NAT64 prefix.
347*91f7d55fSchristos  * @param nat64_prefixlen: NAT64 prefix len.
348*91f7d55fSchristos  * @param nat64_prefixnet: NAT64 prefix mask.
349*91f7d55fSchristos  * @param nat64_addr: the resulting NAT64 address.
350*91f7d55fSchristos  * @param nat64_addrlen: the resulting NAT64 address length.
351*91f7d55fSchristos  */
352*91f7d55fSchristos void addr_to_nat64(const struct sockaddr_storage* addr,
353*91f7d55fSchristos 	const struct sockaddr_storage* nat64_prefix,
354*91f7d55fSchristos 	socklen_t nat64_prefixlen, int nat64_prefixnet,
355*91f7d55fSchristos 	struct sockaddr_storage* nat64_addr, socklen_t* nat64_addrlen);
356*91f7d55fSchristos 
357*91f7d55fSchristos /**
3583b6c3722Schristos  * See if sockaddr is an ipv6 mapped ipv4 address, "::ffff:0.0.0.0"
3593b6c3722Schristos  * @param addr: address
3603b6c3722Schristos  * @param addrlen: length of address
3613b6c3722Schristos  * @return true if so
3623b6c3722Schristos  */
3633b6c3722Schristos int addr_is_ip4mapped(struct sockaddr_storage* addr, socklen_t addrlen);
3643b6c3722Schristos 
3653b6c3722Schristos /**
3663b6c3722Schristos  * See if sockaddr is 255.255.255.255.
3673b6c3722Schristos  * @param addr: address
3683b6c3722Schristos  * @param addrlen: length of address
3693b6c3722Schristos  * @return true if so
3703b6c3722Schristos  */
3713b6c3722Schristos int addr_is_broadcast(struct sockaddr_storage* addr, socklen_t addrlen);
3723b6c3722Schristos 
3733b6c3722Schristos /**
3743b6c3722Schristos  * See if sockaddr is 0.0.0.0 or ::0.
3753b6c3722Schristos  * @param addr: address
3763b6c3722Schristos  * @param addrlen: length of address
3773b6c3722Schristos  * @return true if so
3783b6c3722Schristos  */
3793b6c3722Schristos int addr_is_any(struct sockaddr_storage* addr, socklen_t addrlen);
3803b6c3722Schristos 
3813b6c3722Schristos /**
3823b6c3722Schristos  * Insert new socket list item. If fails logs error.
3833b6c3722Schristos  * @param list: pointer to pointer to first item.
3843b6c3722Schristos  * @param addr: address or NULL if 'cache'.
3853b6c3722Schristos  * @param len: length of addr, or 0 if 'cache'.
3863b6c3722Schristos  * @param region: where to allocate
3873b6c3722Schristos  */
3883b6c3722Schristos void sock_list_insert(struct sock_list** list, struct sockaddr_storage* addr,
3893b6c3722Schristos 	socklen_t len, struct regional* region);
3903b6c3722Schristos 
3913b6c3722Schristos /**
3923b6c3722Schristos  * Append one list to another.  Must both be from same qstate(regional).
3933b6c3722Schristos  * @param list: pointer to result list that is modified.
3943b6c3722Schristos  * @param add: item(s) to add.  They are prepended to list.
3953b6c3722Schristos  */
3963b6c3722Schristos void sock_list_prepend(struct sock_list** list, struct sock_list* add);
3973b6c3722Schristos 
3983b6c3722Schristos /**
3993b6c3722Schristos  * Find addr in list.
4003b6c3722Schristos  * @param list: to search in
4013b6c3722Schristos  * @param addr: address to look for.
4023b6c3722Schristos  * @param len: length. Can be 0, look for 'cache entry'.
4033b6c3722Schristos  * @return true if found.
4043b6c3722Schristos  */
4053b6c3722Schristos int sock_list_find(struct sock_list* list, struct sockaddr_storage* addr,
4063b6c3722Schristos         socklen_t len);
4073b6c3722Schristos 
4083b6c3722Schristos /**
4093b6c3722Schristos  * Merge socklist into another socket list.  Allocates the new entries
4103b6c3722Schristos  * freshly and copies them over, so also performs a region switchover.
4113b6c3722Schristos  * Allocation failures are logged.
4123b6c3722Schristos  * @param list: the destination list (checked for duplicates)
4133b6c3722Schristos  * @param region: where to allocate
4143b6c3722Schristos  * @param add: the list of entries to add.
4153b6c3722Schristos  */
4163b6c3722Schristos void sock_list_merge(struct sock_list** list, struct regional* region,
4173b6c3722Schristos 	struct sock_list* add);
4183b6c3722Schristos 
4193b6c3722Schristos /**
4203b6c3722Schristos  * Log libcrypto error with descriptive string. Calls log_err().
4213b6c3722Schristos  * @param str: what failed.
4223b6c3722Schristos  */
4233b6c3722Schristos void log_crypto_err(const char* str);
4243b6c3722Schristos 
4253b6c3722Schristos /**
42601049ae6Schristos  * Log libcrypto error from errcode with descriptive string, calls log_err.
42701049ae6Schristos  * @param str: what failed.
42801049ae6Schristos  * @param err: error code from ERR_get_error.
42901049ae6Schristos  */
43001049ae6Schristos void log_crypto_err_code(const char* str, unsigned long err);
43101049ae6Schristos 
43201049ae6Schristos /**
433*91f7d55fSchristos  * Log an error from libcrypto that came from SSL_write and so on, with
434*91f7d55fSchristos  * a value from SSL_get_error, calls log_err. If that fails it logs with
435*91f7d55fSchristos  * log_crypto_err.
436*91f7d55fSchristos  * @param str: what failed
437*91f7d55fSchristos  * @param r: output of SSL_get_error on the I/O operation result.
438*91f7d55fSchristos  */
439*91f7d55fSchristos void log_crypto_err_io(const char* str, int r);
440*91f7d55fSchristos 
441*91f7d55fSchristos /**
442*91f7d55fSchristos  * Log an error from libcrypt that came from an I/O routine with the
443*91f7d55fSchristos  * errcode from ERR_get_error. Calls log_err() and log_crypto_err_code.
444*91f7d55fSchristos  * @param str: what failed
445*91f7d55fSchristos  * @param r: output of SSL_get_error on the I/O operation result.
446*91f7d55fSchristos  * @param err: error code from ERR_get_error
447*91f7d55fSchristos  */
448*91f7d55fSchristos void log_crypto_err_io_code(const char* str, int r, unsigned long err);
449*91f7d55fSchristos 
450*91f7d55fSchristos /**
451d0eba39bSchristos  * Log certificate details verbosity, string, of X509 cert
452d0eba39bSchristos  * @param level: verbosity level
453d0eba39bSchristos  * @param str: string to prefix on output
454d0eba39bSchristos  * @param cert: X509* structure.
455d0eba39bSchristos  */
456d0eba39bSchristos void log_cert(unsigned level, const char* str, void* cert);
457d0eba39bSchristos 
458d0eba39bSchristos /**
4590cd9f4ecSchristos  * Set SSL_OP_NOxxx options on SSL context to disable bad crypto
4600cd9f4ecSchristos  * @param ctxt: SSL_CTX*
4610cd9f4ecSchristos  * @return false on failure.
4620cd9f4ecSchristos  */
4630cd9f4ecSchristos int listen_sslctx_setup(void* ctxt);
4640cd9f4ecSchristos 
4650cd9f4ecSchristos /**
4660cd9f4ecSchristos  * Further setup of listening SSL context, after keys loaded.
4670cd9f4ecSchristos  * @param ctxt: SSL_CTX*
4680cd9f4ecSchristos  */
4690cd9f4ecSchristos void listen_sslctx_setup_2(void* ctxt);
4700cd9f4ecSchristos 
4710cd9f4ecSchristos /**
4723b6c3722Schristos  * create SSL listen context
4733b6c3722Schristos  * @param key: private key file.
4743b6c3722Schristos  * @param pem: public key cert.
4753b6c3722Schristos  * @param verifypem: if nonNULL, verifylocation file.
4763b6c3722Schristos  * return SSL_CTX* or NULL on failure (logged).
4773b6c3722Schristos  */
4783b6c3722Schristos void* listen_sslctx_create(char* key, char* pem, char* verifypem);
4793b6c3722Schristos 
4803b6c3722Schristos /**
4813b6c3722Schristos  * create SSL connect context
4823b6c3722Schristos  * @param key: if nonNULL (also pem nonNULL), the client private key.
4833b6c3722Schristos  * @param pem: client public key (or NULL if key is NULL).
4843b6c3722Schristos  * @param verifypem: if nonNULL used for verifylocation file.
4857cd94d69Schristos  * @param wincert: add system certificate store to ctx (add to verifypem ca
4867cd94d69Schristos  * 	certs).
4873b6c3722Schristos  * @return SSL_CTX* or NULL on failure (logged).
4883b6c3722Schristos  */
4897cd94d69Schristos void* connect_sslctx_create(char* key, char* pem, char* verifypem, int wincert);
4903b6c3722Schristos 
4913b6c3722Schristos /**
4923b6c3722Schristos  * accept a new fd and wrap it in a BIO in SSL
4933b6c3722Schristos  * @param sslctx: the SSL_CTX to use (from listen_sslctx_create()).
4943b6c3722Schristos  * @param fd: from accept, nonblocking.
4953b6c3722Schristos  * @return SSL or NULL on alloc failure.
4963b6c3722Schristos  */
4973b6c3722Schristos void* incoming_ssl_fd(void* sslctx, int fd);
4983b6c3722Schristos 
4993b6c3722Schristos /**
5003b6c3722Schristos  * connect a new fd and wrap it in a BIO in SSL
5013b6c3722Schristos  * @param sslctx: the SSL_CTX to use (from connect_sslctx_create())
5023b6c3722Schristos  * @param fd: from connect.
5033b6c3722Schristos  * @return SSL or NULL on alloc failure
5043b6c3722Schristos  */
5053b6c3722Schristos void* outgoing_ssl_fd(void* sslctx, int fd);
5063b6c3722Schristos 
5073b6c3722Schristos /**
508d0eba39bSchristos  * check if authname SSL functionality is available, false if not
509d0eba39bSchristos  * @param auth_name: the name for the remote server, used for error print.
510d0eba39bSchristos  * @return false if SSL functionality to check the SSL name is not available.
511d0eba39bSchristos  */
512d0eba39bSchristos int check_auth_name_for_ssl(char* auth_name);
513d0eba39bSchristos 
514d0eba39bSchristos /**
515d0eba39bSchristos  * set auth name on SSL for verification
516d0eba39bSchristos  * @param ssl: SSL* to set
517d0eba39bSchristos  * @param auth_name: if NULL nothing happens, otherwise the name to check.
518d0eba39bSchristos  * @param use_sni: if SNI will be used.
519d0eba39bSchristos  * @return 1 on success or NULL auth_name, 0 on failure.
520d0eba39bSchristos  */
521d0eba39bSchristos int set_auth_name_on_ssl(void* ssl, char* auth_name, int use_sni);
522d0eba39bSchristos 
523d0eba39bSchristos /**
5243b6c3722Schristos  * Initialize openssl locking for thread safety
5253b6c3722Schristos  * @return false on failure (alloc failure).
5263b6c3722Schristos  */
5273b6c3722Schristos int ub_openssl_lock_init(void);
5283b6c3722Schristos 
5293b6c3722Schristos /**
5303b6c3722Schristos  * De-init the allocated openssl locks
5313b6c3722Schristos  */
5323b6c3722Schristos void ub_openssl_lock_delete(void);
5333b6c3722Schristos 
534f42d8de7Schristos /**
535f42d8de7Schristos  * setup TLS session ticket
536f42d8de7Schristos  * @param sslctx: the SSL_CTX to use (from connect_sslctx_create())
537f42d8de7Schristos  * @param tls_session_ticket_keys: TLS ticket secret filenames
538f42d8de7Schristos  * @return false on failure (alloc failure).
539f42d8de7Schristos  */
540f42d8de7Schristos int listen_sslctx_setup_ticket_keys(void* sslctx,
541f42d8de7Schristos 	struct config_strlist* tls_session_ticket_keys);
542f42d8de7Schristos 
543f42d8de7Schristos /** Free memory used for TLS session ticket keys */
544f42d8de7Schristos void listen_sslctx_delete_ticket_keys(void);
545f42d8de7Schristos 
546d0eba39bSchristos /**
547d0eba39bSchristos  * RPZ format netblock to network byte order address and netblock
548d0eba39bSchristos  * example RPZ netblock format dnames:
549d0eba39bSchristos  *  - 24.10.100.51.198.rpz-ip -> 198.51.100.10/24
550d0eba39bSchristos  *  - 32.10.zz.db8.2001.rpz-ip -> 2001:db8:0:0:0:0:0:10/32
551d0eba39bSchristos  * @param dname: the dname containing RPZ format netblock
552d0eba39bSchristos  * @param dnamelen: length of dname
553d0eba39bSchristos  * @param addr: where to store sockaddr.
554d0eba39bSchristos  * @param addrlen: length of stored sockaddr is returned.
555d0eba39bSchristos  * @param net: where to store netmask
556d0eba39bSchristos  * @param af: where to store address family.
557d0eba39bSchristos  * @return 0 on error.
558d0eba39bSchristos  */
559d0eba39bSchristos int netblockdnametoaddr(uint8_t* dname, size_t dnamelen,
560d0eba39bSchristos 	struct sockaddr_storage* addr, socklen_t* addrlen, int* net, int* af);
561d0eba39bSchristos 
562d0eba39bSchristos /** Return strerror or wsastrerror for socket error printout */
563d0eba39bSchristos char* sock_strerror(int errn);
564d0eba39bSchristos /** close the socket with close, or wsa closesocket */
565d0eba39bSchristos void sock_close(int socket);
566d0eba39bSchristos 
5673b6c3722Schristos #endif /* NET_HELP_H */
568