Skip to content

Commit

Permalink
Improve error message on hostname lookup failure
Browse files Browse the repository at this point in the history
Throw exceptions with a message, including the OS error, which means retrieving
it properly from the winsock library on Windows.
  • Loading branch information
qris committed Aug 4, 2017
1 parent 01e238b commit 4285f25
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
6 changes: 6 additions & 0 deletions lib/common/Logging.h
Expand Up @@ -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, \
Expand All @@ -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, \
Expand Down
26 changes: 10 additions & 16 deletions lib/server/Socket.cpp
Expand Up @@ -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;
Expand Down

0 comments on commit 4285f25

Please sign in to comment.