-
Notifications
You must be signed in to change notification settings - Fork 516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove Ip::Address implicit sockaddr_in6 conversion #1823
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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<struct sockaddr_in *>(&ss); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hope this particular code will disappear due to another change request, but the idea expressed here applies to (another area in) adjusted code as well. If possible, please avoid never-nil pointers to local objects and reduce the difference with the current code:
Suggested change
Same for the other similar changes in this PR, of course. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. I will do once that other discussion is resolved. |
||||||||||
|
||||||||||
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<unsigned short>(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<struct sockaddr_in6 *>(&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<unsigned short>(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<struct sockaddr_in6*>(&ss); | ||||||||||
Comment on lines
+536
to
+537
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we do the opposite -- continue to use
Suggested change
... and adjust Ip::Address construction accordingly. Same for other similar test cases, of course. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not without adding more technical debt for the future. I would like to get the API as minimal as possible without locking out capabilities like that. But not to go as far as a C library with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I do not see any technical debt. I suspect that my suggestion was misinterpreted. I now also realize that the second part of my suggestion was wrong -- one must not cast a sockaddr_in6 variable to sockaddr_storage. Only the opposite cast is safe!
I see no relationship between the facts mentioned above and my suggestion (that we should (continue to) use sockaddr_in6 type to create a sockaddr_in6 address for the test case). We obviously want to test with Ip::Address with IPv6 addresses. Let's (continue to) use the corresponding sockaddr_in6 type for that test. There is no reason to outlaw that type; we ought to support it at least until the last sockaddr_in6 expression is gone from Squid primary code.
We can also add sockaddr_storage tests, of course. They can (and probably should) reuse the bulk of existing sockaddr_in6 and sockaddr_in test code, memcopying configured sockaddr_in... addresses to sockaddr_storage variables.
No API changes or "locking out capabilities" suggested in this change request. |
||||||||||
|
||||||||||
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); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If possible:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am reluctant to do this one. While it matches up with the earlier IPv4 constructor test case, the intention of this case is to verify that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will rephrase to avoid contaminating this trivial change request with complex changes from another one:
Suggested change
|
||||||||||
|
||||||||||
bnIPA.toUrl(buf,MAX_IPSTRLEN); | ||||||||||
CPPUNIT_ASSERT( memcmp("[c0a8:640c:ffff:ffff:ffff:ffff:ffff:ffff]:80", buf, 44) == 0 ); | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I support adding a sockaddr_storage constructor, but why remove this constructor? What is wrong with it (other than the fact that it allows implicit conversions -- a problem that should be fixed by adding "explicit" rather than by removing)?
PR description says "Migration to sockaddr_storage API", but that phrase does not answer my question (and raises more red flags). If that phrase was meant to explain why sockaddr_in6 conversion constructor is removed, please rephrase. Otherwise, please remove that PR description phrase completely, so that we do not have to argue about (and adjust it to clarify) its meaning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This constructor (and matching assignment operator) are no longer used after this PR updates the callers to
sockaddr_storage
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... but their code is still there; it is just not wrapped in a Address methods anymore and is now duplicated and hidden. I was going to flag that problem in later review iterations (if still necessary)...
For now, I was hoping that you will agree to restore those methods, preserve Ip::Address API symmetry with respect to IPv4/IPv6, and focus on prohibiting implicit conversions and/or on adding explicit sockaddr_storage conversion support. The last two are good goals that can be accomplished without removing those IPv6-specific methods.