From 9ffca127090e98f473a3a6b686804672fa0c40b0 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Mon, 12 Jan 2015 10:54:28 +0100 Subject: [PATCH] cNetwork: Fixed Linux compilation. --- src/OSSupport/Network.cpp | 28 +++++++++++++++++++++++----- tests/Network/EchoServer.cpp | 2 +- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/OSSupport/Network.cpp b/src/OSSupport/Network.cpp index 2c9b2ef37..a34f7ebca 100644 --- a/src/OSSupport/Network.cpp +++ b/src/OSSupport/Network.cpp @@ -310,6 +310,22 @@ protected: +//////////////////////////////////////////////////////////////////////////////// +// Globals: + +bool IsValidSocket(evutil_socket_t a_Socket) +{ + #ifdef _WIN32 + return (a_Socket != INVALID_SOCKET); + #else // _WIN32 + return (a_Socket >= 0); + #endif // else _WIN32 +} + + + + + //////////////////////////////////////////////////////////////////////////////// // cHostnameLookup: @@ -683,7 +699,7 @@ void cTCPLinkImpl::UpdateAddress(const sockaddr * a_Address, int a_AddrLen, AStr void cTCPLinkImpl::UpdateLocalAddress(void) { sockaddr_storage sa; - int salen = static_cast(sizeof(sa)); + socklen_t salen = static_cast(sizeof(sa)); getsockname(bufferevent_getfd(m_BufferEvent), reinterpret_cast(&sa), &salen); UpdateAddress(reinterpret_cast(&sa), salen, m_LocalIP, m_LocalPort); } @@ -695,7 +711,7 @@ void cTCPLinkImpl::UpdateLocalAddress(void) void cTCPLinkImpl::UpdateRemoteAddress(void) { sockaddr_storage sa; - int salen = static_cast(sizeof(sa)); + socklen_t salen = static_cast(sizeof(sa)); getpeername(bufferevent_getfd(m_BufferEvent), reinterpret_cast(&sa), &salen); UpdateAddress(reinterpret_cast(&sa), salen, m_LocalIP, m_LocalPort); } @@ -753,12 +769,14 @@ bool cServerHandleImpl::Listen(UInt16 a_Port) // It should listen on IPv6 with IPv4 fallback, when available; IPv4 when IPv6 is not available. bool NeedsTwoSockets = false; evutil_socket_t MainSock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); - if (MainSock == SOCKET_ERROR) + if (!IsValidSocket(MainSock)) { // Failed to create IPv6 socket, create an IPv4 one instead: MainSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (MainSock == SOCKET_ERROR) + if (!IsValidSocket(MainSock)) { + int err = EVUTIL_SOCKET_ERROR(); + LOGWARNING("%s: Cannot create a socket for neither IPv6 nor IPv4: %d (%s)", __FUNCTION__, err, evutil_socket_error_to_string(err)); return false; } @@ -822,7 +840,7 @@ bool cServerHandleImpl::Listen(UInt16 a_Port) if (NeedsTwoSockets) { evutil_socket_t SecondSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (SecondSock != SOCKET_ERROR) + if (!IsValidSocket(SecondSock)) { evutil_make_socket_nonblocking(SecondSock); m_SecondaryConnListener = evconnlistener_new(cNetworkSingleton::Get().m_EventBase, Callback, this, LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, 0, SecondSock); diff --git a/tests/Network/EchoServer.cpp b/tests/Network/EchoServer.cpp index 2316d1177..3d9a6228d 100644 --- a/tests/Network/EchoServer.cpp +++ b/tests/Network/EchoServer.cpp @@ -17,7 +17,7 @@ class cEchoServerCallbacks: { virtual void OnAccepted(cTCPLink & a_Link) override { - LOGD("New client accepted (%s:%p), sending welcome message.", a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort()); + LOGD("New client accepted (%s:%d), sending welcome message.", a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort()); // Send a welcome message to each connecting client: a_Link.Send("Welcome to the simple echo server.\r\n"); LOGD("Welcome message queued.");