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

18
/* $Id: kx_36.c,v 1.24 2000/05/05 23:20:02 marka Exp $ */
Mark Andrews's avatar
Mark Andrews committed
19

20 21 22
/* Reviewed: Thu Mar 16 17:24:54 PST 2000 by explorer */

/* RFC 2230 */
Mark Andrews's avatar
Mark Andrews committed
23

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

27 28
#define RRTYPE_KX_ATTRIBUTES (0)

29
static inline isc_result_t
30
fromtext_in_kx(dns_rdataclass_t rdclass, dns_rdatatype_t type,
Mark Andrews's avatar
Mark Andrews committed
31 32 33
	       isc_lex_t *lexer, dns_name_t *origin,
	       isc_boolean_t downcase, isc_buffer_t *target)
{
Mark Andrews's avatar
Mark Andrews committed
34 35 36 37 38
	isc_token_t token;
	dns_name_t name;
	isc_buffer_t buffer;

	REQUIRE(type == 36);
39
	REQUIRE(rdclass == 1);
Mark Andrews's avatar
Mark Andrews committed
40 41 42 43 44 45

	RETERR(gettoken(lexer, &token, isc_tokentype_number, ISC_FALSE));
	RETERR(uint16_tobuffer(token.value.as_ulong, target));

	RETERR(gettoken(lexer, &token, isc_tokentype_string, ISC_FALSE));
	dns_name_init(&name, NULL);
46
	buffer_fromregion(&buffer, &token.value.as_region);
Mark Andrews's avatar
Mark Andrews committed
47 48 49 50
	origin = (origin != NULL) ? origin : dns_rootname;
	return (dns_name_fromtext(&name, &buffer, origin, downcase, target));
}

51
static inline isc_result_t
52 53 54
totext_in_kx(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx, 
	     isc_buffer_t *target) 
{
Mark Andrews's avatar
Mark Andrews committed
55 56 57 58 59 60 61 62
	isc_region_t region;
	dns_name_t name;
	dns_name_t prefix;
	isc_boolean_t sub;
	char buf[sizeof "64000"];
	unsigned short num;

	REQUIRE(rdata->type == 36);
63
	REQUIRE(rdata->rdclass == 1);
Mark Andrews's avatar
Mark Andrews committed
64 65 66 67 68 69 70 71 72

	dns_name_init(&name, NULL);
	dns_name_init(&prefix, NULL);

	dns_rdata_toregion(rdata, &region);
	num = uint16_fromregion(&region);
	isc_region_consume(&region, 2);
	sprintf(buf, "%u", num);
	RETERR(str_totext(buf, target));
73

Mark Andrews's avatar
Mark Andrews committed
74 75 76
	RETERR(str_totext(" ", target));

	dns_name_fromregion(&name, &region);
77
	sub = name_prefix(&name, tctx->origin, &prefix);
78
	return (dns_name_totext(&prefix, sub, target));
Mark Andrews's avatar
Mark Andrews committed
79 80
}

81
static inline isc_result_t
82
fromwire_in_kx(dns_rdataclass_t rdclass, dns_rdatatype_t type,
Mark Andrews's avatar
Mark Andrews committed
83 84 85
	       isc_buffer_t *source, dns_decompress_t *dctx,
	       isc_boolean_t downcase, isc_buffer_t *target)
{
Mark Andrews's avatar
Mark Andrews committed
86 87 88 89
        dns_name_t name;
	isc_region_t sregion;

	REQUIRE(type == 36);
90
	REQUIRE(rdclass == 1);
Mark Andrews's avatar
Mark Andrews committed
91

92
	dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
Mark Andrews's avatar
Mark Andrews committed
93 94 95
        
        dns_name_init(&name, NULL);

96
	isc_buffer_activeregion(source, &sregion);
Mark Andrews's avatar
Mark Andrews committed
97
	if (sregion.length < 2)
98
		return (ISC_R_UNEXPECTEDEND);
Mark Andrews's avatar
Mark Andrews committed
99 100 101 102 103
	RETERR(mem_tobuffer(target, sregion.base, 2));
	isc_buffer_forward(source, 2);
	return (dns_name_fromwire(&name, source, dctx, downcase, target));
}

104
static inline isc_result_t
105 106
towire_in_kx(dns_rdata_t *rdata, dns_compress_t *cctx, isc_buffer_t *target)
{
Mark Andrews's avatar
Mark Andrews committed
107 108 109 110
	dns_name_t name;
	isc_region_t region;

	REQUIRE(rdata->type == 36);
111
	REQUIRE(rdata->rdclass == 1);
Mark Andrews's avatar
Mark Andrews committed
112

113
	dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
Mark Andrews's avatar
Mark Andrews committed
114 115 116 117 118 119 120 121 122 123
	dns_rdata_toregion(rdata, &region);
	RETERR(mem_tobuffer(target, region.base, 2));
	isc_region_consume(&region, 2);

	dns_name_init(&name, NULL);
	dns_name_fromregion(&name, &region);

	return (dns_name_towire(&name, cctx, target));
}

