1 /* $NetBSD: ttl.c,v 1.7 2014/12/10 04:37:58 christos Exp $ */
4 * Copyright (C) 2004, 2005, 2007, 2011-2014 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.
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) \
45 } while (/*CONSTCOND*/0)
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 memmove(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;
95 RETERR(ttlfmt(weeks
, "week", verbose
, ISC_TF(x
> 0), target
));
99 RETERR(ttlfmt(days
, "day", verbose
, ISC_TF(x
> 0), target
));
103 RETERR(ttlfmt(hours
, "hour", verbose
, ISC_TF(x
> 0), target
));
107 RETERR(ttlfmt(mins
, "minute", verbose
, ISC_TF(x
> 0), target
));
111 (weeks
== 0 && days
== 0 && hours
== 0 && mins
== 0)) {
112 RETERR(ttlfmt(secs
, "second", verbose
, ISC_TF(x
> 0), target
));
117 * If only a single unit letter is printed, print it
118 * in upper case. (Why? Because BIND 8 does that.
119 * Presumably it has a reason.)
121 if (x
== 1 && !verbose
) {
124 * The unit letter is the last character in the
125 * used region of the buffer.
127 * toupper() does not need its argument to be masked of cast
128 * here because region.base is type unsigned char *.
130 isc_buffer_usedregion(target
, ®ion
);
131 region
.base
[region
.length
- 1] =
132 toupper(region
.base
[region
.length
- 1]);
134 return (ISC_R_SUCCESS
);
138 dns_counter_fromtext(isc_textregion_t
*source
, isc_uint32_t
*ttl
) {
139 return (bind_ttl(source
, ttl
));
143 dns_ttl_fromtext(isc_textregion_t
*source
, isc_uint32_t
*ttl
) {
146 result
= bind_ttl(source
, ttl
);
147 if (result
!= ISC_R_SUCCESS
&& result
!= ISC_R_RANGE
)
148 result
= DNS_R_BADTTL
;
153 bind_ttl(isc_textregion_t
*source
, isc_uint32_t
*ttl
) {
154 isc_uint64_t tmp
= 0ULL;
158 char nbuf
[64]; /* Number buffer */
161 * Copy the buffer as it may not be NULL terminated.
162 * No legal counter / ttl is longer that 63 characters.
164 if (source
->length
> sizeof(buf
) - 1)
165 return (DNS_R_SYNTAX
);
166 strncpy(buf
, source
->base
, source
->length
);
167 buf
[source
->length
] = '\0';
174 while (*s
!= '\0' && isdigit((unsigned char)*s
))
177 INSIST(np
- nbuf
<= (int)sizeof(nbuf
));
178 result
= isc_parse_uint32(&n
, nbuf
, 10);
179 if (result
!= ISC_R_SUCCESS
)
180 return (DNS_R_SYNTAX
);
184 tmp
+= (isc_uint64_t
) n
* 7 * 24 * 3600;
189 tmp
+= (isc_uint64_t
) n
* 24 * 3600;
194 tmp
+= (isc_uint64_t
) n
* 3600;
199 tmp
+= (isc_uint64_t
) n
* 60;
204 tmp
+= (isc_uint64_t
) n
;
210 return (DNS_R_SYNTAX
);
214 return (DNS_R_SYNTAX
);
216 } while (*s
!= '\0');
218 if (tmp
> 0xffffffffULL
)
219 return (ISC_R_RANGE
);
221 *ttl
= (isc_uint32_t
)(tmp
& 0xffffffffUL
);
222 return (ISC_R_SUCCESS
);