"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.17.5/lib/dns/ipkeylist.c" (4 Sep 2020, 5184 Bytes) of package /linux/misc/dns/bind9/9.17.5/bind-9.17.5.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ipkeylist.c" see the Fossies "Dox" file reference documentation.

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