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