caa_257.c 8.41 KB
Newer Older
Evan Hunt's avatar
Evan Hunt committed
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
/*
 * Copyright (C) 2014  Internet Systems Consortium, Inc. ("ISC")
 *
 * 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.
 */

#ifndef GENERIC_CAA_257_C
#define GENERIC_CAA_257_C 1

#define RRTYPE_CAA_ATTRIBUTES (0)

static unsigned char const alphanumeric[256] = {
	/* 0x00-0x0f */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
	/* 0x10-0x1f */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
	/* 0x20-0x2f */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
	/* 0x30-0x3f */ 1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 0, 0, 0, 0, 0, 0,
	/* 0x40-0x4f */ 0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
	/* 0x50-0x5f */ 1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 0, 0, 0, 0, 0,
	/* 0x60-0x6f */ 0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
	/* 0x70-0x7f */ 1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 0, 0, 0, 0, 0,
	/* 0x80-0x8f */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
	/* 0x90-0x9f */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
	/* 0xa0-0xaf */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
	/* 0xb0-0xbf */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
	/* 0xc0-0xcf */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
	/* 0xd0-0xdf */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
	/* 0xe0-0xef */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
	/* 0xf0-0xff */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
};

static inline isc_result_t
fromtext_caa(ARGS_FROMTEXT) {
	isc_token_t token;
	isc_textregion_t tr;
	isc_uint8_t flags;
	unsigned int i;

	REQUIRE(type == 257);

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

	/* Flags. */
	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
				      ISC_FALSE));
	if (token.value.as_ulong > 255U)
		RETTOK(ISC_R_RANGE);
	flags = token.value.as_ulong;
	RETERR(uint8_tobuffer(flags, target));

	/*
	 * Tag
	 */
	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
				      ISC_FALSE));
	tr = token.value.as_textregion;
	for (i = 0; i < tr.length; i++)
		if (!alphanumeric[(unsigned int) tr.base[i]])
			RETTOK(DNS_R_SYNTAX);
	RETERR(uint8_tobuffer(tr.length, target));
	RETERR(mem_tobuffer(target, tr.base, tr.length));

	/*
	 * Value
	 */
	RETERR(isc_lex_getmastertoken(lexer, &token,
				      isc_tokentype_qstring, ISC_FALSE));
	if (token.type != isc_tokentype_qstring &&
	    token.type != isc_tokentype_string)
		RETERR(DNS_R_SYNTAX);
	RETERR(multitxt_fromtext(&token.value.as_textregion, target,
				 ISC_FALSE));
	return (ISC_R_SUCCESS);
}

static inline isc_result_t
totext_caa(ARGS_TOTEXT) {
	isc_region_t region;
	isc_uint8_t flags;
	char buf[256];

	UNUSED(tctx);

	REQUIRE(rdata->type == 257);
	REQUIRE(rdata->length >= 3U);
	REQUIRE(rdata->data != NULL);

	dns_rdata_toregion(rdata, &region);

	/*
	 * Flags
	 */
	flags = uint8_consume_fromregion(&region);
	sprintf(buf, "%u ", flags);
	RETERR(str_totext(buf, target));

	/*
	 * Tag
	 */
	RETERR(txt_totext(&region, ISC_FALSE, target));
	RETERR(str_totext(" ", target));

	/*
	 * Value
	 */
	RETERR(multitxt_totext(&region, target, ISC_FALSE));
	return (ISC_R_SUCCESS);
}

static inline isc_result_t
fromwire_caa(ARGS_FROMWIRE) {
	isc_region_t sr;
	unsigned int len, i;

	REQUIRE(type == 257);

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

	/*
	 * Flags
	 */
	isc_buffer_activeregion(source, &sr);
	if (sr.length < 2)
		return (ISC_R_UNEXPECTEDEND);

	/*
	 * Flags, tag length
	 */
	RETERR(mem_tobuffer(target, sr.base, 2));
	len = sr.base[1];
	isc_region_consume(&sr, 2);
	isc_buffer_forward(source, 2);

	/*
	 * Zero length tag fields are illegal.
	 */
	if (sr.length < len || len == 0)
		RETERR(DNS_R_FORMERR);

	/* Tag */
	for (i = 0; i < sr.length; i++)
		if (!alphanumeric[sr.base[i]])
			RETERR(DNS_R_FORMERR);
	RETERR(mem_tobuffer(target, sr.base, len));
	isc_region_consume(&sr, len);
	isc_buffer_forward(source, len);

	/*
	 * Value
	 */
	RETERR(multitxt_fromwire(source, target, ISC_FALSE));

	return (ISC_R_SUCCESS);
}

static inline isc_result_t
towire_caa(ARGS_TOWIRE) {
	isc_region_t region;

	REQUIRE(rdata->type == 257);
	REQUIRE(rdata->length >= 3U);
	REQUIRE(rdata->data != NULL);

	UNUSED(cctx);

	dns_rdata_toregion(rdata, &region);
	return (mem_tobuffer(target, region.base, region.length));
}

