2016-05-26 21:23:19 +02:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2016-05-26 21:23:19 +02:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2016-05-26 21:23:19 +02:00
|
|
|
*/
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
2016-05-26 19:21:28 -07:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-05-26 21:23:19 +02:00
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/sockaddr.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
|
|
|
#include <dns/ipkeylist.h>
|
|
|
|
#include <dns/name.h>
|
|
|
|
|
2016-06-22 10:49:25 +02:00
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_ipkeylist_init(dns_ipkeylist_t *ipkl) {
|
2016-06-22 10:49:25 +02:00
|
|
|
ipkl->count = 0;
|
|
|
|
ipkl->allocated = 0;
|
|
|
|
ipkl->addrs = NULL;
|
|
|
|
ipkl->dscps = NULL;
|
|
|
|
ipkl->keys = NULL;
|
|
|
|
ipkl->labels = NULL;
|
|
|
|
}
|
|
|
|
|
2016-05-26 21:23:19 +02:00
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_ipkeylist_clear(isc_mem_t *mctx, dns_ipkeylist_t *ipkl) {
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t i;
|
2016-05-26 21:23:19 +02:00
|
|
|
|
|
|
|
REQUIRE(ipkl != NULL);
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ipkl->allocated == 0) {
|
2016-05-26 21:23:19 +02:00
|
|
|
return;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-05-26 21:23:19 +02:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ipkl->addrs != NULL) {
|
2016-05-26 21:29:54 -07:00
|
|
|
isc_mem_put(mctx, ipkl->addrs,
|
2016-06-22 10:49:25 +02:00
|
|
|
ipkl->allocated * sizeof(isc_sockaddr_t));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-05-26 21:29:54 -07:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ipkl->dscps != NULL) {
|
2016-05-26 21:23:19 +02:00
|
|
|
isc_mem_put(mctx, ipkl->dscps,
|
2016-06-22 10:49:25 +02:00
|
|
|
ipkl->allocated * sizeof(isc_dscp_t));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-06-22 10:49:25 +02:00
|
|
|
|
|
|
|
if (ipkl->keys != NULL) {
|
|
|
|
for (i = 0; i < ipkl->allocated; i++) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ipkl->keys[i] == NULL) {
|
2016-06-22 10:49:25 +02:00
|
|
|
continue;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (dns_name_dynamic(ipkl->keys[i])) {
|
2016-06-22 10:49:25 +02:00
|
|
|
dns_name_free(ipkl->keys[i], mctx);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-06-22 10:49:25 +02:00
|
|
|
isc_mem_put(mctx, ipkl->keys[i], sizeof(dns_name_t));
|
|
|
|
}
|
|
|
|
isc_mem_put(mctx, ipkl->keys,
|
|
|
|
ipkl->allocated * sizeof(dns_name_t *));
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
2016-05-26 21:29:54 -07:00
|
|
|
|
2016-06-22 10:49:25 +02:00
|
|
|
if (ipkl->labels != NULL) {
|
|
|
|
for (i = 0; i < ipkl->allocated; i++) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ipkl->labels[i] == NULL) {
|
2016-06-22 10:49:25 +02:00
|
|
|
continue;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (dns_name_dynamic(ipkl->labels[i])) {
|
2016-06-22 10:49:25 +02:00
|
|
|
dns_name_free(ipkl->labels[i], mctx);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-06-22 10:49:25 +02:00
|
|
|
isc_mem_put(mctx, ipkl->labels[i], sizeof(dns_name_t));
|
|
|
|
}
|
|
|
|
isc_mem_put(mctx, ipkl->labels,
|
|
|
|
ipkl->allocated * sizeof(dns_name_t *));
|
|
|
|
}
|
2016-05-26 21:29:54 -07:00
|
|
|
|
2016-06-22 10:49:25 +02:00
|
|
|
dns_ipkeylist_init(ipkl);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
2016-05-31 08:12:51 +10:00
|
|
|
isc_result_t
|
2016-05-26 21:23:19 +02:00
|
|
|
dns_ipkeylist_copy(isc_mem_t *mctx, const dns_ipkeylist_t *src,
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_ipkeylist_t *dst) {
|
2016-05-31 08:12:51 +10:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
2020-02-13 14:44:37 -08:00
|
|
|
uint32_t i;
|
2016-05-26 21:23:19 +02:00
|
|
|
|
|
|
|
REQUIRE(dst != NULL);
|
2016-06-22 10:49:25 +02:00
|
|
|
/* dst might be preallocated, we don't care, but it must be empty */
|
|
|
|
REQUIRE(dst->count == 0);
|
2016-05-26 21:23:19 +02:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (src->count == 0) {
|
2016-05-31 08:12:51 +10:00
|
|
|
return (ISC_R_SUCCESS);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-05-26 21:23:19 +02:00
|
|
|
|
2016-06-22 10:49:25 +02:00
|
|
|
result = dns_ipkeylist_resize(mctx, dst, src->count);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2016-06-22 10:49:25 +02:00
|
|
|
return (result);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-05-31 08:12:51 +10:00
|
|
|
|
|
|
|
memmove(dst->addrs, src->addrs, src->count * sizeof(isc_sockaddr_t));
|
2016-05-26 21:23:19 +02:00
|
|
|
|
|
|
|
if (src->dscps != NULL) {
|
|
|
|
memmove(dst->dscps, src->dscps,
|
|
|
|
src->count * sizeof(isc_dscp_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src->keys != NULL) {
|
|
|
|
for (i = 0; i < src->count; i++) {
|
|
|
|
if (src->keys[i] != NULL) {
|
2020-02-13 14:44:37 -08:00
|
|
|
dst->keys[i] = isc_mem_get(mctx,
|
|
|
|
sizeof(dns_name_t));
|
2016-05-31 08:12:51 +10:00
|
|
|
dns_name_init(dst->keys[i], NULL);
|
2019-11-01 08:31:13 -05:00
|
|
|
dns_name_dup(src->keys[i], mctx, dst->keys[i]);
|
2016-05-26 21:23:19 +02:00
|
|
|
} else {
|
|
|
|
dst->keys[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-31 08:12:51 +10:00
|
|
|
|
2016-06-22 10:49:25 +02:00
|
|
|
if (src->labels != NULL) {
|
|
|
|
for (i = 0; i < src->count; i++) {
|
|
|
|
if (src->labels[i] != NULL) {
|
2020-02-12 13:59:18 +01:00
|
|
|
dst->labels[i] =
|
|
|
|
isc_mem_get(mctx, sizeof(dns_name_t));
|
2016-06-22 10:49:25 +02:00
|
|
|
dns_name_init(dst->labels[i], NULL);
|
2019-11-01 08:31:13 -05:00
|
|
|
dns_name_dup(src->labels[i], mctx,
|
|
|
|
dst->labels[i]);
|
2016-06-22 10:49:25 +02:00
|
|
|
} else {
|
|
|
|
dst->labels[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dst->count = src->count;
|
2016-05-31 08:12:51 +10:00
|
|
|
return (ISC_R_SUCCESS);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
2016-06-22 10:49:25 +02:00
|
|
|
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_ipkeylist_resize(isc_mem_t *mctx, dns_ipkeylist_t *ipkl, unsigned int n) {
|
2016-06-22 10:49:25 +02:00
|
|
|
isc_sockaddr_t *addrs = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_dscp_t *dscps = NULL;
|
|
|
|
dns_name_t **keys = NULL;
|
|
|
|
dns_name_t **labels = NULL;
|
2016-06-22 10:49:25 +02:00
|
|
|
|
|
|
|
REQUIRE(ipkl != NULL);
|
|
|
|
REQUIRE(n > ipkl->count);
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (n <= ipkl->allocated) {
|
2016-06-22 10:49:25 +02:00
|
|
|
return (ISC_R_SUCCESS);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-06-22 10:49:25 +02:00
|
|
|
|
|
|
|
addrs = isc_mem_get(mctx, n * sizeof(isc_sockaddr_t));
|
|
|
|
dscps = isc_mem_get(mctx, n * sizeof(isc_dscp_t));
|
|
|
|
keys = isc_mem_get(mctx, n * sizeof(dns_name_t *));
|
|
|
|
labels = isc_mem_get(mctx, n * sizeof(dns_name_t *));
|
|
|
|
|
2018-01-22 09:36:12 +11:00
|
|
|
if (ipkl->addrs != NULL) {
|
|
|
|
memmove(addrs, ipkl->addrs,
|
|
|
|
ipkl->allocated * sizeof(isc_sockaddr_t));
|
2016-06-22 10:49:25 +02:00
|
|
|
isc_mem_put(mctx, ipkl->addrs,
|
|
|
|
ipkl->allocated * sizeof(isc_sockaddr_t));
|
2018-01-22 09:36:12 +11:00
|
|
|
}
|
2016-06-22 10:49:25 +02:00
|
|
|
ipkl->addrs = addrs;
|
|
|
|
memset(&ipkl->addrs[ipkl->allocated], 0,
|
|
|
|
(n - ipkl->allocated) * sizeof(isc_sockaddr_t));
|
|
|
|
|
2018-01-22 09:36:12 +11:00
|
|
|
if (ipkl->dscps != NULL) {
|
|
|
|
memmove(dscps, ipkl->dscps,
|
|
|
|
ipkl->allocated * sizeof(isc_dscp_t));
|
2016-06-22 10:49:25 +02:00
|
|
|
isc_mem_put(mctx, ipkl->dscps,
|
|
|
|
ipkl->allocated * sizeof(isc_dscp_t));
|
2018-01-22 09:36:12 +11:00
|
|
|
}
|
2016-06-22 10:49:25 +02:00
|
|
|
ipkl->dscps = dscps;
|
|
|
|
memset(&ipkl->dscps[ipkl->allocated], 0,
|
|
|
|
(n - ipkl->allocated) * sizeof(isc_dscp_t));
|
|
|
|
|
2018-01-22 09:36:12 +11:00
|
|
|
if (ipkl->keys) {
|
|
|
|
memmove(keys, ipkl->keys,
|
|
|
|
ipkl->allocated * sizeof(dns_name_t *));
|
2016-06-22 10:49:25 +02:00
|
|
|
isc_mem_put(mctx, ipkl->keys,
|
|
|
|
ipkl->allocated * sizeof(dns_name_t *));
|
2018-01-22 09:36:12 +11:00
|
|
|
}
|
2016-06-22 10:49:25 +02:00
|
|
|
ipkl->keys = keys;
|
|
|
|
memset(&ipkl->keys[ipkl->allocated], 0,
|
|
|
|
(n - ipkl->allocated) * sizeof(dns_name_t *));
|
|
|
|
|
2018-01-22 09:36:12 +11:00
|
|
|
if (ipkl->labels != NULL) {
|
|
|
|
memmove(labels, ipkl->labels,
|
|
|
|
ipkl->allocated * sizeof(dns_name_t *));
|
2016-06-22 10:49:25 +02:00
|
|
|
isc_mem_put(mctx, ipkl->labels,
|
|
|
|
ipkl->allocated * sizeof(dns_name_t *));
|
2018-01-22 09:36:12 +11:00
|
|
|
}
|
2016-06-22 10:49:25 +02:00
|
|
|
ipkl->labels = labels;
|
|
|
|
memset(&ipkl->labels[ipkl->allocated], 0,
|
|
|
|
(n - ipkl->allocated) * sizeof(dns_name_t *));
|
|
|
|
|
|
|
|
ipkl->allocated = n;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
2019-07-16 15:52:14 +02:00
|
|
|
isc_mem_put(mctx, addrs, n * sizeof(isc_sockaddr_t));
|
|
|
|
isc_mem_put(mctx, dscps, n * sizeof(isc_dscp_t));
|
|
|
|
isc_mem_put(mctx, keys, n * sizeof(dns_name_t *));
|
|
|
|
isc_mem_put(mctx, labels, n * sizeof(dns_name_t *));
|
2016-06-22 10:49:25 +02:00
|
|
|
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|