Home | History | Annotate | Line # | Download | only in dns
      1 /*	$NetBSD: ipkeylist.c,v 1.8 2025/01/26 16:25:23 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 #include <inttypes.h>
     17 #include <string.h>
     18 
     19 #include <isc/mem.h>
     20 #include <isc/sockaddr.h>
     21 #include <isc/util.h>
     22 
     23 #include <dns/ipkeylist.h>
     24 #include <dns/name.h>
     25 
     26 void
     27 dns_ipkeylist_init(dns_ipkeylist_t *ipkl) {
     28 	ipkl->count = 0;
     29 	ipkl->allocated = 0;
     30 	ipkl->addrs = NULL;
     31 	ipkl->sources = NULL;
     32 	ipkl->keys = NULL;
     33 	ipkl->tlss = NULL;
     34 	ipkl->labels = NULL;
     35 }
     36 
     37 void
     38 dns_ipkeylist_clear(isc_mem_t *mctx, dns_ipkeylist_t *ipkl) {
     39 	REQUIRE(ipkl != NULL);
     40 
     41 	if (ipkl->allocated == 0) {
     42 		return;
     43 	}
     44 
     45 	if (ipkl->addrs != NULL) {
     46 		isc_mem_cput(mctx, ipkl->addrs, ipkl->allocated,
     47 			     sizeof(ipkl->addrs[0]));
     48 	}
     49 
     50 	if (ipkl->sources != NULL) {
     51 		isc_mem_cput(mctx, ipkl->sources, ipkl->allocated,
     52 			     sizeof(ipkl->sources[0]));
     53 	}
     54 
     55 	if (ipkl->keys != NULL) {
     56 		for (size_t i = 0; i < ipkl->allocated; i++) {
     57 			if (ipkl->keys[i] != NULL) {
     58 				if (dns_name_dynamic(ipkl->keys[i])) {
     59 					dns_name_free(ipkl->keys[i], mctx);
     60 				}
     61 				isc_mem_put(mctx, ipkl->keys[i],
     62 					    sizeof(*ipkl->keys[i]));
     63 			}
     64 		}
     65 		isc_mem_cput(mctx, ipkl->keys, ipkl->allocated,
     66 			     sizeof(ipkl->keys[0]));
     67 	}
     68 
     69 	if (ipkl->tlss != NULL) {
     70 		for (size_t i = 0; i < ipkl->allocated; i++) {
     71 			if (ipkl->tlss[i] != NULL) {
     72 				if (dns_name_dynamic(ipkl->tlss[i])) {
     73 					dns_name_free(ipkl->tlss[i], mctx);
     74 				}
     75 				isc_mem_put(mctx, ipkl->tlss[i],
     76 					    sizeof(*ipkl->tlss[i]));
     77 			}
     78 		}
     79 		isc_mem_cput(mctx, ipkl->tlss, ipkl->allocated,
     80 			     sizeof(ipkl->tlss[0]));
     81 	}
     82 
     83 	if (ipkl->labels != NULL) {
     84 		for (size_t i = 0; i < ipkl->allocated; i++) {
     85 			if (ipkl->labels[i] != NULL) {
     86 				if (dns_name_dynamic(ipkl->labels[i])) {
     87 					dns_name_free(ipkl->labels[i], mctx);
     88 				}
     89 				isc_mem_put(mctx, ipkl->labels[i],
     90 					    sizeof(*ipkl->labels[i]));
     91 			}
     92 		}
     93 		isc_mem_cput(mctx, ipkl->labels, ipkl->allocated,
     94 			     sizeof(ipkl->labels[0]));
     95 	}
     96 
     97 	dns_ipkeylist_init(ipkl);
     98 }
     99 
    100 isc_result_t
    101 dns_ipkeylist_copy(isc_mem_t *mctx, const dns_ipkeylist_t *src,
    102 		   dns_ipkeylist_t *dst) {
    103 	isc_result_t result = ISC_R_SUCCESS;
    104 	uint32_t i;
    105 
    106 	REQUIRE(dst != NULL);
    107 	/* dst might be preallocated, we don't care, but it must be empty */
    108 	REQUIRE(dst->count == 0);
    109 
    110 	if (src->count == 0) {
    111 		return ISC_R_SUCCESS;
    112 	}
    113 
    114 	result = dns_ipkeylist_resize(mctx, dst, src->count);
    115 	if (result != ISC_R_SUCCESS) {
    116 		return result;
    117 	}
    118 
    119 	memmove(dst->addrs, src->addrs, src->count * sizeof(isc_sockaddr_t));
    120 
    121 	if (src->sources != NULL) {
    122 		memmove(dst->sources, src->sources,
    123 			src->count * sizeof(isc_sockaddr_t));
    124 	}
    125 
    126 	if (src->keys != NULL) {
    127 		for (i = 0; i < src->count; i++) {
    128 			if (src->keys[i] != NULL) {
    129 				dst->keys[i] = isc_mem_get(mctx,
    130 							   sizeof(dns_name_t));
    131 				dns_name_init(dst->keys[i], NULL);
    132 				dns_name_dup(src->keys[i], mctx, dst->keys[i]);
    133 			} else {
    134 				dst->keys[i] = NULL;
    135 			}
    136 		}
    137 	}
    138 
    139 	if (src->tlss != NULL) {
    140 		for (i = 0; i < src->count; i++) {
    141 			if (src->tlss[i] != NULL) {
    142 				dst->tlss[i] = isc_mem_get(mctx,
    143 							   sizeof(dns_name_t));
    144 				dns_name_init(dst->tlss[i], NULL);
    145 				dns_name_dup(src->tlss[i], mctx, dst->tlss[i]);
    146 			} else {
    147 				dst->tlss[i] = NULL;
    148 			}
    149 		}
    150 	}
    151 
    152 	if (src->labels != NULL) {
    153 		for (i = 0; i < src->count; i++) {
    154 			if (src->labels[i] != NULL) {
    155 				dst->labels[i] =
    156 					isc_mem_get(mctx, sizeof(dns_name_t));
    157 				dns_name_init(dst->labels[i], NULL);
    158 				dns_name_dup(src->labels[i], mctx,
    159 					     dst->labels[i]);
    160 			} else {
    161 				dst->labels[i] = NULL;
    162 			}
    163 		}
    164 	}
    165 	dst->count = src->count;
    166 	return ISC_R_SUCCESS;
    167 }
    168 
    169 isc_result_t
    170 dns_ipkeylist_resize(isc_mem_t *mctx, dns_ipkeylist_t *ipkl, unsigned int n) {
    171 	REQUIRE(ipkl != NULL);
    172 	REQUIRE(n > ipkl->count);
    173 
    174 	if (n <= ipkl->allocated) {
    175 		return ISC_R_SUCCESS;
    176 	}
    177 
    178 	ipkl->addrs = isc_mem_creget(mctx, ipkl->addrs, ipkl->allocated, n,
    179 				     sizeof(ipkl->addrs[0]));
    180 	ipkl->sources = isc_mem_creget(mctx, ipkl->sources, ipkl->allocated, n,
    181 				       sizeof(ipkl->sources[0]));
    182 	ipkl->keys = isc_mem_creget(mctx, ipkl->keys, ipkl->allocated, n,
    183 				    sizeof(ipkl->keys[0]));
    184 	ipkl->tlss = isc_mem_creget(mctx, ipkl->tlss, ipkl->allocated, n,
    185 				    sizeof(ipkl->tlss[0]));
    186 	ipkl->labels = isc_mem_creget(mctx, ipkl->labels, ipkl->allocated, n,
    187 				      sizeof(ipkl->labels[0]));
    188 
    189 	ipkl->allocated = n;
    190 	return ISC_R_SUCCESS;
    191 }
    192