e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2000, 2001, 2004, 2005, 2007, 2009, 2016 Internet Systems Consortium, Inc. ("ISC")
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson */
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
0e7cb68ea0c0bf27d1a02b02c501045faef27ae2Evan Hunt/* $Id: soa.c,v 1.12 2009/09/10 02:18:40 each Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*! \file */
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson#include <config.h>
0e7cb68ea0c0bf27d1a02b02c501045faef27ae2Evan Hunt#include <string.h>
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#include <isc/buffer.h>
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson#include <isc/util.h>
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson#include <dns/rdata.h>
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#include <dns/rdatastruct.h>
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson#include <dns/soa.h>
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonstatic inline isc_uint32_t
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssondecode_uint32(unsigned char *p) {
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson return ((p[0] << 24) +
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson (p[1] << 16) +
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson (p[2] << 8) +
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson (p[3] << 0));
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson}
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonstatic inline void
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonencode_uint32(isc_uint32_t val, unsigned char *p) {
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson p[0] = (isc_uint8_t)(val >> 24);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson p[1] = (isc_uint8_t)(val >> 16);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson p[2] = (isc_uint8_t)(val >> 8);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson p[3] = (isc_uint8_t)(val >> 0);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson}
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonstatic isc_uint32_t
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonsoa_get(dns_rdata_t *rdata, int offset) {
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson INSIST(rdata->type == dns_rdatatype_soa);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson /*
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * Locate the field within the SOA RDATA based
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * on its position relative to the end of the data.
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson *
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * This is a bit of a kludge, but the alternative approach of
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * using dns_rdata_tostruct() and dns_rdata_fromstruct() would
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * involve a lot of unnecessary work (like building domain
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * names and allocating temporary memory) when all we really
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * want to do is to get 32 bits of fixed-sized data.
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson */
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson INSIST(rdata->length >= 20);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson INSIST(offset >= 0 && offset <= 16);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson return (decode_uint32(rdata->data + rdata->length - 20 + offset));
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson}
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Huntisc_result_t
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Huntdns_soa_buildrdata(dns_name_t *origin, dns_name_t *contact,
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt dns_rdataclass_t rdclass,
c6fb85f9500350e5ce58c9a24f5d264c8a8bd6f4Automatic Updater isc_uint32_t serial, isc_uint32_t refresh,
c6fb85f9500350e5ce58c9a24f5d264c8a8bd6f4Automatic Updater isc_uint32_t retry, isc_uint32_t expire,
dbabab1f37f9f1713d2179ce4bb4600499a1a3b1Evan Hunt isc_uint32_t minimum, unsigned char *buffer,
dbabab1f37f9f1713d2179ce4bb4600499a1a3b1Evan Hunt dns_rdata_t *rdata) {
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt dns_rdata_soa_t soa;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt isc_buffer_t rdatabuf;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt REQUIRE(origin != NULL);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt REQUIRE(contact != NULL);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
dbabab1f37f9f1713d2179ce4bb4600499a1a3b1Evan Hunt memset(buffer, 0, DNS_SOA_BUFFERSIZE);
dbabab1f37f9f1713d2179ce4bb4600499a1a3b1Evan Hunt isc_buffer_init(&rdatabuf, buffer, DNS_SOA_BUFFERSIZE);
dbabab1f37f9f1713d2179ce4bb4600499a1a3b1Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt soa.common.rdtype = dns_rdatatype_soa;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt soa.common.rdclass = rdclass;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt soa.mctx = NULL;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt soa.serial = serial;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt soa.refresh = refresh;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt soa.retry = retry;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt soa.expire = expire;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt soa.minimum = minimum;
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt dns_name_init(&soa.origin, NULL);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt dns_name_clone(origin, &soa.origin);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt dns_name_init(&soa.contact, NULL);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt dns_name_clone(contact, &soa.contact);
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt return (dns_rdata_fromstruct(rdata, rdclass, dns_rdatatype_soa,
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt &soa, &rdatabuf));
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt}
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonisc_uint32_t
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssondns_soa_getserial(dns_rdata_t *rdata) {
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson return soa_get(rdata, 0);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson}
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonisc_uint32_t
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssondns_soa_getrefresh(dns_rdata_t *rdata) {
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson return soa_get(rdata, 4);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson}
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonisc_uint32_t
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssondns_soa_getretry(dns_rdata_t *rdata) {
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson return soa_get(rdata, 8);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson}
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonisc_uint32_t
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssondns_soa_getexpire(dns_rdata_t *rdata) {
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson return soa_get(rdata, 12);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson}
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonisc_uint32_t
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssondns_soa_getminimum(dns_rdata_t *rdata) {
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson return soa_get(rdata, 16);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson}
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonstatic void
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonsoa_set(dns_rdata_t *rdata, isc_uint32_t val, int offset) {
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson INSIST(rdata->type == dns_rdatatype_soa);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson INSIST(rdata->length >= 20);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson INSIST(offset >= 0 && offset <= 16);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson encode_uint32(val, rdata->data + rdata->length - 20 + offset);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson}
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonvoid
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssondns_soa_setserial(isc_uint32_t val, dns_rdata_t *rdata) {
c39911afea7a963b40e7757628cc8c13b11b12f0Brian Wellington soa_set(rdata, val, 0);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson}
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonvoid
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssondns_soa_setrefresh(isc_uint32_t val, dns_rdata_t *rdata) {
c39911afea7a963b40e7757628cc8c13b11b12f0Brian Wellington soa_set(rdata, val, 4);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson}
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonvoid
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssondns_soa_setretry(isc_uint32_t val, dns_rdata_t *rdata) {
c39911afea7a963b40e7757628cc8c13b11b12f0Brian Wellington soa_set(rdata, val, 8);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson}
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonvoid
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssondns_soa_setexpire(isc_uint32_t val, dns_rdata_t *rdata) {
c39911afea7a963b40e7757628cc8c13b11b12f0Brian Wellington soa_set(rdata, val, 12);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson}
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonvoid
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssondns_soa_setminimum(isc_uint32_t val, dns_rdata_t *rdata) {
c39911afea7a963b40e7757628cc8c13b11b12f0Brian Wellington soa_set(rdata, val, 16);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson}