home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.sbin / named / db_save.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-24  |  5.0 KB  |  213 lines

  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_save.c    4.16 (Berkeley) 3/21/91";
  36. #endif /* not lint */
  37.  
  38. /*
  39.  * Buffer allocation and deallocation routines.
  40.  */
  41.  
  42. #include <sys/param.h>
  43. #include <arpa/nameser.h>
  44. #include <syslog.h>
  45. #include <stdio.h>
  46. #include "db.h"
  47.  
  48. #ifdef DEBUG
  49. extern int debug;
  50. extern FILE *ddt;
  51. #endif
  52.  
  53. extern char *strcpy();
  54.  
  55. /*
  56.  * Allocate a name buffer & save name.
  57.  */
  58. struct namebuf *
  59. savename(name)
  60.     char *name;
  61. {
  62.     register struct namebuf *np;
  63.  
  64.     np = (struct namebuf *) malloc(sizeof(struct namebuf));
  65.     if (np == NULL) {
  66.         syslog(LOG_ERR, "savename: %m");
  67.         exit(1);
  68.     }
  69.     np->n_dname = savestr(name);
  70.     np->n_next = NULL;
  71.     np->n_data = NULL;
  72.     np->n_hash = NULL;
  73.     return (np);
  74. }
  75.  
  76. /*
  77.  * Allocate a data buffer & save data.
  78.  */
  79. struct databuf *
  80. savedata(class, type, ttl, data, size)
  81.     int class, type;
  82.     u_long ttl;
  83.     char *data;
  84.     int size;
  85. {
  86.     register struct databuf *dp;
  87.  
  88.     if (type == T_NS)
  89.         dp = (struct databuf *) 
  90.             malloc((unsigned)DATASIZE(size)+sizeof(u_long));
  91.     else
  92.         dp = (struct databuf *) malloc((unsigned)DATASIZE(size));
  93.     if (dp == NULL) {
  94.         syslog(LOG_ERR, "savedata: %m");
  95.         exit(1);
  96.     }
  97.     dp->d_next = NULL;
  98.     dp->d_type = type;
  99.     dp->d_class = class;
  100.     dp->d_ttl = ttl;
  101.     dp->d_size = size;
  102.     dp->d_mark = 0;
  103.     dp->d_flags = 0;
  104.     dp->d_nstime = 0;
  105.     bcopy(data, dp->d_data, dp->d_size);
  106.     return (dp);
  107. }
  108.  
  109. int hashsizes[] = {    /* hashtable sizes */
  110.     2,
  111.     11,
  112.     113,
  113.     337,
  114.     977,
  115.     2053,
  116.     4073,
  117.     8011,
  118.     16001,
  119.     0
  120. };
  121.  
  122. /*
  123.  * Allocate a data buffer & save data.
  124.  */
  125. struct hashbuf *
  126. savehash(oldhtp)
  127.     register struct hashbuf *oldhtp;
  128. {
  129.     register struct hashbuf *htp;
  130.     register struct namebuf *np, *nnp, **hp;
  131.     register int n;
  132.     int newsize;
  133.  
  134.     if (oldhtp == NULL)
  135.         newsize = hashsizes[0];
  136.     else {
  137.         for (n = 0; newsize = hashsizes[n++]; )
  138.             if (oldhtp->h_size == newsize) {
  139.                 newsize = hashsizes[n];
  140.                 break;
  141.             }
  142.         if (newsize == 0)
  143.             newsize = oldhtp->h_size * 2 + 1;
  144.     }
  145. #ifdef DEBUG
  146.     if(debug > 3)
  147.         fprintf(ddt, "savehash GROWING to %d\n", newsize);
  148. #endif
  149.     htp = (struct hashbuf *) malloc((unsigned)HASHSIZE(newsize));
  150.     if (htp == NULL) {
  151.         syslog(LOG_ERR, "savehash: %m");
  152.         exit(1);
  153.     }
  154.     htp->h_size = newsize;
  155.     bzero((char *) htp->h_tab, newsize * sizeof(struct hashbuf *));
  156.     if (oldhtp == NULL) {
  157.         htp->h_cnt = 0;
  158.         return (htp);
  159.     }
  160. #ifdef DEBUG
  161.     if (debug > 3)
  162.         fprintf(ddt,"savehash(%#x) cnt=%d, sz=%d, newsz=%d\n",
  163.             oldhtp, oldhtp->h_cnt, oldhtp->h_size, newsize);
  164. #endif
  165.     htp->h_cnt = oldhtp->h_cnt;
  166.     for (n = 0; n < oldhtp->h_size; n++) {
  167.         for (np = oldhtp->h_tab[n]; np != NULL; np = nnp) {
  168.             nnp = np->n_next;
  169.             hp = &htp->h_tab[np->n_hashval % htp->h_size];
  170.             np->n_next = *hp;
  171.             *hp = np;
  172.         }
  173.     }
  174.     free((char *) oldhtp);
  175.     return (htp);
  176. }
  177.  
  178. /*
  179.  * Allocate an inverse query buffer.
  180.  */
  181. struct invbuf *
  182. saveinv()
  183. {
  184.     register struct invbuf *ip;
  185.  
  186.     ip = (struct invbuf *) malloc(sizeof(struct invbuf));
  187.     if (ip == NULL) {
  188.         syslog(LOG_ERR, "saveinv: %m");
  189.         exit(1);
  190.     }
  191.     ip->i_next = NULL;
  192.     bzero((char *)ip->i_dname, sizeof(ip->i_dname));
  193.     return (ip);
  194. }
  195.  
  196. /*
  197.  * Make a copy of a string and return a pointer to it.
  198.  */
  199. char *
  200. savestr(str)
  201.     char *str;
  202. {
  203.     char *cp;
  204.  
  205.     cp = malloc((unsigned)strlen(str) + 1);
  206.     if (cp == NULL) {
  207.         syslog(LOG_ERR, "savestr: %m");
  208.         exit(1);
  209.     }
  210.     (void) strcpy(cp, str);
  211.     return (cp);
  212. }
  213.