#715: improved reporting for binding errors

on Windows
This commit is contained in:
Andrey Prygunkov
2021-04-23 20:24:42 +02:00
parent 799de88b3e
commit 0432cf13d3
2 changed files with 43 additions and 32 deletions

View File

@@ -209,6 +209,8 @@ bool Connection::Bind()
return true; return true;
} }
int errcode = 0;
#ifndef WIN32 #ifndef WIN32
if (m_host && m_host[0] == '/') if (m_host && m_host[0] == '/')
{ {
@@ -280,6 +282,7 @@ bool Connection::Bind()
break; break;
} }
// Connection failed // Connection failed
errcode = GetLastNetworkError();
closesocket(m_socket); closesocket(m_socket);
m_socket = INVALID_SOCKET; m_socket = INVALID_SOCKET;
} }
@@ -320,6 +323,7 @@ bool Connection::Bind()
if (res == -1) if (res == -1)
{ {
// Connection failed // Connection failed
errcode = GetLastNetworkError();
closesocket(m_socket); closesocket(m_socket);
m_socket = INVALID_SOCKET; m_socket = INVALID_SOCKET;
} }
@@ -328,7 +332,7 @@ bool Connection::Bind()
if (m_socket == INVALID_SOCKET) if (m_socket == INVALID_SOCKET)
{ {
ReportError("Binding socket failed for %s", m_host, true); ReportError("Binding socket failed for %s", m_host, true, errcode);
return false; return false;
} }
@@ -785,18 +789,15 @@ bool Connection::ConnectWithTimeout(void* address, int address_len)
ret = connect(m_socket, (struct sockaddr*)address, address_len); ret = connect(m_socket, (struct sockaddr*)address, address_len);
if (ret < 0) if (ret < 0)
{ {
int err = GetLastNetworkError();
#ifdef WIN32 #ifdef WIN32
int err = WSAGetLastError();
if (err != WSAEWOULDBLOCK) if (err != WSAEWOULDBLOCK)
{
return false;
}
#else #else
if (errno != EINPROGRESS) if (err != EINPROGRESS)
#endif
{ {
return false; return false;
} }
#endif
} }
//connect succeeded right away? //connect succeeded right away?
@@ -916,7 +917,16 @@ void Connection::Cancel()
} }
} }
void Connection::ReportError(const char* msgPrefix, const char* msgArg, bool PrintErrCode, int herrno, const char* herrMsg) int Connection::GetLastNetworkError()
{
#ifdef WIN32
return WSAGetLastError();
#else
return errno;
#endif
}
void Connection::ReportError(const char* msgPrefix, const char* msgArg, bool printErrCode, int errCode, const char* errMsg)
{ {
#ifndef DISABLE_TLS #ifndef DISABLE_TLS
if (m_tlsError) if (m_tlsError)
@@ -929,34 +939,34 @@ void Connection::ReportError(const char* msgPrefix, const char* msgArg, bool Pri
BString<1024> errPrefix(msgPrefix, msgArg); BString<1024> errPrefix(msgPrefix, msgArg);
if (PrintErrCode) if (printErrCode)
{ {
#ifdef WIN32 BString<1024> printErrMsg;
int ErrCode = WSAGetLastError(); if (errCode == 0)
char errMsg[1024];
errMsg[0] = '\0';
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, ErrCode, 0, errMsg, 1024, nullptr);
errMsg[1024-1] = '\0';
#else
const char* errMsg = herrMsg;
int ErrCode = herrno;
if (herrno == 0)
{ {
ErrCode = errno; errCode = GetLastNetworkError();
errMsg = strerror(ErrCode);
} }
else if (!herrMsg) if (errMsg)
{ {
errMsg = hstrerror(ErrCode); printErrMsg = errMsg;
}
#endif
if (m_suppressErrors)
{
debug("%s: ErrNo %i, %s", *errPrefix, ErrCode, errMsg);
} }
else else
{ {
PrintError(BString<1024>("%s: ErrNo %i, %s", *errPrefix, ErrCode, errMsg)); #ifdef WIN32
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, errCode, 0, printErrMsg, printErrMsg.Capacity(), nullptr);
printErrMsg[1024-1] = '\0';
#else
printErrMsg = strerror(errCode);
#endif
}
if (m_suppressErrors)
{
debug("%s: Error %i - %s", *errPrefix, errCode, (const char*)printErrMsg);
}
else
{
PrintError(BString<1024>("%s: Error %i - %s", *errPrefix, errCode, (const char*)printErrMsg));
} }
} }
else else
@@ -1074,7 +1084,7 @@ in_addr_t Connection::ResolveHostAddr(const char* host)
#endif #endif
if (err) if (err)
{ {
ReportError("Could not resolve hostname %s", host, true, h_errnop); ReportError("Could not resolve hostname %s", host, true, h_errnop, hstrerror(h_errnop));
return INADDR_NONE; return INADDR_NONE;
} }

View File

@@ -136,9 +136,10 @@ protected:
#endif #endif
#endif #endif
void ReportError(const char* msgPrefix, const char* msgArg, bool PrintErrCode, int herrno = 0, void ReportError(const char* msgPrefix, const char* msgArg, bool printErrCode, int errCode = 0,
const char* herrMsg = nullptr); const char* errMsg = nullptr);
virtual void PrintError(const char* errMsg); virtual void PrintError(const char* errMsg);
int GetLastNetworkError();
bool DoConnect(); bool DoConnect();
bool DoDisconnect(); bool DoDisconnect();
bool InitSocketOpts(SOCKET socket); bool InitSocketOpts(SOCKET socket);