home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / NETWORK / ISP / bind.4.8.3.lzh / BIND483 / NAMED / db_save.c < prev    next >
Text File  |  1993-08-24  |  4KB  |  199 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 are permitted provided
  6.  * that: (1) source distributions retain this entire copyright notice and
  7.  * comment, and (2) distributions including binaries display the following
  8.  * acknowledgement:  ``This product includes software developed by the
  9.  * University of California, Berkeley and its contributors'' in the
  10.  * documentation or other materials provided with the distribution and in
  11.  * all advertising materials mentioning features or use of this software.
  12.  * Neither the name of the University nor the names of its contributors may
  13.  * be used to endorse or promote products derived from this software without
  14.  * specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  16.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  17.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #ifndef lint
  21. static char sccsid[] = "@(#)db_save.c    4.15 (Berkeley) 6/1/90";
  22. #endif /* not lint */
  23.  
  24. /*
  25.  * Buffer allocation and deallocation routines.
  26.  */
  27.  
  28. #include <sys/types.h>
  29. #include <stdio.h>
  30. #include <syslog.h>
  31. #include <arpa/nameser.h>
  32. #include "db.h"
  33.  
  34. #ifdef DEBUG
  35. extern int debug;
  36. extern FILE *ddt;
  37. #endif
  38.  
  39. extern char *strcpy();
  40.  
  41. /*
  42.  * Allocate a name buffer & save name.
  43.  */
  44. struct namebuf *
  45. savename(name)
  46.     char *name;
  47. {
  48.     register struct namebuf *np;
  49.  
  50.     np = (struct namebuf *) malloc(sizeof(struct namebuf));
  51.     if (np == NULL) {
  52.         syslog(LOG_ERR, "savename: %m");
  53.         exit(1);
  54.     }
  55.     np->n_dname = savestr(name);
  56.     np->n_next = NULL;
  57.     np->n_data = NULL;
  58.     np->n_hash = NULL;
  59.     return (np);
  60. }
  61.  
  62. /*
  63.  * Allocate a data buffer & save data.
  64.  */
  65. struct databuf *
  66. savedata(class, type, ttl, data, size)
  67.     int class, type;
  68.     u_long ttl;
  69.     char *data;
  70.     int size;
  71. {
  72.     register struct databuf *dp;
  73.  
  74.     if (type == T_NS)
  75.         dp = (struct databuf *) 
  76.             malloc((unsigned)DATASIZE(size)+sizeof(u_long));
  77.     else
  78.         dp = (struct databuf *) malloc((unsigned)DATASIZE(size));
  79.     if (dp == NULL) {
  80.         syslog(LOG_ERR, "savedata: %m");
  81.         exit(1);
  82.     }
  83.     dp->d_next = NULL;
  84.     dp->d_type = type;
  85.     dp->d_class = class;
  86.     dp->d_ttl = ttl;
  87.     dp->d_size = size;
  88.     dp->d_mark = 0;
  89.     dp->d_flags = 0;
  90.     dp->d_nstime = 0;
  91.     bcopy(data, dp->d_data, dp->d_size);
  92.     return (dp);
  93. }
  94.  
  95. int hashsizes[] = {    /* hashtable sizes */
  96.     2,
  97.     11,
  98.     113,
  99.     337,
  100.     977,
  101.     2053,
  102.     4073,
  103.     8011,
  104.     16001,
  105.     0
  106. };
  107.  
  108. /*
  109.  * Allocate a data buffer & save data.
  110.  */
  111. struct hashbuf *
  112. savehash(oldhtp)
  113.     register struct hashbuf *oldhtp;
  114. {
  115.     register struct hashbuf *htp;
  116.     register struct namebuf *np, *nnp, **hp;
  117.     register int n;
  118.     int newsize;
  119.  
  120.     if (oldhtp == NULL)
  121.         newsize = hashsizes[0];
  122.     else {
  123.         for (n = 0; newsize = hashsizes[n++]; )
  124.             if (oldhtp->h_size == newsize) {
  125.                 newsize = hashsizes[n];
  126.                 break;
  127.             }
  128.         if (newsize == 0)
  129.             newsize = oldhtp->h_size * 2 + 1;
  130.     }
  131. #ifdef DEBUG
  132.     if(debug > 3)
  133.         fprintf(ddt, "savehash GROWING to %d\n", newsize);
  134. #endif
  135.     htp = (struct hashbuf *) malloc((unsigned)HASHSIZE(newsize));
  136.     if (htp == NULL) {
  137.         syslog(LOG_ERR, "savehash: %m");
  138.         exit(1);
  139.     }
  140.     htp->h_size = newsize;
  141.     bzero((char *) htp->h_tab, newsize * sizeof(struct hashbuf *));
  142.     if (oldhtp == NULL) {
  143.         htp->h_cnt = 0;
  144.         return (htp);
  145.     }
  146. #ifdef DEBUG
  147.     if (debug > 3)
  148.         fprintf(ddt,"savehash(%#x) cnt=%d, sz=%d, newsz=%d\n",
  149.             oldhtp, oldhtp->h_cnt, oldhtp->h_size, newsize);
  150. #endif
  151.     htp->h_cnt = oldhtp->h_cnt;
  152.     for (n = 0; n < oldhtp->h_size; n++) {
  153.         for (np = oldhtp->h_tab[n]; np != NULL; np = nnp) {
  154.             nnp = np->n_next;
  155.             hp = &htp->h_tab[np->n_hashval % htp->h_size];
  156.             np->n_next = *hp;
  157.             *hp = np;
  158.         }
  159.     }
  160.     free((char *) oldhtp);
  161.     return (htp);
  162. }
  163.  
  164. /*
  165.  * Allocate an inverse query buffer.
  166.  */
  167. struct invbuf *
  168. saveinv()
  169. {
  170.     register struct invbuf *ip;
  171.  
  172.     ip = (struct invbuf *) malloc(sizeof(struct invbuf));
  173.     if (ip == NULL) {
  174.         syslog(LOG_ERR, "saveinv: %m");
  175.         exit(1);
  176.     }
  177.     ip->i_next = NULL;
  178.     bzero((char *)ip->i_dname, sizeof(ip->i_dname));
  179.     return (ip);
  180. }
  181.  
  182. /*
  183.  * Make a copy of a string and return a pointer to it.
  184.  */
  185. char *
  186. savestr(str)
  187.     char *str;
  188. {
  189.     char *cp;
  190.  
  191.     cp = malloc((unsigned)strlen(str) + 1);
  192.     if (cp == NULL) {
  193.         syslog(LOG_ERR, "savestr: %m");
  194.         exit(1);
  195.     }
  196.     (void) strcpy(cp, str);
  197.     return (cp);
  198. }
  199.