"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.17.5/lib/ns/listenlist.c" (4 Sep 2020, 2891 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 "listenlist.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 /*! \file */
   13 
   14 #include <stdbool.h>
   15 
   16 #include <isc/mem.h>
   17 #include <isc/util.h>
   18 
   19 #include <dns/acl.h>
   20 
   21 #include <ns/listenlist.h>
   22 
   23 static void
   24 destroy(ns_listenlist_t *list);
   25 
   26 isc_result_t
   27 ns_listenelt_create(isc_mem_t *mctx, in_port_t port, isc_dscp_t dscp,
   28             dns_acl_t *acl, ns_listenelt_t **target) {
   29     ns_listenelt_t *elt = NULL;
   30     REQUIRE(target != NULL && *target == NULL);
   31     elt = isc_mem_get(mctx, sizeof(*elt));
   32     elt->mctx = mctx;
   33     ISC_LINK_INIT(elt, link);
   34     elt->port = port;
   35     elt->dscp = dscp;
   36     elt->acl = acl;
   37     *target = elt;
   38     return (ISC_R_SUCCESS);
   39 }
   40 
   41 void
   42 ns_listenelt_destroy(ns_listenelt_t *elt) {
   43     if (elt->acl != NULL) {
   44         dns_acl_detach(&elt->acl);
   45     }
   46     isc_mem_put(elt->mctx, elt, sizeof(*elt));
   47 }
   48 
   49 isc_result_t
   50 ns_listenlist_create(isc_mem_t *mctx, ns_listenlist_t **target) {
   51     ns_listenlist_t *list = NULL;
   52     REQUIRE(target != NULL && *target == NULL);
   53     list = isc_mem_get(mctx, sizeof(*list));
   54     list->mctx = mctx;
   55     list->refcount = 1;
   56     ISC_LIST_INIT(list->elts);
   57     *target = list;
   58     return (ISC_R_SUCCESS);
   59 }
   60 
   61 static void
   62 destroy(ns_listenlist_t *list) {
   63     ns_listenelt_t *elt, *next;
   64     for (elt = ISC_LIST_HEAD(list->elts); elt != NULL; elt = next) {
   65         next = ISC_LIST_NEXT(elt, link);
   66         ns_listenelt_destroy(elt);
   67     }
   68     isc_mem_put(list->mctx, list, sizeof(*list));
   69 }
   70 
   71 void
   72 ns_listenlist_attach(ns_listenlist_t *source, ns_listenlist_t **target) {
   73     INSIST(source->refcount > 0);
   74     source->refcount++;
   75     *target = source;
   76 }
   77 
   78 void
   79 ns_listenlist_detach(ns_listenlist_t **listp) {
   80     ns_listenlist_t *list = *listp;
   81     *listp = NULL;
   82     INSIST(list->refcount > 0);
   83     list->refcount--;
   84     if (list->refcount == 0) {
   85         destroy(list);
   86     }
   87 }
   88 
   89 isc_result_t
   90 ns_listenlist_default(isc_mem_t *mctx, in_port_t port, isc_dscp_t dscp,
   91               bool enabled, ns_listenlist_t **target) {
   92     isc_result_t result;
   93     dns_acl_t *acl = NULL;
   94     ns_listenelt_t *elt = NULL;
   95     ns_listenlist_t *list = NULL;
   96 
   97     REQUIRE(target != NULL && *target == NULL);
   98     if (enabled) {
   99         result = dns_acl_any(mctx, &acl);
  100     } else {
  101         result = dns_acl_none(mctx, &acl);
  102     }
  103     if (result != ISC_R_SUCCESS) {
  104         goto cleanup;
  105     }
  106 
  107     result = ns_listenelt_create(mctx, port, dscp, acl, &elt);
  108     if (result != ISC_R_SUCCESS) {
  109         goto cleanup_acl;
  110     }
  111 
  112     result = ns_listenlist_create(mctx, &list);
  113     if (result != ISC_R_SUCCESS) {
  114         goto cleanup_listenelt;
  115     }
  116 
  117     ISC_LIST_APPEND(list->elts, elt, link);
  118 
  119     *target = list;
  120     return (ISC_R_SUCCESS);
  121 
  122 cleanup_listenelt:
  123     ns_listenelt_destroy(elt);
  124 cleanup_acl:
  125     dns_acl_detach(&acl);
  126 cleanup:
  127     return (result);
  128 }