18 #include <boost/lexical_cast.hpp>
32 return (isalpha(c) != 0);
43 {
'S', 1, 0xffffffff / 1 },
44 {
'M', 60, 0xffffffff / 60 },
45 {
'H', 60 * 60, 0xffffffff / (60 * 60) },
46 {
'D', 24 * 60 * 60, 0xffffffff / (24 * 60 * 60) },
47 {
'W', 7 * 24 * 60 * 60, 0xffffffff / (7 * 24 * 60 * 60) }
57 parseTTLString(
const string& ttlstr, uint32_t& ttlval,
string* error_txt) {
59 if (error_txt != NULL) {
60 *error_txt =
"Empty TTL string";
67 const string::const_iterator end = ttlstr.end();
68 string::const_iterator pos = ttlstr.begin();
72 bool units_mode =
false;
76 const string::const_iterator unit = find_if(pos, end, myIsalpha);
81 if (error_txt != NULL) {
82 *error_txt =
"Missing the last unit: " + ttlstr;
88 val = boost::lexical_cast<uint64_t>(ttlstr);
95 uint32_t multiply = 1;
96 uint32_t max_allowed = 0xffffffff;
98 for (
size_t i = 0; i <
sizeof(units) /
sizeof(*units); ++i) {
99 if (toupper(*unit) == units[i].unit) {
101 multiply = units[i].multiply;
102 max_allowed = units[i].max_allowed;
107 if (error_txt != NULL) {
108 *error_txt =
"Unknown unit used: " +
109 boost::lexical_cast<
string>(*unit) +
" in: " + ttlstr;
115 if (error_txt != NULL) {
116 *error_txt =
"Missing number in TTL: " + ttlstr;
120 const uint64_t value =
121 boost::lexical_cast<uint64_t>(string(pos, unit));
122 if (value > max_allowed) {
123 if (error_txt != NULL) {
124 *error_txt =
"Part of TTL out of range: " + ttlstr;
130 const uint64_t seconds = value * multiply;
131 assert(seconds <= 0xffffffff);
138 if (val < seconds || val > 0xffffffff) {
139 if (error_txt != NULL) {
140 *error_txt =
"Part of TTL out of range: " + ttlstr;
147 }
catch (
const boost::bad_lexical_cast&) {
148 if (error_txt != NULL) {
149 *error_txt =
"invalid TTL: " + ttlstr;
154 if (val <= 0xffffffff) {
158 if (error_txt != NULL) {
159 *error_txt =
"TTL out of range: " + ttlstr;
168 RRTTL::RRTTL(
const std::string& ttlstr) {
170 if (!parseTTLString(ttlstr, ttlval_, &error_txt)) {
176 RRTTL::createFromText(
const string& ttlstr) {
178 if (parseTTLString(ttlstr, ttlval, NULL)) {
179 return (
new RRTTL(ttlval));
192 RRTTL::toText()
const {
void writeUint32(uint32_t data)
Write an unsigned 32-bit integer in host byte order into the internal buffer in network byte order...
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
The AbstractMessageRenderer class is an abstract base class that provides common interfaces for rende...
The RRTTL class encapsulates TTLs used in DNS resource records.
ostream & operator<<(ostream &os, const RRTTL &rrttl)
Insert the RRTTL as a string into stream.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
void writeUint32(uint32_t data)
Write an unsigned 32-bit integer in host byte order into the buffer in network byte order...
Defines the logger used by the top-level component of kea-dhcp-ddns.
const std::string toText() const
Convert the RRTTL to a string.
A standard DNS module exception that is thrown if an RRTTL object is being constructed from an unreco...
A standard DNS module exception that is thrown if an RRTTL object is being constructed from a incompl...