1 /* $NetBSD: netaddr.h,v 1.8 2025/01/26 16:25:41 christos Exp $ */ 2 3 /* 4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 * 6 * SPDX-License-Identifier: MPL-2.0 7 * 8 * This Source Code Form is subject to the terms of the Mozilla Public 9 * License, v. 2.0. If a copy of the MPL was not distributed with this 10 * file, you can obtain one at https://mozilla.org/MPL/2.0/. 11 * 12 * See the COPYRIGHT file distributed with this work for additional 13 * information regarding copyright ownership. 14 */ 15 16 #pragma once 17 18 /*! \file isc/netaddr.h */ 19 20 #include <inttypes.h> 21 #include <stdbool.h> 22 #include <sys/types.h> 23 #include <sys/un.h> 24 25 #include <isc/lang.h> 26 #include <isc/net.h> 27 #include <isc/types.h> 28 29 ISC_LANG_BEGINDECLS 30 31 /* 32 * Any updates to this structure should also be applied in 33 * https://gitlab.isc.org/isc-projects/dlz-modules/-/raw/main/modules/include/dlz_minimal.h 34 */ 35 struct isc_netaddr { 36 unsigned int family; 37 union { 38 struct in_addr in; 39 struct in6_addr in6; 40 char un[sizeof(((struct sockaddr_un *)0)->sun_path)]; 41 } type; 42 uint32_t zone; 43 }; 44 45 struct isc_netprefix { 46 isc_netaddr_t addr; 47 unsigned int prefixlen; 48 }; 49 50 bool 51 isc_netaddr_equal(const isc_netaddr_t *a, const isc_netaddr_t *b); 52 53 /*%< 54 * Compare network addresses 'a' and 'b'. Return #true if 55 * they are equal, #false if not. 56 */ 57 58 bool 59 isc_netaddr_eqprefix(const isc_netaddr_t *a, const isc_netaddr_t *b, 60 unsigned int prefixlen); 61 /*%< 62 * Compare the 'prefixlen' most significant bits of the network 63 * addresses 'a' and 'b'. If 'b''s scope is zero then 'a''s scope is 64 * ignored. Return #true if they are equal, #false if not. 65 */ 66 67 isc_result_t 68 isc_netaddr_masktoprefixlen(const isc_netaddr_t *s, unsigned int *lenp); 69 /*%< 70 * Convert a netmask in 's' into a prefix length in '*lenp'. 71 * The mask should consist of zero or more '1' bits in the 72 * most significant part of the address, followed by '0' bits. 73 * If this is not the case, #ISC_R_MASKNONCONTIG is returned. 74 * 75 * Returns: 76 *\li #ISC_R_SUCCESS 77 *\li #ISC_R_MASKNONCONTIG 78 */ 79 80 isc_result_t 81 isc_netaddr_totext(const isc_netaddr_t *netaddr, isc_buffer_t *target); 82 /*%< 83 * Append a text representation of 'sockaddr' to the buffer 'target'. 84 * The text is NOT null terminated. Handles IPv4 and IPv6 addresses. 85 * 86 * Returns: 87 *\li #ISC_R_SUCCESS 88 *\li #ISC_R_NOSPACE The text or the null termination did not fit. 89 *\li #ISC_R_FAILURE Unspecified failure 90 */ 91 92 void 93 isc_netaddr_format(const isc_netaddr_t *na, char *array, unsigned int size); 94 /*%< 95 * Format a human-readable representation of the network address '*na' 96 * into the character array 'array', which is of size 'size'. 97 * The resulting string is guaranteed to be null-terminated. 98 */ 99 100 #define ISC_NETADDR_FORMATSIZE \ 101 sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:XXX.XXX.XXX.XXX%SSSSSSSSSS") 102 /*%< 103 * Minimum size of array to pass to isc_netaddr_format(). 104 */ 105 106 void 107 isc_netaddr_fromsockaddr(isc_netaddr_t *netaddr, const isc_sockaddr_t *source); 108 109 void 110 isc_netaddr_fromin(isc_netaddr_t *netaddr, const struct in_addr *ina); 111 112 void 113 isc_netaddr_fromin6(isc_netaddr_t *netaddr, const struct in6_addr *ina6); 114 115 void 116 isc_netaddr_setzone(isc_netaddr_t *netaddr, uint32_t zone); 117 118 uint32_t 119 isc_netaddr_getzone(const isc_netaddr_t *netaddr); 120 121 void 122 isc_netaddr_any(isc_netaddr_t *netaddr); 123 /*%< 124 * Return the IPv4 wildcard address. 125 */ 126 127 void 128 isc_netaddr_any6(isc_netaddr_t *netaddr); 129 /*%< 130 * Return the IPv6 wildcard address. 131 */ 132 133 void 134 isc_netaddr_unspec(isc_netaddr_t *netaddr); 135 /*%< 136 * Initialize as AF_UNSPEC address. 137 */ 138 139 bool 140 isc_netaddr_ismulticast(const isc_netaddr_t *na); 141 /*%< 142 * Returns true if the address is a multicast address. 143 */ 144 145 bool 146 isc_netaddr_isexperimental(const isc_netaddr_t *na); 147 /*%< 148 * Returns true if the address is a experimental (CLASS E) address. 149 */ 150 151 bool 152 isc_netaddr_islinklocal(const isc_netaddr_t *na); 153 /*%< 154 * Returns #true if the address is a link local address. 155 */ 156 157 bool 158 isc_netaddr_issitelocal(const isc_netaddr_t *na); 159 /*%< 160 * Returns #true if the address is a site local address. 161 */ 162 163 bool 164 isc_netaddr_isnetzero(const isc_netaddr_t *na); 165 /*%< 166 * Returns #true if the address is in net zero. 167 */ 168 169 void 170 isc_netaddr_fromv4mapped(isc_netaddr_t *t, const isc_netaddr_t *s); 171 /*%< 172 * Convert an IPv6 v4mapped address into an IPv4 address. 173 */ 174 175 isc_result_t 176 isc_netaddr_prefixok(const isc_netaddr_t *na, unsigned int prefixlen); 177 /* 178 * Test whether the netaddr 'na' and 'prefixlen' are consistent. 179 * e.g. prefixlen within range. 180 * na does not have bits set which are not covered by the prefixlen. 181 * 182 * Returns: 183 * ISC_R_SUCCESS 184 * ISC_R_RANGE prefixlen out of range 185 * ISC_R_NOTIMPLEMENTED unsupported family 186 * ISC_R_FAILURE extra bits. 187 */ 188 189 bool 190 isc_netaddr_isloopback(const isc_netaddr_t *na); 191 /* 192 * Test whether the netaddr 'na' is a loopback IPv4 or IPv6 address (in 193 * 127.0.0.0/8 or ::1). 194 */ 195 ISC_LANG_ENDDECLS 196