1/* $NetBSD$ */ 2 3/* 4 * Copyright (C) 2004, 2005, 2007 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 33static void 34destroy(ns_listenlist_t *list); 35 36isc_result_t 37ns_listenelt_create(isc_mem_t *mctx, in_port_t port, 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->acl = acl; 49 *target = elt; 50 return (ISC_R_SUCCESS); 51} 52 53void 54ns_listenelt_destroy(ns_listenelt_t *elt) { 55 if (elt->acl != NULL) 56 dns_acl_detach(&elt->acl); 57 isc_mem_put(elt->mctx, elt, sizeof(*elt)); 58} 59 60isc_result_t 61ns_listenlist_create(isc_mem_t *mctx, ns_listenlist_t **target) { 62 ns_listenlist_t *list = NULL; 63 REQUIRE(target != NULL && *target == NULL); 64 list = isc_mem_get(mctx, sizeof(*list)); 65 if (list == NULL) 66 return (ISC_R_NOMEMORY); 67 list->mctx = mctx; 68 list->refcount = 1; 69 ISC_LIST_INIT(list->elts); 70 *target = list; 71 return (ISC_R_SUCCESS); 72} 73 74static void 75destroy(ns_listenlist_t *list) { 76 ns_listenelt_t *elt, *next; 77 for (elt = ISC_LIST_HEAD(list->elts); 78 elt != NULL; 79 elt = next) 80 { 81 next = ISC_LIST_NEXT(elt, link); 82 ns_listenelt_destroy(elt); 83 } 84 isc_mem_put(list->mctx, list, sizeof(*list)); 85} 86 87void 88ns_listenlist_attach(ns_listenlist_t *source, ns_listenlist_t **target) { 89 INSIST(source->refcount > 0); 90 source->refcount++; 91 *target = source; 92} 93 94void 95ns_listenlist_detach(ns_listenlist_t **listp) { 96 ns_listenlist_t *list = *listp; 97 INSIST(list->refcount > 0); 98 list->refcount--; 99 if (list->refcount == 0) 100 destroy(list); 101 *listp = NULL; 102} 103 104isc_result_t 105ns_listenlist_default(isc_mem_t *mctx, in_port_t port, 106 isc_boolean_t enabled, ns_listenlist_t **target) 107{ 108 isc_result_t result; 109 dns_acl_t *acl = NULL; 110 ns_listenelt_t *elt = NULL; 111 ns_listenlist_t *list = NULL; 112 113 REQUIRE(target != NULL && *target == NULL); 114 if (enabled) 115 result = dns_acl_any(mctx, &acl); 116 else 117 result = dns_acl_none(mctx, &acl); 118 if (result != ISC_R_SUCCESS) 119 goto cleanup; 120 121 result = ns_listenelt_create(mctx, port, acl, &elt); 122 if (result != ISC_R_SUCCESS) 123 goto cleanup_acl; 124 125 result = ns_listenlist_create(mctx, &list); 126 if (result != ISC_R_SUCCESS) 127 goto cleanup_listenelt; 128 129 ISC_LIST_APPEND(list->elts, elt, link); 130 131 *target = list; 132 return (ISC_R_SUCCESS); 133 134 cleanup_listenelt: 135 ns_listenelt_destroy(elt); 136 cleanup_acl: 137 dns_acl_detach(&acl); 138 cleanup: 139 return (result); 140} 141