124
static inline int
125 126
compare_in_kx(dns_rdata_t *rdata1, dns_rdata_t *rdata2)
{
Mark Andrews's avatar
Mark Andrews committed
127 128 129 130
	dns_name_t name1;
	dns_name_t name2;
	isc_region_t region1;
	isc_region_t region2;
131
	int order;
Mark Andrews's avatar
Mark Andrews committed
132 133

	REQUIRE(rdata1->type == rdata2->type);
134
	REQUIRE(rdata1->rdclass == rdata2->rdclass);
Mark Andrews's avatar
Mark Andrews committed
135
	REQUIRE(rdata1->type == 36);
136
	REQUIRE(rdata1->rdclass == 1);
Mark Andrews's avatar
Mark Andrews committed
137

138 139 140
	order = memcmp(rdata1->data, rdata2->data, 2);
	if (order != 0)
		return (order < 0 ? -1 : 1);
Mark Andrews's avatar
Mark Andrews committed
141 142 143 144 145 146 147 148 149 150 151 152 153

	dns_name_init(&name1, NULL);
	dns_name_init(&name2, NULL);

	dns_rdata_toregion(rdata1, &region1);
	dns_rdata_toregion(rdata2, &region2);

	isc_region_consume(&region1, 2);
	isc_region_consume(&region2, 2);

	dns_name_fromregion(&name1, &region1);
	dns_name_fromregion(&name2, &region2);

154
	return (dns_name_rdatacompare(&name1, &name2));
Mark Andrews's avatar
Mark Andrews committed
155 156
}

157
static inline isc_result_t
158
fromstruct_in_kx(dns_rdataclass_t rdclass, dns_rdatatype_t type, void *source,
Mark Andrews's avatar
Mark Andrews committed
159 160
		 isc_buffer_t *target)
{
161 162
	dns_rdata_in_kx_t *kx = source;
	isc_region_t region;
Mark Andrews's avatar
Mark Andrews committed
163 164

	REQUIRE(type == 36);
165
	REQUIRE(rdclass == 1);
166 167 168
	REQUIRE(source != NULL);
	REQUIRE(kx->common.rdtype == type);
	REQUIRE(kx->common.rdclass == rdclass);
Mark Andrews's avatar
Mark Andrews committed
169

170 171 172
	RETERR(uint16_tobuffer(kx->preference, target));
	dns_name_toregion(&kx->exchange, &region);
	return (isc_buffer_copyregion(target, &region));
Mark Andrews's avatar
Mark Andrews committed
173 174
}

175
static inline isc_result_t
176 177
tostruct_in_kx(dns_rdata_t *rdata, void *target, isc_mem_t *mctx)
{
178 179 180
	isc_region_t region;
	dns_rdata_in_kx_t *kx = target;
	dns_name_t name;
Mark Andrews's avatar
Mark Andrews committed
181 182

	REQUIRE(rdata->type == 36);
183
	REQUIRE(rdata->rdclass == 1);
184 185 186 187 188
	REQUIRE(target != NULL);

	kx->common.rdclass = rdata->rdclass;
	kx->common.rdtype = rdata->type;
	ISC_LINK_INIT(&kx->common, link);
Mark Andrews's avatar
Mark Andrews committed
189

190 191 192 193 194
	dns_name_init(&name, NULL);
	dns_rdata_toregion(rdata, &region);

	kx->preference = uint16_fromregion(&region);
	isc_region_consume(&region, 2);
Mark Andrews's avatar
Mark Andrews committed
195

196 197
	dns_name_fromregion(&name, &region);
	dns_name_init(&kx->exchange, NULL);
198 199 200
	RETERR(name_duporclone(&name, mctx, &kx->exchange));
	kx->mctx = mctx;
	return (ISC_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
201
}
202

203
static inline void
204 205
freestruct_in_kx(void *source)
{
206 207
	dns_rdata_in_kx_t *kx = source;

208
	REQUIRE(source != NULL);
209 210 211 212 213
	REQUIRE(kx->common.rdclass == 1);
	REQUIRE(kx->common.rdtype == 36);

	if (kx->mctx == NULL)
		return;
214

215 216
	dns_name_free(&kx->exchange, kx->mctx);
	kx->mctx = NULL;
217
}
218

219
static inline isc_result_t
220 221 222
additionaldata_in_kx(dns_rdata_t *rdata, dns_additionaldatafunc_t add,
		     void *arg)
{
Bob Halley's avatar
Bob Halley committed
223 224 225
	dns_name_t name;
	isc_region_t region;

226 227 228
	REQUIRE(rdata->type == 36);
	REQUIRE(rdata->rdclass == 1);

Bob Halley's avatar
Bob Halley committed
229 230 231 232
	dns_name_init(&name, NULL);
	dns_rdata_toregion(rdata, &region);
	isc_region_consume(&region, 2);
	dns_name_fromregion(&name, &region);
233

Bob Halley's avatar
Bob Halley committed
234
	return ((add)(arg, &name, dns_rdatatype_a));
235 236
}

237
static inline isc_result_t
238 239
digest_in_kx(dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg)
{
240 241 242 243 244 245 246 247 248 249
	isc_region_t r1, r2;
	dns_name_t name;

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

	dns_rdata_toregion(rdata, &r1);
	r2 = r1;
	isc_region_consume(&r2, 2);
	r1.length = 2;
Michael Graff's avatar
Michael Graff committed
250
	RETERR((digest)(arg, &r1));
251 252 253 254 255
	dns_name_init(&name, NULL);
	dns_name_fromregion(&name, &r2);
	return (dns_name_digest(&name, digest, arg));
}

256
#endif	/* RDATA_GENERIC_KX_15_C */