1 1.1 christos /* 2 1.1 christos * util/net_help.h - network help functions 3 1.1 christos * 4 1.1 christos * Copyright (c) 2007, NLnet Labs. All rights reserved. 5 1.1 christos * 6 1.1 christos * This software is open source. 7 1.1 christos * 8 1.1 christos * Redistribution and use in source and binary forms, with or without 9 1.1 christos * modification, are permitted provided that the following conditions 10 1.1 christos * are met: 11 1.1 christos * 12 1.1 christos * Redistributions of source code must retain the above copyright notice, 13 1.1 christos * this list of conditions and the following disclaimer. 14 1.1 christos * 15 1.1 christos * Redistributions in binary form must reproduce the above copyright notice, 16 1.1 christos * this list of conditions and the following disclaimer in the documentation 17 1.1 christos * and/or other materials provided with the distribution. 18 1.1 christos * 19 1.1 christos * Neither the name of the NLNET LABS nor the names of its contributors may 20 1.1 christos * be used to endorse or promote products derived from this software without 21 1.1 christos * specific prior written permission. 22 1.1 christos * 23 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 1.1 christos * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 1.1 christos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29 1.1 christos * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 1.1 christos * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 1.1 christos * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 1.1 christos * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 1.1 christos * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 1.1 christos */ 35 1.1 christos 36 1.1 christos /** 37 1.1 christos * \file 38 1.1 christos * 39 1.1 christos * This file contains functions to perform network related tasks. 40 1.1 christos */ 41 1.1 christos 42 1.1 christos #ifndef NET_HELP_H 43 1.1 christos #define NET_HELP_H 44 1.1 christos #include "util/log.h" 45 1.1.1.7 christos #include "util/random.h" 46 1.1 christos struct sock_list; 47 1.1 christos struct regional; 48 1.1.1.4 christos struct config_strlist; 49 1.1 christos 50 1.1 christos /** DNS constants for uint16_t style flag manipulation. host byteorder. 51 1.1 christos * 1 1 1 1 1 1 52 1.1 christos * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 53 1.1 christos * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 54 1.1 christos * |QR| Opcode |AA|TC|RD|RA| Z|AD|CD| RCODE | 55 1.1 christos * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 56 1.1 christos */ 57 1.1 christos /** CD flag */ 58 1.1 christos #define BIT_CD 0x0010 59 1.1 christos /** AD flag */ 60 1.1 christos #define BIT_AD 0x0020 61 1.1 christos /** Z flag */ 62 1.1 christos #define BIT_Z 0x0040 63 1.1 christos /** RA flag */ 64 1.1 christos #define BIT_RA 0x0080 65 1.1 christos /** RD flag */ 66 1.1 christos #define BIT_RD 0x0100 67 1.1 christos /** TC flag */ 68 1.1 christos #define BIT_TC 0x0200 69 1.1 christos /** AA flag */ 70 1.1 christos #define BIT_AA 0x0400 71 1.1 christos /** QR flag */ 72 1.1 christos #define BIT_QR 0x8000 73 1.1 christos /** get RCODE bits from uint16 flags */ 74 1.1 christos #define FLAGS_GET_RCODE(f) ((f) & 0xf) 75 1.1 christos /** set RCODE bits in uint16 flags */ 76 1.1 christos #define FLAGS_SET_RCODE(f, r) (f = (((f) & 0xfff0) | (r))) 77 1.1 christos 78 1.1.1.3 christos /** timeout in milliseconds for UDP queries to auth servers. */ 79 1.1.1.3 christos #define UDP_AUTH_QUERY_TIMEOUT 3000 80 1.1 christos /** Advertised version of EDNS capabilities */ 81 1.1 christos #define EDNS_ADVERTISED_VERSION 0 82 1.1 christos /** Advertised size of EDNS capabilities */ 83 1.1 christos extern uint16_t EDNS_ADVERTISED_SIZE; 84 1.1 christos /** bits for EDNS bitfield */ 85 1.1 christos #define EDNS_DO 0x8000 /* Dnssec Ok */ 86 1.1 christos /** byte size of ip4 address */ 87 1.1 christos #define INET_SIZE 4 88 1.1 christos /** byte size of ip6 address */ 89 1.1 christos #define INET6_SIZE 16 90 1.1 christos 91 1.1 christos /** DNSKEY zone sign key flag */ 92 1.1 christos #define DNSKEY_BIT_ZSK 0x0100 93 1.1 christos /** DNSKEY secure entry point, KSK flag */ 94 1.1 christos #define DNSKEY_BIT_SEP 0x0001 95 1.1 christos 96 1.1.1.7 christos /** return a random 16-bit number given a random source */ 97 1.1.1.7 christos #define GET_RANDOM_ID(rnd) (((unsigned)ub_random(rnd)>>8) & 0xffff) 98 1.1.1.7 christos 99 1.1.1.8 christos /** define MSG_DONTWAIT for unsupported platforms */ 100 1.1.1.8 christos #ifndef MSG_DONTWAIT 101 1.1.1.8 christos #define MSG_DONTWAIT 0 102 1.1.1.8 christos #endif 103 1.1.1.8 christos 104 1.1 christos /** minimal responses when positive answer */ 105 1.1 christos extern int MINIMAL_RESPONSES; 106 1.1 christos 107 1.1 christos /** rrset order roundrobin */ 108 1.1 christos extern int RRSET_ROUNDROBIN; 109 1.1 christos 110 1.1.1.4 christos /** log tag queries with name instead of 'info' for filtering */ 111 1.1.1.4 christos extern int LOG_TAG_QUERYREPLY; 112 1.1.1.4 christos 113 1.1 christos /** 114 1.1 christos * See if string is ip4 or ip6. 115 1.1 christos * @param str: IP specification. 116 1.1 christos * @return: true if string addr is an ip6 specced address. 117 1.1 christos */ 118 1.1 christos int str_is_ip6(const char* str); 119 1.1 christos 120 1.1 christos /** 121 1.1 christos * Set fd nonblocking. 122 1.1 christos * @param s: file descriptor. 123 1.1 christos * @return: 0 on error (error is printed to log). 124 1.1 christos */ 125 1.1 christos int fd_set_nonblock(int s); 126 1.1 christos 127 1.1 christos /** 128 1.1 christos * Set fd (back to) blocking. 129 1.1 christos * @param s: file descriptor. 130 1.1 christos * @return: 0 on error (error is printed to log). 131 1.1 christos */ 132 1.1 christos int fd_set_block(int s); 133 1.1 christos 134 1.1 christos /** 135 1.1 christos * See if number is a power of 2. 136 1.1 christos * @param num: the value. 137 1.1 christos * @return: true if the number is a power of 2. 138 1.1 christos */ 139 1.1 christos int is_pow2(size_t num); 140 1.1 christos 141 1.1 christos /** 142 1.1 christos * Allocate memory and copy over contents. 143 1.1 christos * @param data: what to copy over. 144 1.1 christos * @param len: length of data. 145 1.1 christos * @return: NULL on malloc failure, or newly malloced data. 146 1.1 christos */ 147 1.1 christos void* memdup(void* data, size_t len); 148 1.1 christos 149 1.1 christos /** 150 1.1 christos * Prints the sockaddr in readable format with log_info. Debug helper. 151 1.1 christos * @param v: at what verbosity level to print this. 152 1.1 christos * @param str: descriptive string printed with it. 153 1.1 christos * @param addr: the sockaddr to print. Can be ip4 or ip6. 154 1.1 christos * @param addrlen: length of addr. 155 1.1 christos */ 156 1.1 christos void log_addr(enum verbosity_value v, const char* str, 157 1.1 christos struct sockaddr_storage* addr, socklen_t addrlen); 158 1.1 christos 159 1.1 christos /** 160 1.1 christos * Prints zone name and sockaddr in readable format with log_info. Debug. 161 1.1 christos * @param v: at what verbosity level to print this. 162 1.1 christos * @param str: descriptive string printed with it. 163 1.1 christos * @param zone: DNS domain name, uncompressed wireformat. 164 1.1 christos * @param addr: the sockaddr to print. Can be ip4 or ip6. 165 1.1 christos * @param addrlen: length of addr. 166 1.1 christos */ 167 1.1 christos void log_name_addr(enum verbosity_value v, const char* str, uint8_t* zone, 168 1.1 christos struct sockaddr_storage* addr, socklen_t addrlen); 169 1.1 christos 170 1.1 christos /** 171 1.1 christos * Log errno and addr. 172 1.1 christos * @param str: descriptive string printed with it. 173 1.1 christos * @param err: errno string to print, i.e. strerror(errno). 174 1.1 christos * @param addr: the sockaddr to print. Can be ip4 or ip6. 175 1.1 christos * @param addrlen: length of addr. 176 1.1 christos */ 177 1.1 christos void log_err_addr(const char* str, const char* err, 178 1.1 christos struct sockaddr_storage* addr, socklen_t addrlen); 179 1.1 christos 180 1.1 christos /** 181 1.1 christos * Convert address string, with "@port" appendix, to sockaddr. 182 1.1 christos * Uses DNS port by default. 183 1.1 christos * @param str: the string 184 1.1 christos * @param addr: where to store sockaddr. 185 1.1 christos * @param addrlen: length of stored sockaddr is returned. 186 1.1.1.8 christos * @param port: default port. 187 1.1 christos * @return 0 on error. 188 1.1 christos */ 189 1.1 christos int extstrtoaddr(const char* str, struct sockaddr_storage* addr, 190 1.1.1.8 christos socklen_t* addrlen, int port); 191 1.1 christos 192 1.1 christos /** 193 1.1 christos * Convert ip address string and port to sockaddr. 194 1.1 christos * @param ip: ip4 or ip6 address string. 195 1.1 christos * @param port: port number, host format. 196 1.1 christos * @param addr: where to store sockaddr. 197 1.1 christos * @param addrlen: length of stored sockaddr is returned. 198 1.1 christos * @return 0 on error. 199 1.1 christos */ 200 1.1 christos int ipstrtoaddr(const char* ip, int port, struct sockaddr_storage* addr, 201 1.1 christos socklen_t* addrlen); 202 1.1 christos 203 1.1 christos /** 204 1.1 christos * Convert ip netblock (ip/netsize) string and port to sockaddr. 205 1.1.1.3 christos * performs a copy internally to avoid writing over 'ip' string. 206 1.1 christos * @param ip: ip4 or ip6 address string. 207 1.1 christos * @param port: port number, host format. 208 1.1 christos * @param addr: where to store sockaddr. 209 1.1 christos * @param addrlen: length of stored sockaddr is returned. 210 1.1 christos * @param net: netblock size is returned. 211 1.1 christos * @return 0 on error. 212 1.1 christos */ 213 1.1 christos int netblockstrtoaddr(const char* ip, int port, struct sockaddr_storage* addr, 214 1.1 christos socklen_t* addrlen, int* net); 215 1.1 christos 216 1.1 christos /** 217 1.1.1.3 christos * Convert address string, with "@port" appendix, to sockaddr. 218 1.1.1.3 christos * It can also have an "#tls-auth-name" appendix (after the port). 219 1.1.1.7 christos * The returned auth_name string is a pointer into the input string. 220 1.1.1.7 christos * Uses DNS port by default; TLS port when a "#tls-auth-name" is configured. 221 1.1.1.3 christos * @param str: the string 222 1.1.1.3 christos * @param addr: where to store sockaddr. 223 1.1.1.3 christos * @param addrlen: length of stored sockaddr is returned. 224 1.1.1.3 christos * @param auth_name: returned pointer to tls_auth_name, or NULL if none. 225 1.1.1.3 christos * @return 0 on error. 226 1.1.1.3 christos */ 227 1.1.1.7 christos int authextstrtoaddr(char* str, struct sockaddr_storage* addr, 228 1.1.1.3 christos socklen_t* addrlen, char** auth_name); 229 1.1.1.3 christos 230 1.1.1.3 christos /** 231 1.1.1.7 christos * Convert domain string, with "@port" appendix, to dname. 232 1.1.1.7 christos * It can also have an "#tls-auth-name" appendix (after the port). 233 1.1.1.7 christos * The return port is the parsed port. 234 1.1.1.7 christos * Uses DNS port by default; TLS port when a "#tls-auth-name" is configured. 235 1.1.1.7 christos * The returned auth_name string is a pointer into the input string. 236 1.1.1.7 christos * @param str: the string 237 1.1.1.7 christos * @param port: pointer to be assigned the parsed port value. 238 1.1.1.7 christos * @param auth_name: returned pointer to tls_auth_name, or NULL if none. 239 1.1.1.7 christos * @return pointer to the dname. 240 1.1.1.7 christos */ 241 1.1.1.7 christos uint8_t* authextstrtodname(char* str, int* port, char** auth_name); 242 1.1.1.7 christos 243 1.1.1.7 christos /** 244 1.1.1.3 christos * Store port number into sockaddr structure 245 1.1.1.3 christos * @param addr: sockaddr structure, ip4 or ip6. 246 1.1.1.3 christos * @param addrlen: length of addr. 247 1.1.1.3 christos * @param port: port number to put into the addr. 248 1.1.1.3 christos */ 249 1.1.1.3 christos void sockaddr_store_port(struct sockaddr_storage* addr, socklen_t addrlen, 250 1.1.1.3 christos int port); 251 1.1.1.3 christos 252 1.1.1.3 christos /** 253 1.1 christos * Print string with neat domain name, type and class. 254 1.1 christos * @param v: at what verbosity level to print this. 255 1.1 christos * @param str: string of message. 256 1.1 christos * @param name: domain name uncompressed wireformat. 257 1.1 christos * @param type: host format RR type. 258 1.1 christos * @param dclass: host format RR class. 259 1.1 christos */ 260 1.1 christos void log_nametypeclass(enum verbosity_value v, const char* str, 261 1.1 christos uint8_t* name, uint16_t type, uint16_t dclass); 262 1.1 christos 263 1.1 christos /** 264 1.1.1.4 christos * Like log_nametypeclass, but logs with log_query for query logging 265 1.1.1.4 christos */ 266 1.1.1.4 christos void log_query_in(const char* str, uint8_t* name, uint16_t type, 267 1.1.1.4 christos uint16_t dclass); 268 1.1.1.4 christos 269 1.1.1.4 christos /** 270 1.1 christos * Compare two sockaddrs. Imposes an ordering on the addresses. 271 1.1 christos * Compares address and port. 272 1.1 christos * @param addr1: address 1. 273 1.1 christos * @param len1: lengths of addr1. 274 1.1 christos * @param addr2: address 2. 275 1.1 christos * @param len2: lengths of addr2. 276 1.1 christos * @return: 0 if addr1 == addr2. -1 if addr1 is smaller, +1 if larger. 277 1.1 christos */ 278 1.1 christos int sockaddr_cmp(struct sockaddr_storage* addr1, socklen_t len1, 279 1.1 christos struct sockaddr_storage* addr2, socklen_t len2); 280 1.1 christos 281 1.1 christos /** 282 1.1 christos * Compare two sockaddrs. Compares address, not the port. 283 1.1 christos * @param addr1: address 1. 284 1.1 christos * @param len1: lengths of addr1. 285 1.1 christos * @param addr2: address 2. 286 1.1 christos * @param len2: lengths of addr2. 287 1.1 christos * @return: 0 if addr1 == addr2. -1 if addr1 is smaller, +1 if larger. 288 1.1 christos */ 289 1.1 christos int sockaddr_cmp_addr(struct sockaddr_storage* addr1, socklen_t len1, 290 1.1 christos struct sockaddr_storage* addr2, socklen_t len2); 291 1.1 christos 292 1.1 christos /** 293 1.1.1.9 christos * Compare two sockaddrs. Imposes an ordering on the addresses. 294 1.1.1.9 christos * Compares address and port. It also compares scope_id for ip6. 295 1.1.1.9 christos * @param addr1: address 1. 296 1.1.1.9 christos * @param len1: lengths of addr1. 297 1.1.1.9 christos * @param addr2: address 2. 298 1.1.1.9 christos * @param len2: lengths of addr2. 299 1.1.1.9 christos * @return: 0 if addr1 == addr2. -1 if addr1 is smaller, +1 if larger. 300 1.1.1.9 christos */ 301 1.1.1.9 christos int sockaddr_cmp_scopeid(struct sockaddr_storage* addr1, socklen_t len1, 302 1.1.1.9 christos struct sockaddr_storage* addr2, socklen_t len2); 303 1.1.1.9 christos 304 1.1.1.9 christos /** 305 1.1 christos * Checkout address family. 306 1.1 christos * @param addr: the sockaddr to examine. 307 1.1 christos * @param len: the length of addr. 308 1.1 christos * @return: true if sockaddr is ip6. 309 1.1 christos */ 310 1.1.1.10 christos int addr_is_ip6(const struct sockaddr_storage* addr, socklen_t len); 311 1.1 christos 312 1.1 christos /** 313 1.1 christos * Make sure the sockaddr ends in zeroes. For tree insertion and subsequent 314 1.1 christos * comparison. 315 1.1 christos * @param addr: the ip4 or ip6 addr. 316 1.1 christos * @param len: length of addr. 317 1.1 christos * @param net: number of bits to leave untouched, the rest of the netblock 318 1.1 christos * address is zeroed. 319 1.1 christos */ 320 1.1 christos void addr_mask(struct sockaddr_storage* addr, socklen_t len, int net); 321 1.1 christos 322 1.1 christos /** 323 1.1 christos * See how many bits are shared, equal, between two addrs. 324 1.1 christos * @param addr1: first addr. 325 1.1 christos * @param net1: netblock size of first addr. 326 1.1 christos * @param addr2: second addr. 327 1.1 christos * @param net2: netblock size of second addr. 328 1.1 christos * @param addrlen: length of first addr and of second addr. 329 1.1 christos * They must be of the same length (i.e. same type IP4, IP6). 330 1.1 christos * @return: number of bits the same. 331 1.1 christos */ 332 1.1 christos int addr_in_common(struct sockaddr_storage* addr1, int net1, 333 1.1 christos struct sockaddr_storage* addr2, int net2, socklen_t addrlen); 334 1.1 christos 335 1.1 christos /** 336 1.1 christos * Put address into string, works for IPv4 and IPv6. 337 1.1 christos * @param addr: address 338 1.1 christos * @param addrlen: length of address 339 1.1 christos * @param buf: result string stored here 340 1.1 christos * @param len: length of buf. 341 1.1 christos * On failure a string with "error" is stored inside. 342 1.1 christos */ 343 1.1 christos void addr_to_str(struct sockaddr_storage* addr, socklen_t addrlen, 344 1.1 christos char* buf, size_t len); 345 1.1 christos 346 1.1 christos /** 347 1.1.1.8 christos * Check if the prefix network length is one of the allowed 32, 40, 48, 56, 64, 348 1.1.1.8 christos * or 96. 349 1.1.1.8 christos * @param prefixnet: prefix network length to check. 350 1.1.1.8 christos * @return 1 on success, 0 on failure. 351 1.1.1.8 christos */ 352 1.1.1.8 christos int prefixnet_is_nat64(int prefixnet); 353 1.1.1.8 christos 354 1.1.1.8 christos /** 355 1.1.1.8 christos * Create a NAT64 address from a given address (needs to be IPv4) and a given 356 1.1.1.8 christos * NAT64 prefix. The NAT64 prefix net needs to be one of 32, 40, 48, 56, 64, 96. 357 1.1.1.8 christos * @param addr: IPv4 address. 358 1.1.1.8 christos * @param nat64_prefix: NAT64 prefix. 359 1.1.1.8 christos * @param nat64_prefixlen: NAT64 prefix len. 360 1.1.1.8 christos * @param nat64_prefixnet: NAT64 prefix mask. 361 1.1.1.8 christos * @param nat64_addr: the resulting NAT64 address. 362 1.1.1.8 christos * @param nat64_addrlen: the resulting NAT64 address length. 363 1.1.1.8 christos */ 364 1.1.1.8 christos void addr_to_nat64(const struct sockaddr_storage* addr, 365 1.1.1.8 christos const struct sockaddr_storage* nat64_prefix, 366 1.1.1.8 christos socklen_t nat64_prefixlen, int nat64_prefixnet, 367 1.1.1.8 christos struct sockaddr_storage* nat64_addr, socklen_t* nat64_addrlen); 368 1.1.1.8 christos 369 1.1.1.8 christos /** 370 1.1 christos * See if sockaddr is an ipv6 mapped ipv4 address, "::ffff:0.0.0.0" 371 1.1 christos * @param addr: address 372 1.1 christos * @param addrlen: length of address 373 1.1 christos * @return true if so 374 1.1 christos */ 375 1.1 christos int addr_is_ip4mapped(struct sockaddr_storage* addr, socklen_t addrlen); 376 1.1 christos 377 1.1 christos /** 378 1.1.1.9 christos * See if sockaddr is an ipv6 fe80::/10 link local address. 379 1.1.1.9 christos * @param addr: address 380 1.1.1.9 christos * @param addrlen: length of address 381 1.1.1.9 christos * @return true if so 382 1.1.1.9 christos */ 383 1.1.1.9 christos int addr_is_ip6linklocal(struct sockaddr_storage* addr, socklen_t addrlen); 384 1.1.1.9 christos 385 1.1.1.9 christos /** 386 1.1 christos * See if sockaddr is 255.255.255.255. 387 1.1 christos * @param addr: address 388 1.1 christos * @param addrlen: length of address 389 1.1 christos * @return true if so 390 1.1 christos */ 391 1.1 christos int addr_is_broadcast(struct sockaddr_storage* addr, socklen_t addrlen); 392 1.1 christos 393 1.1 christos /** 394 1.1 christos * See if sockaddr is 0.0.0.0 or ::0. 395 1.1 christos * @param addr: address 396 1.1 christos * @param addrlen: length of address 397 1.1 christos * @return true if so 398 1.1 christos */ 399 1.1 christos int addr_is_any(struct sockaddr_storage* addr, socklen_t addrlen); 400 1.1 christos 401 1.1 christos /** 402 1.1 christos * Insert new socket list item. If fails logs error. 403 1.1 christos * @param list: pointer to pointer to first item. 404 1.1 christos * @param addr: address or NULL if 'cache'. 405 1.1 christos * @param len: length of addr, or 0 if 'cache'. 406 1.1 christos * @param region: where to allocate 407 1.1 christos */ 408 1.1 christos void sock_list_insert(struct sock_list** list, struct sockaddr_storage* addr, 409 1.1 christos socklen_t len, struct regional* region); 410 1.1 christos 411 1.1 christos /** 412 1.1 christos * Append one list to another. Must both be from same qstate(regional). 413 1.1 christos * @param list: pointer to result list that is modified. 414 1.1 christos * @param add: item(s) to add. They are prepended to list. 415 1.1 christos */ 416 1.1 christos void sock_list_prepend(struct sock_list** list, struct sock_list* add); 417 1.1 christos 418 1.1 christos /** 419 1.1 christos * Find addr in list. 420 1.1 christos * @param list: to search in 421 1.1 christos * @param addr: address to look for. 422 1.1 christos * @param len: length. Can be 0, look for 'cache entry'. 423 1.1 christos * @return true if found. 424 1.1 christos */ 425 1.1 christos int sock_list_find(struct sock_list* list, struct sockaddr_storage* addr, 426 1.1 christos socklen_t len); 427 1.1 christos 428 1.1 christos /** 429 1.1 christos * Merge socklist into another socket list. Allocates the new entries 430 1.1 christos * freshly and copies them over, so also performs a region switchover. 431 1.1 christos * Allocation failures are logged. 432 1.1 christos * @param list: the destination list (checked for duplicates) 433 1.1 christos * @param region: where to allocate 434 1.1 christos * @param add: the list of entries to add. 435 1.1 christos */ 436 1.1 christos void sock_list_merge(struct sock_list** list, struct regional* region, 437 1.1 christos struct sock_list* add); 438 1.1 christos 439 1.1 christos /** 440 1.1 christos * Log libcrypto error with descriptive string. Calls log_err(). 441 1.1 christos * @param str: what failed. 442 1.1 christos */ 443 1.1 christos void log_crypto_err(const char* str); 444 1.1 christos 445 1.1.1.2 christos /** 446 1.1.1.5 christos * Log libcrypto error from errcode with descriptive string, calls log_err. 447 1.1.1.5 christos * @param str: what failed. 448 1.1.1.5 christos * @param err: error code from ERR_get_error. 449 1.1.1.5 christos */ 450 1.1.1.5 christos void log_crypto_err_code(const char* str, unsigned long err); 451 1.1.1.5 christos 452 1.1.1.5 christos /** 453 1.1.1.8 christos * Log an error from libcrypto that came from SSL_write and so on, with 454 1.1.1.8 christos * a value from SSL_get_error, calls log_err. If that fails it logs with 455 1.1.1.8 christos * log_crypto_err. 456 1.1.1.8 christos * @param str: what failed 457 1.1.1.8 christos * @param r: output of SSL_get_error on the I/O operation result. 458 1.1.1.8 christos */ 459 1.1.1.8 christos void log_crypto_err_io(const char* str, int r); 460 1.1.1.8 christos 461 1.1.1.8 christos /** 462 1.1.1.8 christos * Log an error from libcrypt that came from an I/O routine with the 463 1.1.1.8 christos * errcode from ERR_get_error. Calls log_err() and log_crypto_err_code. 464 1.1.1.8 christos * @param str: what failed 465 1.1.1.8 christos * @param r: output of SSL_get_error on the I/O operation result. 466 1.1.1.8 christos * @param err: error code from ERR_get_error 467 1.1.1.8 christos */ 468 1.1.1.8 christos void log_crypto_err_io_code(const char* str, int r, unsigned long err); 469 1.1.1.8 christos 470 1.1.1.8 christos /** 471 1.1.1.6 christos * Log certificate details verbosity, string, of X509 cert 472 1.1.1.6 christos * @param level: verbosity level 473 1.1.1.6 christos * @param str: string to prefix on output 474 1.1.1.6 christos * @param cert: X509* structure. 475 1.1.1.6 christos */ 476 1.1.1.6 christos void log_cert(unsigned level, const char* str, void* cert); 477 1.1.1.6 christos 478 1.1.1.6 christos /** 479 1.1.1.2 christos * Set SSL_OP_NOxxx options on SSL context to disable bad crypto 480 1.1.1.2 christos * @param ctxt: SSL_CTX* 481 1.1.1.10 christos * @param tls_protocols: configure string with allowed TLS protocols to use. 482 1.1.1.2 christos * @return false on failure. 483 1.1.1.2 christos */ 484 1.1.1.10 christos int listen_sslctx_setup(void* ctxt, const char* tls_protocols); 485 1.1.1.2 christos 486 1.1.1.2 christos /** 487 1.1.1.2 christos * Further setup of listening SSL context, after keys loaded. 488 1.1.1.2 christos * @param ctxt: SSL_CTX* 489 1.1.1.2 christos */ 490 1.1.1.2 christos void listen_sslctx_setup_2(void* ctxt); 491 1.1.1.2 christos 492 1.1.1.9 christos /** 493 1.1 christos * create SSL listen context 494 1.1 christos * @param key: private key file. 495 1.1 christos * @param pem: public key cert. 496 1.1 christos * @param verifypem: if nonNULL, verifylocation file. 497 1.1.1.9 christos * @param tls_ciphers: if non empty string, tls ciphers to use. 498 1.1.1.9 christos * @param tls_ciphersuites: if non empty string, tls ciphersuites to use. 499 1.1.1.9 christos * @param set_ticket_keys_cb: if the callback for configured ticket keys needs 500 1.1.1.9 christos * to be set. 501 1.1.1.9 christos * @param is_dot: if the TLS connection is for DoT to set the appropriate ALPN. 502 1.1.1.9 christos * @param is_doh: if the TLS connection is for DoH to set the appropriate ALPN. 503 1.1.1.10 christos * @param tls_protocols: configure string with allowed TLS protocols to use. 504 1.1 christos * return SSL_CTX* or NULL on failure (logged). 505 1.1 christos */ 506 1.1.1.9 christos void* listen_sslctx_create(const char* key, const char* pem, 507 1.1.1.9 christos const char* verifypem, const char* tls_ciphers, 508 1.1.1.9 christos const char* tls_ciphersuites, int set_ticket_keys_cb, 509 1.1.1.10 christos int is_dot, int is_doh, const char* tls_protocols); 510 1.1 christos 511 1.1 christos /** 512 1.1 christos * create SSL connect context 513 1.1 christos * @param key: if nonNULL (also pem nonNULL), the client private key. 514 1.1 christos * @param pem: client public key (or NULL if key is NULL). 515 1.1 christos * @param verifypem: if nonNULL used for verifylocation file. 516 1.1.1.3 christos * @param wincert: add system certificate store to ctx (add to verifypem ca 517 1.1.1.3 christos * certs). 518 1.1 christos * @return SSL_CTX* or NULL on failure (logged). 519 1.1 christos */ 520 1.1.1.3 christos void* connect_sslctx_create(char* key, char* pem, char* verifypem, int wincert); 521 1.1 christos 522 1.1 christos /** 523 1.1 christos * accept a new fd and wrap it in a BIO in SSL 524 1.1 christos * @param sslctx: the SSL_CTX to use (from listen_sslctx_create()). 525 1.1 christos * @param fd: from accept, nonblocking. 526 1.1 christos * @return SSL or NULL on alloc failure. 527 1.1 christos */ 528 1.1 christos void* incoming_ssl_fd(void* sslctx, int fd); 529 1.1 christos 530 1.1 christos /** 531 1.1 christos * connect a new fd and wrap it in a BIO in SSL 532 1.1 christos * @param sslctx: the SSL_CTX to use (from connect_sslctx_create()) 533 1.1 christos * @param fd: from connect. 534 1.1 christos * @return SSL or NULL on alloc failure 535 1.1 christos */ 536 1.1 christos void* outgoing_ssl_fd(void* sslctx, int fd); 537 1.1 christos 538 1.1 christos /** 539 1.1.1.6 christos * check if authname SSL functionality is available, false if not 540 1.1.1.6 christos * @param auth_name: the name for the remote server, used for error print. 541 1.1.1.6 christos * @return false if SSL functionality to check the SSL name is not available. 542 1.1.1.6 christos */ 543 1.1.1.6 christos int check_auth_name_for_ssl(char* auth_name); 544 1.1.1.6 christos 545 1.1.1.6 christos /** 546 1.1.1.6 christos * set auth name on SSL for verification 547 1.1.1.6 christos * @param ssl: SSL* to set 548 1.1.1.6 christos * @param auth_name: if NULL nothing happens, otherwise the name to check. 549 1.1.1.6 christos * @param use_sni: if SNI will be used. 550 1.1.1.6 christos * @return 1 on success or NULL auth_name, 0 on failure. 551 1.1.1.6 christos */ 552 1.1.1.6 christos int set_auth_name_on_ssl(void* ssl, char* auth_name, int use_sni); 553 1.1.1.6 christos 554 1.1.1.6 christos /** 555 1.1 christos * Initialize openssl locking for thread safety 556 1.1 christos * @return false on failure (alloc failure). 557 1.1 christos */ 558 1.1 christos int ub_openssl_lock_init(void); 559 1.1 christos 560 1.1 christos /** 561 1.1 christos * De-init the allocated openssl locks 562 1.1 christos */ 563 1.1 christos void ub_openssl_lock_delete(void); 564 1.1 christos 565 1.1.1.4 christos /** 566 1.1.1.4 christos * setup TLS session ticket 567 1.1.1.4 christos * @param tls_session_ticket_keys: TLS ticket secret filenames 568 1.1.1.10 christos * @param chroot: if not NULL, the chroot that is in use. 569 1.1.1.4 christos * @return false on failure (alloc failure). 570 1.1.1.4 christos */ 571 1.1.1.10 christos int listen_sslctx_setup_ticket_keys( 572 1.1.1.10 christos struct config_strlist* tls_session_ticket_keys, char* chroot); 573 1.1.1.4 christos 574 1.1.1.4 christos /** Free memory used for TLS session ticket keys */ 575 1.1.1.4 christos void listen_sslctx_delete_ticket_keys(void); 576 1.1.1.4 christos 577 1.1.1.6 christos /** 578 1.1.1.6 christos * RPZ format netblock to network byte order address and netblock 579 1.1.1.6 christos * example RPZ netblock format dnames: 580 1.1.1.6 christos * - 24.10.100.51.198.rpz-ip -> 198.51.100.10/24 581 1.1.1.6 christos * - 32.10.zz.db8.2001.rpz-ip -> 2001:db8:0:0:0:0:0:10/32 582 1.1.1.6 christos * @param dname: the dname containing RPZ format netblock 583 1.1.1.6 christos * @param dnamelen: length of dname 584 1.1.1.6 christos * @param addr: where to store sockaddr. 585 1.1.1.6 christos * @param addrlen: length of stored sockaddr is returned. 586 1.1.1.6 christos * @param net: where to store netmask 587 1.1.1.6 christos * @param af: where to store address family. 588 1.1.1.6 christos * @return 0 on error. 589 1.1.1.6 christos */ 590 1.1.1.6 christos int netblockdnametoaddr(uint8_t* dname, size_t dnamelen, 591 1.1.1.6 christos struct sockaddr_storage* addr, socklen_t* addrlen, int* net, int* af); 592 1.1.1.6 christos 593 1.1.1.6 christos /** Return strerror or wsastrerror for socket error printout */ 594 1.1.1.6 christos char* sock_strerror(int errn); 595 1.1.1.6 christos /** close the socket with close, or wsa closesocket */ 596 1.1.1.6 christos void sock_close(int socket); 597 1.1.1.6 christos 598 1.1.1.9 christos /** 599 1.1.1.9 christos * Convert binary data to a string of hexadecimal characters. 600 1.1.1.9 christos */ 601 1.1.1.9 christos ssize_t hex_ntop(uint8_t const *src, size_t srclength, char *target, 602 1.1.1.9 christos size_t targsize); 603 1.1.1.9 christos 604 1.1.1.9 christos /** Convert hexadecimal data to binary. */ 605 1.1.1.9 christos ssize_t hex_pton(const char* src, uint8_t* target, size_t targsize); 606 1.1.1.9 christos 607 1.1 christos #endif /* NET_HELP_H */ 608