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

@@ -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");
}