Skip to content

Commit

Permalink
Add helper methods to compare WebCore::IPAddresses
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=248726
rdar://99360259

Reviewed by Tim Horton.

Add a helper method to compare two `WebCore::IPAddress`-es, and use it to implement some comparison
operators. Also, add an additions hook that allows us to adjust newly created `NSURLSession`s.

Test: IPAddressTests.CompareIPAddresses

* Source/WebCore/platform/network/DNS.h:
(WebCore::IPAddress::compare const):
(WebCore::IPAddress::operator< const):
(WebCore::IPAddress::operator> const):
(WebCore::IPAddress::operator== const):
(WebCore::IPAddress::operator!= const):

Add the `compare()` method, and use it to implement several binary comparison operators.

* Source/WebKit/NetworkProcess/cocoa/NetworkSessionCocoa.mm:
(WebKit::applyAdditionalSettings):
(WebKit::createURLSession):

Factor out logic to create a new `NSURLSession` into a separate helper method, and call the
additions hook (`applyAdditionalSettings`) from within this new helper.

(WebKit::SessionWrapper::initialize):
(WebKit::NetworkSessionCocoa::clearCredentials):
* Tools/TestWebKitAPI/Tests/WebCore/IPAddressTests.cpp:
(TestWebKitAPI::TEST):

Canonical link: https://commits.webkit.org/257443@main
  • Loading branch information
whsieh committed Dec 7, 2022
1 parent 91b71c3 commit 26acda4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
31 changes: 31 additions & 0 deletions Source/WebCore/platform/network/DNS.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,37 @@ class IPAddress {
const struct in_addr& ipv4Address() const { return std::get<struct in_addr>(m_address); }
const struct in6_addr& ipv6Address() const { return std::get<struct in6_addr>(m_address); }

enum class ComparisonResult : uint8_t {
CannotCompare,
Less,
Equal,
Greater
};

ComparisonResult compare(const IPAddress& other) const
{
auto comparisonResult = [](int result) {
if (!result)
return ComparisonResult::Equal;
if (result < 0)
return ComparisonResult::Less;
return ComparisonResult::Greater;
};

if (isIPv4() && other.isIPv4())
return comparisonResult(memcmp(&ipv4Address(), &other.ipv4Address(), sizeof(struct in_addr)));

if (isIPv6() && other.isIPv6())
return comparisonResult(memcmp(&ipv6Address(), &other.ipv6Address(), sizeof(struct in6_addr)));

return ComparisonResult::CannotCompare;
}

bool operator<(const IPAddress& other) const { return compare(other) == ComparisonResult::Less; }
bool operator>(const IPAddress& other) const { return compare(other) == ComparisonResult::Greater; }
bool operator==(const IPAddress& other) const { return compare(other) == ComparisonResult::Equal; }
bool operator!=(const IPAddress& other) const { return !(*this == other); }

private:
std::variant<WTF::HashTableEmptyValueType, struct in_addr, struct in6_addr> m_address;
};
Expand Down
23 changes: 19 additions & 4 deletions Source/WebKit/NetworkProcess/cocoa/NetworkSessionCocoa.mm
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,14 @@ static String stringForSSLCipher(SSLCipherSuite cipher)
}
#endif // HAVE(CFNETWORK_METRICS_APIS_V4)

#if USE(APPLE_INTERNAL_SDK) && __has_include(<WebKitAdditions/NetworkSessionCocoaAdditions.mm>)
#import <WebKitAdditions/NetworkSessionCocoaAdditions.mm>
#else
namespace WebKit {
inline static void applyAdditionalSettings(NSURLSession *) { }
}
#endif

