nxt_30.c 7.37 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
1
/*
Brian Wellington's avatar
Brian Wellington committed
2
 * Copyright (C) 1999-2001  Internet Software Consortium.
3
 *
Mark Andrews's avatar
Mark Andrews committed
4 5 6
 * 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.
7
 *
8 9 10 11 12 13 14 15
 * 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.
Mark Andrews's avatar
Mark Andrews committed
16 17
 */

18
/* $Id: nxt_30.c,v 1.46 2001/02/12 03:04:51 bwelling Exp $ */
James Brister's avatar
James Brister committed
19 20

/* reviewed: Wed Mar 15 18:21:15 PST 2000 by brister */
Mark Andrews's avatar
Mark Andrews committed
21

22
/* RFC 2065 */
Mark Andrews's avatar
Mark Andrews committed
23

24 25
#ifndef RDATA_GENERIC_NXT_30_C
#define RDATA_GENERIC_NXT_30_C
Mark Andrews's avatar
Mark Andrews committed
26

27 28 29 30 31
/*
 * The attributes do not include DNS_RDATATYPEATTR_SINGLETON
 * because we must be able to handle a parent/child NXT pair.
 */
#define RRTYPE_NXT_ATTRIBUTES (DNS_RDATATYPEATTR_DNSSEC)
32

33
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
34
fromtext_nxt(ARGS_FROMTEXT) {
Mark Andrews's avatar
Mark Andrews committed
35 36 37 38 39 40
	isc_token_t token;
	dns_name_t name;
	isc_buffer_t buffer;
	char *e;
	unsigned char bm[8*1024]; /* 64k bits */
	dns_rdatatype_t covered;
41
	dns_rdatatype_t maxcovered = 0;
42
	isc_boolean_t first = ISC_TRUE;
43
	long n;
Mark Andrews's avatar
Mark Andrews committed
44 45 46

	REQUIRE(type == 30);

James Brister's avatar
James Brister committed
47
	UNUSED(rdclass);
48

David Lawrence's avatar
David Lawrence committed
49 50 51
	/*
	 * Next domain.
	 */
52 53
	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
				      ISC_FALSE));
Mark Andrews's avatar
Mark Andrews committed
54
	dns_name_init(&name, NULL);
55
	buffer_fromregion(&buffer, &token.value.as_region);
Mark Andrews's avatar
Mark Andrews committed
56 57 58 59
	origin = (origin != NULL) ? origin : dns_rootname;
	RETERR(dns_name_fromtext(&name, &buffer, origin, downcase, target));

	memset(bm, 0, sizeof bm);
60
	do {
61 62
		RETERR(isc_lex_getmastertoken(lexer, &token,
					      isc_tokentype_string, ISC_TRUE));
Mark Andrews's avatar
Mark Andrews committed
63 64
		if (token.type != isc_tokentype_string)
			break;
65
		n = strtol(token.value.as_pointer, &e, 10);
David Lawrence's avatar
David Lawrence committed
66
		if (e != (char *)token.value.as_pointer && *e == '\0') {
67
			covered = (dns_rdatatype_t)n;
68
		} else if (dns_rdatatype_fromtext(&covered,
Mark Andrews's avatar
Mark Andrews committed
69 70
				&token.value.as_textregion) == DNS_R_UNKNOWN)
			return (DNS_R_UNKNOWN);
71 72 73 74
		/*
		 * NXT is only specified for types 1..127.
		 */
		if (covered < 1 || covered > 127)
David Lawrence's avatar
David Lawrence committed
75
			return (ISC_R_RANGE);
76
		if (first || covered > maxcovered)
Mark Andrews's avatar
Mark Andrews committed
77
			maxcovered = covered;
78
		first = ISC_FALSE;
Mark Andrews's avatar
Mark Andrews committed
79
		bm[covered/8] |= (0x80>>(covered%8));
80
	} while (1);
Mark Andrews's avatar
Mark Andrews committed
81
	isc_lex_ungettoken(lexer, &token);
82
	if (first)
83
		return (ISC_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
84 85 86 87
	n = (maxcovered + 8) / 8;
	return (mem_tobuffer(target, bm, n));
}

