4 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-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: ttl.c,v 1.29 2007/06/19 23:47:16 tbox Exp */
31 #include <isc/buffer.h>
32 #include <isc/parseint.h>
33 #include <isc/print.h>
34 #include <isc/region.h>
35 #include <isc/string.h>
38 #include <dns/result.h>
41 #define RETERR(x) do { \
42 isc_result_t _r = (x); \
43 if (_r != ISC_R_SUCCESS) \
48 static isc_result_t
bind_ttl(isc_textregion_t
*source
, isc_uint32_t
*ttl
);
51 * Helper for dns_ttl_totext().
54 ttlfmt(unsigned int t
, const char *s
, isc_boolean_t verbose
,
55 isc_boolean_t space
, isc_buffer_t
*target
)
62 len
= snprintf(tmp
, sizeof(tmp
), "%s%u %s%s",
67 len
= snprintf(tmp
, sizeof(tmp
), "%u%c", t
, s
[0]);
69 INSIST(len
+ 1 <= sizeof(tmp
));
70 isc_buffer_availableregion(target
, ®ion
);
71 if (len
> region
.length
)
72 return (ISC_R_NOSPACE
);
73 memcpy(region
.base
, tmp
, len
);
74 isc_buffer_add(target
, len
);
76 return (ISC_R_SUCCESS
);
80 * Derived from bind8 ns_format_ttl().
83 dns_ttl_totext(isc_uint32_t src
, isc_boolean_t verbose
, isc_buffer_t
*target
) {
84 unsigned secs
, mins
, hours
, days
, weeks
, x
;
86 secs
= src
% 60; src
/= 60;
87 mins
= src
% 60; src
/= 60;
88 hours
= src
% 24; src
/= 24;
89 days
= src
% 7; src
/= 7;
94 RETERR(ttlfmt(weeks
, "week", verbose
, ISC_TF(x
> 0), target
));
98 RETERR(ttlfmt(days
, "day", verbose
, ISC_TF(x
> 0), target
));
102 RETERR(ttlfmt(hours
, "hour", verbose
, ISC_TF(x
> 0), target
));
106 RETERR(ttlfmt(mins
, "minute", verbose
, ISC_TF(x
> 0), target
));
110 (weeks
== 0 && days
== 0 && hours
== 0 && mins
== 0)) {
111 RETERR(ttlfmt(secs
, "second", verbose
, ISC_TF(x
> 0), target
));
116 * If only a single unit letter is printed, print it
117 * in upper case. (Why? Because BIND 8 does that.
118 * Presumably it has a reason.)
120 if (x
== 1 && !verbose
) {
123 * The unit letter is the last character in the
124 * used region of the buffer.
126 * toupper() does not need its argument to be masked of cast
127 * here because region.base is type unsigned char *.
129 isc_buffer_usedregion(target
, ®ion
);
130 region
.base
[region
.length
- 1] =
131 toupper(region
.base
[region
.length
- 1]);
133 return (ISC_R_SUCCESS
);
137 dns_counter_fromtext(isc_textregion_t
*source
, isc_uint32_t
*ttl
) {
138 return (bind_ttl(source
, ttl
));
142 dns_ttl_fromtext(isc_textregion_t
*source
, isc_uint32_t
*ttl
) {
145 result
= bind_ttl(source
, ttl
);
146 if (result
!= ISC_R_SUCCESS
)
147 result
= DNS_R_BADTTL
;
152 bind_ttl(isc_textregion_t
*source
, isc_uint32_t
*ttl
) {
153 isc_uint32_t tmp
= 0;
157 char nbuf
[64]; /* Number buffer */
160 * Copy the buffer as it may not be NULL terminated.
161 * No legal counter / ttl is longer that 63 characters.
163 if (source
->length
> sizeof(buf
) - 1)
164 return (DNS_R_SYNTAX
);
165 strncpy(buf
, source
->base
, source
->length
);
166 buf
[source
->length
] = '\0';
173 while (*s
!= '\0' && isdigit((unsigned char)*s
))
176 INSIST(np
- nbuf
<= (int)sizeof(nbuf
));
177 result
= isc_parse_uint32(&n
, nbuf
, 10);
178 if (result
!= ISC_R_SUCCESS
)
179 return (DNS_R_SYNTAX
);
183 tmp
+= n
* 7 * 24 * 3600;
188 tmp
+= n
* 24 * 3600;
209 return (DNS_R_SYNTAX
);
213 return (DNS_R_SYNTAX
);
215 } while (*s
!= '\0');
217 return (ISC_R_SUCCESS
);