lwsearch.c revision ec5347e2c775f027573ce5648b910361aa926c01
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Copyright (C) 2000, 2001 Internet Software Consortium.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Permission to use, copy, modify, and/or distribute this software for any
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * purpose with or without fee is hereby granted, provided that the above
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * copyright notice and this permission notice appear in all copies.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * PERFORMANCE OF THIS SOFTWARE.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/* $Id: lwsearch.c,v 1.12 2007/06/18 23:47:18 tbox Exp $ */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/*! \file */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <config.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <isc/magic.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <isc/mem.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <isc/mutex.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <isc/result.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <isc/types.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <isc/util.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <dns/name.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <dns/types.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <named/lwsearch.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <named/types.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define LWSEARCHLIST_MAGIC ISC_MAGIC('L', 'W', 'S', 'L')
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define VALID_LWSEARCHLIST(l) ISC_MAGIC_VALID(l, LWSEARCHLIST_MAGIC)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsisc_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsns_lwsearchlist_create(isc_mem_t *mctx, ns_lwsearchlist_t **listp) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ns_lwsearchlist_t *list;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_result_t result;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews REQUIRE(mctx != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews REQUIRE(listp != NULL && *listp == NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews list = isc_mem_get(mctx, sizeof(ns_lwsearchlist_t));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (list == NULL)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (ISC_R_NOMEMORY);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews result = isc_mutex_init(&list->lock);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (result != ISC_R_SUCCESS) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_mem_put(mctx, list, sizeof(ns_lwsearchlist_t));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (result);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews list->mctx = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_mem_attach(mctx, &list->mctx);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews list->refs = 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ISC_LIST_INIT(list->names);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews list->magic = LWSEARCHLIST_MAGIC;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *listp = list;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (ISC_R_SUCCESS);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsvoid
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsns_lwsearchlist_attach(ns_lwsearchlist_t *source, ns_lwsearchlist_t **target) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews REQUIRE(VALID_LWSEARCHLIST(source));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews REQUIRE(target != NULL && *target == NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews LOCK(&source->lock);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews INSIST(source->refs > 0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews source->refs++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews INSIST(source->refs != 0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews UNLOCK(&source->lock);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *target = source;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsvoid
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsns_lwsearchlist_detach(ns_lwsearchlist_t **listp) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ns_lwsearchlist_t *list;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_mem_t *mctx;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews REQUIRE(listp != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews list = *listp;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews REQUIRE(VALID_LWSEARCHLIST(list));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews LOCK(&list->lock);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews INSIST(list->refs > 0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews list->refs--;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews UNLOCK(&list->lock);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *listp = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (list->refs != 0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews mctx = list->mctx;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews while (!ISC_LIST_EMPTY(list->names)) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews dns_name_t *name = ISC_LIST_HEAD(list->names);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ISC_LIST_UNLINK(list->names, name, link);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews dns_name_free(name, list->mctx);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_mem_put(list->mctx, name, sizeof(dns_name_t));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews list->magic = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_mem_put(mctx, list, sizeof(ns_lwsearchlist_t));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_mem_detach(&mctx);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsisc_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsns_lwsearchlist_append(ns_lwsearchlist_t *list, dns_name_t *name) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews dns_name_t *newname;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_result_t result;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews REQUIRE(VALID_LWSEARCHLIST(list));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews REQUIRE(name != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews newname = isc_mem_get(list->mctx, sizeof(dns_name_t));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (newname == NULL)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (ISC_R_NOMEMORY);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews dns_name_init(newname, NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews result = dns_name_dup(name, list->mctx, newname);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (result != ISC_R_SUCCESS) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_mem_put(list->mctx, newname, sizeof(dns_name_t));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (result);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ISC_LINK_INIT(newname, link);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ISC_LIST_APPEND(list->names, newname, link);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (ISC_R_SUCCESS);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsvoid
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsns_lwsearchctx_init(ns_lwsearchctx_t *sctx, ns_lwsearchlist_t *list,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews dns_name_t *name, unsigned int ndots)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews{
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews INSIST(sctx != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews sctx->relname = name;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews sctx->searchname = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews sctx->doneexact = ISC_FALSE;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews sctx->exactfirst = ISC_FALSE;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews sctx->ndots = ndots;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (dns_name_isabsolute(name) || list == NULL) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews sctx->list = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews sctx->list = list;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews sctx->searchname = ISC_LIST_HEAD(sctx->list->names);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (dns_name_countlabels(name) > ndots)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews sctx->exactfirst = ISC_TRUE;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsvoid
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsns_lwsearchctx_first(ns_lwsearchctx_t *sctx) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews REQUIRE(sctx != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews UNUSED(sctx);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsisc_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsns_lwsearchctx_next(ns_lwsearchctx_t *sctx) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews REQUIRE(sctx != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (sctx->list == NULL)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (ISC_R_NOMORE);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (sctx->searchname == NULL) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews INSIST (!sctx->exactfirst || sctx->doneexact);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (sctx->exactfirst || sctx->doneexact)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (ISC_R_NOMORE);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews sctx->doneexact = ISC_TRUE;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (sctx->exactfirst && !sctx->doneexact)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews sctx->doneexact = ISC_TRUE;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews sctx->searchname = ISC_LIST_NEXT(sctx->searchname,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews link);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (sctx->searchname == NULL && sctx->doneexact)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (ISC_R_NOMORE);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (ISC_R_SUCCESS);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsisc_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsns_lwsearchctx_current(ns_lwsearchctx_t *sctx, dns_name_t *absname) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews dns_name_t *tname;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews isc_boolean_t useexact = ISC_FALSE;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews REQUIRE(sctx != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (sctx->list == NULL ||
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews sctx->searchname == NULL ||
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews (sctx->exactfirst && !sctx->doneexact))
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews useexact = ISC_TRUE;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (useexact) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (dns_name_isabsolute(sctx->relname))
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews tname = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews tname = dns_rootname;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews tname = sctx->searchname;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (dns_name_concatenate(sctx->relname, tname, absname, NULL));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews