1/*
2 * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001  Internet Software Consortium.
4 *
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.
8 *
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.
16 */
17
18/* $Id: listenlist.c,v 1.14 2007/06/19 23:46:59 tbox Exp $ */
19
20/*! \file */
21
22#include <config.h>
23
24#include <isc/mem.h>
25#include <isc/util.h>
26
27#include <dns/acl.h>
28
29#include <named/listenlist.h>
30
31static void
32destroy(ns_listenlist_t *list);
33
34isc_result_t
35ns_listenelt_create(isc_mem_t *mctx, in_port_t port,
36		    dns_acl_t *acl, ns_listenelt_t **target)
37{
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);
49}
50
51void
52ns_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));
56}
57
58isc_result_t
59ns_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);
70}
71
72static void
73destroy(ns_listenlist_t *list) {
74	ns_listenelt_t *elt, *next;
75	for (elt = ISC_LIST_HEAD(list->elts);
76	     elt != NULL;
77	     elt = next)
78	{
79		next = ISC_LIST_NEXT(elt, link);
80		ns_listenelt_destroy(elt);
81	}
82	isc_mem_put(list->mctx, list, sizeof(*list));
83}
84
85void
86ns_listenlist_attach(ns_listenlist_t *source, ns_listenlist_t **target) {
87	INSIST(source->refcount > 0);
88	source->refcount++;
89	*target = source;
90}
91
92void
93ns_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;
100}
101
102isc_result_t
103ns_listenlist_default(isc_mem_t *mctx, in_port_t port,
104		      isc_boolean_t enabled, ns_listenlist_t **target)
105{
106	isc_result_t result;
107	dns_acl_t *acl = NULL;
108	ns_listenelt_t *elt = NULL;
109	ns_listenlist_t *list = NULL;
110
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;
118
119	result = ns_listenelt_create(mctx, port, acl, &elt);
120	if (result != ISC_R_SUCCESS)
121		goto cleanup_acl;
122
123	result = ns_listenlist_create(mctx, &list);
124	if (result != ISC_R_SUCCESS)
125		goto cleanup_listenelt;
126
127	ISC_LIST_APPEND(list->elts, elt, link);
128
129	*target = list;
130	return (ISC_R_SUCCESS);
131
132 cleanup_listenelt:
133	ns_listenelt_destroy(elt);
134 cleanup_acl:
135	dns_acl_detach(&acl);
136 cleanup:
137	return (result);
138}
139