confrrset.c 5.75 KB
Newer Older
1
/*
Bob Halley's avatar
Bob Halley committed
2
 * Copyright (C) 1999, 2000  Internet Software Consortium.
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.
 */

David Lawrence's avatar
David Lawrence committed
18
/* $Id: confrrset.c,v 1.14 2000/06/01 18:25:46 tale Exp $ */
James Brister's avatar
James Brister committed
19

20 21
#include <config.h>

22
#include <isc/mem.h>
Bob Halley's avatar
Bob Halley committed
23
#include <isc/util.h>
24 25 26 27

#include <dns/confrrset.h>

isc_result_t
28
dns_c_rrsolist_clear(dns_c_rrsolist_t *olist) {
29 30
	dns_c_rrso_t *elem;
	
31
	REQUIRE(DNS_C_RRSOLIST_VALID(olist));
32 33 34 35

	elem = ISC_LIST_HEAD(olist->elements);
	while (elem != NULL) {
		ISC_LIST_UNLINK(olist->elements, elem, next);
36
		dns_c_rrso_delete(&elem);
37 38 39 40 41 42 43
		elem = ISC_LIST_HEAD(olist->elements);
	}

	return (ISC_R_SUCCESS);
}

isc_result_t
44
dns_c_rrsolist_append(dns_c_rrsolist_t *dest, dns_c_rrsolist_t *src) {
45 46 47 48
	dns_c_rrso_t *oldelem;
	dns_c_rrso_t *newelem;
	isc_result_t res;

49 50
	REQUIRE(DNS_C_RRSOLIST_VALID(dest));
	REQUIRE(DNS_C_RRSOLIST_VALID(src));
51 52 53

	oldelem = ISC_LIST_HEAD(src->elements);
	while (oldelem != NULL) {
54
		res = dns_c_rrso_copy(dest->mem, &newelem, oldelem);
55 56 57 58 59 60 61 62 63 64 65 66
		if (res != ISC_R_SUCCESS) {
			return (res);
		}

		ISC_LIST_APPEND(dest->elements, newelem, next);
		oldelem = ISC_LIST_NEXT(oldelem, next);
	}

	return (ISC_R_SUCCESS);
}

isc_result_t
67
dns_c_rrsolist_new(isc_mem_t *mem, dns_c_rrsolist_t **rval) {
68
	dns_c_rrsolist_t *ro;
69 70 71 72 73 74 75 76

	ro = isc_mem_get(mem, sizeof *ro);
	if (ro == NULL) {
		return (ISC_R_NOMEMORY);
	}
	
	ISC_LIST_INIT(ro->elements);
	ro->mem = mem;
77
	ro->magic = DNS_C_RRSOLIST_MAGIC;
78 79 80 81 82 83 84

	*rval = ro;
	
	return (ISC_R_SUCCESS);
}

isc_result_t
David Lawrence's avatar
David Lawrence committed
85 86 87
dns_c_rrso_new(isc_mem_t *mem, dns_c_rrso_t **res, dns_rdataclass_t oclass,
	       dns_rdatatype_t otype, const char *name,
	       dns_c_ordering_t ordering)
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
{
	dns_c_rrso_t *newo;

	REQUIRE(mem != NULL);
	REQUIRE(res != NULL);
	
	if (name == NULL) {
		name = "*";
	}
	
	newo = isc_mem_get(mem, sizeof *newo);
	if (newo == NULL) {
		return (ISC_R_NOMEMORY);
	}

103
	newo->magic = DNS_C_RRSO_MAGIC;
104 105 106 107 108 109 110 111
	newo->mem = mem;
	newo->otype = otype;
	newo->oclass = oclass;
	newo->ordering = ordering;
	ISC_LINK_INIT(newo, next);

	newo->name = isc_mem_strdup(mem, name);
	if (newo->name == NULL) {
112
		newo->magic = 0;
113 114 115 116 117 118 119 120 121 122
		isc_mem_put(mem, newo, sizeof *newo);
		return (ISC_R_NOMEMORY);
	}

	*res = newo;

	return (ISC_R_SUCCESS);
}

isc_result_t
123
dns_c_rrsolist_delete(dns_c_rrsolist_t **list) {
124
	dns_c_rrso_t *elem, *q;
125
	dns_c_rrsolist_t *l;
126 127 128
	isc_result_t r;

	REQUIRE(list != NULL);
129
	REQUIRE(DNS_C_RRSOLIST_VALID(*list));
130 131 132 133 134 135 136

	l = *list;

	elem = ISC_LIST_HEAD(l->elements);
	while (elem != NULL) {
		q = ISC_LIST_NEXT(elem, next);
		ISC_LIST_UNLINK(l->elements, elem, next);
137
		r = dns_c_rrso_delete(&elem);
138 139 140 141 142 143 144
		if (r != ISC_R_SUCCESS) {
			return (r);
		}
		
		elem = q;
	}

145
	l->magic = 0;
146 147 148 149 150 151 152 153
	isc_mem_put(l->mem, l, sizeof *l);

	*list = NULL;

	return (ISC_R_SUCCESS);
}