@interface WKNetworkSessionDelegate : NSObject <NSURLSessionDataDelegate
#if HAVE(NSURLSESSION_WEBSOCKET)
, NSURLSessionWebSocketDelegate
Expand Down Expand Up @@ -1075,6 +1083,13 @@ - (void)URLSession:(NSURLSession *)session webSocketTask:(NSURLSessionWebSocketT

namespace WebKit {

static RetainPtr<NSURLSession> createURLSession(NSURLSessionConfiguration *configuration, id<NSURLSessionDelegate> delegate)
{
RetainPtr session = [NSURLSession sessionWithConfiguration:configuration delegate:delegate delegateQueue:NSOperationQueue.mainQueue];
applyAdditionalSettings(session.get());
return session;
}

#if ASSERT_ENABLED
static bool sessionsCreated = false;
#endif
Expand Down Expand Up @@ -1198,7 +1213,7 @@ - (void)URLSession:(NSURLSession *)session webSocketTask:(NSURLSessionWebSocketT
configuration._sourceApplicationSecondaryIdentifier = @"com.apple.WebKit.InAppBrowser";

delegate = adoptNS([[WKNetworkSessionDelegate alloc] initWithNetworkSession:networkSession wrapper:*this withCredentials:storedCredentialsPolicy == WebCore::StoredCredentialsPolicy::Use]);
session = [NSURLSession sessionWithConfiguration:configuration delegate:delegate.get() delegateQueue:[NSOperationQueue mainQueue]];
session = createURLSession(configuration, delegate.get());
}

#if HAVE(SESSION_CLEANUP)
Expand Down Expand Up @@ -1580,10 +1595,10 @@ static void activateSessionCleanup(NetworkSessionCocoa& session, const NetworkSe
ASSERT(m_downloadMap.isEmpty());
// FIXME: Use resetWithCompletionHandler instead.
m_sessionWithCredentialStorage = [NSURLSession sessionWithConfiguration:m_sessionWithCredentialStorage.get().configuration delegate:static_cast<id>(m_sessionWithCredentialStorageDelegate.get()) delegateQueue:[NSOperationQueue mainQueue]];
m_statelessSession = [NSURLSession sessionWithConfiguration:m_statelessSession.get().configuration delegate:static_cast<id>(m_statelessSessionDelegate.get()) delegateQueue:[NSOperationQueue mainQueue]];
m_statelessSession = createURLSession([m_statelessSession configuration], static_cast<id>(m_statelessSessionDelegate.get()));
for (auto& entry : m_isolatedSessions.values())
entry.session = [NSURLSession sessionWithConfiguration:entry.session.get().configuration delegate:static_cast<id>(entry.delegate.get()) delegateQueue:[NSOperationQueue mainQueue]];
m_appBoundSession.session = [NSURLSession sessionWithConfiguration:m_appBoundSession.session.get().configuration delegate:static_cast<id>(m_appBoundSession.delegate.get()) delegateQueue:[NSOperationQueue mainQueue]];
entry.session = createURLSession([entry.session configuration], static_cast<id>(entry.delegate.get()));
m_appBoundSession.session = createURLSession([m_appBoundSession.session configuration], m_appBoundSession.delegate.get());
#endif
}

Expand Down
16 changes: 16 additions & 0 deletions Tools/TestWebKitAPI/Tests/WebCore/IPAddressTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ TEST(IPAddressTests, InvalidAddresses)
EXPECT_EQ(WebCore::IPAddress::fromString("192.168.255.256"_s), std::nullopt);
}

TEST(IPAddressTests, CompareIPAddresses)
{
auto address1 = *WebCore::IPAddress::fromString("17.100.100.255"_s);
auto address2 = *WebCore::IPAddress::fromString("18.101.100.0"_s);
auto address3 = *WebCore::IPAddress::fromString("2001:db8::1234:1000"_s);
auto address4 = *WebCore::IPAddress::fromString("2001:db9::1234:0000"_s);

EXPECT_TRUE(address1 < address2);
EXPECT_TRUE(address2 > address1);
EXPECT_TRUE(address3 < address4);
EXPECT_TRUE(address4 > address3);
EXPECT_TRUE(address1 == WebCore::IPAddress::fromString("17.100.100.255"_s));
EXPECT_EQ(address1.compare(address3), WebCore::IPAddress::ComparisonResult::CannotCompare);
EXPECT_EQ(address4.compare(address2), WebCore::IPAddress::ComparisonResult::CannotCompare);
}

#endif // OS(UNIX)

} // namespace TestWebKitAPI
Expand Down

0 comments on commit 26acda4

Please sign in to comment.