soa.c revision 0c27b3fe77ac1d5094ba3521e8142d9e7973133f
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington/*
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington * Copyright (C) 2000, 2001, 2004, 2005, 2007, 2009, 2016 Internet Systems Consortium, Inc. ("ISC")
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington *
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington * This Source Code Form is subject to the terms of the Mozilla Public
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington * License, v. 2.0. If a copy of the MPL was not distributed with this
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington */
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington/* $Id: soa.c,v 1.12 2009/09/10 02:18:40 each Exp $ */
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington/*! \file */
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <config.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <string.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <isc/buffer.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <isc/util.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
0f0162e6297ddf5e4abe848d27f3bcdb373189daBrian Wellington#include <dns/rdata.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <dns/rdatastruct.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <dns/soa.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtonstatic inline isc_uint32_t
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtondecode_uint32(unsigned char *p) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington return ((p[0] << 24) +
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington (p[1] << 16) +
10ba7f6f563d68b8f1284847ccce85b706e793feBob Halley (p[2] << 8) +
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington (p[3] << 0));
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington}
e552b980379e3a7ffce1411a939c62e27f953133Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtonstatic inline void
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtonencode_uint32(isc_uint32_t val, unsigned char *p) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington p[0] = (isc_uint8_t)(val >> 24);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington p[1] = (isc_uint8_t)(val >> 16);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington p[2] = (isc_uint8_t)(val >> 8);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington p[3] = (isc_uint8_t)(val >> 0);
d692d9991a731d60b63e6389da1ebf2b2839cfabBrian Wellington}
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtonstatic isc_uint32_t
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtonsoa_get(dns_rdata_t *rdata, int offset) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington INSIST(rdata->type == dns_rdatatype_soa);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington /*
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington * Locate the field within the SOA RDATA based
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington * on its position relative to the end of the data.
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington *
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington * This is a bit of a kludge, but the alternative approach of
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington * using dns_rdata_tostruct() and dns_rdata_fromstruct() would
e552b980379e3a7ffce1411a939c62e27f953133Brian Wellington * involve a lot of unnecessary work (like building domain
e552b980379e3a7ffce1411a939c62e27f953133Brian Wellington * names and allocating temporary memory) when all we really
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington * want to do is to get 32 bits of fixed-sized data.
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington */
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington INSIST(rdata->length >= 20);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington INSIST(offset >= 0 && offset <= 16);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington return (decode_uint32(rdata->data + rdata->length - 20 + offset));
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington}
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtonisc_result_t
3f6dc1703f76a24b34ed3bc839447291c33ca837Brian Wellingtondns_soa_buildrdata(dns_name_t *origin, dns_name_t *contact,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington dns_rdataclass_t rdclass,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isc_uint32_t serial, isc_uint32_t refresh,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isc_uint32_t retry, isc_uint32_t expire,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isc_uint32_t minimum, unsigned char *buffer,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington dns_rdata_t *rdata) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington dns_rdata_soa_t soa;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isc_buffer_t rdatabuf;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
3f6dc1703f76a24b34ed3bc839447291c33ca837Brian Wellington REQUIRE(origin != NULL);
ac335315cddc0a42b9235001197dcf719ae737b6Brian Wellington REQUIRE(contact != NULL);
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington memset(buffer, 0, DNS_SOA_BUFFERSIZE);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isc_buffer_init(&rdatabuf, buffer, DNS_SOA_BUFFERSIZE);
0a595c94a4137c3d56d529b86f5b8661513b16c3Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington soa.common.rdtype = dns_rdatatype_soa;
3f6dc1703f76a24b34ed3bc839447291c33ca837Brian Wellington soa.common.rdclass = rdclass;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington soa.mctx = NULL;
0a595c94a4137c3d56d529b86f5b8661513b16c3Brian Wellington soa.serial = serial;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington soa.refresh = refresh;
e552b980379e3a7ffce1411a939c62e27f953133Brian Wellington soa.retry = retry;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington soa.expire = expire;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington soa.minimum = minimum;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington dns_name_init(&soa.origin, NULL);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington dns_name_clone(origin, &soa.origin);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington dns_name_init(&soa.contact, NULL);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington dns_name_clone(contact, &soa.contact);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington return (dns_rdata_fromstruct(rdata, rdclass, dns_rdatatype_soa,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington &soa, &rdatabuf));
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington}
d92543b36036157e06025c7f44cf870c7e363a23Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtonisc_uint32_t
e552b980379e3a7ffce1411a939c62e27f953133Brian Wellingtondns_soa_getserial(dns_rdata_t *rdata) {
e552b980379e3a7ffce1411a939c62e27f953133Brian Wellington return soa_get(rdata, 0);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington}
e552b980379e3a7ffce1411a939c62e27f953133Brian Wellingtonisc_uint32_t
e552b980379e3a7ffce1411a939c62e27f953133Brian Wellingtondns_soa_getrefresh(dns_rdata_t *rdata) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington return soa_get(rdata, 4);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington}
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtonisc_uint32_t
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtondns_soa_getretry(dns_rdata_t *rdata) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington return soa_get(rdata, 8);
d2524f38d22998efb4196410643280b14f8b6febBob Halley}
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtonisc_uint32_t
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtondns_soa_getexpire(dns_rdata_t *rdata) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington return soa_get(rdata, 12);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington}
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtonisc_uint32_t
d2524f38d22998efb4196410643280b14f8b6febBob Halleydns_soa_getminimum(dns_rdata_t *rdata) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington return soa_get(rdata, 16);
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington}
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington
d598338952797df77417e69fcb8782b73651f9a9Brian Wellingtonstatic void
d598338952797df77417e69fcb8782b73651f9a9Brian Wellingtonsoa_set(dns_rdata_t *rdata, isc_uint32_t val, int offset) {
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington INSIST(rdata->type == dns_rdatatype_soa);
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington INSIST(rdata->length >= 20);
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington INSIST(offset >= 0 && offset <= 16);
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington encode_uint32(val, rdata->data + rdata->length - 20 + offset);
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington}
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington
d598338952797df77417e69fcb8782b73651f9a9Brian Wellingtonvoid
d598338952797df77417e69fcb8782b73651f9a9Brian Wellingtondns_soa_setserial(isc_uint32_t val, dns_rdata_t *rdata) {
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington soa_set(rdata, val, 0);
4a2f65ad8a26261e758d9682d88eb29395422fb9Brian Wellington}
4a2f65ad8a26261e758d9682d88eb29395422fb9Brian Wellingtonvoid
d598338952797df77417e69fcb8782b73651f9a9Brian Wellingtondns_soa_setrefresh(isc_uint32_t val, dns_rdata_t *rdata) {
0a595c94a4137c3d56d529b86f5b8661513b16c3Brian Wellington soa_set(rdata, val, 4);
0a595c94a4137c3d56d529b86f5b8661513b16c3Brian Wellington}
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtonvoid
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtondns_soa_setretry(isc_uint32_t val, dns_rdata_t *rdata) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington soa_set(rdata, val, 8);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington}
0a595c94a4137c3d56d529b86f5b8661513b16c3Brian Wellingtonvoid
0a595c94a4137c3d56d529b86f5b8661513b16c3Brian Wellingtondns_soa_setexpire(isc_uint32_t val, dns_rdata_t *rdata) {
0a595c94a4137c3d56d529b86f5b8661513b16c3Brian Wellington soa_set(rdata, val, 12);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington}
1a2c699f0eb89fbd776a2dfabb6e197fe36a8c20Brian Wellingtonvoid
1a2c699f0eb89fbd776a2dfabb6e197fe36a8c20Brian Wellingtondns_soa_setminimum(isc_uint32_t val, dns_rdata_t *rdata) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington soa_set(rdata, val, 16);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington}
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington