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