Commit 1e5c2d48 authored by Ian Roddis's avatar Ian Roddis

Adding hostname resolution to peer

parent d6a2ff62
......@@ -41,7 +41,7 @@ public:
~Peer();
const Address& address() const;
const std::string& hostname() const;
const std::string& hostname();
void associateFd(Fd fd);
Fd fd() const;
......@@ -69,7 +69,7 @@ private:
void *ssl_;
};
std::ostream& operator<<(std::ostream& os, const Peer& peer);
std::ostream& operator<<(std::ostream& os, Peer& peer);
} // namespace Tcp
} // namespace Pistache
......@@ -7,6 +7,9 @@
#include <stdexcept>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <pistache/peer.h>
#include <pistache/async.h>
......@@ -41,8 +44,25 @@ const Address& Peer::address() const
return addr;
}
const std::string& Peer::hostname() const
const std::string& Peer::hostname()
{
if (hostname_.empty()) {
char host[NI_MAXHOST];
struct sockaddr_in sa;
sa.sin_family = AF_INET;
if (inet_pton(AF_INET, addr.host().c_str(), &sa.sin_addr) == 0) {
hostname_ = addr.host();
} else {
if (!getnameinfo((struct sockaddr*)&sa, sizeof(sa)
, host, sizeof(host)
, NULL, 0 // Service info
, NI_NAMEREQD // Raise an error if name resolution failed
)
) {
hostname_.assign((char *)host);
}
}
}
return hostname_;
}
......@@ -106,7 +126,7 @@ Peer::send(const RawBuffer& buffer, int flags) {
return transport()->asyncWrite(fd_, buffer, flags);
}
std::ostream& operator<<(std::ostream& os, const Peer& peer) {
std::ostream& operator<<(std::ostream& os, Peer& peer) {
const auto& addr = peer.address();
os << "(" << addr.host() << ", " << addr.port() << ") [" << peer.hostname() << "]";
return os;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment