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.12.5 2004/03/08 09:04:32 marka Exp $ */
27 #include <isc/buffer.h>
28 #include <isc/parseint.h>
29 #include <isc/print.h>
30 #include <isc/region.h>
31 #include <isc/string.h>
34 #include <dns/result.h>
37 #define RETERR(x) do { \
38 isc_result_t _r = (x); \
39 if (_r != ISC_R_SUCCESS) \
44 static isc_result_t bind_ttl(isc_textregion_t *source, isc_uint32_t *ttl);
47 * Helper for dns_ttl_totext().
50 ttlfmt(unsigned int t, const char *s, isc_boolean_t verbose,
51 isc_boolean_t space, isc_buffer_t *target)
58 len = snprintf(tmp, sizeof(tmp), "%s%u %s%s",
63 len = snprintf(tmp, sizeof(tmp), "%u%c", t, s[0]);
65 INSIST(len + 1 <= sizeof(tmp));
66 isc_buffer_availableregion(target, ®ion);
67 if (len > region.length)
68 return (ISC_R_NOSPACE);
69 memcpy(region.base, tmp, len);
70 isc_buffer_add(target, len);
72 return (ISC_R_SUCCESS);
76 * Derived from bind8 ns_format_ttl().
79 dns_ttl_totext(isc_uint32_t src, isc_boolean_t verbose, isc_buffer_t *target) {
80 unsigned secs, mins, hours, days, weeks, x;
82 secs = src % 60; src /= 60;
83 mins = src % 60; src /= 60;
84 hours = src % 24; src /= 24;
85 days = src % 7; src /= 7;
90 RETERR(ttlfmt(weeks, "week", verbose, ISC_TF(x > 0), target));
94 RETERR(ttlfmt(days, "day", verbose, ISC_TF(x > 0), target));
98 RETERR(ttlfmt(hours, "hour", verbose, ISC_TF(x > 0), target));
102 RETERR(ttlfmt(mins, "minute", verbose, ISC_TF(x > 0), target));
106 (weeks == 0 && days == 0 && hours == 0 && mins == 0)) {
107 RETERR(ttlfmt(secs, "second", verbose, ISC_TF(x > 0), target));
112 * If only a single unit letter is printed, print it
113 * in upper case. (Why? Because BIND 8 does that.
114 * Presumably it has a reason.)
116 if (x == 1 && !verbose) {
119 * The unit letter is the last character in the
120 * used region of the buffer.
122 * toupper() does not need its argument to be masked of cast
123 * here because region.base is type unsigned char *.
125 isc_buffer_usedregion(target, ®ion);
126 region.base[region.length - 1] =
127 toupper(region.base[region.length - 1]);
129 return (ISC_R_SUCCESS);
133 dns_counter_fromtext(isc_textregion_t *source, isc_uint32_t *ttl) {
134 return (bind_ttl(source, ttl));
138 dns_ttl_fromtext(isc_textregion_t *source, isc_uint32_t *ttl) {
141 result = bind_ttl(source, ttl);
142 if (result != ISC_R_SUCCESS)
143 result = DNS_R_BADTTL;
148 bind_ttl(isc_textregion_t *source, isc_uint32_t *ttl) {
149 isc_uint32_t tmp = 0;
153 char nbuf[64]; /* Number buffer */
156 * Copy the buffer as it may not be NULL terminated.
157 * No legal counter / ttl is longer that 63 characters.
159 if (source->length > sizeof(buf) - 1)
160 return (DNS_R_SYNTAX);
161 strncpy(buf, source->base, source->length);
162 buf[source->length] = '\0';
169 while (*s != '\0' && isdigit((unsigned char)*s))
172 INSIST(np - nbuf <= (int)sizeof(nbuf));
173 result = isc_parse_uint32(&n, nbuf, 10);
174 if (result != ISC_R_SUCCESS)
175 return (DNS_R_SYNTAX);
179 tmp += n * 7 * 24 * 3600;
184 tmp += n * 24 * 3600;
205 return (DNS_R_SYNTAX);
209 return (DNS_R_SYNTAX);
211 } while (*s != '\0');
213 return (ISC_R_SUCCESS);