mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-26 18:42:41 +01:00
UNIX domain sockets support: replace IPAddress::Family with new enum AddressFamily::Family enum
This commit is contained in:
@@ -36,7 +36,7 @@ public:
|
|||||||
DatagramSocket();
|
DatagramSocket();
|
||||||
/// Creates an unconnected IPv4 datagram socket.
|
/// Creates an unconnected IPv4 datagram socket.
|
||||||
|
|
||||||
explicit DatagramSocket(IPAddress::Family family);
|
explicit DatagramSocket(SocketAddress::Family family);
|
||||||
/// Creates an unconnected datagram socket.
|
/// Creates an unconnected datagram socket.
|
||||||
///
|
///
|
||||||
/// The socket will be created for the
|
/// The socket will be created for the
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ public:
|
|||||||
/// be an IPv6 socket. Otherwise, it will be
|
/// be an IPv6 socket. Otherwise, it will be
|
||||||
/// an IPv4 socket.
|
/// an IPv4 socket.
|
||||||
|
|
||||||
explicit DatagramSocketImpl(IPAddress::Family family);
|
explicit DatagramSocketImpl(SocketAddress::Family family);
|
||||||
/// Creates an unconnected datagram socket.
|
/// Creates an unconnected datagram socket.
|
||||||
///
|
///
|
||||||
/// The socket will be created for the
|
/// The socket will be created for the
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ public:
|
|||||||
mutable Poco::BasicEvent<ICMPEventArgs> pingError;
|
mutable Poco::BasicEvent<ICMPEventArgs> pingError;
|
||||||
mutable Poco::BasicEvent<ICMPEventArgs> pingEnd;
|
mutable Poco::BasicEvent<ICMPEventArgs> pingEnd;
|
||||||
|
|
||||||
explicit ICMPClient(IPAddress::Family family);
|
explicit ICMPClient(SocketAddress::Family family);
|
||||||
/// Creates an ICMP client.
|
/// Creates an ICMP client.
|
||||||
|
|
||||||
~ICMPClient();
|
~ICMPClient();
|
||||||
@@ -64,7 +64,7 @@ public:
|
|||||||
///
|
///
|
||||||
/// Returns the number of valid replies.
|
/// 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.
|
/// Pings the specified address [repeat] times.
|
||||||
/// Notifications are not posted for events.
|
/// Notifications are not posted for events.
|
||||||
///
|
///
|
||||||
@@ -77,7 +77,7 @@ public:
|
|||||||
/// Returns the number of valid replies.
|
/// Returns the number of valid replies.
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable IPAddress::Family _family;
|
mutable SocketAddress::Family _family;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ class Net_API ICMPPacket
|
|||||||
/// This class is the ICMP packet abstraction.
|
/// This class is the ICMP packet abstraction.
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ICMPPacket(IPAddress::Family family, int dataSize = 48);
|
ICMPPacket(SocketAddress::Family family, int dataSize = 48);
|
||||||
/// Creates an ICMPPacket of specified family.
|
/// Creates an ICMPPacket of specified family.
|
||||||
|
|
||||||
~ICMPPacket();
|
~ICMPPacket();
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ class Net_API ICMPSocket: public Socket
|
|||||||
/// ICMP client socket.
|
/// ICMP client socket.
|
||||||
{
|
{
|
||||||
public:
|
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.
|
/// Creates an unconnected ICMP socket.
|
||||||
///
|
///
|
||||||
/// The socket will be created for the
|
/// The socket will be created for the
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ class Net_API ICMPSocketImpl: public RawSocketImpl
|
|||||||
/// This class implements an ICMP socket.
|
/// This class implements an ICMP socket.
|
||||||
{
|
{
|
||||||
public:
|
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.
|
/// Creates an unconnected ICMP socket.
|
||||||
///
|
///
|
||||||
/// The socket will be created for the given address family.
|
/// The socket will be created for the given address family.
|
||||||
|
|||||||
@@ -58,14 +58,14 @@ class Net_API IPAddress
|
|||||||
public:
|
public:
|
||||||
typedef std::vector<IPAddress> List;
|
typedef std::vector<IPAddress> List;
|
||||||
|
|
||||||
enum Family
|
// The following declarations keep the Family type
|
||||||
/// Possible address families for IP addresses.
|
// backwards compatible with the previously used
|
||||||
{
|
// enum declaration.
|
||||||
IPv4 = Poco::Net::Impl::IPAddressImpl::IPv4,
|
typedef AddressFamily::Family Family;
|
||||||
#ifdef POCO_HAVE_IPv6
|
static const Family IPv4 = AddressFamily::IPv4;
|
||||||
IPv6 = Poco::Net::Impl::IPAddressImpl::IPv6
|
#if defined(POCO_HAVE_IPv6)
|
||||||
|
static const Family IPv6 = AddressFamily::IPv6;
|
||||||
#endif
|
#endif
|
||||||
};
|
|
||||||
|
|
||||||
IPAddress();
|
IPAddress();
|
||||||
/// Creates a wildcard (zero) IPv4 IPAddress.
|
/// Creates a wildcard (zero) IPv4 IPAddress.
|
||||||
|
|||||||
@@ -39,14 +39,7 @@ class IPAddressImpl
|
|||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
enum Family
|
typedef AddressFamily::Family Family;
|
||||||
/// Possible address families for IP addresses.
|
|
||||||
{
|
|
||||||
IPv4,
|
|
||||||
#ifdef POCO_HAVE_IPv6
|
|
||||||
IPv6
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
virtual ~IPAddressImpl();
|
virtual ~IPAddressImpl();
|
||||||
|
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ public:
|
|||||||
MulticastSocket();
|
MulticastSocket();
|
||||||
/// Creates the MulticastSocket.
|
/// Creates the MulticastSocket.
|
||||||
|
|
||||||
explicit MulticastSocket(IPAddress::Family family);
|
explicit MulticastSocket(SocketAddress::Family family);
|
||||||
/// Creates an unconnected datagram socket.
|
/// Creates an unconnected datagram socket.
|
||||||
///
|
///
|
||||||
/// The socket will be created for the
|
/// The socket will be created for the
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ class Net_API NTPClient
|
|||||||
public:
|
public:
|
||||||
mutable Poco::BasicEvent<NTPEventArgs> response;
|
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.
|
/// Creates an NTP client.
|
||||||
|
|
||||||
~NTPClient();
|
~NTPClient();
|
||||||
@@ -55,7 +55,7 @@ public:
|
|||||||
/// Returns the number of valid replies.
|
/// Returns the number of valid replies.
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable IPAddress::Family _family;
|
mutable SocketAddress::Family _family;
|
||||||
int _timeout;
|
int _timeout;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ public:
|
|||||||
RawSocket();
|
RawSocket();
|
||||||
/// Creates an unconnected IPv4 raw socket.
|
/// 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.
|
/// Creates an unconnected raw socket.
|
||||||
///
|
///
|
||||||
/// The socket will be created for the
|
/// The socket will be created for the
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ public:
|
|||||||
RawSocketImpl();
|
RawSocketImpl();
|
||||||
/// Creates an unconnected IPv4 raw socket with IPPROTO_RAW.
|
/// 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.
|
/// Creates an unconnected raw socket.
|
||||||
///
|
///
|
||||||
/// The socket will be created for the
|
/// The socket will be created for the
|
||||||
|
|||||||
@@ -43,17 +43,17 @@ class Net_API SocketAddress
|
|||||||
/// host address and a port number.
|
/// host address and a port number.
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
enum Family
|
// The following declarations keep the Family type
|
||||||
/// Possible address families for socket addresses.
|
// backwards compatible with the previously used
|
||||||
{
|
// enum declaration.
|
||||||
IPv4 = Poco::Net::Impl::SocketAddressImpl::IPv4,
|
typedef AddressFamily::Family Family;
|
||||||
#ifdef POCO_HAVE_IPv6
|
static const Family IPv4 = AddressFamily::IPv4;
|
||||||
IPv6 = Poco::Net::Impl::SocketAddressImpl::IPv6,
|
#if defined(POCO_HAVE_IPv6)
|
||||||
|
static const Family IPv6 = AddressFamily::IPv6;
|
||||||
#endif
|
#endif
|
||||||
#ifdef POCO_OS_FAMILY_UNIX
|
#if defined(POCO_OS_FAMILY_UNIX)
|
||||||
UNIX_LOCAL = Poco::Net::Impl::SocketAddressImpl::UNIX_LOCAL
|
static const Family UNIX_LOCAL = AddressFamily::UNIX_LOCAL;
|
||||||
#endif
|
#endif
|
||||||
};
|
|
||||||
|
|
||||||
SocketAddress();
|
SocketAddress();
|
||||||
/// Creates a wildcard (all zero) IPv4 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 defined(POCO_OS_FAMILY_UNIX)
|
||||||
if (family() == UNIX_LOCAL)
|
if (family() == UNIX_LOCAL)
|
||||||
return host() == socketAddress.host() && port() == socketAddress.port();
|
return toString() == socketAddress.toString();
|
||||||
else
|
else
|
||||||
#endif
|
#endif
|
||||||
return toString() == socketAddress.toString();
|
return host() == socketAddress.host() && port() == socketAddress.port();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline bool SocketAddress::operator != (const SocketAddress& socketAddress) const
|
inline bool SocketAddress::operator != (const SocketAddress& socketAddress) const
|
||||||
{
|
{
|
||||||
return !operator == (socketAddress);
|
return !(operator == (socketAddress));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,7 @@
|
|||||||
#include "Poco/RefCountedObject.h"
|
#include "Poco/RefCountedObject.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
namespace Poco {
|
namespace Poco {
|
||||||
namespace Net {
|
namespace Net {
|
||||||
namespace Impl {
|
namespace Impl {
|
||||||
@@ -38,17 +39,7 @@ class Net_API SocketAddressImpl
|
|||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
enum Family
|
typedef AddressFamily::Family Family;
|
||||||
/// Possible address families for socket addresses.
|
|
||||||
{
|
|
||||||
IPv4,
|
|
||||||
#ifdef POCO_HAVE_IPv6
|
|
||||||
IPv6,
|
|
||||||
#endif
|
|
||||||
#ifdef POCO_OS_FAMILY_UNIX
|
|
||||||
UNIX_LOCAL
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
virtual ~SocketAddressImpl();
|
virtual ~SocketAddressImpl();
|
||||||
|
|
||||||
@@ -124,7 +115,7 @@ inline int IPv4SocketAddressImpl::af() const
|
|||||||
|
|
||||||
inline SocketAddressImpl::Family IPv4SocketAddressImpl::family() 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
|
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
|
inline SocketAddressImpl::Family LocalSocketAddressImpl::family() const
|
||||||
{
|
{
|
||||||
return SocketAddressImpl::UNIX_LOCAL;
|
return AddressFamily::UNIX_LOCAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -353,4 +353,32 @@
|
|||||||
#endif
|
#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
|
#endif // Net_SocketDefs_INCLUDED
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ public:
|
|||||||
/// Creates a stream socket and connects it to
|
/// Creates a stream socket and connects it to
|
||||||
/// the socket specified by address.
|
/// the socket specified by address.
|
||||||
|
|
||||||
explicit StreamSocket(IPAddress::Family family);
|
explicit StreamSocket(SocketAddress::Family family);
|
||||||
/// Creates an unconnected stream socket
|
/// Creates an unconnected stream socket
|
||||||
/// for the given address family.
|
/// for the given address family.
|
||||||
///
|
///
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ public:
|
|||||||
StreamSocketImpl();
|
StreamSocketImpl();
|
||||||
/// Creates a StreamSocketImpl.
|
/// Creates a StreamSocketImpl.
|
||||||
|
|
||||||
explicit StreamSocketImpl(IPAddress::Family addressFamily);
|
explicit StreamSocketImpl(SocketAddress::Family addressFamily);
|
||||||
/// Creates a SocketImpl, with the underlying
|
/// Creates a SocketImpl, with the underlying
|
||||||
/// socket initialized for the given address family.
|
/// socket initialized for the given address family.
|
||||||
|
|
||||||
|
|||||||
@@ -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))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
init(AF_INET);
|
||||||
#if defined(POCO_HAVE_IPv6)
|
#if defined(POCO_HAVE_IPv6)
|
||||||
else if (family == IPAddress::IPv6)
|
else if (family == SocketAddress::IPv6)
|
||||||
init(AF_INET6);
|
init(AF_INET6);
|
||||||
|
#endif
|
||||||
|
#if defined(POCO_OS_FAMILY_UNIX)
|
||||||
|
else if (family == SocketAddress::UNIX_LOCAL)
|
||||||
|
init(AF_UNIX);
|
||||||
#endif
|
#endif
|
||||||
else throw InvalidArgumentException("Invalid or unsupported address family passed to DatagramSocketImpl");
|
else throw InvalidArgumentException("Invalid or unsupported address family passed to DatagramSocketImpl");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ namespace Poco {
|
|||||||
namespace Net {
|
namespace Net {
|
||||||
|
|
||||||
|
|
||||||
ICMPClient::ICMPClient(IPAddress::Family family):
|
ICMPClient::ICMPClient(SocketAddress::Family family):
|
||||||
_family(family)
|
_family(family)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,6 +42,12 @@ namespace Poco {
|
|||||||
namespace Net {
|
namespace Net {
|
||||||
|
|
||||||
|
|
||||||
|
const IPAddress::Family IPAddress::IPv4;
|
||||||
|
#if defined(POCO_HAVE_IPv6)
|
||||||
|
const IPAddress::Family IPAddress::IPv6;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
IPAddress::IPAddress()
|
IPAddress::IPAddress()
|
||||||
{
|
{
|
||||||
newIPv4();
|
newIPv4();
|
||||||
|
|||||||
@@ -145,7 +145,7 @@ const void* IPv4AddressImpl::addr() const
|
|||||||
|
|
||||||
IPAddressImpl::Family IPv4AddressImpl::family() 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
|
IPAddressImpl::Family IPv6AddressImpl::family() const
|
||||||
{
|
{
|
||||||
return IPAddressImpl::IPv6;
|
return AddressFamily::IPv6;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -534,6 +534,8 @@ unsigned IPv6AddressImpl::prefixLength() const
|
|||||||
throw NotImplementedException("prefixLength() not implemented");
|
throw NotImplementedException("prefixLength() not implemented");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Poco::UInt32 IPv6AddressImpl::scope() const
|
Poco::UInt32 IPv6AddressImpl::scope() const
|
||||||
{
|
{
|
||||||
return _scope;
|
return _scope;
|
||||||
|
|||||||
@@ -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());
|
impl()->setOption(IPPROTO_IPV6, IPV6_MULTICAST_IF, interfc.index());
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
else
|
else throw UnsupportedFamilyException("Unknown or unsupported socket family.");
|
||||||
throw UnsupportedFamilyException("Unknown or unsupported socket family.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -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))
|
Socket(new RawSocketImpl(family, proto))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
init2(AF_INET, proto);
|
||||||
#if defined(POCO_HAVE_IPv6)
|
#if defined(POCO_HAVE_IPv6)
|
||||||
else if (family == IPAddress::IPv6)
|
else if (family == SocketAddress::IPv6)
|
||||||
init2(AF_INET6, proto);
|
init2(AF_INET6, proto);
|
||||||
#endif
|
#endif
|
||||||
else throw InvalidArgumentException("Invalid or unsupported address family passed to RawSocketImpl");
|
else throw InvalidArgumentException("Invalid or unsupported address family passed to RawSocketImpl");
|
||||||
|
|||||||
@@ -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()
|
SocketAddress::SocketAddress()
|
||||||
{
|
{
|
||||||
newIPv4();
|
newIPv4();
|
||||||
@@ -117,17 +126,17 @@ SocketAddress::SocketAddress(const SocketAddress& socketAddress)
|
|||||||
|
|
||||||
SocketAddress::SocketAddress(const struct sockaddr* sockAddr, poco_socklen_t length)
|
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));
|
newIPv4(reinterpret_cast<const struct sockaddr_in*>(sockAddr));
|
||||||
#if defined(POCO_HAVE_IPv6)
|
#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));
|
newIPv6(reinterpret_cast<const struct sockaddr_in6*>(sockAddr));
|
||||||
#endif
|
#endif
|
||||||
#if defined(POCO_OS_FAMILY_UNIX)
|
#if defined(POCO_OS_FAMILY_UNIX)
|
||||||
else if (length > 0 && length <= sizeof(struct sockaddr_un) && sockAddr->sa_family == AF_UNIX)
|
else if (length > 0 && length <= sizeof(struct sockaddr_un) && sockAddr->sa_family == AF_UNIX)
|
||||||
newLocal(reinterpret_cast<const sockaddr_un*>(sockAddr));
|
newLocal(reinterpret_cast<const sockaddr_un*>(sockAddr));
|
||||||
#endif
|
#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)
|
void SocketAddress::init(const std::string& hostAndPort)
|
||||||
{
|
{
|
||||||
|
poco_assert (!hostAndPort.empty());
|
||||||
|
|
||||||
std::string host;
|
std::string host;
|
||||||
std::string port;
|
std::string port;
|
||||||
std::string::const_iterator it = hostAndPort.begin();
|
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();
|
ostr << address.toString();
|
||||||
return ostr;
|
return ostr;
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ IPv4SocketAddressImpl::IPv4SocketAddressImpl(const void* addr, UInt16 port)
|
|||||||
{
|
{
|
||||||
std::memset(&_addr, 0, sizeof(_addr));
|
std::memset(&_addr, 0, sizeof(_addr));
|
||||||
_addr.sin_family = AF_INET;
|
_addr.sin_family = AF_INET;
|
||||||
|
poco_set_sin_len(&_addr);
|
||||||
std::memcpy(&_addr.sin_addr, addr, sizeof(_addr.sin_addr));
|
std::memcpy(&_addr.sin_addr, addr, sizeof(_addr.sin_addr));
|
||||||
_addr.sin_port = port;
|
_addr.sin_port = port;
|
||||||
}
|
}
|
||||||
@@ -73,7 +74,7 @@ std::string IPv4SocketAddressImpl::toString() const
|
|||||||
std::string result;
|
std::string result;
|
||||||
result.append(host().toString());
|
result.append(host().toString());
|
||||||
result.append(":");
|
result.append(":");
|
||||||
NumberFormatter::append(result, port());
|
NumberFormatter::append(result, ntohs(port()));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,7 +121,7 @@ std::string IPv6SocketAddressImpl::toString() const
|
|||||||
result.append(host().toString());
|
result.append(host().toString());
|
||||||
result.append("]");
|
result.append("]");
|
||||||
result.append(":");
|
result.append(":");
|
||||||
NumberFormatter::append(result, port());
|
NumberFormatter::append(result, ntohs(port()));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
init(AF_INET);
|
||||||
#if defined(POCO_HAVE_IPv6)
|
#if defined(POCO_HAVE_IPv6)
|
||||||
else if (family == IPAddress::IPv6)
|
else if (family == SocketAddress::IPv6)
|
||||||
init(AF_INET6);
|
init(AF_INET6);
|
||||||
|
#endif
|
||||||
|
#if defined(POCO_OS_FAMILY_UNIX)
|
||||||
|
else if (family == SocketAddress::UNIX_LOCAL)
|
||||||
|
init(AF_UNIX);
|
||||||
#endif
|
#endif
|
||||||
else throw Poco::InvalidArgumentException("Invalid or unsupported address family passed to StreamSocketImpl");
|
else throw Poco::InvalidArgumentException("Invalid or unsupported address family passed to StreamSocketImpl");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,8 +43,11 @@ void SocketAddressTest::testSocketAddress()
|
|||||||
assert (wild.port() == 0);
|
assert (wild.port() == 0);
|
||||||
|
|
||||||
SocketAddress sa1("192.168.1.100", 100);
|
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.host().toString() == "192.168.1.100");
|
||||||
assert (sa1.port() == 100);
|
assert (sa1.port() == 100);
|
||||||
|
assert (sa1.toString() == "192.168.1.100:100");
|
||||||
|
|
||||||
SocketAddress sa2("192.168.1.100", "100");
|
SocketAddress sa2("192.168.1.100", "100");
|
||||||
assert (sa2.host().toString() == "192.168.1.100");
|
assert (sa2.host().toString() == "192.168.1.100");
|
||||||
@@ -135,6 +138,38 @@ void SocketAddressTest::testSocketRelationals()
|
|||||||
void SocketAddressTest::testSocketAddress6()
|
void SocketAddressTest::testSocketAddress6()
|
||||||
{
|
{
|
||||||
#ifdef POCO_HAVE_IPv6
|
#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
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,6 +191,7 @@ CppUnit::Test* SocketAddressTest::suite()
|
|||||||
CppUnit_addTest(pSuite, SocketAddressTest, testSocketAddress);
|
CppUnit_addTest(pSuite, SocketAddressTest, testSocketAddress);
|
||||||
CppUnit_addTest(pSuite, SocketAddressTest, testSocketRelationals);
|
CppUnit_addTest(pSuite, SocketAddressTest, testSocketRelationals);
|
||||||
CppUnit_addTest(pSuite, SocketAddressTest, testSocketAddress6);
|
CppUnit_addTest(pSuite, SocketAddressTest, testSocketAddress6);
|
||||||
|
CppUnit_addTest(pSuite, SocketAddressTest, testSocketAddressUnixLocal);
|
||||||
|
|
||||||
return pSuite;
|
return pSuite;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ public:
|
|||||||
void testSocketAddress();
|
void testSocketAddress();
|
||||||
void testSocketRelationals();
|
void testSocketRelationals();
|
||||||
void testSocketAddress6();
|
void testSocketAddress6();
|
||||||
|
void testSocketAddressUnixLocal();
|
||||||
|
|
||||||
void setUp();
|
void setUp();
|
||||||
void tearDown();
|
void tearDown();
|
||||||
|
|||||||
Reference in New Issue
Block a user