UNIX domain sockets support: replace IPAddress::Family with new enum AddressFamily::Family enum

This commit is contained in:
Guenter Obiltschnig
2015-09-08 14:15:38 +02:00
parent 843f251487
commit d64ec94653
31 changed files with 161 additions and 81 deletions

View File

@@ -36,7 +36,7 @@ public:
DatagramSocket();
/// Creates an unconnected IPv4 datagram socket.
explicit DatagramSocket(IPAddress::Family family);
explicit DatagramSocket(SocketAddress::Family family);
/// Creates an unconnected datagram socket.
///
/// The socket will be created for the

View File

@@ -39,7 +39,7 @@ public:
/// be an IPv6 socket. Otherwise, it will be
/// an IPv4 socket.
explicit DatagramSocketImpl(IPAddress::Family family);
explicit DatagramSocketImpl(SocketAddress::Family family);
/// Creates an unconnected datagram socket.
///
/// The socket will be created for the

View File

@@ -46,7 +46,7 @@ public:
mutable Poco::BasicEvent<ICMPEventArgs> pingError;
mutable Poco::BasicEvent<ICMPEventArgs> pingEnd;
explicit ICMPClient(IPAddress::Family family);
explicit ICMPClient(SocketAddress::Family family);
/// Creates an ICMP client.
~ICMPClient();
@@ -64,7 +64,7 @@ public:
///
/// Returns the number of valid replies.
static int ping(SocketAddress& address, IPAddress::Family family, int repeat = 1);
static int ping(SocketAddress& address, SocketAddress::Family family, int repeat = 1);
/// Pings the specified address [repeat] times.
/// Notifications are not posted for events.
///
@@ -77,7 +77,7 @@ public:
/// Returns the number of valid replies.
private:
mutable IPAddress::Family _family;
mutable SocketAddress::Family _family;
};

View File

