From 4285f2560793dcb388c051cf72854bf8f3e2027d Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Fri, 4 Aug 2017 20:46:42 +0100 Subject: [PATCH] Improve error message on hostname lookup failure Throw exceptions with a message, including the OS error, which means retrieving it properly from the winsock library on Windows. --- lib/common/Logging.h | 6 ++++++ lib/server/Socket.cpp | 26 ++++++++++---------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/common/Logging.h b/lib/common/Logging.h index 7f80d84c0..bcc57d3d1 100644 --- a/lib/common/Logging.h +++ b/lib/common/Logging.h @@ -112,12 +112,16 @@ #define THROW_WIN_ERROR_NUMBER(message, error_number, exception, subtype) \ THROW_EXCEPTION_MESSAGE(exception, subtype, \ BOX_WIN_ERRNO_MESSAGE(error_number, message)) + #define THROW_WIN_ERROR(message, exception, subtype) \ + THROW_WIN_ERROR_NUMBER(message, GetLastError(), exception, subtype) #define THROW_WIN_FILE_ERRNO(message, filename, error_number, exception, subtype) \ THROW_WIN_ERROR_NUMBER(BOX_FILE_MESSAGE(filename, message), \ error_number, exception, subtype) #define THROW_WIN_FILE_ERROR(message, filename, exception, subtype) \ THROW_WIN_FILE_ERRNO(message, filename, GetLastError(), \ exception, subtype) + #define THROW_SOCKET_ERROR(message, exception, subtype) \ + THROW_WIN_ERROR_NUMBER(message, WSAGetLastError(), exception, subtype) #define EMU_ERRNO winerrno #define THROW_EMU_ERROR(message, exception, subtype) \ THROW_EXCEPTION_MESSAGE(exception, subtype, \ @@ -127,6 +131,8 @@ BOX_SYS_ERRNO_MESSAGE(error_number, stuff) #define BOX_LOG_NATIVE_ERROR(stuff) BOX_LOG_SYS_ERROR(stuff) #define BOX_LOG_NATIVE_WARNING(stuff) BOX_LOG_SYS_WARNING(stuff) + #define THROW_SOCKET_ERROR(message, exception, subtype) \ + THROW_SYS_ERROR(message, exception, subtype) #define EMU_ERRNO errno #define THROW_EMU_ERROR(message, exception, subtype) \ THROW_EXCEPTION_MESSAGE(exception, subtype, \ diff --git a/lib/server/Socket.cpp b/lib/server/Socket.cpp index c9c1773db..44752fcb2 100644 --- a/lib/server/Socket.cpp +++ b/lib/server/Socket.cpp @@ -52,30 +52,24 @@ void Socket::NameLookupToSockAddr(SocketAllAddr &addr, int &sockDomain, { // Lookup hostname struct hostent *phost = ::gethostbyname(rName.c_str()); - if(phost != NULL) + if(phost != NULL && phost->h_addr_list[0] != 0) { - if(phost->h_addr_list[0] != 0) - { - sockAddrLen = sizeof(addr.sa_inet); + sockAddrLen = sizeof(addr.sa_inet); #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN - addr.sa_inet.sin_len = sizeof(addr.sa_inet); + addr.sa_inet.sin_len = sizeof(addr.sa_inet); #endif - addr.sa_inet.sin_family = PF_INET; - addr.sa_inet.sin_port = htons(Port); - addr.sa_inet.sin_addr = *((in_addr*)phost->h_addr_list[0]); - for(unsigned int l = 0; l < sizeof(addr.sa_inet.sin_zero); ++l) - { - addr.sa_inet.sin_zero[l] = 0; - } - } - else + addr.sa_inet.sin_family = PF_INET; + addr.sa_inet.sin_port = htons(Port); + addr.sa_inet.sin_addr = *((in_addr*)phost->h_addr_list[0]); + for(unsigned int l = 0; l < sizeof(addr.sa_inet.sin_zero); ++l) { - THROW_EXCEPTION(ConnectionException, SocketNameLookupError); + addr.sa_inet.sin_zero[l] = 0; } } else { - THROW_EXCEPTION(ConnectionException, SocketNameLookupError); + THROW_SOCKET_ERROR("Failed to resolve hostname: " << rName, + ConnectionException, SocketNameLookupError); } } break;