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