]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/bind9/lib/dns/soa.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / bind9 / lib / dns / soa.c
1 /*
2  * Copyright (C) 2004, 2005, 2007, 2009  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2000, 2001  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id: soa.c,v 1.12 2009/09/10 02:18:40 each Exp $ */
19
20 /*! \file */
21
22 #include <config.h>
23 #include <string.h>
24
25 #include <isc/buffer.h>
26 #include <isc/util.h>
27
28 #include <dns/rdata.h>
29 #include <dns/rdatastruct.h>
30 #include <dns/soa.h>
31
32 static inline isc_uint32_t
33 decode_uint32(unsigned char *p) {
34         return ((p[0] << 24) +
35                 (p[1] << 16) +
36                 (p[2] <<  8) +
37                 (p[3] <<  0));
38 }
39
40 static inline void
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);
46 }
47
48 static isc_uint32_t
49 soa_get(dns_rdata_t *rdata, int offset) {
50         INSIST(rdata->type == dns_rdatatype_soa);
51         /*
52          * Locate the field within the SOA RDATA based
53          * on its position relative to the end of the data.
54          *
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.
60          */
61         INSIST(rdata->length >= 20);
62         INSIST(offset >= 0 && offset <= 16);
63         return (decode_uint32(rdata->data + rdata->length - 20 + offset));
64 }
65
66 isc_result_t
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,
72                    dns_rdata_t *rdata) {
73         dns_rdata_soa_t soa;
74         isc_buffer_t rdatabuf;
75
76         REQUIRE(origin != NULL);
77         REQUIRE(contact != NULL);
78
79         memset(buffer, 0, DNS_SOA_BUFFERSIZE);
80         isc_buffer_init(&rdatabuf, buffer, DNS_SOA_BUFFERSIZE);
81
82         soa.common.rdtype = dns_rdatatype_soa;
83         soa.common.rdclass = rdclass;
84         soa.mctx = NULL;
85         soa.serial = serial;
86         soa.refresh = refresh;
87         soa.retry = retry;
88         soa.expire = expire;
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);
94
95         return (dns_rdata_fromstruct(rdata, rdclass, dns_rdatatype_soa,
96                                       &soa, &rdatabuf));
97 }
98
99 isc_uint32_t
100 dns_soa_getserial(dns_rdata_t *rdata) {
101         return soa_get(rdata, 0);
102 }
103 isc_uint32_t
104 dns_soa_getrefresh(dns_rdata_t *rdata) {
105         return soa_get(rdata, 4);
106 }
107 isc_uint32_t
108 dns_soa_getretry(dns_rdata_t *rdata) {
109         return soa_get(rdata, 8);
110 }
111 isc_uint32_t
112 dns_soa_getexpire(dns_rdata_t *rdata) {
113         return soa_get(rdata, 12);
114 }
115 isc_uint32_t
116 dns_soa_getminimum(dns_rdata_t *rdata) {
117         return soa_get(rdata, 16);
118 }
119
120 static void
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);
126 }
127
128 void
129 dns_soa_setserial(isc_uint32_t val, dns_rdata_t *rdata) {
130         soa_set(rdata, val, 0);
131 }
132 void
133 dns_soa_setrefresh(isc_uint32_t val, dns_rdata_t *rdata) {
134         soa_set(rdata, val, 4);
135 }
136 void
137 dns_soa_setretry(isc_uint32_t val, dns_rdata_t *rdata) {
138         soa_set(rdata, val, 8);
139 }
140 void
141 dns_soa_setexpire(isc_uint32_t val, dns_rdata_t *rdata) {
142         soa_set(rdata, val, 12);
143 }
144 void
145 dns_soa_setminimum(isc_uint32_t val, dns_rdata_t *rdata) {
146         soa_set(rdata, val, 16);
147 }