2 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: soa.c,v 1.12 2009/09/10 02:18:40 each Exp $ */
25 #include <isc/buffer.h>
28 #include <dns/rdata.h>
29 #include <dns/rdatastruct.h>
32 static inline isc_uint32_t
33 decode_uint32(unsigned char *p) {
34 return ((p[0] << 24) +
41 encode_uint32(isc_uint32_t val, unsigned char *p) {
42 p[0] = (isc_uint8_t)(val >> 24);
43 p[1] = (isc_uint8_t)(val >> 16);
44 p[2] = (isc_uint8_t)(val >> 8);
45 p[3] = (isc_uint8_t)(val >> 0);
49 soa_get(dns_rdata_t *rdata, int offset) {
50 INSIST(rdata->type == dns_rdatatype_soa);
52 * Locate the field within the SOA RDATA based
53 * on its position relative to the end of the data.
55 * This is a bit of a kludge, but the alternative approach of
56 * using dns_rdata_tostruct() and dns_rdata_fromstruct() would
57 * involve a lot of unnecessary work (like building domain
58 * names and allocating temporary memory) when all we really
59 * want to do is to get 32 bits of fixed-sized data.
61 INSIST(rdata->length >= 20);
62 INSIST(offset >= 0 && offset <= 16);
63 return (decode_uint32(rdata->data + rdata->length - 20 + offset));
67 dns_soa_buildrdata(dns_name_t *origin, dns_name_t *contact,
68 dns_rdataclass_t rdclass,
69 isc_uint32_t serial, isc_uint32_t refresh,
70 isc_uint32_t retry, isc_uint32_t expire,
71 isc_uint32_t minimum, unsigned char *buffer,
74 isc_buffer_t rdatabuf;
76 REQUIRE(origin != NULL);
77 REQUIRE(contact != NULL);
79 memset(buffer, 0, DNS_SOA_BUFFERSIZE);
80 isc_buffer_init(&rdatabuf, buffer, DNS_SOA_BUFFERSIZE);
82 soa.common.rdtype = dns_rdatatype_soa;
83 soa.common.rdclass = rdclass;
86 soa.refresh = refresh;
89 soa.minimum = minimum;
90 dns_name_init(&soa.origin, NULL);
91 dns_name_clone(origin, &soa.origin);
92 dns_name_init(&soa.contact, NULL);
93 dns_name_clone(contact, &soa.contact);
95 return (dns_rdata_fromstruct(rdata, rdclass, dns_rdatatype_soa,
100 dns_soa_getserial(dns_rdata_t *rdata) {
101 return soa_get(rdata, 0);
104 dns_soa_getrefresh(dns_rdata_t *rdata) {
105 return soa_get(rdata, 4);
108 dns_soa_getretry(dns_rdata_t *rdata) {
109 return soa_get(rdata, 8);
112 dns_soa_getexpire(dns_rdata_t *rdata) {
113 return soa_get(rdata, 12);
116 dns_soa_getminimum(dns_rdata_t *rdata) {
117 return soa_get(rdata, 16);
121 soa_set(dns_rdata_t *rdata, isc_uint32_t val, int offset) {
122 INSIST(rdata->type == dns_rdatatype_soa);
123 INSIST(rdata->length >= 20);
124 INSIST(offset >= 0 && offset <= 16);
125 encode_uint32(val, rdata->data + rdata->length - 20 + offset);
129 dns_soa_setserial(isc_uint32_t val, dns_rdata_t *rdata) {
130 soa_set(rdata, val, 0);
133 dns_soa_setrefresh(isc_uint32_t val, dns_rdata_t *rdata) {
134 soa_set(rdata, val, 4);
137 dns_soa_setretry(isc_uint32_t val, dns_rdata_t *rdata) {
138 soa_set(rdata, val, 8);
141 dns_soa_setexpire(isc_uint32_t val, dns_rdata_t *rdata) {
142 soa_set(rdata, val, 12);
145 dns_soa_setminimum(isc_uint32_t val, dns_rdata_t *rdata) {
146 soa_set(rdata, val, 16);