static inline int
compare_caa(ARGS_COMPARE) {
	isc_region_t r1, r2;

	REQUIRE(rdata1->type == rdata2->type);
	REQUIRE(rdata1->rdclass == rdata2->rdclass);
	REQUIRE(rdata1->type == 257);
	REQUIRE(rdata1->length >= 3U);
	REQUIRE(rdata2->length >= 3U);
	REQUIRE(rdata1->data != NULL);
	REQUIRE(rdata2->data != NULL);

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

static inline isc_result_t
fromstruct_caa(ARGS_FROMSTRUCT) {
	dns_rdata_caa_t *caa = source;
	isc_region_t region;
	unsigned int i;

	REQUIRE(type == 257);
	REQUIRE(source != NULL);
	REQUIRE(caa->common.rdtype == type);
	REQUIRE(caa->common.rdclass == rdclass);
	REQUIRE(caa->tag != NULL && caa->tag_len != 0);
	REQUIRE(caa->value != NULL);

	UNUSED(type);
	UNUSED(rdclass);

	/*
	 * Flags
	 */
	RETERR(uint8_tobuffer(caa->flags, target));

	/*
	 * Tag length
	 */
	RETERR(uint8_tobuffer(caa->tag_len, target));

	/*
	 * Tag
	 */
	region.base = caa->tag;
	region.length = caa->tag_len;
	for (i = 0; i < region.length; i++)
		if (!alphanumeric[region.base[i]])
			RETERR(DNS_R_SYNTAX);
	RETERR(isc_buffer_copyregion(target, &region));

	/*
	 * Value
	 */
	region.base = caa->value;
	region.length = caa->value_len;
	return (isc_buffer_copyregion(target, &region));
}

static inline isc_result_t
tostruct_caa(ARGS_TOSTRUCT) {
	dns_rdata_caa_t *caa = target;
	isc_region_t sr;

	REQUIRE(rdata->type == 257);
	REQUIRE(target != NULL);
	REQUIRE(rdata->length >= 3U);
	REQUIRE(rdata->data != NULL);

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

	dns_rdata_toregion(rdata, &sr);

	/*
	 * Flags
	 */
	if (sr.length < 1)
		return (ISC_R_UNEXPECTEDEND);
	caa->flags = uint8_fromregion(&sr);
	isc_region_consume(&sr, 1);

	/*
	 * Tag length
	 */
	if (sr.length < 1)
		return (ISC_R_UNEXPECTEDEND);
	caa->tag_len = uint8_fromregion(&sr);
	isc_region_consume(&sr, 1);

	/*
	 * Tag
	 */
	if (sr.length < caa->tag_len)
		return (ISC_R_UNEXPECTEDEND);
	caa->tag = mem_maybedup(mctx, sr.base, caa->tag_len);
	if (caa->tag == NULL)
		return (ISC_R_NOMEMORY);
	isc_region_consume(&sr, caa->tag_len);

	/*
	 * Value
	 */
	caa->value_len = sr.length;
	caa->value = mem_maybedup(mctx, sr.base, sr.length);
	if (caa->value == NULL)
		return (ISC_R_NOMEMORY);

	caa->mctx = mctx;
	return (ISC_R_SUCCESS);
}

static inline void
freestruct_caa(ARGS_FREESTRUCT) {
	dns_rdata_caa_t *caa = (dns_rdata_caa_t *) source;

	REQUIRE(source != NULL);
	REQUIRE(caa->common.rdtype == 257);

	if (caa->mctx == NULL)
		return;

	if (caa->tag != NULL)
		isc_mem_free(caa->mctx, caa->tag);
	if (caa->value != NULL)
		isc_mem_free(caa->mctx, caa->value);
	caa->mctx = NULL;
}

static inline isc_result_t
additionaldata_caa(ARGS_ADDLDATA) {
	REQUIRE(rdata->type == 257);
	REQUIRE(rdata->data != NULL);
	REQUIRE(rdata->length >= 3U);

	UNUSED(rdata);
	UNUSED(add);
	UNUSED(arg);

	return (ISC_R_SUCCESS);
}

static inline isc_result_t
digest_caa(ARGS_DIGEST) {
	isc_region_t r;

	REQUIRE(rdata->type == 257);
	REQUIRE(rdata->data != NULL);
	REQUIRE(rdata->length >= 3U);

	dns_rdata_toregion(rdata, &r);

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

static inline isc_boolean_t
checkowner_caa(ARGS_CHECKOWNER) {

	REQUIRE(type == 257);

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

	return (ISC_TRUE);
}

static inline isc_boolean_t
checknames_caa(ARGS_CHECKNAMES) {

	REQUIRE(rdata->type == 257);
	REQUIRE(rdata->data != NULL);
	REQUIRE(rdata->length >= 3U);

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

	return (ISC_TRUE);
}

static inline int
casecompare_caa(ARGS_COMPARE) {
	return (compare_caa(rdata1, rdata2));
}

#endif /* GENERIC_CAA_257_C */