soa.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007, 2009  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 2000, 2001  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id: soa.c,v 1.12 2009/09/10 02:18:40 each Exp $ */
00019 
00020 /*! \file */
00021 
00022 #include <config.h>
00023 #include <string.h>
00024 
00025 #include <isc/buffer.h>
00026 #include <isc/util.h>
00027 
00028 #include <dns/rdata.h>
00029 #include <dns/rdatastruct.h>
00030 #include <dns/soa.h>
00031 
00032 static inline isc_uint32_t
00033 decode_uint32(unsigned char *p) {
00034         return ((p[0] << 24) +
00035                 (p[1] << 16) +
00036                 (p[2] <<  8) +
00037                 (p[3] <<  0));
00038 }
00039 
00040 static inline void
00041 encode_uint32(isc_uint32_t val, unsigned char *p) {
00042         p[0] = (isc_uint8_t)(val >> 24);
00043         p[1] = (isc_uint8_t)(val >> 16);
00044         p[2] = (isc_uint8_t)(val >>  8);
00045         p[3] = (isc_uint8_t)(val >>  0);
00046 }
00047 
00048 static isc_uint32_t
00049 soa_get(dns_rdata_t *rdata, int offset) {
00050         INSIST(rdata->type == dns_rdatatype_soa);
00051         /*
00052          * Locate the field within the SOA RDATA based
00053          * on its position relative to the end of the data.
00054          *
00055          * This is a bit of a kludge, but the alternative approach of
00056          * using dns_rdata_tostruct() and dns_rdata_fromstruct() would
00057          * involve a lot of unnecessary work (like building domain
00058          * names and allocating temporary memory) when all we really
00059          * want to do is to get 32 bits of fixed-sized data.
00060          */
00061         INSIST(rdata->length >= 20);
00062         INSIST(offset >= 0 && offset <= 16);
00063         return (decode_uint32(rdata->data + rdata->length - 20 + offset));
00064 }
00065 
00066 isc_result_t
00067 dns_soa_buildrdata(dns_name_t *origin, dns_name_t *contact,
00068                    dns_rdataclass_t rdclass,
00069                    isc_uint32_t serial, isc_uint32_t refresh,
00070                    isc_uint32_t retry, isc_uint32_t expire,
00071                    isc_uint32_t minimum, unsigned char *buffer,
00072                    dns_rdata_t *rdata) {
00073         dns_rdata_soa_t soa;
00074         isc_buffer_t rdatabuf;
00075 
00076         REQUIRE(origin != NULL);
00077         REQUIRE(contact != NULL);
00078 
00079         memset(buffer, 0, DNS_SOA_BUFFERSIZE);
00080         isc_buffer_init(&rdatabuf, buffer, DNS_SOA_BUFFERSIZE);
00081 
00082         soa.common.rdtype = dns_rdatatype_soa;
00083         soa.common.rdclass = rdclass;
00084         soa.mctx = NULL;
00085         soa.serial = serial;
00086         soa.refresh = refresh;
00087         soa.retry = retry;
00088         soa.expire = expire;
00089         soa.minimum = minimum;
00090         dns_name_init(&soa.origin, NULL);
00091         dns_name_clone(origin, &soa.origin);
00092         dns_name_init(&soa.contact, NULL);
00093         dns_name_clone(contact, &soa.contact);
00094 
00095         return (dns_rdata_fromstruct(rdata, rdclass, dns_rdatatype_soa,
00096                                       &soa, &rdatabuf));
00097 }
00098 
00099 isc_uint32_t
00100 dns_soa_getserial(dns_rdata_t *rdata) {
00101         return soa_get(rdata, 0);
00102 }
00103 isc_uint32_t
00104 dns_soa_getrefresh(dns_rdata_t *rdata) {
00105         return soa_get(rdata, 4);
00106 }
00107 isc_uint32_t
00108 dns_soa_getretry(dns_rdata_t *rdata) {
00109         return soa_get(rdata, 8);
00110 }
00111 isc_uint32_t
00112 dns_soa_getexpire(dns_rdata_t *rdata) {
00113         return soa_get(rdata, 12);
00114 }
00115 isc_uint32_t
00116 dns_soa_getminimum(dns_rdata_t *rdata) {
00117         return soa_get(rdata, 16);
00118 }
00119 
00120 static void
00121 soa_set(dns_rdata_t *rdata, isc_uint32_t val, int offset) {
00122         INSIST(rdata->type == dns_rdatatype_soa);
00123         INSIST(rdata->length >= 20);
00124         INSIST(offset >= 0 && offset <= 16);
00125         encode_uint32(val, rdata->data + rdata->length - 20 + offset);
00126 }
00127 
00128 void
00129 dns_soa_setserial(isc_uint32_t val, dns_rdata_t *rdata) {
00130         soa_set(rdata, val, 0);
00131 }
00132 void
00133 dns_soa_setrefresh(isc_uint32_t val, dns_rdata_t *rdata) {
00134         soa_set(rdata, val, 4);
00135 }
00136 void
00137 dns_soa_setretry(isc_uint32_t val, dns_rdata_t *rdata) {
00138         soa_set(rdata, val, 8);
00139 }
00140 void
00141 dns_soa_setexpire(isc_uint32_t val, dns_rdata_t *rdata) {
00142         soa_set(rdata, val, 12);
00143 }
00144 void
00145 dns_soa_setminimum(isc_uint32_t val, dns_rdata_t *rdata) {
00146         soa_set(rdata, val, 16);
00147 }

Generated on Tue Apr 28 17:41:01 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha