fixed GH #2042: Android abstract namespace local socket address

This commit is contained in:
Günter Obiltschnig
2017-12-18 10:14:09 +01:00
parent 3f8c637610
commit 98feaa7bb9
3 changed files with 26 additions and 11 deletions

View File

@@ -57,18 +57,18 @@ public:
/// Creates a wildcard (all zero) IPv4 SocketAddress. /// Creates a wildcard (all zero) IPv4 SocketAddress.
explicit SocketAddress(Family family); explicit SocketAddress(Family family);
/// Creates a SocketAddress with unspecified (wildcard) IP address /// Creates a SocketAddress with unspecified (wildcard) IP address
/// of the given family. /// of the given family.
SocketAddress(const IPAddress& hostAddress, Poco::UInt16 portNumber); SocketAddress(const IPAddress& hostAddress, Poco::UInt16 portNumber);
/// Creates a SocketAddress from an IP address and given port number. /// Creates a SocketAddress from an IP address and given port number.
explicit SocketAddress(Poco::UInt16 port); explicit SocketAddress(Poco::UInt16 port);
/// Creates a SocketAddress with unspecified (wildcard) IP address /// Creates a SocketAddress with unspecified (wildcard) IP address
/// and given port number. /// and given port number.
SocketAddress(Family family, Poco::UInt16 port); SocketAddress(Family family, Poco::UInt16 port);
/// Creates a SocketAddress with unspecified (wildcard) IP address /// Creates a SocketAddress with unspecified (wildcard) IP address
/// of the given family, and given port number. /// of the given family, and given port number.
SocketAddress(const std::string& hostAddress, Poco::UInt16 portNumber); SocketAddress(const std::string& hostAddress, Poco::UInt16 portNumber);
@@ -125,7 +125,7 @@ public:
/// www.appinf.com:8080 /// www.appinf.com:8080
/// ///
/// On POSIX platforms supporting UNIX_LOCAL sockets, hostAndPort /// On POSIX platforms supporting UNIX_LOCAL sockets, hostAndPort
/// can also be the absolute path of a local socket, starting with a /// can also be the absolute path of a local socket, starting with a
/// slash, e.g. "/tmp/local.socket". /// slash, e.g. "/tmp/local.socket".
SocketAddress(Family family, const std::string& addr); SocketAddress(Family family, const std::string& addr);
@@ -174,7 +174,7 @@ public:
enum enum
{ {
MAX_ADDRESS_LENGTH = MAX_ADDRESS_LENGTH =
#if defined(POCO_OS_FAMILY_UNIX) #if defined(POCO_OS_FAMILY_UNIX)
sizeof(struct sockaddr_un) sizeof(struct sockaddr_un)
#elif defined(POCO_HAVE_IPv6) #elif defined(POCO_HAVE_IPv6)
@@ -202,7 +202,7 @@ private:
void newIPv4(); void newIPv4();
void newIPv4(const sockaddr_in*); void newIPv4(const sockaddr_in*);
void newIPv4(const IPAddress& hostAddress, Poco::UInt16 portNumber); void newIPv4(const IPAddress& hostAddress, Poco::UInt16 portNumber);
#if defined(POCO_HAVE_IPv6) #if defined(POCO_HAVE_IPv6)
void newIPv6(const sockaddr_in6*); void newIPv6(const sockaddr_in6*);
void newIPv6(const IPAddress& hostAddress, Poco::UInt16 portNumber); void newIPv6(const IPAddress& hostAddress, Poco::UInt16 portNumber);
@@ -212,7 +212,7 @@ private:
void newLocal(const sockaddr_un* sockAddr); void newLocal(const sockaddr_un* sockAddr);
void newLocal(const std::string& path); void newLocal(const std::string& path);
#endif #endif
Ptr _pImpl; Ptr _pImpl;
}; };
@@ -250,7 +250,7 @@ inline void SocketAddress::newIPv6(const sockaddr_in6* sockAddr)
{ {
_pImpl = new Poco::Net::Impl::IPv6SocketAddressImpl(sockAddr); _pImpl = new Poco::Net::Impl::IPv6SocketAddressImpl(sockAddr);
} }
inline void SocketAddress::newIPv6(const IPAddress& hostAddress, Poco::UInt16 portNumber) inline void SocketAddress::newIPv6(const IPAddress& hostAddress, Poco::UInt16 portNumber)
{ {
@@ -268,7 +268,7 @@ inline void SocketAddress::newLocal(const sockaddr_un* sockAddr)
inline void SocketAddress::newLocal(const std::string& path) inline void SocketAddress::newLocal(const std::string& path)
{ {
_pImpl = new Poco::Net::Impl::LocalSocketAddressImpl(path.c_str()); _pImpl = new Poco::Net::Impl::LocalSocketAddressImpl(path.c_str(), path.size());
} }
#endif // POCO_OS_FAMILY_UNIX #endif // POCO_OS_FAMILY_UNIX
@@ -280,7 +280,7 @@ inline bool SocketAddress::operator == (const SocketAddress& socketAddress) con
return toString() == socketAddress.toString(); return toString() == socketAddress.toString();
else else
#endif #endif
return host() == socketAddress.host() && port() == socketAddress.port(); return host() == socketAddress.host() && port() == socketAddress.port();
} }

