1999-06-08 10:35:23 +00:00
|
|
|
/*
|
2000-02-03 23:50:32 +00:00
|
|
|
* Copyright (C) 1999, 2000 Internet Software Consortium.
|
1999-06-08 10:35:23 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
2000-04-25 20:20:31 +00:00
|
|
|
#include <stdio.h>
|
1999-10-29 06:36:05 +00:00
|
|
|
#include <string.h>
|
1999-06-08 10:35:23 +00:00
|
|
|
|
|
|
|
#include <isc/assertions.h>
|
|
|
|
#include <isc/error.h>
|
|
|
|
#include <isc/boolean.h>
|
|
|
|
#include <isc/region.h>
|
|
|
|
#include <isc/buffer.h>
|
1999-11-02 13:07:53 +00:00
|
|
|
#include <isc/print.h>
|
1999-06-08 10:35:23 +00:00
|
|
|
|
|
|
|
#include <dns/result.h>
|
|
|
|
#include <dns/ttl.h>
|
|
|
|
|
|
|
|
#define RETERR(x) do { \
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t __r = (x); \
|
2000-04-06 22:03:35 +00:00
|
|
|
if (__r != ISC_R_SUCCESS) \
|
1999-06-08 10:35:23 +00:00
|
|
|
return (__r); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
1999-12-23 00:09:04 +00:00
|
|
|
static isc_result_t bind_ttl(isc_textregion_t *source, isc_uint32_t *ttl);
|
1999-11-02 13:07:53 +00:00
|
|
|
|
1999-06-08 10:35:23 +00:00
|
|
|
/* Helper for dns_ttl_totext(). */
|
|
|
|
|
1999-12-23 00:09:04 +00:00
|
|
|
static isc_result_t
|
1999-06-08 10:35:23 +00:00
|
|
|
ttlfmt(unsigned int t, 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)
|
1999-11-02 13:07:53 +00:00
|
|
|
len = snprintf(tmp, sizeof(tmp), "%s%u %s%s",
|
|
|
|
space ? " " : "",
|
|
|
|
t, s,
|
|
|
|
t == 1 ? "" : "s");
|
1999-06-08 10:35:23 +00:00
|
|
|
else
|
2000-04-25 20:20:31 +00:00
|
|
|
len = snprintf(tmp, sizeof(tmp), "%u%c", t, s[0]);
|
|
|
|
|
1999-06-08 10:35:23 +00:00
|
|
|
INSIST(len + 1 <= sizeof tmp);
|
|
|
|
isc_buffer_available(target, ®ion);
|
|
|
|
if (len > region.length)
|
2000-04-06 22:03:35 +00:00
|
|
|
return (ISC_R_NOSPACE);
|
1999-06-08 10:35:23 +00:00
|
|
|
memcpy(region.base, tmp, len);
|
|
|
|
isc_buffer_add(target, len);
|
2000-04-06 22:03:35 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-06-08 10:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Derived from bind8 ns_format_ttl(). */
|
|
|
|
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t
|
2000-04-25 20:20:31 +00:00
|
|
|
dns_ttl_totext(isc_uint32_t src, isc_boolean_t verbose, isc_buffer_t *target) {
|
1999-06-08 10:35:23 +00:00
|
|
|
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) {
|
1999-10-29 12:56:58 +00:00
|
|
|
RETERR(ttlfmt(weeks, "week", verbose, ISC_TF(x > 0), target));
|
1999-06-08 10:35:23 +00:00
|
|
|
x++;
|
|
|
|
}
|
|
|
|
if (days != 0) {
|
1999-10-29 12:56:58 +00:00
|
|
|
RETERR(ttlfmt(days, "day", verbose, ISC_TF(x > 0), target));
|
1999-06-08 10:35:23 +00:00
|
|
|
x++;
|
|
|
|
}
|
|
|
|
if (hours != 0) {
|
1999-10-29 12:56:58 +00:00
|
|
|
RETERR(ttlfmt(hours, "hour", verbose, ISC_TF(x > 0), target));
|
1999-06-08 10:35:23 +00:00
|
|
|
x++;
|
|
|
|
}
|
|
|
|
if (mins != 0) {
|
1999-10-29 12:56:58 +00:00
|
|
|
RETERR(ttlfmt(mins, "minute", verbose, ISC_TF(x > 0), target));
|
1999-06-08 10:35:23 +00:00
|
|
|
x++;
|
|
|
|
}
|
|
|
|
if (secs != 0 || (weeks == 0 && days == 0 && hours == 0 && mins == 0)) {
|
1999-10-29 12:56:58 +00:00
|
|
|
RETERR(ttlfmt(secs, "second", verbose, ISC_TF(x > 0), target));
|
1999-06-08 10:35:23 +00:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
isc_buffer_used(target, ®ion);
|
|
|
|
region.base[region.length - 1] =
|
|
|
|
toupper(region.base[region.length - 1]);
|
|
|
|
}
|
2000-04-06 22:03:35 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-06-08 10:35:23 +00:00
|
|
|
}
|
|
|
|
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t
|
1999-11-02 13:07:53 +00:00
|
|
|
dns_counter_fromtext(isc_textregion_t *source, isc_uint32_t *ttl) {
|
|
|
|
return (bind_ttl(source, ttl));
|
|
|
|
}
|
|
|
|
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t
|
1999-11-02 13:07:53 +00:00
|
|
|
dns_ttl_fromtext(isc_textregion_t *source, isc_uint32_t *ttl) {
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t result;
|
1999-11-04 01:21:28 +00:00
|
|
|
|
|
|
|
result = bind_ttl(source, ttl);
|
2000-04-06 22:03:35 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
1999-11-04 01:21:28 +00:00
|
|
|
result = DNS_R_BADTTL;
|
|
|
|
return (result);
|
1999-11-02 13:07:53 +00:00
|
|
|
}
|
|
|
|
|
1999-12-23 00:09:04 +00:00
|
|
|
static isc_result_t
|
1999-11-02 13:07:53 +00:00
|
|
|
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;
|
2000-04-06 22:03:35 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-11-02 13:07:53 +00:00
|
|
|
}
|