1999-06-08 10:35:23 +00:00
|
|
|
/*
|
2016-06-27 14:56:38 +10:00
|
|
|
* Copyright (C) 1999-2001, 2004, 2005, 2007, 2011-2014, 2016 Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
1999-06-08 10:35:23 +00:00
|
|
|
*/
|
|
|
|
|
2012-03-08 00:21:15 +11:00
|
|
|
/* $Id$ */
|
2005-04-27 04:57:32 +00:00
|
|
|
|
|
|
|
/*! \file */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
1999-06-08 10:35:23 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
2001-11-26 23:51:21 +00:00
|
|
|
#include <errno.h>
|
2000-04-25 20:20:31 +00:00
|
|
|
#include <stdio.h>
|
2000-04-25 21:23:04 +00:00
|
|
|
#include <stdlib.h>
|
1999-06-08 10:35:23 +00:00
|
|
|
|
|
|
|
#include <isc/buffer.h>
|
2001-11-30 01:02:18 +00:00
|
|
|
#include <isc/parseint.h>
|
1999-11-02 13:07:53 +00:00
|
|
|
#include <isc/print.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/region.h>
|
|
|
|
#include <isc/string.h>
|
2000-04-28 01:12:23 +00:00
|
|
|
#include <isc/util.h>
|
1999-06-08 10:35:23 +00:00
|
|
|
|
|
|
|
#include <dns/result.h>
|
|
|
|
#include <dns/ttl.h>
|
|
|
|
|
|
|
|
#define RETERR(x) do { \
|
2000-05-16 05:19:47 +00:00
|
|
|
isc_result_t _r = (x); \
|
|
|
|
if (_r != ISC_R_SUCCESS) \
|
|
|
|
return (_r); \
|
1999-06-08 10:35:23 +00:00
|
|
|
} 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
|
|
|
|
2000-05-24 05:10:00 +00:00
|
|
|
/*
|
|
|
|
* Helper for dns_ttl_totext().
|
|
|
|
*/
|
1999-12-23 00:09:04 +00:00
|
|
|
static isc_result_t
|
2000-06-01 18:26:56 +00:00
|
|
|
ttlfmt(unsigned int t, const char *s, isc_boolean_t verbose,
|
1999-06-08 10:35:23 +00:00
|
|
|
isc_boolean_t space, isc_buffer_t *target)
|
|
|
|
{
|
|
|
|
char tmp[60];
|
2013-12-04 12:47:23 +11:00
|
|
|
unsigned int len;
|
1999-06-08 10:35:23 +00:00
|
|
|
isc_region_t region;
|
2000-06-01 18:26:56 +00:00
|
|
|
|
1999-06-08 10:35:23 +00:00
|
|
|
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]);
|
|
|
|
|
2001-11-12 19:05:39 +00:00
|
|
|
INSIST(len + 1 <= sizeof(tmp));
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_availableregion(target, ®ion);
|
1999-06-08 10:35:23 +00:00
|
|
|
if (len > region.length)
|
2000-04-06 22:03:35 +00:00
|
|
|
return (ISC_R_NOSPACE);
|
2014-01-08 16:27:10 -08:00
|
|
|
memmove(region.base, tmp, len);
|
1999-06-08 10:35:23 +00:00
|
|
|
isc_buffer_add(target, len);
|
2000-06-01 18:26:56 +00:00
|
|
|
|
2000-04-06 22:03:35 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-06-08 10:35:23 +00:00
|
|
|
}
|
|
|
|
|
2000-05-24 05:10:00 +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) {
|
2014-04-29 16:58:36 -07:00
|
|
|
return (dns_ttl_totext2(src, verbose, ISC_TRUE, target));
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
dns_ttl_totext2(isc_uint32_t src, isc_boolean_t verbose,
|
|
|
|
isc_boolean_t upcase, 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;
|
2011-03-11 06:11:27 +00:00
|
|
|
POST(src);
|
1999-06-08 10:35:23 +00:00
|
|
|
|
|
|
|
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++;
|
|
|
|
}
|
2000-05-24 05:10:00 +00:00
|
|
|
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.)
|
|
|
|
*/
|
2014-04-29 16:58:36 -07:00
|
|
|
if (x == 1 && upcase && !verbose) {
|
1999-06-08 10:35:23 +00:00
|
|
|
isc_region_t region;
|
|
|
|
/*
|
2000-08-01 01:33:37 +00:00
|
|
|
* The unit letter is the last character in the
|
1999-06-08 10:35:23 +00:00
|
|
|
* used region of the buffer.
|
2000-05-09 22:22:25 +00:00
|
|
|
*
|
|
|
|
* toupper() does not need its argument to be masked of cast
|
|
|
|
* here because region.base is type unsigned char *.
|
1999-06-08 10:35:23 +00:00
|
|
|
*/
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_usedregion(target, ®ion);
|
1999-06-08 10:35:23 +00:00
|
|
|
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);
|
2014-02-18 23:26:50 -08:00
|
|
|
if (result != ISC_R_SUCCESS && result != ISC_R_RANGE)
|
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) {
|
2014-02-18 23:26:50 -08:00
|
|
|
isc_uint64_t tmp = 0ULL;
|
2001-11-30 01:02:18 +00:00
|
|
|
isc_uint32_t n;
|
|
|
|
char *s;
|
1999-11-02 13:07:53 +00:00
|
|
|
char buf[64];
|
2001-11-30 01:02:18 +00:00
|
|
|
char nbuf[64]; /* Number buffer */
|
1999-11-02 13:07:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
2001-11-26 23:51:21 +00:00
|
|
|
return (DNS_R_SYNTAX);
|
1999-11-02 13:07:53 +00:00
|
|
|
strncpy(buf, source->base, source->length);
|
|
|
|
buf[source->length] = '\0';
|
|
|
|
s = buf;
|
|
|
|
|
|
|
|
do {
|
2001-11-30 01:02:18 +00:00
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
char *np = nbuf;
|
|
|
|
while (*s != '\0' && isdigit((unsigned char)*s))
|
|
|
|
*np++ = *s++;
|
|
|
|
*np++ = '\0';
|
|
|
|
INSIST(np - nbuf <= (int)sizeof(nbuf));
|
|
|
|
result = isc_parse_uint32(&n, nbuf, 10);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
1999-11-02 13:07:53 +00:00
|
|
|
return (DNS_R_SYNTAX);
|
2001-11-30 01:02:18 +00:00
|
|
|
switch (*s) {
|
1999-11-02 13:07:53 +00:00
|
|
|
case 'w':
|
|
|
|
case 'W':
|
2014-02-18 23:26:50 -08:00
|
|
|
tmp += (isc_uint64_t) n * 7 * 24 * 3600;
|
2001-11-30 01:02:18 +00:00
|
|
|
s++;
|
1999-11-02 13:07:53 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
case 'D':
|
2014-02-18 23:26:50 -08:00
|
|
|
tmp += (isc_uint64_t) n * 24 * 3600;
|
2001-11-30 01:02:18 +00:00
|
|
|
s++;
|
1999-11-02 13:07:53 +00:00
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
case 'H':
|
2014-02-18 23:26:50 -08:00
|
|
|
tmp += (isc_uint64_t) n * 3600;
|
2001-11-30 01:02:18 +00:00
|
|
|
s++;
|
1999-11-02 13:07:53 +00:00
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
case 'M':
|
2014-02-18 23:26:50 -08:00
|
|
|
tmp += (isc_uint64_t) n * 60;
|
2001-11-30 01:02:18 +00:00
|
|
|
s++;
|
1999-11-02 13:07:53 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
case 'S':
|
2014-02-18 23:26:50 -08:00
|
|
|
tmp += (isc_uint64_t) n;
|
2001-11-30 01:02:18 +00:00
|
|
|
s++;
|
1999-11-02 13:07:53 +00:00
|
|
|
break;
|
|
|
|
case '\0':
|
|
|
|
/* Plain number? */
|
2014-02-18 23:26:50 -08:00
|
|
|
if (tmp != 0ULL)
|
1999-11-02 13:07:53 +00:00
|
|
|
return (DNS_R_SYNTAX);
|
|
|
|
tmp = n;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (DNS_R_SYNTAX);
|
|
|
|
}
|
2001-11-30 01:02:18 +00:00
|
|
|
} while (*s != '\0');
|
2014-02-18 23:26:50 -08:00
|
|
|
|
|
|
|
if (tmp > 0xffffffffULL)
|
|
|
|
return (ISC_R_RANGE);
|
|
|
|
|
|
|
|
*ttl = (isc_uint32_t)(tmp & 0xffffffffUL);
|
2000-04-06 22:03:35 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-11-02 13:07:53 +00:00
|
|
|
}
|