ttl.c revision f3ca27e9fe307b55e35ea8d7b37351650630e5a3
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 1999-2001 Internet Software Consortium.
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Permission to use, copy, modify, and distribute this software for any
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * purpose with or without fee is hereby granted, provided that the above
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * copyright notice and this permission notice appear in all copies.
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt *
19c7b1a0293498a3e36692c59646ed6e15ffc8d0Tinderbox User * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
6715db6593ce9a271ac3131cd7a886feaa386a2eEvan Hunt * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt */
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt/* $Id: ttl.c,v 1.22 2001/11/12 19:05:34 gson Exp $ */
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt#include <config.h>
be0982e9e4b0631670a4c449ddf8a2bfaf04eb92Evan Hunt
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt#include <ctype.h>
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt#include <stdio.h>
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt#include <stdlib.h>
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt#include <isc/buffer.h>
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt#include <isc/print.h>
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt#include <isc/region.h>
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt#include <isc/string.h>
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt#include <isc/util.h>
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt#include <dns/result.h>
9f40a783228eed48531756a6ab510d1e9c51648cTinderbox User#include <dns/ttl.h>
938440694b33cd752e9e4b71a526368b4811c177Tinderbox User
19c7b1a0293498a3e36692c59646ed6e15ffc8d0Tinderbox User#define RETERR(x) do { \
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews isc_result_t _r = (x); \
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt if (_r != ISC_R_SUCCESS) \
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt return (_r); \
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt } while (0)
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Huntstatic isc_result_t bind_ttl(isc_textregion_t *source, isc_uint32_t *ttl);
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt/*
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt * Helper for dns_ttl_totext().
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt */
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Huntstatic isc_result_t
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Huntttlfmt(unsigned int t, const char *s, isc_boolean_t verbose,
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt isc_boolean_t space, isc_buffer_t *target)
30eec077db2bdcb6f2a0dc388a3cdde2ede75ec1Mark Andrews{
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt char tmp[60];
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt size_t len;
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt isc_region_t region;
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt if (verbose)
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt len = snprintf(tmp, sizeof(tmp), "%s%u %s%s",
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt space ? " " : "",
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt t, s,
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt t == 1 ? "" : "s");
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt else
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt len = snprintf(tmp, sizeof(tmp), "%u%c", t, s[0]);
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt INSIST(len + 1 <= sizeof(tmp));
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt isc_buffer_availableregion(target, &region);
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt if (len > region.length)
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt return (ISC_R_NOSPACE);
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt memcpy(region.base, tmp, len);
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt isc_buffer_add(target, len);
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt return (ISC_R_SUCCESS);
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt}
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt/*
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt * Derived from bind8 ns_format_ttl().
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt */
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Huntisc_result_t
30eec077db2bdcb6f2a0dc388a3cdde2ede75ec1Mark Andrewsdns_ttl_totext(isc_uint32_t src, isc_boolean_t verbose, isc_buffer_t *target) {
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt unsigned secs, mins, hours, days, weeks, x;
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt secs = src % 60; src /= 60;
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt mins = src % 60; src /= 60;
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt hours = src % 24; src /= 24;
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt days = src % 7; src /= 7;
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt weeks = src; src = 0;
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt x = 0;
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt if (weeks != 0) {
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt RETERR(ttlfmt(weeks, "week", verbose, ISC_TF(x > 0), target));
30eec077db2bdcb6f2a0dc388a3cdde2ede75ec1Mark Andrews x++;
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt }
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt if (days != 0) {
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt RETERR(ttlfmt(days, "day", verbose, ISC_TF(x > 0), target));
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt x++;
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt }
0f66aced2640d964aeb6db41210711ba0640d7f2Evan Hunt if (hours != 0) {
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt 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);
}