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
   6:  * provided that this notice is preserved and that due credit is given
   7:  * to the University of California at Berkeley. The name of the University
   8:  * may not be used to endorse or promote products derived from this
   9:  * software without specific prior written permission. This software
  10:  * is provided ``as is'' without express or implied warranty.
  11:  */
  12: 
  13: #if defined(DOSCCS) && !defined(lint)
  14: static char sccsid[] = "@(#)db_save.c	4.13.1 (2.11BSD GTE) 1/1/94";
  15: #endif
  16: 
  17: /*
  18:  * Buffer allocation and deallocation routines.
  19:  */
  20: 
  21: #include <sys/types.h>
  22: #include <stdio.h>
  23: #include <syslog.h>
  24: #include <arpa/nameser.h>
  25: #include "db.h"
  26: 
  27: #ifdef DEBUG
  28: extern int debug;
  29: extern FILE *ddt;
  30: #endif
  31: 
  32: extern char *strcpy();
  33: 
  34: /*
  35:  * Allocate a name buffer & save name.
  36:  */
  37: struct namebuf *
  38: savename(name)
  39:     char *name;
  40: {
  41:     register struct namebuf *np;
  42: 
  43:     np = (struct namebuf *) malloc(sizeof(struct namebuf));
  44:     if (np == NULL) {
  45:         syslog(LOG_ERR, "savename: %m");
  46:         exit(1);
  47:     }
  48:     np->n_dname = savestr(name);
  49:     np->n_next = NULL;
  50:     np->n_data = NULL;
  51:     np->n_hash = NULL;
  52:     return (np);
  53: }
  54: 
  55: /*
  56:  * Allocate a data buffer & save data.
  57:  */
  58: struct databuf *
  59: savedata(class, type, ttl, data, size)
  60:     int class, type;
  61:     u_long ttl;
  62:     char *data;
  63:     int size;
  64: {
  65:     register struct databuf *dp;
  66: 
  67:     if (type == T_NS)
  68:         dp = (struct databuf *)
  69:             malloc((unsigned)DATASIZE(size)+sizeof(u_long));
  70:     else
  71:         dp = (struct databuf *) malloc((unsigned)DATASIZE(size));
  72:     if (dp == NULL) {
  73:         syslog(LOG_ERR, "savedata: %m");
  74:         exit(1);
  75:     }
  76:     dp->d_next = NULL;
  77:     dp->d_type = type;
  78:     dp->d_class = class;
  79:     dp->d_ttl = ttl;
  80:     dp->d_size = size;
  81:     dp->d_mark = 0;
  82:     dp->d_flags = 0;
  83:     dp->d_nstime = 0;
  84:     bcopy(data, dp->d_data, dp->d_size);
  85:     return (dp);
  86: }
  87: 
  88: int hashsizes[] = { /* hashtable sizes */
  89:     2,
  90:     11,
  91:     113,
  92:     337,
  93:     977,
  94:     2053,
  95:     4073,
  96:     8011,
  97:     16001,
  98:     0
  99: };
 100: 
 101: /*
 102:  * Allocate a data buffer & save data.
 103:  */
 104: struct hashbuf *
 105: savehash(oldhtp)
 106:     register struct hashbuf *oldhtp;
 107: {
 108:     register struct hashbuf *htp;
 109:     register struct namebuf *np, *nnp, **hp;
 110:     register int n;
 111:     int newsize;
 112: 
 113:     if (oldhtp == NULL)
 114:         newsize = hashsizes[0];
 115:     else {
 116:         for (n = 0; newsize = hashsizes[n++]; )
 117:             if (oldhtp->h_size == newsize) {
 118:                 newsize = hashsizes[n];
 119:                 break;
 120:             }
 121:         if (newsize == 0)
 122:             newsize = oldhtp->h_size * 2 + 1;
 123:     }
 124: #ifdef DEBUG
 125:     if(debug > 3)
 126:         fprintf(ddt, "savehash GROWING to %d\n", newsize);
 127: #endif
 128:     htp = (struct hashbuf *) malloc((unsigned)HASHSIZE(newsize));
 129:     if (htp == NULL) {
 130:         syslog(LOG_ERR, "savehash: %m");
 131:         exit(1);
 132:     }
 133:     htp->h_size = newsize;
 134:     bzero((char *) htp->h_tab, newsize * sizeof(struct hashbuf *));
 135:     if (oldhtp == NULL) {
 136:         htp->h_cnt = 0;
 137:         return (htp);
 138:     }
 139: #ifdef DEBUG
 140:     if (debug > 3)
 141:         fprintf(ddt,"savehash(%x) cnt=%d, sz=%d, newsz=%d\n",
 142:             oldhtp, oldhtp->h_cnt, oldhtp->h_size, newsize);
 143: #endif
 144:     htp->h_cnt = oldhtp->h_cnt;
 145:     for (n = 0; n < oldhtp->h_size; n++) {
 146:         for (np = oldhtp->h_tab[n]; np != NULL; np = nnp) {
 147:             nnp = np->n_next;
 148:             hp = &htp->h_tab[np->n_hashval % htp->h_size];
 149:             np->n_next = *hp;
 150:             *hp = np;
 151:         }
 152:     }
 153:     free((char *) oldhtp);
 154:     return (htp);
 155: }
 156: 
 157: /*
 158:  * Allocate an inverse query buffer.
 159:  */
 160: struct invbuf *
 161: saveinv()
 162: {
 163:     register struct invbuf *ip;
 164: 
 165:     ip = (struct invbuf *) malloc(sizeof(struct invbuf));
 166:     if (ip == NULL) {
 167:         syslog(LOG_ERR, "saveinv: %m");
 168:         exit(1);
 169:     }
 170:     ip->i_next = NULL;
 171:     bzero((char *)ip->i_dname, sizeof(ip->i_dname));
 172:     return (ip);
 173: }
 174: 
 175: /*
 176:  * Make a copy of a string and return a pointer to it.
 177:  */
 178: char *
 179: savestr(str)
 180:     char *str;
 181: {
 182:     char *cp;
 183: 
 184:     cp = malloc((unsigned)strlen(str) + 1);
 185:     if (cp == NULL) {
 186:         syslog(LOG_ERR, "savestr: %m");
 187:         exit(1);
 188:     }
 189:     (void) strcpy(cp, str);
 190:     return (cp);
 191: }
Last modified: 1994-01-11
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1482
Valid CSS Valid XHTML 1.0 Strict