Coverage Report

Created: 2026-07-30 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/bind9/lib/dns/ttl.c
Line
Count
Source
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
 * SPDX-License-Identifier: MPL-2.0
5
 *
6
 * This Source Code Form is subject to the terms of the Mozilla Public
7
 * License, v. 2.0. If a copy of the MPL was not distributed with this
8
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9
 *
10
 * See the COPYRIGHT file distributed with this work for additional
11
 * information regarding copyright ownership.
12
 */
13
14
/*! \file */
15
16
#include <ctype.h>
17
#include <errno.h>
18
#include <inttypes.h>
19
#include <stdbool.h>
20
#include <stdio.h>
21
#include <stdlib.h>
22
23
#include <isc/ascii.h>
24
#include <isc/buffer.h>
25
#include <isc/parseint.h>
26
#include <isc/region.h>
27
#include <isc/result.h>
28
#include <isc/string.h>
29
#include <isc/util.h>
30
31
#include <dns/ttl.h>
32
33
static isc_result_t
34
bind_ttl(isc_textregion_t *source, uint32_t *ttl);
35
36
/*
37
 * Helper for dns_ttl_totext().
38
 */
39
static isc_result_t
40
ttlfmt(unsigned int t, const char *s, bool verbose, bool space,
41
0
       isc_buffer_t *target) {
42
0
  char tmp[60];
43
0
  unsigned int len;
44
0
  isc_region_t region;
45
46
0
  if (verbose) {
47
0
    len = snprintf(tmp, sizeof(tmp), "%s%u %s%s", space ? " " : "",
48
0
             t, s, t == 1 ? "" : "s");
49
0
  } else {
50
0
    len = snprintf(tmp, sizeof(tmp), "%u%c", t, s[0]);
51
0
  }
52
53
0
  INSIST(len + 1 <= sizeof(tmp));
54
0
  isc_buffer_availableregion(target, &region);
55
0
  if (len > region.length) {
56
0
    return ISC_R_NOSPACE;
57
0
  }
58
0
  memmove(region.base, tmp, len);
59
0
  isc_buffer_add(target, len);
60
61
0
  return ISC_R_SUCCESS;
62
0
}
63
64
/*
65
 * Derived from bind8 ns_format_ttl().
66
 */
