Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / lib / dns / soa.c
blobc75c82b90f70685bbfa614ebd3de53d136ea597f
1 /* $NetBSD: soa.c,v 1.4 2014/12/10 04:37:58 christos Exp $ */
3 /*
4 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: soa.c,v 1.12 2009/09/10 02:18:40 each Exp */
22 /*! \file */
24 #include <config.h>
25 #include <string.h>
27 #include <isc/buffer.h>
28 #include <isc/util.h>
30 #include <dns/rdata.h>
31 #include <dns/rdatastruct.h>
32 #include <dns/soa.h>
34 static inline isc_uint32_t
35 decode_uint32(unsigned char *p) {
36 return ((p[0] << 24) +
37 (p[1] << 16) +
38 (p[2] << 8) +
39 (p[3] << 0));
42 static inline void
43 encode_uint32(isc_uint32_t val, unsigned char *p) {
44 p[0] = (isc_uint8_t)(val >> 24);
45 p[1] = (isc_uint8_t)(val >> 16);
46 p[2] = (isc_uint8_t)(val >> 8);
47 p[3] = (isc_uint8_t)(val >> 0);
50 static isc_uint32_t
51 soa_get(dns_rdata_t *rdata, int offset) {
52 INSIST(rdata->type == dns_rdatatype_soa);
54 * Locate the field within the SOA RDATA based
55 * on its position relative to the end of the data.
57 * This is a bit of a kludge, but the alternative approach of
58 * using dns_rdata_tostruct() and dns_rdata_fromstruct() would
59 * involve a lot of unnecessary work (like building domain
60 * names and allocating temporary memory) when all we really
61 * want to do is to get 32 bits of fixed-sized data.
63 INSIST(rdata->length >= 20);
64 INSIST(offset >= 0 && offset <= 16);
65 return (decode_uint32(rdata->data + rdata->length - 20 + offset));
68 isc_result_t
69 dns_soa_buildrdata(dns_name_t *origin, dns_name_t *contact,
70 dns_rdataclass_t rdclass,
71 isc_uint32_t serial, isc_uint32_t refresh,
72 isc_uint32_t retry, isc_uint32_t expire,
73 isc_uint32_t minimum, unsigned char *buffer,
74 dns_rdata_t *rdata) {
75 dns_rdata_soa_t soa;
76 isc_buffer_t rdatabuf;
78 REQUIRE(origin != NULL);
79 REQUIRE(contact != NULL);
81 memset(buffer, 0, DNS_SOA_BUFFERSIZE);
82 isc_buffer_init(&rdatabuf, buffer, DNS_SOA_BUFFERSIZE);
84 soa.common.rdtype = dns_rdatatype_soa;
85 soa.common.rdclass = rdclass;
86 soa.mctx = NULL;
87 soa.serial = serial;
88 soa.refresh = refresh;
89 soa.retry = retry;
90 soa.expire = expire;
91 soa.minimum = minimum;
92 dns_name_init(&soa.origin, NULL);
93 dns_name_clone(origin, &soa.origin);
94 dns_name_init(&soa.contact, NULL);
95 dns_name_clone(contact, &soa.contact);
97 return (dns_rdata_fromstruct(rdata, rdclass, dns_rdatatype_soa,
98 &soa, &rdatabuf));
101 isc_uint32_t
102 dns_soa_getserial(dns_rdata_t *rdata) {
103 return soa_get(rdata, 0);
105 isc_uint32_t
106 dns_soa_getrefresh(dns_rdata_t *rdata) {
107 return soa_get(rdata, 4);
109 isc_uint32_t
110 dns_soa_getretry(dns_rdata_t *rdata) {
111 return soa_get(rdata, 8);
113 isc_uint32_t
114 dns_soa_getexpire(dns_rdata_t *rdata) {
115 return soa_get(rdata, 12);
117 isc_uint32_t
118 dns_soa_getminimum(dns_rdata_t *rdata) {
119 return soa_get(rdata, 16);
122 static void
123 soa_set(dns_rdata_t *rdata, isc_uint32_t val, int offset) {
124 INSIST(rdata->type == dns_rdatatype_soa);
125 INSIST(rdata->length >= 20);
126 INSIST(offset >= 0 && offset <= 16);
127 encode_uint32(val, rdata->data + rdata->length - 20 + offset);
130 void
131 dns_soa_setserial(isc_uint32_t val, dns_rdata_t *rdata) {
132 soa_set(rdata, val, 0);
134 void
135 dns_soa_setrefresh(isc_uint32_t val, dns_rdata_t *rdata) {
136 soa_set(rdata, val, 4);
138 void
139 dns_soa_setretry(isc_uint32_t val, dns_rdata_t *rdata) {
140 soa_set(rdata, val, 8);
142 void
143 dns_soa_setexpire(isc_uint32_t val, dns_rdata_t *rdata) {
144 soa_set(rdata, val, 12);
146 void
147 dns_soa_setminimum(isc_uint32_t val, dns_rdata_t *rdata) {
148 soa_set(rdata, val, 16);