]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/bind9/lib/dns/ttl.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / bind9 / lib / dns / ttl.c
1 /*
2  * Copyright (C) 2004, 2005, 2007, 2011, 2012  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2001  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or 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.
8  *
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.
16  */
17
18 /* $Id$ */
19
20 /*! \file */
21
22 #include <config.h>
23
24 #include <ctype.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include <isc/buffer.h>
30 #include <isc/parseint.h>
31 #include <isc/print.h>
32 #include <isc/region.h>
33 #include <isc/string.h>
34 #include <isc/util.h>
35
36 #include <dns/result.h>
37 #include <dns/ttl.h>
38
39 #define RETERR(x) do { \
40         isc_result_t _r = (x); \
41         if (_r != ISC_R_SUCCESS) \
42                 return (_r); \
43         } while (0)
44
45
46 static isc_result_t bind_ttl(isc_textregion_t *source, isc_uint32_t *ttl);
47
48 /*
49  * Helper for dns_ttl_totext().
50  */
51 static isc_result_t
52 ttlfmt(unsigned int t, const char *s, isc_boolean_t verbose,
53        isc_boolean_t space, isc_buffer_t *target)
54 {
55         char tmp[60];
56         size_t len;
57         isc_region_t region;
58
59         if (verbose)
60                 len = snprintf(tmp, sizeof(tmp), "%s%u %s%s",
61                                space ? " " : "",
62                                t, s,
63                                t == 1 ? "" : "s");
64         else
65                 len = snprintf(tmp, sizeof(tmp), "%u%c", t, s[0]);
66
67         INSIST(len + 1 <= sizeof(tmp));
68         isc_buffer_availableregion(target, &region);
69         if (len > region.length)
70                 return (ISC_R_NOSPACE);
71         memcpy(region.base, tmp, len);
72         isc_buffer_add(target, len);
73
74         return (ISC_R_SUCCESS);
75 }
76
77 /*
78  * Derived from bind8 ns_format_ttl().
79  */
80 isc_result_t
81 dns_ttl_totext(isc_uint32_t src, isc_boolean_t verbose, isc_buffer_t *target) {
82         unsigned secs, mins, hours, days, weeks, x;
83
84         secs = src % 60;   src /= 60;
85         mins = src % 60;   src /= 60;
86         hours = src % 24;  src /= 24;
87         days = src % 7;    src /= 7;
88         weeks = src;       src = 0;
89         POST(src);
90
91         x = 0;
92         if (weeks != 0) {
93                 RETERR(ttlfmt(weeks, "week", verbose, ISC_TF(x > 0), target));
94                 x++;
95         }
96         if (days != 0) {
97                 RETERR(ttlfmt(days, "day", verbose, ISC_TF(x > 0), target));
98                 x++;
99         }
100         if (hours != 0) {
101                 RETERR(ttlfmt(hours, "hour", verbose, ISC_TF(x > 0), target));
102                 x++;
103         }
104         if (mins != 0) {
105                 RETERR(ttlfmt(mins, "minute", verbose, ISC_TF(x > 0), target));
106                 x++;
107         }
108         if (secs != 0 ||
109             (weeks == 0 && days == 0 && hours == 0 && mins == 0)) {
110                 RETERR(ttlfmt(secs, "second", verbose, ISC_TF(x > 0), target));
111                 x++;
112         }
113         INSIST (x > 0);
114         /*
115          * If only a single unit letter is printed, print it
116          * in upper case. (Why?  Because BIND 8 does that.
117          * Presumably it has a reason.)
118          */
119         if (x == 1 && !verbose) {
120                 isc_region_t region;
121                 /*
122                  * The unit letter is the last character in the
123                  * used region of the buffer.
124                  *
125                  * toupper() does not need its argument to be masked of cast
126                  * here because region.base is type unsigned char *.
127                  */
128                 isc_buffer_usedregion(target, &region);
129                 region.base[region.length - 1] =
130                         toupper(region.base[region.length - 1]);
131         }
132         return (ISC_R_SUCCESS);
133 }
134
135 isc_result_t
136 dns_counter_fromtext(isc_textregion_t *source, isc_uint32_t *ttl) {
137         return (bind_ttl(source, ttl));
138 }
139
140 isc_result_t
141 dns_ttl_fromtext(isc_textregion_t *source, isc_uint32_t *ttl) {
142         isc_result_t result;
143
144         result = bind_ttl(source, ttl);
145         if (result != ISC_R_SUCCESS)
146                 result = DNS_R_BADTTL;
147         return (result);
148 }
149
150 static isc_result_t
151 bind_ttl(isc_textregion_t *source, isc_uint32_t *ttl) {
152         isc_uint32_t tmp = 0;
153         isc_uint32_t n;
154         char *s;
155         char buf[64];
156         char nbuf[64]; /* Number buffer */
157
158         /*
159          * Copy the buffer as it may not be NULL terminated.
160          * No legal counter / ttl is longer that 63 characters.
161          */
162         if (source->length > sizeof(buf) - 1)
163                 return (DNS_R_SYNTAX);
164         strncpy(buf, source->base, source->length);
165         buf[source->length] = '\0';
166         s = buf;
167
168         do {
169                 isc_result_t result;
170
171                 char *np = nbuf;
172                 while (*s != '\0' && isdigit((unsigned char)*s))
173                         *np++ = *s++;
174                 *np++ = '\0';
175                 INSIST(np - nbuf <= (int)sizeof(nbuf));
176                 result = isc_parse_uint32(&n, nbuf, 10);
177                 if (result != ISC_R_SUCCESS)
178                         return (DNS_R_SYNTAX);
179                 switch (*s) {
180                 case 'w':
181                 case 'W':
182                         tmp += n * 7 * 24 * 3600;
183                         s++;
184                         break;
185                 case 'd':
186                 case 'D':
187                         tmp += n * 24 * 3600;
188                         s++;
189                         break;
190                 case 'h':
191                 case 'H':
192                         tmp += n * 3600;
193                         s++;
194                         break;
195                 case 'm':
196                 case 'M':
197                         tmp += n * 60;
198                         s++;
199                         break;
200                 case 's':
201                 case 'S':
202                         tmp += n;
203                         s++;
204                         break;
205                 case '\0':
206                         /* Plain number? */
207                         if (tmp != 0)
208                                 return (DNS_R_SYNTAX);
209                         tmp = n;
210                         break;
211                 default:
212                         return (DNS_R_SYNTAX);
213                 }
214         } while (*s != '\0');
215         *ttl = tmp;
216         return (ISC_R_SUCCESS);
217 }