| 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, 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. |
| 32 | */ |
| 33 | |
| 34 | #ifndef lint |
| 35 | static char sccsid[] = "@(#)db_lookup.c 4.18 (Berkeley) 3/21/91"; |
| 36 | #endif /* not lint */ |
| 37 | |
| 38 | /* |
| 39 | * Table lookup routines. |
| 40 | */ |
| 41 | |
| 42 | #include <sys/param.h> |
| 43 | #include <arpa/nameser.h> |
| 44 | #include <stdio.h> |
| 45 | #include "db.h" |
| 46 | |
| 47 | struct hashbuf *hashtab; /* root hash table */ |
| 48 | struct hashbuf *fcachetab; /* hash table of cache read from file */ |
| 49 | |
| 50 | #ifdef DEBUG |
| 51 | extern int debug; |
| 52 | extern 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 | */ |
| 60 | struct namebuf * |
| 61 | nlookup(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 | |
| 74 | htp = *htpp; |
| 75 | hval = 0; |
| 76 | *fname = "???"; |
| 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'; |
| 102 | /* |
| 103 | * Lookup this label in current hash table. |
| 104 | */ |
| 105 | for (np = htp->h_tab[hval % htp->h_size]; np != NULL; np = np->n_next) { |
| 106 | if (np->n_hashval == hval && |
| 107 | strcasecmp(name, np->n_dname) == 0) { |
| 108 | *cp = c; |
| 109 | *fname = name; |
| 110 | return (np); |
| 111 | } |
| 112 | } |
| 113 | if (!insert) { |
| 114 | /* |
| 115 | * Look for wildcard in this hash table. |
| 116 | * Don't use a cached "*" name as a wildcard, |
| 117 | * only authoritative. |
| 118 | */ |
| 119 | hval = ('*' & HASHMASK) % htp->h_size; |
| 120 | for (np = htp->h_tab[hval]; np != NULL; np = np->n_next) { |
| 121 | if (np->n_dname[0] == '*' && np->n_dname[1] == '\0' && |
| 122 | np->n_data && np->n_data->d_zone != 0) { |
| 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; |
| 133 | np->n_hashval = hval; |
| 134 | hval %= htp->h_size; |
| 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) { |
| 139 | *htpp = savehash(htp); |
| 140 | if (parent == NULL) { |
| 141 | if (htp == hashtab) |
| 142 | hashtab = *htpp; |
| 143 | else |
| 144 | fcachetab = *htpp; |
| 145 | } |
| 146 | else |
| 147 | parent->n_hash = *htpp; |
| 148 | htp = *htpp; |
| 149 | } |
| 150 | *cp = c; |
| 151 | *fname = name; |
| 152 | return (np); |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Does the data record match the class and type? |
| 157 | */ |
| 158 | match(dp, class, type) |
| 159 | register struct databuf *dp; |
| 160 | register int class, type; |
| 161 | { |
| 162 | #ifdef DEBUG |
| 163 | if (debug >= 5) |
| 164 | fprintf(ddt,"match(0x%x, %d, %d) %d, %d\n", dp, class, type, |
| 165 | dp->d_class, dp->d_type); |
| 166 | #endif |
| 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 | } |