summaryrefslogtreecommitdiff
path: root/usr.sbin/bind/lib/dns/ttl.c
blob: c771563aa51f1bd0a88cfd1175b00070be7eca79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright (C) 1999-2001  Internet Software Consortium.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/* $ISC: ttl.c,v 1.21 2001/01/09 21:51:41 bwelling Exp $ */

#include <config.h>

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

#include <isc/buffer.h>
#include <isc/print.h>
#include <isc/region.h>
#include <isc/string.h>
#include <isc/util.h>

#include <dns/result.h>
#include <dns/ttl.h>

#define RETERR(x) do { \
	isc_result_t _r = (x); \
	if (_r != ISC_R_SUCCESS) \
		return (_r); \
	} while (0)


static isc_result_t bind_ttl(isc_textregion_t *source, isc_uint32_t *ttl);

/*
 * Helper for dns_ttl_totext().
 */
static isc_result_t
ttlfmt(unsigned int t, const char *s, isc_boolean_t verbose,
       isc_boolean_t space, isc_buffer_t *target)
{
	char tmp[60];
	size_t len;
	isc_region_t region;

	if (verbose)
		len = snprintf(tmp, sizeof(tmp), "%s%u %s%s",
			       space ? " " : "",
			       t, s,
			       t == 1 ? "" : "s");
	else
		len = snprintf(tmp, sizeof(tmp), "%u%c", t, s[0]);

	INSIST(len + 1 <= sizeof tmp);
	isc_buffer_availableregion(target, &region);
	if (len > region.length)
		return (ISC_R_NOSPACE);
	memcpy(region.base, tmp, len);
	isc_buffer_add(target, len);

	return (ISC_R_SUCCESS);
}

/*
 * Derived from bind8 ns_format_ttl().
 */
isc_result_t
dns_ttl_totext(isc_uint32_t src, isc_boolean_t verbose, isc_buffer_t *target) {
	unsigned secs, mins, hours, days, weeks, x;

	secs = src % 60;   src /= 60;
	mins = src % 60;   src /= 60;
	hours = src % 24;  src /= 24;
	days = src % 7;    src /= 7;
	weeks = src;       src = 0;

	x = 0;
	if (weeks != 0) {
		RETERR(ttlfmt(weeks, "week", verbose, ISC_TF(x > 0), target));
		x++;
	}
	if (days != 0) {
		RETERR(ttlfmt(days, "day", verbose, ISC_TF(x > 0), target));
		x++;
	}
	if (hours != 0) {
		RETERR(ttlfmt(hours, "hour", verbose, ISC_TF(x > 0), target));
		x++;
	}
	if (mins != 0) {
		RETERR(ttlfmt(mins, "minute", verbose, ISC_TF(x > 0), target));
		x++;
	}
	if (secs != 0 ||
	    (weeks == 0 && days == 0 && hours == 0 && mins == 0)) {
		RETERR(ttlfmt(secs, "second", verbose, ISC_TF(x > 0), target));
		x++;
	}
	INSIST (x > 0);
	/*
	 * If only a single unit letter is printed, print it
	 * in upper case. (Why?  Because BIND 8 does that.
	 * Presumably it has a reason.)
	 */
	if (x == 1 && !verbose) {
		isc_region_t region;
		/*
		 * The unit letter is the last character in the
		 * used region of the buffer.
		 *
		 * toupper() does not need its argument to be masked of cast
		 * here because region.base is type unsigned char *.
		 */
		isc_buffer_usedregion(target, &region);
		region.base[region.length - 1] =
			toupper(region.base[region.length - 1]);
	}
	return (ISC_R_SUCCESS);
}

isc_result_t
dns_counter_fromtext(isc_textregion_t *source, isc_uint32_t *ttl) {
	return (bind_ttl(source, ttl));
}

isc_result_t
dns_ttl_fromtext(isc_textregion_t *source, isc_uint32_t *ttl) {
	isc_result_t result;

	result = bind_ttl(source, ttl);
	if (result != ISC_R_SUCCESS)
		result = DNS_R_BADTTL;
	return (result);
}

static isc_result_t
bind_ttl(isc_textregion_t *source, isc_uint32_t *ttl) {
	isc_uint32_t tmp = 0;
	unsigned long n;
	char *e, *s;
	char buf[64];

	/*
	 * Copy the buffer as it may not be NULL terminated.
	 * No legal counter / ttl is longer that 63 characters.
	 */
	if (source->length > sizeof(buf) - 1)
		return(DNS_R_SYNTAX);
	strncpy(buf, source->base, source->length);
	buf[source->length] = '\0';
	s = buf;

	do {
		n = strtoul(s, &e, 10);
		if (s == e)
			return (DNS_R_SYNTAX);
		switch (*e) {
		case 'w':
		case 'W':
			tmp += n * 7 * 24 * 3600;
			s = e + 1;
			break;
		case 'd':
		case 'D':
			tmp += n * 24 * 3600;
			s = e + 1;
			break;
		case 'h':
		case 'H':
			tmp += n * 3600;
			s = e + 1;
			break;
		case 'm':
		case 'M':
			tmp += n * 60;
			s = e + 1;
			break;
		case 's':
		case 'S':
			tmp += n;
			s = e + 1;
			break;
		case '\0':
			/* Plain number? */
			if (tmp != 0)
				return (DNS_R_SYNTAX);
			tmp = n;
			s = e;
			break;
		default:
			return (DNS_R_SYNTAX);
		}
	} while (*s != 0);
	*ttl = tmp;
	return (ISC_R_SUCCESS);
}