// YYYY-mm-dd HH:MM:SS TZOFF
strftime(buffer, sizeof(buffer), "%F %T %z", tm);
buffer[sizeof(buffer)-1] = '\0';
- return buffer;
+ return string(buffer);
}
uint16_t getShort(const unsigned char *p)
}
// returns -1 in case of error, 0 if no data is available, 1 if there is. In the first two cases, errno is set
-int waitForMultiData(const set<int>& fds, const int seconds, const int useconds, int* fd) {
+int waitForMultiData(const set<int>& fds, const int seconds, const int useconds, int* fdOut) {
set<int> realFDs;
for (const auto& fd : fds) {
if (fd >= 0 && realFDs.count(fd) == 0) {
}
set<int>::const_iterator it(pollinFDs.begin());
advance(it, random() % pollinFDs.size());
- *fd = *it;
+ *fdOut = *it;
return 1;
}
d_set.tv_sec=d_set.tv_usec=0;
}
-DTime::DTime(const DTime &dt)
+DTime::DTime(const DTime &dt) : d_set(dt.d_set)
{
- d_set=dt.d_set;
}
time_t DTime::time()
if(gethostname(tmp, MAXHOSTNAMELEN))
return "UNKNOWN";
- return tmp;
+ return string(tmp);
}
string itoa(int i)
string U32ToIP(uint32_t val)
{
char tmp[17];
- snprintf(tmp, sizeof(tmp)-1, "%u.%u.%u.%u",
+ snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u",
(val >> 24)&0xff,
(val >> 16)&0xff,
(val >> 8)&0xff,
(val )&0xff);
- return tmp;
+ return string(tmp);
}
ret.reserve((int)(str.size()*2.2));
for(string::size_type n=0;n<str.size();++n) {
- sprintf(tmp,"%02x ", (unsigned char)str[n]);
+ snprintf(tmp, sizeof(tmp), "%02x ", (unsigned char)str[n]);
ret+=tmp;
}
return ret;
bool readFileIfThere(const char* fname, std::string* line)
{
line->clear();
- FILE* fp = fopen(fname, "r");
+ auto fp = std::unique_ptr<FILE, int(*)(FILE*)>(fopen(fname, "r"), fclose);
if(!fp)
return false;
- stringfgets(fp, *line);
- fclose(fp);
+ stringfgets(fp.get(), *line);
+ fp.reset();
+
return true;
}
return flags & O_NONBLOCK;
}
+bool setReceiveSocketErrors(int sock, int af)
+{
+#ifdef __linux__
+ int tmp = 1, ret;
+ if (af == AF_INET) {
+ ret = setsockopt(sock, IPPROTO_IP, IP_RECVERR, &tmp, sizeof(tmp));
+ } else {
+ ret = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVERR, &tmp, sizeof(tmp));
+ }
+ if (ret < 0) {
+ throw PDNSException(string("Setsockopt failed: ") + strerror(errno));
+ }
+#endif
+ return true;
+}
+
// Closes a socket.
int closesocket( int socket )
{
uint64_t getRealMemoryUsage(const std::string&)
{
#ifdef __linux__
- ifstream ifs("/proc/"+std::to_string(getpid())+"/smaps");
+ ifstream ifs("/proc/self/statm");
+ if(!ifs)
+ return 0;
+
+ uint64_t size, resident, shared, text, lib, data;
+ ifs >> size >> resident >> shared >> text >> lib >> data;
+
+ return data * getpagesize();
+#else
+ struct rusage ru;
+ if (getrusage(RUSAGE_SELF, &ru) != 0)
+ return 0;
+ return ru.ru_maxrss * 1024;
+#endif
+}
+
+
+uint64_t getSpecialMemoryUsage(const std::string&)
+{
+#ifdef __linux__
+ ifstream ifs("/proc/self/smaps");
if(!ifs)
return 0;
string line;
return ENOSYS;
#endif /* HAVE_PTHREAD_SETAFFINITY_NP */
}
+
+std::vector<ComboAddress> getResolvers(const std::string& resolvConfPath)
+{
+ std::vector<ComboAddress> results;
+
+ ifstream ifs(resolvConfPath);
+ if (!ifs) {
+ return results;
+ }
+
+ string line;
+ while(std::getline(ifs, line)) {
+ boost::trim_right_if(line, is_any_of(" \r\n\x1a"));
+ boost::trim_left(line); // leading spaces, let's be nice
+
+ string::size_type tpos = line.find_first_of(";#");
+ if (tpos != string::npos) {
+ line.resize(tpos);
+ }
+
+ if (boost::starts_with(line, "nameserver ") || boost::starts_with(line, "nameserver\t")) {
+ vector<string> parts;
+ stringtok(parts, line, " \t,"); // be REALLY nice
+ for(vector<string>::const_iterator iter = parts.begin() + 1; iter != parts.end(); ++iter) {
+ try {
+ results.emplace_back(*iter, 53);
+ }
+ catch(...)
+ {
+ }
+ }
+ }
+ }
+
+ return results;
+}