aboutsummaryrefslogtreecommitdiff
path: root/lib/dns/rdata/in_1/apl_42.c
blob: 70f6880ccd8938b6faa382d4e1f10f05080e2420 (plain) (blame)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
 * Copyright (C) 2004, 2005, 2007, 2008  Internet Systems Consortium, Inc. ("ISC")
 * Copyright (C) 2002  Internet Software Consortium.
 *
 * Permission to use, copy, modify, and/or 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 ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC 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.
 */

/* $Id: apl_42.c,v 1.14 2008-01-22 23:28:04 tbox Exp $ */

/* RFC3123 */

#ifndef RDATA_IN_1_APL_42_C
#define RDATA_IN_1_APL_42_C

#define RRTYPE_APL_ATTRIBUTES (0)

static inline isc_result_t
fromtext_in_apl(ARGS_FROMTEXT) {
	isc_token_t token;
	unsigned char addr[16];
	unsigned long afi;
	isc_uint8_t prefix;
	isc_uint8_t len;
	isc_boolean_t neg;
	char *cp, *ap, *slash;
	int n;

	REQUIRE(type == 42);
	REQUIRE(rdclass == 1);

	UNUSED(type);
	UNUSED(rdclass);
	UNUSED(origin);
	UNUSED(options);
	UNUSED(callbacks);

	do {
		RETERR(isc_lex_getmastertoken(lexer, &token,
					      isc_tokentype_string, ISC_TRUE));
		if (token.type != isc_tokentype_string)
			break;

		cp = DNS_AS_STR(token);
		neg = ISC_TF(*cp == '!');
		if (neg)
			cp++;
		afi = strtoul(cp, &ap, 10);
		if (*ap++ != ':' || cp == ap)
			RETTOK(DNS_R_SYNTAX);
		if (afi > 0xffffU)
			RETTOK(ISC_R_RANGE);
		slash = strchr(ap, '/');
		if (slash == NULL || slash == ap)
			RETTOK(DNS_R_SYNTAX);
		RETTOK(isc_parse_uint8(&prefix, slash + 1, 10));
		switch (afi) {
		case 1:
			*slash = '\0';
			n = inet_pton(AF_INET, ap, addr);
			*slash = '/';
			if (n != 1)
				RETTOK(DNS_R_BADDOTTEDQUAD);
			if (prefix > 32)
				RETTOK(ISC_R_RANGE);
			for (len = 4; len > 0; len--)
				if (addr[len - 1] != 0)
					break;
			break;

		case 2:
			*slash = '\0';
			n = inet_pton(AF_INET6, ap, addr);
			*slash = '/';
			if (n != 1)
				RETTOK(DNS_R_BADAAAA);
			if (prefix > 128)
				RETTOK(ISC_R_RANGE);
			for (len = 16; len > 0; len--)
				if (addr[len - 1] != 0)
					break;
			break;

		default:
			RETTOK(ISC_R_NOTIMPLEMENTED);
		}
		RETERR(uint16_tobuffer(afi, target));
		RETERR(uint8_tobuffer(prefix, target));
		RETERR(uint8_tobuffer(len | ((neg) ? 0x80 : 0), target));
		RETERR(mem_tobuffer(target, addr, len));
	} while (1);

	/*
	 * Let upper layer handle eol/eof.
	 */
	isc_lex_ungettoken(lexer, &token);

	return (ISC_R_SUCCESS);
}

static inline isc_result_t
totext_in_apl(ARGS_TOTEXT) {
	isc_region_t sr;
	isc_region_t ir;
	isc_uint16_t afi;
	isc_uint8_t prefix;
	isc_uint8_t len;
	isc_boolean_t neg;
	unsigned char buf[16];
	char txt[sizeof(" !64000")];
	const char *sep = "";
	int n;

	REQUIRE(rdata->type == 42);
	REQUIRE(rdata->rdclass == 1);

	UNUSED(tctx);

	dns_rdata_toregion(rdata, &sr);
	ir.base = buf;
	ir.length = sizeof(buf);

	while (sr.length > 0) {
		INSIST(sr.length >= 4);
		afi = uint16_fromregion(&sr);
		isc_region_consume(&sr, 2);
		prefix = *sr.base;
		isc_region_consume(&sr, 1);
		len = (*sr.base & 0x7f);
		neg = ISC_TF((*sr.base & 0x80) != 0);
		isc_region_consume(&sr, 1);
		INSIST(len <= sr.length);
		n = snprintf(txt, sizeof(txt), "%s%s%u:", sep,
			     neg ? "!": "", afi);
		INSIST(n < (int)sizeof(txt));
		RETERR(str_totext(txt, target));
		switch (afi) {
		case 1:
			INSIST(len <= 4);
			INSIST(prefix <= 32);
			memset(buf, 0, sizeof(buf));
			memcpy(buf, sr.base, len);
			RETERR(inet_totext(AF_INET, &ir, target));
			break;

		case 2:
			INSIST(len <= 16);
			INSIST(prefix <= 128);
			memset(buf, 0, sizeof(buf));
			memcpy(buf, sr.base, len);
			RETERR(inet_totext(AF_INET6, &ir, target));
			break;

		default:
			return (ISC_R_NOTIMPLEMENTED);
		}
		n = snprintf(txt, sizeof(txt), "/%u", prefix);
		INSIST(n < (int)sizeof(txt));
		RETERR(str_totext(txt, target));
		isc_region_consume(&sr, len);
		sep = " ";
	}
	return (ISC_R_SUCCESS);
}

