BIND - Update BIND to 9.5.2
[dragonfly.git] / contrib / bind-9.5.2 / bin / named / listenlist.c
blob513fe9c70b138aca85267c22c6072620bf3e9d6b
1 /*
2 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: listenlist.c,v 1.14 2007/06/19 23:46:59 tbox Exp $ */
20 /*! \file */
22 #include <config.h>
24 #include <isc/mem.h>
25 #include <isc/util.h>
27 #include <dns/acl.h>
29 #include <named/listenlist.h>
31 static void
32 destroy(ns_listenlist_t *list);
34 isc_result_t
35 ns_listenelt_create(isc_mem_t *mctx, in_port_t port,
36 dns_acl_t *acl, ns_listenelt_t **target)
38 ns_listenelt_t *elt = NULL;
39 REQUIRE(target != NULL && *target == NULL);
40 elt = isc_mem_get(mctx, sizeof(*elt));
41 if (elt == NULL)
42 return (ISC_R_NOMEMORY);
43 elt->mctx = mctx;
44 ISC_LINK_INIT(elt, link);
45 elt->port = port;
46 elt->acl = acl;
47 *target = elt;
48 return (ISC_R_SUCCESS);
51 void
52 ns_listenelt_destroy(ns_listenelt_t *elt) {
53 if (elt->acl != NULL)
54 dns_acl_detach(&elt->acl);
55 isc_mem_put(elt->mctx, elt, sizeof(*elt));
58 isc_result_t
59 ns_listenlist_create(isc_mem_t *mctx, ns_listenlist_t **target) {
60 ns_listenlist_t *list = NULL;
61 REQUIRE(target != NULL && *target == NULL);
62 list = isc_mem_get(mctx, sizeof(*list));
63 if (list == NULL)
64 return (ISC_R_NOMEMORY);
65 list->mctx = mctx;
66 list->refcount = 1;
67 ISC_LIST_INIT(list->elts);
68 *target = list;
69 return (ISC_R_SUCCESS);
72 static void
73 destroy(ns_listenlist_t *list) {
74 ns_listenelt_t *elt, *next;
75 for (elt = ISC_LIST_HEAD(list->elts);
76 elt != NULL;
77 elt = next)
79 next = ISC_LIST_NEXT(elt, link);
80 ns_listenelt_destroy(elt);
82 isc_mem_put(list->mctx, list, sizeof(*list));
85 void
86 ns_listenlist_attach(ns_listenlist_t *source, ns_listenlist_t **target) {
87 INSIST(source->refcount > 0);
88 source->refcount++;
89 *target = source;
92 void
93 ns_listenlist_detach(ns_listenlist_t **listp) {
94 ns_listenlist_t *list = *listp;
95 INSIST(list->refcount > 0);
96 list->refcount--;
97 if (list->refcount == 0)
98 destroy(list);
99 *listp = NULL;
102 isc_result_t
103 ns_listenlist_default(isc_mem_t *mctx, in_port_t port,
104 isc_boolean_t enabled, ns_listenlist_t **target)
106 isc_result_t result;
107 dns_acl_t *acl = NULL;
108 ns_listenelt_t *elt = NULL;
109 ns_listenlist_t *list = NULL;
111 REQUIRE(target != NULL && *target == NULL);
112 if (enabled)
113 result = dns_acl_any(mctx, &acl);
114 else
115 result = dns_acl_none(mctx, &acl);
116 if (result != ISC_R_SUCCESS)
117 goto cleanup;
119 result = ns_listenelt_create(mctx, port, acl, &elt);
120 if (result != ISC_R_SUCCESS)
121 goto cleanup_acl;
123 result = ns_listenlist_create(mctx, &list);
124 if (result != ISC_R_SUCCESS)
125 goto cleanup_listenelt;
127 ISC_LIST_APPEND(list->elts, elt, link);
129 *target = list;
130 return (ISC_R_SUCCESS);
132 cleanup_listenelt:
133 ns_listenelt_destroy(elt);
134 cleanup_acl:
135 dns_acl_detach(&acl);
136 cleanup:
137 return (result);