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