Annotation of src/external/mpl/bind/dist/lib/dns/ipkeylist.c, Revision 1.1
1.1 ! christos 1: /* $NetBSD$ */
! 2:
! 3: /*
! 4: * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
! 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 http://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 <config.h>
! 15:
! 16: #include <string.h>
! 17:
! 18: #include <isc/mem.h>
! 19: #include <isc/sockaddr.h>
! 20: #include <isc/util.h>
! 21:
! 22: #include <dns/ipkeylist.h>
! 23: #include <dns/name.h>
! 24:
! 25: void
! 26: dns_ipkeylist_init(dns_ipkeylist_t *ipkl) {
! 27: ipkl->count = 0;
! 28: ipkl->allocated = 0;
! 29: ipkl->addrs = NULL;
! 30: ipkl->dscps = NULL;
! 31: ipkl->keys = NULL;
! 32: ipkl->labels = NULL;
! 33: }
! 34:
! 35: void
! 36: dns_ipkeylist_clear(isc_mem_t *mctx, dns_ipkeylist_t *ipkl) {
! 37: isc_uint32_t i;
! 38:
! 39: REQUIRE(ipkl != NULL);
! 40:
! 41: if (ipkl->allocated == 0)
! 42: return;
! 43:
! 44: if (ipkl->addrs != NULL)
! 45: isc_mem_put(mctx, ipkl->addrs,
! 46: ipkl->allocated * sizeof(isc_sockaddr_t));
! 47:
! 48: if (ipkl->dscps != NULL)
! 49: isc_mem_put(mctx, ipkl->dscps,
! 50: ipkl->allocated * sizeof(isc_dscp_t));
! 51:
! 52: if (ipkl->keys != NULL) {
! 53: for (i = 0; i < ipkl->allocated; i++) {
! 54: if (ipkl->keys[i] == NULL)
! 55: continue;
! 56: if (dns_name_dynamic(ipkl->keys[i]))
! 57: dns_name_free(ipkl->keys[i], mctx);
! 58: isc_mem_put(mctx, ipkl->keys[i], sizeof(dns_name_t));
! 59: }
! 60: isc_mem_put(mctx, ipkl->keys,
! 61: ipkl->allocated * sizeof(dns_name_t *));
! 62: }
! 63:
! 64: if (ipkl->labels != NULL) {
! 65: for (i = 0; i < ipkl->allocated; i++) {
! 66: if (ipkl->labels[i] == NULL)
! 67: continue;
! 68: if (dns_name_dynamic(ipkl->labels[i]))
! 69: dns_name_free(ipkl->labels[i], mctx);
! 70: isc_mem_put(mctx, ipkl->labels[i], sizeof(dns_name_t));
! 71: }
! 72: isc_mem_put(mctx, ipkl->labels,
! 73: ipkl->allocated * sizeof(dns_name_t *));
! 74: }
! 75:
! 76: dns_ipkeylist_init(ipkl);
! 77: }
! 78:
! 79: isc_result_t
! 80: dns_ipkeylist_copy(isc_mem_t *mctx, const dns_ipkeylist_t *src,
! 81: dns_ipkeylist_t *dst)
! 82: {
! 83: isc_result_t result = ISC_R_SUCCESS;
! 84: isc_uint32_t i;
! 85:
! 86: REQUIRE(dst != NULL);
! 87: /* dst might be preallocated, we don't care, but it must be empty */
! 88: REQUIRE(dst->count == 0);
! 89:
! 90: if (src->count == 0)
! 91: return (ISC_R_SUCCESS);
! 92:
! 93: result = dns_ipkeylist_resize(mctx, dst, src->count);
! 94: if (result != ISC_R_SUCCESS)
! 95: return (result);
! 96:
! 97: memmove(dst->addrs, src->addrs, src->count * sizeof(isc_sockaddr_t));
! 98:
! 99: if (src->dscps != NULL) {
! 100: memmove(dst->dscps, src->dscps,
! 101: src->count * sizeof(isc_dscp_t));
! 102: }
! 103:
! 104: if (src->keys != NULL) {
! 105: for (i = 0; i < src->count; i++) {
! 106: if (src->keys[i] != NULL) {
! 107: dst->keys[i] = isc_mem_get(mctx,
! 108: sizeof(dns_name_t));
! 109: if (dst->keys[i] == NULL) {
! 110: result = ISC_R_NOMEMORY;
! 111: goto cleanup_keys;
! 112: }
! 113: dns_name_init(dst->keys[i], NULL);
! 114: result = dns_name_dup(src->keys[i], mctx,
! 115: dst->keys[i]);
! 116: if (result != ISC_R_SUCCESS)
! 117: goto cleanup_keys;
! 118: } else {
! 119: dst->keys[i] = NULL;
! 120: }
! 121: }
! 122: }
! 123:
! 124: if (src->labels != NULL) {
! 125: for (i = 0; i < src->count; i++) {
! 126: if (src->labels[i] != NULL) {
! 127: dst->labels[i] = isc_mem_get(mctx,
! 128: sizeof(dns_name_t));
! 129: if (dst->labels[i] == NULL) {
! 130: result = ISC_R_NOMEMORY;
! 131: goto cleanup_labels;
! 132: }
! 133: dns_name_init(dst->labels[i], NULL);
! 134: result = dns_name_dup(src->labels[i], mctx,
! 135: dst->labels[i]);
! 136: if (result != ISC_R_SUCCESS)
! 137: goto cleanup_labels;
! 138: } else {
! 139: dst->labels[i] = NULL;
! 140: }
! 141: }
! 142: }
! 143: dst->count = src->count;
! 144: return (ISC_R_SUCCESS);
! 145:
! 146: cleanup_labels:
! 147: do {
! 148: if (dst->labels[i] != NULL) {
! 149: if (dns_name_dynamic(dst->labels[i]))
! 150: dns_name_free(dst->labels[i], mctx);
! 151: isc_mem_put(mctx, dst->labels[i], sizeof(dns_name_t));
! 152: dst->labels[i] = NULL;
! 153: }
! 154: } while (i-- > 0);
! 155:
! 156: cleanup_keys:
! 157: do {
! 158: if (dst->keys[i] != NULL) {
! 159: if (dns_name_dynamic(dst->keys[i]))
! 160: dns_name_free(dst->keys[i], mctx);
! 161: isc_mem_put(mctx, dst->keys[i], sizeof(dns_name_t));
! 162: dst->keys[i] = NULL;
! 163: }
! 164: } while (i-- > 0);
! 165:
! 166: return (result);
! 167: }
! 168:
! 169: isc_result_t
! 170: dns_ipkeylist_resize(isc_mem_t *mctx, dns_ipkeylist_t *ipkl, unsigned int n) {
! 171: isc_sockaddr_t *addrs = NULL;
! 172: isc_dscp_t *dscps = NULL;
! 173: dns_name_t **keys = NULL;
! 174: dns_name_t **labels = NULL;
! 175:
! 176: REQUIRE(ipkl != NULL);
! 177: REQUIRE(n > ipkl->count);
! 178:
! 179: if (n <= ipkl->allocated)
! 180: return (ISC_R_SUCCESS);
! 181:
! 182: addrs = isc_mem_get(mctx, n * sizeof(isc_sockaddr_t));
! 183: if (addrs == NULL)
! 184: goto nomemory;
! 185: dscps = isc_mem_get(mctx, n * sizeof(isc_dscp_t));
! 186: if (dscps == NULL)
! 187: goto nomemory;
! 188: keys = isc_mem_get(mctx, n * sizeof(dns_name_t *));
! 189: if (keys == NULL)
! 190: goto nomemory;
! 191: labels = isc_mem_get(mctx, n * sizeof(dns_name_t *));
! 192: if (labels == NULL)
! 193: goto nomemory;
! 194:
! 195: if (ipkl->addrs != NULL) {
! 196: memmove(addrs, ipkl->addrs,
! 197: ipkl->allocated * sizeof(isc_sockaddr_t));
! 198: isc_mem_put(mctx, ipkl->addrs,
! 199: ipkl->allocated * sizeof(isc_sockaddr_t));
! 200: }
! 201: ipkl->addrs = addrs;
! 202: memset(&ipkl->addrs[ipkl->allocated], 0,
! 203: (n - ipkl->allocated) * sizeof(isc_sockaddr_t));
! 204:
! 205: if (ipkl->dscps != NULL) {
! 206: memmove(dscps, ipkl->dscps,
! 207: ipkl->allocated * sizeof(isc_dscp_t));
! 208: isc_mem_put(mctx, ipkl->dscps,
! 209: ipkl->allocated * sizeof(isc_dscp_t));
! 210: }
! 211: ipkl->dscps = dscps;
! 212: memset(&ipkl->dscps[ipkl->allocated], 0,
! 213: (n - ipkl->allocated) * sizeof(isc_dscp_t));
! 214:
! 215: if (ipkl->keys) {
! 216: memmove(keys, ipkl->keys,
! 217: ipkl->allocated * sizeof(dns_name_t *));
! 218: isc_mem_put(mctx, ipkl->keys,
! 219: ipkl->allocated * sizeof(dns_name_t *));
! 220: }
! 221: ipkl->keys = keys;
! 222: memset(&ipkl->keys[ipkl->allocated], 0,
! 223: (n - ipkl->allocated) * sizeof(dns_name_t *));
! 224:
! 225: if (ipkl->labels != NULL) {
! 226: memmove(labels, ipkl->labels,
! 227: ipkl->allocated * sizeof(dns_name_t *));
! 228: isc_mem_put(mctx, ipkl->labels,
! 229: ipkl->allocated * sizeof(dns_name_t *));
! 230: }
! 231: ipkl->labels = labels;
! 232: memset(&ipkl->labels[ipkl->allocated], 0,
! 233: (n - ipkl->allocated) * sizeof(dns_name_t *));
! 234:
! 235: ipkl->allocated = n;
! 236: return (ISC_R_SUCCESS);
! 237:
! 238: nomemory:
! 239: if (addrs != NULL)
! 240: isc_mem_put(mctx, addrs, n * sizeof(isc_sockaddr_t));
! 241: if (dscps != NULL)
! 242: isc_mem_put(mctx, dscps, n * sizeof(isc_dscp_t));
! 243: if (keys != NULL)
! 244: isc_mem_put(mctx, keys, n * sizeof(dns_name_t *));
! 245: if (labels != NULL)
! 246: isc_mem_put(mctx, labels, n * sizeof(dns_name_t *));
! 247:
! 248: return (ISC_R_NOMEMORY);
! 249: }
CVSweb <webmaster@jp.NetBSD.org>