#include "misc.hh"
#include <vector>
#include <sstream>
-#include <errno.h>
+#include <cerrno>
#include <cstring>
#include <iostream>
#include <sys/types.h>
return len;
}
+auto pdns::getMessageFromErrno(const int errnum) -> std::string
+{
+ const size_t errLen = 2048;
+ std::string errMsgData{};
+ errMsgData.resize(errLen);
+
+ const char* errMsg = nullptr;
+#ifdef _GNU_SOURCE
+ errMsg = strerror_r(errnum, errMsgData.data(), errMsgData.length());
+#else
+ // This can fail, and when it does, it sets errno. We ignore that and
+ // set our own error message instead.
+ int res = strerror_r(errnum, errMsgData.data(), errMsgData.length());
+ errMsg = errMsgData.c_str();
+ if (res != 0) {
+ errMsg = "Unknown (the exact error could not be retrieved)";
+ }
+#endif
+
+ // We make a copy here because `strerror_r()` might return a static
+ // immutable buffer for an error message. The copy shouldn't be
+ // critical though, we're on the bailout/error-handling path anyways.
+ std::string message{errMsg};
+ return message;
+}
+
string nowTime()
{
time_t now = time(nullptr);
typedef enum { TSIG_MD5, TSIG_SHA1, TSIG_SHA224, TSIG_SHA256, TSIG_SHA384, TSIG_SHA512, TSIG_GSS } TSIGHashEnum;
+namespace pdns
+{
+/**
+ * \brief Retrieves the errno-based error message in a reentrant way.
+ *
+ * This internally handles the portability issues around using
+ * `strerror_r` and returns a `std::string` that owns the error
+ * message's contents.
+ *
+ * \param[in] errnum The errno value.
+ *
+ * \return The `std::string` error message.
+ */
+auto getMessageFromErrno(int errnum) -> std::string;
+}
+
string nowTime();
const string unquotify(const string &item);
string humanDuration(time_t passed);