soa.c revision 1.2 1 /* $NetBSD: soa.c,v 1.2 2018/08/12 13:02:35 christos Exp $ */
2
3 /*
4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14
15 /*! \file */
16
17 #include <config.h>
18 #include <string.h>
19
20 #include <isc/buffer.h>
21 #include <isc/util.h>
22
23 #include <dns/rdata.h>
24 #include <dns/rdatastruct.h>
25 #include <dns/soa.h>
26
27 static inline isc_uint32_t
28 decode_uint32(unsigned char *p) {
29 return ((p[0] << 24) +
30 (p[1] << 16) +
31 (p[2] << 8) +
32 (p[3] << 0));
33 }
34
35 static inline void
36 encode_uint32(isc_uint32_t val, unsigned char *p) {
37 p[0] = (isc_uint8_t)(val >> 24);
38 p[1] = (isc_uint8_t)(val >> 16);
39 p[2] = (isc_uint8_t)(val >> 8);
40 p[3] = (isc_uint8_t)(val >> 0);
41 }
42
43 static isc_uint32_t
44 soa_get(dns_rdata_t *rdata, int offset) {
45 INSIST(rdata->type == dns_rdatatype_soa);
46 /*
47 * Locate the field within the SOA RDATA based
48 * on its position relative to the end of the data.
49 *
50 * This is a bit of a kludge, but the alternative approach of
51 * using dns_rdata_tostruct() and dns_rdata_fromstruct() would
52 * involve a lot of unnecessary work (like building domain
53 * names and allocating temporary memory) when all we really
54 * want to do is to get 32 bits of fixed-sized data.
55 */
56 INSIST(rdata->length >= 20);
57 INSIST(offset >= 0 && offset <= 16);
58 return (decode_uint32(rdata->data + rdata->length - 20 + offset));
59 }
60
61 isc_result_t
62 dns_soa_buildrdata(const dns_name_t *origin, const dns_name_t *contact,
63 dns_rdataclass_t rdclass,
64 isc_uint32_t serial, isc_uint32_t refresh,
65 isc_uint32_t retry, isc_uint32_t expire,
66 isc_uint32_t minimum, unsigned char *buffer,
67 dns_rdata_t *rdata) {
68 dns_rdata_soa_t soa;
69 isc_buffer_t rdatabuf;
70
71 REQUIRE(origin != NULL);
72 REQUIRE(contact != NULL);
73
74 memset(buffer, 0, DNS_SOA_BUFFERSIZE);
75 isc_buffer_init(&rdatabuf, buffer, DNS_SOA_BUFFERSIZE);
76
77 soa.common.rdtype = dns_rdatatype_soa;
78 soa.common.rdclass = rdclass;
79 soa.mctx = NULL;
80 soa.serial = serial;
81 soa.refresh = refresh;
82 soa.retry = retry;
83 soa.expire = expire;
84 soa.minimum = minimum;
85 dns_name_init(&soa.origin, NULL);
86 dns_name_clone(origin, &soa.origin);
87 dns_name_init(&soa.contact, NULL);
88 dns_name_clone(contact, &soa.contact);
89
90 return (dns_rdata_fromstruct(rdata, rdclass, dns_rdatatype_soa,
91 &soa, &rdatabuf));
92 }
93
94 isc_uint32_t
95 dns_soa_getserial(dns_rdata_t *rdata) {
96 return soa_get(rdata, 0);
97 }
98 isc_uint32_t
99 dns_soa_getrefresh(dns_rdata_t *rdata) {
100 return soa_get(rdata, 4);
101 }
102 isc_uint32_t
103 dns_soa_getretry(dns_rdata_t *rdata) {
104 return soa_get(rdata, 8);
105 }
106 isc_uint32_t
107 dns_soa_getexpire(dns_rdata_t *rdata) {
108 return soa_get(rdata, 12);
109 }
110 isc_uint32_t
111 dns_soa_getminimum(dns_rdata_t *rdata) {
112 return soa_get(rdata, 16);
113 }
114
115 static void
116 soa_set(dns_rdata_t *rdata, isc_uint32_t val, int offset) {
117 INSIST(rdata->type == dns_rdatatype_soa);
118 INSIST(rdata->length >= 20);
119 INSIST(offset >= 0 && offset <= 16);
120 encode_uint32(val, rdata->data + rdata->length - 20 + offset);
121 }
122
123 void
124 dns_soa_setserial(isc_uint32_t val, dns_rdata_t *rdata) {
125 soa_set(rdata, val, 0);
126 }
127 void
128 dns_soa_setrefresh(isc_uint32_t val, dns_rdata_t *rdata) {
129 soa_set(rdata, val, 4);
130 }
131 void
132 dns_soa_setretry(isc_uint32_t val, dns_rdata_t *rdata) {
133 soa_set(rdata, val, 8);
134 }
135 void
136 dns_soa_setexpire(isc_uint32_t val, dns_rdata_t *rdata) {
137 soa_set(rdata, val, 12);
138 }
139 void
140 dns_soa_setminimum(isc_uint32_t val, dns_rdata_t *rdata) {
141 soa_set(rdata, val, 16);
142 }
143