summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/socketengine.h8
-rw-r--r--src/inspsocket.cpp10
-rw-r--r--src/socketengine.cpp23
3 files changed, 36 insertions, 5 deletions
diff --git a/include/socketengine.h b/include/socketengine.h
index 2fc3cdbfd..37b7d6373 100644
--- a/include/socketengine.h
+++ b/include/socketengine.h
@@ -494,6 +494,14 @@ public:
* Checks EAGAIN and WSAEWOULDBLOCK
*/
static bool IgnoreError();
+
+ /** Return the last socket related error. strrerror(errno) on *nix
+ */
+ static std::string LastError();
+
+ /** Returns the error for the given error num, strerror(errnum) on *nix
+ */
+ static std::string GetError(int errnum);
};
inline bool SocketEngine::IgnoreError()
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
+}