BSD 4_3_Net_2 release
[unix-history] / usr / src / usr.sbin / named / db_lookup.c
CommitLineData
95f51977 1/*
ca67e7b4
C
2 * Copyright (c) 1986 Regents of the University of California.
3 * All rights reserved.
4 *
af359dea
C
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
95f51977
C
32 */
33
ca67e7b4 34#ifndef lint
af359dea 35static char sccsid[] = "@(#)db_lookup.c 4.18 (Berkeley) 3/21/91";
ca67e7b4
C
36#endif /* not lint */
37
95f51977
C
38/*
39 * Table lookup routines.
40 */
41
af359dea 42#include <sys/param.h>
95f51977 43#include <arpa/nameser.h>
af359dea 44#include <stdio.h>
95f51977
C
45#include "db.h"
46
47struct hashbuf *hashtab; /* root hash table */
ca67e7b4 48struct hashbuf *fcachetab; /* hash table of cache read from file */
95f51977
C
49
50#ifdef DEBUG
51extern int debug;
52extern FILE *ddt;
53#endif
54
55/*
56 * Lookup 'name' and return a pointer to the namebuf;
57 * NULL otherwise. If 'insert', insert name into tables.
58 * Wildcard lookups are handled.
59 */
60struct namebuf *
61nlookup(name, htpp, fname, insert)
62 char *name;
63 struct hashbuf **htpp;
64 char **fname;
65 int insert;
66{
67 register struct namebuf *np;
68 register char *cp;
69 register int c;
70 register unsigned hval;
71 register struct hashbuf *htp;
72 struct namebuf *parent = NULL;
73
95f51977
C
74 htp = *htpp;
75 hval = 0;
ca67e7b4 76 *fname = "???";
95f51977
C
77 for (cp = name; c = *cp++; ) {
78 if (c == '.') {
79 parent = np = nlookup(cp, htpp, fname, insert);
80 if (np == NULL)
81 return (NULL);
82 if (*fname != cp)
83 return (np);
84 if ((htp = np->n_hash) == NULL) {
85 if (!insert) {
86 if (np->n_dname[0] == '*' &&
87 np->n_dname[1] == '\0')
88 *fname = name;
89 return (np);
90 }
91 htp = savehash((struct hashbuf *)NULL);
92 np->n_hash = htp;
93 }
94 *htpp = htp;
95 break;
96 }
97 hval <<= HASHSHIFT;
98 hval += c & HASHMASK;
99 }
100 c = *--cp;
101 *cp = '\0';
95f51977 102 /*
ca67e7b4 103 * Lookup this label in current hash table.
95f51977 104 */
ca67e7b4 105 for (np = htp->h_tab[hval % htp->h_size]; np != NULL; np = np->n_next) {
ca67e7b4
C
106 if (np->n_hashval == hval &&
107 strcasecmp(name, np->n_dname) == 0) {
95f51977
C
108 *cp = c;
109 *fname = name;
110 return (np);
111 }
112 }
113 if (!insert) {
114 /*
1c15e888
C
115 * Look for wildcard in this hash table.
116 * Don't use a cached "*" name as a wildcard,
117 * only authoritative.
95f51977
C
118 */
119 hval = ('*' & HASHMASK) % htp->h_size;
120 for (np = htp->h_tab[hval]; np != NULL; np = np->n_next) {
1c15e888
C
121 if (np->n_dname[0] == '*' && np->n_dname[1] == '\0' &&
122 np->n_data && np->n_data->d_zone != 0) {
95f51977
C
123 *cp = c;
124 *fname = name;
125 return (np);
126 }
127 }
128 *cp = c;
129 return (parent);
130 }
131 np = savename(name);
132 np->n_parent = parent;
ca67e7b4
C
133 np->n_hashval = hval;
134 hval %= htp->h_size;
95f51977
C
135 np->n_next = htp->h_tab[hval];
136 htp->h_tab[hval] = np;
137 /* increase hash table size */
138 if (++htp->h_cnt > htp->h_size * 2) {
ca67e7b4
C
139 *htpp = savehash(htp);
140 if (parent == NULL) {
141 if (htp == hashtab)
142 hashtab = *htpp;
143 else
144 fcachetab = *htpp;
145 }
95f51977 146 else
ca67e7b4
C
147 parent->n_hash = *htpp;
148 htp = *htpp;
95f51977
C
149 }
150 *cp = c;
151 *fname = name;
152 return (np);
153}
154
155/*
156 * Does the data record match the class and type?
157 */
158match(dp, class, type)
ca67e7b4
C
159 register struct databuf *dp;
160 register int class, type;
95f51977 161{
95f51977
C
162#ifdef DEBUG
163 if (debug >= 5)
ca67e7b4 164 fprintf(ddt,"match(0x%x, %d, %d) %d, %d\n", dp, class, type,
95f51977
C
165 dp->d_class, dp->d_type);
166#endif
95f51977
C
167 if (dp->d_class != class && class != C_ANY)
168 return (0);
169 if (dp->d_type != type && type != T_ANY)
170 return (0);
171 return (1);
172}