0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2000, 2001, 2003-2005, 2007, 2013, 2016 Internet Systems Consortium, Inc. ("ISC")
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater/* $Id: lookup.c,v 1.21 2007/06/18 23:47:40 tbox Exp $ */
8804fd9936acd703073c4a75072852c38738a990Brian Wellington#include <isc/string.h> /* Required for HP/UX (and others?) */
8804fd9936acd703073c4a75072852c38738a990Brian Wellington /* Unlocked. */
8804fd9936acd703073c4a75072852c38738a990Brian Wellington /* Locked by lock. */
8804fd9936acd703073c4a75072852c38738a990Brian Wellington#define LOOKUP_MAGIC ISC_MAGIC('l', 'o', 'o', 'k')
8804fd9936acd703073c4a75072852c38738a990Brian Wellington#define VALID_LOOKUP(l) ISC_MAGIC_VALID((l), LOOKUP_MAGIC)
8804fd9936acd703073c4a75072852c38738a990Brian Wellingtonstatic void lookup_find(dns_lookup_t *lookup, dns_fetchevent_t *event);
8804fd9936acd703073c4a75072852c38738a990Brian Wellingtonfetch_done(isc_task_t *task, isc_event_t *event) {
8804fd9936acd703073c4a75072852c38738a990Brian Wellington REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
8804fd9936acd703073c4a75072852c38738a990Brian Wellington * The caller must be holding the lookup's lock.
8804fd9936acd703073c4a75072852c38738a990Brian Wellington result = dns_resolver_createfetch(lookup->view->resolver,
8804fd9936acd703073c4a75072852c38738a990Brian Wellington name = isc_mem_get(lookup->mctx, sizeof(dns_name_t));
8804fd9936acd703073c4a75072852c38738a990Brian Wellington result = dns_name_dup(dns_fixedname_name(&lookup->name),
13090db2b1c210b8386793ff0cbbb0b4348c1ecaBrian Wellington if (dns_rdataset_isassociated(&lookup->rdataset)) {
13090db2b1c210b8386793ff0cbbb0b4348c1ecaBrian Wellington rdataset = isc_mem_get(lookup->mctx, sizeof(dns_rdataset_t));
13090db2b1c210b8386793ff0cbbb0b4348c1ecaBrian Wellington dns_rdataset_clone(&lookup->rdataset, rdataset);
e3e3bafa138a20558a2253470effc01702fc6dfdBrian Wellington if (dns_rdataset_isassociated(&lookup->sigrdataset)) {
e3e3bafa138a20558a2253470effc01702fc6dfdBrian Wellington dns_rdataset_clone(&lookup->sigrdataset, sigrdataset);
8804fd9936acd703073c4a75072852c38738a990Brian Wellington isc_mem_put(lookup->mctx, name, sizeof(dns_name_t));
8804fd9936acd703073c4a75072852c38738a990Brian Wellington isc_mem_put(lookup->mctx, rdataset, sizeof(dns_rdataset_t));
13090db2b1c210b8386793ff0cbbb0b4348c1ecaBrian Wellingtonview_find(dns_lookup_t *lookup, dns_name_t *foundname) {
13090db2b1c210b8386793ff0cbbb0b4348c1ecaBrian Wellington dns_name_t *name = dns_fixedname_name(&lookup->name);
13090db2b1c210b8386793ff0cbbb0b4348c1ecaBrian Wellington result = dns_view_find(lookup->view, name, type, 0, 0, ISC_FALSE,
8804fd9936acd703073c4a75072852c38738a990Brian Wellingtonlookup_find(dns_lookup_t *lookup, dns_fetchevent_t *event) {
8804fd9936acd703073c4a75072852c38738a990Brian Wellington INSIST(!dns_rdataset_isassociated(&lookup->rdataset));
a56f5ada432128085e4a06815328023ee0c9610dMark Andrews * If we have restarted then clear the old node. */
8804fd9936acd703073c4a75072852c38738a990Brian Wellington * We don't know anything about the name.
8804fd9936acd703073c4a75072852c38738a990Brian Wellington * Launch a fetch.
8804fd9936acd703073c4a75072852c38738a990Brian Wellington fname = dns_fixedname_name(&event->foundname);
8804fd9936acd703073c4a75072852c38738a990Brian Wellington INSIST(event->rdataset == &lookup->rdataset);
e3e3bafa138a20558a2253470effc01702fc6dfdBrian Wellington INSIST(event->sigrdataset == &lookup->sigrdataset);
8804fd9936acd703073c4a75072852c38738a990Brian Wellington * If we've been canceled, forget about the result.
13090db2b1c210b8386793ff0cbbb0b4348c1ecaBrian Wellington dns_db_attach(event->db, &lookup->event->db);
8804fd9936acd703073c4a75072852c38738a990Brian Wellington * Copy the CNAME's target into the lookup's
8804fd9936acd703073c4a75072852c38738a990Brian Wellington * query name and start over.
8804fd9936acd703073c4a75072852c38738a990Brian Wellington result = dns_rdataset_first(&lookup->rdataset);
8804fd9936acd703073c4a75072852c38738a990Brian Wellington dns_rdataset_current(&lookup->rdataset, &rdata);
8804fd9936acd703073c4a75072852c38738a990Brian Wellington result = dns_rdata_tostruct(&rdata, &cname, NULL);
b7cd261f2fca2c7138cdc6ae8ee434e9c0031303Brian Wellington result = dns_name_copy(&cname.cname, name, NULL);
8804fd9936acd703073c4a75072852c38738a990Brian Wellington namereln = dns_name_fullcompare(name, fname, &order,
8804fd9936acd703073c4a75072852c38738a990Brian Wellington INSIST(namereln == dns_namereln_subdomain);
8804fd9936acd703073c4a75072852c38738a990Brian Wellington * Get the target name of the DNAME.
8804fd9936acd703073c4a75072852c38738a990Brian Wellington result = dns_rdataset_first(&lookup->rdataset);
8804fd9936acd703073c4a75072852c38738a990Brian Wellington dns_rdataset_current(&lookup->rdataset, &rdata);
8804fd9936acd703073c4a75072852c38738a990Brian Wellington result = dns_rdata_tostruct(&rdata, &dname, NULL);
8804fd9936acd703073c4a75072852c38738a990Brian Wellington * Construct the new query name and start over.
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉 dns_name_split(name, nlabels, prefix, NULL);
8804fd9936acd703073c4a75072852c38738a990Brian Wellington result = dns_name_concatenate(prefix, &dname.dname,
8804fd9936acd703073c4a75072852c38738a990Brian Wellington if (dns_rdataset_isassociated(&lookup->rdataset))
8804fd9936acd703073c4a75072852c38738a990Brian Wellington dns_rdataset_disassociate(&lookup->rdataset);
e3e3bafa138a20558a2253470effc01702fc6dfdBrian Wellington if (dns_rdataset_isassociated(&lookup->sigrdataset))
e3e3bafa138a20558a2253470effc01702fc6dfdBrian Wellington dns_rdataset_disassociate(&lookup->sigrdataset);
13090db2b1c210b8386793ff0cbbb0b4348c1ecaBrian Wellington dns_db_detachnode(event->db, &event->node);
8804fd9936acd703073c4a75072852c38738a990Brian Wellington * Limit the number of restarts.
8804fd9936acd703073c4a75072852c38738a990Brian Wellington if (want_restart && lookup->restarts == MAX_RESTARTS) {
8804fd9936acd703073c4a75072852c38738a990Brian Wellington REQUIRE(event->ev_type == DNS_EVENT_LOOKUPDONE);
8804fd9936acd703073c4a75072852c38738a990Brian Wellington isc_mem_put(mctx, levent->name, sizeof(dns_name_t));
8804fd9936acd703073c4a75072852c38738a990Brian Wellington dns_rdataset_disassociate(levent->rdataset);
8804fd9936acd703073c4a75072852c38738a990Brian Wellington isc_mem_put(mctx, levent->rdataset, sizeof(dns_rdataset_t));
e3e3bafa138a20558a2253470effc01702fc6dfdBrian Wellington dns_rdataset_disassociate(levent->sigrdataset);
e3e3bafa138a20558a2253470effc01702fc6dfdBrian Wellington isc_mem_put(mctx, levent->sigrdataset, sizeof(dns_rdataset_t));
13090db2b1c210b8386793ff0cbbb0b4348c1ecaBrian Wellington dns_db_detachnode(levent->db, &levent->node);
8804fd9936acd703073c4a75072852c38738a990Brian Wellingtondns_lookup_create(isc_mem_t *mctx, dns_name_t *name, dns_rdatatype_t type,
8804fd9936acd703073c4a75072852c38738a990Brian Wellington dns_view_t *view, unsigned int options, isc_task_t *task,
8804fd9936acd703073c4a75072852c38738a990Brian Wellington isc_taskaction_t action, void *arg, dns_lookup_t **lookupp)
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson lookup = isc_mem_get(mctx, sizeof(*lookup));
8804fd9936acd703073c4a75072852c38738a990Brian Wellington ievent = isc_event_allocate(mctx, lookup, DNS_EVENT_LOOKUPDONE,
8804fd9936acd703073c4a75072852c38738a990Brian Wellington lookup->event = (dns_lookupevent_t *)ievent;
8804fd9936acd703073c4a75072852c38738a990Brian Wellington lookup->event->ev_destroy = levent_destroy;
b7cd261f2fca2c7138cdc6ae8ee434e9c0031303Brian Wellington result = dns_name_copy(name, dns_fixedname_name(&lookup->name), NULL);
df925e6c66d45d960fbac0383169763967d2111cEvan Hunt isc_mem_putanddetach(&mctx, lookup, sizeof(*lookup));
8804fd9936acd703073c4a75072852c38738a990Brian Wellingtondns_lookup_destroy(dns_lookup_t **lookupp) {
8804fd9936acd703073c4a75072852c38738a990Brian Wellington if (dns_rdataset_isassociated(&lookup->rdataset))
8804fd9936acd703073c4a75072852c38738a990Brian Wellington dns_rdataset_disassociate(&lookup->rdataset);
e3e3bafa138a20558a2253470effc01702fc6dfdBrian Wellington if (dns_rdataset_isassociated(&lookup->sigrdataset))
e3e3bafa138a20558a2253470effc01702fc6dfdBrian Wellington dns_rdataset_disassociate(&lookup->sigrdataset);