1: #ifndef lint
   2: static char sccsid[] = "@(#)db_lookup.c	4.3 (Berkeley) 6/4/86";
   3: #endif
   4: 
   5: /*
   6:  * Copyright (c) 1986 Regents of the University of California
   7:  *	All Rights Reserved
   8:  */
   9: 
  10: /*
  11:  * Table lookup routines.
  12:  */
  13: 
  14: #include <sys/types.h>
  15: #include <stdio.h>
  16: #include <arpa/nameser.h>
  17: #include "db.h"
  18: 
  19: struct hashbuf *hashtab;    /* root hash table */
  20: 
  21: #ifdef DEBUG
  22: extern int debug;
  23: extern FILE *ddt;
  24: #endif
  25: 
  26: /*
  27:  * Lookup 'name' and return a pointer to the namebuf;
  28:  * NULL otherwise. If 'insert', insert name into tables.
  29:  * Wildcard lookups are handled.
  30:  */
  31: struct namebuf *
  32: nlookup(name, htpp, fname, insert)
  33:     char *name;
  34:     struct hashbuf **htpp;
  35:     char **fname;
  36:     int insert;
  37: {
  38:     register struct namebuf *np;
  39:     register char *cp;
  40:     register int c;
  41:     register unsigned hval;
  42:     register struct hashbuf *htp;
  43:     struct namebuf *parent = NULL;
  44: 
  45: 
  46:     htp = *htpp;
  47:     hval = 0;
  48:     for (cp = name; c = *cp++; ) {
  49:         if (c == '.') {
  50:             parent = np = nlookup(cp, htpp, fname, insert);
  51:             if (np == NULL)
  52:                 return (NULL);
  53:             if (*fname != cp)
  54:                 return (np);
  55:             if ((htp = np->n_hash) == NULL) {
  56:                 if (!insert) {
  57:                     if (np->n_dname[0] == '*' &&
  58:                         np->n_dname[1] == '\0')
  59:                         *fname = name;
  60:                     return (np);
  61:                 }
  62:                 htp = savehash((struct hashbuf *)NULL);
  63:                 np->n_hash = htp;
  64:             }
  65:             *htpp = htp;
  66:             break;
  67:         }
  68:         hval <<= HASHSHIFT;
  69:         hval += c & HASHMASK;
  70:     }
  71:     c = *--cp;
  72:     *cp = '\0';
  73:     hval %= htp->h_size;
  74:     /*
  75: 	 * Lookup this label in current hash table
  76: 	 */
  77:     for (np = htp->h_tab[hval]; np != NULL; np = np->n_next) {
  78:         if (cistrcmp(name, np->n_dname) == 0) {
  79:             *cp = c;
  80:             *fname = name;
  81:             return (np);
  82:         }
  83:     }
  84:     if (!insert) {
  85:         /*
  86: 		 * look for wildcard in this hash table
  87: 		 */
  88:         hval = ('*' & HASHMASK)  % htp->h_size;
  89:         for (np = htp->h_tab[hval]; np != NULL; np = np->n_next) {
  90:             if (np->n_dname[0] == '*'  && np->n_dname[1] == '\0') {
  91:                 *cp = c;
  92:                 *fname = name;
  93:                 return (np);
  94:             }
  95:         }
  96:         *cp = c;
  97:         return (parent);
  98:     }
  99:     np = savename(name);
 100:     np->n_parent = parent;
 101:     np->n_next = htp->h_tab[hval];
 102:     htp->h_tab[hval] = np;
 103:     /* increase hash table size */
 104:     if (++htp->h_cnt > htp->h_size * 2) {
 105:         *htpp = htp = savehash(htp);
 106:         if (parent == NULL)
 107:             hashtab = htp;
 108:         else
 109:             parent->n_hash = htp;
 110:     }
 111:     *cp = c;
 112:     *fname = name;
 113:     return (np);
 114: }
 115: 
 116: /*
 117:  * Does the data record match the class and type?
 118:  */
 119: match(dp, class, type)
 120:     struct databuf *dp;
 121:     int class, type;
 122: {
 123: 
 124: #ifdef DEBUG
 125:     if (debug >= 5)
 126:         fprintf(ddt,"match(%x, %d, %d) %d, %d\n", dp, class, type,
 127:             dp->d_class, dp->d_type);
 128: #endif
 129: 
 130:     if (dp->d_class != class && class != C_ANY)
 131:         return (0);
 132:     if (dp->d_type != type && type != T_ANY)
 133:         return (0);
 134:     return (1);
 135: }
Last modified: 1986-06-04
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1144
Valid CSS Valid XHTML 1.0 Strict