Skip to content

Commit 0838d8e

Browse files
Added common ResolveDNSName function
1 parent f04b80f commit 0838d8e

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/lib/Convert.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ using namespace std::chrono;
4040
using namespace boost::uuids;
4141
using namespace boost::posix_time;
4242
using namespace boost::gregorian;
43+
using namespace boost::asio::ip;
4344
using namespace sttp;
4445

4546
const datetime_t DateTimeEpoch(date(1400, 1, 1), TimeSpan(0, 0, 0));
@@ -864,3 +865,52 @@ StringMap<string> sttp::ParseKeyValuePairs(const string& value, const char param
864865

865866
return keyValuePairs;
866867
}
868+
869+
std::string sttp::ResolveDNSName(IOContext& service, const TcpEndPoint& source)
870+
{
871+
string hostName;
872+
return ResolveDNSName(service, source, hostName);
873+
}
874+
875+
string sttp::ResolveDNSName(IOContext& service, const TcpEndPoint& source, string& hostName)
876+
{
877+
const IPAddress address = source.address();
878+
const string port = ToString(source.port());
879+
string connectionID;
880+
881+
if (source.protocol() == tcp::v6())
882+
connectionID = "[" + address.to_string() + "]:" + port;
883+
else
884+
connectionID = address.to_string() + ":" + port;
885+
886+
hostName.clear();
887+
888+
try
889+
{
890+
DnsResolver resolver(service);
891+
const DnsResolver::query dnsQuery(address.to_string(), port);
892+
DnsResolver::iterator iterator = resolver.resolve(dnsQuery);
893+
const DnsResolver::iterator end;
894+
895+
while (iterator != end)
896+
{
897+
const auto& endPoint = *iterator++;
898+
899+
if (!endPoint.host_name().empty())
900+
{
901+
hostName = endPoint.host_name();
902+
connectionID = hostName + " (" + connectionID + ")"; // NOLINT
903+
break;
904+
}
905+
}
906+
}
907+
catch (...)
908+
{ //-V565
909+
// DNS lookup failure is not catastrophic
910+
}
911+
912+
if (hostName.empty())
913+
hostName = address.to_string();
914+
915+
return connectionID;
916+
}

src/lib/Convert.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,12 @@ namespace sttp
163163

164164
// Parses a string of key/value pairs into a case-insensitive string dictionary
165165
StringMap<std::string> ParseKeyValuePairs(const std::string& value, char parameterDelimiter = ';', char keyValueDelimiter = '=', char startValueDelimiter = '{', char endValueDelimiter = '}');
166+
167+
// Returns a DNS resolved host name and port for the specified end point
168+
std::string ResolveDNSName(IOContext& service, const TcpEndPoint& source);
169+
170+
// Returns a DNS resolved host name and port for the specified end point.
171+
// Out parameter hostName is set to the DNS host name, if resolvable,
172+
// otherwise will be set to the IP address of the end point.
173+
std::string ResolveDNSName(IOContext& service, const TcpEndPoint& source, std::string& hostName);
166174
}

0 commit comments

Comments
 (0)