summaryrefslogtreecommitdiffstats
path: root/contrib/bind/bin/named/ns_parseutil.c
blob: aed15af2baa67d936e6dad97dfa15f1126b2eee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * Copyright (c) 1996, 1997 by Internet Software Consortium.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

/* Global C stuff goes here. */


#include "port_before.h"

#include <sys/types.h>

#include <netinet/in.h>
#include <arpa/nameser.h>

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>

#include <isc/eventlib.h>
#include <isc/logging.h>
#include <isc/memcluster.h>

#include "port_after.h"

#include "named.h"
#include "ns_parseutil.h"


/*
 * Symbol Table
 */

symbol_table
new_symbol_table(int size_guess, free_function free_value) {
	symbol_table st;

	st = (symbol_table)memget(sizeof (struct symbol_table));
	if (st == NULL)
		panic("memget failed in new_symbol_table()", NULL);
	st->table = (symbol_entry *)memget(size_guess * sizeof *st->table);
	if (st->table == NULL)
		panic("memget failed in new_symbol_table()", NULL);
	memset(st->table, 0, size_guess * sizeof (symbol_entry));
	st->size = size_guess;   /* size_guess should be prime */
	st->free_value = free_value;
	return (st);
}

void
free_symbol(symbol_table st, symbol_entry ste) {
	if (ste->flags & SYMBOL_FREE_KEY)
		freestr(ste->key);
	if (ste->flags & SYMBOL_FREE_VALUE)
		(st->free_value)(ste->type, ste->value.pointer);
}

void
free_symbol_table(symbol_table st) {
	int i;
	symbol_entry ste, ste_next;

	for (i = 0; i < st->size; i++) {
		for (ste = st->table[i]; ste != NULL; ste = ste_next) {
			ste_next = ste->next;
			free_symbol(st, ste);
			memput(ste, sizeof *ste);
		}
	}
	memput(st->table, st->size * sizeof (symbol_entry));
	memput(st, sizeof *st);
}

void
dprint_symbol_table(int level, symbol_table st) {
	int i;
	symbol_entry ste;

	for (i = 0; i < st->size; i++) {
		for (ste = st->table[i]; ste != NULL; ste = ste->next)
			ns_debug(ns_log_parser, level,
				 "%7d: (%s: %d %p/%d %04x) ",
				 i, ste->key, ste->type, ste->value.pointer,
				 ste->value.integer, ste->flags);
	}
}

/*
 * P. J. Weinberger's hash function, adapted from p. 436 of
 * _Compilers: Principles, Techniques, and Tools_, Aho, Sethi
 * and Ullman, Addison-Wesley, 1986, ISBN 0-201-10088-6.
 */
static int
symbol_hash(const char *key, int prime) {
	const char *s;
	unsigned int h = 0;
	unsigned int g;
	int c;

	for (s = key; *s != '\0'; s++) {
		c = *s;
		if (isascii(c) && isupper(c))
			c = tolower(c);
		h = ( h << 4 ) + c;
		if ((g = ( h & 0xf0000000 )) != 0) {
			h = h ^ (g >> 24);
			h = h ^ g;
		}
	}
	return (h % prime);
}

int
lookup_symbol(symbol_table st, const char *key, int type,
	      symbol_value *value) {
	int hash;
	symbol_entry ste;

	hash = symbol_hash(key, st->size);
	for (ste = st->table[hash]; ste != NULL; ste = ste->next)
		if ((type == 0 || ste->type == type) &&
		    strcasecmp(ste->key, key) == 0)
			break;
	if (ste != NULL) {
		if (value != NULL)
			*value = ste->value;
		return (1);
	}
	return (0);
}

void
define_symbol(symbol_table st, char *key, int type, symbol_value value,
	      unsigned int flags) {
	int hash;
	symbol_entry ste;

	hash = symbol_hash(key, st->size);
	for (ste = st->table[hash]; ste != NULL; ste = ste->next)
		if ((type == 0 || ste->type == type) &&
		    strcasecmp(ste->key, key) == 0)
			break;
	if (ste == NULL) {
		ste = (symbol_entry)memget(sizeof *ste);
		if (ste == NULL)
			panic("memget failed in define_symbol()", NULL);
		ste->key = key;
		ste->type = type;
		ste->value = value;
		ste->flags = flags;
		ste->next = st->table[hash];
		st->table[hash] = ste;
	} else {
		ns_debug(ns_log_parser, 7, "redefined symbol %s type %d",
			 key, type);
		free_symbol(st, ste);
		ste->key = key;
		ste->value = value;
		ste->flags = flags;
	}
}

void
undefine_symbol(symbol_table st, char *key, int type) {
	int hash;
	symbol_entry prev_ste, ste, next_ste;

	hash = symbol_hash(key, st->size);
	for (prev_ste = NULL, ste = st->table[hash];
	     ste != NULL;
	     prev_ste = ste, ste = ste->next)
		if ((type == 0 || ste->type == type) &&
		    strcasecmp(ste->key, key) == 0)
			break;
	if (ste != NULL) {
		free_symbol(st, ste);
		if (prev_ste != NULL)
			prev_ste->next = ste->next;
		else
			st->table[hash] = ste->next;
		memput(ste, sizeof *ste);
	}
}

/*
 * Conversion Routines
 */

int
unit_to_ulong(char *in, u_long *out) {	
	int c, units_done = 0;
	u_long result = 0L;

	INSIST(in != NULL);

	for (; (c = *in) != '\0'; in++) {
		if (units_done)
			return (0);
		if (isdigit(c)) {
			result *= 10;
			result += (c - '0');
		} else {
			switch (c) {
			case 'k':
			case 'K':
				result *= 1024;
				units_done = 1;
				break;
			case 'm':
			case 'M':
				result *= (1024*1024);
				units_done = 1;
				break;
			case 'g':
			case 'G':
				result *= (1024*1024*1024);
				units_done = 1;
				break;
			default:
				return (0);
			}
		}
	}

	*out = result;
	return (1);
}
OpenPOWER on IntegriCloud