diff options
author | Adam <Adam@anope.org> | 2013-07-03 17:26:45 -0400 |
---|---|---|
committer | attilamolnar <attilamolnar@hush.com> | 2013-07-14 20:23:53 +0200 |
commit | 6e0b904d342461cd2ac2a3cd0cf2a43d864d2b00 (patch) | |
tree | 980eb8923398714aeef820d0bb6b719f3b149837 /src | |
parent | 2a9aa9be8ac4a97ce766c797aff76abf135bb139 (diff) |
Use the correct socket related error messages on Windows
Diffstat (limited to 'src')
-rw-r--r-- | src/inspsocket.cpp | 10 | ||||
-rw-r--r-- | src/socketengine.cpp | 23 |
2 files changed, 28 insertions, 5 deletions
diff --git a/src/inspsocket.cpp b/src/inspsocket.cpp index 410f928d9..356904f74 100644 --- a/src/inspsocket.cpp +++ b/src/inspsocket.cpp @@ -56,7 +56,7 @@ void BufferedSocket::DoConnect(const std::string &ipaddr, int aport, unsigned lo if (err != I_ERR_NONE) { state = I_ERROR; - SetError(strerror(errno)); + SetError(SocketEngine::LastError()); OnError(err); } } @@ -210,7 +210,7 @@ void StreamSocket::DoRead() } else { - error = strerror(errno); + error = SocketEngine::LastError(); ServerInstance->SE->ChangeEventMask(this, FD_WANT_NO_READ | FD_WANT_NO_WRITE); } } @@ -296,7 +296,7 @@ void StreamSocket::DoWrite() if (errno == EINTR || SocketEngine::IgnoreError()) ServerInstance->SE->ChangeEventMask(this, FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK); else - SetError(strerror(errno)); + SetError(SocketEngine::LastError()); return; } else if (rv < itemlen) @@ -401,7 +401,7 @@ void StreamSocket::DoWrite() } else { - error = strerror(errno); + error = SocketEngine::LastError(); } } if (!error.empty()) @@ -496,7 +496,7 @@ void StreamSocket::HandleEvent(EventType et, int errornum) if (errornum == 0) SetError("Connection closed"); else - SetError(strerror(errornum)); + SetError(SocketEngine::GetError(errornum)); switch (errornum) { case ETIMEDOUT: diff --git a/src/socketengine.cpp b/src/socketengine.cpp index 6c99edc95..8af598b06 100644 --- a/src/socketengine.cpp +++ b/src/socketengine.cpp @@ -255,3 +255,26 @@ void SocketEngine::GetStats(float &kbitpersec_in, float &kbitpersec_out, float & kbitpersec_in = in_kbit / 1024; kbitpersec_out = out_kbit / 1024; } + +std::string SocketEngine::LastError() +{ +#ifndef _WIN32 + return strerror(errno); +#else + char szErrorString[500]; + DWORD dwErrorCode = WSAGetLastError(); + if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)szErrorString, _countof(szErrorString), NULL) == 0) + sprintf_s(szErrorString, _countof(szErrorString), "Error code: %u", dwErrorCode); + return szErrorString; +#endif +} + +std::string SocketEngine::GetError(int errnum) +{ +#ifndef _WIN32 + return strerror(errnum); +#else + WSASetLastError(errnum); + return LastError(); +#endif +} |