2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and 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: lwsearch.c,v 1.7.2.1 2004/03/09 06:09:19 marka Exp $ */
22 #include <isc/magic.h>
24 #include <isc/mutex.h>
25 #include <isc/result.h>
26 #include <isc/types.h>
30 #include <dns/types.h>
32 #include <named/lwsearch.h>
33 #include <named/types.h>
35 #define LWSEARCHLIST_MAGIC ISC_MAGIC('L', 'W', 'S', 'L')
36 #define VALID_LWSEARCHLIST(l) ISC_MAGIC_VALID(l, LWSEARCHLIST_MAGIC)
39 ns_lwsearchlist_create(isc_mem_t *mctx, ns_lwsearchlist_t **listp) {
40 ns_lwsearchlist_t *list;
42 REQUIRE(mctx != NULL);
43 REQUIRE(listp != NULL && *listp == NULL);
45 list = isc_mem_get(mctx, sizeof(ns_lwsearchlist_t));
47 return (ISC_R_NOMEMORY);
49 RUNTIME_CHECK(isc_mutex_init(&list->lock) == ISC_R_SUCCESS);
51 isc_mem_attach(mctx, &list->mctx);
53 ISC_LIST_INIT(list->names);
54 list->magic = LWSEARCHLIST_MAGIC;
57 return (ISC_R_SUCCESS);
61 ns_lwsearchlist_attach(ns_lwsearchlist_t *source, ns_lwsearchlist_t **target) {
62 REQUIRE(VALID_LWSEARCHLIST(source));
63 REQUIRE(target != NULL && *target == NULL);
66 INSIST(source->refs > 0);
68 INSIST(source->refs != 0);
69 UNLOCK(&source->lock);
75 ns_lwsearchlist_detach(ns_lwsearchlist_t **listp) {
76 ns_lwsearchlist_t *list;
79 REQUIRE(listp != NULL);
81 REQUIRE(VALID_LWSEARCHLIST(list));
84 INSIST(list->refs > 0);
93 while (!ISC_LIST_EMPTY(list->names)) {
94 dns_name_t *name = ISC_LIST_HEAD(list->names);
95 ISC_LIST_UNLINK(list->names, name, link);
96 dns_name_free(name, list->mctx);
97 isc_mem_put(list->mctx, name, sizeof(dns_name_t));
100 isc_mem_put(mctx, list, sizeof(ns_lwsearchlist_t));
101 isc_mem_detach(&mctx);
105 ns_lwsearchlist_append(ns_lwsearchlist_t *list, dns_name_t *name) {
109 REQUIRE(VALID_LWSEARCHLIST(list));
110 REQUIRE(name != NULL);
112 newname = isc_mem_get(list->mctx, sizeof(dns_name_t));
114 return (ISC_R_NOMEMORY);
115 dns_name_init(newname, NULL);
116 result = dns_name_dup(name, list->mctx, newname);
117 if (result != ISC_R_SUCCESS) {
118 isc_mem_put(list->mctx, newname, sizeof(dns_name_t));
121 ISC_LINK_INIT(newname, link);
122 ISC_LIST_APPEND(list->names, newname, link);
123 return (ISC_R_SUCCESS);
127 ns_lwsearchctx_init(ns_lwsearchctx_t *sctx, ns_lwsearchlist_t *list,
128 dns_name_t *name, unsigned int ndots)
130 INSIST(sctx != NULL);
131 sctx->relname = name;
132 sctx->searchname = NULL;
133 sctx->doneexact = ISC_FALSE;
134 sctx->exactfirst = ISC_FALSE;
136 if (dns_name_isabsolute(name) || list == NULL) {
141 sctx->searchname = ISC_LIST_HEAD(sctx->list->names);
142 if (dns_name_countlabels(name) > ndots)
143 sctx->exactfirst = ISC_TRUE;
147 ns_lwsearchctx_first(ns_lwsearchctx_t *sctx) {
148 REQUIRE(sctx != NULL);
153 ns_lwsearchctx_next(ns_lwsearchctx_t *sctx) {
154 REQUIRE(sctx != NULL);
156 if (sctx->list == NULL)
157 return (ISC_R_NOMORE);
159 if (sctx->searchname == NULL) {
160 INSIST (!sctx->exactfirst || sctx->doneexact);
161 if (sctx->exactfirst || sctx->doneexact)
162 return (ISC_R_NOMORE);
163 sctx->doneexact = ISC_TRUE;
165 if (sctx->exactfirst && !sctx->doneexact)
166 sctx->doneexact = ISC_TRUE;
168 sctx->searchname = ISC_LIST_NEXT(sctx->searchname,
170 if (sctx->searchname == NULL && sctx->doneexact)
171 return (ISC_R_NOMORE);
175 return (ISC_R_SUCCESS);
179 ns_lwsearchctx_current(ns_lwsearchctx_t *sctx, dns_name_t *absname) {
181 isc_boolean_t useexact = ISC_FALSE;
183 REQUIRE(sctx != NULL);
185 if (sctx->list == NULL ||
186 sctx->searchname == NULL ||
187 (sctx->exactfirst && !sctx->doneexact))
191 if (dns_name_isabsolute(sctx->relname))
194 tname = dns_rootname;
196 tname = sctx->searchname;
198 return (dns_name_concatenate(sctx->relname, tname, absname, NULL));