1933707f3Ssthen /* 2933707f3Ssthen * util/net_help.h - network help functions 3933707f3Ssthen * 4933707f3Ssthen * Copyright (c) 2007, NLnet Labs. All rights reserved. 5933707f3Ssthen * 6933707f3Ssthen * This software is open source. 7933707f3Ssthen * 8933707f3Ssthen * Redistribution and use in source and binary forms, with or without 9933707f3Ssthen * modification, are permitted provided that the following conditions 10933707f3Ssthen * are met: 11933707f3Ssthen * 12933707f3Ssthen * Redistributions of source code must retain the above copyright notice, 13933707f3Ssthen * this list of conditions and the following disclaimer. 14933707f3Ssthen * 15933707f3Ssthen * Redistributions in binary form must reproduce the above copyright notice, 16933707f3Ssthen * this list of conditions and the following disclaimer in the documentation 17933707f3Ssthen * and/or other materials provided with the distribution. 18933707f3Ssthen * 19933707f3Ssthen * Neither the name of the NLNET LABS nor the names of its contributors may 20933707f3Ssthen * be used to endorse or promote products derived from this software without 21933707f3Ssthen * specific prior written permission. 22933707f3Ssthen * 23933707f3Ssthen * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 245d76a658Ssthen * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 255d76a658Ssthen * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 265d76a658Ssthen * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 275d76a658Ssthen * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 285d76a658Ssthen * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 295d76a658Ssthen * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 305d76a658Ssthen * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 315d76a658Ssthen * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 325d76a658Ssthen * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 335d76a658Ssthen * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34933707f3Ssthen */ 35933707f3Ssthen 36933707f3Ssthen /** 37933707f3Ssthen * \file 38933707f3Ssthen * 39933707f3Ssthen * This file contains functions to perform network related tasks. 40933707f3Ssthen */ 41933707f3Ssthen 42933707f3Ssthen #ifndef NET_HELP_H 43933707f3Ssthen #define NET_HELP_H 44933707f3Ssthen #include "util/log.h" 45191f22c6Ssthen #include "util/random.h" 46933707f3Ssthen struct sock_list; 47933707f3Ssthen struct regional; 48f6b99bafSsthen struct config_strlist; 49933707f3Ssthen 50933707f3Ssthen /** DNS constants for uint16_t style flag manipulation. host byteorder. 51933707f3Ssthen * 1 1 1 1 1 1 52933707f3Ssthen * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 53933707f3Ssthen * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 54933707f3Ssthen * |QR| Opcode |AA|TC|RD|RA| Z|AD|CD| RCODE | 55933707f3Ssthen * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 56933707f3Ssthen */ 57933707f3Ssthen /** CD flag */ 58933707f3Ssthen #define BIT_CD 0x0010 59933707f3Ssthen /** AD flag */ 60933707f3Ssthen #define BIT_AD 0x0020 61933707f3Ssthen /** Z flag */ 62933707f3Ssthen #define BIT_Z 0x0040 63933707f3Ssthen /** RA flag */ 64933707f3Ssthen #define BIT_RA 0x0080 65933707f3Ssthen /** RD flag */ 66933707f3Ssthen #define BIT_RD 0x0100 67933707f3Ssthen /** TC flag */ 68933707f3Ssthen #define BIT_TC 0x0200 69933707f3Ssthen /** AA flag */ 70933707f3Ssthen #define BIT_AA 0x0400 71933707f3Ssthen /** QR flag */ 72933707f3Ssthen #define BIT_QR 0x8000 73933707f3Ssthen /** get RCODE bits from uint16 flags */ 74933707f3Ssthen #define FLAGS_GET_RCODE(f) ((f) & 0xf) 75933707f3Ssthen /** set RCODE bits in uint16 flags */ 76933707f3Ssthen #define FLAGS_SET_RCODE(f, r) (f = (((f) & 0xfff0) | (r))) 77933707f3Ssthen 7820237c55Ssthen /** timeout in milliseconds for UDP queries to auth servers. */ 7920237c55Ssthen #define UDP_AUTH_QUERY_TIMEOUT 3000 80933707f3Ssthen /** Advertised version of EDNS capabilities */ 81933707f3Ssthen #define EDNS_ADVERTISED_VERSION 0 82933707f3Ssthen /** Advertised size of EDNS capabilities */ 83933707f3Ssthen extern uint16_t EDNS_ADVERTISED_SIZE; 84933707f3Ssthen /** bits for EDNS bitfield */ 85933707f3Ssthen #define EDNS_DO 0x8000 /* Dnssec Ok */ 86933707f3Ssthen /** byte size of ip4 address */ 87933707f3Ssthen #define INET_SIZE 4 88933707f3Ssthen /** byte size of ip6 address */ 89933707f3Ssthen #define INET6_SIZE 16 90933707f3Ssthen 91933707f3Ssthen /** DNSKEY zone sign key flag */ 92933707f3Ssthen #define DNSKEY_BIT_ZSK 0x0100 93933707f3Ssthen /** DNSKEY secure entry point, KSK flag */ 94933707f3Ssthen #define DNSKEY_BIT_SEP 0x0001 95933707f3Ssthen 96191f22c6Ssthen /** return a random 16-bit number given a random source */ 97191f22c6Ssthen #define GET_RANDOM_ID(rnd) (((unsigned)ub_random(rnd)>>8) & 0xffff) 98191f22c6Ssthen 9945872187Ssthen /** define MSG_DONTWAIT for unsupported platforms */ 10045872187Ssthen #ifndef MSG_DONTWAIT 10145872187Ssthen #define MSG_DONTWAIT 0 10245872187Ssthen #endif 10345872187Ssthen 104d8d14d0cSsthen /** minimal responses when positive answer */ 105d8d14d0cSsthen extern int MINIMAL_RESPONSES; 106d8d14d0cSsthen 107d8d14d0cSsthen /** rrset order roundrobin */ 108d8d14d0cSsthen extern int RRSET_ROUNDROBIN; 109d8d14d0cSsthen 110f6b99bafSsthen /** log tag queries with name instead of 'info' for filtering */ 111f6b99bafSsthen extern int LOG_TAG_QUERYREPLY; 112f6b99bafSsthen 113933707f3Ssthen /** 114933707f3Ssthen * See if string is ip4 or ip6. 115933707f3Ssthen * @param str: IP specification. 116933707f3Ssthen * @return: true if string addr is an ip6 specced address. 117933707f3Ssthen */ 118933707f3Ssthen int str_is_ip6(const char* str); 119933707f3Ssthen 120933707f3Ssthen /** 121933707f3Ssthen * Set fd nonblocking. 122933707f3Ssthen * @param s: file descriptor. 123933707f3Ssthen * @return: 0 on error (error is printed to log). 124933707f3Ssthen */ 125933707f3Ssthen int fd_set_nonblock(int s); 126933707f3Ssthen 127933707f3Ssthen /** 128933707f3Ssthen * Set fd (back to) blocking. 129933707f3Ssthen * @param s: file descriptor. 130933707f3Ssthen * @return: 0 on error (error is printed to log). 131933707f3Ssthen */ 132933707f3Ssthen int fd_set_block(int s); 133933707f3Ssthen 134933707f3Ssthen /** 135933707f3Ssthen * See if number is a power of 2. 136933707f3Ssthen * @param num: the value. 137933707f3Ssthen * @return: true if the number is a power of 2. 138933707f3Ssthen */ 139933707f3Ssthen int is_pow2(size_t num); 140933707f3Ssthen 141933707f3Ssthen /** 142933707f3Ssthen * Allocate memory and copy over contents. 143933707f3Ssthen * @param data: what to copy over. 144933707f3Ssthen * @param len: length of data. 145933707f3Ssthen * @return: NULL on malloc failure, or newly malloced data. 146933707f3Ssthen */ 147933707f3Ssthen void* memdup(void* data, size_t len); 148933707f3Ssthen 149933707f3Ssthen /** 150933707f3Ssthen * Prints the sockaddr in readable format with log_info. Debug helper. 151933707f3Ssthen * @param v: at what verbosity level to print this. 152933707f3Ssthen * @param str: descriptive string printed with it. 153933707f3Ssthen * @param addr: the sockaddr to print. Can be ip4 or ip6. 154933707f3Ssthen * @param addrlen: length of addr. 155933707f3Ssthen */ 156933707f3Ssthen void log_addr(enum verbosity_value v, const char* str, 157933707f3Ssthen struct sockaddr_storage* addr, socklen_t addrlen); 158933707f3Ssthen 159933707f3Ssthen /** 160933707f3Ssthen * Prints zone name and sockaddr in readable format with log_info. Debug. 161933707f3Ssthen * @param v: at what verbosity level to print this. 162933707f3Ssthen * @param str: descriptive string printed with it. 163933707f3Ssthen * @param zone: DNS domain name, uncompressed wireformat. 164933707f3Ssthen * @param addr: the sockaddr to print. Can be ip4 or ip6. 165933707f3Ssthen * @param addrlen: length of addr. 166933707f3Ssthen */ 167933707f3Ssthen void log_name_addr(enum verbosity_value v, const char* str, uint8_t* zone, 168933707f3Ssthen struct sockaddr_storage* addr, socklen_t addrlen); 169933707f3Ssthen 170933707f3Ssthen /** 17198f3ca02Sbrad * Log errno and addr. 17298f3ca02Sbrad * @param str: descriptive string printed with it. 17398f3ca02Sbrad * @param err: errno string to print, i.e. strerror(errno). 17498f3ca02Sbrad * @param addr: the sockaddr to print. Can be ip4 or ip6. 17598f3ca02Sbrad * @param addrlen: length of addr. 17698f3ca02Sbrad */ 17798f3ca02Sbrad void log_err_addr(const char* str, const char* err, 17898f3ca02Sbrad struct sockaddr_storage* addr, socklen_t addrlen); 17998f3ca02Sbrad 18098f3ca02Sbrad /** 181933707f3Ssthen * Convert address string, with "@port" appendix, to sockaddr. 182933707f3Ssthen * Uses DNS port by default. 183933707f3Ssthen * @param str: the string 184933707f3Ssthen * @param addr: where to store sockaddr. 185933707f3Ssthen * @param addrlen: length of stored sockaddr is returned. 18645872187Ssthen * @param port: default port. 187933707f3Ssthen * @return 0 on error. 188933707f3Ssthen */ 189933707f3Ssthen int extstrtoaddr(const char* str, struct sockaddr_storage* addr, 19045872187Ssthen socklen_t* addrlen, int port); 191933707f3Ssthen 192933707f3Ssthen /** 193933707f3Ssthen * Convert ip address string and port to sockaddr. 194933707f3Ssthen * @param ip: ip4 or ip6 address string. 195933707f3Ssthen * @param port: port number, host format. 196933707f3Ssthen * @param addr: where to store sockaddr. 197933707f3Ssthen * @param addrlen: length of stored sockaddr is returned. 198933707f3Ssthen * @return 0 on error. 199933707f3Ssthen */ 200933707f3Ssthen int ipstrtoaddr(const char* ip, int port, struct sockaddr_storage* addr, 201933707f3Ssthen socklen_t* addrlen); 202933707f3Ssthen 203933707f3Ssthen /** 204933707f3Ssthen * Convert ip netblock (ip/netsize) string and port to sockaddr. 20520237c55Ssthen * performs a copy internally to avoid writing over 'ip' string. 206933707f3Ssthen * @param ip: ip4 or ip6 address string. 207933707f3Ssthen * @param port: port number, host format. 208933707f3Ssthen * @param addr: where to store sockaddr. 209933707f3Ssthen * @param addrlen: length of stored sockaddr is returned. 210933707f3Ssthen * @param net: netblock size is returned. 211933707f3Ssthen * @return 0 on error. 212933707f3Ssthen */ 213933707f3Ssthen int netblockstrtoaddr(const char* ip, int port, struct sockaddr_storage* addr, 214933707f3Ssthen socklen_t* addrlen, int* net); 215933707f3Ssthen 216933707f3Ssthen /** 21720237c55Ssthen * Convert address string, with "@port" appendix, to sockaddr. 21820237c55Ssthen * It can also have an "#tls-auth-name" appendix (after the port). 219e21c60efSsthen * The returned auth_name string is a pointer into the input string. 220e21c60efSsthen * Uses DNS port by default; TLS port when a "#tls-auth-name" is configured. 22120237c55Ssthen * @param str: the string 22220237c55Ssthen * @param addr: where to store sockaddr. 22320237c55Ssthen * @param addrlen: length of stored sockaddr is returned. 22420237c55Ssthen * @param auth_name: returned pointer to tls_auth_name, or NULL if none. 22520237c55Ssthen * @return 0 on error. 22620237c55Ssthen */ 22720237c55Ssthen int authextstrtoaddr(char* str, struct sockaddr_storage* addr, 22820237c55Ssthen socklen_t* addrlen, char** auth_name); 22920237c55Ssthen 23020237c55Ssthen /** 231e21c60efSsthen * Convert domain string, with "@port" appendix, to dname. 232e21c60efSsthen * It can also have an "#tls-auth-name" appendix (after the port). 233e21c60efSsthen * The return port is the parsed port. 234e21c60efSsthen * Uses DNS port by default; TLS port when a "#tls-auth-name" is configured. 235e21c60efSsthen * The returned auth_name string is a pointer into the input string. 236e21c60efSsthen * @param str: the string 237e21c60efSsthen * @param port: pointer to be assigned the parsed port value. 238e21c60efSsthen * @param auth_name: returned pointer to tls_auth_name, or NULL if none. 239e21c60efSsthen * @return pointer to the dname. 240e21c60efSsthen */ 241e21c60efSsthen uint8_t* authextstrtodname(char* str, int* port, char** auth_name); 242e21c60efSsthen 243e21c60efSsthen /** 244938a3a5eSflorian * Store port number into sockaddr structure 245938a3a5eSflorian * @param addr: sockaddr structure, ip4 or ip6. 246938a3a5eSflorian * @param addrlen: length of addr. 247938a3a5eSflorian * @param port: port number to put into the addr. 248938a3a5eSflorian */ 249938a3a5eSflorian void sockaddr_store_port(struct sockaddr_storage* addr, socklen_t addrlen, 250938a3a5eSflorian int port); 251938a3a5eSflorian 252938a3a5eSflorian /** 253933707f3Ssthen * Print string with neat domain name, type and class. 254933707f3Ssthen * @param v: at what verbosity level to print this. 255933707f3Ssthen * @param str: string of message. 256933707f3Ssthen * @param name: domain name uncompressed wireformat. 257933707f3Ssthen * @param type: host format RR type. 258933707f3Ssthen * @param dclass: host format RR class. 259933707f3Ssthen */ 260933707f3Ssthen void log_nametypeclass(enum verbosity_value v, const char* str, 261933707f3Ssthen uint8_t* name, uint16_t type, uint16_t dclass); 262933707f3Ssthen 263933707f3Ssthen /** 264f6b99bafSsthen * Like log_nametypeclass, but logs with log_query for query logging 265f6b99bafSsthen */ 266f6b99bafSsthen void log_query_in(const char* str, uint8_t* name, uint16_t type, 267f6b99bafSsthen uint16_t dclass); 268f6b99bafSsthen 269f6b99bafSsthen /** 270933707f3Ssthen * Compare two sockaddrs. Imposes an ordering on the addresses. 271933707f3Ssthen * Compares address and port. 272933707f3Ssthen * @param addr1: address 1. 273933707f3Ssthen * @param len1: lengths of addr1. 274933707f3Ssthen * @param addr2: address 2. 275933707f3Ssthen * @param len2: lengths of addr2. 276933707f3Ssthen * @return: 0 if addr1 == addr2. -1 if addr1 is smaller, +1 if larger. 277933707f3Ssthen */ 278933707f3Ssthen int sockaddr_cmp(struct sockaddr_storage* addr1, socklen_t len1, 279933707f3Ssthen struct sockaddr_storage* addr2, socklen_t len2); 280933707f3Ssthen 281933707f3Ssthen /** 282933707f3Ssthen * Compare two sockaddrs. Compares address, not the port. 283933707f3Ssthen * @param addr1: address 1. 284933707f3Ssthen * @param len1: lengths of addr1. 285933707f3Ssthen * @param addr2: address 2. 286933707f3Ssthen * @param len2: lengths of addr2. 287933707f3Ssthen * @return: 0 if addr1 == addr2. -1 if addr1 is smaller, +1 if larger. 288933707f3Ssthen */ 289933707f3Ssthen int sockaddr_cmp_addr(struct sockaddr_storage* addr1, socklen_t len1, 290933707f3Ssthen struct sockaddr_storage* addr2, socklen_t len2); 291933707f3Ssthen 292933707f3Ssthen /** 293933707f3Ssthen * Checkout address family. 294933707f3Ssthen * @param addr: the sockaddr to examine. 295933707f3Ssthen * @param len: the length of addr. 296933707f3Ssthen * @return: true if sockaddr is ip6. 297933707f3Ssthen */ 298933707f3Ssthen int addr_is_ip6(struct sockaddr_storage* addr, socklen_t len); 299933707f3Ssthen 300933707f3Ssthen /** 301933707f3Ssthen * Make sure the sockaddr ends in zeroes. For tree insertion and subsequent 302933707f3Ssthen * comparison. 303933707f3Ssthen * @param addr: the ip4 or ip6 addr. 304933707f3Ssthen * @param len: length of addr. 305933707f3Ssthen * @param net: number of bits to leave untouched, the rest of the netblock 306933707f3Ssthen * address is zeroed. 307933707f3Ssthen */ 308933707f3Ssthen void addr_mask(struct sockaddr_storage* addr, socklen_t len, int net); 309933707f3Ssthen 310933707f3Ssthen /** 311933707f3Ssthen * See how many bits are shared, equal, between two addrs. 312933707f3Ssthen * @param addr1: first addr. 313933707f3Ssthen * @param net1: netblock size of first addr. 314933707f3Ssthen * @param addr2: second addr. 315933707f3Ssthen * @param net2: netblock size of second addr. 316933707f3Ssthen * @param addrlen: length of first addr and of second addr. 317933707f3Ssthen * They must be of the same length (i.e. same type IP4, IP6). 318933707f3Ssthen * @return: number of bits the same. 319933707f3Ssthen */ 320933707f3Ssthen int addr_in_common(struct sockaddr_storage* addr1, int net1, 321933707f3Ssthen struct sockaddr_storage* addr2, int net2, socklen_t addrlen); 322933707f3Ssthen 323933707f3Ssthen /** 324933707f3Ssthen * Put address into string, works for IPv4 and IPv6. 325933707f3Ssthen * @param addr: address 326933707f3Ssthen * @param addrlen: length of address 327933707f3Ssthen * @param buf: result string stored here 328933707f3Ssthen * @param len: length of buf. 329933707f3Ssthen * On failure a string with "error" is stored inside. 330933707f3Ssthen */ 331933707f3Ssthen void addr_to_str(struct sockaddr_storage* addr, socklen_t addrlen, 332933707f3Ssthen char* buf, size_t len); 333933707f3Ssthen 334933707f3Ssthen /** 3358b7325afSsthen * Check if the prefix network length is one of the allowed 32, 40, 48, 56, 64, 3368b7325afSsthen * or 96. 3378b7325afSsthen * @param prefixnet: prefix network length to check. 3388b7325afSsthen * @return 1 on success, 0 on failure. 3398b7325afSsthen */ 3408b7325afSsthen int prefixnet_is_nat64(int prefixnet); 3418b7325afSsthen 3428b7325afSsthen /** 3438b7325afSsthen * Create a NAT64 address from a given address (needs to be IPv4) and a given 3448b7325afSsthen * NAT64 prefix. The NAT64 prefix net needs to be one of 32, 40, 48, 56, 64, 96. 3458b7325afSsthen * @param addr: IPv4 address. 3468b7325afSsthen * @param nat64_prefix: NAT64 prefix. 3478b7325afSsthen * @param nat64_prefixlen: NAT64 prefix len. 3488b7325afSsthen * @param nat64_prefixnet: NAT64 prefix mask. 3498b7325afSsthen * @param nat64_addr: the resulting NAT64 address. 3508b7325afSsthen * @param nat64_addrlen: the resulting NAT64 address length. 3518b7325afSsthen */ 3528b7325afSsthen void addr_to_nat64(const struct sockaddr_storage* addr, 3538b7325afSsthen const struct sockaddr_storage* nat64_prefix, 3548b7325afSsthen socklen_t nat64_prefixlen, int nat64_prefixnet, 3558b7325afSsthen struct sockaddr_storage* nat64_addr, socklen_t* nat64_addrlen); 3568b7325afSsthen 3578b7325afSsthen /** 358933707f3Ssthen * See if sockaddr is an ipv6 mapped ipv4 address, "::ffff:0.0.0.0" 359933707f3Ssthen * @param addr: address 360933707f3Ssthen * @param addrlen: length of address 361933707f3Ssthen * @return true if so 362933707f3Ssthen */ 363933707f3Ssthen int addr_is_ip4mapped(struct sockaddr_storage* addr, socklen_t addrlen); 364933707f3Ssthen 365933707f3Ssthen /** 366*98bc733bSsthen * See if sockaddr is an ipv6 fe80::/10 link local address. 367*98bc733bSsthen * @param addr: address 368*98bc733bSsthen * @param addrlen: length of address 369*98bc733bSsthen * @return true if so 370*98bc733bSsthen */ 371*98bc733bSsthen int addr_is_ip6linklocal(struct sockaddr_storage* addr, socklen_t addrlen); 372*98bc733bSsthen 373*98bc733bSsthen /** 374933707f3Ssthen * See if sockaddr is 255.255.255.255. 375933707f3Ssthen * @param addr: address 376933707f3Ssthen * @param addrlen: length of address 377933707f3Ssthen * @return true if so 378933707f3Ssthen */ 379933707f3Ssthen int addr_is_broadcast(struct sockaddr_storage* addr, socklen_t addrlen); 380933707f3Ssthen 381933707f3Ssthen /** 382933707f3Ssthen * See if sockaddr is 0.0.0.0 or ::0. 383933707f3Ssthen * @param addr: address 384933707f3Ssthen * @param addrlen: length of address 385933707f3Ssthen * @return true if so 386933707f3Ssthen */ 387933707f3Ssthen int addr_is_any(struct sockaddr_storage* addr, socklen_t addrlen); 388933707f3Ssthen 389933707f3Ssthen /** 390933707f3Ssthen * Insert new socket list item. If fails logs error. 391933707f3Ssthen * @param list: pointer to pointer to first item. 392933707f3Ssthen * @param addr: address or NULL if 'cache'. 393933707f3Ssthen * @param len: length of addr, or 0 if 'cache'. 394933707f3Ssthen * @param region: where to allocate 395933707f3Ssthen */ 396933707f3Ssthen void sock_list_insert(struct sock_list** list, struct sockaddr_storage* addr, 397933707f3Ssthen socklen_t len, struct regional* region); 398933707f3Ssthen 399933707f3Ssthen /** 400933707f3Ssthen * Append one list to another. Must both be from same qstate(regional). 401933707f3Ssthen * @param list: pointer to result list that is modified. 402933707f3Ssthen * @param add: item(s) to add. They are prepended to list. 403933707f3Ssthen */ 404933707f3Ssthen void sock_list_prepend(struct sock_list** list, struct sock_list* add); 405933707f3Ssthen 406933707f3Ssthen /** 407933707f3Ssthen * Find addr in list. 408933707f3Ssthen * @param list: to search in 409933707f3Ssthen * @param addr: address to look for. 410933707f3Ssthen * @param len: length. Can be 0, look for 'cache entry'. 411933707f3Ssthen * @return true if found. 412933707f3Ssthen */ 413933707f3Ssthen int sock_list_find(struct sock_list* list, struct sockaddr_storage* addr, 414933707f3Ssthen socklen_t len); 415933707f3Ssthen 416933707f3Ssthen /** 417933707f3Ssthen * Merge socklist into another socket list. Allocates the new entries 418933707f3Ssthen * freshly and copies them over, so also performs a region switchover. 419933707f3Ssthen * Allocation failures are logged. 420933707f3Ssthen * @param list: the destination list (checked for duplicates) 421933707f3Ssthen * @param region: where to allocate 422933707f3Ssthen * @param add: the list of entries to add. 423933707f3Ssthen */ 424933707f3Ssthen void sock_list_merge(struct sock_list** list, struct regional* region, 425933707f3Ssthen struct sock_list* add); 426933707f3Ssthen 427933707f3Ssthen /** 428933707f3Ssthen * Log libcrypto error with descriptive string. Calls log_err(). 429933707f3Ssthen * @param str: what failed. 430933707f3Ssthen */ 431933707f3Ssthen void log_crypto_err(const char* str); 432933707f3Ssthen 433933707f3Ssthen /** 434ebf5bb73Ssthen * Log libcrypto error from errcode with descriptive string, calls log_err. 435ebf5bb73Ssthen * @param str: what failed. 436ebf5bb73Ssthen * @param err: error code from ERR_get_error. 437ebf5bb73Ssthen */ 438ebf5bb73Ssthen void log_crypto_err_code(const char* str, unsigned long err); 439ebf5bb73Ssthen 440ebf5bb73Ssthen /** 441d896b962Ssthen * Log an error from libcrypto that came from SSL_write and so on, with 442d896b962Ssthen * a value from SSL_get_error, calls log_err. If that fails it logs with 443d896b962Ssthen * log_crypto_err. 444d896b962Ssthen * @param str: what failed 445d896b962Ssthen * @param r: output of SSL_get_error on the I/O operation result. 446d896b962Ssthen */ 447d896b962Ssthen void log_crypto_err_io(const char* str, int r); 448d896b962Ssthen 449d896b962Ssthen /** 450d896b962Ssthen * Log an error from libcrypt that came from an I/O routine with the 451d896b962Ssthen * errcode from ERR_get_error. Calls log_err() and log_crypto_err_code. 452d896b962Ssthen * @param str: what failed 453d896b962Ssthen * @param r: output of SSL_get_error on the I/O operation result. 454d896b962Ssthen * @param err: error code from ERR_get_error 455d896b962Ssthen */ 456d896b962Ssthen void log_crypto_err_io_code(const char* str, int r, unsigned long err); 457d896b962Ssthen 458d896b962Ssthen /** 459a3167c07Ssthen * Log certificate details verbosity, string, of X509 cert 460a3167c07Ssthen * @param level: verbosity level 461a3167c07Ssthen * @param str: string to prefix on output 462a3167c07Ssthen * @param cert: X509* structure. 463a3167c07Ssthen */ 464a3167c07Ssthen void log_cert(unsigned level, const char* str, void* cert); 465a3167c07Ssthen 466a3167c07Ssthen /** 4677191de28Ssthen * Set SSL_OP_NOxxx options on SSL context to disable bad crypto 4687191de28Ssthen * @param ctxt: SSL_CTX* 4697191de28Ssthen * @return false on failure. 4707191de28Ssthen */ 4717191de28Ssthen int listen_sslctx_setup(void* ctxt); 4727191de28Ssthen 4737191de28Ssthen /** 4747191de28Ssthen * Further setup of listening SSL context, after keys loaded. 4757191de28Ssthen * @param ctxt: SSL_CTX* 4767191de28Ssthen */ 4777191de28Ssthen void listen_sslctx_setup_2(void* ctxt); 4787191de28Ssthen 4797191de28Ssthen /** 480933707f3Ssthen * create SSL listen context 481933707f3Ssthen * @param key: private key file. 482933707f3Ssthen * @param pem: public key cert. 483933707f3Ssthen * @param verifypem: if nonNULL, verifylocation file. 484933707f3Ssthen * return SSL_CTX* or NULL on failure (logged). 485933707f3Ssthen */ 486933707f3Ssthen void* listen_sslctx_create(char* key, char* pem, char* verifypem); 487933707f3Ssthen 488933707f3Ssthen /** 489933707f3Ssthen * create SSL connect context 490933707f3Ssthen * @param key: if nonNULL (also pem nonNULL), the client private key. 491933707f3Ssthen * @param pem: client public key (or NULL if key is NULL). 492933707f3Ssthen * @param verifypem: if nonNULL used for verifylocation file. 49320237c55Ssthen * @param wincert: add system certificate store to ctx (add to verifypem ca 49420237c55Ssthen * certs). 495933707f3Ssthen * @return SSL_CTX* or NULL on failure (logged). 496933707f3Ssthen */ 49720237c55Ssthen void* connect_sslctx_create(char* key, char* pem, char* verifypem, int wincert); 498933707f3Ssthen 499933707f3Ssthen /** 500933707f3Ssthen * accept a new fd and wrap it in a BIO in SSL 501933707f3Ssthen * @param sslctx: the SSL_CTX to use (from listen_sslctx_create()). 502933707f3Ssthen * @param fd: from accept, nonblocking. 503933707f3Ssthen * @return SSL or NULL on alloc failure. 504933707f3Ssthen */ 505933707f3Ssthen void* incoming_ssl_fd(void* sslctx, int fd); 506933707f3Ssthen 507933707f3Ssthen /** 508933707f3Ssthen * connect a new fd and wrap it in a BIO in SSL 509933707f3Ssthen * @param sslctx: the SSL_CTX to use (from connect_sslctx_create()) 510933707f3Ssthen * @param fd: from connect. 511933707f3Ssthen * @return SSL or NULL on alloc failure 512933707f3Ssthen */ 513933707f3Ssthen void* outgoing_ssl_fd(void* sslctx, int fd); 514933707f3Ssthen 515229e174cSsthen /** 516a3167c07Ssthen * check if authname SSL functionality is available, false if not 517a3167c07Ssthen * @param auth_name: the name for the remote server, used for error print. 518a3167c07Ssthen * @return false if SSL functionality to check the SSL name is not available. 519a3167c07Ssthen */ 520a3167c07Ssthen int check_auth_name_for_ssl(char* auth_name); 521a3167c07Ssthen 522a3167c07Ssthen /** 523a3167c07Ssthen * set auth name on SSL for verification 524a3167c07Ssthen * @param ssl: SSL* to set 525a3167c07Ssthen * @param auth_name: if NULL nothing happens, otherwise the name to check. 526a3167c07Ssthen * @param use_sni: if SNI will be used. 527a3167c07Ssthen * @return 1 on success or NULL auth_name, 0 on failure. 528a3167c07Ssthen */ 529a3167c07Ssthen int set_auth_name_on_ssl(void* ssl, char* auth_name, int use_sni); 530a3167c07Ssthen 531a3167c07Ssthen /** 532229e174cSsthen * Initialize openssl locking for thread safety 533229e174cSsthen * @return false on failure (alloc failure). 534229e174cSsthen */ 535229e174cSsthen int ub_openssl_lock_init(void); 536229e174cSsthen 537229e174cSsthen /** 538229e174cSsthen * De-init the allocated openssl locks 539229e174cSsthen */ 540229e174cSsthen void ub_openssl_lock_delete(void); 541229e174cSsthen 542f6b99bafSsthen /** 543f6b99bafSsthen * setup TLS session ticket 544f6b99bafSsthen * @param sslctx: the SSL_CTX to use (from connect_sslctx_create()) 545f6b99bafSsthen * @param tls_session_ticket_keys: TLS ticket secret filenames 546f6b99bafSsthen * @return false on failure (alloc failure). 547f6b99bafSsthen */ 548f6b99bafSsthen int listen_sslctx_setup_ticket_keys(void* sslctx, 549f6b99bafSsthen struct config_strlist* tls_session_ticket_keys); 550f6b99bafSsthen 551f6b99bafSsthen /** Free memory used for TLS session ticket keys */ 552f6b99bafSsthen void listen_sslctx_delete_ticket_keys(void); 553f6b99bafSsthen 554eaf2578eSsthen /** 555eaf2578eSsthen * RPZ format netblock to network byte order address and netblock 556eaf2578eSsthen * example RPZ netblock format dnames: 557eaf2578eSsthen * - 24.10.100.51.198.rpz-ip -> 198.51.100.10/24 558eaf2578eSsthen * - 32.10.zz.db8.2001.rpz-ip -> 2001:db8:0:0:0:0:0:10/32 559eaf2578eSsthen * @param dname: the dname containing RPZ format netblock 560eaf2578eSsthen * @param dnamelen: length of dname 561eaf2578eSsthen * @param addr: where to store sockaddr. 562eaf2578eSsthen * @param addrlen: length of stored sockaddr is returned. 563eaf2578eSsthen * @param net: where to store netmask 564eaf2578eSsthen * @param af: where to store address family. 565eaf2578eSsthen * @return 0 on error. 566eaf2578eSsthen */ 567eaf2578eSsthen int netblockdnametoaddr(uint8_t* dname, size_t dnamelen, 568eaf2578eSsthen struct sockaddr_storage* addr, socklen_t* addrlen, int* net, int* af); 5692c144df0Ssthen 5702c144df0Ssthen /** Return strerror or wsastrerror for socket error printout */ 5712c144df0Ssthen char* sock_strerror(int errn); 5722c144df0Ssthen /** close the socket with close, or wsa closesocket */ 5732c144df0Ssthen void sock_close(int socket); 5742c144df0Ssthen 575*98bc733bSsthen /** 576*98bc733bSsthen * Convert binary data to a string of hexadecimal characters. 577*98bc733bSsthen */ 578*98bc733bSsthen ssize_t hex_ntop(uint8_t const *src, size_t srclength, char *target, 579*98bc733bSsthen size_t targsize); 580*98bc733bSsthen 581*98bc733bSsthen /** Convert hexadecimal data to binary. */ 582*98bc733bSsthen ssize_t hex_pton(const char* src, uint8_t* target, size_t targsize); 583*98bc733bSsthen 584933707f3Ssthen #endif /* NET_HELP_H */ 585