bind - Upgraded vendor branch to 9.5.2-P1
[dragonfly.git] / contrib / bind-9.5.2 / lib / dns / soa.c
1 /*
2  * Copyright (C) 2004, 2005, 2007  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.8 2007/06/19 23:47:16 tbox Exp $ */
19
20 /*! \file */
21
22 #include <config.h>
23
24 #include <isc/util.h>
25
26 #include <dns/rdata.h>
27 #include <dns/soa.h>
28
29 static inline isc_uint32_t
30 decode_uint32(unsigned char *p) {
31         return ((p[0] << 24) +
32                 (p[1] << 16) +
33                 (p[2] <<  8) +
34                 (p[3] <<  0));
35 }
36
37 static inline void
38 encode_uint32(isc_uint32_t val, unsigned char *p) {
39         p[0] = (isc_uint8_t)(val >> 24);
40         p[1] = (isc_uint8_t)(val >> 16);
41         p[2] = (isc_uint8_t)(val >>  8);
42         p[3] = (isc_uint8_t)(val >>  0);
43 }
44
45 static isc_uint32_t
46 soa_get(dns_rdata_t *rdata, int offset) {
47         INSIST(rdata->type == dns_rdatatype_soa);
48         /*
49          * Locate the field within the SOA RDATA based
50          * on its position relative to the end of the data.
51          *
52          * This is a bit of a kludge, but the alternative approach of
53          * using dns_rdata_tostruct() and dns_rdata_fromstruct() would
54          * involve a lot of unnecessary work (like building domain
55          * names and allocating temporary memory) when all we really
56          * want to do is to get 32 bits of fixed-sized data.
57          */
58         INSIST(rdata->length >= 20);
59         INSIST(offset >= 0 && offset <= 16);
60         return (decode_uint32(rdata->data + rdata->length - 20 + offset));
61 }
62
63 isc_uint32_t
64 dns_soa_getserial(dns_rdata_t *rdata) {
65         return soa_get(rdata, 0);
66 }
67 isc_uint32_t
68 dns_soa_getrefresh(dns_rdata_t *rdata) {
69         return soa_get(rdata, 4);
70 }
71 isc_uint32_t
72 dns_soa_getretry(dns_rdata_t *rdata) {
73         return soa_get(rdata, 8);
74 }
75 isc_uint32_t
76 dns_soa_getexpire(dns_rdata_t *rdata) {
77         return soa_get(rdata, 12);
78 }
79 isc_uint32_t
80 dns_soa_getminimum(dns_rdata_t *rdata) {
81         return soa_get(rdata, 16);
82 }
83
84 static void
85 soa_set(dns_rdata_t *rdata, isc_uint32_t val, int offset) {
86         INSIST(rdata->type == dns_rdatatype_soa);
87         INSIST(rdata->length >= 20);
88         INSIST(offset >= 0 && offset <= 16);
89         encode_uint32(val, rdata->data + rdata->length - 20 + offset);
90 }
91
92 void
93 dns_soa_setserial(isc_uint32_t val, dns_rdata_t *rdata) {
94         soa_set(rdata, val, 0);
95 }
96 void
97 dns_soa_setrefresh(isc_uint32_t val, dns_rdata_t *rdata) {
98         soa_set(rdata, val, 4);
99 }
100 void
101 dns_soa_setretry(isc_uint32_t val, dns_rdata_t *rdata) {
102         soa_set(rdata, val, 8);
103 }
104 void
105 dns_soa_setexpire(isc_uint32_t val, dns_rdata_t *rdata) {
106         soa_set(rdata, val, 12);
107 }
108 void
109 dns_soa_setminimum(isc_uint32_t val, dns_rdata_t *rdata) {
110         soa_set(rdata, val, 16);
111 }