Commit 54fa023d authored by rakshasa's avatar rakshasa

Added direct conversion from sin_addr and sin6_addr to socket_address_key.

parent 0500ef7d
......@@ -22,7 +22,10 @@ public:
// TODO: Make from_sockaddr an rvalue reference.
static bool is_comparable_sockaddr(const sockaddr* sa);
static socket_address_key from_sockaddr(const sockaddr* sa);
static socket_address_key from_sin_addr(const sockaddr_in& sa);
static socket_address_key from_sin6_addr(const sockaddr_in6& sa);
bool operator < (const socket_address_key& sa) const;
bool operator > (const socket_address_key& sa) const;
......@@ -48,9 +51,10 @@ inline socket_address_key
socket_address_key::from_sockaddr(const sockaddr* sa) {
socket_address_key result;
result.m_family = AF_UNSPEC;
std::memset(&result, 0, sizeof(socket_address_key));
result.m_family = AF_UNSPEC;
if (sa == NULL)
return result;
......@@ -74,7 +78,29 @@ socket_address_key::from_sockaddr(const sockaddr* sa) {
return result;
}
// TODO: Add sockaddr_in/in6.
inline socket_address_key
socket_address_key::from_sin_addr(const sockaddr_in& sa) {
socket_address_key result;
std::memset(&result, 0, sizeof(socket_address_key));
result.m_family = AF_INET;
result.m_addr.s_addr = ntohl(sa.sin_addr.s_addr);
return result;
}
inline socket_address_key
socket_address_key::from_sin6_addr(const sockaddr_in6& sa) {
socket_address_key result;
std::memset(&result, 0, sizeof(socket_address_key));
result.m_family = AF_INET6;
result.m_addr6 = sa.sin6_addr;
return result;
}
inline bool
socket_address_key::operator < (const socket_address_key& sa) const {
......
......@@ -62,6 +62,11 @@ test_create_inet6(const char* hostname) {
return test_create_valid(hostname, std::bind(&torrent::address_info_lookup, hostname, AF_INET6, 0));
}
static bool
test_create_inet6_throws(const char* hostname) {
return test_create_throws(hostname, std::bind(&torrent::address_info_lookup, hostname, AF_INET6, 0));
}
//
// Basic tests:
//
......@@ -75,7 +80,7 @@ test_socket_address_key::test_basic() {
CPPUNIT_ASSERT(test_create_inet6("2001:0db8:85a3:0000:0000:8a2e:0370:7334").is_valid());
CPPUNIT_ASSERT(test_create_inet6("2001:db8:a::123").is_valid());
// CPPUNIT_ASSERT(test_create_inet6("1.1.1.1").is_valid());
CPPUNIT_ASSERT(test_create_inet6_throws("2001:db8:a::22123"));
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment