00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <config.h>
00023
00024 #include <ctype.h>
00025 #include <errno.h>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028
00029 #include <isc/buffer.h>
00030 #include <isc/parseint.h>
00031 #include <isc/print.h>
00032 #include <isc/region.h>
00033 #include <isc/string.h>
00034 #include <isc/util.h>
00035
00036 #include <dns/result.h>
00037 #include <dns/ttl.h>
00038
00039 #define RETERR(x) do { \
00040 isc_result_t _r = (x); \
00041 if (_r != ISC_R_SUCCESS) \
00042 return (_r); \
00043 } while (0)
00044
00045
00046 static isc_result_t bind_ttl(isc_textregion_t *source, isc_uint32_t *ttl);
00047
00048
00049
00050
00051 static isc_result_t
00052 ttlfmt(unsigned int t, const char *s, isc_boolean_t verbose,
00053 isc_boolean_t space, isc_buffer_t *target)
00054 {
00055 char tmp[60];
00056 unsigned int len;
00057 isc_region_t region;
00058
00059 if (verbose)
00060 len = snprintf(tmp, sizeof(tmp), "%s%u %s%s",
00061 space ? " " : "",
00062 t, s,
00063 t == 1 ? "" : "s");
00064 else
00065 len = snprintf(tmp, sizeof(tmp), "%u%c", t, s[0]);
00066
00067 INSIST(len + 1 <= sizeof(tmp));
00068 isc_buffer_availableregion(target, ®ion);
00069 if (len > region.length)
00070 return (ISC_R_NOSPACE);
00071 memmove(region.base, tmp, len);
00072 isc_buffer_add(target, len);
00073
00074 return (ISC_R_SUCCESS);
00075 }
00076
00077
00078
00079
00080 isc_result_t
00081 dns_ttl_totext(isc_uint32_t src, isc_boolean_t verbose, isc_buffer_t *target) {
00082 return (dns_ttl_totext2(src, verbose, ISC_TRUE, target));
00083 }
00084
00085 isc_result_t
00086 dns_ttl_totext2(isc_uint32_t src, isc_boolean_t verbose,
00087 isc_boolean_t upcase, isc_buffer_t *target)
00088 {
00089 unsigned secs, mins, hours, days, weeks, x;
00090
00091 secs = src % 60; src /= 60;
00092 mins = src % 60; src /= 60;
00093 hours = src % 24; src /= 24;
00094 days = src % 7; src /= 7;
00095 weeks = src; src = 0;
00096 POST(src);
00097
00098 x = 0;
00099 if (weeks != 0) {
00100 RETERR(ttlfmt(weeks, "week", verbose, ISC_TF(x > 0), target));
00101 x++;
00102 }
00103 if (days != 0) {
00104 RETERR(ttlfmt(days, "day", verbose, ISC_TF(x > 0), target));
00105 x++;
00106 }
00107 if (hours != 0) {
00108 RETERR(ttlfmt(hours, "hour", verbose, ISC_TF(x > 0), target));
00109 x++;
00110 }
00111 if (mins != 0) {
00112 RETERR(ttlfmt(mins, "minute", verbose, ISC_TF(x > 0), target));
00113 x++;
00114 }
00115 if (secs != 0 ||
00116 (weeks == 0 && days == 0 && hours == 0 && mins == 0)) {
00117 RETERR(ttlfmt(secs, "second", verbose, ISC_TF(x > 0), target));
00118 x++;
00119 }
00120 INSIST (x > 0);
00121
00122
00123
00124
00125
00126 if (x == 1 && upcase && !verbose) {
00127 isc_region_t region;
00128
00129
00130
00131
00132
00133
00134
00135 isc_buffer_usedregion(target, ®ion);
00136 region.base[region.length - 1] =
00137 toupper(region.base[region.length - 1]);
00138 }
00139 return (ISC_R_SUCCESS);
00140 }
00141
00142 isc_result_t
00143 dns_counter_fromtext(isc_textregion_t *source, isc_uint32_t *ttl) {
00144 return (bind_ttl(source, ttl));
00145 }
00146
00147 isc_result_t
00148 dns_ttl_fromtext(isc_textregion_t *source, isc_uint32_t *ttl) {
00149 isc_result_t result;
00150
00151 result = bind_ttl(source, ttl);
00152 if (result != ISC_R_SUCCESS && result != ISC_R_RANGE)
00153 result = DNS_R_BADTTL;
00154 return (result);
00155 }
00156
00157 static isc_result_t
00158 bind_ttl(isc_textregion_t *source, isc_uint32_t *ttl) {
00159 isc_uint64_t tmp = 0ULL;
00160 isc_uint32_t n;
00161 char *s;
00162 char buf[64];
00163 char nbuf[64];
00164
00165
00166
00167
00168
00169 if (source->length > sizeof(buf) - 1)
00170 return (DNS_R_SYNTAX);
00171 strncpy(buf, source->base, source->length);
00172 buf[source->length] = '\0';
00173 s = buf;
00174
00175 do {
00176 isc_result_t result;
00177
00178 char *np = nbuf;
00179 while (*s != '\0' && isdigit((unsigned char)*s))
00180 *np++ = *s++;
00181 *np++ = '\0';
00182 INSIST(np - nbuf <= (int)sizeof(nbuf));
00183 result = isc_parse_uint32(&n, nbuf, 10);
00184 if (result != ISC_R_SUCCESS)
00185 return (DNS_R_SYNTAX);
00186 switch (*s) {
00187 case 'w':
00188 case 'W':
00189 tmp += (isc_uint64_t) n * 7 * 24 * 3600;
00190 s++;
00191 break;
00192 case 'd':
00193 case 'D':
00194 tmp += (isc_uint64_t) n * 24 * 3600;
00195 s++;
00196 break;
00197 case 'h':
00198 case 'H':
00199 tmp += (isc_uint64_t) n * 3600;
00200 s++;
00201 break;
00202 case 'm':
00203 case 'M':
00204 tmp += (isc_uint64_t) n * 60;
00205 s++;
00206 break;
00207 case 's':
00208 case 'S':
00209 tmp += (isc_uint64_t) n;
00210 s++;
00211 break;
00212 case '\0':
00213
00214 if (tmp != 0ULL)
00215 return (DNS_R_SYNTAX);
00216 tmp = n;
00217 break;
00218 default:
00219 return (DNS_R_SYNTAX);
00220 }
00221 } while (*s != '\0');
00222
00223 if (tmp > 0xffffffffULL)
00224 return (ISC_R_RANGE);
00225
00226 *ttl = (isc_uint32_t)(tmp & 0xffffffffUL);
00227 return (ISC_R_SUCCESS);
00228 }