67
isc_result_t
68
0
dns_ttl_totext(uint32_t src, bool verbose, bool upcase, isc_buffer_t *target) {
69
0
  unsigned int secs, mins, hours, days, weeks, x;
70
71
0
  secs = src % 60;
72
0
  src /= 60;
73
0
  mins = src % 60;
74
0
  src /= 60;
75
0
  hours = src % 24;
76
0
  src /= 24;
77
0
  days = src % 7;
78
0
  src /= 7;
79
0
  weeks = src;
80
0
  src = 0;
81
0
  POST(src);
82
83
0
  x = 0;
84
0
  if (weeks != 0) {
85
0
    RETERR(ttlfmt(weeks, "week", verbose, x > 0, target));
86
0
    x++;
87
0
  }
88
0
  if (days != 0) {
89
0
    RETERR(ttlfmt(days, "day", verbose, x > 0, target));
90
0
    x++;
91
0
  }
92
0
  if (hours != 0) {
93
0
    RETERR(ttlfmt(hours, "hour", verbose, x > 0, target));
94
0
    x++;
95
0
  }
96
0
  if (mins != 0) {
97
0
    RETERR(ttlfmt(mins, "minute", verbose, x > 0, target));
98
0
    x++;
99
0
  }
100
0
  if (secs != 0 || (weeks == 0 && days == 0 && hours == 0 && mins == 0)) {
101
0
    RETERR(ttlfmt(secs, "second", verbose, x > 0, target));
102
0
    x++;
103
0
  }
104
0
  INSIST(x > 0);
105
  /*
106
   * If only a single unit letter is printed, print it
107
   * in upper case. (Why?  Because BIND 8 does that.
108
   * Presumably it has a reason.)
109
   */
110
0
  if (x == 1 && upcase && !verbose) {
111
0
    isc_region_t region;
112
    /*
113
     * The unit letter is the last character in the
114
     * used region of the buffer.
115
     */
116
0
    isc_buffer_usedregion(target, &region);
117
0
    region.base[region.length - 1] =
118
0
      isc_ascii_toupper(region.base[region.length - 1]);
119
0
  }
120
0
  return ISC_R_SUCCESS;
121
0
}
122
123
isc_result_t
124
0
dns_counter_fromtext(isc_textregion_t *source, uint32_t *ttl) {
125
0
  return bind_ttl(source, ttl);
126
0
}
127
128
isc_result_t
129
0
dns_ttl_fromtext(isc_textregion_t *source, uint32_t *ttl) {
130
0
  isc_result_t result;
131
132
0
  result = bind_ttl(source, ttl);
133
0
  if (result != ISC_R_SUCCESS && result != ISC_R_RANGE) {
134
0
    result = DNS_R_BADTTL;
135
0
  }
136
0
  return result;
137
0
}
138
139
static isc_result_t
140
0
bind_ttl(isc_textregion_t *source, uint32_t *ttl) {
141
0
  uint64_t tmp = 0ULL;
142
0
  uint32_t n;
143
0
  char *s;
144
0
  char buf[64];
145
0
  char nbuf[64]; /* Number buffer */
146
147
  /*
148
   * Copy the buffer as it may not be NULL terminated.
149
   * No legal counter / ttl is longer that 63 characters.
150
   */
151
0
  if (source->length > sizeof(buf) - 1) {
152
0
    return DNS_R_SYNTAX;
153
0
  }
154
  /* Copy source->length bytes and NUL terminate. */
155
0
  snprintf(buf, sizeof(buf), "%.*s", (int)source->length, source->base);
156
0
  s = buf;
157
158
0
  do {
159
0
    isc_result_t result;
160
161
0
    char *np = nbuf;
162
0
    while (*s != '\0' && isdigit((unsigned char)*s)) {
163
0
      *np++ = *s++;
164
0
    }
165
0
    *np++ = '\0';
166
0
    INSIST(np - nbuf <= (int)sizeof(nbuf));
167
0
    result = isc_parse_uint32(&n, nbuf, 10);
168
0
    if (result != ISC_R_SUCCESS) {
169
0
      return DNS_R_SYNTAX;
170
0
    }
171
0
    switch (*s) {
172
0
    case 'w':
173
0
    case 'W':
174
0
      tmp += (uint64_t)n * 7 * 24 * 3600;
175
0
      s++;
176
0
      break;
177
0
    case 'd':
178
0
    case 'D':
179
0
      tmp += (uint64_t)n * 24 * 3600;
180
0
      s++;
181
0
      break;
182
0
    case 'h':
183
0
    case 'H':
184
0
      tmp += (uint64_t)n * 3600;
185
0
      s++;
186
0
      break;
187
0
    case 'm':
188
0
    case 'M':
189
0
      tmp += (uint64_t)n * 60;
190
0
      s++;
191
0
      break;
192
0
    case 's':
193
0
    case 'S':
194
0
      tmp += (uint64_t)n;
195
0
      s++;
196
0
      break;
197
0
    case '\0':
198
      /* Plain number? */
199
0
      if (tmp != 0ULL) {
200
0
        return DNS_R_SYNTAX;
201
0
      }
202
0
      tmp = n;
203
0
      break;
204
0
    default:
205
0
      return DNS_R_SYNTAX;
206
0
    }
207
0
  } while (*s != '\0');
208
209
0
  if (tmp > 0xffffffffULL) {
210
0
    return ISC_R_RANGE;
211
0
  }
212
213
0
  *ttl = (uint32_t)(tmp & 0xffffffffUL);
214
0
  return ISC_R_SUCCESS;
215
0
}