@@ -33,7 +33,7 @@ class Net_API ICMPPacket
/// This class is the ICMP packet abstraction.
{
public:
ICMPPacket(IPAddress::Family family, int dataSize = 48);
ICMPPacket(SocketAddress::Family family, int dataSize = 48);
/// Creates an ICMPPacket of specified family.
~ICMPPacket();

View File

@@ -33,7 +33,7 @@ class Net_API ICMPSocket: public Socket
/// ICMP client socket.
{
public:
ICMPSocket(IPAddress::Family family, int dataSize = 48, int ttl = 128, int timeout = 500000);
ICMPSocket(SocketAddress::Family family, int dataSize = 48, int ttl = 128, int timeout = 500000);
/// Creates an unconnected ICMP socket.
///
/// The socket will be created for the

View File

@@ -34,7 +34,7 @@ class Net_API ICMPSocketImpl: public RawSocketImpl
/// This class implements an ICMP socket.
{
public:
ICMPSocketImpl(IPAddress::Family family, int dataSize, int ttl, int timeout);
ICMPSocketImpl(SocketAddress::Family family, int dataSize, int ttl, int timeout);
/// Creates an unconnected ICMP socket.
///
/// The socket will be created for the given address family.

View File

@@ -57,15 +57,15 @@ class Net_API IPAddress
{
public:
typedef std::vector<IPAddress> List;
enum Family
/// Possible address families for IP addresses.
{
IPv4 = Poco::Net::Impl::IPAddressImpl::IPv4,
#ifdef POCO_HAVE_IPv6
IPv6 = Poco::Net::Impl::IPAddressImpl::IPv6
// The following declarations keep the Family type
// backwards compatible with the previously used
// enum declaration.
typedef AddressFamily::Family Family;
static const Family IPv4 = AddressFamily::IPv4;
#if defined(POCO_HAVE_IPv6)
static const Family IPv6 = AddressFamily::IPv6;
#endif
};
IPAddress();
/// Creates a wildcard (zero) IPv4 IPAddress.

View File

@@ -39,14 +39,7 @@ class IPAddressImpl
#endif
{
public:
enum Family
/// Possible address families for IP addresses.
{
IPv4,
#ifdef POCO_HAVE_IPv6
IPv6
#endif
};
typedef AddressFamily::Family Family;
virtual ~IPAddressImpl();

View File

@@ -43,7 +43,7 @@ public:
MulticastSocket();
/// Creates the MulticastSocket.
explicit MulticastSocket(IPAddress::Family family);
explicit MulticastSocket(SocketAddress::Family family);
/// Creates an unconnected datagram socket.
///
/// The socket will be created for the

View File

@@ -36,7 +36,7 @@ class Net_API NTPClient
public:
mutable Poco::BasicEvent<NTPEventArgs> response;
explicit NTPClient(IPAddress::Family family, int timeout = 3000000);
explicit NTPClient(SocketAddress::Family family, int timeout = 3000000);
/// Creates an NTP client.
~NTPClient();
@@ -55,7 +55,7 @@ public:
/// Returns the number of valid replies.
private:
mutable IPAddress::Family _family;
mutable SocketAddress::Family _family;
int _timeout;
};

View File

@@ -36,7 +36,7 @@ public:
RawSocket();
/// Creates an unconnected IPv4 raw socket.
RawSocket(IPAddress::Family family, int proto = IPPROTO_RAW);
RawSocket(SocketAddress::Family family, int proto = IPPROTO_RAW);
/// Creates an unconnected raw socket.
///
/// The socket will be created for the

View File

@@ -35,7 +35,7 @@ public:
RawSocketImpl();
/// Creates an unconnected IPv4 raw socket with IPPROTO_RAW.
RawSocketImpl(IPAddress::Family family, int proto = IPPROTO_RAW);
RawSocketImpl(SocketAddress::Family family, int proto = IPPROTO_RAW);
/// Creates an unconnected raw socket.
///
/// The socket will be created for the

View File

@@ -43,17 +43,17 @@ class Net_API SocketAddress
/// host address and a port number.
{
public:
enum Family
/// Possible address families for socket addresses.
{
IPv4 = Poco::Net::Impl::SocketAddressImpl::IPv4,
#ifdef POCO_HAVE_IPv6
IPv6 = Poco::Net::Impl::SocketAddressImpl::IPv6,
// The following declarations keep the Family type
// backwards compatible with the previously used
// enum declaration.
typedef AddressFamily::Family Family;
static const Family IPv4 = AddressFamily::IPv4;
#if defined(POCO_HAVE_IPv6)
static const Family IPv6 = AddressFamily::IPv6;
#endif
#ifdef POCO_OS_FAMILY_UNIX
UNIX_LOCAL = Poco::Net::Impl::SocketAddressImpl::UNIX_LOCAL
#if defined(POCO_OS_FAMILY_UNIX)
static const Family UNIX_LOCAL = AddressFamily::UNIX_LOCAL;
#endif
};
SocketAddress();
/// Creates a wildcard (all zero) IPv4 SocketAddress.
@@ -316,16 +316,16 @@ inline bool SocketAddress::operator == (const SocketAddress& socketAddress) cons
{
#if defined(POCO_OS_FAMILY_UNIX)
if (family() == UNIX_LOCAL)
return host() == socketAddress.host() && port() == socketAddress.port();
return toString() == socketAddress.toString();
else
#endif
return toString() == socketAddress.toString();
return host() == socketAddress.host() && port() == socketAddress.port();
}
inline bool SocketAddress::operator != (const SocketAddress& socketAddress) const
{
return !operator == (socketAddress);
return !(operator == (socketAddress));
}

View File

@@ -27,6 +27,7 @@
#include "Poco/RefCountedObject.h"
#endif
namespace Poco {
namespace Net {
namespace Impl {
@@ -38,17 +39,7 @@ class Net_API SocketAddressImpl
#endif
{
public:
enum Family
/// Possible address families for socket addresses.
{
IPv4,
#ifdef POCO_HAVE_IPv6
IPv6,
#endif
#ifdef POCO_OS_FAMILY_UNIX
UNIX_LOCAL
#endif
};
typedef AddressFamily::Family Family;
virtual ~SocketAddressImpl();
@@ -124,7 +115,7 @@ inline int IPv4SocketAddressImpl::af() const
inline SocketAddressImpl::Family IPv4SocketAddressImpl::family() const
{
return SocketAddressImpl::IPv4;
return AddressFamily::IPv4;
}
@@ -186,7 +177,7 @@ inline int IPv6SocketAddressImpl::af() const
inline SocketAddressImpl::Family IPv6SocketAddressImpl::family() const
{
return SocketAddressImpl::IPv6;
return AddressFamily::IPv6;
}
@@ -254,7 +245,7 @@ inline int LocalSocketAddressImpl::af() const
inline SocketAddressImpl::Family LocalSocketAddressImpl::family() const
{
return SocketAddressImpl::UNIX_LOCAL;
return AddressFamily::UNIX_LOCAL;
}

View File

@@ -353,4 +353,32 @@
#endif
namespace Poco {
namespace Net {
struct AddressFamily
/// AddressFamily::Family replaces the previously used IPAddress::Family
/// enumeration and is now used for IPAddress::Family and SocketAddress::Family.
{
enum Family
/// Possible address families for socket addresses.
{
IPv4,
/// IPv4 address family.
#if defined(POCO_HAVE_IPv6)
IPv6,
/// IPv6 address family.
#endif
#if defined(POCO_OS_FAMILY_UNIX)
UNIX_LOCAL
/// UNIX domain socket address family. Available on UNIX/POSIX platforms only.
#endif
};
};
} } // namespace Poco::Net
#endif // Net_SocketDefs_INCLUDED

View File

@@ -47,7 +47,7 @@ public:
/// Creates a stream socket and connects it to
/// the socket specified by address.
explicit StreamSocket(IPAddress::Family family);
explicit StreamSocket(SocketAddress::Family family);
/// Creates an unconnected stream socket
/// for the given address family.
///

View File

@@ -35,7 +35,7 @@ public:
StreamSocketImpl();
/// Creates a StreamSocketImpl.
explicit StreamSocketImpl(IPAddress::Family addressFamily);
explicit StreamSocketImpl(SocketAddress::Family addressFamily);
/// Creates a SocketImpl, with the underlying
/// socket initialized for the given address family.

View File

@@ -31,7 +31,7 @@ DatagramSocket::DatagramSocket(): Socket(new DatagramSocketImpl)
}
DatagramSocket::DatagramSocket(IPAddress::Family family): Socket(new DatagramSocketImpl(family))
DatagramSocket::DatagramSocket(SocketAddress::Family family): Socket(new DatagramSocketImpl(family))
{
}

View File

@@ -31,13 +31,17 @@ DatagramSocketImpl::DatagramSocketImpl()
}
DatagramSocketImpl::DatagramSocketImpl(IPAddress::Family family)
DatagramSocketImpl::DatagramSocketImpl(SocketAddress::Family family)
{
if (family == IPAddress::IPv4)
if (family == SocketAddress::IPv4)
init(AF_INET);
#if defined(POCO_HAVE_IPv6)
else if (family == IPAddress::IPv6)
else if (family == SocketAddress::IPv6)
init(AF_INET6);
#endif
#if defined(POCO_OS_FAMILY_UNIX)
else if (family == SocketAddress::UNIX_LOCAL)
init(AF_UNIX);
#endif
else throw InvalidArgumentException("Invalid or unsupported address family passed to DatagramSocketImpl");
}

View File

@@ -36,7 +36,7 @@ namespace Poco {
namespace Net {
ICMPClient::ICMPClient(IPAddress::Family family):
ICMPClient::ICMPClient(SocketAddress::Family family):
_family(family)
{
}

View File

@@ -42,6 +42,12 @@ namespace Poco {
namespace Net {
const IPAddress::Family IPAddress::IPv4;
#if defined(POCO_HAVE_IPv6)
const IPAddress::Family IPAddress::IPv6;
#endif
IPAddress::IPAddress()
{
newIPv4();

View File

@@ -145,7 +145,7 @@ const void* IPv4AddressImpl::addr() const
IPAddressImpl::Family IPv4AddressImpl::family() const
{
return IPAddressImpl::IPv4;
return AddressFamily::IPv4;
}
@@ -499,7 +499,7 @@ const void* IPv6AddressImpl::addr() const
IPAddressImpl::Family IPv6AddressImpl::family() const
{
return IPAddressImpl::IPv6;
return AddressFamily::IPv6;
}
@@ -534,6 +534,8 @@ unsigned IPv6AddressImpl::prefixLength() const
throw NotImplementedException("prefixLength() not implemented");
#endif
}
Poco::UInt32 IPv6AddressImpl::scope() const
{
return _scope;

View File

@@ -53,8 +53,12 @@ MulticastSocket::MulticastSocket()
}
MulticastSocket::MulticastSocket(IPAddress::Family family): DatagramSocket(family)
MulticastSocket::MulticastSocket(SocketAddress::Family family): DatagramSocket(family)
{
#if defined(POCO_OS_FAMILY_UNIX)
if (family == SocketAddress::UNIX_LOCAL)
throw Poco::InvalidArgumentException("Cannot create a MulticastSocket with UNIX_LOCAL socket");
#endif
}
@@ -92,8 +96,7 @@ void MulticastSocket::setInterface(const NetworkInterface& interfc)
impl()->setOption(IPPROTO_IPV6, IPV6_MULTICAST_IF, interfc.index());
}
#endif
else
throw UnsupportedFamilyException("Unknown or unsupported socket family.");
else throw UnsupportedFamilyException("Unknown or unsupported socket family.");
}

View File

@@ -32,7 +32,7 @@ RawSocket::RawSocket():
}
RawSocket::RawSocket(IPAddress::Family family, int proto):
RawSocket::RawSocket(SocketAddress::Family family, int proto):
Socket(new RawSocketImpl(family, proto))
{
}

View File

@@ -31,12 +31,12 @@ RawSocketImpl::RawSocketImpl()
}
RawSocketImpl::RawSocketImpl(IPAddress::Family family, int proto)
RawSocketImpl::RawSocketImpl(SocketAddress::Family family, int proto)
{
if (family == IPAddress::IPv4)
if (family == SocketAddress::IPv4)
init2(AF_INET, proto);
#if defined(POCO_HAVE_IPv6)
else if (family == IPAddress::IPv6)
else if (family == SocketAddress::IPv6)
init2(AF_INET6, proto);
#endif
else throw InvalidArgumentException("Invalid or unsupported address family passed to RawSocketImpl");

View File

@@ -58,6 +58,15 @@ struct AFLT
//
const SocketAddress::Family SocketAddress::IPv4;
#if defined(POCO_HAVE_IPv6)
const SocketAddress::Family SocketAddress::IPv6;
#endif
#if defined(POCO_OS_FAMILY_UNIX)
const SocketAddress::Family SocketAddress::UNIX_LOCAL;
#endif
SocketAddress::SocketAddress()
{
newIPv4();
@@ -117,17 +126,17 @@ SocketAddress::SocketAddress(const SocketAddress& socketAddress)
SocketAddress::SocketAddress(const struct sockaddr* sockAddr, poco_socklen_t length)
{
if (length == sizeof(struct sockaddr_in))
if (length == sizeof(struct sockaddr_in) && sockAddr->sa_family == AF_INET)
newIPv4(reinterpret_cast<const struct sockaddr_in*>(sockAddr));
#if defined(POCO_HAVE_IPv6)
else if (length == sizeof(struct sockaddr_in6))
else if (length == sizeof(struct sockaddr_in6) && sockAddr->sa_family == AF_INET6)
newIPv6(reinterpret_cast<const struct sockaddr_in6*>(sockAddr));
#endif
#if defined(POCO_OS_FAMILY_UNIX)
else if (length > 0 && length <= sizeof(struct sockaddr_un) && sockAddr->sa_family == AF_UNIX)
newLocal(reinterpret_cast<const sockaddr_un*>(sockAddr));
#endif
else throw Poco::InvalidArgumentException("Invalid address length passed to SocketAddress()");
else throw Poco::InvalidArgumentException("Invalid address length or family passed to SocketAddress()");
}
@@ -263,6 +272,8 @@ void SocketAddress::init(Family family, const std::string& address)
void SocketAddress::init(const std::string& hostAndPort)
{
poco_assert (!hostAndPort.empty());
std::string host;
std::string port;
std::string::const_iterator it = hostAndPort.begin();
@@ -332,7 +343,7 @@ Poco::BinaryReader& operator >> (Poco::BinaryReader& reader, Poco::Net::SocketAd
}
inline std::ostream& operator << (std::ostream& ostr, const Poco::Net::SocketAddress& address)
std::ostream& operator << (std::ostream& ostr, const Poco::Net::SocketAddress& address)
{
ostr << address.toString();
return ostr;

View File

@@ -63,6 +63,7 @@ IPv4SocketAddressImpl::IPv4SocketAddressImpl(const void* addr, UInt16 port)
{
std::memset(&_addr, 0, sizeof(_addr));
_addr.sin_family = AF_INET;
poco_set_sin_len(&_addr);
std::memcpy(&_addr.sin_addr, addr, sizeof(_addr.sin_addr));
_addr.sin_port = port;
}
@@ -73,7 +74,7 @@ std::string IPv4SocketAddressImpl::toString() const
std::string result;
result.append(host().toString());
result.append(":");
NumberFormatter::append(result, port());
NumberFormatter::append(result, ntohs(port()));
return result;
}
@@ -120,7 +121,7 @@ std::string IPv6SocketAddressImpl::toString() const
result.append(host().toString());
result.append("]");
result.append(":");
NumberFormatter::append(result, port());
NumberFormatter::append(result, ntohs(port()));
return result;
}

View File

@@ -41,7 +41,7 @@ StreamSocket::StreamSocket(const SocketAddress& address): Socket(new StreamSocke
}
StreamSocket::StreamSocket(IPAddress::Family family): Socket(new StreamSocketImpl(family))
StreamSocket::StreamSocket(SocketAddress::Family family): Socket(new StreamSocketImpl(family))
{
}

View File

@@ -28,13 +28,17 @@ StreamSocketImpl::StreamSocketImpl()
}
StreamSocketImpl::StreamSocketImpl(IPAddress::Family family)
StreamSocketImpl::StreamSocketImpl(SocketAddress::Family family)
{
if (family == IPAddress::IPv4)
if (family == SocketAddress::IPv4)
init(AF_INET);
#if defined(POCO_HAVE_IPv6)
else if (family == IPAddress::IPv6)
else if (family == SocketAddress::IPv6)
init(AF_INET6);
#endif
#if defined(POCO_OS_FAMILY_UNIX)
else if (family == SocketAddress::UNIX_LOCAL)
init(AF_UNIX);
#endif
else throw Poco::InvalidArgumentException("Invalid or unsupported address family passed to StreamSocketImpl");
}

View File

@@ -43,8 +43,11 @@ void SocketAddressTest::testSocketAddress()
assert (wild.port() == 0);
SocketAddress sa1("192.168.1.100", 100);
assert (sa1.af() == AF_INET);
assert (sa1.family() == SocketAddress::IPv4);
assert (sa1.host().toString() == "192.168.1.100");
assert (sa1.port() == 100);
assert (sa1.toString() == "192.168.1.100:100");
SocketAddress sa2("192.168.1.100", "100");
assert (sa2.host().toString() == "192.168.1.100");
@@ -135,6 +138,38 @@ void SocketAddressTest::testSocketRelationals()
void SocketAddressTest::testSocketAddress6()
{
#ifdef POCO_HAVE_IPv6
SocketAddress sa1("FE80::E6CE:8FFF:FE4A:EDD0", 100);
assert (sa1.af() == AF_INET6);
assert (sa1.family() == SocketAddress::IPv6);
assert (sa1.host().toString() == "fe80::e6ce:8fff:fe4a:edd0");
assert (sa1.port() == 100);
assert (sa1.toString() == "[fe80::e6ce:8fff:fe4a:edd0]:100");
SocketAddress sa2("[FE80::E6CE:8FFF:FE4A:EDD0]:100");
assert (sa2.af() == AF_INET6);
assert (sa2.family() == SocketAddress::IPv6);
assert (sa2.host().toString() == "fe80::e6ce:8fff:fe4a:edd0");
assert (sa2.port() == 100);
assert (sa2.toString() == "[fe80::e6ce:8fff:fe4a:edd0]:100");
#endif
}
void SocketAddressTest::testSocketAddressUnixLocal()
{
#ifdef POCO_OS_FAMILY_UNIX
SocketAddress sa1(SocketAddress::UNIX_LOCAL, "/tmp/sock1");
assert (sa1.af() == AF_UNIX);
assert (sa1.family() == SocketAddress::UNIX_LOCAL);
assert (sa1.toString() == "/tmp/sock1");
SocketAddress sa2(SocketAddress::UNIX_LOCAL, "/tmp/sock2");
assert (sa1 != sa2);
assert (sa1 < sa2);
SocketAddress sa3(SocketAddress::UNIX_LOCAL, "/tmp/sock1");
assert (sa1 == sa3);
assert (!(sa1 < sa3));
#endif
}
@@ -156,6 +191,7 @@ CppUnit::Test* SocketAddressTest::suite()
CppUnit_addTest(pSuite, SocketAddressTest, testSocketAddress);
CppUnit_addTest(pSuite, SocketAddressTest, testSocketRelationals);
CppUnit_addTest(pSuite, SocketAddressTest, testSocketAddress6);
CppUnit_addTest(pSuite, SocketAddressTest, testSocketAddressUnixLocal);
return pSuite;
}

View File

@@ -29,6 +29,7 @@ public:
void testSocketAddress();
void testSocketRelationals();
void testSocketAddress6();
void testSocketAddressUnixLocal();
void setUp();
void tearDown();