1999-06-08 10:35:23 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) 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/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
1999-06-08 10:35:23 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|
1999-06-08 10:35:23 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <ctype.h>
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <stdio.h>
|
1999-06-08 10:35:23 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
2004-08-28 06:20:14 +00:00
|
|
|
#include <isc/print.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/region.h>
|
2011-03-09 07:22:32 +00:00
|
|
|
#include <isc/serial.h>
|
2001-05-15 22:05:35 +00:00
|
|
|
#include <isc/stdtime.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <isc/string.h> /* Required for HP/UX (and others?) */
|
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/time.h>
|
|
|
|
|
2014-04-24 13:15:40 +10:00
|
|
|
static const int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
1999-06-08 10:35:23 +00:00
|
|
|
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_time64_totext(int64_t t, isc_buffer_t *target)
|
|
|
|
{
|
1999-06-08 10:35:23 +00:00
|
|
|
struct tm tm;
|
2020-02-12 13:59:18 +01:00
|
|
|
char buf[sizeof("!!!!!!YYYY!!!!!!!!MM!!!!!!!!DD!!!!!!!!HH!!!!!!!!MM!!!!"
|
|
|
|
"!!!!SS")];
|
|
|
|
int secs;
|
1999-06-08 10:35:23 +00:00
|
|
|
unsigned int l;
|
|
|
|
isc_region_t region;
|
|
|
|
|
2011-03-09 07:22:32 +00:00
|
|
|
/*
|
|
|
|
* Warning. Do NOT use arguments with side effects with these macros.
|
|
|
|
*/
|
1999-06-08 10:35:23 +00:00
|
|
|
#define is_leap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
|
2020-02-12 13:59:18 +01:00
|
|
|
#define year_secs(y) ((is_leap(y) ? 366 : 365) * 86400)
|
|
|
|
#define month_secs(m, y) ((days[m] + ((m == 1 && is_leap(y)) ? 1 : 0)) * 86400)
|
1999-06-08 10:35:23 +00:00
|
|
|
|
|
|
|
tm.tm_year = 70;
|
2011-03-09 07:22:32 +00:00
|
|
|
while (t < 0) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (tm.tm_year == 0) {
|
2011-03-09 07:22:32 +00:00
|
|
|
return (ISC_R_RANGE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2011-03-09 07:22:32 +00:00
|
|
|
tm.tm_year--;
|
|
|
|
secs = year_secs(tm.tm_year + 1900);
|
|
|
|
t += secs;
|
|
|
|
}
|
1999-06-08 10:35:23 +00:00
|
|
|
while ((secs = year_secs(tm.tm_year + 1900)) <= t) {
|
|
|
|
t -= secs;
|
|
|
|
tm.tm_year++;
|
2020-02-13 21:48:23 +01:00
|
|
|
if (tm.tm_year + 1900 > 9999) {
|
2000-05-15 21:14:38 +00:00
|
|
|
return (ISC_R_RANGE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-06-08 10:35:23 +00:00
|
|
|
}
|
|
|
|
tm.tm_mon = 0;
|
|
|
|
while ((secs = month_secs(tm.tm_mon, tm.tm_year + 1900)) <= t) {
|
|
|
|
t -= secs;
|
|
|
|
tm.tm_mon++;
|
|
|
|
}
|
|
|
|
tm.tm_mday = 1;
|
|
|
|
while (86400 <= t) {
|
|
|
|
t -= 86400;
|
|
|
|
tm.tm_mday++;
|
|
|
|
}
|
|
|
|
tm.tm_hour = 0;
|
|
|
|
while (3600 <= t) {
|
|
|
|
t -= 3600;
|
|
|
|
tm.tm_hour++;
|
|
|
|
}
|
|
|
|
tm.tm_min = 0;
|
|
|
|
while (60 <= t) {
|
|
|
|
t -= 60;
|
|
|
|
tm.tm_min++;
|
|
|
|
}
|
1999-10-08 23:58:07 +00:00
|
|
|
tm.tm_sec = (int)t;
|
2020-02-12 13:59:18 +01:00
|
|
|
/* yyyy mm dd HH MM SS */
|
2003-04-11 07:25:31 +00:00
|
|
|
snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02d",
|
2020-02-12 13:59:18 +01:00
|
|
|
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
|
|
|
|
tm.tm_min, tm.tm_sec);
|
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_availableregion(target, ®ion);
|
1999-06-08 10:35:23 +00:00
|
|
|
l = strlen(buf);
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (l > region.length) {
|
2000-04-06 22:03:35 +00:00
|
|
|
return (ISC_R_NOSPACE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-06-08 10:35:23 +00:00
|
|
|
|
2014-01-08 16:27:10 -08:00
|
|
|
memmove(region.base, buf, l);
|
1999-06-08 10:35:23 +00:00
|
|
|
isc_buffer_add(target, l);
|
2000-04-06 22:03:35 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-06-08 10:35:23 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
int64_t
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_time64_from32(uint32_t value)
|
|
|
|
{
|
2001-05-15 22:05:35 +00:00
|
|
|
isc_stdtime_t now;
|
2020-02-12 13:59:18 +01:00
|
|
|
int64_t start;
|
|
|
|
int64_t t;
|
1999-06-08 10:35:23 +00:00
|
|
|
|
2000-05-08 14:38:29 +00:00
|
|
|
/*
|
2001-05-15 22:05:35 +00:00
|
|
|
* Adjust the time to the closest epoch. This should be changed
|
|
|
|
* to use a 64-bit counterpart to isc_stdtime_get() if one ever
|
|
|
|
* is defined, but even the current code is good until the year
|
|
|
|
* 2106.
|
2000-05-08 14:38:29 +00:00
|
|
|
*/
|
2001-05-15 22:05:35 +00:00
|
|
|
isc_stdtime_get(&now);
|
2020-02-12 13:59:18 +01:00
|
|
|
start = (int64_t)now;
|
2020-02-13 21:48:23 +01:00
|
|
|
if (isc_serial_gt(value, now)) {
|
2011-03-09 07:22:32 +00:00
|
|
|
t = start + (value - now);
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2011-03-09 07:22:32 +00:00
|
|
|
t = start - (now - value);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-01-27 01:43:53 +00:00
|
|
|
|
|
|
|
return (t);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_time32_totext(uint32_t value, isc_buffer_t *target)
|
|
|
|
{
|
2012-01-27 01:43:53 +00:00
|
|
|
return (dns_time64_totext(dns_time64_from32(value), target));
|
1999-06-08 10:35:23 +00:00
|
|
|
}
|
|
|
|
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_time64_fromtext(const char *source, int64_t *target)
|
|
|
|
{
|
|
|
|
int year, month, day, hour, minute, second;
|
2018-03-28 14:19:37 +02:00
|
|
|
int64_t value;
|
2020-02-12 13:59:18 +01:00
|
|
|
int secs;
|
|
|
|
int i;
|
1999-06-08 10:35:23 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define RANGE(min, max, value) \
|
|
|
|
do { \
|
1999-06-08 10:35:23 +00:00
|
|
|
if (value < (min) || value > (max)) \
|
2020-02-13 21:48:23 +01:00
|
|
|
return ((ISC_R_RANGE)); \
|
1999-06-08 10:35:23 +00:00
|
|
|
} while (0)
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (strlen(source) != 14U) {
|
1999-06-08 10:35:23 +00:00
|
|
|
return (DNS_R_SYNTAX);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2010-04-21 02:21:31 +00:00
|
|
|
/*
|
|
|
|
* Confirm the source only consists digits. sscanf() allows some
|
|
|
|
* minor exceptions.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < 14; i++) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (!isdigit((unsigned char)source[i])) {
|
2010-04-21 02:21:31 +00:00
|
|
|
return (DNS_R_SYNTAX);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2010-04-21 02:21:31 +00:00
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
if (sscanf(source, "%4d%2d%2d%2d%2d%2d", &year, &month, &day, &hour,
|
2020-02-13 21:48:23 +01:00
|
|
|
&minute, &second) != 6) {
|
1999-06-08 10:35:23 +00:00
|
|
|
return (DNS_R_SYNTAX);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-06-08 10:35:23 +00:00
|
|
|
|
2011-03-09 07:22:32 +00:00
|
|
|
RANGE(0, 9999, year);
|
1999-06-08 10:35:23 +00:00
|
|
|
RANGE(1, 12, month);
|
2020-02-12 13:59:18 +01:00
|
|
|
RANGE(1, days[month - 1] + ((month == 2 && is_leap(year)) ? 1 : 0),
|
|
|
|
day);
|
2014-07-02 15:28:02 +10:00
|
|
|
#ifdef __COVERITY__
|
|
|
|
/*
|
|
|
|
* Use a simplified range to silence Coverity warning (in
|
|
|
|
* arithmetic with day below).
|
|
|
|
*/
|
|
|
|
RANGE(1, 31, day);
|
|
|
|
#endif /* __COVERITY__ */
|
|
|
|
|
1999-06-08 10:35:23 +00:00
|
|
|
RANGE(0, 23, hour);
|
|
|
|
RANGE(0, 59, minute);
|
2020-02-12 13:59:18 +01:00
|
|
|
RANGE(0, 60, second); /* 60 == leap second. */
|
1999-06-08 10:35:23 +00:00
|
|
|
|
2000-05-08 14:38:29 +00:00
|
|
|
/*
|
2011-03-09 07:22:32 +00:00
|
|
|
* Calculate seconds from epoch.
|
|
|
|
* Note: this uses a idealized calendar.
|
2000-05-08 14:38:29 +00:00
|
|
|
*/
|
1999-06-08 10:35:23 +00:00
|
|
|
value = second + (60 * minute) + (3600 * hour) + ((day - 1) * 86400);
|
2020-02-13 21:48:23 +01:00
|
|
|
for (i = 0; i < (month - 1); i++) {
|
1999-06-08 10:35:23 +00:00
|
|
|
value += days[i] * 86400;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (is_leap(year) && month > 2) {
|
1999-06-08 10:35:23 +00:00
|
|
|
value += 86400;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2011-03-09 07:22:32 +00:00
|
|
|
if (year < 1970) {
|
|
|
|
for (i = 1969; i >= year; i--) {
|
|
|
|
secs = (is_leap(i) ? 366 : 365) * 86400;
|
|
|
|
value -= secs;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 1970; i < year; i++) {
|
|
|
|
secs = (is_leap(i) ? 366 : 365) * 86400;
|
|
|
|
value += secs;
|
|
|
|
}
|
1999-06-08 10:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*target = value;
|
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
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_time32_fromtext(const char *source, uint32_t *target)
|
|
|
|
{
|
|
|
|
int64_t value64;
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t result;
|
1999-06-08 10:35:23 +00:00
|
|
|
result = dns_time64_fromtext(source, &value64);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
1999-06-08 10:35:23 +00:00
|
|
|
return (result);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2018-03-28 14:19:37 +02:00
|
|
|
*target = (uint32_t)value64;
|
1999-06-08 10:35:23 +00:00
|
|
|
|
2000-05-15 21:14:38 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-06-08 10:35:23 +00:00
|
|
|
}
|