From f93ec0f4881d781c6360fe32a22e9405ce5cb443 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Sun, 16 Apr 2017 21:49:51 +0100 Subject: [PATCH] Show IP address as well as hostname for ServerException::SocketOpenError (cherry picked from commit 1a51adcd6be000f09fa280e42fb12e5631cd69b9) --- lib/server/SocketStream.cpp | 41 ++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/lib/server/SocketStream.cpp b/lib/server/SocketStream.cpp index 0c4b30096..f783e20a0 100644 --- a/lib/server/SocketStream.cpp +++ b/lib/server/SocketStream.cpp @@ -13,12 +13,16 @@ #include #endif -#include #include #include -#ifndef WIN32 +#include + +#ifdef WIN32 + #include // for InetNtop +#else #include + #include // for inet_ntop #endif #ifdef HAVE_UCRED_H @@ -182,13 +186,44 @@ void SocketStream::Open(Socket::Type Type, const std::string& rName, int Port) } // Connect it + std::string name_pretty; + if(Type == Socket::TypeUNIX) + { + // For a UNIX socket, the path is all we need to show the user: + name_pretty = rName; + } + else + { + // For an IP socket, try to include the resolved IP address as well as the hostname: + std::ostringstream name_oss; + char name_buf[256]; +#ifdef WIN32 + const char* addr_str = InetNtop(sockDomain, &addr.sa_generic, name_buf, + sizeof(name_buf)); +#else + const char* addr_str = inet_ntop(sockDomain, &addr.sa_generic, name_buf, + sizeof(name_buf)); +#endif + name_oss << rName << " ("; + if(addr_str == NULL) + { + name_oss << "failed to convert IP address to string"; + } + else + { + name_oss << addr_str; + } + name_oss << ")"; + name_pretty = name_oss.str(); + } + if(::connect(mSocketHandle, &addr.sa_generic, addrLen) == -1) { // Dispose of the socket try { THROW_EXCEPTION_MESSAGE(ServerException, SocketOpenError, - BOX_SOCKET_ERROR_MESSAGE(Type, rName, Port, + BOX_SOCKET_ERROR_MESSAGE(Type, name_pretty, Port, "Failed to connect to socket")); } catch(ServerException &e)