|
|
1.1 root 1: /*
2: * Copyright (c) 1986 Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted provided
6: * that: (1) source distributions retain this entire copyright notice and
7: * comment, and (2) distributions including binaries display the following
8: * acknowledgement: ``This product includes software developed by the
9: * University of California, Berkeley and its contributors'' in the
10: * documentation or other materials provided with the distribution and in
11: * all advertising materials mentioning features or use of this software.
12: * Neither the name of the University nor the names of its contributors may
13: * be used to endorse or promote products derived from this software without
14: * specific prior written permission.
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
16: * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
17: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18: */
19:
20: #ifndef lint
21: static char sccsid[] = "@(#)db_lookup.c 4.17 (Berkeley) 6/1/90";
22: #endif /* not lint */
23:
24: /*
25: * Table lookup routines.
26: */
27:
28: #include <sys/types.h>
29: #include <stdio.h>
30: #include <arpa/nameser.h>
31: #include "db.h"
32:
33: struct hashbuf *hashtab; /* root hash table */
34: struct hashbuf *fcachetab; /* hash table of cache read from file */
35:
36: #ifdef DEBUG
37: extern int debug;
38: extern FILE *ddt;
39: #endif
40:
41: /*
42: * Lookup 'name' and return a pointer to the namebuf;
43: * NULL otherwise. If 'insert', insert name into tables.
44: * Wildcard lookups are handled.
45: */
46: struct namebuf *
47: nlookup(name, htpp, fname, insert)
48: char *name;
49: struct hashbuf **htpp;
50: char **fname;
51: int insert;
52: {
53: register struct namebuf *np;
54: register char *cp;
55: register int c;
56: register unsigned hval;
57: register struct hashbuf *htp;
58: struct namebuf *parent = NULL;
59:
60: htp = *htpp;
61: hval = 0;
62: *fname = "???";
63: for (cp = name; c = *cp++; ) {
64: if (c == '.') {
65: parent = np = nlookup(cp, htpp, fname, insert);
66: if (np == NULL)
67: return (NULL);
68: if (*fname != cp)
69: return (np);
70: if ((htp = np->n_hash) == NULL) {
71: if (!insert) {
72: if (np->n_dname[0] == '*' &&
73: np->n_dname[1] == '\0')
74: *fname = name;
75: return (np);
76: }
77: htp = savehash((struct hashbuf *)NULL);
78: np->n_hash = htp;
79: }
80: *htpp = htp;
81: break;
82: }
83: hval <<= HASHSHIFT;
84: hval += c & HASHMASK;
85: }
86: c = *--cp;
87: *cp = '\0';
88: /*
89: * Lookup this label in current hash table.
90: */
91: for (np = htp->h_tab[hval % htp->h_size]; np != NULL; np = np->n_next) {
92: if (np->n_hashval == hval &&
93: strcasecmp(name, np->n_dname) == 0) {
94: *cp = c;
95: *fname = name;
96: return (np);
97: }
98: }
99: if (!insert) {
100: /*
101: * Look for wildcard in this hash table.
102: * Don't use a cached "*" name as a wildcard,
103: * only authoritative.
104: */
105: hval = ('*' & HASHMASK) % htp->h_size;
106: for (np = htp->h_tab[hval]; np != NULL; np = np->n_next) {
107: if (np->n_dname[0] == '*' && np->n_dname[1] == '\0' &&
108: np->n_data && np->n_data->d_zone != 0) {
109: *cp = c;
110: *fname = name;
111: return (np);
112: }
113: }
114: *cp = c;
115: return (parent);
116: }
117: np = savename(name);
118: np->n_parent = parent;
119: np->n_hashval = hval;
120: hval %= htp->h_size;
121: np->n_next = htp->h_tab[hval];
122: htp->h_tab[hval] = np;
123: /* increase hash table size */
124: if (++htp->h_cnt > htp->h_size * 2) {
125: *htpp = savehash(htp);
126: if (parent == NULL) {
127: if (htp == hashtab)
128: hashtab = *htpp;
129: else
130: fcachetab = *htpp;
131: }
132: else
133: parent->n_hash = *htpp;
134: htp = *htpp;
135: }
136: *cp = c;
137: *fname = name;
138: return (np);
139: }
140:
141: /*
142: * Does the data record match the class and type?
143: */
144: match(dp, class, type)
145: register struct databuf *dp;
146: register int class, type;
147: {
148: #ifdef DEBUG
149: if (debug >= 5)
150: fprintf(ddt,"match(0x%x, %d, %d) %d, %d\n", dp, class, type,
151: dp->d_class, dp->d_type);
152: #endif
153: if (dp->d_class != class && class != C_ANY)
154: return (0);
155: if (dp->d_type != type && type != T_ANY)
156: return (0);
157: return (1);
158: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.