home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / n / bind / bind-4.001 / bind-4~ / bind-4.9.3-BETA9 / named / db_save.c < prev    next >
C/C++ Source or Header  |  1994-07-22  |  6KB  |  215 lines

  1. #if !defined(lint) && !defined(SABER)
  2. static char sccsid[] = "@(#)db_save.c    4.16 (Berkeley) 3/21/91";
  3. static char rcsid[] = "$Id: db_save.c,v 4.9.1.7 1994/07/22 08:42:39 vixie Exp $";
  4. #endif /* not lint */
  5.  
  6. /*
  7.  * ++Copyright++ 1986
  8.  * -
  9.  * Copyright (c) 1986
  10.  *    The Regents of the University of California.  All rights reserved.
  11.  * 
  12.  * Redistribution and use in source and binary forms, with or without
  13.  * modification, are permitted provided that the following conditions
  14.  * are met:
  15.  * 1. Redistributions of source code must retain the above copyright
  16.  *    notice, this list of conditions and the following disclaimer.
  17.  * 2. Redistributions in binary form must reproduce the above copyright
  18.  *    notice, this list of conditions and the following disclaimer in the
  19.  *    documentation and/or other materials provided with the distribution.
  20.  * 3. All advertising materials mentioning features or use of this software
  21.  *    must display the following acknowledgement:
  22.  *     This product includes software developed by the University of
  23.  *     California, Berkeley and its contributors.
  24.  * 4. Neither the name of the University nor the names of its contributors
  25.  *    may be used to endorse or promote products derived from this software
  26.  *    without specific prior written permission.
  27.  * 
  28.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  29.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  32.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  37.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38.  * SUCH DAMAGE.
  39.  * -
  40.  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  41.  * 
  42.  * Permission to use, copy, modify, and distribute this software for any
  43.  * purpose with or without fee is hereby granted, provided that the above
  44.  * copyright notice and this permission notice appear in all copies, and that
  45.  * the name of Digital Equipment Corporation not be used in advertising or
  46.  * publicity pertaining to distribution of the document or software without
  47.  * specific, written prior permission.
  48.  * 
  49.  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  50.  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  51.  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
  52.  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  53.  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  54.  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  55.  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  56.  * SOFTWARE.
  57.  * -
  58.  * --Copyright--
  59.  */
  60.  
  61. /*
  62.  * Buffer allocation and deallocation routines.
  63.  */
  64.  
  65. #include <sys/param.h>
  66. #include <sys/socket.h>
  67. #include <netinet/in.h>
  68. #include <arpa/nameser.h>
  69. #include <syslog.h>
  70. #include <stdio.h>
  71.  
  72. #include "named.h"
  73.  
  74. /*
  75.  * Allocate a name buffer & save name.
  76.  */
  77. struct namebuf *
  78. savename(name)
  79.     char *name;
  80. {
  81.     register struct namebuf *np;
  82.  
  83.     np = (struct namebuf *) malloc(sizeof(struct namebuf));
  84.     if (np == NULL) {
  85.         syslog(LOG_ERR, "savename: %m");
  86.         exit(1);
  87.     }
  88.     np->n_dname = savestr(name);
  89.     np->n_next = NULL;
  90.     np->n_data = NULL;
  91.     np->n_hash = NULL;
  92.     return (np);
  93. }
  94.  
  95. /*
  96.  * Allocate a data buffer & save data.
  97.  */
  98. struct databuf *
  99. #ifdef DMALLOC
  100. savedata_tagged(file, line, class, type, ttl, data, size)
  101.     char *file;
  102.     int line;
  103. #else
  104. savedata(class, type, ttl, data, size)
  105. #endif
  106.     int class, type;
  107.     u_int32_t ttl;
  108.     u_char *data;
  109.     int size;
  110. {
  111.     register struct databuf *dp;
  112.  
  113.     if (type == T_NS)
  114.         dp = (struct databuf *) 
  115. #ifdef DMALLOC
  116.             dmalloc(file, line,
  117. #else
  118.             malloc(
  119. #endif
  120.                (unsigned)DATASIZE(size)+INT32SZ);
  121.     else
  122.         dp = (struct databuf *)
  123. #ifdef DMALLOC
  124.                 dmalloc(file, line,
  125. #else
  126.                 malloc(
  127. #endif
  128.                    (unsigned)DATASIZE(size));
  129.     if (dp == NULL) {
  130.         syslog(LOG_ERR, "savedata: %m");
  131.         exit(1);
  132.     }
  133.     dp->d_next = NULL;
  134.     dp->d_type = type;
  135.     dp->d_class = class;
  136.     dp->d_ttl = ttl;
  137.     dp->d_size = size;
  138.     dp->d_mark = 0;
  139.     dp->d_flags = 0;
  140.     dp->d_cred = 0;
  141.     dp->d_clev = 0;
  142. #ifdef NCACHE
  143.     dp->d_rcode = NOERROR;
  144. #endif
  145. #ifdef STATS
  146.     dp->d_ns = NULL;
  147. #endif
  148.     dp->d_nstime = 0;
  149.     bcopy(data, dp->d_data, dp->d_size);
  150.     return (dp);
  151. }
  152.  
  153. int hashsizes[] = {    /* hashtable sizes */
  154.     2,
  155.     11,
  156.     113,
  157.     337,
  158.     977,
  159.     2053,
  160.     4073,
  161.     8011,
  162.     16001,
  163.     0
  164. };
  165.  
  166. /*
  167.  * Allocate a data buffer & save data.
  168.  */
  169. struct hashbuf *
  170. savehash(oldhtp)
  171.     register struct hashbuf *oldhtp;
  172. {
  173.     register struct hashbuf *htp;
  174.     register struct namebuf *np, *nnp, **hp;
  175.     register int n;
  176.     int newsize;
  177.  
  178.     if (oldhtp == NULL)
  179.         newsize = hashsizes[0];
  180.     else {
  181.         for (n = 0; newsize = hashsizes[n++]; )
  182.             if (oldhtp->h_size == newsize) {
  183.                 newsize = hashsizes[n];
  184.                 break;
  185.             }
  186.         if (newsize == 0)
  187.             newsize = oldhtp->h_size * 2 + 1;
  188.     }
  189.     dprintf(4, (ddt, "savehash GROWING to %d\n", newsize));
  190.     htp = (struct hashbuf *) malloc((unsigned)HASHSIZE(newsize));
  191.     if (htp == NULL) {
  192.         syslog(LOG_ERR, "savehash: %m");
  193.         exit(1);
  194.     }
  195.     htp->h_size = newsize;
  196.     bzero((char *) htp->h_tab, newsize * sizeof(struct namebuf *));
  197.     if (oldhtp == NULL) {
  198.         htp->h_cnt = 0;
  199.         return (htp);
  200.     }
  201.     dprintf(4, (ddt, "savehash(%#x) cnt=%d, sz=%d, newsz=%d\n",
  202.             oldhtp, oldhtp->h_cnt, oldhtp->h_size, newsize));
  203.     htp->h_cnt = oldhtp->h_cnt;
  204.     for (n = 0; n < oldhtp->h_size; n++) {
  205.         for (np = oldhtp->h_tab[n]; np != NULL; np = nnp) {
  206.             nnp = np->n_next;
  207.             hp = &htp->h_tab[np->n_hashval % htp->h_size];
  208.             np->n_next = *hp;
  209.             *hp = np;
  210.         }
  211.     }
  212.     free((char *) oldhtp);
  213.     return (htp);
  214. }
  215.