DNSEntry DNSServer::getDNS(const std::string& domain) const { std::shared_lock<std::shared_timed_mutex> lockguard(data_mutex); DNSEntry entry = -1; try { entry = data.at(domain); } catch(const std::exception& ex){ } return entry; }
static struct servent *getservbyname_r(const char *name, const char *proto, struct servent *result, bsl::vector<char> *buffer) // Re-entrant version of 'getservbyname', with a private improvement that // it never fails with ERANGE but instead resizes 'buffer' as needed. // Return 'result' with all entries filled in upon success, and 0 if // 'getservbyname' fails. { static BloombergLP::bslmt::Mutex mutex; BloombergLP::bslmt::LockGuard<BloombergLP::bslmt::Mutex> lockguard(&mutex); struct servent *server = getservbyname(const_cast<char *>(name), const_cast<char *>(proto)); if (0 == server) { return 0; // RETURN } char **alias; int len = 0; // Compute size of server data and resize 'buffer' if necessary. len += bsl::strlen(server->s_proto) + 1; len += bsl::strlen(server->s_name) + 1; len += sizeof alias; for (alias = server->s_aliases; *alias != 0; ++alias) { len += sizeof *alias + bsl::strlen(*alias) + 1; } if (len > buffer->size()) { buffer->reserve(len); // to force capacity == len, buffer->resize(len); // otherwise the resize may round capacity // to smallest power of two not smaller than len } // Buffer is large enough; copy server data, and set 'result' pointers. char *sp = &buffer->front() + (1 + alias - server->s_aliases) * sizeof *alias; char **ap = reinterpret_cast<char **>(&buffer->front()); bsl::strcpy(sp, server->s_name); result->s_name = sp; sp += bsl::strlen(sp) + 1; bsl::strcpy(sp, server->s_proto); result->s_proto = sp; sp += bsl::strlen(sp) + 1; result->s_aliases = ap; for (alias = server->s_aliases; *alias != 0; ++alias) { bsl::strcpy(sp, *alias); *ap++ = sp; sp += bsl::strlen(sp) + 1; } *ap = 0; result->s_port = server->s_port; return result; }
void DNSServer::newEntry(const std::string& domain, const DNSEntry& dns) { std::lock_guard<std::shared_timed_mutex> lockguard(data_mutex); data.insert(std::pair<std::string, DNSEntry>(domain, dns)); }