@@ -40,6 +40,7 @@ using namespace std::chrono;
4040using namespace boost ::uuids;
4141using namespace boost ::posix_time;
4242using namespace boost ::gregorian;
43+ using namespace boost ::asio::ip;
4344using namespace sttp ;
4445
4546const 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+ }
0 commit comments