1/*	$NetBSD: soa.c,v 1.2.6.1 2012/06/05 21:15:01 bouyer Exp $	*/
2
3/*
4 * Copyright (C) 2004, 2005, 2007, 2009  Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001  Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19
20/* Id: soa.c,v 1.12 2009/09/10 02:18:40 each Exp  */
21
22/*! \file */
23
24#include <config.h>
25#include <string.h>
26
27#include <isc/buffer.h>
28#include <isc/util.h>
29
30#include <dns/rdata.h>
31#include <dns/rdatastruct.h>
32#include <dns/soa.h>
33
34static inline isc_uint32_t
35decode_uint32(unsigned char *p) {
36	return ((p[0] << 24) +
37		(p[1] << 16) +
38		(p[2] <<  8) +
39		(p[3] <<  0));
40}
41
42static inline void
43encode_uint32(isc_uint32_t val, unsigned char *p) {
44	p[0] = (isc_uint8_t)(val >> 24);
45	p[1] = (isc_uint8_t)(val >> 16);
46	p[2] = (isc_uint8_t)(val >>  8);
47	p[3] = (isc_uint8_t)(val >>  0);
48}
49
50static isc_uint32_t
51soa_get(dns_rdata_t *rdata, int offset) {
52	INSIST(rdata->type == dns_rdatatype_soa);
53	/*
54	 * Locate the field within the SOA RDATA based
55	 * on its position relative to the end of the data.
56	 *
57	 * This is a bit of a kludge, but the alternative approach of
58	 * using dns_rdata_tostruct() and dns_rdata_fromstruct() would
59	 * involve a lot of unnecessary work (like building domain
60	 * names and allocating temporary memory) when all we really
61	 * want to do is to get 32 bits of fixed-sized data.
62	 */
63	INSIST(rdata->length >= 20);
64	INSIST(offset >= 0 && offset <= 16);
65	return (decode_uint32(rdata->data + rdata->length - 20 + offset));
66}
67
68isc_result_t
69dns_soa_buildrdata(dns_name_t *origin, dns_name_t *contact,
70		   dns_rdataclass_t rdclass,
71		   isc_uint32_t serial, isc_uint32_t refresh,
72		   isc_uint32_t retry, isc_uint32_t expire,
73		   isc_uint32_t minimum, unsigned char *buffer,
74		   dns_rdata_t *rdata) {
75	dns_rdata_soa_t soa;
76	isc_buffer_t rdatabuf;
77
78	REQUIRE(origin != NULL);
79	REQUIRE(contact != NULL);
80
81	memset(buffer, 0, DNS_SOA_BUFFERSIZE);
82	isc_buffer_init(&rdatabuf, buffer, DNS_SOA_BUFFERSIZE);
83
84	soa.common.rdtype = dns_rdatatype_soa;
85	soa.common.rdclass = rdclass;
86	soa.mctx = NULL;
87	soa.serial = serial;
88	soa.refresh = refresh;
89	soa.retry = retry;
90	soa.expire = expire;
91	soa.minimum = minimum;
92	dns_name_init(&soa.origin, NULL);
93	dns_name_clone(origin, &soa.origin);
94	dns_name_init(&soa.contact, NULL);
95	dns_name_clone(contact, &soa.contact);
96
97	return (dns_rdata_fromstruct(rdata, rdclass, dns_rdatatype_soa,
98				      &soa, &rdatabuf));
99}
100
101isc_uint32_t
102dns_soa_getserial(dns_rdata_t *rdata) {
103	return soa_get(rdata, 0);
104}
105isc_uint32_t
106dns_soa_getrefresh(dns_rdata_t *rdata) {
107	return soa_get(rdata, 4);
108}
109isc_uint32_t
110dns_soa_getretry(dns_rdata_t *rdata) {
111	return soa_get(rdata, 8);
112}
113isc_uint32_t
114dns_soa_getexpire(dns_rdata_t *rdata) {
115	return soa_get(rdata, 12);
116}
117isc_uint32_t
118dns_soa_getminimum(dns_rdata_t *rdata) {
119	return soa_get(rdata, 16);
120}
121
122static void
123soa_set(dns_rdata_t *rdata, isc_uint32_t val, int offset) {
124	INSIST(rdata->type == dns_rdatatype_soa);
125	INSIST(rdata->length >= 20);
126	INSIST(offset >= 0 && offset <= 16);
127	encode_uint32(val, rdata->data + rdata->length - 20 + offset);
128}
129
130void
131dns_soa_setserial(isc_uint32_t val, dns_rdata_t *rdata) {
132	soa_set(rdata, val, 0);
133}
134void
135dns_soa_setrefresh(isc_uint32_t val, dns_rdata_t *rdata) {
136	soa_set(rdata, val, 4);
137}
138void
139dns_soa_setretry(isc_uint32_t val, dns_rdata_t *rdata) {
140	soa_set(rdata, val, 8);
141}
142void
143dns_soa_setexpire(isc_uint32_t val, dns_rdata_t *rdata) {
144	soa_set(rdata, val, 12);
145}
146void
147dns_soa_setminimum(isc_uint32_t val, dns_rdata_t *rdata) {
148	soa_set(rdata, val, 16);
149}
150