88
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
89
totext_nxt(ARGS_TOTEXT) {
Mark Andrews's avatar
Mark Andrews committed
90 91 92 93 94 95 96
	isc_region_t sr;
	unsigned int i, j;
	dns_name_t name;
	dns_name_t prefix;
	isc_boolean_t sub;

	REQUIRE(rdata->type == 30);
97
	REQUIRE(rdata->length != 0);
Mark Andrews's avatar
Mark Andrews committed
98 99 100 101 102 103

	dns_name_init(&name, NULL);
	dns_name_init(&prefix, NULL);
	dns_rdata_toregion(rdata, &sr);
	dns_name_fromregion(&name, &sr);
	isc_region_consume(&sr, name_length(&name));
104
	sub = name_prefix(&name, tctx->origin, &prefix);
Mark Andrews's avatar
Mark Andrews committed
105 106 107 108 109
	RETERR(dns_name_totext(&prefix, sub, target));

	for (i = 0 ; i < sr.length ; i++) {
		if (sr.base[i] != 0)
			for (j = 0; j < 8; j++)
110
				if ((sr.base[i] & (0x80 >> j)) != 0) {
111
					dns_rdatatype_t t = i * 8 + j;
112
					RETERR(str_totext(" ", target));
113
					if (dns_rdatatype_isknown(t)) {
114 115
						RETERR(dns_rdatatype_totext(t,
								      target));
116 117 118
					} else {
						char buf[sizeof "65535"];
						sprintf(buf, "%u", t);
119 120
						RETERR(str_totext(buf,
								  target));
Mark Andrews's avatar
Mark Andrews committed
121 122 123
					}
				}
	}
124
	return (ISC_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
125 126
}

127
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
128
fromwire_nxt(ARGS_FROMWIRE) {
Mark Andrews's avatar
Mark Andrews committed
129 130 131 132 133
	isc_region_t sr;
	dns_name_t name;

	REQUIRE(type == 30);

James Brister's avatar
James Brister committed
134
	UNUSED(rdclass);
Mark Andrews's avatar
Mark Andrews committed
135

136
	dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
Mark Andrews's avatar
Mark Andrews committed
137

Mark Andrews's avatar
Mark Andrews committed
138 139
	dns_name_init(&name, NULL);
	RETERR(dns_name_fromwire(&name, source, dctx, downcase, target));
140

141
	isc_buffer_activeregion(source, &sr);
Bob Halley's avatar
Bob Halley committed
142
	/* XXXRTH  Enforce RFC 2535 length rules if bit 0 is not set. */
Mark Andrews's avatar
Mark Andrews committed
143 144 145 146
	if (sr.length > 8 * 1024)
		return (DNS_R_EXTRADATA);
	RETERR(mem_tobuffer(target, sr.base, sr.length));
	isc_buffer_forward(source, sr.length);
147
	return (ISC_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
148 149
}

150
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
151
towire_nxt(ARGS_TOWIRE) {
Mark Andrews's avatar
Mark Andrews committed
152 153
	isc_region_t sr;
	dns_name_t name;
154
	dns_offsets_t offsets;
Mark Andrews's avatar
Mark Andrews committed
155 156

	REQUIRE(rdata->type == 30);
157
	REQUIRE(rdata->length != 0);
Mark Andrews's avatar
Mark Andrews committed
158

159
	dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
160
	dns_name_init(&name, offsets);
Mark Andrews's avatar
Mark Andrews committed
161 162 163 164 165 166 167 168
	dns_rdata_toregion(rdata, &sr);
	dns_name_fromregion(&name, &sr);
	isc_region_consume(&sr, name_length(&name));
	RETERR(dns_name_towire(&name, cctx, target));

	return (mem_tobuffer(target, sr.base, sr.length));
}

169
static inline int
David Lawrence's avatar
David Lawrence committed
170
compare_nxt(ARGS_COMPARE) {
Mark Andrews's avatar
Mark Andrews committed
171 172 173 174
	isc_region_t r1;
	isc_region_t r2;
	dns_name_t name1;
	dns_name_t name2;
175
	int order;
Mark Andrews's avatar
Mark Andrews committed
176 177

	REQUIRE(rdata1->type == rdata2->type);
178
	REQUIRE(rdata1->rdclass == rdata2->rdclass);
Mark Andrews's avatar
Mark Andrews committed
179
	REQUIRE(rdata1->type == 30);
180 181
	REQUIRE(rdata1->length != 0);
	REQUIRE(rdata2->length != 0);
Mark Andrews's avatar
Mark Andrews committed
182 183 184 185 186 187 188

	dns_name_init(&name1, NULL);
	dns_name_init(&name2, NULL);
	dns_rdata_toregion(rdata1, &r1);
	dns_rdata_toregion(rdata2, &r2);
	dns_name_fromregion(&name1, &r1);
	dns_name_fromregion(&name2, &r2);
189 190 191
	order = dns_name_rdatacompare(&name1, &name2);
	if (order != 0)
		return (order);
Mark Andrews's avatar
Mark Andrews committed
192 193 194 195

	return (compare_region(&r1, &r2));
}

196
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
197
fromstruct_nxt(ARGS_FROMSTRUCT) {
198
	dns_rdata_nxt_t *nxt = source;
199
	isc_region_t region;
Mark Andrews's avatar
Mark Andrews committed
200 201

	REQUIRE(type == 30);
202 203 204
	REQUIRE(source != NULL);
	REQUIRE(nxt->common.rdtype == type);
	REQUIRE(nxt->common.rdclass == rdclass);
205
	REQUIRE(nxt->typebits != NULL || nxt->len == 0);
Mark Andrews's avatar
Mark Andrews committed
206

207 208
	UNUSED(rdclass);

209 210 211 212
	dns_name_toregion(&nxt->next, &region);
	RETERR(isc_buffer_copyregion(target, &region));

	return (mem_tobuffer(target, nxt->typebits, nxt->len));
Mark Andrews's avatar
Mark Andrews committed
213 214
}

215
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
216
tostruct_nxt(ARGS_TOSTRUCT) {
217
	isc_region_t region;
218
	dns_rdata_nxt_t *nxt = target;
219
	dns_name_t name;
Mark Andrews's avatar
Mark Andrews committed
220 221

	REQUIRE(rdata->type == 30);
222
	REQUIRE(target != NULL);
223
	REQUIRE(rdata->length != 0);
Mark Andrews's avatar
Mark Andrews committed
224

225 226 227
	nxt->common.rdclass = rdata->rdclass;
	nxt->common.rdtype = rdata->type;
	ISC_LINK_INIT(&nxt->common, link);
Mark Andrews's avatar
Mark Andrews committed
228

229 230 231 232 233 234 235 236
	dns_name_init(&name, NULL);
	dns_rdata_toregion(rdata, &region);
	dns_name_fromregion(&name, &region);
	isc_region_consume(&region, name_length(&name));
	dns_name_init(&nxt->next, NULL);
	RETERR(name_duporclone(&name, mctx, &nxt->next));

	nxt->len = region.length;
237 238 239
	nxt->typebits = mem_maybedup(mctx, region.base, region.length);
	if (nxt->typebits == NULL)
		goto cleanup;
240 241 242

	nxt->mctx = mctx;
	return (ISC_R_SUCCESS);
243 244 245 246 247

 cleanup:
	if (mctx != NULL)
		dns_name_free(&nxt->next, mctx);
	return (ISC_R_NOMEMORY);
Mark Andrews's avatar
Mark Andrews committed
248
}
249

250
static inline void
David Lawrence's avatar
David Lawrence committed
251
freestruct_nxt(ARGS_FREESTRUCT) {
252 253 254 255
	dns_rdata_nxt_t *nxt = source;

	REQUIRE(source != NULL);
	REQUIRE(nxt->common.rdtype == 30);
256

257 258 259
	if (nxt->mctx == NULL)
		return;

260 261 262
	dns_name_free(&nxt->next, nxt->mctx);
	if (nxt->typebits != NULL)
		isc_mem_free(nxt->mctx, nxt->typebits);
263
	nxt->mctx = NULL;
264
}
265

266
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
267
additionaldata_nxt(ARGS_ADDLDATA) {
268 269
	REQUIRE(rdata->type == 30);

270 271 272
	UNUSED(rdata);
	UNUSED(add);
	UNUSED(arg);
273

274
	return (ISC_R_SUCCESS);
275 276
}

277
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
278
digest_nxt(ARGS_DIGEST) {
Bob Halley's avatar
Bob Halley committed
279 280
	isc_region_t r;
	dns_name_t name;
281
	isc_result_t result;
Bob Halley's avatar
Bob Halley committed
282 283 284 285 286 287 288

	REQUIRE(rdata->type == 30);

	dns_rdata_toregion(rdata, &r);
	dns_name_init(&name, NULL);
	dns_name_fromregion(&name, &r);
	result = dns_name_digest(&name, digest, arg);
289
	if (result != ISC_R_SUCCESS)
Bob Halley's avatar
Bob Halley committed
290 291 292 293 294 295
		return (result);
	isc_region_consume(&r, name_length(&name));

	return ((digest)(arg, &r));
}

296
#endif	/* RDATA_GENERIC_NXT_30_C */