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