isc_result_t
154
dns_c_rrso_delete(dns_c_rrso_t **order) {
155
	dns_c_rrso_t *oldo;
James Brister's avatar
James Brister committed
156

157
	REQUIRE(order != NULL);
158
	REQUIRE(DNS_C_RRSO_VALID(*order));
159 160 161 162 163 164

	oldo = *order;

	REQUIRE(oldo->name != NULL);
	isc_mem_free(oldo->mem, oldo->name);

165
	oldo->magic = 0;
166 167 168 169 170 171 172 173
	isc_mem_put(oldo->mem, oldo, sizeof *oldo);

	*order = NULL;
	
	return (ISC_R_SUCCESS);
}

isc_result_t
174
dns_c_rrso_copy(isc_mem_t *mem, dns_c_rrso_t **dest, dns_c_rrso_t *source) {
175 176 177
	dns_c_rrso_t *newo;
	isc_result_t res;

178 179
	REQUIRE(DNS_C_RRSO_VALID(*dest));
	REQUIRE(DNS_C_RRSO_VALID(source));
180

181
	res = dns_c_rrso_new(mem, &newo, source->oclass,
182 183 184 185 186 187 188 189 190 191 192 193
			     source->otype, source->name,
			     source->ordering);
	if (res == ISC_R_SUCCESS) {
		*dest = newo;
	} else {
		*dest = NULL;
	}
	
	return (res);
}

isc_result_t
194
dns_c_rrsolist_copy(isc_mem_t *mem, dns_c_rrsolist_t **dest,
James Brister's avatar
James Brister committed
195
		    dns_c_rrsolist_t *source)
196 197

{
198
	dns_c_rrsolist_t *nlist;
199 200
	dns_c_rrso_t *elem;
	dns_c_rrso_t *newe;
201
	isc_result_t res;
202 203 204

	REQUIRE(DNS_C_RRSOLIST_VALID(source));
	REQUIRE(dest != NULL);
205
	
206
	res = dns_c_rrsolist_new(mem, &nlist);
207
	if (res != ISC_R_SUCCESS) {
208 209 210 211 212
		return (res);
	}
	
	elem = ISC_LIST_HEAD(source->elements);
	while (elem != NULL) {
213
		res = dns_c_rrso_copy(mem, &newe, elem);
214
		if (res != ISC_R_SUCCESS) {
215
			dns_c_rrsolist_delete(&nlist);
216 217 218 219 220 221 222 223 224 225 226 227 228 229
			return (res);
		}

		ISC_LIST_APPEND(nlist->elements, newe, next);
		
		elem = ISC_LIST_NEXT(elem, next);
	}

	*dest = nlist;

	return (ISC_R_SUCCESS);
}

void
230
dns_c_rrsolist_print(FILE *fp, int indent, dns_c_rrsolist_t *rrlist) {
231 232
	dns_c_rrso_t *or;

233
	REQUIRE(DNS_C_RRSOLIST_VALID(rrlist));
234 235 236 237 238
	
	if (ISC_LIST_EMPTY(rrlist->elements)) {
		return;
	}
	
239
	dns_c_printtabs(fp, indent);
240 241 242 243
	fprintf(fp, "rrset-order {\n");

	or = ISC_LIST_HEAD(rrlist->elements);
	while (or != NULL) {
244
		dns_c_rrso_print(fp, indent + 1, or);
245 246 247
		or = ISC_LIST_NEXT(or, next);
	}

248
	dns_c_printtabs(fp, indent);
249 250 251 252 253
	fprintf(fp, "};\n");
	
}

void
254
dns_c_rrso_print(FILE *fp, int indent, dns_c_rrso_t *order) {
255 256
	REQUIRE(DNS_C_RRSO_VALID(order));
	
257
	dns_c_printtabs(fp, indent);
258
	
259 260 261 262
	fputs("class ", fp);
	if (order->oclass == dns_rdataclass_any) {
		fputc('*', fp);
	} else {
263
		dns_c_dataclass_tostream(fp, order->oclass);
264 265 266 267 268 269 270
	}
	

	fputs(" type ", fp);
	if (order->otype == dns_rdatatype_any) {
		fputc('*', fp);
	} else {
271
		dns_c_datatype_tostream(fp, order->otype);
272 273
	}

274
	fprintf(fp, " name \"%s\"", order->name);
275 276

	fprintf(fp, " order %s",
277
		dns_c_ordering2string(order->ordering, ISC_TRUE));
278 279 280
	
	fputs(";\n", fp);
}