1*bcda20f6Schristos /* $NetBSD: ipkeylist.c,v 1.8 2025/01/26 16:25:23 christos Exp $ */ 2d68c78b8Schristos 3d68c78b8Schristos /* 4d68c78b8Schristos * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5d68c78b8Schristos * 68596601aSchristos * SPDX-License-Identifier: MPL-2.0 78596601aSchristos * 8d68c78b8Schristos * This Source Code Form is subject to the terms of the Mozilla Public 9d68c78b8Schristos * License, v. 2.0. If a copy of the MPL was not distributed with this 10fce770bdSchristos * file, you can obtain one at https://mozilla.org/MPL/2.0/. 11d68c78b8Schristos * 12d68c78b8Schristos * See the COPYRIGHT file distributed with this work for additional 13d68c78b8Schristos * information regarding copyright ownership. 14d68c78b8Schristos */ 15d68c78b8Schristos 16d4a20c3eSchristos #include <inttypes.h> 17d68c78b8Schristos #include <string.h> 18d68c78b8Schristos 19d68c78b8Schristos #include <isc/mem.h> 20d68c78b8Schristos #include <isc/sockaddr.h> 21d68c78b8Schristos #include <isc/util.h> 22d68c78b8Schristos 23d68c78b8Schristos #include <dns/ipkeylist.h> 24d68c78b8Schristos #include <dns/name.h> 25d68c78b8Schristos 26d68c78b8Schristos void 27d68c78b8Schristos dns_ipkeylist_init(dns_ipkeylist_t *ipkl) { 28d68c78b8Schristos ipkl->count = 0; 29d68c78b8Schristos ipkl->allocated = 0; 30d68c78b8Schristos ipkl->addrs = NULL; 31*bcda20f6Schristos ipkl->sources = NULL; 32d68c78b8Schristos ipkl->keys = NULL; 33bb5aa156Schristos ipkl->tlss = NULL; 34d68c78b8Schristos ipkl->labels = NULL; 35d68c78b8Schristos } 36d68c78b8Schristos 37d68c78b8Schristos void 38d68c78b8Schristos dns_ipkeylist_clear(isc_mem_t *mctx, dns_ipkeylist_t *ipkl) { 39d68c78b8Schristos REQUIRE(ipkl != NULL); 40d68c78b8Schristos 415606745fSchristos if (ipkl->allocated == 0) { 42d68c78b8Schristos return; 435606745fSchristos } 44d68c78b8Schristos 455606745fSchristos if (ipkl->addrs != NULL) { 46*bcda20f6Schristos isc_mem_cput(mctx, ipkl->addrs, ipkl->allocated, 47*bcda20f6Schristos sizeof(ipkl->addrs[0])); 48*bcda20f6Schristos } 49*bcda20f6Schristos 50*bcda20f6Schristos if (ipkl->sources != NULL) { 51*bcda20f6Schristos isc_mem_cput(mctx, ipkl->sources, ipkl->allocated, 52*bcda20f6Schristos sizeof(ipkl->sources[0])); 535606745fSchristos } 54d68c78b8Schristos 55d68c78b8Schristos if (ipkl->keys != NULL) { 56*bcda20f6Schristos for (size_t i = 0; i < ipkl->allocated; i++) { 57*bcda20f6Schristos if (ipkl->keys[i] != NULL) { 585606745fSchristos if (dns_name_dynamic(ipkl->keys[i])) { 59d68c78b8Schristos dns_name_free(ipkl->keys[i], mctx); 605606745fSchristos } 61*bcda20f6Schristos isc_mem_put(mctx, ipkl->keys[i], 62*bcda20f6Schristos sizeof(*ipkl->keys[i])); 63d68c78b8Schristos } 64*bcda20f6Schristos } 65*bcda20f6Schristos isc_mem_cput(mctx, ipkl->keys, ipkl->allocated, 66*bcda20f6Schristos sizeof(ipkl->keys[0])); 67d68c78b8Schristos } 68d68c78b8Schristos 69bb5aa156Schristos if (ipkl->tlss != NULL) { 70*bcda20f6Schristos for (size_t i = 0; i < ipkl->allocated; i++) { 71*bcda20f6Schristos if (ipkl->tlss[i] != NULL) { 72bb5aa156Schristos if (dns_name_dynamic(ipkl->tlss[i])) { 73bb5aa156Schristos dns_name_free(ipkl->tlss[i], mctx); 74bb5aa156Schristos } 75*bcda20f6Schristos isc_mem_put(mctx, ipkl->tlss[i], 76*bcda20f6Schristos sizeof(*ipkl->tlss[i])); 77bb5aa156Schristos } 78*bcda20f6Schristos } 79*bcda20f6Schristos isc_mem_cput(mctx, ipkl->tlss, ipkl->allocated, 80*bcda20f6Schristos sizeof(ipkl->tlss[0])); 81bb5aa156Schristos } 82bb5aa156Schristos 83d68c78b8Schristos if (ipkl->labels != NULL) { 84*bcda20f6Schristos for (size_t i = 0; i < ipkl->allocated; i++) { 85*bcda20f6Schristos if (ipkl->labels[i] != NULL) { 865606745fSchristos if (dns_name_dynamic(ipkl->labels[i])) { 87d68c78b8Schristos dns_name_free(ipkl->labels[i], mctx); 885606745fSchristos } 89*bcda20f6Schristos isc_mem_put(mctx, ipkl->labels[i], 90*bcda20f6Schristos sizeof(*ipkl->labels[i])); 91d68c78b8Schristos } 92*bcda20f6Schristos } 93*bcda20f6Schristos isc_mem_cput(mctx, ipkl->labels, ipkl->allocated, 94*bcda20f6Schristos sizeof(ipkl->labels[0])); 95d68c78b8Schristos } 96d68c78b8Schristos 97d68c78b8Schristos dns_ipkeylist_init(ipkl); 98d68c78b8Schristos } 99d68c78b8Schristos 100d68c78b8Schristos isc_result_t 101d68c78b8Schristos dns_ipkeylist_copy(isc_mem_t *mctx, const dns_ipkeylist_t *src, 1025606745fSchristos dns_ipkeylist_t *dst) { 103d68c78b8Schristos isc_result_t result = ISC_R_SUCCESS; 104d4a20c3eSchristos uint32_t i; 105d68c78b8Schristos 106d68c78b8Schristos REQUIRE(dst != NULL); 107d68c78b8Schristos /* dst might be preallocated, we don't care, but it must be empty */ 108d68c78b8Schristos REQUIRE(dst->count == 0); 109d68c78b8Schristos 1105606745fSchristos if (src->count == 0) { 111*bcda20f6Schristos return ISC_R_SUCCESS; 1125606745fSchristos } 113d68c78b8Schristos 114d68c78b8Schristos result = dns_ipkeylist_resize(mctx, dst, src->count); 1155606745fSchristos if (result != ISC_R_SUCCESS) { 116*bcda20f6Schristos return result; 1175606745fSchristos } 118d68c78b8Schristos 119d68c78b8Schristos memmove(dst->addrs, src->addrs, src->count * sizeof(isc_sockaddr_t)); 120d68c78b8Schristos 121*bcda20f6Schristos if (src->sources != NULL) { 122*bcda20f6Schristos memmove(dst->sources, src->sources, 123*bcda20f6Schristos src->count * sizeof(isc_sockaddr_t)); 124*bcda20f6Schristos } 125*bcda20f6Schristos 126d68c78b8Schristos if (src->keys != NULL) { 127d68c78b8Schristos for (i = 0; i < src->count; i++) { 128d68c78b8Schristos if (src->keys[i] != NULL) { 129d68c78b8Schristos dst->keys[i] = isc_mem_get(mctx, 130d68c78b8Schristos sizeof(dns_name_t)); 131d68c78b8Schristos dns_name_init(dst->keys[i], NULL); 1325606745fSchristos dns_name_dup(src->keys[i], mctx, dst->keys[i]); 133d68c78b8Schristos } else { 134d68c78b8Schristos dst->keys[i] = NULL; 135d68c78b8Schristos } 136d68c78b8Schristos } 137d68c78b8Schristos } 138d68c78b8Schristos 139bb5aa156Schristos if (src->tlss != NULL) { 140bb5aa156Schristos for (i = 0; i < src->count; i++) { 141bb5aa156Schristos if (src->tlss[i] != NULL) { 142bb5aa156Schristos dst->tlss[i] = isc_mem_get(mctx, 143bb5aa156Schristos sizeof(dns_name_t)); 144bb5aa156Schristos dns_name_init(dst->tlss[i], NULL); 145bb5aa156Schristos dns_name_dup(src->tlss[i], mctx, dst->tlss[i]); 146bb5aa156Schristos } else { 147bb5aa156Schristos dst->tlss[i] = NULL; 148bb5aa156Schristos } 149bb5aa156Schristos } 150bb5aa156Schristos } 151bb5aa156Schristos 152d68c78b8Schristos if (src->labels != NULL) { 153d68c78b8Schristos for (i = 0; i < src->count; i++) { 154d68c78b8Schristos if (src->labels[i] != NULL) { 1555606745fSchristos dst->labels[i] = 1565606745fSchristos isc_mem_get(mctx, sizeof(dns_name_t)); 157d68c78b8Schristos dns_name_init(dst->labels[i], NULL); 1585606745fSchristos dns_name_dup(src->labels[i], mctx, 159d68c78b8Schristos dst->labels[i]); 160d68c78b8Schristos } else { 161d68c78b8Schristos dst->labels[i] = NULL; 162d68c78b8Schristos } 163d68c78b8Schristos } 164d68c78b8Schristos } 165d68c78b8Schristos dst->count = src->count; 166*bcda20f6Schristos return ISC_R_SUCCESS; 167d68c78b8Schristos } 168d68c78b8Schristos 169d68c78b8Schristos isc_result_t 170d68c78b8Schristos dns_ipkeylist_resize(isc_mem_t *mctx, dns_ipkeylist_t *ipkl, unsigned int n) { 171d68c78b8Schristos REQUIRE(ipkl != NULL); 172d68c78b8Schristos REQUIRE(n > ipkl->count); 173d68c78b8Schristos 1745606745fSchristos if (n <= ipkl->allocated) { 175*bcda20f6Schristos return ISC_R_SUCCESS; 1765606745fSchristos } 177d68c78b8Schristos 178*bcda20f6Schristos ipkl->addrs = isc_mem_creget(mctx, ipkl->addrs, ipkl->allocated, n, 179*bcda20f6Schristos sizeof(ipkl->addrs[0])); 180*bcda20f6Schristos ipkl->sources = isc_mem_creget(mctx, ipkl->sources, ipkl->allocated, n, 181*bcda20f6Schristos sizeof(ipkl->sources[0])); 182*bcda20f6Schristos ipkl->keys = isc_mem_creget(mctx, ipkl->keys, ipkl->allocated, n, 183*bcda20f6Schristos sizeof(ipkl->keys[0])); 184*bcda20f6Schristos ipkl->tlss = isc_mem_creget(mctx, ipkl->tlss, ipkl->allocated, n, 185*bcda20f6Schristos sizeof(ipkl->tlss[0])); 186*bcda20f6Schristos ipkl->labels = isc_mem_creget(mctx, ipkl->labels, ipkl->allocated, n, 187*bcda20f6Schristos sizeof(ipkl->labels[0])); 188d68c78b8Schristos 189d68c78b8Schristos ipkl->allocated = n; 190*bcda20f6Schristos return ISC_R_SUCCESS; 191d68c78b8Schristos } 192