1 /* $NetBSD: byaddr.c,v 1.1 2024/02/18 20:57:30 christos Exp $ */
2
3 /*
4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 *
6 * SPDX-License-Identifier: MPL-2.0
7 *
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 *
12 * See the COPYRIGHT file distributed with this work for additional
13 * information regarding copyright ownership.
14 */
15
16 /*! \file */
17
18 #include <stdbool.h>
19
20 #include <isc/mem.h>
21 #include <isc/netaddr.h>
22 #include <isc/print.h>
23 #include <isc/string.h> /* Required for HP/UX (and others?) */
24 #include <isc/task.h>
25 #include <isc/util.h>
26
27 #include <dns/byaddr.h>
28 #include <dns/db.h>
29 #include <dns/events.h>
30 #include <dns/lookup.h>
31 #include <dns/rdata.h>
32 #include <dns/rdataset.h>
33 #include <dns/rdatastruct.h>
34 #include <dns/resolver.h>
35 #include <dns/result.h>
36 #include <dns/view.h>
37
38 /*
39 * XXXRTH We could use a static event...
40 */
41
42 static char hex_digits[] = { '0', '1', '2', '3', '4', '5', '6', '7',
43 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
44
45 isc_result_t
dns_byaddr_createptrname(const isc_netaddr_t * address,unsigned int options,dns_name_t * name)46 dns_byaddr_createptrname(const isc_netaddr_t *address, unsigned int options,
47 dns_name_t *name) {
48 char textname[128];
49 const unsigned char *bytes;
50 int i;
51 char *cp;
52 isc_buffer_t buffer;
53 unsigned int len;
54
55 REQUIRE(address != NULL);
56 UNUSED(options);
57
58 /*
59 * We create the text representation and then convert to a
60 * dns_name_t. This is not maximally efficient, but it keeps all
61 * of the knowledge of wire format in the dns_name_ routines.
62 */
63
64 bytes = (const unsigned char *)(&address->type);
65 if (address->family == AF_INET) {
66 (void)snprintf(textname, sizeof(textname),
67 "%u.%u.%u.%u.in-addr.arpa.",
68 ((unsigned int)bytes[3] & 0xffU),
69 ((unsigned int)bytes[2] & 0xffU),
70 ((unsigned int)bytes[1] & 0xffU),
71 ((unsigned int)bytes[0] & 0xffU));
72 } else if (address->family == AF_INET6) {
73 size_t remaining;
74
75 cp = textname;
76 for (i = 15; i >= 0; i--) {
77 *cp++ = hex_digits[bytes[i] & 0x0f];
78 *cp++ = '.';
79 *cp++ = hex_digits[(bytes[i] >> 4) & 0x0f];
80 *cp++ = '.';
81 }
82 remaining = sizeof(textname) - (cp - textname);
83 strlcpy(cp, "ip6.arpa.", remaining);
84 } else {
85 return (ISC_R_NOTIMPLEMENTED);
86 }
87
88 len = (unsigned int)strlen(textname);
89 isc_buffer_init(&buffer, textname, len);
90 isc_buffer_add(&buffer, len);
91 return (dns_name_fromtext(name, &buffer, dns_rootname, 0, NULL));
92 }
93
94 struct dns_byaddr {
95 /* Unlocked. */
96 unsigned int magic;
97 isc_mem_t *mctx;
98 isc_mutex_t lock;
99 dns_fixedname_t name;
100 /* Locked by lock. */
101 unsigned int options;
102 dns_lookup_t *lookup;
103 isc_task_t *task;
104 dns_byaddrevent_t *event;
105 bool canceled;
106 };
107
108 #define BYADDR_MAGIC ISC_MAGIC('B', 'y', 'A', 'd')
109 #define VALID_BYADDR(b) ISC_MAGIC_VALID(b, BYADDR_MAGIC)
110
111 #define MAX_RESTARTS 16
112
113 static isc_result_t
copy_ptr_targets(dns_byaddr_t * byaddr,dns_rdataset_t * rdataset)114 copy_ptr_targets(dns_byaddr_t *byaddr, dns_rdataset_t *rdataset) {
115 isc_result_t result;
116 dns_name_t *name;
117 dns_rdata_t rdata = DNS_RDATA_INIT;
118
119 /*
120 * The caller must be holding the byaddr's lock.
121 */
122
123 result = dns_rdataset_first(rdataset);
124 while (result == ISC_R_SUCCESS) {
125 dns_rdata_ptr_t ptr;
126 dns_rdataset_current(rdataset, &rdata);
127 result = dns_rdata_tostruct(&rdata, &ptr, NULL);
128 if (result != ISC_R_SUCCESS) {
129 return (result);
130 }
131 name = isc_mem_get(byaddr->mctx, sizeof(*name));
132 dns_name_init(name, NULL);
133 dns_name_dup(&ptr.ptr, byaddr->mctx, name);
134 dns_rdata_freestruct(&ptr);
135 ISC_LIST_APPEND(byaddr->event->names, name, link);
136 dns_rdata_reset(&rdata);
137 result = dns_rdataset_next(rdataset);
138 }
139 if (result == ISC_R_NOMORE) {
140 result = ISC_R_SUCCESS;
141 }
142
143 return (result);
144 }
145
146 static void
lookup_done(isc_task_t * task,isc_event_t * event)147 lookup_done(isc_task_t *task, isc_event_t *event) {
148 dns_byaddr_t *byaddr = event->ev_arg;
149 dns_lookupevent_t *levent;
150 isc_result_t result;
151
152 REQUIRE(event->ev_type == DNS_EVENT_LOOKUPDONE);
153 REQUIRE(VALID_BYADDR(byaddr));
154 REQUIRE(byaddr->task == task);
155
156 UNUSED(task);
157
158 levent = (dns_lookupevent_t *)event;
159
160 if (levent->result == ISC_R_SUCCESS) {
161 result = copy_ptr_targets(byaddr, levent->rdataset);
162 byaddr->event->result = result;
163 } else {
164 byaddr->event->result = levent->result;
165 }
166 isc_event_free(&event);
167 isc_task_sendanddetach(&byaddr->task, (isc_event_t **)(void *)&byaddr->event);
168 }
169
170 static void
bevent_destroy(isc_event_t * event)171 bevent_destroy(isc_event_t *event) {
172 dns_byaddrevent_t *bevent;
173 dns_name_t *name, *next_name;
174 isc_mem_t *mctx;
175
176 REQUIRE(event->ev_type == DNS_EVENT_BYADDRDONE);
177 mctx = event->ev_destroy_arg;
178 bevent = (dns_byaddrevent_t *)event;
179
180 for (name = ISC_LIST_HEAD(bevent->names); name != NULL;
181 name = next_name)
182 {
183 next_name = ISC_LIST_NEXT(name, link);
184 ISC_LIST_UNLINK(bevent->names, name, link);
185 dns_name_free(name, mctx);
186 isc_mem_put(mctx, name, sizeof(*name));
187 }
188 isc_mem_put(mctx, event, event->ev_size);
189 }
190
191 isc_result_t
dns_byaddr_create(isc_mem_t * mctx,const isc_netaddr_t * address,dns_view_t * view,unsigned int options,isc_task_t * task,isc_taskaction_t action,void * arg,dns_byaddr_t ** byaddrp)192 dns_byaddr_create(isc_mem_t *mctx, const isc_netaddr_t *address,
193 dns_view_t *view, unsigned int options, isc_task_t *task,
194 isc_taskaction_t action, void *arg, dns_byaddr_t **byaddrp) {
195 isc_result_t result;
196 dns_byaddr_t *byaddr;
197 isc_event_t *ievent;
198
199 byaddr = isc_mem_get(mctx, sizeof(*byaddr));
200 byaddr->mctx = NULL;
201 isc_mem_attach(mctx, &byaddr->mctx);
202 byaddr->options = options;
203
204 byaddr->event = isc_mem_get(mctx, sizeof(*byaddr->event));
205 ISC_EVENT_INIT(byaddr->event, sizeof(*byaddr->event), 0, NULL,
206 DNS_EVENT_BYADDRDONE, action, arg, byaddr,
207 bevent_destroy, mctx);
208 byaddr->event->result = ISC_R_FAILURE;
209 ISC_LIST_INIT(byaddr->event->names);
210
211 byaddr->task = NULL;
212 isc_task_attach(task, &byaddr->task);
213
214 isc_mutex_init(&byaddr->lock);
215
216 dns_fixedname_init(&byaddr->name);
217
218 result = dns_byaddr_createptrname(address, options,
219 dns_fixedname_name(&byaddr->name));
220 if (result != ISC_R_SUCCESS) {
221 goto cleanup_lock;
222 }
223
224 byaddr->lookup = NULL;
225 result = dns_lookup_create(mctx, dns_fixedname_name(&byaddr->name),
226 dns_rdatatype_ptr, view, 0, task,
227 lookup_done, byaddr, &byaddr->lookup);
228 if (result != ISC_R_SUCCESS) {
229 goto cleanup_lock;
230 }
231
232 byaddr->canceled = false;
233 byaddr->magic = BYADDR_MAGIC;
234
235 *byaddrp = byaddr;
236
237 return (ISC_R_SUCCESS);
238
239 cleanup_lock:
240 isc_mutex_destroy(&byaddr->lock);
241
242 ievent = (isc_event_t *)byaddr->event;
243 isc_event_free(&ievent);
244 byaddr->event = NULL;
245
246 isc_task_detach(&byaddr->task);
247
248 isc_mem_putanddetach(&mctx, byaddr, sizeof(*byaddr));
249
250 return (result);
251 }
252
253 void
dns_byaddr_cancel(dns_byaddr_t * byaddr)254 dns_byaddr_cancel(dns_byaddr_t *byaddr) {
255 REQUIRE(VALID_BYADDR(byaddr));
256
257 LOCK(&byaddr->lock);
258
259 if (!byaddr->canceled) {
260 byaddr->canceled = true;
261 if (byaddr->lookup != NULL) {
262 dns_lookup_cancel(byaddr->lookup);
263 }
264 }
265
266 UNLOCK(&byaddr->lock);
267 }
268
269 void
dns_byaddr_destroy(dns_byaddr_t ** byaddrp)270 dns_byaddr_destroy(dns_byaddr_t **byaddrp) {
271 dns_byaddr_t *byaddr;
272
273 REQUIRE(byaddrp != NULL);
274 byaddr = *byaddrp;
275 *byaddrp = NULL;
276 REQUIRE(VALID_BYADDR(byaddr));
277 REQUIRE(byaddr->event == NULL);
278 REQUIRE(byaddr->task == NULL);
279 dns_lookup_destroy(&byaddr->lookup);
280
281 isc_mutex_destroy(&byaddr->lock);
282 byaddr->magic = 0;
283 isc_mem_putanddetach(&byaddr->mctx, byaddr, sizeof(*byaddr));
284 }
285