This also creates the Pdns namespace and renames pdns_stou to Pdns::stou and separates
signed from unsigned conversion (Pdns::stoi and Pdns::stou) which are now implemented in
terms of Pdns::checked_conv and return a generic integer.
SSqlStatement::row_t row;
while (d_getDomainKeysQuery_stmt->hasNextRow()) {
d_getDomainKeysQuery_stmt->nextRow(row);
- kd.id = pdns_stou(row[0]);
- kd.flags = pdns_stou(row[1]);
+ pdns::checked_stoi_into(kd.id, row[0]);
+ pdns::checked_stoi_into(kd.flags, row[1]);
kd.active = (row[2] == "1");
kd.published = (row[3] == "1");
kd.content = row[4];
for (size_t i = 0; i < glob_result.gl_pathc; i++) {
if (regexec(®, glob_result.gl_pathv[i], 5, regm, 0) == 0) {
DNSBackend::KeyData kd;
- kd.id = pdns_stou(glob_result.gl_pathv[i] + regm[3].rm_so);
+ pdns::checked_stoi_into(kd.id, glob_result.gl_pathv[i] + regm[3].rm_so);
kd.active = !strncmp(glob_result.gl_pathv[i] + regm[4].rm_so, "1", 1);
kd.published = true;
- kd.flags = pdns_stou(glob_result.gl_pathv[i] + regm[2].rm_so);
+ pdns::checked_stoi_into(kd.flags, glob_result.gl_pathv[i] + regm[2].rm_so);
ifstream ifs(glob_result.gl_pathv[i]);
ostringstream content;
char buffer[1024];
if (glob(pathname.str().c_str(), GLOB_ERR, NULL, &glob_result) == 0) {
for (size_t i = 0; i < glob_result.gl_pathc; i++) {
if (regexec(®, glob_result.gl_pathv[i], 5, regm, 0) == 0) {
- unsigned int kid = pdns_stou(glob_result.gl_pathv[i] + regm[3].rm_so);
+ auto kid = pdns::checked_stoi<unsigned int>(glob_result.gl_pathv[i] + regm[3].rm_so);
if (kid == id) {
if (unlink(glob_result.gl_pathv[i])) {
cerr << "Cannot delete key:" << strerror(errno) << endl;
if (glob(pathname.str().c_str(), GLOB_ERR, NULL, &glob_result) == 0) {
for (size_t i = 0; i < glob_result.gl_pathc; i++) {
if (regexec(®, glob_result.gl_pathv[i], 5, regm, 0) == 0) {
- unsigned int kid = pdns_stou(glob_result.gl_pathv[i] + regm[3].rm_so);
+ auto kid = pdns::checked_stoi<unsigned int>(glob_result.gl_pathv[i] + regm[3].rm_so);
if (kid >= nextid)
nextid = kid + 1;
}
if (glob(pathname.str().c_str(), GLOB_ERR, NULL, &glob_result) == 0) {
for (size_t i = 0; i < glob_result.gl_pathc; i++) {
if (regexec(®, glob_result.gl_pathv[i], 5, regm, 0) == 0) {
- unsigned int kid = pdns_stou(glob_result.gl_pathv[i] + regm[3].rm_so);
+ auto kid = pdns::checked_stoi<unsigned int>(glob_result.gl_pathv[i] + regm[3].rm_so);
if (kid == id && !strcmp(glob_result.gl_pathv[i] + regm[4].rm_so, "0")) {
ostringstream newpath;
- newpath << getArg("dnssec-keydir") << "/" << dom.domain.toStringNoDot() << "." << pdns_stou(glob_result.gl_pathv[i] + regm[2].rm_so) << "." << kid << ".1.key";
+ newpath << getArg("dnssec-keydir") << "/" << dom.domain.toStringNoDot() << "." << pdns::checked_stoi<unsigned int>(glob_result.gl_pathv[i] + regm[2].rm_so) << "." << kid << ".1.key";
if (rename(glob_result.gl_pathv[i], newpath.str().c_str())) {
cerr << "Cannot activate key: " << strerror(errno) << endl;
}
if (glob(pathname.str().c_str(), GLOB_ERR, NULL, &glob_result) == 0) {
for (size_t i = 0; i < glob_result.gl_pathc; i++) {
if (regexec(®, glob_result.gl_pathv[i], 5, regm, 0) == 0) {
- unsigned int kid = pdns_stou(glob_result.gl_pathv[i] + regm[3].rm_so);
+ auto kid = pdns::checked_stoi<unsigned int>(glob_result.gl_pathv[i] + regm[3].rm_so);
if (kid == id && !strcmp(glob_result.gl_pathv[i] + regm[4].rm_so, "1")) {
ostringstream newpath;
- newpath << getArg("dnssec-keydir") << "/" << dom.domain.toStringNoDot() << "." << pdns_stou(glob_result.gl_pathv[i] + regm[2].rm_so) << "." << kid << ".0.key";
+ newpath << getArg("dnssec-keydir") << "/" << dom.domain.toStringNoDot() << "." << pdns::checked_stoi<unsigned int>(glob_result.gl_pathv[i] + regm[2].rm_so) << "." << kid << ".0.key";
if (rename(glob_result.gl_pathv[i], newpath.str().c_str())) {
cerr << "Cannot deactivate key: " << strerror(errno) << endl;
}
if (qtype2 != QType(local_result.qtype).toString())
continue;
- local_result.ttl = pdns_stou(rdata.substr(pos + 1));
+ pdns::checked_stoi_into(local_result.ttl, rdata.substr(pos + 1));
}
}
di.zone = DNSName(domain);
if (result.count("PdnsDomainLastCheck") && !result["PdnsDomainLastCheck"].empty())
- di.last_check = pdns_stou(result["PdnsDomainLastCheck"][0]);
+ pdns::checked_stoi_into(di.last_check, result["PdnsDomainLastCheck"][0]);
else
di.last_check = 0;
if (result.count("PdnsDomainNotifiedSerial") && !result["PdnsDomainNotifiedSerial"].empty())
- di.notified_serial = pdns_stou(result["PdnsDomainNotifiedSerial"][0]);
+ pdns::checked_stoi_into(di.notified_serial, result["PdnsDomainNotifiedSerial"][0]);
else
di.notified_serial = 0;
}
r.qname = DNSName(parts[1 + extraFields]);
r.qtype = parts[3 + extraFields];
- r.ttl = pdns_stou(parts[4 + extraFields]);
- r.domain_id = std::stoi(parts[5 + extraFields]);
+ pdns::checked_stoi_into(r.ttl, parts[4 + extraFields]);
+ pdns::checked_stoi_into(r.domain_id, parts[5 + extraFields]);
if (r.qtype.getCode() != QType::MX && r.qtype.getCode() != QType::SRV) {
r.content.clear();
d_InfoOfAllSlaveDomainsQuery=getArg("info-all-slaves-query");
d_SuperMasterInfoQuery=getArg("supermaster-query");
d_GetSuperMasterIPs=getArg("supermaster-name-to-ips");
- d_AddSuperMaster=getArg("supermaster-add");
+ d_AddSuperMaster=getArg("supermaster-add");
d_RemoveAutoPrimaryQuery=getArg("autoprimary-remove");
d_ListAutoPrimariesQuery=getArg("list-autoprimaries");
d_InsertZoneQuery=getArg("insert-zone-query");
d_GetLastInsertedKeyIdQuery = getArg("get-last-inserted-key-id-query");
d_ListDomainKeysQuery = getArg("list-domain-keys-query");
- d_GetAllDomainMetadataQuery = getArg("get-all-domain-metadata-query");
+ d_GetAllDomainMetadataQuery = getArg("get-all-domain-metadata-query");
d_GetDomainMetadataQuery = getArg("get-domain-metadata-query");
d_ClearDomainMetadataQuery = getArg("clear-domain-metadata-query");
d_ClearDomainAllMetadataQuery = getArg("clear-domain-all-metadata-query");
ASSERT_ROW_COLUMNS("info-zone-query", d_result[0], 7);
- di.id=pdns_stou(d_result[0][0]);
+ pdns::checked_stoi_into(di.id, d_result[0][0]);
try {
di.zone=DNSName(d_result[0][1]);
} catch (...) {
stringtok(masters, d_result[0][2], " ,\t");
for(const auto& m : masters)
di.masters.emplace_back(m, 53);
- di.last_check=pdns_stou(d_result[0][3]);
- di.notified_serial = pdns_stou(d_result[0][4]);
+ pdns::checked_stoi_into(di.last_check, d_result[0][3]);
+ pdns::checked_stoi_into(di.notified_serial, d_result[0][4]);
di.backend=this;
di.serial = 0;
}
try {
- sd.id=pdns_stou(row[0]);
+ pdns::checked_stoi_into(sd.id, row[0]);
} catch (const std::exception &e) {
g_log<<Logger::Warning<<"Could not convert id ("<<row[0]<<") for domain '"<<sd.zone<<"' into an integer: "<<e.what()<<endl;
continue;
}
try {
- sd.last_check=pdns_stou(row[3]);
+ pdns::checked_stoi_into(sd.last_check, row[3]);
} catch (const std::exception &e) {
g_log<<Logger::Warning<<"Could not convert last_check ("<<row[3]<<") for domain '"<<sd.zone<<"' into an integer: "<<e.what()<<endl;
continue;
stringtok( parts, d_result[n][3] );
try {
- uint32_t serial = parts.size() > 2 ? pdns_stou(parts[2]) : 0;
- uint32_t notified_serial = pdns_stou( d_result[n][2] );
+ uint32_t serial = parts.size() > 2 ? pdns::checked_stoi<uint32_t>(parts[2]) : 0;
+ auto notified_serial = pdns::checked_stoi<uint32_t>(d_result[n][2]);
if( serial != notified_serial ) {
- di.id = pdns_stou( d_result[n][0] );
+ pdns::checked_stoi_into(di.id, d_result[n][0]);
di.zone = DNSName( d_result[n][1] );
di.serial = serial;
di.notified_serial = notified_serial;
//~ for(const auto& val: row) {
//~ cerr<<"'"<<val<<"'"<<endl;
//~ }
- kd.id = pdns_stou(row[0]);
- kd.flags = pdns_stou(row[1]);
+ pdns::checked_stoi_into(kd.id, row[0]);
+ pdns::checked_stoi_into(kd.flags, row[1]);
kd.active = row[2] == "1";
kd.published = row[3] == "1";
kd.content = row[4];
d_getAllDomainsQuery_stmt->nextRow(row);
ASSERT_ROW_COLUMNS("get-all-domains-query", row, 8);
DomainInfo di;
- di.id = pdns_stou(row[0]);
+ pdns::checked_stoi_into(di.id, row[0]);
try {
di.zone = DNSName(row[1]);
} catch (...) {
}
try {
- di.notified_serial = pdns_stou(row[5]);
- di.last_check = pdns_stou(row[6]);
+ pdns::checked_stoi_into(di.notified_serial, row[5]);
+ pdns::checked_stoi_into(di.last_check, row[6]);
} catch(...) {
continue;
}
if (r.qtype == QType::MX || r.qtype == QType::SRV) {
string::size_type pos = content.find_first_not_of("0123456789");
if (pos != string::npos) {
- prio=pdns_stou(content.substr(0,pos));
+ pdns::checked_stoi_into(prio, content.substr(0,pos));
boost::erase_head(content, pos);
}
boost::trim_left(content);
if (row[1].empty())
r.ttl = defaultTTL;
else
- r.ttl=pdns_stou(row[1]);
+ pdns::checked_stoi_into(r.ttl, row[1]);
if(!d_qname.empty())
r.qname=d_qname;
r.disabled = !row[5].empty() && row[5][0]=='1';
- r.domain_id=pdns_stou(row[4]);
+ pdns::checked_stoi_into(r.domain_id, row[4]);
if (row.size() > 8) { // if column 8 exists, it holds an ordername
if (!row.at(8).empty()) {
void GSQLBackend::extractComment(SSqlStatement::row_t& row, Comment& comment)
{
- comment.domain_id = pdns_stou(row[0]);
+ pdns::checked_stoi_into(comment.domain_id, row[0]);
comment.qname = DNSName(row[1]);
comment.qtype = row[2];
- comment.modified_at = pdns_stou(row[3]);
+ pdns::checked_stoi_into(comment.modified_at, row[3]);
comment.account = std::move(row[4]);
comment.content = std::move(row[5]);
}
}
try {
- workFactor = pdns_stou(parameters.at(0).substr(3));
+ workFactor = pdns::checked_stoi<uint64_t>(parameters.at(0).substr(3));
workFactor = static_cast<uint64_t>(1) << workFactor;
if (workFactor > pwhash_max_work_factor) {
throw std::runtime_error("Invalid work factor of " + std::to_string(workFactor) + " in hashed password string, maximum is " + std::to_string(pwhash_max_work_factor));
}
- parallelFactor = pdns_stou(parameters.at(1).substr(2));
- blockSize = pdns_stou(parameters.at(2).substr(2));
+ parallelFactor = pdns::checked_stoi<uint64_t>(parameters.at(1).substr(2));
+ blockSize = pdns::checked_stoi<uint64_t>(parameters.at(2).substr(2));
auto b64Salt = hash.substr(saltPos, saltEnd - saltPos);
salt.reserve(pwhash_salt_size);
PrivateKey: ODIyNjAzODQ2MjgwODAxMjI2NDUxOTAyMDQxNDIyNjI=
*/
- drc.d_algorithm = pdns_stou(stormap["algorithm"]);
+ pdns::checked_stoi_into(drc.d_algorithm, stormap["algorithm"]);
string privateKey = stormap["privatekey"];
if (privateKey.length() != DECAF_EDDSA_25519_PRIVATE_BYTES)
PrivateKey: xZ+5Cgm463xugtkY5B0Jx6erFTXp13rYegst0qRtNsOYnaVpMx0Z/c5EiA9x8wWbDDct/U3FhYWA
*/
- drc.d_algorithm = pdns_stou(stormap["algorithm"]);
+ pdns::checked_stoi_into(drc.d_algorithm, stormap["algorithm"]);
string privateKey = stormap["privatekey"];
if (privateKey.length() != DECAF_EDDSA_448_PRIVATE_BYTES)
try {
data.nameserver = DNSName(parts.at(0));
data.hostmaster = DNSName(parts.at(1));
- data.serial = pdns_stou(parts.at(2).c_str());
- data.refresh = pdns_stou(parts.at(3).c_str());
- data.retry = pdns_stou(parts.at(4).c_str());
- data.expire = pdns_stou(parts.at(5).c_str());
- data.minimum = pdns_stou(parts.at(6).c_str());
+ pdns::checked_stoi_into(data.serial, parts.at(2));
+ pdns::checked_stoi_into(data.refresh, parts.at(3));
+ pdns::checked_stoi_into(data.retry, parts.at(4));
+ pdns::checked_stoi_into(data.expire, parts.at(5));
+ pdns::checked_stoi_into(data.minimum, parts.at(6));
}
catch(const std::out_of_range& oor) {
throw PDNSException("Out of range exception parsing '" + content + "'");
break;
/* non-terminal actions follow */
case DNSResponseAction::Action::Delay:
- dr.delayMsec = static_cast<int>(pdns_stou(ruleresult)); // sorry
+ pdns::checked_stoi_into(dr.delayMsec, ruleresult); // sorry
break;
case DNSResponseAction::Action::None:
break;
break;
/* non-terminal actions follow */
case DNSAction::Action::Delay:
- dq.delayMsec = static_cast<int>(pdns_stou(ruleresult)); // sorry
+ pdns::checked_stoi_into(dq.delayMsec, ruleresult); // sorry
break;
case DNSAction::Action::None:
/* fall-through */
return NGHTTP2_ERR_CALLBACK_FAILURE;
}
try {
- stream->second.d_responseCode = pdns_stou(std::string(reinterpret_cast<const char*>(value), valuelen));
+ pdns::checked_stoi_into(stream->second.d_responseCode, std::string(reinterpret_cast<const char*>(value), valuelen));
}
catch (...) {
vinfolog("Error parsing the status header for stream ID %d", frame->hd.stream_id);
}
const string& relevant = (parts.size() > 2) ? parts.at(2) : "";
- unsigned int total = pdns_stou(parts.at(1));
+ auto total = pdns::checked_stoi<unsigned int>(parts.at(1));
if (relevant.size() % 2 || (relevant.size() / 2) != total) {
throw MOADNSException((boost::format("invalid unknown record length: size not equal to length field (%d != 2 * %d)") % relevant.size() % total).str());
}
return iter->second.second;
if (isUnknownType(name)) {
- return (uint16_t)pdns_stou(name.substr(4));
+ return pdns::checked_stoi<uint16_t>(name.substr(4));
}
throw runtime_error("Unknown DNS type '"+name+"'");
}
else if (it->second == KeyTypes::numeric) {
try {
- unsigned int num = pdns_stou(value);
+ auto num = pdns::checked_stoi<unsigned int>(value);
stormap[key] = std::to_string(num);
if (key == "algorithm") {
algorithm = num;
std::tie(key,value)=splitField(sline, ':');
boost::trim(value);
if(pdns_iequals(key,"algorithm")) {
- algorithm = pdns_stou(value);
+ pdns::checked_stoi_into(algorithm, value);
stormap["algorithm"]=std::to_string(algorithm);
continue;
} else if (pdns_iequals(key,"pin")) {
else {
uint16_t port;
try {
- port = static_cast<uint16_t>(pdns_stou(::arg()["remote-port"]));
+ pdns::checked_stoi_into(port, ::arg()["remote-port"]);
}
catch(...) {
cerr<<"Unable to convert '"<<::arg()["remote-port"]<<"' to a port number for connecting to remote PowerDNS\n";
{
if (input[0] == '[') { // case 1
auto both = splitField(input.substr(1), ']');
- return ComboAddress(both.first, both.second.empty() ? port : static_cast<uint16_t>(pdns_stou(both.second.substr(1))));
+ return ComboAddress(both.first, both.second.empty() ? port : pdns::checked_stoi<uint16_t>(both.second.substr(1)));
}
string::size_type count = 0;
both.first = input.substr(0, cpos);
both.second = input.substr(cpos + 1);
- uint16_t newport = static_cast<uint16_t>(pdns_stou(both.second));
+ auto newport = pdns::checked_stoi<uint16_t>(both.second);
return ComboAddress(both.first, newport);
}
default: // case 4
d_network = makeComboAddress(split.first);
if (!split.second.empty()) {
- setBits(static_cast<uint8_t>(pdns_stou(split.second)));
+ setBits(pdns::checked_stoi<uint8_t>(split.second));
}
else if (d_network.sin4.sin_family == AF_INET) {
setBits(32);
}
st.host = parts[0];
if (parts.size() > 1) {
- st.port = pdns_stou(parts[1]);
+ pdns::checked_stoi_into(st.port, parts[1]);
}
}
st.host = descr.substr(1, pos - 1);
if (pos + 2 < descr.length()) {
- st.port = pdns_stou(descr.substr(pos + 2));
+ pdns::checked_stoi_into(st.port, descr.substr(pos + 2));
}
}
if (pos + 2 > addr.size() || addr[pos+1]!=':')
return -1;
try {
- port = pdns_stou(addr.substr(pos+2));
+ pdns::checked_stoi_into(port, addr.substr(pos + 2));
portSet = true;
}
catch(const std::out_of_range&) {
while((entry = readdir(dirhdl))) {
uint32_t num;
try {
- num = pdns_stou(entry->d_name);
+ pdns::checked_stoi_into(num, entry->d_name);
} catch (...) {
continue; // was not a number.
}
return result;
}
-unsigned int pdns_stou(const std::string& str, size_t* idx, int base)
-{
- if (str.empty()) {
- return 0; // compatibility
- }
-
- unsigned long result;
- try {
- result = std::stoul(str, idx, base);
- }
- catch (std::invalid_argument& e) {
- throw std::invalid_argument(string(e.what()) + "; (invalid argument during std::stoul); data was \"" + str + "\"");
- }
- catch (std::out_of_range& e) {
- throw std::out_of_range(string(e.what()) + "; (out of range during std::stoul); data was \"" + str + "\"");
- }
- if (result > std::numeric_limits<unsigned int>::max()) {
- throw std::out_of_range("stoul returned result out of unsigned int range; data was \"" + str + "\"");
- }
- return static_cast<unsigned int>(result);
-}
-
bool isSettingThreadCPUAffinitySupported()
{
#ifdef HAVE_PTHREAD_SETAFFINITY_NP
uid_t strToUID(const string &str);
gid_t strToGID(const string &str);
-unsigned int pdns_stou(const std::string& str, size_t* idx = 0, int base = 10);
+namespace pdns
+{
+/**
+ * \brief Does a checked conversion from one integer type to another.
+ *
+ * \warning The source type `F` and target type `T` must have the same
+ * signedness, otherwise a compilation error is thrown.
+ *
+ * \exception std::out_of_range Thrown if the source value does not fit
+ * in the target type.
+ *
+ * \param[in] from The source value of type `F`.
+ *
+ * \return The target value of type `T`.
+ */
+template <typename T, typename F>
+auto checked_conv(F from) -> T
+{
+ static_assert(std::numeric_limits<F>::is_integer, "checked_conv: The `F` type must be an integer");
+ static_assert(std::numeric_limits<T>::is_integer, "checked_conv: The `T` type must be an integer");
+ static_assert((std::numeric_limits<F>::is_signed && std::numeric_limits<T>::is_signed) || (!std::numeric_limits<F>::is_signed && !std::numeric_limits<T>::is_signed),
+ "checked_conv: The `T` and `F` types must either both be signed or unsigned");
+
+ if (from < std::numeric_limits<T>::min() || from > std::numeric_limits<T>::max()) {
+ throw std::out_of_range("checked_conv: conversion from value that is out of range for target type");
+ }
+
+ return static_cast<T>(from);
+}
+
+/**
+ * \brief Performs a conversion from `std::string&` to integer.
+ *
+ * This function internally calls `std::stoll` and `std::stoull` to do
+ * the conversion from `std::string&` and calls `pdns::checked_conv` to
+ * do the checked conversion from `long long`/`unsigned long long` to
+ * `T`.
+ *
+ * \warning The target type `T` must be an integer, otherwise a
+ * compilation error is thrown.
+ *
+ * \exception std:stoll Throws what std::stoll throws.
+ *
+ * \exception std::stoull Throws what std::stoull throws.
+ *
+ * \exception pdns::checked_conv Throws what pdns::checked_conv throws.
+ *
+ * \param[in] str The input string to be converted.
+ *
+ * \param[in] idx Location to store the index at which processing
+ * stopped.
+ *
+ * \param[in] base The numerical base for conversion.
+ *
+ * \return `str` converted to integer `T`, or 0 if `str` is empty.
+ */
+template <typename T>
+auto checked_stoi(const std::string& str, size_t* idx = nullptr, int base = 10) -> T
+{
+ static_assert(std::numeric_limits<T>::is_integer, "checked_stoi: The `T` type must be an integer");
+
+ if (str.empty()) {
+ return 0; // compatibility
+ }
+
+ if constexpr (std::is_unsigned_v<T>) {
+ return pdns::checked_conv<T>(std::stoull(str, idx, base));
+ }
+ else {
+ return pdns::checked_conv<T>(std::stoll(str, idx, base));
+ }
+}
+
+/**
+ * \brief Performs a conversion from `std::string&` to integer.
+ *
+ * This function internally calls `pdns::checked_stoi` and stores its
+ * result in `out`.
+ *
+ * \exception pdns::checked_stoi Throws what pdns::checked_stoi throws.
+ *
+ * \param[out] out `str` converted to integer `T`, or 0 if `str` is
+ * empty.
+ *
+ * \param[in] str The input string to be converted.
+ *
+ * \param[in] idx Location to store the index at which processing
+ * stopped.
+ *
+ * \param[in] base The numerical base for conversion.
+ *
+ * \return `str` converted to integer `T`, or 0 if `str` is empty.
+ */
+template <typename T>
+auto checked_stoi_into(T& out, const std::string& str, size_t* idx = nullptr, int base = 10)
+{
+ out = checked_stoi<T>(str, idx, base);
+}
+}
bool isSettingThreadCPUAffinitySupported();
int mapThreadToCPUList(pthread_t tid, const std::set<int>& cpus);
auto dmq1 = parse(stormap, "exponent2");
auto iqmp = parse(stormap, "coefficient");
- drc.d_algorithm = pdns_stou(stormap["algorithm"]);
+ pdns::checked_stoi_into(drc.d_algorithm, stormap["algorithm"]);
if (drc.d_algorithm != d_algorithm) {
throw runtime_error(getName() + " tried to feed an algorithm " + std::to_string(drc.d_algorithm) + " to a " + std::to_string(d_algorithm) + " key");
continue;
}
for(auto const &digestAlgo : digestAlgos){
- rr.dr.d_content=std::make_shared<DSRecordContent>(makeDSFromDNSKey(p.qdomain, value.first.getDNSKEY(), pdns_stou(digestAlgo)));
+ rr.dr.d_content=std::make_shared<DSRecordContent>(makeDSFromDNSKey(p.qdomain, value.first.getDNSKEY(), pdns::checked_stoi<uint8_t>(digestAlgo)));
r->addRecord(DNSZoneRecord(rr));
haveOne=true;
}
cerr << "Syntax: pdnsutil test-algorithm algonum"<<endl;
return 0;
}
- if (testAlgorithm(pdns_stou(cmds.at(1))))
+ if (testAlgorithm(pdns::checked_stoi<int>(cmds.at(1))))
return 0;
return 1;
}
uint64_t workFactor = CredentialsHolder::s_defaultWorkFactor;
if (cmds.size() > 1) {
try {
- workFactor = pdns_stou(cmds.at(1));
+ pdns::checked_stoi_into(workFactor, cmds.at(1));
}
catch (const std::exception& e) {
cerr<<"Unable to parse the supplied work factor: "<<e.what()<<endl;
cerr << "Syntax: pdnsutil test-speed numcores [signing-server]"<<endl;
return 0;
}
- testSpeed(dk, DNSName(cmds.at(1)), (cmds.size() > 3) ? cmds.at(3) : "", pdns_stou(cmds.at(2)));
+ testSpeed(dk, DNSName(cmds.at(1)), (cmds.size() > 3) ? cmds.at(3) : "", pdns::checked_stoi<int>(cmds.at(2)));
}
else if (cmds.at(0) == "verify-crypto") {
if(cmds.size() != 2) {
return 0;
}
DNSName zone(cmds.at(1));
- unsigned int id = atoi(cmds.at(2).c_str()); // if you make this pdns_stou, the error gets worse
+ unsigned int id = atoi(cmds.at(2).c_str()); // if you make this pdns::checked_stoi, the error gets worse
if(!id)
{
cerr << "Invalid KEY-ID '" << cmds.at(2) << "'" << endl;
return 0;
}
DNSName zone(cmds.at(1));
- unsigned int id = pdns_stou(cmds.at(2));
+ auto id = pdns::checked_stoi<unsigned int>(cmds.at(2));
if(!id)
{
cerr<<"Invalid KEY-ID"<<endl;
return 0;
}
DNSName zone(cmds.at(1));
- unsigned int id = atoi(cmds.at(2).c_str()); // if you make this pdns_stou, the error gets worse
+ unsigned int id = atoi(cmds.at(2).c_str()); // if you make this pdns::checked_stoi, the error gets worse
if(!id)
{
cerr << "Invalid KEY-ID '" << cmds.at(2) << "'" << endl;
return 0;
}
DNSName zone(cmds.at(1));
- unsigned int id = atoi(cmds.at(2).c_str()); // if you make this pdns_stou, the error gets worse
+ unsigned int id = atoi(cmds.at(2).c_str()); // if you make this pdns::checked_stoi, the error gets worse
if(!id)
{
cerr << "Invalid KEY-ID '" << cmds.at(2) << "'" << endl;
else if (pdns_iequals(cmds.at(n), "unpublished")) {
published = false;
}
- else if (pdns_stou(cmds.at(n))) {
- bits = pdns_stou(cmds.at(n));
+ else if (pdns::checked_stoi<int>(cmds.at(n)) != 0) {
+ pdns::checked_stoi_into(bits, cmds.at(n));
}
else {
cerr << "Unknown algorithm, key flag or size '" << cmds.at(n) << "'" << endl;
return 0;
}
DNSName zone(cmds.at(1));
- unsigned int id = pdns_stou(cmds.at(2));
+ auto id = pdns::checked_stoi<unsigned int>(cmds.at(2));
if (!dk.removeKey(zone, id)) {
cerr<<"Cannot remove key " << id << " from " << zone <<endl;
return 1;
}
string zone = cmds.at(1);
- unsigned int id = pdns_stou(cmds.at(2));
+ auto id = pdns::checked_stoi<unsigned int>(cmds.at(2));
DNSSECPrivateKey dpk=dk.getKeyById(DNSName(zone), id);
cout << dpk.getKey()->convertToISC() <<endl;
}
}
dpk.setKey(key);
- dpk.d_algorithm = pdns_stou(cmds.at(3));
+ pdns::checked_stoi_into(dpk.d_algorithm, cmds.at(3));
if(dpk.d_algorithm == DNSSECKeeper::RSASHA1NSEC3SHA1)
dpk.d_algorithm = DNSSECKeeper::RSASHA1;
}
DNSName zone(cmds.at(1));
- unsigned int id = pdns_stou(cmds.at(2));
+ auto id = pdns::checked_stoi<unsigned int>(cmds.at(2));
DNSSECPrivateKey dpk=dk.getKeyById(zone, id);
cout << zone<<" IN DNSKEY "<<dpk.getDNSKEY().getZoneRepresentation() <<endl;
}
else if ((tmp_algo = DNSSECKeeper::shorthand2algorithm(cmds.at(n))) > 0) {
algorithm = tmp_algo;
}
- else if (pdns_stou(cmds.at(n)))
- bits = pdns_stou(cmds.at(n));
+ else if (pdns::checked_stoi<int>(cmds.at(n)) != 0)
+ pdns::checked_stoi_into(bits, cmds.at(n));
else {
cerr << "Unknown algorithm, key flag or size '" << cmds.at(n) << "'" << endl;
return 0;
return 1;
}
- id = pdns_stou(cmds.at(3));
+ pdns::checked_stoi_into(id, cmds.at(3));
std::vector<DNSBackend::KeyData> keys;
if (!B.getDomainKeys(zone, keys)) {
cerr << "No keys found for zone " << zone << std::endl;
return 1;
}
if (cmds.size() > 4) {
- bits = pdns_stou(cmds.at(4));
+ pdns::checked_stoi_into(bits, cmds.at(4));
}
if (bits < 1) {
cerr << "Invalid bit size " << bits << "given, must be positive integer";
}
public:
- Pkcs11Token(const std::shared_ptr<LockGuarded<Pkcs11Slot>>& slot, const std::string& label, const std::string& pub_label);
+ Pkcs11Token(const std::shared_ptr<LockGuarded<Pkcs11Slot>>& slot, const std::string& label, const std::string& pub_label);
~Pkcs11Token();
bool Login(const std::string& pin) {
};
void PKCS11DNSCryptoKeyEngine::fromISCMap(DNSKEYRecordContent& drc, stormap_t& stormap) {
- drc.d_algorithm = pdns_stou(stormap["algorithm"]);
+ pdns::checked_stoi_into(drc.d_algorithm, stormap["algorithm"]);
d_module = stormap["engine"];
d_slot_id = stormap["slot"];
boost::trim(d_slot_id);
throw RecordTextException("expected digits at position "+std::to_string(d_pos)+" in '"+d_string+"'");
size_t pos;
- val=pdns_stou(d_string.c_str()+d_pos, &pos);
-
+ val = pdns::checked_stoi<uint32_t>(d_string.c_str() + d_pos, &pos);
+
d_pos += pos;
}
if (end - begin != 1)
return "Need to supply new minimum TTL number\n";
try {
- SyncRes::s_minimumTTL = pdns_stou(*begin);
+ pdns::checked_stoi_into(SyncRes::s_minimumTTL, *begin);
return "New minimum TTL: " + std::to_string(SyncRes::s_minimumTTL) + "\n";
}
catch (const std::exception& e) {
if (end - begin != 1)
return "Need to supply new ECS minimum TTL number\n";
try {
- SyncRes::s_minimumECSTTL = pdns_stou(*begin);
+ pdns::checked_stoi_into(SyncRes::s_minimumECSTTL, *begin);
return "New minimum ECS TTL: " + std::to_string(SyncRes::s_minimumECSTTL) + "\n";
}
catch (const std::exception& e) {
if (end - begin != 1)
return "Need to supply new cache size\n";
try {
- g_maxCacheEntries = pdns_stou(*begin);
+ g_maxCacheEntries = pdns::checked_stoi<uint32_t>(*begin);
return "New max cache entries: " + std::to_string(g_maxCacheEntries) + "\n";
}
catch (const std::exception& e) {
if (end - begin != 1)
return "Need to supply new packet cache size\n";
try {
- g_maxPacketCacheEntries = pdns_stou(*begin);
+ g_maxPacketCacheEntries = pdns::checked_stoi<uint32_t>(*begin);
return "New max packetcache entries: " + std::to_string(g_maxPacketCacheEntries) + "\n";
}
catch (const std::exception& e) {
return "No event trace enabled value specified\n";
}
try {
- SyncRes::s_event_trace_enabled = pdns_stou(*begin);
+ pdns::checked_stoi_into(SyncRes::s_event_trace_enabled, *begin);
return "New event trace enabled value: " + std::to_string(SyncRes::s_event_trace_enabled) + "\n";
}
catch (const std::exception& e) {
uint64_t workFactor = CredentialsHolder::s_defaultWorkFactor;
if (commands.size() > 1) {
try {
- workFactor = pdns_stou(commands.at(1));
+ pdns::checked_stoi_into(workFactor, commands.at(1));
}
catch (const std::exception& e) {
cerr << "Unable to parse the supplied work factor: " << e.what() << endl;
boost::trim(headers.first);
boost::trim(headers.second);
- unsigned int threadId = pdns_stou(headers.first);
+ auto threadId = pdns::checked_stoi<unsigned int>(headers.first);
std::vector<std::string> cpus;
stringtok(cpus, headers.second, ",");
throw ResolverException("Query to '" + ipport.toLogString() + "' for SOA of '" + domain.toLogString() + "' produced an unparseable response");
try {
- *serial=pdns_stou(parts[2]);
+ *serial = pdns::checked_stoi<uint32_t>(parts[2]);
}
catch(const std::out_of_range& oor) {
throw ResolverException("Query to '" + ipport.toLogString() + "' for SOA of '" + domain.toLogString() + "' produced an unparseable serial");
PrivateKey: GU6SnQ/Ou+xC5RumuIUIuJZteXT2z0O/ok1s38Et6mQ=
*/
- drc.d_algorithm = pdns_stou(stormap["algorithm"]);
+ pdns::checked_stoi_into(drc.d_algorithm, stormap["algorithm"]);
string privateKey = stormap["privatekey"];
if (privateKey.length() != crypto_sign_ed25519_SEEDBYTES)
if (k.substr(0, 3) == "key") {
try {
generic = true;
- return SvcParam::SvcParamKey(pdns_stou(k.substr(3)));
+ return SvcParam::SvcParamKey(pdns::checked_stoi<uint16_t>(k.substr(3)));
}
catch (...) {
}
zrrs.push_back(zrr);
} else {
for(auto const &digestAlgo : digestAlgos) {
- zrr.dr.d_content=std::make_shared<DSRecordContent>(makeDSFromDNSKey(target, value.first.getDNSKEY(), pdns_stou(digestAlgo)));
+ zrr.dr.d_content=std::make_shared<DSRecordContent>(makeDSFromDNSKey(target, value.first.getDNSKEY(), pdns::checked_stoi<uint8_t>(digestAlgo)));
zrrs.push_back(zrr);
}
}
stringtok(parts, rr->d_content->getZoneRepresentation());
if (parts.size() >= 3) {
try {
- serial=pdns_stou(parts[2]);
+ pdns::checked_stoi_into(serial, parts[2]);
}
catch(const std::out_of_range& oor) {
g_log<<Logger::Warning<<logPrefix<<"invalid serial in IXFR query"<<endl;
> MetaDataStorage;
// Initialize our backend ID from the suffix, skipping the '-' that DNSBackend adds there
- SimpleBackend(const std::string& suffix): d_suffix(suffix), d_backendId(pdns_stou(suffix.substr(1)))
+ SimpleBackend(const std::string& suffix) :
+ d_suffix(suffix), d_backendId(pdns::checked_stoi<decltype(d_backendId)>(suffix.substr(1)))
{
}
unsigned int ttl;
std::getline(ifs, host, ' ');
std::getline(ifs, type, ' ');
- ttl = pdns_stou(type);
+ pdns::checked_stoi_into(ttl, type);
std::getline(ifs, type, ' ');
std::getline(ifs, type, ' ');
std::getline(ifs, data, '\n');
std::set<unsigned int> CDSalgos;
for(auto const &digestAlgo : digestAlgos) {
- CDSalgos.insert(pdns_stou(digestAlgo));
+ CDSalgos.insert(pdns::checked_stoi<unsigned int>(digestAlgo));
}
if (value.second.keyType == DNSSECKeeper::KSK || value.second.keyType == DNSSECKeeper::CSK) {
string retval;
g_numRecords++;
string content(ocontent);
- if(qtype == "MX" || qtype == "SRV") {
- prio=pdns_stou(content);
-
+ if(qtype == "MX" || qtype == "SRV") {
+ pdns::checked_stoi_into(prio, content);
+
string::size_type pos = content.find_first_not_of("0123456789");
if(pos != string::npos)
boost::erase_head(content, pos);
}
if ( !args["domainid"].empty() )
- g_domainid = pdns_stou( args["domainid"] );
+ pdns::checked_stoi_into(g_domainid, args["domainid"]);
else
g_domainid = 1;
return; // NSECs do not go in the database
if((qtype == "MX" || qtype == "SRV")) {
- prio=pdns_stou(content);
-
+ pdns::checked_stoi_into(prio, content);
+
string::size_type pos = content.find_first_not_of("0123456789");
if(pos != string::npos)
boost::erase_head(content, pos);
unsigned int val;
try {
- val=pdns_stou(str);
+ pdns::checked_stoi_into(val, str);
}
catch (const std::out_of_range& oor) {
throw PDNSException("Unable to parse time specification '"+str+"' "+getLineOfFile());