bind - Removed version tag from contrib directory and updated README.DRAGONFLY.
[dragonfly.git] / contrib / bind / lib / dns / ttl.c
1 /*
2  * Copyright (C) 2004, 2005, 2007  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: ttl.c,v 1.29 2007/06/19 23:47:16 tbox Exp $ */
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
90         x = 0;
91         if (weeks != 0) {
92                 RETERR(ttlfmt(weeks, "week", verbose, ISC_TF(x > 0), target));
93                 x++;
94         }
95         if (days != 0) {
96                 RETERR(ttlfmt(days, "day", verbose, ISC_TF(x > 0), target));
97                 x++;
98         }
99         if (hours != 0) {
100                 RETERR(ttlfmt(hours, "hour", verbose, ISC_TF(x > 0), target));
101                 x++;
102         }
103         if (mins != 0) {
104                 RETERR(ttlfmt(mins, "minute", verbose, ISC_TF(x > 0), target));
105                 x++;
106         }
107         if (secs != 0 ||
108             (weeks == 0 && days == 0 && hours == 0 && mins == 0)) {
109                 RETERR(ttlfmt(secs, "second", verbose, ISC_TF(x > 0), target));
110                 x++;
111         }
112         INSIST (x > 0);
113         /*
114          * If only a single unit letter is printed, print it
115          * in upper case. (Why?  Because BIND 8 does that.
116          * Presumably it has a reason.)
117          */
118         if (x == 1 && !verbose) {
119                 isc_region_t region;
120                 /*
121                  * The unit letter is the last character in the
122                  * used region of the buffer.
123                  *
124                  * toupper() does not need its argument to be masked of cast
125                  * here because region.base is type unsigned char *.
126                  */
127                 isc_buffer_usedregion(target, &region);
128                 region.base[region.length - 1] =
129                         toupper(region.base[region.length - 1]);
130         }
131         return (ISC_R_SUCCESS);
132 }
133
134 isc_result_t
135 dns_counter_fromtext(isc_textregion_t *source, isc_uint32_t *ttl) {
136         return (bind_ttl(source, ttl));
137 }
138
139 isc_result_t
140 dns_ttl_fromtext(isc_textregion_t *source, isc_uint32_t *ttl) {
141         isc_result_t result;
142
143         result = bind_ttl(source, ttl);
144         if (result != ISC_R_SUCCESS)
145                 result = DNS_R_BADTTL;
146         return (result);
147 }
148
149 static isc_result_t
150 bind_ttl(isc_textregion_t *source, isc_uint32_t *ttl) {
151         isc_uint32_t tmp = 0;
152         isc_uint32_t n;
153         char *s;
154         char buf[64];
155         char nbuf[64]; /* Number buffer */
156
157         /*
158          * Copy the buffer as it may not be NULL terminated.
159          * No legal counter / ttl is longer that 63 characters.
160          */
161         if (source->length > sizeof(buf) - 1)
162                 return (DNS_R_SYNTAX);
163         strncpy(buf, source->base, source->length);
164         buf[source->length] = '\0';
165         s = buf;
166
167         do {
168                 isc_result_t result;
169
170                 char *np = nbuf;
171                 while (*s != '\0' && isdigit((unsigned char)*s))
172                         *np++ = *s++;
173                 *np++ = '\0';
174                 INSIST(np - nbuf <= (int)sizeof(nbuf));
175                 result = isc_parse_uint32(&n, nbuf, 10);
176                 if (result != ISC_R_SUCCESS)
177                         return (DNS_R_SYNTAX);
178                 switch (*s) {
179                 case 'w':
180                 case 'W':
181                         tmp += n * 7 * 24 * 3600;
182                         s++;
183                         break;
184                 case 'd':
185                 case 'D':
186                         tmp += n * 24 * 3600;
187                         s++;
188                         break;
189                 case 'h':
190                 case 'H':
191                         tmp += n * 3600;
192                         s++;
193                         break;
194                 case 'm':
195                 case 'M':
196                         tmp += n * 60;
197                         s++;
198                         break;
199                 case 's':
200                 case 'S':
201                         tmp += n;
202                         s++;
203                         break;
204                 case '\0':
205                         /* Plain number? */
206                         if (tmp != 0)
207                                 return (DNS_R_SYNTAX);
208                         tmp = n;
209                         break;
210                 default:
211                         return (DNS_R_SYNTAX);
212                 }
213         } while (*s != '\0');
214         *ttl = tmp;
215         return (ISC_R_SUCCESS);
216 }