diff --git a/src/ip/Address.cc b/src/ip/Address.cc index 62a07cfb9c2..7b9d9543b54 100644 --- a/src/ip/Address.cc +++ b/src/ip/Address.cc @@ -445,6 +445,12 @@ Ip::Address::operator =(struct sockaddr_in const &s) return *this; }; +Ip::Address::Address(struct sockaddr_storage const &s) +{ + setEmpty(); + operator=(s); +}; + Ip::Address & Ip::Address::operator =(const struct sockaddr_storage &s) { @@ -459,19 +465,6 @@ Ip::Address::operator =(const struct sockaddr_storage &s) return *this; }; -Ip::Address::Address(struct sockaddr_in6 const &s) -{ - setEmpty(); - operator=(s); -}; - -Ip::Address & -Ip::Address::operator =(struct sockaddr_in6 const &s) -{ - memmove(&mSocketAddr_, &s, sizeof(struct sockaddr_in6)); - return *this; -}; - Ip::Address::Address(struct in_addr const &s) { setEmpty(); @@ -557,8 +550,6 @@ Ip::Address::operator =(const struct addrinfo &s) struct sockaddr_in* ipv4 = nullptr; - struct sockaddr_in6* ipv6 = nullptr; - //struct addrinfo { // int ai_flags; /* input flags */ // int ai_family; /* protocol family for socket */ @@ -580,10 +571,9 @@ Ip::Address::operator =(const struct addrinfo &s) break; case AF_INET6: - ipv6 = (sockaddr_in6*)(s.ai_addr); /* this */ - assert(ipv6); - operator=(*ipv6); + assert(s.ai_addr); + memmove(&mSocketAddr_, s.ai_addr, sizeof(struct sockaddr_in6)); break; case AF_UNSPEC: @@ -592,7 +582,7 @@ Ip::Address::operator =(const struct addrinfo &s) // such as those where data only comes from getsockopt() if (s.ai_addr != nullptr) { if (s.ai_addrlen == sizeof(struct sockaddr_in6)) { - operator=(*((struct sockaddr_in6*)s.ai_addr)); + memmove(&mSocketAddr_, s.ai_addr, s.ai_addrlen); return true; } else if (s.ai_addrlen == sizeof(struct sockaddr_in)) { operator=(*((struct sockaddr_in*)s.ai_addr)); diff --git a/src/ip/Address.h b/src/ip/Address.h index 49021a77d2a..58c68bb61bf 100644 --- a/src/ip/Address.h +++ b/src/ip/Address.h @@ -44,10 +44,10 @@ class Address /** @name Constructors */ /*@{*/ Address() { setEmpty(); } + explicit Address(const struct sockaddr_storage &); Address(const struct in_addr &); Address(const struct sockaddr_in &); Address(const struct in6_addr &); - Address(const struct sockaddr_in6 &); Address(const struct hostent &); Address(const struct addrinfo &); Address(const char*); @@ -59,7 +59,6 @@ class Address Address& operator =(struct sockaddr_storage const &s); Address& operator =(struct in_addr const &s); Address& operator =(struct in6_addr const &s); - Address& operator =(struct sockaddr_in6 const &s); bool operator =(const struct hostent &s); bool operator =(const struct addrinfo &s); bool operator =(const char *s); diff --git a/src/tests/stub_libip.cc b/src/tests/stub_libip.cc index b46ba075fc2..a394b541af8 100644 --- a/src/tests/stub_libip.cc +++ b/src/tests/stub_libip.cc @@ -13,10 +13,10 @@ #include "tests/STUB.h" #include "ip/Address.h" +Ip::Address::Address(const struct sockaddr_storage &) STUB Ip::Address::Address(const struct in_addr &) STUB Ip::Address::Address(const struct sockaddr_in &) STUB Ip::Address::Address(const struct in6_addr &) STUB -Ip::Address::Address(const struct sockaddr_in6 &) STUB Ip::Address::Address(const struct hostent &) STUB Ip::Address::Address(const struct addrinfo &) STUB Ip::Address::Address(const char*) STUB @@ -24,7 +24,6 @@ Ip::Address& Ip::Address::operator =(struct sockaddr_in const &) STUB_RETVAL(*th Ip::Address& Ip::Address::operator =(struct sockaddr_storage const &) STUB_RETVAL(*this) Ip::Address& Ip::Address::operator =(struct in_addr const &) STUB_RETVAL(*this) Ip::Address& Ip::Address::operator =(struct in6_addr const &) STUB_RETVAL(*this) -Ip::Address& Ip::Address::operator =(struct sockaddr_in6 const &) STUB_RETVAL(*this) bool Ip::Address::operator =(const struct hostent &) STUB_RETVAL(false) bool Ip::Address::operator =(const struct addrinfo &) STUB_RETVAL(false) bool Ip::Address::operator =(const char *) STUB_RETVAL(false) diff --git a/src/tests/testIpAddress.cc b/src/tests/testIpAddress.cc index f9b549a6591..9f836ea880f 100644 --- a/src/tests/testIpAddress.cc +++ b/src/tests/testIpAddress.cc @@ -36,7 +36,6 @@ class TestIpAddress : public CPPUNIT_NS::TestFixture CPPUNIT_TEST(testInAddrConstructor); CPPUNIT_TEST(testInAddr6Constructor); CPPUNIT_TEST(testSockAddrConstructor); - CPPUNIT_TEST(testSockAddr6Constructor); CPPUNIT_TEST(testHostentConstructor); CPPUNIT_TEST(testStringConstructor); CPPUNIT_TEST(testCopyConstructor); @@ -59,7 +58,6 @@ class TestIpAddress : public CPPUNIT_NS::TestFixture void testInAddrConstructor(); void testInAddr6Constructor(); void testSockAddrConstructor(); - void testSockAddr6Constructor(); void testHostentConstructor(); void testStringConstructor(); void testCopyConstructor(); @@ -144,62 +142,64 @@ TestIpAddress::testInAddr6Constructor() void TestIpAddress::testSockAddrConstructor() { - struct sockaddr_in insock; - struct sockaddr_in outsock; + // sockaddr_storage containing sockaddr_in + { + struct sockaddr_storage ss = {}; + auto *insock = reinterpret_cast(&ss); - memset(&insock, 0, sizeof(struct sockaddr_in)); - memset(&outsock, 0, sizeof(struct sockaddr_in)); + struct sockaddr_in outsock; + memset(&outsock, 0, sizeof(struct sockaddr_in)); - insock.sin_family = AF_INET; - insock.sin_port = htons(80); - insock.sin_addr.s_addr = htonl(0xC0A8640C); + insock->sin_family = AF_INET; + insock->sin_port = htons(80); + insock->sin_addr.s_addr = htonl(0xC0A8640C); #if HAVE_SIN_LEN_IN_SAI - insock.sin_len = sizeof(struct sockaddr_in); + insock->sin_len = sizeof(struct sockaddr_in); #endif - Ip::Address anIPA(insock); + Ip::Address anIPA(ss); + + /* test stored values */ + CPPUNIT_ASSERT(!anIPA.isAnyAddr()); + CPPUNIT_ASSERT(!anIPA.isNoAddr()); + CPPUNIT_ASSERT(anIPA.isIPv4()); + CPPUNIT_ASSERT(!anIPA.isIPv6()); + CPPUNIT_ASSERT(anIPA.isSockAddr()); + CPPUNIT_ASSERT_EQUAL(static_cast(80), anIPA.port()); + anIPA.getSockAddr(outsock); + CPPUNIT_ASSERT(memcmp(insock, &outsock, sizeof(struct sockaddr_in)) == 0); + } - /* test stored values */ - CPPUNIT_ASSERT( !anIPA.isAnyAddr() ); - CPPUNIT_ASSERT( !anIPA.isNoAddr() ); - CPPUNIT_ASSERT( anIPA.isIPv4() ); - CPPUNIT_ASSERT( !anIPA.isIPv6() ); - CPPUNIT_ASSERT( anIPA.isSockAddr() ); - CPPUNIT_ASSERT_EQUAL( (unsigned short) 80, anIPA.port() ); - anIPA.getSockAddr(outsock); - CPPUNIT_ASSERT( memcmp( &insock, &outsock, sizeof(struct sockaddr_in)) == 0 ); -} + // sockaddr_storage containing sockaddr_in6 + { + struct sockaddr_storage ss = {}; + auto *insock = reinterpret_cast(&ss); -void -TestIpAddress::testSockAddr6Constructor() -{ - struct sockaddr_in6 insock; - struct sockaddr_in6 outsock; - - memset(&insock, 0, sizeof(struct sockaddr_in6)); - memset(&outsock, 0, sizeof(struct sockaddr_in6)); - - insock.sin6_family = AF_INET6; - insock.sin6_port = htons(80); - insock.sin6_addr.s6_addr32[0] = htonl(0xFFFFFFFF); - insock.sin6_addr.s6_addr32[1] = htonl(0x00000000); - insock.sin6_addr.s6_addr32[2] = htonl(0x0000FFFF); - insock.sin6_addr.s6_addr32[3] = htonl(0xC0A8640C); + struct sockaddr_in6 outsock; + memset(&outsock, 0, sizeof(struct sockaddr_in6)); + + insock->sin6_family = AF_INET6; + insock->sin6_port = htons(80); + insock->sin6_addr.s6_addr32[0] = htonl(0xFFFFFFFF); + insock->sin6_addr.s6_addr32[1] = htonl(0x00000000); + insock->sin6_addr.s6_addr32[2] = htonl(0x0000FFFF); + insock->sin6_addr.s6_addr32[3] = htonl(0xC0A8640C); #if HAVE_SIN6_LEN_IN_SAI - insock.sin6_len = sizeof(struct sockaddr_in6); + insock->sin6_len = sizeof(struct sockaddr_in6); #endif - Ip::Address anIPA((const struct sockaddr_in6)insock); - - /* test stored values */ - CPPUNIT_ASSERT( !anIPA.isAnyAddr() ); - CPPUNIT_ASSERT( !anIPA.isNoAddr() ); - CPPUNIT_ASSERT( !anIPA.isIPv4() ); - CPPUNIT_ASSERT( anIPA.isIPv6() ); - CPPUNIT_ASSERT( anIPA.isSockAddr() ); - CPPUNIT_ASSERT_EQUAL( (unsigned short) 80, anIPA.port() ); - anIPA.getSockAddr(outsock); - CPPUNIT_ASSERT( memcmp( &insock, &outsock, sizeof(struct sockaddr_in6)) == 0 ); + Ip::Address anIP(ss); + + /* test stored values */ + CPPUNIT_ASSERT(!anIP.isAnyAddr()); + CPPUNIT_ASSERT(!anIP.isNoAddr()); + CPPUNIT_ASSERT(!anIP.isIPv4()); + CPPUNIT_ASSERT(anIP.isIPv6()); + CPPUNIT_ASSERT(anIP.isSockAddr()); + CPPUNIT_ASSERT_EQUAL(static_cast(80), anIP.port()); + anIP.getSockAddr(outsock); + CPPUNIT_ASSERT(memcmp(insock, &outsock, sizeof(struct sockaddr_in6)) == 0); + } } void @@ -532,20 +532,21 @@ TestIpAddress::testtoUrl_fromSockAddr() anIPA.toUrl(buf,MAX_IPSTRLEN); CPPUNIT_ASSERT( memcmp("192.168.100.12:80", buf, 17) == 0 ); - /* test output when constructed from in6_addr with IPv6 */ - struct sockaddr_in6 ip6val; - - ip6val.sin6_addr.s6_addr32[0] = htonl(0xC0A8640C); - ip6val.sin6_addr.s6_addr32[1] = htonl(0xFFFFFFFF); - ip6val.sin6_addr.s6_addr32[2] = htonl(0xFFFFFFFF); - ip6val.sin6_addr.s6_addr32[3] = htonl(0xFFFFFFFF); - ip6val.sin6_port = htons(80); - ip6val.sin6_family = AF_INET6; + /* test output when constructed from sockaddr_storage with IPv6 */ + struct sockaddr_storage ss; + auto *ip6val = reinterpret_cast(&ss); + + ip6val->sin6_addr.s6_addr32[0] = htonl(0xC0A8640C); + ip6val->sin6_addr.s6_addr32[1] = htonl(0xFFFFFFFF); + ip6val->sin6_addr.s6_addr32[2] = htonl(0xFFFFFFFF); + ip6val->sin6_addr.s6_addr32[3] = htonl(0xFFFFFFFF); + ip6val->sin6_port = htons(80); + ip6val->sin6_family = AF_INET6; #if HAVE_SIN6_LEN_IN_SAI - ip6val.sin6_len = sizeof(struct sockaddr_in6); + ip6val->sin6_len = sizeof(struct sockaddr_in6); #endif - Ip::Address bnIPA(ip6val); + Ip::Address bnIPA(ss); bnIPA.toUrl(buf,MAX_IPSTRLEN); CPPUNIT_ASSERT( memcmp("[c0a8:640c:ffff:ffff:ffff:ffff:ffff:ffff]:80", buf, 44) == 0 );