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 $ */
26 #include <isc/buffer.h>
27 #include <isc/print.h>
28 #include <isc/region.h>
29 #include <isc/string.h>
32 #include <dns/result.h>
35 #define RETERR(x) do { \
36 isc_result_t _r = (x); \
37 if (_r != ISC_R_SUCCESS) \
42 static isc_result_t
bind_ttl(isc_textregion_t
*source
, isc_uint32_t
*ttl
);
45 * Helper for dns_ttl_totext().
48 ttlfmt(unsigned int t
, const char *s
, isc_boolean_t verbose
,
49 isc_boolean_t space
, isc_buffer_t
*target
)
56 len
= snprintf(tmp
, sizeof(tmp
), "%s%u %s%s",
61 len
= snprintf(tmp
, sizeof(tmp
), "%u%c", t
, s
[0]);
63 INSIST(len
+ 1 <= sizeof tmp
);
64 isc_buffer_availableregion(target
, ®ion
);
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().
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;
88 RETERR(ttlfmt(weeks
, "week", verbose
, ISC_TF(x
> 0), target
));
92 RETERR(ttlfmt(days
, "day", verbose
, ISC_TF(x
> 0), target
));
96 RETERR(ttlfmt(hours
, "hour", verbose
, ISC_TF(x
> 0), target
));
100 RETERR(ttlfmt(mins
, "minute", verbose
, ISC_TF(x
> 0), target
));
104 (weeks
== 0 && days
== 0 && hours
== 0 && mins
== 0)) {
105 RETERR(ttlfmt(secs
, "second", verbose
, ISC_TF(x
> 0), target
));
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
) {
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
, ®ion
);
124 region
.base
[region
.length
- 1] =
125 toupper(region
.base
[region
.length
- 1]);
127 return (ISC_R_SUCCESS
);
131 dns_counter_fromtext(isc_textregion_t
*source
, isc_uint32_t
*ttl
) {
132 return (bind_ttl(source
, ttl
));
136 dns_ttl_fromtext(isc_textregion_t
*source
, isc_uint32_t
*ttl
) {
139 result
= bind_ttl(source
, ttl
);
140 if (result
!= ISC_R_SUCCESS
)
141 result
= DNS_R_BADTTL
;
146 bind_ttl(isc_textregion_t
*source
, isc_uint32_t
*ttl
) {
147 isc_uint32_t tmp
= 0;
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';
163 n
= strtoul(s
, &e
, 10);
165 return (DNS_R_SYNTAX
);
169 tmp
+= n
* 7 * 24 * 3600;
174 tmp
+= n
* 24 * 3600;
195 return (DNS_R_SYNTAX
);
200 return (DNS_R_SYNTAX
);
204 return (ISC_R_SUCCESS
);