From 24987e069496b185ad88de09657dced2a73189ef Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Tue, 6 Mar 2018 13:55:27 -0500 Subject: [PATCH] merge modified patch from synergy PR https://github.com/symless/synergy-core/pull/6178 --- src/lib/arch/IArchNetwork.h | 1 + src/lib/arch/unix/ArchNetworkBSD.cpp | 131 ++++++++++++--------- src/lib/arch/unix/ArchNetworkBSD.h | 5 +- src/lib/arch/win32/ArchNetworkWinsock.cpp | 137 +++++++++++++--------- src/lib/arch/win32/ArchNetworkWinsock.h | 9 +- src/lib/client/Client.cpp | 4 +- src/lib/ipc/IpcClient.cpp | 4 +- src/lib/ipc/IpcServer.cpp | 2 +- src/lib/net/ISocketFactory.h | 9 +- src/lib/net/NetworkAddress.cpp | 2 +- src/lib/net/SecureListenSocket.cpp | 5 +- src/lib/net/SecureListenSocket.h | 3 +- src/lib/net/SecureSocket.cpp | 5 +- src/lib/net/SecureSocket.h | 2 +- src/lib/net/TCPListenSocket.cpp | 4 +- src/lib/net/TCPListenSocket.h | 2 +- src/lib/net/TCPSocket.cpp | 4 +- src/lib/net/TCPSocket.h | 2 +- src/lib/net/TCPSocketFactory.cpp | 12 +- src/lib/net/TCPSocketFactory.h | 11 +- src/lib/server/ClientListener.cpp | 4 +- 21 files changed, 213 insertions(+), 145 deletions(-) diff --git a/src/lib/arch/IArchNetwork.h b/src/lib/arch/IArchNetwork.h index 97e6e877..b8595061 100644 --- a/src/lib/arch/IArchNetwork.h +++ b/src/lib/arch/IArchNetwork.h @@ -64,6 +64,7 @@ public: enum EAddressFamily { kUNKNOWN, kINET, + kINET6, }; //! Supported socket types diff --git a/src/lib/arch/unix/ArchNetworkBSD.cpp b/src/lib/arch/unix/ArchNetworkBSD.cpp index c4429e8a..149218c8 100644 --- a/src/lib/arch/unix/ArchNetworkBSD.cpp +++ b/src/lib/arch/unix/ArchNetworkBSD.cpp @@ -52,7 +52,8 @@ static const int s_family[] = { PF_UNSPEC, - PF_INET + PF_INET, + PF_INET6, }; static const int s_type[] = { SOCK_DGRAM, @@ -191,7 +192,7 @@ ArchNetworkBSD::bindSocket(ArchSocket s, ArchNetAddress addr) assert(s != NULL); assert(addr != NULL); - if (bind(s->m_fd, &addr->m_addr, addr->m_len) == -1) { + if (bind(s->m_fd, TYPED_ADDR(struct sockaddr, addr), addr->m_len) == -1) { throwError(errno); } } @@ -224,7 +225,7 @@ ArchNetworkBSD::acceptSocket(ArchSocket s, ArchNetAddress* addr) // accept on socket ACCEPT_TYPE_ARG3 len = (ACCEPT_TYPE_ARG3)((*addr)->m_len); - int fd = accept(s->m_fd, &(*addr)->m_addr, &len); + int fd = accept(s->m_fd, TYPED_ADDR(struct sockaddr, (*addr)), &len); (*addr)->m_len = (socklen_t)len; if (fd == -1) { int err = errno; @@ -266,7 +267,7 @@ ArchNetworkBSD::connectSocket(ArchSocket s, ArchNetAddress addr) assert(s != NULL); assert(addr != NULL); - if (connect(s->m_fd, &addr->m_addr, addr->m_len) == -1) { + if (connect(s->m_fd, TYPED_ADDR(struct sockaddr, addr), addr->m_len) == -1) { if (errno == EISCONN) { return true; } @@ -645,8 +646,7 @@ ArchNetworkBSD::newAnyAddr(EAddressFamily family) // fill it in switch (family) { case kINET: { - struct sockaddr_in* ipAddr = - reinterpret_cast(&addr->m_addr); + auto* ipAddr = TYPED_ADDR(struct sockaddr_in, addr); ipAddr->sin_family = AF_INET; ipAddr->sin_port = 0; ipAddr->sin_addr.s_addr = INADDR_ANY; @@ -654,6 +654,14 @@ ArchNetworkBSD::newAnyAddr(EAddressFamily family) break; } + case kINET6: { + auto* ipAddr = TYPED_ADDR(struct sockaddr_in6, addr); + ipAddr->sin6_family = AF_INET6; + ipAddr->sin6_port = 0; + memcpy(&ipAddr->sin6_addr, &in6addr_any, sizeof(in6addr_any)); + addr->m_len = (socklen_t)sizeof(struct sockaddr_in6); + break; + } default: delete addr; assert(0 && "invalid family"); @@ -677,48 +685,31 @@ ArchNetworkBSD::nameToAddr(const std::string& name) // allocate address ArchNetAddressImpl* addr = new ArchNetAddressImpl; - // try to convert assuming an IPv4 dot notation address - struct sockaddr_in inaddr; - memset(&inaddr, 0, sizeof(inaddr)); - if (inet_aton(name.c_str(), &inaddr.sin_addr) != 0) { - // it's a dot notation address - addr->m_len = (socklen_t)sizeof(struct sockaddr_in); - inaddr.sin_family = AF_INET; - inaddr.sin_port = 0; - memcpy(&addr->m_addr, &inaddr, addr->m_len); - } + char ipstr[INET6_ADDRSTRLEN]; + struct addrinfo hints; + struct addrinfo *p; + int ret; - else { - // mutexed address lookup (ugh) - ARCH->lockMutex(m_mutex); - struct hostent* info = gethostbyname(name.c_str()); - if (info == NULL) { - ARCH->unlockMutex(m_mutex); - delete addr; - throwNameError(h_errno); - } + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; - // copy over address (only IPv4 currently supported) - if (info->h_addrtype == AF_INET) { - addr->m_len = (socklen_t)sizeof(struct sockaddr_in); - inaddr.sin_family = info->h_addrtype; - inaddr.sin_port = 0; - memcpy(&inaddr.sin_addr, info->h_addr_list[0], - sizeof(inaddr.sin_addr)); - memcpy(&addr->m_addr, &inaddr, addr->m_len); - } - else { - ARCH->unlockMutex(m_mutex); - delete addr; - throw XArchNetworkNameUnsupported( - "The requested name is valid but " - "does not have a supported address family"); - } - - // done with static buffer + ARCH->lockMutex(m_mutex); + if ((ret = getaddrinfo(name.c_str(), NULL, &hints, &p)) != 0) { ARCH->unlockMutex(m_mutex); + delete addr; + throwNameError(ret); } + if (p->ai_family == AF_INET) { + addr->m_len = (socklen_t)sizeof(struct sockaddr_in); + } else { + addr->m_len = (socklen_t)sizeof(struct sockaddr_in6); + } + + memcpy(&addr->m_addr, p->ai_addr, addr->m_len); + freeaddrinfo(p); + ARCH->unlockMutex(m_mutex); + return addr; } @@ -737,15 +728,17 @@ ArchNetworkBSD::addrToName(ArchNetAddress addr) // mutexed name lookup (ugh) ARCH->lockMutex(m_mutex); - struct hostent* info = gethostbyaddr(&addr->m_addr, - addr->m_len, addr->m_addr.sa_family); - if (info == NULL) { + char host[1024]; + char service[20]; + int ret = getnameinfo(TYPED_ADDR(struct sockaddr, addr), addr->m_len, host, + sizeof(host), service, sizeof(service), 0); + if (ret != 0) { ARCH->unlockMutex(m_mutex); - throwNameError(h_errno); + throwNameError(ret); } // save (primary) name - std::string name = info->h_name; + std::string name = host; // done with static buffer ARCH->unlockMutex(m_mutex); @@ -760,14 +753,22 @@ ArchNetworkBSD::addrToString(ArchNetAddress addr) switch (getAddrFamily(addr)) { case kINET: { - struct sockaddr_in* ipAddr = - reinterpret_cast(&addr->m_addr); + auto* ipAddr = TYPED_ADDR(struct sockaddr_in, addr); ARCH->lockMutex(m_mutex); std::string s = inet_ntoa(ipAddr->sin_addr); ARCH->unlockMutex(m_mutex); return s; } + case kINET6: { + char strAddr[INET6_ADDRSTRLEN]; + auto* ipAddr = TYPED_ADDR(struct sockaddr_in6, addr); + ARCH->lockMutex(m_mutex); + inet_ntop(AF_INET6, &ipAddr->sin6_addr, strAddr, INET6_ADDRSTRLEN); + ARCH->unlockMutex(m_mutex); + return strAddr; + } + default: assert(0 && "unknown address family"); return ""; @@ -779,9 +780,11 @@ ArchNetworkBSD::getAddrFamily(ArchNetAddress addr) { assert(addr != NULL); - switch (addr->m_addr.sa_family) { + switch (addr->m_addr.ss_family) { case AF_INET: return kINET; + case AF_INET6: + return kINET6; default: return kUNKNOWN; @@ -795,12 +798,17 @@ ArchNetworkBSD::setAddrPort(ArchNetAddress addr, int port) switch (getAddrFamily(addr)) { case kINET: { - struct sockaddr_in* ipAddr = - reinterpret_cast(&addr->m_addr); + auto* ipAddr = TYPED_ADDR(struct sockaddr_in, addr); ipAddr->sin_port = htons(port); break; } + case kINET6: { + auto* ipAddr = TYPED_ADDR(struct sockaddr_in6, addr); + ipAddr->sin6_port = htons(port); + break; + } + default: assert(0 && "unknown address family"); break; @@ -814,11 +822,15 @@ ArchNetworkBSD::getAddrPort(ArchNetAddress addr) switch (getAddrFamily(addr)) { case kINET: { - struct sockaddr_in* ipAddr = - reinterpret_cast(&addr->m_addr); + auto* ipAddr = TYPED_ADDR(struct sockaddr_in, addr); return ntohs(ipAddr->sin_port); } + case kINET6: { + auto* ipAddr = TYPED_ADDR(struct sockaddr_in6, addr); + return ntohs(ipAddr->sin6_port); + } + default: assert(0 && "unknown address family"); return 0; @@ -832,12 +844,17 @@ ArchNetworkBSD::isAnyAddr(ArchNetAddress addr) switch (getAddrFamily(addr)) { case kINET: { - struct sockaddr_in* ipAddr = - reinterpret_cast(&addr->m_addr); + auto* ipAddr = TYPED_ADDR(struct sockaddr_in, addr); return (ipAddr->sin_addr.s_addr == INADDR_ANY && addr->m_len == (socklen_t)sizeof(struct sockaddr_in)); } + case kINET6: { + auto* ipAddr = TYPED_ADDR(struct sockaddr_in6, addr); + return (memcmp(&ipAddr->sin6_addr, &in6addr_any, sizeof(in6addr_any)) == 0 && + addr->m_len == (socklen_t)sizeof(struct sockaddr_in6)); + } + default: assert(0 && "unknown address family"); return true; diff --git a/src/lib/arch/unix/ArchNetworkBSD.h b/src/lib/arch/unix/ArchNetworkBSD.h index 4c26ed06..3f5679a3 100644 --- a/src/lib/arch/unix/ArchNetworkBSD.h +++ b/src/lib/arch/unix/ArchNetworkBSD.h @@ -38,6 +38,7 @@ typedef int socklen_t; typedef char optval_t; #define ARCH_NETWORK ArchNetworkBSD +#define TYPED_ADDR(type_, addr_) (reinterpret_cast(&addr_->m_addr)) class ArchSocketImpl { public: @@ -50,8 +51,8 @@ public: ArchNetAddressImpl() : m_len(sizeof(m_addr)) { } public: - struct sockaddr m_addr; - socklen_t m_len; + struct sockaddr_storage m_addr; + socklen_t m_len; }; //! Berkeley (BSD) sockets implementation of IArchNetwork diff --git a/src/lib/arch/win32/ArchNetworkWinsock.cpp b/src/lib/arch/win32/ArchNetworkWinsock.cpp index b6bf2397..722c4c58 100644 --- a/src/lib/arch/win32/ArchNetworkWinsock.cpp +++ b/src/lib/arch/win32/ArchNetworkWinsock.cpp @@ -26,7 +26,8 @@ static const int s_family[] = { PF_UNSPEC, - PF_INET + PF_INET, + PF_INET6, }; static const int s_type[] = { SOCK_DGRAM, @@ -156,7 +157,7 @@ ArchNetworkWinsock::initModule(HMODULE module) setfunc(startup, WSAStartup, int(PASCAL FAR*)(WORD, LPWSADATA)); // startup network library - WORD version = MAKEWORD(2 /*major*/, 0 /*minor*/); + WORD version = MAKEWORD(2 /*major*/, 2 /*minor*/); WSADATA data; int err = startup(version, &data); if (data.wVersion != version) { @@ -212,6 +213,11 @@ ArchNetworkWinsock::newSocket(EAddressFamily family, ESocketType type) } try { setBlockingOnSocket(fd, false); + BOOL flag = 0; + int size = sizeof(flag); + if (setsockopt_winsock(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flag, size) == SOCKET_ERROR) { + throwError(getsockerror_winsock()); + } } catch (...) { close_winsock(fd); @@ -294,7 +300,7 @@ ArchNetworkWinsock::bindSocket(ArchSocket s, ArchNetAddress addr) assert(s != NULL); assert(addr != NULL); - if (bind_winsock(s->m_socket, &addr->m_addr, addr->m_len) == SOCKET_ERROR) { + if (bind_winsock(s->m_socket, TYPED_ADDR(struct sockaddr, addr), addr->m_len) == SOCKET_ERROR) { throwError(getsockerror_winsock()); } } @@ -317,10 +323,10 @@ ArchNetworkWinsock::acceptSocket(ArchSocket s, ArchNetAddress* const addr) // create new socket and temporary address ArchSocketImpl* socket = new ArchSocketImpl; - ArchNetAddress tmp = ArchNetAddressImpl::alloc(sizeof(struct sockaddr)); + ArchNetAddress tmp = ArchNetAddressImpl::alloc(sizeof(struct sockaddr_in6)); // accept on socket - SOCKET fd = accept_winsock(s->m_socket, &tmp->m_addr, &tmp->m_len); + SOCKET fd = accept_winsock(s->m_socket, TYPED_ADDR(struct sockaddr, tmp), &tmp->m_len); if (fd == INVALID_SOCKET) { int err = getsockerror_winsock(); delete socket; @@ -368,7 +374,7 @@ ArchNetworkWinsock::connectSocket(ArchSocket s, ArchNetAddress addr) assert(s != NULL); assert(addr != NULL); - if (connect_winsock(s->m_socket, &addr->m_addr, + if (connect_winsock(s->m_socket, TYPED_ADDR(struct sockaddr, addr), addr->m_len) == SOCKET_ERROR) { if (getsockerror_winsock() == WSAEISCONN) { return true; @@ -678,13 +684,22 @@ ArchNetworkWinsock::newAnyAddr(EAddressFamily family) switch (family) { case kINET: { addr = ArchNetAddressImpl::alloc(sizeof(struct sockaddr_in)); - struct sockaddr_in* ipAddr = TYPED_ADDR(struct sockaddr_in, addr); + auto* ipAddr = TYPED_ADDR(struct sockaddr_in, addr); ipAddr->sin_family = AF_INET; ipAddr->sin_port = 0; ipAddr->sin_addr.s_addr = INADDR_ANY; break; } + case kINET6: { + addr = ArchNetAddressImpl::alloc(sizeof(struct sockaddr_in6)); + auto* ipAddr = TYPED_ADDR(struct sockaddr_in6, addr); + ipAddr->sin6_family = AF_INET6; + ipAddr->sin6_port = 0; + memcpy(&ipAddr->sin6_addr, &in6addr_any, sizeof(in6addr_any)); + break; + } + default: assert(0 && "invalid family"); } @@ -705,41 +720,31 @@ ArchNetAddress ArchNetworkWinsock::nameToAddr(const std::string& name) { // allocate address - ArchNetAddressImpl* addr = NULL; - // try to convert assuming an IPv4 dot notation address - struct sockaddr_in inaddr; - memset(&inaddr, 0, sizeof(inaddr)); - inaddr.sin_family = AF_INET; - inaddr.sin_port = 0; - inaddr.sin_addr.s_addr = inet_addr_winsock(name.c_str()); - if (inaddr.sin_addr.s_addr != INADDR_NONE) { - // it's a dot notation address - addr = ArchNetAddressImpl::alloc(sizeof(struct sockaddr_in)); - memcpy(TYPED_ADDR(void, addr), &inaddr, addr->m_len); + ArchNetAddressImpl* addr = new ArchNetAddressImpl; + + struct addrinfo hints; + struct addrinfo *p; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + int ret = -1; + + ARCH->lockMutex(m_mutex); + if ((ret = getaddrinfo(name.c_str(), NULL, &hints, &p)) != 0) { + ARCH->unlockMutex(m_mutex); + delete addr; + throwNameError(ret); } - else { - // address lookup - struct hostent* info = gethostbyname_winsock(name.c_str()); - if (info == NULL) { - throwNameError(getsockerror_winsock()); - } - - // copy over address (only IPv4 currently supported) - if (info->h_addrtype == AF_INET) { - addr = ArchNetAddressImpl::alloc(sizeof(struct sockaddr_in)); - memcpy(&inaddr.sin_addr, info->h_addr_list[0], - sizeof(inaddr.sin_addr)); - memcpy(TYPED_ADDR(void, addr), &inaddr, addr->m_len); - } - else { - throw XArchNetworkNameUnsupported( - "The requested name is valid but " - "does not have a supported address family"); - } + if (p->ai_family == AF_INET) { + addr->m_len = (socklen_t)sizeof(struct sockaddr_in); + } else { + addr->m_len = (socklen_t)sizeof(struct sockaddr_in6); } + memcpy(&addr->m_addr, p->ai_addr, addr->m_len); + freeaddrinfo(p); + ARCH->unlockMutex(m_mutex); return addr; } @@ -756,16 +761,17 @@ ArchNetworkWinsock::addrToName(ArchNetAddress addr) { assert(addr != NULL); - // name lookup - struct hostent* info = gethostbyaddr_winsock( - reinterpret_cast(&addr->m_addr), - addr->m_len, addr->m_addr.sa_family); - if (info == NULL) { - throwNameError(getsockerror_winsock()); + char host[1024]; + char service[20]; + int ret = getnameinfo(TYPED_ADDR(struct sockaddr, addr), addr->m_len, host, sizeof(host), service, sizeof(service), 0); + + if (ret != NULL) { + throwNameError(ret); } // return (primary) name - return info->h_name; + std::string name = host; + return name; } std::string @@ -775,11 +781,17 @@ ArchNetworkWinsock::addrToString(ArchNetAddress addr) switch (getAddrFamily(addr)) { case kINET: { - struct sockaddr_in* ipAddr = - reinterpret_cast(&addr->m_addr); + auto* ipAddr = TYPED_ADDR(struct sockaddr_in, addr); return inet_ntoa_winsock(ipAddr->sin_addr); } + case kINET6: { + char strAddr[INET6_ADDRSTRLEN]; + auto* ipAddr = TYPED_ADDR(struct sockaddr_in6, addr); + inet_ntop(AF_INET6, &ipAddr->sin6_addr, strAddr, INET6_ADDRSTRLEN); + return strAddr; + } + default: assert(0 && "unknown address family"); return ""; @@ -791,10 +803,13 @@ ArchNetworkWinsock::getAddrFamily(ArchNetAddress addr) { assert(addr != NULL); - switch (addr->m_addr.sa_family) { + switch (addr->m_addr.ss_family) { case AF_INET: return kINET; + case AF_INET6: + return kINET6; + default: return kUNKNOWN; } @@ -807,9 +822,14 @@ ArchNetworkWinsock::setAddrPort(ArchNetAddress addr, int port) switch (getAddrFamily(addr)) { case kINET: { - struct sockaddr_in* ipAddr = - reinterpret_cast(&addr->m_addr); - ipAddr->sin_port = htons_winsock(port); + auto* ipAddr = TYPED_ADDR(struct sockaddr_in, addr); + ipAddr->sin_port = htons_winsock(static_cast(port)); + break; + } + + case kINET6: { + auto* ipAddr = TYPED_ADDR(struct sockaddr_in6, addr); + ipAddr->sin6_port = htons_winsock(static_cast(port)); break; } @@ -826,11 +846,15 @@ ArchNetworkWinsock::getAddrPort(ArchNetAddress addr) switch (getAddrFamily(addr)) { case kINET: { - struct sockaddr_in* ipAddr = - reinterpret_cast(&addr->m_addr); + auto* ipAddr = TYPED_ADDR(struct sockaddr_in, addr); return ntohs_winsock(ipAddr->sin_port); } + case kINET6: { + auto* ipAddr = TYPED_ADDR(struct sockaddr_in6, addr); + return ntohs_winsock(ipAddr->sin6_port); + } + default: assert(0 && "unknown address family"); return 0; @@ -844,12 +868,17 @@ ArchNetworkWinsock::isAnyAddr(ArchNetAddress addr) switch (getAddrFamily(addr)) { case kINET: { - struct sockaddr_in* ipAddr = - reinterpret_cast(&addr->m_addr); + auto* ipAddr = TYPED_ADDR(struct sockaddr_in, addr); return (addr->m_len == sizeof(struct sockaddr_in) && ipAddr->sin_addr.s_addr == INADDR_ANY); } + case kINET6: { + auto* ipAddr = TYPED_ADDR(struct sockaddr_in6, addr); + return (addr->m_len == sizeof(struct sockaddr_in) && + memcmp(&ipAddr->sin6_addr, &in6addr_any, sizeof(in6addr_any))== 0); + } + default: assert(0 && "unknown address family"); return true; diff --git a/src/lib/arch/win32/ArchNetworkWinsock.h b/src/lib/arch/win32/ArchNetworkWinsock.h index 55ae1a6d..0b016712 100644 --- a/src/lib/arch/win32/ArchNetworkWinsock.h +++ b/src/lib/arch/win32/ArchNetworkWinsock.h @@ -18,8 +18,11 @@ #pragma once +#include // declare no functions in winsock2 +#ifndef INCL_WINSOCK_API_PROTOTYPES #define INCL_WINSOCK_API_PROTOTYPES 0 +#endif #define INCL_WINSOCK_API_TYPEDEFS 0 #include "arch/IArchNetwork.h" @@ -30,6 +33,8 @@ #include #include +#pragma comment(lib, "ws2_32.lib") + #define ARCH_NETWORK ArchNetworkWinsock class ArchSocketImpl { @@ -45,8 +50,8 @@ public: static ArchNetAddressImpl* alloc(size_t); public: - int m_len; - struct sockaddr m_addr; + int m_len; + struct sockaddr_storage m_addr; }; #define ADDR_HDR_SIZE offsetof(ArchNetAddressImpl, m_addr) #define TYPED_ADDR(type_, addr_) (reinterpret_cast(&addr_->m_addr)) diff --git a/src/lib/client/Client.cpp b/src/lib/client/Client.cpp index 5b61c4ff..2158ee27 100644 --- a/src/lib/client/Client.cpp +++ b/src/lib/client/Client.cpp @@ -147,7 +147,9 @@ Client::connect() } // create the socket - IDataSocket* socket = m_socketFactory->create(m_useSecureNetwork); + IDataSocket* socket = m_socketFactory->create( + ARCH->getAddrFamily(m_serverAddress.getAddress()), + m_useSecureNetwork); m_socket = dynamic_cast(socket); // filter socket messages, including a packetizing filter diff --git a/src/lib/ipc/IpcClient.cpp b/src/lib/ipc/IpcClient.cpp index 04c04141..4eeae5b8 100644 --- a/src/lib/ipc/IpcClient.cpp +++ b/src/lib/ipc/IpcClient.cpp @@ -28,7 +28,7 @@ IpcClient::IpcClient(IEventQueue* events, SocketMultiplexer* socketMultiplexer) : m_serverAddress(NetworkAddress(IPC_HOST, IPC_PORT)), - m_socket(events, socketMultiplexer), + m_socket(events, socketMultiplexer, IArchNetwork::kINET), m_server(nullptr), m_events(events) { @@ -37,7 +37,7 @@ IpcClient::IpcClient(IEventQueue* events, SocketMultiplexer* socketMultiplexer) IpcClient::IpcClient(IEventQueue* events, SocketMultiplexer* socketMultiplexer, int port) : m_serverAddress(NetworkAddress(IPC_HOST, port)), - m_socket(events, socketMultiplexer), + m_socket(events, socketMultiplexer, IArchNetwork::kINET), m_server(nullptr), m_events(events) { diff --git a/src/lib/ipc/IpcServer.cpp b/src/lib/ipc/IpcServer.cpp index bc778c73..e05a913f 100644 --- a/src/lib/ipc/IpcServer.cpp +++ b/src/lib/ipc/IpcServer.cpp @@ -54,7 +54,7 @@ IpcServer::IpcServer(IEventQueue* events, SocketMultiplexer* socketMultiplexer, void IpcServer::init() { - m_socket = new TCPListenSocket(m_events, m_socketMultiplexer); + m_socket = new TCPListenSocket(m_events, m_socketMultiplexer, IArchNetwork::kINET); m_clientsMutex = ARCH->newMutex(); m_address.resolve(); diff --git a/src/lib/net/ISocketFactory.h b/src/lib/net/ISocketFactory.h index 2bfd5393..e4409533 100644 --- a/src/lib/net/ISocketFactory.h +++ b/src/lib/net/ISocketFactory.h @@ -19,6 +19,7 @@ #pragma once #include "common/IInterface.h" +#include "arch/IArchNetwork.h" class IDataSocket; class IListenSocket; @@ -34,10 +35,14 @@ public: //@{ //! Create data socket - virtual IDataSocket* create(bool secure) const = 0; + virtual IDataSocket* create( + IArchNetwork::EAddressFamily family, + bool secure) const = 0; //! Create listen socket - virtual IListenSocket* createListen(bool secure) const = 0; + virtual IListenSocket* createListen( + IArchNetwork::EAddressFamily family, + bool secure) const = 0; //@} }; diff --git a/src/lib/net/NetworkAddress.cpp b/src/lib/net/NetworkAddress.cpp index 2b8b01d3..c395ab03 100644 --- a/src/lib/net/NetworkAddress.cpp +++ b/src/lib/net/NetworkAddress.cpp @@ -87,7 +87,7 @@ NetworkAddress::NetworkAddress(const String& hostname, int port) : // notation. in that case we assume it's not a port suffix. // the user can replace the double colon with zeros to // disambiguate. - if ((!doubleColon || dotNotation) || !colonNotation) { + if ((!doubleColon || dotNotation) && !colonNotation) { // parse port from hostname char* end; const char* chostname = m_hostname.c_str(); diff --git a/src/lib/net/SecureListenSocket.cpp b/src/lib/net/SecureListenSocket.cpp index 1e279077..58ffe096 100644 --- a/src/lib/net/SecureListenSocket.cpp +++ b/src/lib/net/SecureListenSocket.cpp @@ -32,8 +32,9 @@ static const char s_certificateFilename[] = { "Barrier.pem" }; SecureListenSocket::SecureListenSocket( IEventQueue* events, - SocketMultiplexer* socketMultiplexer) : - TCPListenSocket(events, socketMultiplexer) + SocketMultiplexer* socketMultiplexer, + IArchNetwork::EAddressFamily family) : + TCPListenSocket(events, socketMultiplexer, family) { } diff --git a/src/lib/net/SecureListenSocket.h b/src/lib/net/SecureListenSocket.h index 2bef73df..d0c6e232 100644 --- a/src/lib/net/SecureListenSocket.h +++ b/src/lib/net/SecureListenSocket.h @@ -27,7 +27,8 @@ class IDataSocket; class SecureListenSocket : public TCPListenSocket{ public: SecureListenSocket(IEventQueue* events, - SocketMultiplexer* socketMultiplexer); + SocketMultiplexer* socketMultiplexer, + IArchNetwork::EAddressFamily family); // IListenSocket overrides virtual IDataSocket* diff --git a/src/lib/net/SecureSocket.cpp b/src/lib/net/SecureSocket.cpp index 83a69080..1fefae0d 100644 --- a/src/lib/net/SecureSocket.cpp +++ b/src/lib/net/SecureSocket.cpp @@ -56,8 +56,9 @@ struct Ssl { SecureSocket::SecureSocket( IEventQueue* events, - SocketMultiplexer* socketMultiplexer) : - TCPSocket(events, socketMultiplexer), + SocketMultiplexer* socketMultiplexer, + IArchNetwork::EAddressFamily family) : + TCPSocket(events, socketMultiplexer, family), m_ssl(nullptr), m_secureReady(false), m_fatal(false) diff --git a/src/lib/net/SecureSocket.h b/src/lib/net/SecureSocket.h index 45ae09d3..01d3c3ff 100644 --- a/src/lib/net/SecureSocket.h +++ b/src/lib/net/SecureSocket.h @@ -32,7 +32,7 @@ A secure socket using SSL. */ class SecureSocket : public TCPSocket { public: - SecureSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer); + SecureSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer, IArchNetwork::EAddressFamily family); SecureSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer, ArchSocket socket); diff --git a/src/lib/net/TCPListenSocket.cpp b/src/lib/net/TCPListenSocket.cpp index 294054d8..8e1540e9 100644 --- a/src/lib/net/TCPListenSocket.cpp +++ b/src/lib/net/TCPListenSocket.cpp @@ -34,13 +34,13 @@ // TCPListenSocket // -TCPListenSocket::TCPListenSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer) : +TCPListenSocket::TCPListenSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer, IArchNetwork::EAddressFamily family) : m_events(events), m_socketMultiplexer(socketMultiplexer) { m_mutex = new Mutex; try { - m_socket = ARCH->newSocket(IArchNetwork::kINET, IArchNetwork::kSTREAM); + m_socket = ARCH->newSocket(family, IArchNetwork::kSTREAM); } catch (XArchNetwork& e) { throw XSocketCreate(e.what()); diff --git a/src/lib/net/TCPListenSocket.h b/src/lib/net/TCPListenSocket.h index 54bbbae4..10603562 100644 --- a/src/lib/net/TCPListenSocket.h +++ b/src/lib/net/TCPListenSocket.h @@ -32,7 +32,7 @@ A listen socket using TCP. */ class TCPListenSocket : public IListenSocket { public: - TCPListenSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer); + TCPListenSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer, IArchNetwork::EAddressFamily family); virtual ~TCPListenSocket(); // ISocket overrides diff --git a/src/lib/net/TCPSocket.cpp b/src/lib/net/TCPSocket.cpp index b91e5fb6..dce81eef 100644 --- a/src/lib/net/TCPSocket.cpp +++ b/src/lib/net/TCPSocket.cpp @@ -37,7 +37,7 @@ // TCPSocket // -TCPSocket::TCPSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer) : +TCPSocket::TCPSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer, IArchNetwork::EAddressFamily family) : IDataSocket(events), m_events(events), m_mutex(), @@ -45,7 +45,7 @@ TCPSocket::TCPSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer) m_socketMultiplexer(socketMultiplexer) { try { - m_socket = ARCH->newSocket(IArchNetwork::kINET, IArchNetwork::kSTREAM); + m_socket = ARCH->newSocket(family, IArchNetwork::kSTREAM); } catch (XArchNetwork& e) { throw XSocketCreate(e.what()); diff --git a/src/lib/net/TCPSocket.h b/src/lib/net/TCPSocket.h index d685f45c..1006f88e 100644 --- a/src/lib/net/TCPSocket.h +++ b/src/lib/net/TCPSocket.h @@ -36,7 +36,7 @@ A data socket using TCP. */ class TCPSocket : public IDataSocket { public: - TCPSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer); + TCPSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer, IArchNetwork::EAddressFamily family); TCPSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer, ArchSocket socket); virtual ~TCPSocket(); diff --git a/src/lib/net/TCPSocketFactory.cpp b/src/lib/net/TCPSocketFactory.cpp index 91b11521..6ff4ef8a 100644 --- a/src/lib/net/TCPSocketFactory.cpp +++ b/src/lib/net/TCPSocketFactory.cpp @@ -41,27 +41,27 @@ TCPSocketFactory::~TCPSocketFactory() } IDataSocket* -TCPSocketFactory::create(bool secure) const +TCPSocketFactory::create(IArchNetwork::EAddressFamily family, bool secure) const { if (secure) { - SecureSocket* secureSocket = new SecureSocket(m_events, m_socketMultiplexer); + SecureSocket* secureSocket = new SecureSocket(m_events, m_socketMultiplexer, family); secureSocket->initSsl (false); return secureSocket; } else { - return new TCPSocket(m_events, m_socketMultiplexer); + return new TCPSocket(m_events, m_socketMultiplexer, family); } } IListenSocket* -TCPSocketFactory::createListen(bool secure) const +TCPSocketFactory::createListen(IArchNetwork::EAddressFamily family, bool secure) const { IListenSocket* socket = NULL; if (secure) { - socket = new SecureListenSocket(m_events, m_socketMultiplexer); + socket = new SecureListenSocket(m_events, m_socketMultiplexer, family); } else { - socket = new TCPListenSocket(m_events, m_socketMultiplexer); + socket = new TCPListenSocket(m_events, m_socketMultiplexer, family); } return socket; diff --git a/src/lib/net/TCPSocketFactory.h b/src/lib/net/TCPSocketFactory.h index 3e7afcb2..0195ec4f 100644 --- a/src/lib/net/TCPSocketFactory.h +++ b/src/lib/net/TCPSocketFactory.h @@ -19,6 +19,7 @@ #pragma once #include "net/ISocketFactory.h" +#include "arch/IArchNetwork.h" class IEventQueue; class SocketMultiplexer; @@ -30,10 +31,12 @@ public: virtual ~TCPSocketFactory(); // ISocketFactory overrides - virtual IDataSocket* - create(bool secure) const; - virtual IListenSocket* - createListen(bool secure) const; + virtual IDataSocket* create( + IArchNetwork::EAddressFamily family, + bool secure) const; + virtual IListenSocket* createListen( + IArchNetwork::EAddressFamily family, + bool secure) const; private: IEventQueue* m_events; diff --git a/src/lib/server/ClientListener.cpp b/src/lib/server/ClientListener.cpp index 334e730c..00067bab 100644 --- a/src/lib/server/ClientListener.cpp +++ b/src/lib/server/ClientListener.cpp @@ -45,7 +45,9 @@ ClientListener::ClientListener(const NetworkAddress& address, assert(m_socketFactory != NULL); try { - m_listen = m_socketFactory->createListen(m_useSecureNetwork); + m_listen = m_socketFactory->createListen( + ARCH->getAddrFamily(address.getAddress()), + m_useSecureNetwork); // setup event handler m_events->adoptHandler(m_events->forIListenSocket().connecting(),