static inline isc_result_t
fromwire_in_apl(ARGS_FROMWIRE) {
	isc_region_t sr, sr2;
	isc_region_t tr;
	isc_uint16_t afi;
	isc_uint8_t prefix;
	isc_uint8_t len;

	REQUIRE(type == 42);
	REQUIRE(rdclass == 1);

	UNUSED(type);
	UNUSED(dctx);
	UNUSED(rdclass);
	UNUSED(options);

	isc_buffer_activeregion(source, &sr);
	isc_buffer_availableregion(target, &tr);
	if (sr.length > tr.length)
		return (ISC_R_NOSPACE);
	sr2 = sr;

	/* Zero or more items */
	while (sr.length > 0) {
		if (sr.length < 4)
			return (ISC_R_UNEXPECTEDEND);
		afi = uint16_fromregion(&sr);
		isc_region_consume(&sr, 2);
		prefix = *sr.base;
		isc_region_consume(&sr, 1);
		len = (*sr.base & 0x7f);
		isc_region_consume(&sr, 1);
		if (len > sr.length)
			return (ISC_R_UNEXPECTEDEND);
		switch (afi) {
		case 1:
			if (prefix > 32 || len > 4)
				return (ISC_R_RANGE);
			break;
		case 2:
			if (prefix > 128 || len > 16)
				return (ISC_R_RANGE);
		}
		if (len > 0 && sr.base[len - 1] == 0)
			return (DNS_R_FORMERR);
		isc_region_consume(&sr, len);
	}
	isc_buffer_forward(source, sr2.length);
	return (mem_tobuffer(target, sr2.base, sr2.length));
}

static inline isc_result_t
towire_in_apl(ARGS_TOWIRE) {
	UNUSED(cctx);

	REQUIRE(rdata->type == 42);
	REQUIRE(rdata->rdclass == 1);

	return (mem_tobuffer(target, rdata->data, rdata->length));
}

static inline int
compare_in_apl(ARGS_COMPARE) {
	isc_region_t r1;
	isc_region_t r2;

	REQUIRE(rdata1->type == rdata2->type);
	REQUIRE(rdata1->rdclass == rdata2->rdclass);
	REQUIRE(rdata1->type == 42);
	REQUIRE(rdata1->rdclass == 1);

	dns_rdata_toregion(rdata1, &r1);
	dns_rdata_toregion(rdata2, &r2);
	return (isc_region_compare(&r1, &r2));
}

static inline isc_result_t
fromstruct_in_apl(ARGS_FROMSTRUCT) {
	dns_rdata_in_apl_t *apl = source;
	isc_buffer_t b;

	REQUIRE(type == 42);
	REQUIRE(rdclass == 1);
	REQUIRE(source != NULL);
	REQUIRE(apl->common.rdtype == type);
	REQUIRE(apl->common.rdclass == rdclass);
	REQUIRE(apl->apl != NULL || apl->apl_len == 0);

	isc_buffer_init(&b, apl->apl, apl->apl_len);
	isc_buffer_add(&b, apl->apl_len);
	isc_buffer_setactive(&b, apl->apl_len);
	return(fromwire_in_apl(rdclass, type, &b, NULL, ISC_FALSE, target));
}

static inline isc_result_t
tostruct_in_apl(ARGS_TOSTRUCT) {
	dns_rdata_in_apl_t *apl = target;
	isc_region_t r;

	REQUIRE(rdata->type == 42);
	REQUIRE(rdata->rdclass == 1);

	apl->common.rdclass = rdata->rdclass;
	apl->common.rdtype = rdata->type;
	ISC_LINK_INIT(&apl->common, link);

	dns_rdata_toregion(rdata, &r);
	apl->apl_len = r.length;
	apl->apl = mem_maybedup(mctx, r.base, r.length);
	if (apl->apl == NULL)
		return (ISC_R_NOMEMORY);

	apl->offset = 0;
	apl->mctx = mctx;
	return (ISC_R_SUCCESS);
}

static inline void
freestruct_in_apl(ARGS_FREESTRUCT) {
	dns_rdata_in_apl_t *apl = source;

	REQUIRE(source != NULL);
	REQUIRE(apl->common.rdtype == 42);
	REQUIRE(apl->common.rdclass == 1);

	if (apl->mctx == NULL)
		return;
	if (apl->apl != NULL)
		isc_mem_free(apl->mctx, apl->apl);
	apl->mctx = NULL;
}

