From 9eb7b11b614e987d7608678d0396873f37ca817e Mon Sep 17 00:00:00 2001 From: "wez@chromium.org" Date: Wed, 28 Mar 2012 20:19:31 +0000 Subject: 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 --- net/dns/dns_config_service_posix_unittest.cc | 6 ++---- net/dns/dns_query.cc | 10 +++++----- net/dns/dns_response.cc | 10 +++++----- net/dns/dns_test_util.cc | 4 ++-- net/dns/dns_transaction_unittest.cc | 4 ++-- 5 files changed, 16 insertions(+), 18 deletions(-) (limited to 'net/dns') 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(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(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(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(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(buffer)->ancount = htons(1); + reinterpret_cast(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(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 - -- cgit v1.1