00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00053
00054
00055
00056
00057
00058
00059
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 }