sstream.cpp: better error reporting when things go wrong (specifically, say which host is responsible)

metadata
Wenzel Jakob 2013-12-13 17:02:21 +01:00
parent a424a29602
commit f24b51666a
4 changed files with 17 additions and 14 deletions

View File

@ -78,7 +78,10 @@ public:
std::string toString() const; std::string toString() const;
/// Handle the last socket-specific error (looks up the appropriate OS description) /// Handle the last socket-specific error (looks up the appropriate OS description)
static bool handleError(const std::string &cmd, ELogLevel level = EError); inline bool handleError(const std::string &cmd, ELogLevel level = EError) { return SocketStream::handleError(m_peer, cmd, level); }
/// Handle the last socket-specific error (looks up the appropriate OS description)
static bool handleError(const std::string &peer, const std::string &cmd, ELogLevel level = EError);
//! @} //! @}
// ============================================================= // =============================================================

View File

@ -246,15 +246,15 @@ void SocketStream::flush() {
/* Ignore */ /* Ignore */
} }
bool SocketStream::handleError(const std::string &cmd, ELogLevel level) { bool SocketStream::handleError(const std::string &peer, const std::string &cmd, ELogLevel level) {
#if !defined(__WINDOWS__) #if !defined(__WINDOWS__)
if (level == EWarn && errno == EINTR) /* This is not really a warning -- just retry the operation. */ if (level == EWarn && errno == EINTR) /* This is not really a warning -- just retry the operation. */
return false; return false;
if (cmd.find("(") == std::string::npos) if (cmd.find("(") == std::string::npos)
Log(level, "Error in %s(): %s!", cmd.c_str(), strerror(errno)); Log(level, "[%s] Error in %s(): %s!", peer.c_str(), cmd.c_str(), strerror(errno));
else else
Log(level, "Error in %s: %s!", cmd.c_str(), strerror(errno)); Log(level, "[%s] Error in %s: %s!", peer.c_str(), cmd.c_str(), strerror(errno));
#else #else
std::string err; std::string err;
int error = WSAGetLastError(); int error = WSAGetLastError();

View File

@ -293,15 +293,15 @@ int mtssrv(int argc, char **argv) {
/* Allocate a socket */ /* Allocate a socket */
sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol); sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (sock == -1) if (sock == -1)
SocketStream::handleError("socket"); SocketStream::handleError("none", "socket");
/* Avoid "bind: socket already in use" */ /* Avoid "bind: socket already in use" */
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(int)) < 0) if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(int)) < 0)
SocketStream::handleError("setsockopt"); SocketStream::handleError("none", "setsockopt");
/* Bind the socket to the port number */ /* Bind the socket to the port number */
if (bind(sock, p->ai_addr, (socklen_t) p->ai_addrlen) == -1) { if (bind(sock, p->ai_addr, (socklen_t) p->ai_addrlen) == -1) {
SocketStream::handleError(formatString("bind(%s:%i)", hostName.c_str(), listenPort), EError); SocketStream::handleError("none", formatString("bind(%s:%i)", hostName.c_str(), listenPort), EError);
#if defined(__WINDOWS__) #if defined(__WINDOWS__)
closesocket(sock); closesocket(sock);
#else #else
@ -317,7 +317,7 @@ int mtssrv(int argc, char **argv) {
freeaddrinfo(servinfo); freeaddrinfo(servinfo);
if (listen(sock, CONN_BACKLOG) == -1) if (listen(sock, CONN_BACKLOG) == -1)
SocketStream::handleError("bind"); SocketStream::handleError("none", "bind");
SLog(EInfo, "Enter mtssrv -h for more options"); SLog(EInfo, "Enter mtssrv -h for more options");
#if defined(__WINDOWS__) #if defined(__WINDOWS__)
@ -358,7 +358,7 @@ int mtssrv(int argc, char **argv) {
if (errno == EINTR) if (errno == EINTR)
continue; continue;
#endif #endif
SocketStream::handleError("accept", EWarn); SocketStream::handleError("none", "accept", EWarn);
continue; continue;
} }

View File

@ -57,15 +57,15 @@ void ServerThread::run() {
/* Allocate a socket */ /* Allocate a socket */
m_socket = socket(p->ai_family, p->ai_socktype, p->ai_protocol); m_socket = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (m_socket == -1) if (m_socket == -1)
SocketStream::handleError("socket"); SocketStream::handleError("none", "socket");
/* Avoid "bind: socket already in use" */ /* Avoid "bind: socket already in use" */
if (setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(int)) < 0) if (setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(int)) < 0)
SocketStream::handleError("setsockopt"); SocketStream::handleError("none", "setsockopt");
/* Bind the socket to the port number */ /* Bind the socket to the port number */
if (bind(m_socket, p->ai_addr, (socklen_t) p->ai_addrlen) == -1) { if (bind(m_socket, p->ai_addr, (socklen_t) p->ai_addrlen) == -1) {
SocketStream::handleError(formatString("bind(%s:%i)", m_nodeName.c_str(), m_listenPort), EError); SocketStream::handleError("none", formatString("bind(%s:%i)", m_nodeName.c_str(), m_listenPort), EError);
#if defined(__WINDOWS__) #if defined(__WINDOWS__)
closesocket(m_socket); closesocket(m_socket);
#else #else
@ -81,7 +81,7 @@ void ServerThread::run() {
freeaddrinfo(servinfo); freeaddrinfo(servinfo);
if (listen(m_socket, CONN_BACKLOG) == -1) if (listen(m_socket, CONN_BACKLOG) == -1)
SocketStream::handleError("bind"); SocketStream::handleError("none", "bind");
SLog(EInfo, "%s: Listening on port %i.. Close the window to stop.", SLog(EInfo, "%s: Listening on port %i.. Close the window to stop.",
m_nodeName.c_str(), m_listenPort); m_nodeName.c_str(), m_listenPort);
@ -114,7 +114,7 @@ void ServerThread::run() {
if (errno == EINTR) if (errno == EINTR)
continue; continue;
#endif #endif
SocketStream::handleError("accept", EWarn); SocketStream::handleError("none", "accept", EWarn);
continue; continue;
} }