Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / dns / ttl.c
blob09d8311f501a8601fb38b5ae133b363f2743e392
1 /*
2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: ttl.c,v 1.21.2.1 2004/03/09 06:11:09 marka Exp $ */
20 #include <config.h>
22 #include <ctype.h>
23 #include <stdio.h>
24 #include <stdlib.h>
26 #include <isc/buffer.h>
27 #include <isc/print.h>
28 #include <isc/region.h>
29 #include <isc/string.h>
30 #include <isc/util.h>
32 #include <dns/result.h>
33 #include <dns/ttl.h>
35 #define RETERR(x) do { \
36 isc_result_t _r = (x); \
37 if (_r != ISC_R_SUCCESS) \
38 return (_r); \
39 } while (0)
42 static isc_result_t bind_ttl(isc_textregion_t *source, isc_uint32_t *ttl);
45 * Helper for dns_ttl_totext().
47 static isc_result_t
48 ttlfmt(unsigned int t, const char *s, isc_boolean_t verbose,
49 isc_boolean_t space, isc_buffer_t *target)
51 char tmp[60];
52 size_t len;
53 isc_region_t region;
55 if (verbose)
56 len = snprintf(tmp, sizeof(tmp), "%s%u %s%s",
57 space ? " " : "",
58 t, s,
59 t == 1 ? "" : "s");
60 else
61 len = snprintf(tmp, sizeof(tmp), "%u%c", t, s[0]);
63 INSIST(len + 1 <= sizeof tmp);
64 isc_buffer_availableregion(target, &region);
65 if (len > region.length)
66 return (ISC_R_NOSPACE);
67 memcpy(region.base, tmp, len);
68 isc_buffer_add(target, len);
70 return (ISC_R_SUCCESS);
74 * Derived from bind8 ns_format_ttl().
76 isc_result_t
77 dns_ttl_totext(isc_uint32_t src, isc_boolean_t verbose, isc_buffer_t *target) {
78 unsigned secs, mins, hours, days, weeks, x;
80 secs = src % 60; src /= 60;
81 mins = src % 60; src /= 60;
82 hours = src % 24; src /= 24;
83 days = src % 7; src /= 7;
84 weeks = src; src = 0;
86 x = 0;
87 if (weeks != 0) {
88 RETERR(ttlfmt(weeks, "week", verbose, ISC_TF(x > 0), target));
89 x++;
91 if (days != 0) {
92 RETERR(ttlfmt(days, "day", verbose, ISC_TF(x > 0), target));
93 x++;
95 if (hours != 0) {
96 RETERR(ttlfmt(hours, "hour", verbose, ISC_TF(x > 0), target));
97 x++;
99 if (mins != 0) {
100 RETERR(ttlfmt(mins, "minute", verbose, ISC_TF(x > 0), target));
101 x++;
103 if (secs != 0 ||
104 (weeks == 0 && days == 0 && hours == 0 && mins == 0)) {
105 RETERR(ttlfmt(secs, "second", verbose, ISC_TF(x > 0), target));
106 x++;
108 INSIST (x > 0);
110 * If only a single unit letter is printed, print it
111 * in upper case. (Why? Because BIND 8 does that.
112 * Presumably it has a reason.)
114 if (x == 1 && !verbose) {
115 isc_region_t region;
117 * The unit letter is the last character in the
118 * used region of the buffer.
120 * toupper() does not need its argument to be masked of cast
121 * here because region.base is type unsigned char *.
123 isc_buffer_usedregion(target, &region);
124 region.base[region.length - 1] =
125 toupper(region.base[region.length - 1]);
127 return (ISC_R_SUCCESS);
130 isc_result_t
131 dns_counter_fromtext(isc_textregion_t *source, isc_uint32_t *ttl) {
132 return (bind_ttl(source, ttl));
135 isc_result_t
136 dns_ttl_fromtext(isc_textregion_t *source, isc_uint32_t *ttl) {
137 isc_result_t result;
139 result = bind_ttl(source, ttl);
140 if (result != ISC_R_SUCCESS)
141 result = DNS_R_BADTTL;
142 return (result);
145 static isc_result_t
146 bind_ttl(isc_textregion_t *source, isc_uint32_t *ttl) {
147 isc_uint32_t tmp = 0;
148 unsigned long n;
149 char *e, *s;
150 char buf[64];
153 * Copy the buffer as it may not be NULL terminated.
154 * No legal counter / ttl is longer that 63 characters.
156 if (source->length > sizeof(buf) - 1)
157 return(DNS_R_SYNTAX);
158 strncpy(buf, source->base, source->length);
159 buf[source->length] = '\0';
160 s = buf;
162 do {
163 n = strtoul(s, &e, 10);
164 if (s == e)
165 return (DNS_R_SYNTAX);
166 switch (*e) {
167 case 'w':
168 case 'W':
169 tmp += n * 7 * 24 * 3600;
170 s = e + 1;
171 break;
172 case 'd':
173 case 'D':
174 tmp += n * 24 * 3600;
175 s = e + 1;
176 break;
177 case 'h':
178 case 'H':
179 tmp += n * 3600;
180 s = e + 1;
181 break;
182 case 'm':
183 case 'M':
184 tmp += n * 60;
185 s = e + 1;
186 break;
187 case 's':
188 case 'S':
189 tmp += n;
190 s = e + 1;
191 break;
192 case '\0':
193 /* Plain number? */
194 if (tmp != 0)
195 return (DNS_R_SYNTAX);
196 tmp = n;
197 s = e;
198 break;
199 default:
200 return (DNS_R_SYNTAX);
202 } while (*s != 0);
203 *ttl = tmp;
204 return (ISC_R_SUCCESS);