listenlist.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007, 2013  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 2000, 2001  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id: listenlist.c,v 1.14 2007/06/19 23:46:59 tbox Exp $ */
00019 
00020 /*! \file */
00021 
00022 #include <config.h>
00023 
00024 #include <isc/mem.h>
00025 #include <isc/util.h>
00026 
00027 #include <dns/acl.h>
00028 
00029 #include <named/listenlist.h>
00030 
00031 static void
00032 destroy(ns_listenlist_t *list);
00033 
00034 isc_result_t
00035 ns_listenelt_create(isc_mem_t *mctx, in_port_t port, isc_dscp_t dscp,
00036                     dns_acl_t *acl, ns_listenelt_t **target)
00037 {
00038         ns_listenelt_t *elt = NULL;
00039         REQUIRE(target != NULL && *target == NULL);
00040         elt = isc_mem_get(mctx, sizeof(*elt));
00041         if (elt == NULL)
00042                 return (ISC_R_NOMEMORY);
00043         elt->mctx = mctx;
00044         ISC_LINK_INIT(elt, link);
00045         elt->port = port;
00046         elt->dscp = dscp;
00047         elt->acl = acl;
00048         *target = elt;
00049         return (ISC_R_SUCCESS);
00050 }
00051 
00052 void
00053 ns_listenelt_destroy(ns_listenelt_t *elt) {
00054         if (elt->acl != NULL)
00055                 dns_acl_detach(&elt->acl);
00056         isc_mem_put(elt->mctx, elt, sizeof(*elt));
00057 }
00058 
00059 isc_result_t
00060 ns_listenlist_create(isc_mem_t *mctx, ns_listenlist_t **target) {
00061         ns_listenlist_t *list = NULL;
00062         REQUIRE(target != NULL && *target == NULL);
00063         list = isc_mem_get(mctx, sizeof(*list));
00064         if (list == NULL)
00065                 return (ISC_R_NOMEMORY);
00066         list->mctx = mctx;
00067         list->refcount = 1;
00068         ISC_LIST_INIT(list->elts);
00069         *target = list;
00070         return (ISC_R_SUCCESS);
00071 }
00072 
00073 static void
00074 destroy(ns_listenlist_t *list) {
00075         ns_listenelt_t *elt, *next;
00076         for (elt = ISC_LIST_HEAD(list->elts);
00077              elt != NULL;
00078              elt = next)
00079         {
00080                 next = ISC_LIST_NEXT(elt, link);
00081                 ns_listenelt_destroy(elt);
00082         }
00083         isc_mem_put(list->mctx, list, sizeof(*list));
00084 }
00085 
00086 void
00087 ns_listenlist_attach(ns_listenlist_t *source, ns_listenlist_t **target) {
00088         INSIST(source->refcount > 0);
00089         source->refcount++;
00090         *target = source;
00091 }
00092 
00093 void
00094 ns_listenlist_detach(ns_listenlist_t **listp) {
00095         ns_listenlist_t *list = *listp;
00096         INSIST(list->refcount > 0);
00097         list->refcount--;
00098         if (list->refcount == 0)
00099                 destroy(list);
00100         *listp = NULL;
00101 }
00102 
00103 isc_result_t
00104 ns_listenlist_default(isc_mem_t *mctx, in_port_t port, isc_dscp_t dscp,
00105                       isc_boolean_t enabled, ns_listenlist_t **target)
00106 {
00107         isc_result_t result;
00108         dns_acl_t *acl = NULL;
00109         ns_listenelt_t *elt = NULL;
00110         ns_listenlist_t *list = NULL;
00111 
00112         REQUIRE(target != NULL && *target == NULL);
00113         if (enabled)
00114                 result = dns_acl_any(mctx, &acl);
00115         else
00116                 result = dns_acl_none(mctx, &acl);
00117         if (result != ISC_R_SUCCESS)
00118                 goto cleanup;
00119 
00120         result = ns_listenelt_create(mctx, port, dscp, acl, &elt);
00121         if (result != ISC_R_SUCCESS)
00122                 goto cleanup_acl;
00123 
00124         result = ns_listenlist_create(mctx, &list);
00125         if (result != ISC_R_SUCCESS)
00126                 goto cleanup_listenelt;
00127 
00128         ISC_LIST_APPEND(list->elts, elt, link);
00129 
00130         *target = list;
00131         return (ISC_R_SUCCESS);
00132 
00133  cleanup_listenelt:
00134         ns_listenelt_destroy(elt);
00135  cleanup_acl:
00136         dns_acl_detach(&acl);
00137  cleanup:
00138         return (result);
00139 }

Generated on Tue Apr 28 17:40:54 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha