summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorwez@chromium.org <wez@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-28 20:19:31 +0000
committerwez@chromium.org <wez@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-28 20:19:31 +0000
commit9eb7b11b614e987d7608678d0396873f37ca817e (patch)
tree7f222f4ae73bd3c3edc33d39f3bf7050d10f4254 /net
parent97f37d558f60a245190b17ce14d3d9039fc83767 (diff)
downloadchromium_src-9eb7b11b614e987d7608678d0396873f37ca817e.zip
chromium_src-9eb7b11b614e987d7608678d0396873f37ca817e.tar.gz
chromium_src-9eb7b11b614e987d7608678d0396873f37ca817e.tar.bz2
Add base::HostToNetXX() & NetToHostXX(), and use them to replace htonX() & ntohX() in Chrome.
This primarily addresses issues with code using the OS-provided htonX() & ntohX() functions from within the Chrome sandbox. Under Windows these functions are provided by ws2_32.dll, which is no longer available within Chrome's sandbox. The new base::HostToNetXX() and NetToHostXX() functions are safe for use by sandboxed code on Windows, and provide a single place where future fixes for other platforms can be made. BUG=117252 Review URL: http://codereview.chromium.org/9716020 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@129476 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r--net/base/host_resolver_proc.cc6
-rw-r--r--net/base/ip_endpoint.cc11
-rw-r--r--net/base/listen_socket.cc5
-rw-r--r--net/base/listen_socket_unittest.cc5
-rw-r--r--net/base/net_util.cc14
-rw-r--r--net/base/net_util_unittest.cc5
-rw-r--r--net/dns/dns_config_service_posix_unittest.cc6
-rw-r--r--net/dns/dns_query.cc10
-rw-r--r--net/dns/dns_response.cc10
-rw-r--r--net/dns/dns_test_util.cc4
-rw-r--r--net/dns/dns_transaction_unittest.cc4
-rw-r--r--net/server/web_socket.cc4
-rw-r--r--net/socket/socks5_client_socket.cc5
-rw-r--r--net/socket/socks5_client_socket_unittest.cc3
-rw-r--r--net/socket/socks_client_socket.cc5
-rw-r--r--net/socket/web_socket_server_socket.cc4
16 files changed, 50 insertions, 51 deletions
diff --git a/net/base/host_resolver_proc.cc b/net/base/host_resolver_proc.cc
index e88fa2c..c2f2ddd 100644
--- a/net/base/host_resolver_proc.cc
+++ b/net/base/host_resolver_proc.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -7,6 +7,7 @@
#include "build/build_config.h"
#include "base/logging.h"
+#include "base/sys_byteorder.h"
#include "net/base/address_list.h"
#include "net/base/dns_reloader.h"
#include "net/base/net_errors.h"
@@ -28,7 +29,8 @@ bool IsAllLocalhostOfOneFamily(const struct addrinfo* ai) {
case AF_INET: {
const struct sockaddr_in* addr_in =
reinterpret_cast<struct sockaddr_in*>(ai->ai_addr);
- if ((ntohl(addr_in->sin_addr.s_addr) & 0xff000000) == 0x7f000000)
+ if ((base::NetToHost32(addr_in->sin_addr.s_addr) & 0xff000000) ==
+ 0x7f000000)
saw_v4_localhost = true;
else
return false;
diff --git a/net/base/ip_endpoint.cc b/net/base/ip_endpoint.cc
index 2578ded..e962d91 100644
--- a/net/base/ip_endpoint.cc
+++ b/net/base/ip_endpoint.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -6,6 +6,7 @@
#include "base/logging.h"
#include "base/string_number_conversions.h"
+#include "base/sys_byteorder.h"
#if defined(OS_WIN)
#include <winsock2.h>
#elif defined(OS_POSIX)
@@ -54,7 +55,7 @@ bool IPEndPoint::ToSockAddr(struct sockaddr* address,
struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*>(address);
memset(addr, 0, sizeof(struct sockaddr_in));
addr->sin_family = AF_INET;
- addr->sin_port = htons(port_);
+ addr->sin_port = base::HostToNet16(port_);
memcpy(&addr->sin_addr, &address_[0], kIPv4AddressSize);
break;
}
@@ -66,7 +67,7 @@ bool IPEndPoint::ToSockAddr(struct sockaddr* address,
reinterpret_cast<struct sockaddr_in6*>(address);
memset(addr6, 0, sizeof(struct sockaddr_in6));
addr6->sin6_family = AF_INET6;
- addr6->sin6_port = htons(port_);
+ addr6->sin6_port = base::HostToNet16(port_);
memcpy(&addr6->sin6_addr, &address_[0], kIPv6AddressSize);
break;
}
@@ -87,7 +88,7 @@ bool IPEndPoint::FromSockAddr(const struct sockaddr* address,
return false;
const struct sockaddr_in* addr =
reinterpret_cast<const struct sockaddr_in*>(address);
- port_ = ntohs(addr->sin_port);
+ port_ = base::NetToHost16(addr->sin_port);
const char* bytes = reinterpret_cast<const char*>(&addr->sin_addr);
address_.assign(&bytes[0], &bytes[kIPv4AddressSize]);
break;
@@ -97,7 +98,7 @@ bool IPEndPoint::FromSockAddr(const struct sockaddr* address,
return false;
const struct sockaddr_in6* addr =
reinterpret_cast<const struct sockaddr_in6*>(address);
- port_ = ntohs(addr->sin6_port);
+ port_ = base::NetToHost16(addr->sin6_port);
const char* bytes = reinterpret_cast<const char*>(&addr->sin6_addr);
address_.assign(&bytes[0], &bytes[kIPv6AddressSize]);
break;
diff --git a/net/base/listen_socket.cc b/net/base/listen_socket.cc
index 92bfcf8..c600594 100644
--- a/net/base/listen_socket.cc
+++ b/net/base/listen_socket.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -18,6 +18,7 @@
#endif
#include "base/eintr_wrapper.h"
+#include "base/sys_byteorder.h"
#include "base/threading/platform_thread.h"
#include "net/base/net_util.h"
#include "net/base/listen_socket.h"
@@ -116,7 +117,7 @@ SOCKET ListenSocket::Listen(std::string ip, int port) {
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(ip.c_str());
- addr.sin_port = htons(port);
+ addr.sin_port = base::HostToNet16(port);
if (bind(s, reinterpret_cast<sockaddr*>(&addr), sizeof(addr))) {
#if defined(OS_WIN)
closesocket(s);
diff --git a/net/base/listen_socket_unittest.cc b/net/base/listen_socket_unittest.cc
index d3a7eb5..5e3811e 100644
--- a/net/base/listen_socket_unittest.cc
+++ b/net/base/listen_socket_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -9,6 +9,7 @@
#include "base/bind.h"
#include "base/eintr_wrapper.h"
+#include "base/sys_byteorder.h"
#include "net/base/net_util.h"
#include "testing/platform_test.h"
@@ -50,7 +51,7 @@ void ListenSocketTester::SetUp() {
struct sockaddr_in client;
client.sin_family = AF_INET;
client.sin_addr.s_addr = inet_addr(kLoopback);
- client.sin_port = htons(kTestPort);
+ client.sin_port = base::HostToNet16(kTestPort);
int ret = HANDLE_EINTR(
connect(test_socket_, reinterpret_cast<sockaddr*>(&client),
sizeof(client)));
diff --git a/net/base/net_util.cc b/net/base/net_util.cc
index 21fb07f..29b83a8 100644
--- a/net/base/net_util.cc
+++ b/net/base/net_util.cc
@@ -76,14 +76,6 @@
using base::Time;
-#if defined(OS_WIN)
-// Allow htons/ntohs to be called without requiring ws2_32.dll to be loaded,
-// which isn't available in Chrome's sandbox. See crbug.com/116591.
-// TODO(wez): Replace these calls with base::htons() etc when available.
-#define ntohs(x) _byteswap_ushort(x)
-#define htons(x) _byteswap_ushort(x)
-#endif // OS_WIN
-
namespace net {
namespace {
@@ -2344,7 +2336,7 @@ uint16 GetPortFromAddrinfo(const struct addrinfo* info) {
const uint16* port_field = GetPortFieldFromAddrinfo(info);
if (!port_field)
return -1;
- return ntohs(*port_field);
+ return base::NetToHost16(*port_field);
}
const uint16* GetPortFieldFromSockaddr(const struct sockaddr* address,
@@ -2369,7 +2361,7 @@ int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) {
const uint16* port_field = GetPortFieldFromSockaddr(address, address_len);
if (!port_field)
return -1;
- return ntohs(*port_field);
+ return base::NetToHost16(*port_field);
}
// Assign |port| to each address in the linked list starting from |head|.
@@ -2378,7 +2370,7 @@ void SetPortForAllAddrinfos(struct addrinfo* head, uint16 port) {
for (struct addrinfo* ai = head; ai; ai = ai->ai_next) {
uint16* port_field = GetPortFieldFromAddrinfo(ai);
if (port_field)
- *port_field = htons(port);
+ *port_field = base::HostToNet16(port);
}
}
diff --git a/net/base/net_util_unittest.cc b/net/base/net_util_unittest.cc
index 98d79a7..caf64b5 100644
--- a/net/base/net_util_unittest.cc
+++ b/net/base/net_util_unittest.cc
@@ -13,6 +13,7 @@
#include "base/string_number_conversions.h"
#include "base/string_util.h"
#include "base/stringprintf.h"
+#include "base/sys_byteorder.h"
#include "base/sys_string_conversions.h"
#include "base/test/test_file_util.h"
#include "base/time.h"
@@ -420,7 +421,7 @@ const struct addrinfo* GetIPv4Address(const uint8* bytes, int port) {
struct sockaddr_in* addr4 = &static_addr4;
memset(addr4, 0, sizeof(static_addr4));
- addr4->sin_port = htons(port);
+ addr4->sin_port = base::HostToNet16(port);
addr4->sin_family = ai->ai_family;
memcpy(&addr4->sin_addr, bytes, 4);
@@ -444,7 +445,7 @@ const struct addrinfo* GetIPv6Address(const uint8* bytes, int port) {
struct sockaddr_in6* addr6 = &static_addr6;
memset(addr6, 0, sizeof(static_addr6));
- addr6->sin6_port = htons(port);
+ addr6->sin6_port = base::HostToNet16(port);
addr6->sin6_family = ai->ai_family;
memcpy(&addr6->sin6_addr, bytes, 16);
diff --git a/net/dns/dns_config_service_posix_unittest.cc b/net/dns/dns_config_service_posix_unittest.cc
index e4ae0d8..a0408d3c 100644
--- a/net/dns/dns_config_service_posix_unittest.cc
+++ b/net/dns/dns_config_service_posix_unittest.cc
@@ -73,7 +73,7 @@ void InitializeResState(res_state res, unsigned generation) {
for (int i = 0; i < 3; ++i) {
struct sockaddr_in sa;
sa.sin_family = AF_INET;
- sa.sin_port = htons(NS_DEFAULTPORT + i - generation);
+ sa.sin_port = base::HostToNet16(NS_DEFAULTPORT + i - generation);
inet_pton(AF_INET, ip4addr[i], &sa.sin_addr);
res->nsaddr_list[i] = sa;
}
@@ -90,7 +90,7 @@ void InitializeResState(res_state res, unsigned generation) {
struct sockaddr_in6 *sa6;
sa6 = (struct sockaddr_in6 *)malloc(sizeof(*sa6));
sa6->sin6_family = AF_INET6;
- sa6->sin6_port = htons(NS_DEFAULTPORT - i);
+ sa6->sin6_port = base::HostToNet16(NS_DEFAULTPORT - i);
inet_pton(AF_INET6, ip6addr[i], &sa6->sin6_addr);
res->_u._ext.nsaddrs[i] = sa6;
}
@@ -127,5 +127,3 @@ TEST(DnsConfigServicePosixTest, ConvertResStateToDnsConfig) {
} // namespace
} // namespace net
-
-
diff --git a/net/dns/dns_query.cc b/net/dns/dns_query.cc
index 45d7c16..72e97cf 100644
--- a/net/dns/dns_query.cc
+++ b/net/dns/dns_query.cc
@@ -28,9 +28,9 @@ DnsQuery::DnsQuery(uint16 id, const base::StringPiece& qname, uint16 qtype)
dns_protocol::Header* header =
reinterpret_cast<dns_protocol::Header*>(io_buffer_->data());
memset(header, 0, sizeof(dns_protocol::Header));
- header->id = htons(id);
- header->flags = htons(dns_protocol::kFlagRD);
- header->qdcount = htons(1);
+ header->id = base::HostToNet16(id);
+ header->flags = base::HostToNet16(dns_protocol::kFlagRD);
+ header->qdcount = base::HostToNet16(1);
// Write question section after the header.
BigEndianWriter writer(reinterpret_cast<char*>(header + 1), question_size);
@@ -49,7 +49,7 @@ DnsQuery* DnsQuery::CloneWithNewId(uint16 id) const {
uint16 DnsQuery::id() const {
const dns_protocol::Header* header =
reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data());
- return ntohs(header->id);
+ return base::NetToHost16(header->id);
}
base::StringPiece DnsQuery::qname() const {
@@ -77,7 +77,7 @@ DnsQuery::DnsQuery(const DnsQuery& orig, uint16 id) {
io_buffer_.get()->size());
dns_protocol::Header* header =
reinterpret_cast<dns_protocol::Header*>(io_buffer_->data());
- header->id = htons(id);
+ header->id = base::HostToNet16(id);
}
} // namespace net
diff --git a/net/dns/dns_response.cc b/net/dns/dns_response.cc
index 725629b..4ad6465 100644
--- a/net/dns/dns_response.cc
+++ b/net/dns/dns_response.cc
@@ -149,11 +149,11 @@ bool DnsResponse::InitParse(int nbytes, const DnsQuery& query) {
return false;
// Match the query id.
- if (ntohs(header()->id) != query.id())
+ if (base::NetToHost16(header()->id) != query.id())
return false;
// Match question count.
- if (ntohs(header()->qdcount) != 1)
+ if (base::NetToHost16(header()->qdcount) != 1)
return false;
// Match the question section.
@@ -177,17 +177,17 @@ bool DnsResponse::IsValid() const {
uint16 DnsResponse::flags() const {
DCHECK(parser_.IsValid());
- return ntohs(header()->flags) & ~(dns_protocol::kRcodeMask);
+ return base::NetToHost16(header()->flags) & ~(dns_protocol::kRcodeMask);
}
uint8 DnsResponse::rcode() const {
DCHECK(parser_.IsValid());
- return ntohs(header()->flags) & dns_protocol::kRcodeMask;
+ return base::NetToHost16(header()->flags) & dns_protocol::kRcodeMask;
}
unsigned DnsResponse::answer_count() const {
DCHECK(parser_.IsValid());
- return ntohs(header()->ancount);
+ return base::NetToHost16(header()->ancount);
}
base::StringPiece DnsResponse::qname() const {
diff --git a/net/dns/dns_test_util.cc b/net/dns/dns_test_util.cc
index a3c26cf..0cbdf94 100644
--- a/net/dns/dns_test_util.cc
+++ b/net/dns/dns_test_util.cc
@@ -86,7 +86,8 @@ class MockTransaction : public DnsTransaction,
size_t answer_size = 12 + rdata_size;
// Write answer with loopback IP address.
- reinterpret_cast<dns_protocol::Header*>(buffer)->ancount = htons(1);
+ reinterpret_cast<dns_protocol::Header*>(buffer)->ancount =
+ base::HostToNet16(1);
BigEndianWriter writer(buffer + nbytes, answer_size);
writer.WriteU16(kPointerToQueryName);
writer.WriteU16(qtype_);
@@ -167,4 +168,3 @@ void MockDnsConfigService::Watch(const CallbackType& callback) {
}
} // namespace net
-
diff --git a/net/dns/dns_transaction_unittest.cc b/net/dns/dns_transaction_unittest.cc
index ded54be..e30fa31 100644
--- a/net/dns/dns_transaction_unittest.cc
+++ b/net/dns/dns_transaction_unittest.cc
@@ -8,6 +8,7 @@
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "base/rand_util.h"
+#include "base/sys_byteorder.h"
#include "base/test/test_timeouts.h"
#include "net/base/big_endian.h"
#include "net/base/dns_util.h"
@@ -279,7 +280,7 @@ class DnsTransactionTest : public testing::Test {
0);
dns_protocol::Header* header =
reinterpret_cast<dns_protocol::Header*>(response->io_buffer()->data());
- header->flags |= htons(dns_protocol::kFlagResponse | rcode);
+ header->flags |= base::HostToNet16(dns_protocol::kFlagResponse | rcode);
responses_.push_back(response);
writes_.push_back(MockWrite(ASYNC,
@@ -715,4 +716,3 @@ TEST_F(DnsTransactionTest, SuffixSearchStop) {
} // namespace
} // namespace net
-
diff --git a/net/server/web_socket.cc b/net/server/web_socket.cc
index 0abbcf7..774c4d6 100644
--- a/net/server/web_socket.cc
+++ b/net/server/web_socket.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -37,7 +37,7 @@ static uint32 WebSocketKeyFingerprint(const std::string& str) {
int64 number = 0;
if (!base::StringToInt64(result, &number))
return 0;
- return htonl(static_cast<uint32>(number / spaces));
+ return base::HostToNet32(static_cast<uint32>(number / spaces));
}
class WebSocketHixie76 : public net::WebSocket {
diff --git a/net/socket/socks5_client_socket.cc b/net/socket/socks5_client_socket.cc
index b8b3439..2c9d62a 100644
--- a/net/socket/socks5_client_socket.cc
+++ b/net/socket/socks5_client_socket.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -9,6 +9,7 @@
#include "base/debug/trace_event.h"
#include "base/format_macros.h"
#include "base/string_util.h"
+#include "base/sys_byteorder.h"
#include "net/base/io_buffer.h"
#include "net/base/net_log.h"
#include "net/base/net_util.h"
@@ -364,7 +365,7 @@ int SOCKS5ClientSocket::BuildHandshakeWriteBuffer(std::string* handshake)
host_request_info_.hostname().size()));
handshake->append(host_request_info_.hostname());
- uint16 nw_port = htons(host_request_info_.port());
+ uint16 nw_port = base::HostToNet16(host_request_info_.port());
handshake->append(reinterpret_cast<char*>(&nw_port), sizeof(nw_port));
return OK;
}
diff --git a/net/socket/socks5_client_socket_unittest.cc b/net/socket/socks5_client_socket_unittest.cc
index bf6277d..c63d7805 100644
--- a/net/socket/socks5_client_socket_unittest.cc
+++ b/net/socket/socks5_client_socket_unittest.cc
@@ -7,6 +7,7 @@
#include <algorithm>
#include <map>
+#include "base/sys_byteorder.h"
#include "net/base/address_list.h"
#include "net/base/net_log.h"
#include "net/base/net_log_unittest.h"
@@ -56,7 +57,7 @@ class SOCKS5ClientSocketTest : public PlatformTest {
};
SOCKS5ClientSocketTest::SOCKS5ClientSocketTest()
- : kNwPort(htons(80)),
+ : kNwPort(base::HostToNet16(80)),
net_log_(CapturingNetLog::kUnbounded),
host_resolver_(new MockHostResolver) {
}
diff --git a/net/socket/socks_client_socket.cc b/net/socket/socks_client_socket.cc
index 4c368c1..517508d 100644
--- a/net/socket/socks_client_socket.cc
+++ b/net/socket/socks_client_socket.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -7,6 +7,7 @@
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/compiler_specific.h"
+#include "base/sys_byteorder.h"
#include "net/base/io_buffer.h"
#include "net/base/net_log.h"
#include "net/base/net_util.h"
@@ -297,7 +298,7 @@ const std::string SOCKSClientSocket::BuildHandshakeWriteBuffer() const {
SOCKS4ServerRequest request;
request.version = kSOCKSVersion4;
request.command = kSOCKSStreamRequest;
- request.nw_port = htons(host_request_info_.port());
+ request.nw_port = base::HostToNet16(host_request_info_.port());
const struct addrinfo* ai = addresses_.head();
DCHECK(ai);
diff --git a/net/socket/web_socket_server_socket.cc b/net/socket/web_socket_server_socket.cc
index c316415..e8cb886 100644
--- a/net/socket/web_socket_server_socket.cc
+++ b/net/socket/web_socket_server_socket.cc
@@ -689,8 +689,8 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket {
}
char challenge[4 + 4 + sizeof(key3)];
- int32 part1 = htonl(key_number1 / spaces1);
- int32 part2 = htonl(key_number2 / spaces2);
+ int32 part1 = base::HostToNet32(key_number1 / spaces1);
+ int32 part2 = base::HostToNet32(key_number2 / spaces2);
memcpy(challenge, &part1, 4);
memcpy(challenge + 4, &part2, 4);
memcpy(challenge + 4 + 4, key3, sizeof(key3));