diff options
author | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-12 04:42:39 +0000 |
---|---|---|
committer | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-12 04:42:39 +0000 |
commit | 1a2123c629bc9088e7fa67b466681f5b1ca17893 (patch) | |
tree | b4ee7b0e7f2f24a74f244b8a642d480b05c059d8 /net | |
parent | 5effdcf0b8d9f252f7644b922da91b3dd2d8c390 (diff) | |
download | chromium_src-1a2123c629bc9088e7fa67b466681f5b1ca17893.zip chromium_src-1a2123c629bc9088e7fa67b466681f5b1ca17893.tar.gz chromium_src-1a2123c629bc9088e7fa67b466681f5b1ca17893.tar.bz2 |
Use IPEndPoint for P2P IPC messages
BUG=None
TEST=None
Review URL: http://codereview.chromium.org/6685013
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@77923 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/base/ip_endpoint.cc | 11 | ||||
-rw-r--r-- | net/base/ip_endpoint.h | 5 | ||||
-rw-r--r-- | net/base/ip_endpoint_unittest.cc | 22 |
3 files changed, 34 insertions, 4 deletions
diff --git a/net/base/ip_endpoint.cc b/net/base/ip_endpoint.cc index 626d12e..df90f1a 100644 --- a/net/base/ip_endpoint.cc +++ b/net/base/ip_endpoint.cc @@ -5,6 +5,7 @@ #include "net/base/ip_endpoint.h" #include "base/logging.h" +#include "base/string_number_conversions.h" #if defined(OS_WIN) #include <winsock2.h> #elif defined(OS_POSIX) @@ -108,6 +109,16 @@ bool IPEndPoint::FromSockAddr(const struct sockaddr* address, return true; } +std::string IPEndPoint::ToString() const { + struct sockaddr_storage addr_storage; + size_t addr_len = sizeof(addr_storage); + struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); + if (!ToSockAddr(addr, &addr_len)) { + return ""; + } + return NetAddressToString(addr, addr_len) + ":" + base::IntToString(port_); +} + bool IPEndPoint::operator<(const IPEndPoint& that) const { return address_ < that.address_ || port_ < that.port_; } diff --git a/net/base/ip_endpoint.h b/net/base/ip_endpoint.h index af5de87..f76a82a 100644 --- a/net/base/ip_endpoint.h +++ b/net/base/ip_endpoint.h @@ -44,6 +44,11 @@ class IPEndPoint { // Returns true on success, false on failure. bool FromSockAddr(const struct sockaddr* address, size_t address_length); + // Returns value as a string (e.g. "127.0.0.1:80"). Returns empty + // string if the address is invalid, and cannot not be converted to a + // string. + std::string ToString() const; + bool operator<(const IPEndPoint& that) const; bool operator==(const IPEndPoint& that) const; diff --git a/net/base/ip_endpoint_unittest.cc b/net/base/ip_endpoint_unittest.cc index ae09ad3..17f6e0b 100644 --- a/net/base/ip_endpoint_unittest.cc +++ b/net/base/ip_endpoint_unittest.cc @@ -4,6 +4,7 @@ #include "net/base/ip_endpoint.h" +#include "base/string_number_conversions.h" #include "net/base/net_util.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/platform_test.h" @@ -19,13 +20,14 @@ namespace { struct TestData { std::string host; + std::string host_normalized; bool ipv6; IPAddressNumber ip_address; } tests[] = { - { "127.0.00.1", false}, - { "192.168.1.1", false }, - { "::1", true }, - { "2001:db8:0::42", true }, + { "127.0.00.1", "127.0.0.1", false}, + { "192.168.1.1", "192.168.1.1", false }, + { "::1", "::1", true }, + { "2001:db8:0::42", "2001:db8::42", true }, }; int test_count = ARRAYSIZE_UNSAFE(tests); @@ -136,6 +138,18 @@ TEST_F(IPEndPointTest, LessThan) { EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); } +TEST_F(IPEndPointTest, ToString) { + IPEndPoint endpoint; + EXPECT_EQ(0, endpoint.port()); + + for (int index = 0; index < test_count; ++index) { + int port = 100 + index; + IPEndPoint endpoint(tests[index].ip_address, port); + EXPECT_EQ(tests[index].host_normalized + ":" + base::IntToString(port), + endpoint.ToString()); + } +} + } // namespace } // namespace net |