home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / n / bind / bind-4.001 / bind-4~ / bind-4.9.3-BETA9 / named / db_lookup.c < prev    next >
C/C++ Source or Header  |  1994-07-22  |  6KB  |  197 lines

  1. #if !defined(lint) && !defined(SABER)
  2. static char sccsid[] = "@(#)db_lookup.c    4.18 (Berkeley) 3/21/91";
  3. static char rcsid[] = "$Id: db_lookup.c,v 4.9.1.5 1994/06/01 21:09:39 vixie Exp $";
  4. #endif /* not lint */
  5.  
  6. /*
  7.  * ++Copyright++ 1986
  8.  * -
  9.  * Copyright (c) 1986
  10.  *    The Regents of the University of California.  All rights reserved.
  11.  * 
  12.  * Redistribution and use in source and binary forms, with or without
  13.  * modification, are permitted provided that the following conditions
  14.  * are met:
  15.  * 1. Redistributions of source code must retain the above copyright
  16.  *    notice, this list of conditions and the following disclaimer.
  17.  * 2. Redistributions in binary form must reproduce the above copyright
  18.  *    notice, this list of conditions and the following disclaimer in the
  19.  *    documentation and/or other materials provided with the distribution.
  20.  * 3. All advertising materials mentioning features or use of this software
  21.  *    must display the following acknowledgement:
  22.  *     This product includes software developed by the University of
  23.  *     California, Berkeley and its contributors.
  24.  * 4. Neither the name of the University nor the names of its contributors
  25.  *    may be used to endorse or promote products derived from this software
  26.  *    without specific prior written permission.
  27.  * 
  28.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  29.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  32.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  37.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38.  * SUCH DAMAGE.
  39.  * -
  40.  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  41.  * 
  42.  * Permission to use, copy, modify, and distribute this software for any
  43.  * purpose with or without fee is hereby granted, provided that the above
  44.  * copyright notice and this permission notice appear in all copies, and that
  45.  * the name of Digital Equipment Corporation not be used in advertising or
  46.  * publicity pertaining to distribution of the document or software without
  47.  * specific, written prior permission.
  48.  * 
  49.  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  50.  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  51.  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
  52.  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  53.  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  54.  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  55.  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  56.  * SOFTWARE.
  57.  * -
  58.  * --Copyright--
  59.  */
  60.  
  61. /*
  62.  * Table lookup routines.
  63.  */
  64.  
  65. #include <syslog.h>
  66. #include <sys/param.h>
  67. #include <sys/socket.h>
  68. #include <netinet/in.h>
  69. #include <arpa/nameser.h>
  70. #include <stdio.h>
  71. #include <ctype.h>
  72.  
  73. #include "named.h"
  74.  
  75. /* 
  76.  * Lookup 'name' and return a pointer to the namebuf;
  77.  * NULL otherwise. If 'insert', insert name into tables.
  78.  * Wildcard lookups are handled.
  79.  */
  80. struct namebuf *
  81. nlookup(name, htpp, fname, insert)
  82.     char *name;
  83.     struct hashbuf **htpp;
  84.     char **fname;
  85.     int insert;
  86. {
  87.     register struct namebuf *np;
  88.     register char *cp;
  89.     register int c;
  90.     register unsigned hval;
  91.     register struct hashbuf *htp;
  92.     struct namebuf *parent = NULL;
  93.  
  94.     htp = *htpp;
  95.     hval = 0;
  96.     *fname = "???";
  97.     for (cp = name; c = *cp++; ) {
  98.         if (c == '.') {
  99.             parent = np = nlookup(cp, htpp, fname, insert);
  100.             if (np == NULL)
  101.                 return (NULL);
  102.             if (*fname != cp)
  103.                 return (np);
  104.             if ((htp = np->n_hash) == NULL) {
  105.                 if (!insert) {
  106.                     if (np->n_dname[0] == '*' && 
  107.                         np->n_dname[1] == '\0')
  108.                         *fname = name;
  109.                     return (np);
  110.                 }
  111.                 htp = savehash((struct hashbuf *)NULL);
  112.                 np->n_hash = htp;
  113.             }
  114.             *htpp = htp;
  115.             break;
  116.         }
  117.         hval <<= HASHSHIFT;
  118.         hval += (isupper(c) ? tolower(c) : c) & HASHMASK;
  119.     }
  120.     c = *--cp;
  121.     *cp = '\0';
  122.     /*
  123.      * Lookup this label in current hash table.
  124.      */
  125.     for (np = htp->h_tab[hval % htp->h_size];
  126.          np != NULL;
  127.          np = np->n_next) {
  128.         if (np->n_hashval == hval &&
  129.             strcasecmp(name, np->n_dname) == 0) {
  130.             *fname = name;
  131.             *cp = c;
  132.             return (np);
  133.         }
  134.     }
  135.     if (!insert) {
  136.         /*
  137.          * Look for wildcard in this hash table.
  138.          * Don't use a cached "*" name as a wildcard,
  139.          * only authoritative.
  140.          */
  141.         hval = ('*' & HASHMASK) % htp->h_size;
  142.         for (np = htp->h_tab[hval]; np != NULL; np = np->n_next) {
  143.             if (np->n_dname[0] == '*'  && np->n_dname[1] == '\0' &&
  144.                 np->n_data && np->n_data->d_zone != 0) {
  145.                 *fname = name;
  146.                 *cp = c;
  147.                 return (np);
  148.             }
  149.         }
  150.         *cp = c;
  151.         return (parent);
  152.     }
  153.     np = savename(name);
  154.     np->n_parent = parent;
  155.     np->n_hashval = hval;
  156.     hval %= htp->h_size;
  157.     np->n_next = htp->h_tab[hval];
  158.     htp->h_tab[hval] = np;
  159.     /* Increase hash table size. */
  160.     if (++htp->h_cnt > htp->h_size * 2) {
  161.         *htpp = savehash(htp);
  162.         if (parent == NULL) {
  163.             if (htp == hashtab) {
  164.                 hashtab = *htpp;
  165.             } else {
  166.                 fcachetab = *htpp;
  167.                 }
  168.         }
  169.         else
  170.             parent->n_hash = *htpp;
  171.         htp = *htpp;
  172.     }
  173.     *fname = name;
  174.     *cp = c;
  175.     return (np);
  176. }
  177.  
  178. /* int
  179.  * match(dp, class, type)
  180.  *    Does data record `dp' match the class and type?
  181.  * return value:
  182.  *    boolean
  183.  */
  184. int
  185. match(dp, class, type)
  186.     register struct databuf *dp;
  187.     register int class, type;
  188. {
  189.     dprintf(5, (ddt, "match(0x%x, %d, %d) %d, %d\n",
  190.             dp, class, type, dp->d_class, dp->d_type));
  191.     if (dp->d_class != class && class != C_ANY)
  192.         return (0);
  193.     if (dp->d_type != type && type != T_ANY)
  194.         return (0);
  195.     return (1);
  196. }
  197.