Merge from vendor branch BIND:
[dragonfly.git] / contrib / bind-9.2.4rc7 / bin / named / lwsearch.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2000, 2001  Internet Software Consortium.
4  *
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.
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: lwsearch.c,v 1.7.2.1 2004/03/09 06:09:19 marka Exp $ */
19
20 #include <config.h>
21
22 #include <isc/magic.h>
23 #include <isc/mem.h>
24 #include <isc/mutex.h>
25 #include <isc/result.h>
26 #include <isc/types.h>
27 #include <isc/util.h>
28
29 #include <dns/name.h>
30 #include <dns/types.h>
31
32 #include <named/lwsearch.h>
33 #include <named/types.h>
34
35 #define LWSEARCHLIST_MAGIC              ISC_MAGIC('L', 'W', 'S', 'L')
36 #define VALID_LWSEARCHLIST(l)           ISC_MAGIC_VALID(l, LWSEARCHLIST_MAGIC)
37
38 isc_result_t
39 ns_lwsearchlist_create(isc_mem_t *mctx, ns_lwsearchlist_t **listp) {
40         ns_lwsearchlist_t *list;
41
42         REQUIRE(mctx != NULL);
43         REQUIRE(listp != NULL && *listp == NULL);
44
45         list = isc_mem_get(mctx, sizeof(ns_lwsearchlist_t));
46         if (list == NULL)
47                 return (ISC_R_NOMEMORY);
48         
49         RUNTIME_CHECK(isc_mutex_init(&list->lock) == ISC_R_SUCCESS);
50         list->mctx = NULL;
51         isc_mem_attach(mctx, &list->mctx);
52         list->refs = 1;
53         ISC_LIST_INIT(list->names);
54         list->magic = LWSEARCHLIST_MAGIC;
55
56         *listp = list;
57         return (ISC_R_SUCCESS);
58 }
59
60 void
61 ns_lwsearchlist_attach(ns_lwsearchlist_t *source, ns_lwsearchlist_t **target) {
62         REQUIRE(VALID_LWSEARCHLIST(source));
63         REQUIRE(target != NULL && *target == NULL);
64
65         LOCK(&source->lock);
66         INSIST(source->refs > 0);
67         source->refs++;
68         INSIST(source->refs != 0);
69         UNLOCK(&source->lock);
70
71         *target = source;
72 }
73
74 void
75 ns_lwsearchlist_detach(ns_lwsearchlist_t **listp) {
76         ns_lwsearchlist_t *list;
77         isc_mem_t *mctx;
78
79         REQUIRE(listp != NULL);
80         list = *listp;
81         REQUIRE(VALID_LWSEARCHLIST(list));
82
83         LOCK(&list->lock);
84         INSIST(list->refs > 0);
85         list->refs--;
86         UNLOCK(&list->lock);
87
88         *listp = NULL;
89         if (list->refs != 0)
90                 return;
91
92         mctx = list->mctx;
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));
98         }
99         list->magic = 0;
100         isc_mem_put(mctx, list, sizeof(ns_lwsearchlist_t));
101         isc_mem_detach(&mctx);
102 }
103
104 isc_result_t
105 ns_lwsearchlist_append(ns_lwsearchlist_t *list, dns_name_t *name) {
106         dns_name_t *newname;
107         isc_result_t result;
108
109         REQUIRE(VALID_LWSEARCHLIST(list));
110         REQUIRE(name != NULL);
111
112         newname = isc_mem_get(list->mctx, sizeof(dns_name_t));
113         if (newname == NULL)
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));
119                 return (result);
120         }
121         ISC_LINK_INIT(newname, link);
122         ISC_LIST_APPEND(list->names, newname, link);
123         return (ISC_R_SUCCESS);
124 }
125
126 void
127 ns_lwsearchctx_init(ns_lwsearchctx_t *sctx, ns_lwsearchlist_t *list,
128                     dns_name_t *name, unsigned int ndots)
129 {
130         INSIST(sctx != NULL);
131         sctx->relname = name;
132         sctx->searchname = NULL;
133         sctx->doneexact = ISC_FALSE;
134         sctx->exactfirst = ISC_FALSE;
135         sctx->ndots = ndots;
136         if (dns_name_isabsolute(name) || list == NULL) {
137                 sctx->list = NULL;
138                 return;
139         }
140         sctx->list = list;
141         sctx->searchname = ISC_LIST_HEAD(sctx->list->names);
142         if (dns_name_countlabels(name) > ndots)
143                 sctx->exactfirst = ISC_TRUE;
144 }
145
146 void
147 ns_lwsearchctx_first(ns_lwsearchctx_t *sctx) {
148         REQUIRE(sctx != NULL);
149         UNUSED(sctx);
150 }
151
152 isc_result_t
153 ns_lwsearchctx_next(ns_lwsearchctx_t *sctx) {
154         REQUIRE(sctx != NULL);
155
156         if (sctx->list == NULL)
157                 return (ISC_R_NOMORE);
158
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;
164         } else {
165                 if (sctx->exactfirst && !sctx->doneexact)
166                         sctx->doneexact = ISC_TRUE;
167                 else {
168                         sctx->searchname = ISC_LIST_NEXT(sctx->searchname,
169                                                          link);
170                         if (sctx->searchname == NULL && sctx->doneexact)
171                                 return (ISC_R_NOMORE);
172                 }
173         }
174
175         return (ISC_R_SUCCESS);
176 }
177
178 isc_result_t
179 ns_lwsearchctx_current(ns_lwsearchctx_t *sctx, dns_name_t *absname) {
180         dns_name_t *tname;
181         isc_boolean_t useexact = ISC_FALSE;
182
183         REQUIRE(sctx != NULL);
184
185         if (sctx->list == NULL ||
186             sctx->searchname == NULL ||
187             (sctx->exactfirst && !sctx->doneexact))
188                 useexact = ISC_TRUE;
189
190         if (useexact) {
191                 if (dns_name_isabsolute(sctx->relname))
192                         tname = NULL;
193                 else
194                         tname = dns_rootname;
195         } else
196                 tname = sctx->searchname;
197
198         return (dns_name_concatenate(sctx->relname, tname, absname, NULL));
199 }