1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 /* $Id: ttl.c,v 1.6 2020/09/14 08:40:43 florian Exp $ */
18
19 /*! \file */
20
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include <isc/buffer.h>
26 #include <isc/region.h>
27 #include <isc/util.h>
28
29 #include <dns/ttl.h>
30
31 #define RETERR(x) do { \
32 isc_result_t _r = (x); \
33 if (_r != ISC_R_SUCCESS) \
34 return (_r); \
35 } while (0)
36
37 /*
38 * Helper for dns_ttl_totext().
39 */
40 static isc_result_t
ttlfmt(unsigned int t,const char * s,int verbose,int space,isc_buffer_t * target)41 ttlfmt(unsigned int t, const char *s, int verbose,
42 int space, isc_buffer_t *target)
43 {
44 char tmp[60];
45 unsigned int len;
46 isc_region_t region;
47
48 if (verbose)
49 len = snprintf(tmp, sizeof(tmp), "%s%u %s%s",
50 space ? " " : "",
51 t, s,
52 t == 1 ? "" : "s");
53 else
54 len = snprintf(tmp, sizeof(tmp), "%u%c", t, s[0]);
55
56 INSIST(len + 1 <= sizeof(tmp));
57 isc_buffer_availableregion(target, ®ion);
58 if (len > region.length)
59 return (ISC_R_NOSPACE);
60 memmove(region.base, tmp, len);
61 isc_buffer_add(target, len);
62
63 return (ISC_R_SUCCESS);
64 }
65
66 /*
67 * Derived from bind8 ns_format_ttl().
68 */
69 isc_result_t
dns_ttl_totext(uint32_t src,int verbose,isc_buffer_t * target)70 dns_ttl_totext(uint32_t src, int verbose, isc_buffer_t *target) {
71 unsigned secs, mins, hours, days, weeks, x;
72
73 secs = src % 60; src /= 60;
74 mins = src % 60; src /= 60;
75 hours = src % 24; src /= 24;
76 days = src % 7; src /= 7;
77 weeks = src; src = 0;
78 POST(src);
79
80 x = 0;
81 if (weeks != 0) {
82 RETERR(ttlfmt(weeks, "week", verbose, (x > 0), target));
83 x++;
84 }
85 if (days != 0) {
86 RETERR(ttlfmt(days, "day", verbose, (x > 0), target));
87 x++;
88 }
89 if (hours != 0) {
90 RETERR(ttlfmt(hours, "hour", verbose, (x > 0), target));
91 x++;
92 }
93 if (mins != 0) {
94 RETERR(ttlfmt(mins, "minute", verbose, (x > 0), target));
95 x++;
96 }
97 if (secs != 0 ||
98 (weeks == 0 && days == 0 && hours == 0 && mins == 0)) {
99 RETERR(ttlfmt(secs, "second", verbose, (x > 0), target));
100 x++;
101 }
102 INSIST (x > 0);
103 /*
104 * If only a single unit letter is printed, print it
105 * in upper case. (Why? Because BIND 8 does that.
106 * Presumably it has a reason.)
107 */
108 if (x == 1 && !verbose) {
109 isc_region_t region;
110 /*
111 * The unit letter is the last character in the
112 * used region of the buffer.
113 *
114 * toupper() does not need its argument to be masked of cast
115 * here because region.base is type unsigned char *.
116 */
117 isc_buffer_usedregion(target, ®ion);
118 region.base[region.length - 1] =
119 toupper(region.base[region.length - 1]);
120 }
121 return (ISC_R_SUCCESS);
122 }
123