Home | History | Annotate | Line # | Download | only in dns
soa.c revision 1.3
      1 /*	$NetBSD: soa.c,v 1.3 2019/01/09 16:55:12 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 
     19 #include <inttypes.h>
     20 #include <string.h>
     21 
     22 #include <isc/buffer.h>
     23 #include <isc/util.h>
     24 
     25 #include <dns/rdata.h>
     26 #include <dns/rdatastruct.h>
     27 #include <dns/soa.h>
     28 
     29 static inline 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(uint32_t val, unsigned char *p) {
     39 	p[0] = (uint8_t)(val >> 24);
     40 	p[1] = (uint8_t)(val >> 16);
     41 	p[2] = (uint8_t)(val >>  8);
     42 	p[3] = (uint8_t)(val >>  0);
     43 }
     44 
     45 static 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_result_t
     64 dns_soa_buildrdata(const dns_name_t *origin, const dns_name_t *contact,
     65 		   dns_rdataclass_t rdclass,
     66 		   uint32_t serial, uint32_t refresh,
     67 		   uint32_t retry, uint32_t expire,
     68 		   uint32_t minimum, unsigned char *buffer,
     69 		   dns_rdata_t *rdata) {
     70 	dns_rdata_soa_t soa;
     71 	isc_buffer_t rdatabuf;
     72 
     73 	REQUIRE(origin != NULL);
     74 	REQUIRE(contact != NULL);
     75 
     76 	memset(buffer, 0, DNS_SOA_BUFFERSIZE);
     77 	isc_buffer_init(&rdatabuf, buffer, DNS_SOA_BUFFERSIZE);
     78 
     79 	soa.common.rdtype = dns_rdatatype_soa;
     80 	soa.common.rdclass = rdclass;
     81 	soa.mctx = NULL;
     82 	soa.serial = serial;
     83 	soa.refresh = refresh;
     84 	soa.retry = retry;
     85 	soa.expire = expire;
     86 	soa.minimum = minimum;
     87 	dns_name_init(&soa.origin, NULL);
     88 	dns_name_clone(origin, &soa.origin);
     89 	dns_name_init(&soa.contact, NULL);
     90 	dns_name_clone(contact, &soa.contact);
     91 
     92 	return (dns_rdata_fromstruct(rdata, rdclass, dns_rdatatype_soa,
     93 				      &soa, &rdatabuf));
     94 }
     95 
     96 uint32_t
     97 dns_soa_getserial(dns_rdata_t *rdata) {
     98 	return soa_get(rdata, 0);
     99 }
    100 uint32_t
    101 dns_soa_getrefresh(dns_rdata_t *rdata) {
    102 	return soa_get(rdata, 4);
    103 }
    104 uint32_t
    105 dns_soa_getretry(dns_rdata_t *rdata) {
    106 	return soa_get(rdata, 8);
    107 }
    108 uint32_t
    109 dns_soa_getexpire(dns_rdata_t *rdata) {
    110 	return soa_get(rdata, 12);
    111 }
    112 uint32_t
    113 dns_soa_getminimum(dns_rdata_t *rdata) {
    114 	return soa_get(rdata, 16);
    115 }
    116 
    117 static void
    118 soa_set(dns_rdata_t *rdata, uint32_t val, int offset) {
    119 	INSIST(rdata->type == dns_rdatatype_soa);
    120 	INSIST(rdata->length >= 20);
    121 	INSIST(offset >= 0 && offset <= 16);
    122 	encode_uint32(val, rdata->data + rdata->length - 20 + offset);
    123 }
    124 
    125 void
    126 dns_soa_setserial(uint32_t val, dns_rdata_t *rdata) {
    127 	soa_set(rdata, val, 0);
    128 }
    129 void
    130 dns_soa_setrefresh(uint32_t val, dns_rdata_t *rdata) {
    131 	soa_set(rdata, val, 4);
    132 }
    133 void
    134 dns_soa_setretry(uint32_t val, dns_rdata_t *rdata) {
    135 	soa_set(rdata, val, 8);
    136 }
    137 void
    138 dns_soa_setexpire(uint32_t val, dns_rdata_t *rdata) {
    139 	soa_set(rdata, val, 12);
    140 }
    141 void
    142 dns_soa_setminimum(uint32_t val, dns_rdata_t *rdata) {
    143 	soa_set(rdata, val, 16);
    144 }
    145