View File

@@ -185,12 +185,13 @@ class Net_API LocalSocketAddressImpl: public SocketAddressImpl
public: public:
LocalSocketAddressImpl(const struct sockaddr_un* addr); LocalSocketAddressImpl(const struct sockaddr_un* addr);
LocalSocketAddressImpl(const char* path); LocalSocketAddressImpl(const char* path);
LocalSocketAddressImpl(const char* path, std::size_t length);
~LocalSocketAddressImpl(); ~LocalSocketAddressImpl();
IPAddress host() const; IPAddress host() const;
UInt16 port() const; UInt16 port() const;
poco_socklen_t length() const; poco_socklen_t length() const;
const struct sockaddr* addr() const; const struct sockaddr* addr() const;
int af() const; int af() const;
Family family() const; Family family() const;
const char* path() const; const char* path() const;
std::string toString() const; std::string toString() const;

View File

@@ -144,6 +144,8 @@ LocalSocketAddressImpl::LocalSocketAddressImpl(const struct sockaddr_un* addr)
LocalSocketAddressImpl::LocalSocketAddressImpl(const char* path) LocalSocketAddressImpl::LocalSocketAddressImpl(const char* path)
{ {
poco_assert (std::strlen(path) < sizeof(_pAddr->sun_path));
_pAddr = new sockaddr_un; _pAddr = new sockaddr_un;
poco_set_sun_len(_pAddr, std::strlen(path) + sizeof(struct sockaddr_un) - sizeof(_pAddr->sun_path) + 1); poco_set_sun_len(_pAddr, std::strlen(path) + sizeof(struct sockaddr_un) - sizeof(_pAddr->sun_path) + 1);
_pAddr->sun_family = AF_UNIX; _pAddr->sun_family = AF_UNIX;
@@ -151,6 +153,18 @@ LocalSocketAddressImpl::LocalSocketAddressImpl(const char* path)
} }
LocalSocketAddressImpl::LocalSocketAddressImpl(const char* path, std::size_t length)
{
poco_assert (length < sizeof(_pAddr->sun_path));
_pAddr = new sockaddr_un;
poco_set_sun_len(_pAddr, length + sizeof(struct sockaddr_un) - sizeof(_pAddr->sun_path) + 1);
_pAddr->sun_family = AF_UNIX;
std::memcpy(_pAddr->sun_path, path, length);
_pAddr->sun_path[length] = 0;
}
LocalSocketAddressImpl::~LocalSocketAddressImpl() LocalSocketAddressImpl::~LocalSocketAddressImpl()
{ {
delete _pAddr; delete _pAddr;