summaryrefslogtreecommitdiffstats
path: root/net/base
diff options
context:
space:
mode:
authorericroman@google.com <ericroman@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-02 21:45:18 +0000
committerericroman@google.com <ericroman@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-02 21:45:18 +0000
commit50d7d7283db42b531a9df9c6f5a34b8526d4d5b0 (patch)
tree6d29abd0b0a82a658a9a526e37279a26a636142e /net/base
parente97cdd5e81a3f8c3bd735ab1e7d264c36c2c3870 (diff)
downloadchromium_src-50d7d7283db42b531a9df9c6f5a34b8526d4d5b0.zip
chromium_src-50d7d7283db42b531a9df9c6f5a34b8526d4d5b0.tar.gz
chromium_src-50d7d7283db42b531a9df9c6f5a34b8526d4d5b0.tar.bz2
Add js bindings layer for ProxyResolverV8 {"alert()", "dnsResolve()", "myIpAddress" <-- [partial]}.
Also adds a utility function to net_util for turning a addrinfo into an IP address string. BUG=2764 Review URL: http://codereview.chromium.org/31009 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@10730 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-rw-r--r--net/base/net_util.cc25
-rw-r--r--net/base/net_util.h5
-rw-r--r--net/base/net_util_unittest.cc89
3 files changed, 118 insertions, 1 deletions
diff --git a/net/base/net_util.cc b/net/base/net_util.cc
index 7cee8f1..7527b6c 100644
--- a/net/base/net_util.cc
+++ b/net/base/net_util.cc
@@ -15,7 +15,10 @@
#if defined(OS_WIN)
#include <windows.h>
#include <winsock2.h>
+#include <ws2tcpip.h>
+#include <wspiapi.h> // Needed for Win2k compat.
#elif defined(OS_POSIX)
+#include <netdb.h>
#include <sys/socket.h>
#include <fcntl.h>
#endif
@@ -40,6 +43,9 @@
#include "googleurl/src/url_parse.h"
#include "net/base/escape.h"
#include "net/base/net_module.h"
+#if defined(OS_WIN)
+#include "net/base/winsock_init.h"
+#endif
#include "net/base/base64.h"
#include "unicode/datefmt.h"
@@ -1012,4 +1018,23 @@ bool GetHostAndPort(const std::string& host_and_port,
return GetHostAndPort(host_and_port.begin(), host_and_port.end(), host, port);
}
+std::string NetAddressToString(const struct addrinfo* net_address) {
+#if defined(OS_WIN)
+ EnsureWinsockInit();
+#endif
+
+ // This buffer is large enough to fit the biggest IPv6 string.
+ char buffer[INET6_ADDRSTRLEN];
+
+ int result = getnameinfo(net_address->ai_addr,
+ net_address->ai_addrlen, buffer, sizeof(buffer), NULL, 0, NI_NUMERICHOST);
+
+ if (result != 0) {
+ DLOG(INFO) << "getnameinfo() failed with " << result;
+ return std::string();
+ }
+
+ return std::string(buffer);
+}
+
} // namespace net
diff --git a/net/base/net_util.h b/net/base/net_util.h
index 1fda9bd..6f3c1ff 100644
--- a/net/base/net_util.h
+++ b/net/base/net_util.h
@@ -17,6 +17,7 @@
#include "googleurl/src/url_canon.h"
#include "googleurl/src/url_parse.h"
+struct addrinfo;
class FilePath;
class GURL;
@@ -53,6 +54,10 @@ bool GetHostAndPort(const std::string& host_and_port,
std::string* host,
int* port);
+// Returns the string representation of an address, like "192.168.0.1".
+// Returns empty string on failure.
+std::string NetAddressToString(const struct addrinfo* net_address);
+
// Return the value of the HTTP response header with name 'name'. 'headers'
// should be in the format that URLRequest::GetResponseHeaders() returns.
// Returns the empty string if the header is not found.
diff --git a/net/base/net_util_unittest.cc b/net/base/net_util_unittest.cc
index c83d510..2293368 100644
--- a/net/base/net_util_unittest.cc
+++ b/net/base/net_util_unittest.cc
@@ -10,6 +10,12 @@
#include "net/base/net_util.h"
#include "testing/gtest/include/gtest/gtest.h"
+#if defined(OS_WIN)
+#include <ws2tcpip.h>
+#else
+#include <netdb.h>
+#endif
+
namespace {
class NetUtilTest : public testing::Test {
@@ -57,6 +63,52 @@ struct SuggestedFilenameCase {
const wchar_t* expected_filename;
};
+// Returns an addrinfo for the given 32-bit address (IPv4.)
+// The result lives in static storage, so don't delete it.
+const struct addrinfo* GetIPv4Address(const uint8 bytes[4]) {
+ static struct addrinfo static_ai;
+ static struct sockaddr_in static_addr4;
+
+ struct addrinfo* ai = &static_ai;
+ ai->ai_socktype = SOCK_STREAM;
+ memset(ai, 0, sizeof(static_ai));
+
+ ai->ai_family = AF_INET;
+ ai->ai_addrlen = sizeof(static_addr4);
+
+ struct sockaddr_in* addr4 = &static_addr4;
+ memset(addr4, 0, sizeof(static_addr4));
+ addr4->sin_port = htons(80);
+ addr4->sin_family = ai->ai_family;
+ memcpy(&addr4->sin_addr, bytes, sizeof(bytes));
+
+ ai->ai_addr = (sockaddr*)addr4;
+ return ai;
+}
+
+// Returns a addrinfo for the given 128-bit address (IPv6.)
+// The result lives in static storage, so don't delete it.
+const struct addrinfo* GetIPv6Address(const uint8 bytes[16]) {
+ static struct addrinfo static_ai;
+ static struct sockaddr_in6 static_addr6;
+
+ struct addrinfo* ai = &static_ai;
+ ai->ai_socktype = SOCK_STREAM;
+ memset(ai, 0, sizeof(static_ai));
+
+ ai->ai_family = AF_INET6;
+ ai->ai_addrlen = sizeof(static_addr6);
+
+ struct sockaddr_in6* addr6 = &static_addr6;
+ memset(addr6, 0, sizeof(static_addr6));
+ addr6->sin6_port = htons(80);
+ addr6->sin6_family = ai->ai_family;
+ memcpy(&addr6->sin6_addr, bytes, sizeof(bytes));
+
+ ai->ai_addr = (sockaddr*)addr6;
+ return ai;
+}
+
} // anonymous namespace
TEST(NetUtilTest, FileURLConversion) {
@@ -736,6 +788,8 @@ TEST(NetUtilTest, GetDirectoryListingEntry) {
}
}
+#endif
+
TEST(NetUtilTest, GetHostAndPort) {
const struct {
const char* input;
@@ -781,4 +835,37 @@ TEST(NetUtilTest, GetHostAndPort) {
}
}
-#endif
+TEST(NetUtilTest, NetAddressToString_IPv4) {
+ const struct {
+ uint8 addr[4];
+ const char* result;
+ } tests[] = {
+ {{0, 0, 0, 0}, "0.0.0.0"},
+ {{127, 0, 0, 1}, "127.0.0.1"},
+ {{192, 168, 0, 1}, "192.168.0.1"},
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
+ const addrinfo* ai = GetIPv4Address(tests[i].addr);
+ std::string result = net::NetAddressToString(ai);
+ EXPECT_EQ(std::string(tests[i].result), result);
+ }
+}
+
+// TODO(eroman): On windows this is failing with WSAEINVAL (10022).
+TEST(NetUtilTest, DISABLED_NetAddressToString_IPv6) {
+ const struct {
+ uint8 addr[16];
+ const char* result;
+ } tests[] = {
+ {{0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA,
+ 0x98, 0x76, 0x54, 0x32, 0x10},
+ "fedc:ba98:7654:3210:fedc:ba98:7654:3210"},
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
+ const addrinfo* ai = GetIPv6Address(tests[i].addr);
+ std::string result = net::NetAddressToString(ai);
+ EXPECT_EQ(std::string(tests[i].result), result);
+ }
+}