isc_result_t
dns_rdata_apl_first(dns_rdata_in_apl_t *apl) {
	isc_uint32_t length;

	REQUIRE(apl != NULL);
	REQUIRE(apl->common.rdtype == 42);
	REQUIRE(apl->common.rdclass == 1);
	REQUIRE(apl->apl != NULL || apl->apl_len == 0);

	/*
	 * If no APL return ISC_R_NOMORE.
	 */
	if (apl->apl == NULL)
		return (ISC_R_NOMORE);

	/*
	 * Sanity check data.
	 */
	INSIST(apl->apl_len > 3U);
	length = apl->apl[apl->offset + 3] & 0x7f;
	INSIST(length <= apl->apl_len);

	apl->offset = 0;
	return (ISC_R_SUCCESS);
}

isc_result_t
dns_rdata_apl_next(dns_rdata_in_apl_t *apl) {
	isc_uint32_t length;

	REQUIRE(apl != NULL);
	REQUIRE(apl->common.rdtype == 42);
	REQUIRE(apl->common.rdclass == 1);
	REQUIRE(apl->apl != NULL || apl->apl_len == 0);

	/*
	 * No APL or have already reached the end return ISC_R_NOMORE.
	 */
	if (apl->apl == NULL || apl->offset == apl->apl_len)
		return (ISC_R_NOMORE);

	/*
	 * Sanity check data.
	 */
	INSIST(apl->offset < apl->apl_len);
	INSIST(apl->apl_len > 3U);
	INSIST(apl->offset <= apl->apl_len - 4U);
	length = apl->apl[apl->offset + 3] & 0x7f;
	/*
	 * 16 to 32 bits promotion as 'length' is 32 bits so there is
	 * no overflow problems.
	 */
	INSIST(length + apl->offset <= apl->apl_len);

	apl->offset += apl->apl[apl->offset + 3] & 0x7f;
	return ((apl->offset >= apl->apl_len) ? ISC_R_SUCCESS : ISC_R_NOMORE);
}

isc_result_t
dns_rdata_apl_current(dns_rdata_in_apl_t *apl, dns_rdata_apl_ent_t *ent) {
	isc_uint32_t length;

	REQUIRE(apl != NULL);
	REQUIRE(apl->common.rdtype == 42);
	REQUIRE(apl->common.rdclass == 1);
	REQUIRE(ent != NULL);
	REQUIRE(apl->apl != NULL || apl->apl_len == 0);
	REQUIRE(apl->offset <= apl->apl_len);

	if (apl->offset == apl->apl_len)
		return (ISC_R_NOMORE);

	/*
	 * Sanity check data.
	 */
	INSIST(apl->apl_len > 3U);
	INSIST(apl->offset <= apl->apl_len - 4U);
	length = apl->apl[apl->offset + 3] & 0x7f;
	/*
	 * 16 to 32 bits promotion as 'length' is 32 bits so there is
	 * no overflow problems.
	 */
	INSIST(length + apl->offset <= apl->apl_len);

	ent->family = (apl->apl[apl->offset] << 8) + apl->apl[apl->offset + 1];
	ent->prefix = apl->apl[apl->offset + 2];
	ent->length = apl->apl[apl->offset + 3] & 0x7f;
	ent->negative = ISC_TF((apl->apl[apl->offset + 3] & 0x80) != 0);
	if (ent->length != 0)
		ent->data = &apl->apl[apl->offset + 4];
	else
		ent->data = NULL;
	return (ISC_R_SUCCESS);
}

static inline isc_result_t
additionaldata_in_apl(ARGS_ADDLDATA) {
	REQUIRE(rdata->type == 42);
	REQUIRE(rdata->rdclass == 1);

	(void)add;
	(void)arg;

	return (ISC_R_SUCCESS);
}

static inline isc_result_t
digest_in_apl(ARGS_DIGEST) {
	isc_region_t r;

	REQUIRE(rdata->type == 42);
	REQUIRE(rdata->rdclass == 1);

	dns_rdata_toregion(rdata, &r);

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

static inline isc_boolean_t
checkowner_in_apl(ARGS_CHECKOWNER) {

	REQUIRE(type == 42);
	REQUIRE(rdclass == 1);

	UNUSED(name);
	UNUSED(type);
	UNUSED(rdclass);
	UNUSED(wildcard);

	return (ISC_TRUE);
}


static inline isc_boolean_t
checknames_in_apl(ARGS_CHECKNAMES) {

	REQUIRE(rdata->type == 42);
	REQUIRE(rdata->rdclass == 1);

	UNUSED(rdata);
	UNUSED(owner);
	UNUSED(bad);

	return (ISC_TRUE);
}

#endif	/* RDATA_IN_1_APL_42_C */