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