summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--chrome/browser/dom_ui/net_internals_ui.cc2
-rw-r--r--net/base/address_list.cc28
-rw-r--r--net/base/address_list_net_log_param.cc2
-rw-r--r--net/base/net_util.cc41
-rw-r--r--net/base/net_util.h10
-rw-r--r--net/base/net_util_unittest.cc33
-rw-r--r--net/socket/tcp_client_socket_libevent.cc2
-rw-r--r--net/socket/tcp_client_socket_win.cc2
8 files changed, 84 insertions, 36 deletions
diff --git a/chrome/browser/dom_ui/net_internals_ui.cc b/chrome/browser/dom_ui/net_internals_ui.cc
index 44345ff..553d197 100644
--- a/chrome/browser/dom_ui/net_internals_ui.cc
+++ b/chrome/browser/dom_ui/net_internals_ui.cc
@@ -727,7 +727,7 @@ void NetInternalsMessageHandler::IOThreadImpl::OnGetHostResolverCache(
const struct addrinfo* current_address = entry->addrlist.head();
while (current_address) {
address_list->Append(Value::CreateStringValue(
- net::NetAddressToString(current_address)));
+ net::NetAddressToStringWithPort(current_address)));
current_address = current_address->ai_next;
}
entry_dict->Set(L"addresses", address_list);
diff --git a/net/base/address_list.cc b/net/base/address_list.cc
index 1736a19..1c97311 100644
--- a/net/base/address_list.cc
+++ b/net/base/address_list.cc
@@ -7,6 +7,7 @@
#include <stdlib.h>
#include "base/logging.h"
+#include "net/base/net_util.h"
#include "net/base/sys_addrinfo.h"
namespace net {
@@ -71,28 +72,9 @@ void FreeMyAddrinfo(struct addrinfo* info) {
FreeMyAddrinfo(next);
}
-// Returns the address to port field in |info|.
-uint16* GetPortField(const struct addrinfo* info) {
- DCHECK(info);
- if (info->ai_family == AF_INET) {
- DCHECK_EQ(sizeof(sockaddr_in), info->ai_addrlen);
- struct sockaddr_in* sockaddr =
- reinterpret_cast<struct sockaddr_in*>(info->ai_addr);
- return &sockaddr->sin_port;
- } else if (info->ai_family == AF_INET6) {
- DCHECK_EQ(sizeof(sockaddr_in6), info->ai_addrlen);
- struct sockaddr_in6* sockaddr =
- reinterpret_cast<struct sockaddr_in6*>(info->ai_addr);
- return &sockaddr->sin6_port;
- } else {
- NOTREACHED();
- return NULL;
- }
-}
-
// Assign the port for all addresses in the list.
void SetPortRecursive(struct addrinfo* info, int port) {
- uint16* port_field = GetPortField(info);
+ uint16* port_field = GetPortFieldFromAddrinfo(info);
if (port_field)
*port_field = htons(port);
@@ -135,11 +117,7 @@ void AddressList::SetPort(int port) {
}
int AddressList::GetPort() const {
- uint16* port_field = GetPortField(data_->head);
- if (!port_field)
- return -1;
-
- return ntohs(*port_field);
+ return GetPortFromAddrinfo(data_->head);
}
bool AddressList::GetCanonicalName(std::string* canonical_name) const {
diff --git a/net/base/address_list_net_log_param.cc b/net/base/address_list_net_log_param.cc
index 9143ae9..05b1a0d 100644
--- a/net/base/address_list_net_log_param.cc
+++ b/net/base/address_list_net_log_param.cc
@@ -20,7 +20,7 @@ Value* AddressListNetLogParam::ToValue() const {
for (const addrinfo* head = address_list_.head();
head != NULL ; head = head->ai_next) {
- list->Append(Value::CreateStringValue(NetAddressToString(head)));
+ list->Append(Value::CreateStringValue(NetAddressToStringWithPort(head)));
}
dict->Set(L"address_list", list);
diff --git a/net/base/net_util.cc b/net/base/net_util.cc
index 5eb2e68..946b7ff7 100644
--- a/net/base/net_util.cc
+++ b/net/base/net_util.cc
@@ -1294,6 +1294,21 @@ std::string NetAddressToString(const struct addrinfo* net_address) {
return std::string(buffer);
}
+std::string NetAddressToStringWithPort(const struct addrinfo* net_address) {
+ std::string ip_address_string = NetAddressToString(net_address);
+ if (ip_address_string.empty())
+ return std::string(); // Failed.
+
+ int port = GetPortFromAddrinfo(net_address);
+
+ if (ip_address_string.find(':') != std::string::npos) {
+ // Surround with square brackets to avoid ambiguity.
+ return StringPrintf("[%s]:%d", ip_address_string.c_str(), port);
+ }
+
+ return StringPrintf("%s:%d", ip_address_string.c_str(), port);
+}
+
std::string GetHostName() {
#if defined(OS_WIN)
EnsureWinsockInit();
@@ -1882,4 +1897,30 @@ bool IPNumberMatchesPrefix(const IPAddressNumber& ip_number,
return true;
}
+// Returns the port field of the sockaddr in |info|.
+uint16* GetPortFieldFromAddrinfo(const struct addrinfo* info) {
+ DCHECK(info);
+ if (info->ai_family == AF_INET) {
+ DCHECK_EQ(sizeof(sockaddr_in), info->ai_addrlen);
+ struct sockaddr_in* sockaddr =
+ reinterpret_cast<struct sockaddr_in*>(info->ai_addr);
+ return &sockaddr->sin_port;
+ } else if (info->ai_family == AF_INET6) {
+ DCHECK_EQ(sizeof(sockaddr_in6), info->ai_addrlen);
+ struct sockaddr_in6* sockaddr =
+ reinterpret_cast<struct sockaddr_in6*>(info->ai_addr);
+ return &sockaddr->sin6_port;
+ } else {
+ NOTREACHED();
+ return NULL;
+ }
+}
+
+int GetPortFromAddrinfo(const struct addrinfo* info) {
+ uint16* port_field = GetPortFieldFromAddrinfo(info);
+ if (!port_field)
+ return -1;
+ return ntohs(*port_field);
+}
+
} // namespace net
diff --git a/net/base/net_util.h b/net/base/net_util.h
index 61e1957..5633c11 100644
--- a/net/base/net_util.h
+++ b/net/base/net_util.h
@@ -95,6 +95,10 @@ std::string GetHostAndOptionalPort(const GURL& url);
// Returns empty string on failure.
std::string NetAddressToString(const struct addrinfo* net_address);
+// Same as NetAddressToString, but additionally includes the port number. For
+// example: "192.168.0.1:99" or "[::1]:80".
+std::string NetAddressToStringWithPort(const struct addrinfo* net_address);
+
// Returns the hostname of the current system. Returns empty string on failure.
std::string GetHostName();
@@ -368,6 +372,12 @@ bool IPNumberMatchesPrefix(const IPAddressNumber& ip_number,
const IPAddressNumber& ip_prefix,
size_t prefix_length_in_bits);
+// Returns the port field of the sockaddr in |info|.
+uint16* GetPortFieldFromAddrinfo(const struct addrinfo* info);
+
+// Returns the value of |info's| port (in host byte ordering).
+int GetPortFromAddrinfo(const struct addrinfo* info);
+
} // namespace net
#endif // NET_BASE_NET_UTIL_H_
diff --git a/net/base/net_util_unittest.cc b/net/base/net_util_unittest.cc
index 9bc5d58..4e760fe 100644
--- a/net/base/net_util_unittest.cc
+++ b/net/base/net_util_unittest.cc
@@ -373,7 +373,7 @@ struct UrlTestData {
// Returns an addrinfo for the given 32-bit address (IPv4.)
// The result lives in static storage, so don't delete it.
// |bytes| should be an array of length 4.
-const struct addrinfo* GetIPv4Address(const uint8* bytes) {
+const struct addrinfo* GetIPv4Address(const uint8* bytes, int port) {
static struct addrinfo static_ai;
static struct sockaddr_in static_addr4;
@@ -386,7 +386,7 @@ const struct addrinfo* GetIPv4Address(const uint8* bytes) {
struct sockaddr_in* addr4 = &static_addr4;
memset(addr4, 0, sizeof(static_addr4));
- addr4->sin_port = htons(80);
+ addr4->sin_port = htons(port);
addr4->sin_family = ai->ai_family;
memcpy(&addr4->sin_addr, bytes, 4);
@@ -397,7 +397,7 @@ const struct addrinfo* GetIPv4Address(const uint8* bytes) {
// Returns a addrinfo for the given 128-bit address (IPv6.)
// The result lives in static storage, so don't delete it.
// |bytes| should be an array of length 16.
-const struct addrinfo* GetIPv6Address(const uint8* bytes) {
+const struct addrinfo* GetIPv6Address(const uint8* bytes, int port) {
static struct addrinfo static_ai;
static struct sockaddr_in6 static_addr6;
@@ -410,7 +410,7 @@ const struct addrinfo* GetIPv6Address(const uint8* bytes) {
struct sockaddr_in6* addr6 = &static_addr6;
memset(addr6, 0, sizeof(static_addr6));
- addr6->sin6_port = htons(80);
+ addr6->sin6_port = htons(port);
addr6->sin6_family = ai->ai_family;
memcpy(&addr6->sin6_addr, bytes, 16);
@@ -418,7 +418,6 @@ const struct addrinfo* GetIPv6Address(const uint8* bytes) {
return ai;
}
-
// A helper for IDN*{Fast,Slow}.
// Append "::<language list>" to |expected| and |actual| to make it
// easy to tell which sub-case fails without debugging.
@@ -1260,7 +1259,7 @@ TEST(NetUtilTest, NetAddressToString_IPv4) {
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
- const addrinfo* ai = GetIPv4Address(tests[i].addr);
+ const addrinfo* ai = GetIPv4Address(tests[i].addr, 80);
std::string result = net::NetAddressToString(ai);
EXPECT_EQ(std::string(tests[i].result), result);
}
@@ -1277,7 +1276,7 @@ TEST(NetUtilTest, NetAddressToString_IPv6) {
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
- const addrinfo* ai = GetIPv6Address(tests[i].addr);
+ const addrinfo* ai = GetIPv6Address(tests[i].addr, 80);
std::string result = net::NetAddressToString(ai);
// Allow NetAddressToString() to fail, in case the system doesn't
// support IPv6.
@@ -1286,6 +1285,26 @@ TEST(NetUtilTest, NetAddressToString_IPv6) {
}
}
+TEST(NetUtilTest, NetAddressToStringWithPort_IPv4) {
+ uint8 addr[] = {127, 0, 0, 1};
+ const addrinfo* ai = GetIPv4Address(addr, 166);
+ std::string result = net::NetAddressToStringWithPort(ai);
+ EXPECT_EQ("127.0.0.1:166", result);
+}
+
+TEST(NetUtilTest, NetAddressToStringWithPort_IPv6) {
+ uint8 addr[] = {
+ 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA,
+ 0x98, 0x76, 0x54, 0x32, 0x10
+ };
+ const addrinfo* ai = GetIPv6Address(addr, 361);
+ std::string result = net::NetAddressToStringWithPort(ai);
+
+ // May fail on systems that don't support IPv6.
+ if (!result.empty())
+ EXPECT_EQ("[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:361", result);
+}
+
TEST(NetUtilTest, GetHostName) {
// We can't check the result of GetHostName() directly, since the result
// will differ across machines. Our goal here is to simply exercise the
diff --git a/net/socket/tcp_client_socket_libevent.cc b/net/socket/tcp_client_socket_libevent.cc
index 81c3ccd..102a8fb 100644
--- a/net/socket/tcp_client_socket_libevent.cc
+++ b/net/socket/tcp_client_socket_libevent.cc
@@ -179,7 +179,7 @@ int TCPClientSocketLibevent::DoConnect() {
net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT,
new NetLogStringParameter(
- "address", NetAddressToString(current_ai_)));
+ "address", NetAddressToStringWithPort(current_ai_)));
next_connect_state_ = CONNECT_STATE_CONNECT_COMPLETE;
diff --git a/net/socket/tcp_client_socket_win.cc b/net/socket/tcp_client_socket_win.cc
index b25d07f..3faf41b 100644
--- a/net/socket/tcp_client_socket_win.cc
+++ b/net/socket/tcp_client_socket_win.cc
@@ -338,7 +338,7 @@ int TCPClientSocketWin::DoConnect() {
net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT,
new NetLogStringParameter(
- "address", NetAddressToString(current_ai_)));
+ "address", NetAddressToStringWithPort(current_ai_)));
next_connect_state_ = CONNECT_STATE_CONNECT_COMPLETE;