Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
3 | | * |
4 | | * SPDX-License-Identifier: MPL-2.0 |
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 https://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 | | /*! \file */ |
15 | | |
16 | | #include <inttypes.h> |
17 | | #include <string.h> |
18 | | |
19 | | #include <isc/buffer.h> |
20 | | #include <isc/util.h> |
21 | | |
22 | | #include <dns/rdata.h> |
23 | | #include <dns/rdatastruct.h> |
24 | | #include <dns/soa.h> |
25 | | |
26 | | static uint32_t |
27 | 0 | decode_uint32(unsigned char *p) { |
28 | 0 | return ((uint32_t)p[0] << 24) + ((uint32_t)p[1] << 16) + |
29 | 0 | ((uint32_t)p[2] << 8) + ((uint32_t)p[3] << 0); |
30 | 0 | } |
31 | | |
32 | | static void |
33 | 0 | encode_uint32(uint32_t val, unsigned char *p) { |
34 | 0 | p[0] = (uint8_t)(val >> 24); |
35 | 0 | p[1] = (uint8_t)(val >> 16); |
36 | 0 | p[2] = (uint8_t)(val >> 8); |
37 | 0 | p[3] = (uint8_t)(val >> 0); |
38 | 0 | } |
39 | | |
40 | | static uint32_t |
41 | 0 | soa_get(dns_rdata_t *rdata, int offset) { |
42 | 0 | INSIST(rdata->type == dns_rdatatype_soa); |
43 | | /* |
44 | | * Locate the field within the SOA RDATA based |
45 | | * on its position relative to the end of the data. |
46 | | * |
47 | | * This is a bit of a kludge, but the alternative approach of |
48 | | * using dns_rdata_tostruct() and dns_rdata_fromstruct() would |
49 | | * involve a lot of unnecessary work (like building domain |
50 | | * names and allocating temporary memory) when all we really |
51 | | * want to do is to get 32 bits of fixed-sized data. |
52 | | */ |
53 | 0 | INSIST(rdata->length >= 20); |
54 | 0 | INSIST(offset >= 0 && offset <= 16); |
55 | 0 | return decode_uint32(rdata->data + rdata->length - 20 + offset); |
56 | 0 | } |
57 | | |
58 | | isc_result_t |
59 | | dns_soa_buildrdata(const dns_name_t *origin, const dns_name_t *contact, |
60 | | dns_rdataclass_t rdclass, uint32_t serial, uint32_t refresh, |
61 | | uint32_t retry, uint32_t expire, uint32_t minimum, |
62 | 0 | unsigned char *buffer, dns_rdata_t *rdata) { |
63 | 0 | dns_rdata_soa_t soa; |
64 | 0 | isc_buffer_t rdatabuf; |
65 | |
|
66 | 0 | REQUIRE(origin != NULL); |
67 | 0 | REQUIRE(contact != NULL); |
68 | |
|
69 | 0 | memset(buffer, 0, DNS_SOA_BUFFERSIZE); |
70 | 0 | isc_buffer_init(&rdatabuf, buffer, DNS_SOA_BUFFERSIZE); |
71 | |
|
72 | 0 | soa.common.rdtype = dns_rdatatype_soa; |
73 | 0 | soa.common.rdclass = rdclass; |
74 | 0 | soa.mctx = NULL; |
75 | 0 | soa.serial = serial; |
76 | 0 | soa.refresh = refresh; |
77 | 0 | soa.retry = retry; |
78 | 0 | soa.expire = expire; |
79 | 0 | soa.minimum = minimum; |
80 | 0 | dns_name_init(&soa.origin); |
81 | 0 | dns_name_clone(origin, &soa.origin); |
82 | 0 | dns_name_init(&soa.contact); |
83 | 0 | dns_name_clone(contact, &soa.contact); |
84 | |
|
85 | 0 | return dns_rdata_fromstruct(rdata, rdclass, dns_rdatatype_soa, &soa, |
86 | 0 | &rdatabuf); |
87 | 0 | } |
88 | | |
89 | | uint32_t |
90 | 0 | dns_soa_getserial(dns_rdata_t *rdata) { |
91 | 0 | return soa_get(rdata, 0); |
92 | 0 | } |
93 | | uint32_t |
94 | 0 | dns_soa_getrefresh(dns_rdata_t *rdata) { |
95 | 0 | return soa_get(rdata, 4); |
96 | 0 | } |
97 | | uint32_t |
98 | 0 | dns_soa_getretry(dns_rdata_t *rdata) { |
99 | 0 | return soa_get(rdata, 8); |
100 | 0 | } |
101 | | uint32_t |
102 | 0 | dns_soa_getexpire(dns_rdata_t *rdata) { |
103 | 0 | return soa_get(rdata, 12); |
104 | 0 | } |
105 | | uint32_t |
106 | 0 | dns_soa_getminimum(dns_rdata_t *rdata) { |
107 | 0 | return soa_get(rdata, 16); |
108 | 0 | } |
109 | | |
110 | | static void |
111 | 0 | soa_set(dns_rdata_t *rdata, uint32_t val, int offset) { |
112 | 0 | INSIST(rdata->type == dns_rdatatype_soa); |
113 | 0 | INSIST(rdata->length >= 20); |
114 | 0 | INSIST(offset >= 0 && offset <= 16); |
115 | 0 | encode_uint32(val, rdata->data + rdata->length - 20 + offset); |
116 | 0 | } |
117 | | |
118 | | void |
119 | 0 | dns_soa_setserial(uint32_t val, dns_rdata_t *rdata) { |
120 | 0 | soa_set(rdata, val, 0); |
121 | 0 | } |
122 | | void |
123 | 0 | dns_soa_setrefresh(uint32_t val, dns_rdata_t *rdata) { |
124 | 0 | soa_set(rdata, val, 4); |
125 | 0 | } |
126 | | void |
127 | 0 | dns_soa_setretry(uint32_t val, dns_rdata_t *rdata) { |
128 | 0 | soa_set(rdata, val, 8); |
129 | 0 | } |
130 | | void |
131 | 0 | dns_soa_setexpire(uint32_t val, dns_rdata_t *rdata) { |
132 | 0 | soa_set(rdata, val, 12); |
133 | 0 | } |
134 | | void |
135 | 0 | dns_soa_setminimum(uint32_t val, dns_rdata_t *rdata) { |
136 | 0 | soa_set(rdata, val, 16); |
137 | 0 | } |