From d55eab926418a6a870c2a64c200acd9fc95a1a3d Mon Sep 17 00:00:00 2001 From: Guenter Obiltschnig Date: Tue, 8 Sep 2015 15:00:43 +0200 Subject: [PATCH] clean build with POCO_NET_NO_IPv6, add test for Unix domain sockets --- Net/include/Poco/Net/IPAddress.h | 88 +++++++++++++++------------- Net/include/Poco/Net/IPAddressImpl.h | 6 ++ Net/include/Poco/Net/SocketAddress.h | 4 +- Net/src/IPAddress.cpp | 15 +++-- Net/src/ServerSocket.cpp | 4 ++ Net/testsuite/src/EchoServer.cpp | 10 ++++ Net/testsuite/src/EchoServer.h | 3 + Net/testsuite/src/SocketTest.cpp | 24 ++++++++ Net/testsuite/src/SocketTest.h | 1 + 9 files changed, 108 insertions(+), 47 deletions(-) diff --git a/Net/include/Poco/Net/IPAddress.h b/Net/include/Poco/Net/IPAddress.h index fa6fe73f1..a8ae5e672 100644 --- a/Net/include/Poco/Net/IPAddress.h +++ b/Net/include/Poco/Net/IPAddress.h @@ -374,21 +374,15 @@ private: #endif Ptr pImpl() const; - - void newIPv4(const void* hostAddr); - - void newIPv6(const void* hostAddr); - - void newIPv6(const void* hostAddr, Poco::UInt32 scope); - - void newIPv4(unsigned prefix); - - void newIPv6(unsigned prefix); - void newIPv4(); - + void newIPv4(const void* hostAddr); + void newIPv4(unsigned prefix); +#if defined(POCO_HAVE_IPv6) void newIPv6(); - + void newIPv6(const void* hostAddr); + void newIPv6(const void* hostAddr, Poco::UInt32 scope); + void newIPv6(unsigned prefix); +#endif void destruct(); #ifdef POCO_HAVE_ALIGNMENT @@ -405,7 +399,11 @@ private: AlignerType aligner; } #else // !POCO_ENABLE_CPP11 - AlignedCharArrayUnion + #if defined(POCO_HAVE_IPv6) + AlignedCharArrayUnion + #else + AlignedCharArrayUnion + #endif #endif // POCO_ENABLE_CPP11 _memory; #else // !POCO_HAVE_ALIGNMENT @@ -438,6 +436,16 @@ inline IPAddress::Ptr IPAddress::pImpl() const } +inline void IPAddress::newIPv4() +{ +#ifdef POCO_HAVE_ALIGNMENT + new (storage()) Poco::Net::Impl::IPv4AddressImpl; +#else + _pImpl = new Poco::Net::Impl::IPv4AddressImpl; +#endif +} + + inline void IPAddress::newIPv4(const void* hostAddr) { #ifdef POCO_HAVE_ALIGNMENT @@ -448,6 +456,29 @@ inline void IPAddress::newIPv4(const void* hostAddr) } +inline void IPAddress::newIPv4(unsigned prefix) +{ +#ifdef POCO_HAVE_ALIGNMENT + new (storage()) Poco::Net::Impl::IPv4AddressImpl(prefix); +#else + _pImpl = new Poco::Net::Impl::IPv4AddressImpl(prefix); +#endif +} + + +#if defined(POCO_HAVE_IPv6) + + +inline void IPAddress::newIPv6() +{ +#ifdef POCO_HAVE_ALIGNMENT + new (storage()) Poco::Net::Impl::IPv6AddressImpl; +#else + _pImpl = new Poco::Net::Impl::IPv6AddressImpl; +#endif +} + + inline void IPAddress::newIPv6(const void* hostAddr) { #ifdef POCO_HAVE_ALIGNMENT @@ -468,16 +499,6 @@ inline void IPAddress::newIPv6(const void* hostAddr, Poco::UInt32 scope) } -inline void IPAddress::newIPv4(unsigned prefix) -{ -#ifdef POCO_HAVE_ALIGNMENT - new (storage()) Poco::Net::Impl::IPv4AddressImpl(prefix); -#else - _pImpl = new Poco::Net::Impl::IPv4AddressImpl(prefix); -#endif -} - - inline void IPAddress::newIPv6(unsigned prefix) { #ifdef POCO_HAVE_ALIGNMENT @@ -488,24 +509,7 @@ inline void IPAddress::newIPv6(unsigned prefix) } -inline void IPAddress::newIPv4() -{ -#ifdef POCO_HAVE_ALIGNMENT - new (storage()) Poco::Net::Impl::IPv4AddressImpl; -#else - _pImpl = new Poco::Net::Impl::IPv4AddressImpl; -#endif -} - - -inline void IPAddress::newIPv6() -{ -#ifdef POCO_HAVE_ALIGNMENT - new (storage()) Poco::Net::Impl::IPv6AddressImpl; -#else - _pImpl = new Poco::Net::Impl::IPv6AddressImpl; -#endif -} +#endif // POCO_HAVE_IPv6 #ifdef POCO_HAVE_ALIGNMENT diff --git a/Net/include/Poco/Net/IPAddressImpl.h b/Net/include/Poco/Net/IPAddressImpl.h index d68049cee..4be1e20cb 100644 --- a/Net/include/Poco/Net/IPAddressImpl.h +++ b/Net/include/Poco/Net/IPAddressImpl.h @@ -124,6 +124,9 @@ private: }; +#if defined(POCO_HAVE_IPv6) + + // // IPv6AddressImpl // @@ -174,6 +177,9 @@ private: }; +#endif // POCO_HAVE_IPv6 + + } } } // namespace Poco::Net::Impl diff --git a/Net/include/Poco/Net/SocketAddress.h b/Net/include/Poco/Net/SocketAddress.h index f0dcea2e6..164742d8b 100644 --- a/Net/include/Poco/Net/SocketAddress.h +++ b/Net/include/Poco/Net/SocketAddress.h @@ -139,7 +139,9 @@ public: enum { MAX_ADDRESS_LENGTH = -#if defined(POCO_HAVE_IPv6) +#if defined(POCO_OS_FAMILY_UNIX) + sizeof(struct sockaddr_un) +#elif defined(POCO_HAVE_IPv6) sizeof(struct sockaddr_in6) #else sizeof(struct sockaddr_in) diff --git a/Net/src/IPAddress.cpp b/Net/src/IPAddress.cpp index e3d0039a5..1f7b7945d 100644 --- a/Net/src/IPAddress.cpp +++ b/Net/src/IPAddress.cpp @@ -35,7 +35,9 @@ using Poco::UInt16; using Poco::UInt32; using Poco::Net::Impl::IPAddressImpl; using Poco::Net::Impl::IPv4AddressImpl; +#if defined(POCO_HAVE_IPv6) using Poco::Net::Impl::IPv6AddressImpl; +#endif namespace Poco { @@ -58,8 +60,10 @@ IPAddress::IPAddress(const IPAddress& addr) { if (addr.family() == IPv4) newIPv4(addr.addr()); +#if defined(POCO_HAVE_IPv6) else newIPv6(addr.addr(), addr.scope()); +#endif } @@ -71,8 +75,7 @@ IPAddress::IPAddress(Family family) else if (family == IPv6) newIPv6(); #endif - else - throw Poco::InvalidArgumentException("Invalid or unsupported address family passed to IPAddress()"); + else throw Poco::InvalidArgumentException("Invalid or unsupported address family passed to IPAddress()"); } @@ -227,8 +230,12 @@ IPAddress& IPAddress::operator = (const IPAddress& addr) destruct(); if (addr.family() == IPAddress::IPv4) newIPv4(addr.addr()); - else +#if defined(POCO_HAVE_IPv6) + else if (addr.family() == IPAddress::IPv6) newIPv6(addr.addr(), addr.scope()); +#endif + else + throw Poco::InvalidArgumentException("Invalid or unsupported address family"); } return *this; } @@ -236,7 +243,7 @@ IPAddress& IPAddress::operator = (const IPAddress& addr) IPAddress::Family IPAddress::family() const { - return static_cast(pImpl()->family()); + return pImpl()->family(); } diff --git a/Net/src/ServerSocket.cpp b/Net/src/ServerSocket.cpp index 99e52b95f..d52e5977a 100644 --- a/Net/src/ServerSocket.cpp +++ b/Net/src/ServerSocket.cpp @@ -96,9 +96,13 @@ void ServerSocket::bind6(const SocketAddress& address, bool reuseAddress, bool i void ServerSocket::bind6(Poco::UInt16 port, bool reuseAddress, bool ipV6Only) { +#if defined(POCO_HAVE_IPv6) IPAddress wildcardAddr(IPAddress::IPv6); SocketAddress address(wildcardAddr, port); impl()->bind6(address, reuseAddress, ipV6Only); +#else + throw Poco::NotImplementedException("No IPv6 support available"); +#endif // POCO_HAVE_IPv6 } diff --git a/Net/testsuite/src/EchoServer.cpp b/Net/testsuite/src/EchoServer.cpp index 712acd8ff..c97f97d24 100644 --- a/Net/testsuite/src/EchoServer.cpp +++ b/Net/testsuite/src/EchoServer.cpp @@ -32,6 +32,16 @@ EchoServer::EchoServer(): } +EchoServer::EchoServer(const Poco::Net::SocketAddress& address): + _socket(address), + _thread("EchoServer"), + _stop(false) +{ + _thread.start(*this); + _ready.wait(); +} + + EchoServer::~EchoServer() { _stop = true; diff --git a/Net/testsuite/src/EchoServer.h b/Net/testsuite/src/EchoServer.h index ccab60ff1..5dfe3ec9d 100644 --- a/Net/testsuite/src/EchoServer.h +++ b/Net/testsuite/src/EchoServer.h @@ -29,6 +29,9 @@ public: EchoServer(); /// Creates the EchoServer. + EchoServer(const Poco::Net::SocketAddress& address); + /// Creates the EchoServer using the given address. + ~EchoServer(); /// Destroys the EchoServer. diff --git a/Net/testsuite/src/SocketTest.cpp b/Net/testsuite/src/SocketTest.cpp index 6f6780ea8..4c8603c15 100644 --- a/Net/testsuite/src/SocketTest.cpp +++ b/Net/testsuite/src/SocketTest.cpp @@ -23,6 +23,7 @@ #include "Poco/Buffer.h" #include "Poco/FIFOBuffer.h" #include "Poco/Delegate.h" +#include "Poco/File.h" #include @@ -501,6 +502,28 @@ void SocketTest::testSelect3() } +void SocketTest::testEchoUnixLocal() +{ +#if defined(POCO_OS_FAMILY_UNIX) + Poco::File socketFile("/tmp/SocketTest.sock"); + if (socketFile.exists()) socketFile.remove(); + SocketAddress localAddr(SocketAddress::UNIX_LOCAL, socketFile.path()); + EchoServer echoServer(localAddr); + StreamSocket ss(SocketAddress::UNIX_LOCAL); + ss.connect(localAddr); + int n = ss.sendBytes("hello", 5); + assert (n == 5); + char buffer[256]; + n = ss.receiveBytes(buffer, sizeof(buffer)); + assert (n == 5); + assert (std::string(buffer, n) == "hello"); + ss.close(); + socketFile.remove(); +#endif +} + + + void SocketTest::onReadable(bool& b) { if (b) ++_notToReadable; @@ -549,6 +572,7 @@ CppUnit::Test* SocketTest::suite() CppUnit_addTest(pSuite, SocketTest, testSelect); CppUnit_addTest(pSuite, SocketTest, testSelect2); CppUnit_addTest(pSuite, SocketTest, testSelect3); + CppUnit_addTest(pSuite, SocketTest, testEchoUnixLocal); return pSuite; } diff --git a/Net/testsuite/src/SocketTest.h b/Net/testsuite/src/SocketTest.h index 5a8033ffa..0b29662c0 100644 --- a/Net/testsuite/src/SocketTest.h +++ b/Net/testsuite/src/SocketTest.h @@ -42,6 +42,7 @@ public: void testSelect(); void testSelect2(); void testSelect3(); + void testEchoUnixLocal(); void setUp(); void tearDown();