2000-01-12 20:05:15 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2000-01-12 20:05:15 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
|
|
|
|
/*! \file */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2000-01-18 19:07:39 +00:00
|
|
|
#include <isc/mem.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/netaddr.h>
|
2004-08-28 06:20:14 +00:00
|
|
|
#include <isc/print.h>
|
2000-05-08 19:23:32 +00:00
|
|
|
#include <isc/string.h> /* Required for HP/UX (and others?) */
|
2000-01-12 20:05:15 +00:00
|
|
|
#include <isc/task.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
|
|
|
#include <dns/byaddr.h>
|
2000-01-15 02:20:49 +00:00
|
|
|
#include <dns/db.h>
|
2000-01-15 00:48:49 +00:00
|
|
|
#include <dns/events.h>
|
2000-10-17 01:57:42 +00:00
|
|
|
#include <dns/lookup.h>
|
2000-01-15 02:20:49 +00:00
|
|
|
#include <dns/rdata.h>
|
2000-01-15 00:48:49 +00:00
|
|
|
#include <dns/rdataset.h>
|
2000-10-07 00:09:28 +00:00
|
|
|
#include <dns/rdatastruct.h>
|
2000-01-12 20:05:15 +00:00
|
|
|
#include <dns/resolver.h>
|
2000-05-02 03:54:17 +00:00
|
|
|
#include <dns/result.h>
|
2000-01-12 20:05:15 +00:00
|
|
|
#include <dns/view.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXXRTH We could use a static event...
|
|
|
|
*/
|
|
|
|
|
2000-01-18 02:50:23 +00:00
|
|
|
static char hex_digits[] = {
|
2000-08-01 01:33:37 +00:00
|
|
|
'0', '1', '2', '3', '4', '5', '6', '7',
|
2000-01-18 02:50:23 +00:00
|
|
|
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
|
|
|
|
};
|
|
|
|
|
2000-08-14 19:09:56 +00:00
|
|
|
isc_result_t
|
2018-04-03 13:10:07 +02:00
|
|
|
dns_byaddr_createptrname(const isc_netaddr_t *address, unsigned int options,
|
2000-08-14 19:09:56 +00:00
|
|
|
dns_name_t *name)
|
|
|
|
{
|
2000-01-18 02:50:23 +00:00
|
|
|
char textname[128];
|
2016-12-30 15:45:08 +11:00
|
|
|
const unsigned char *bytes;
|
2000-01-18 02:50:23 +00:00
|
|
|
int i;
|
|
|
|
char *cp;
|
|
|
|
isc_buffer_t buffer;
|
|
|
|
unsigned int len;
|
2000-01-15 02:20:49 +00:00
|
|
|
|
2000-08-14 19:09:56 +00:00
|
|
|
REQUIRE(address != NULL);
|
2018-11-02 14:23:01 +00:00
|
|
|
UNUSED(options);
|
2000-01-15 02:20:49 +00:00
|
|
|
|
2000-01-18 02:50:23 +00:00
|
|
|
/*
|
|
|
|
* We create the text representation and then convert to a
|
|
|
|
* dns_name_t. This is not maximally efficient, but it keeps all
|
|
|
|
* of the knowledge of wire format in the dns_name_ routines.
|
|
|
|
*/
|
2000-01-15 02:20:49 +00:00
|
|
|
|
2016-12-30 15:45:08 +11:00
|
|
|
bytes = (const unsigned char *)(&address->type);
|
2000-01-18 02:50:23 +00:00
|
|
|
if (address->family == AF_INET) {
|
2003-04-11 07:25:31 +00:00
|
|
|
(void)snprintf(textname, sizeof(textname),
|
|
|
|
"%u.%u.%u.%u.in-addr.arpa.",
|
2019-03-14 19:46:10 +11:00
|
|
|
((unsigned int)bytes[3] & 0xffU),
|
|
|
|
((unsigned int)bytes[2] & 0xffU),
|
|
|
|
((unsigned int)bytes[1] & 0xffU),
|
|
|
|
((unsigned int)bytes[0] & 0xffU));
|
2000-01-18 02:50:23 +00:00
|
|
|
} else if (address->family == AF_INET6) {
|
2017-09-13 00:14:37 -07:00
|
|
|
size_t remaining;
|
|
|
|
|
2002-08-27 04:53:43 +00:00
|
|
|
cp = textname;
|
|
|
|
for (i = 15; i >= 0; i--) {
|
|
|
|
*cp++ = hex_digits[bytes[i] & 0x0f];
|
|
|
|
*cp++ = '.';
|
|
|
|
*cp++ = hex_digits[(bytes[i] >> 4) & 0x0f];
|
|
|
|
*cp++ = '.';
|
2000-01-18 02:50:23 +00:00
|
|
|
}
|
2017-09-13 00:14:37 -07:00
|
|
|
remaining = sizeof(textname) - (cp - textname);
|
2018-11-02 14:23:01 +00:00
|
|
|
strlcpy(cp, "ip6.arpa.", remaining);
|
2000-01-18 02:50:23 +00:00
|
|
|
} else
|
2000-04-06 22:03:35 +00:00
|
|
|
return (ISC_R_NOTIMPLEMENTED);
|
2000-01-18 02:50:23 +00:00
|
|
|
|
|
|
|
len = (unsigned int)strlen(textname);
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_init(&buffer, textname, len);
|
2000-01-18 02:50:23 +00:00
|
|
|
isc_buffer_add(&buffer, len);
|
2009-09-01 00:22:28 +00:00
|
|
|
return (dns_name_fromtext(name, &buffer, dns_rootname, 0, NULL));
|
2000-08-14 19:09:56 +00:00
|
|
|
}
|
|
|
|
|
2009-09-01 00:22:28 +00:00
|
|
|
struct dns_byaddr {
|
|
|
|
/* Unlocked. */
|
|
|
|
unsigned int magic;
|
|
|
|
isc_mem_t * mctx;
|
|
|
|
isc_mutex_t lock;
|
|
|
|
dns_fixedname_t name;
|
|
|
|
/* Locked by lock. */
|
|
|
|
unsigned int options;
|
|
|
|
dns_lookup_t * lookup;
|
|
|
|
isc_task_t * task;
|
|
|
|
dns_byaddrevent_t * event;
|
2018-04-17 08:29:14 -07:00
|
|
|
bool canceled;
|
2009-09-01 00:22:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define BYADDR_MAGIC ISC_MAGIC('B', 'y', 'A', 'd')
|
|
|
|
#define VALID_BYADDR(b) ISC_MAGIC_VALID(b, BYADDR_MAGIC)
|
|
|
|
|
|
|
|
#define MAX_RESTARTS 16
|
|
|
|
|
2000-01-15 02:20:49 +00:00
|
|
|
static inline isc_result_t
|
2000-10-17 01:57:42 +00:00
|
|
|
copy_ptr_targets(dns_byaddr_t *byaddr, dns_rdataset_t *rdataset) {
|
2000-01-18 19:07:39 +00:00
|
|
|
isc_result_t result;
|
|
|
|
dns_name_t *name;
|
2000-10-25 04:26:57 +00:00
|
|
|
dns_rdata_t rdata = DNS_RDATA_INIT;
|
2000-01-15 02:20:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The caller must be holding the byaddr's lock.
|
|
|
|
*/
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-10-17 01:57:42 +00:00
|
|
|
result = dns_rdataset_first(rdataset);
|
2000-01-18 19:07:39 +00:00
|
|
|
while (result == ISC_R_SUCCESS) {
|
2000-10-07 00:09:28 +00:00
|
|
|
dns_rdata_ptr_t ptr;
|
2000-10-17 01:57:42 +00:00
|
|
|
dns_rdataset_current(rdataset, &rdata);
|
2000-10-07 00:09:28 +00:00
|
|
|
result = dns_rdata_tostruct(&rdata, &ptr, NULL);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
2001-11-12 19:05:39 +00:00
|
|
|
name = isc_mem_get(byaddr->mctx, sizeof(*name));
|
2000-01-18 19:07:39 +00:00
|
|
|
dns_name_init(name, NULL);
|
2019-11-01 08:31:13 -05:00
|
|
|
dns_name_dup(&ptr.ptr, byaddr->mctx, name);
|
2000-10-07 00:09:28 +00:00
|
|
|
dns_rdata_freestruct(&ptr);
|
2000-01-18 19:07:39 +00:00
|
|
|
ISC_LIST_APPEND(byaddr->event->names, name, link);
|
2000-10-31 03:22:05 +00:00
|
|
|
dns_rdata_reset(&rdata);
|
2000-10-17 01:57:42 +00:00
|
|
|
result = dns_rdataset_next(rdataset);
|
2000-01-18 19:07:39 +00:00
|
|
|
}
|
2000-04-06 22:03:35 +00:00
|
|
|
if (result == ISC_R_NOMORE)
|
2000-01-18 19:07:39 +00:00
|
|
|
result = ISC_R_SUCCESS;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-01-18 19:07:39 +00:00
|
|
|
return (result);
|
2000-01-15 02:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-10-17 01:57:42 +00:00
|
|
|
lookup_done(isc_task_t *task, isc_event_t *event) {
|
2000-04-17 19:22:44 +00:00
|
|
|
dns_byaddr_t *byaddr = event->ev_arg;
|
2000-10-17 01:57:42 +00:00
|
|
|
dns_lookupevent_t *levent;
|
|
|
|
isc_result_t result;
|
2000-01-15 02:20:49 +00:00
|
|
|
|
2000-10-17 01:57:42 +00:00
|
|
|
REQUIRE(event->ev_type == DNS_EVENT_LOOKUPDONE);
|
2000-01-15 02:20:49 +00:00
|
|
|
REQUIRE(VALID_BYADDR(byaddr));
|
|
|
|
REQUIRE(byaddr->task == task);
|
2000-11-15 23:07:58 +00:00
|
|
|
|
|
|
|
UNUSED(task);
|
|
|
|
|
2000-10-17 01:57:42 +00:00
|
|
|
levent = (dns_lookupevent_t *)event;
|
2000-01-15 00:48:49 +00:00
|
|
|
|
2000-11-15 23:07:58 +00:00
|
|
|
if (levent->result == ISC_R_SUCCESS) {
|
2000-10-17 01:57:42 +00:00
|
|
|
result = copy_ptr_targets(byaddr, levent->rdataset);
|
2000-11-15 23:07:58 +00:00
|
|
|
byaddr->event->result = result;
|
|
|
|
} else
|
|
|
|
byaddr->event->result = levent->result;
|
2000-10-17 01:57:42 +00:00
|
|
|
isc_event_free(&event);
|
|
|
|
isc_task_sendanddetach(&byaddr->task, (isc_event_t **)&byaddr->event);
|
2000-01-15 00:48:49 +00:00
|
|
|
}
|
2000-01-12 20:05:15 +00:00
|
|
|
|
2000-01-18 19:07:39 +00:00
|
|
|
static void
|
|
|
|
bevent_destroy(isc_event_t *event) {
|
|
|
|
dns_byaddrevent_t *bevent;
|
|
|
|
dns_name_t *name, *next_name;
|
|
|
|
isc_mem_t *mctx;
|
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
REQUIRE(event->ev_type == DNS_EVENT_BYADDRDONE);
|
|
|
|
mctx = event->ev_destroy_arg;
|
2000-01-18 19:07:39 +00:00
|
|
|
bevent = (dns_byaddrevent_t *)event;
|
|
|
|
|
|
|
|
for (name = ISC_LIST_HEAD(bevent->names);
|
|
|
|
name != NULL;
|
|
|
|
name = next_name) {
|
|
|
|
next_name = ISC_LIST_NEXT(name, link);
|
2000-11-20 10:20:10 +00:00
|
|
|
ISC_LIST_UNLINK(bevent->names, name, link);
|
2000-01-18 19:07:39 +00:00
|
|
|
dns_name_free(name, mctx);
|
2001-11-12 19:05:39 +00:00
|
|
|
isc_mem_put(mctx, name, sizeof(*name));
|
2000-01-18 19:07:39 +00:00
|
|
|
}
|
2000-04-17 19:22:44 +00:00
|
|
|
isc_mem_put(mctx, event, event->ev_size);
|
2000-01-18 19:07:39 +00:00
|
|
|
}
|
|
|
|
|
2000-01-12 20:05:15 +00:00
|
|
|
isc_result_t
|
2016-12-30 15:45:08 +11:00
|
|
|
dns_byaddr_create(isc_mem_t *mctx, const isc_netaddr_t *address,
|
|
|
|
dns_view_t *view, unsigned int options, isc_task_t *task,
|
2000-01-12 20:05:15 +00:00
|
|
|
isc_taskaction_t action, void *arg, dns_byaddr_t **byaddrp)
|
|
|
|
{
|
|
|
|
isc_result_t result;
|
|
|
|
dns_byaddr_t *byaddr;
|
2000-01-15 00:48:49 +00:00
|
|
|
isc_event_t *ievent;
|
2000-01-15 02:20:49 +00:00
|
|
|
|
2001-11-12 19:05:39 +00:00
|
|
|
byaddr = isc_mem_get(mctx, sizeof(*byaddr));
|
2013-02-20 21:39:05 -08:00
|
|
|
byaddr->mctx = NULL;
|
|
|
|
isc_mem_attach(mctx, &byaddr->mctx);
|
2000-01-18 02:50:23 +00:00
|
|
|
byaddr->options = options;
|
|
|
|
|
2001-11-12 19:05:39 +00:00
|
|
|
byaddr->event = isc_mem_get(mctx, sizeof(*byaddr->event));
|
|
|
|
ISC_EVENT_INIT(byaddr->event, sizeof(*byaddr->event), 0, NULL,
|
2000-01-18 19:07:39 +00:00
|
|
|
DNS_EVENT_BYADDRDONE, action, arg, byaddr,
|
|
|
|
bevent_destroy, mctx);
|
2000-01-15 00:48:49 +00:00
|
|
|
byaddr->event->result = ISC_R_FAILURE;
|
|
|
|
ISC_LIST_INIT(byaddr->event->names);
|
2000-01-12 20:05:15 +00:00
|
|
|
|
2000-01-18 02:50:23 +00:00
|
|
|
byaddr->task = NULL;
|
|
|
|
isc_task_attach(task, &byaddr->task);
|
|
|
|
|
2018-11-16 15:33:22 +01:00
|
|
|
isc_mutex_init(&byaddr->lock);
|
2000-01-15 00:48:49 +00:00
|
|
|
|
2000-08-16 22:21:17 +00:00
|
|
|
dns_fixedname_init(&byaddr->name);
|
|
|
|
|
2018-04-03 13:10:07 +02:00
|
|
|
result = dns_byaddr_createptrname(address, options,
|
|
|
|
dns_fixedname_name(&byaddr->name));
|
2000-01-15 00:48:49 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_lock;
|
|
|
|
|
2000-10-17 01:57:42 +00:00
|
|
|
byaddr->lookup = NULL;
|
|
|
|
result = dns_lookup_create(mctx, dns_fixedname_name(&byaddr->name),
|
|
|
|
dns_rdatatype_ptr, view, 0, task,
|
|
|
|
lookup_done, byaddr, &byaddr->lookup);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_lock;
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
byaddr->canceled = false;
|
2000-01-15 00:48:49 +00:00
|
|
|
byaddr->magic = BYADDR_MAGIC;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-01-15 00:48:49 +00:00
|
|
|
*byaddrp = byaddr;
|
|
|
|
|
2000-01-12 20:05:15 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
cleanup_lock:
|
2018-11-19 10:31:09 +00:00
|
|
|
isc_mutex_destroy(&byaddr->lock);
|
2000-01-12 20:05:15 +00:00
|
|
|
|
2000-01-15 00:48:49 +00:00
|
|
|
ievent = (isc_event_t *)byaddr->event;
|
|
|
|
isc_event_free(&ievent);
|
|
|
|
byaddr->event = NULL;
|
|
|
|
|
2000-01-15 02:20:49 +00:00
|
|
|
isc_task_detach(&byaddr->task);
|
|
|
|
|
2013-02-20 21:39:05 -08:00
|
|
|
isc_mem_putanddetach(&mctx, byaddr, sizeof(*byaddr));
|
2000-01-12 20:05:15 +00:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dns_byaddr_cancel(dns_byaddr_t *byaddr) {
|
2000-01-15 00:48:49 +00:00
|
|
|
REQUIRE(VALID_BYADDR(byaddr));
|
|
|
|
|
|
|
|
LOCK(&byaddr->lock);
|
|
|
|
|
|
|
|
if (!byaddr->canceled) {
|
2018-04-17 08:29:14 -07:00
|
|
|
byaddr->canceled = true;
|
2000-10-17 01:57:42 +00:00
|
|
|
if (byaddr->lookup != NULL)
|
|
|
|
dns_lookup_cancel(byaddr->lookup);
|
2000-01-15 00:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UNLOCK(&byaddr->lock);
|
2000-01-12 20:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dns_byaddr_destroy(dns_byaddr_t **byaddrp) {
|
2000-01-15 00:48:49 +00:00
|
|
|
dns_byaddr_t *byaddr;
|
2000-01-12 20:05:15 +00:00
|
|
|
|
2000-01-15 00:48:49 +00:00
|
|
|
REQUIRE(byaddrp != NULL);
|
|
|
|
byaddr = *byaddrp;
|
2020-02-08 04:37:54 -08:00
|
|
|
*byaddrp = NULL;
|
2000-01-15 00:48:49 +00:00
|
|
|
REQUIRE(VALID_BYADDR(byaddr));
|
|
|
|
REQUIRE(byaddr->event == NULL);
|
2000-01-15 02:20:49 +00:00
|
|
|
REQUIRE(byaddr->task == NULL);
|
2000-10-17 01:57:42 +00:00
|
|
|
dns_lookup_destroy(&byaddr->lookup);
|
2000-01-15 00:48:49 +00:00
|
|
|
|
2018-11-19 10:31:09 +00:00
|
|
|
isc_mutex_destroy(&byaddr->lock);
|
2000-01-15 00:48:49 +00:00
|
|
|
byaddr->magic = 0;
|
2013-02-20 21:39:05 -08:00
|
|
|
isc_mem_putanddetach(&byaddr->mctx, byaddr, sizeof(*byaddr));
|
2000-01-15 00:48:49 +00:00
|
|
|
}
|