diff options
31 files changed, 175 insertions, 122 deletions
diff --git a/base/sys_byteorder.h b/base/sys_byteorder.h index 9fcdbba..80bb831 100644 --- a/base/sys_byteorder.h +++ b/base/sys_byteorder.h @@ -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,6 +37,28 @@ namespace base { // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness. +inline uint16 ByteSwap(uint16 x) { +#if defined(COMPILER_MSVC) + return _byteswap_ushort(x); +#elif defined(OS_MACOSX) + return OSSwapInt16(x); +#elif defined(OS_OPENBSD) + return swap16(x); +#else + return bswap_16(x); +#endif +} +inline uint32 ByteSwap(uint32 x) { +#if defined(COMPILER_MSVC) + return _byteswap_ulong(x); +#elif defined(OS_MACOSX) + return OSSwapInt32(x); +#elif defined(OS_OPENBSD) + return swap32(x); +#else + return bswap_32(x); +#endif +} inline uint64 ByteSwap(uint64 x) { #if defined(COMPILER_MSVC) return _byteswap_uint64(x); @@ -51,7 +73,21 @@ inline uint64 ByteSwap(uint64 x) { // Converts the bytes in |x| from network to host order (endianness), and // returns the result. -inline uint64 ntohll(uint64 x) { +inline uint16 NetToHost16(uint16 x) { +#if defined(ARCH_CPU_LITTLE_ENDIAN) + return ByteSwap(x); +#else + return x; +#endif +} +inline uint32 NetToHost32(uint32 x) { +#if defined(ARCH_CPU_LITTLE_ENDIAN) + return ByteSwap(x); +#else + return x; +#endif +} +inline uint64 NetToHost64(uint64 x) { #if defined(ARCH_CPU_LITTLE_ENDIAN) return ByteSwap(x); #else @@ -61,7 +97,21 @@ inline uint64 ntohll(uint64 x) { // Converts the bytes in |x| from host to network order (endianness), and // returns the result. -inline uint64 htonll(uint64 x) { +inline uint16 HostToNet16(uint16 x) { +#if defined(ARCH_CPU_LITTLE_ENDIAN) + return ByteSwap(x); +#else + return x; +#endif +} +inline uint32 HostToNet32(uint32 x) { +#if defined(ARCH_CPU_LITTLE_ENDIAN) + return ByteSwap(x); +#else + return x; +#endif +} +inline uint64 HostToNet64(uint64 x) { #if defined(ARCH_CPU_LITTLE_ENDIAN) return ByteSwap(x); #else diff --git a/chrome/browser/chromeos/web_socket_proxy.cc b/chrome/browser/chromeos/web_socket_proxy.cc index 78236b6..fc4e50d 100644 --- a/chrome/browser/chromeos/web_socket_proxy.cc +++ b/chrome/browser/chromeos/web_socket_proxy.cc @@ -841,8 +841,9 @@ void Serv::Run() { struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; - addr.sin_port = htons(0); // let OS allocatate ephemeral port number. - addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + // Let the OS allocate a port number. + addr.sin_port = base::HostToNet16(0); + addr.sin_addr.s_addr = base::HostToNet32(INADDR_LOOPBACK); if (bind(listening_sock_, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr))) { @@ -883,8 +884,8 @@ void Serv::Run() { const int kPort = 10101; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; - addr.sin_port = htons(kPort); - addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + addr.sin_port = base::HostToNet16(kPort); + addr.sin_addr.s_addr = base::HostToNet32(INADDR_LOOPBACK); if (bind(extra_listening_sock_, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr))) { @@ -934,7 +935,7 @@ void Serv::Run() { } BrowserThread::PostTask( BrowserThread::UI, FROM_HERE, - base::Bind(&SendNotification, ntohs(addr.sin_port))); + base::Bind(&SendNotification, base::NetToHost16(addr.sin_port))); LOG(INFO) << "WebSocketProxy: Starting event dispatch loop."; event_base_dispatch(evbase_); @@ -1574,7 +1575,7 @@ void Conn::OnPrimchanRead(struct bufferevent* bev, EventKey evkey) { { struct sockaddr_in sa; memset(&sa, 0, sizeof(sa)); - sa.sin_port = htons(cs->destport_); + sa.sin_port = base::HostToNet16(cs->destport_); if (inet_pton(sa.sin_family = AF_INET, cs->destaddr_.c_str(), &sa.sin_addr) == 1) { @@ -1595,7 +1596,7 @@ void Conn::OnPrimchanRead(struct bufferevent* bev, EventKey evkey) { } struct sockaddr_in6 sa; memset(&sa, 0, sizeof(sa)); - sa.sin6_port = htons(cs->destport_); + sa.sin6_port = base::HostToNet16(cs->destport_); if (inet_pton(sa.sin6_family = AF_INET6, cs->destaddr_.c_str(), &sa.sin6_addr) == 1) { @@ -1754,7 +1755,7 @@ void Conn::OnDestResolutionIPv4(int result, char type, struct sockaddr_in sa; memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; - sa.sin_port = htons(cs->destport_); + sa.sin_port = base::HostToNet16(cs->destport_); DCHECK(sizeof(sa.sin_addr) == sizeof(struct in_addr)); memcpy(&sa.sin_addr, static_cast<struct in_addr*>(addr_list) + i, @@ -1785,7 +1786,7 @@ void Conn::OnDestResolutionIPv6(int result, char type, struct sockaddr_in6 sa; memset(&sa, 0, sizeof(sa)); sa.sin6_family = AF_INET6; - sa.sin6_port = htons(cs->destport_); + sa.sin6_port = base::HostToNet16(cs->destport_); DCHECK(sizeof(sa.sin6_addr) == sizeof(struct in6_addr)); memcpy(&sa.sin6_addr, static_cast<struct in6_addr*>(addr_list) + i, diff --git a/chrome/browser/safe_browsing/protocol_parser.cc b/chrome/browser/safe_browsing/protocol_parser.cc index e9ca15a..ba207be 100644 --- a/chrome/browser/safe_browsing/protocol_parser.cc +++ b/chrome/browser/safe_browsing/protocol_parser.cc @@ -452,7 +452,7 @@ bool SafeBrowsingProtocolParser::ReadChunkId( memcpy(chunk_id, *data, sizeof(*chunk_id)); *data += sizeof(*chunk_id); *remaining -= sizeof(*chunk_id); - *chunk_id = htonl(*chunk_id); + *chunk_id = base::HostToNet32(*chunk_id); DCHECK_GE(*remaining, 0); return true; } diff --git a/chrome/common/metrics/metrics_log_base.cc b/chrome/common/metrics/metrics_log_base.cc index fc0c768..09d1a84 100644 --- a/chrome/common/metrics/metrics_log_base.cc +++ b/chrome/common/metrics/metrics_log_base.cc @@ -43,7 +43,7 @@ inline uint64 HashToUInt64(const std::string& hash) { uint64 value; DCHECK_GE(hash.size(), sizeof(value)); memcpy(&value, hash.data(), sizeof(value)); - return base::htonll(value); + return base::HostToNet64(value); } // Creates an MD5 hash of the given value, and returns hash as a byte buffer diff --git a/content/browser/renderer_host/p2p/socket_host.cc b/content/browser/renderer_host/p2p/socket_host.cc index 0f07127..16c0917 100644 --- a/content/browser/renderer_host/p2p/socket_host.cc +++ b/content/browser/renderer_host/p2p/socket_host.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. @@ -34,15 +34,15 @@ bool P2PSocketHost::GetStunPacketType( if (data_size < kStunHeaderSize) return false; - uint32 cookie = ntohl(*reinterpret_cast<const uint32*>(data + 4)); + uint32 cookie = base::NetToHost32(*reinterpret_cast<const uint32*>(data + 4)); if (cookie != kStunMagicCookie) return false; - uint16 length = ntohs(*reinterpret_cast<const uint16*>(data + 2)); + uint16 length = base::NetToHost16(*reinterpret_cast<const uint16*>(data + 2)); if (length != data_size - kStunHeaderSize) return false; - int message_type = ntohs(*reinterpret_cast<const uint16*>(data)); + int message_type = base::NetToHost16(*reinterpret_cast<const uint16*>(data)); // Verify that the type is known: switch (message_type) { diff --git a/content/browser/renderer_host/p2p/socket_host_tcp.cc b/content/browser/renderer_host/p2p/socket_host_tcp.cc index 6bbe2d1..35307d4 100644 --- a/content/browser/renderer_host/p2p/socket_host_tcp.cc +++ b/content/browser/renderer_host/p2p/socket_host_tcp.cc @@ -168,8 +168,8 @@ void P2PSocketHostTcp::DidCompleteRead(int result) { read_buffer_->set_offset(read_buffer_->offset() + result); if (read_buffer_->offset() > kPacketHeaderSize) { - int packet_size = - ntohs(*reinterpret_cast<uint16*>(read_buffer_->StartOfBuffer())); + int packet_size = base::NetToHost16( + *reinterpret_cast<uint16*>(read_buffer_->StartOfBuffer())); if (packet_size + kPacketHeaderSize <= read_buffer_->offset()) { // We've got a full packet! char* start = read_buffer_->StartOfBuffer() + kPacketHeaderSize; @@ -221,7 +221,8 @@ void P2PSocketHostTcp::Send(const net::IPEndPoint& to, int size = kPacketHeaderSize + data.size(); write_buffer_ = new net::DrainableIOBuffer(new net::IOBuffer(size), size); - *reinterpret_cast<uint16*>(write_buffer_->data()) = htons(data.size()); + *reinterpret_cast<uint16*>(write_buffer_->data()) = + base::HostToNet16(data.size()); memcpy(write_buffer_->data() + kPacketHeaderSize, &data[0], data.size()); DoWrite(); diff --git a/content/browser/renderer_host/p2p/socket_host_tcp_unittest.cc b/content/browser/renderer_host/p2p/socket_host_tcp_unittest.cc index 254b47b..d515dad 100644 --- a/content/browser/renderer_host/p2p/socket_host_tcp_unittest.cc +++ b/content/browser/renderer_host/p2p/socket_host_tcp_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. @@ -42,7 +42,7 @@ class P2PSocketHostTcpTest : public testing::Test { std::string IntToSize(int size) { std::string result; - uint16 size16 = htons(size); + uint16 size16 = base::HostToNet16(size); result.resize(sizeof(size16)); memcpy(&result[0], &size16, sizeof(size16)); return result; diff --git a/content/browser/renderer_host/p2p/socket_host_test_utils.h b/content/browser/renderer_host/p2p/socket_host_test_utils.h index 6c3113b..cd00637 100644 --- a/content/browser/renderer_host/p2p/socket_host_test_utils.h +++ b/content/browser/renderer_host/p2p/socket_host_test_utils.h @@ -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. @@ -232,10 +232,11 @@ void CreateRandomPacket(std::vector<char>* packet) { void CreateStunPacket(std::vector<char>* packet, uint16 type) { CreateRandomPacket(packet); - *reinterpret_cast<uint16*>(&*packet->begin()) = htons(type); + *reinterpret_cast<uint16*>(&*packet->begin()) = base::HostToNet16(type); *reinterpret_cast<uint16*>(&*packet->begin() + 2) = - htons(packet->size() - kStunHeaderSize); - *reinterpret_cast<uint32*>(&*packet->begin() + 4) = htonl(kStunMagicCookie); + base::HostToNet16(packet->size() - kStunHeaderSize); + *reinterpret_cast<uint32*>(&*packet->begin() + 4) = + base::HostToNet32(kStunMagicCookie); } void CreateStunRequest(std::vector<char>* packet) { diff --git a/crypto/encryptor.cc b/crypto/encryptor.cc index 31a7cc8..a673f81 100644 --- a/crypto/encryptor.cc +++ b/crypto/encryptor.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. @@ -21,14 +21,14 @@ Encryptor::Counter::~Counter() { } bool Encryptor::Counter::Increment() { - uint64 low_num = base::ntohll(counter_.components64[1]); + uint64 low_num = base::NetToHost64(counter_.components64[1]); uint64 new_low_num = low_num + 1; - counter_.components64[1] = base::htonll(new_low_num); + counter_.components64[1] = base::HostToNet64(new_low_num); // If overflow occured then increment the most significant component. if (new_low_num < low_num) { counter_.components64[0] = - base::htonll(base::ntohll(counter_.components64[0]) + 1); + base::HostToNet64(base::NetToHost64(counter_.components64[0]) + 1); } // TODO(hclam): Return false if counter value overflows. diff --git a/crypto/p224.cc b/crypto/p224.cc index d0e4baf..575b51f 100644 --- a/crypto/p224.cc +++ b/crypto/p224.cc @@ -13,16 +13,11 @@ #include "base/sys_byteorder.h" -#if defined(OS_WIN) -// Allow htonl/ntohl 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::htonl() etc when available. -#define ntohl(x) _byteswap_ulong(x) -#define htonl(x) _byteswap_ulong(x) -#endif // OS_WIN - namespace { +using base::HostToNet32; +using base::NetToHost32; + // Field element functions. // // The field that we're dealing with is ℤ/pℤ where p = 2**224 - 2**96 + 1. @@ -564,27 +559,33 @@ void ScalarMult(Point* out, const Point& a, // Get224Bits reads 7 words from in and scatters their contents in // little-endian form into 8 words at out, 28 bits per output word. void Get224Bits(uint32* out, const uint32* in) { - out[0] = ntohl(in[6]) & kBottom28Bits; - out[1] = ((ntohl(in[5]) << 4) | (ntohl(in[6]) >> 28)) & kBottom28Bits; - out[2] = ((ntohl(in[4]) << 8) | (ntohl(in[5]) >> 24)) & kBottom28Bits; - out[3] = ((ntohl(in[3]) << 12) | (ntohl(in[4]) >> 20)) & kBottom28Bits; - out[4] = ((ntohl(in[2]) << 16) | (ntohl(in[3]) >> 16)) & kBottom28Bits; - out[5] = ((ntohl(in[1]) << 20) | (ntohl(in[2]) >> 12)) & kBottom28Bits; - out[6] = ((ntohl(in[0]) << 24) | (ntohl(in[1]) >> 8)) & kBottom28Bits; - out[7] = (ntohl(in[0]) >> 4) & kBottom28Bits; + out[0] = NetToHost32(in[6]) & kBottom28Bits; + out[1] = ((NetToHost32(in[5]) << 4) | + (NetToHost32(in[6]) >> 28)) & kBottom28Bits; + out[2] = ((NetToHost32(in[4]) << 8) | + (NetToHost32(in[5]) >> 24)) & kBottom28Bits; + out[3] = ((NetToHost32(in[3]) << 12) | + (NetToHost32(in[4]) >> 20)) & kBottom28Bits; + out[4] = ((NetToHost32(in[2]) << 16) | + (NetToHost32(in[3]) >> 16)) & kBottom28Bits; + out[5] = ((NetToHost32(in[1]) << 20) | + (NetToHost32(in[2]) >> 12)) & kBottom28Bits; + out[6] = ((NetToHost32(in[0]) << 24) | + (NetToHost32(in[1]) >> 8)) & kBottom28Bits; + out[7] = (NetToHost32(in[0]) >> 4) & kBottom28Bits; } // Put224Bits performs the inverse operation to Get224Bits: taking 28 bits from // each of 8 input words and writing them in big-endian order to 7 words at // out. void Put224Bits(uint32* out, const uint32* in) { - out[6] = htonl((in[0] >> 0) | (in[1] << 28)); - out[5] = htonl((in[1] >> 4) | (in[2] << 24)); - out[4] = htonl((in[2] >> 8) | (in[3] << 20)); - out[3] = htonl((in[3] >> 12) | (in[4] << 16)); - out[2] = htonl((in[4] >> 16) | (in[5] << 12)); - out[1] = htonl((in[5] >> 20) | (in[6] << 8)); - out[0] = htonl((in[6] >> 24) | (in[7] << 4)); + out[6] = HostToNet32((in[0] >> 0) | (in[1] << 28)); + out[5] = HostToNet32((in[1] >> 4) | (in[2] << 24)); + out[4] = HostToNet32((in[2] >> 8) | (in[3] << 20)); + out[3] = HostToNet32((in[3] >> 12) | (in[4] << 16)); + out[2] = HostToNet32((in[4] >> 16) | (in[5] << 12)); + out[1] = HostToNet32((in[5] >> 20) | (in[6] << 8)); + out[0] = HostToNet32((in[6] >> 24) | (in[7] << 4)); } } // anonymous namespace diff --git a/crypto/symmetric_key_win.cc b/crypto/symmetric_key_win.cc index 87e0bc3..f4cd751 100644 --- a/crypto/symmetric_key_win.cc +++ b/crypto/symmetric_key_win.cc @@ -4,12 +4,11 @@ #include "crypto/symmetric_key.h" -#include <winsock2.h> // For htonl. - #include <vector> // TODO(wtc): replace scoped_array by std::vector. #include "base/memory/scoped_ptr.h" +#include "base/sys_byteorder.h" namespace crypto { @@ -264,7 +263,7 @@ bool ComputePBKDF2Block(HCRYPTHASH hash, return false; // Iteration U_1: and append (big-endian) INT (i). - uint32 big_endian_block_index = htonl(block_index); + uint32 big_endian_block_index = base::HostToNet32(block_index); ok = CryptHashData(safe_hash, reinterpret_cast<BYTE*>(&big_endian_block_index), sizeof(big_endian_block_index), 0); diff --git a/jingle/notifier/base/chrome_async_socket_unittest.cc b/jingle/notifier/base/chrome_async_socket_unittest.cc index 08dbf301..a64e03f 100644 --- a/jingle/notifier/base/chrome_async_socket_unittest.cc +++ b/jingle/notifier/base/chrome_async_socket_unittest.cc @@ -101,7 +101,7 @@ class AsyncSocketDataProvider : public net::SocketDataProvider { // Takes a 32-bit integer in host byte order and converts it to a // net::IPAddressNumber. net::IPAddressNumber Uint32ToIPAddressNumber(uint32 ip) { - uint32 ip_nbo = htonl(ip); + uint32 ip_nbo = base::HostToNet32(ip); const unsigned char* const ip_start = reinterpret_cast<const unsigned char*>(&ip_nbo); return net::IPAddressNumber(ip_start, ip_start + (sizeof ip_nbo)); diff --git a/native_client_sdk/src/build_tools/debug_server/debug_stub/transport_common.cc b/native_client_sdk/src/build_tools/debug_server/debug_stub/transport_common.cc index 4d442f0..7043d5c 100644 --- a/native_client_sdk/src/build_tools/debug_server/debug_stub/transport_common.cc +++ b/native_client_sdk/src/build_tools/debug_server/debug_stub/transport_common.cc @@ -291,4 +291,3 @@ void ITransport::Free(ITransport* itrans) { } } // namespace port - 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)); diff --git a/ppapi/shared_impl/private/net_address_private_impl.cc b/ppapi/shared_impl/private/net_address_private_impl.cc index 06875cb..148d5e5 100644 --- a/ppapi/shared_impl/private/net_address_private_impl.cc +++ b/ppapi/shared_impl/private/net_address_private_impl.cc @@ -72,11 +72,11 @@ uint16_t GetPort(const PP_NetAddress_Private* addr) { switch (GetFamilyInternal(addr)) { case AF_INET: { const sockaddr_in* a = reinterpret_cast<const sockaddr_in*>(addr->data); - return ntohs(a->sin_port); + return base::NetToHost16(a->sin_port); } case AF_INET6: { const sockaddr_in6* a = reinterpret_cast<const sockaddr_in6*>(addr->data); - return ntohs(a->sin6_port); + return base::NetToHost16(a->sin6_port); } default: return 0; @@ -181,8 +181,8 @@ PP_Bool AreEqual(const PP_NetAddress_Private* addr1, #if defined(OS_WIN) || defined(OS_MACOSX) std::string ConvertIPv4AddressToString(const sockaddr_in* a, bool include_port) { - unsigned ip = ntohl(a->sin_addr.s_addr); - unsigned port = ntohs(a->sin_port); + unsigned ip = base::NetToHost32(a->sin_addr.s_addr); + unsigned port = base::NetToHost16(a->sin_port); std::string description = base::StringPrintf( "%u.%u.%u.%u", (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff); @@ -204,7 +204,7 @@ std::string ConvertIPv4AddressToString(const sockaddr_in* a, // 5952, but consistent with |getnameinfo()|. std::string ConvertIPv6AddressToString(const sockaddr_in6* a, bool include_port) { - unsigned port = ntohs(a->sin6_port); + unsigned port = base::NetToHost16(a->sin6_port); unsigned scope = a->sin6_scope_id; std::string description(include_port ? "[" : ""); @@ -229,7 +229,7 @@ std::string ConvertIPv6AddressToString(const sockaddr_in6* a, int curr_start = 0; int curr_length = 0; for (int i = 0; i < 8; i++) { - if (ntohs(a->sin6_addr.s6_addr16[i]) != 0) { + if (base::NetToHost16(a->sin6_addr.s6_addr16[i]) != 0) { curr_length = 0; } else { if (!curr_length) @@ -249,7 +249,7 @@ std::string ConvertIPv6AddressToString(const sockaddr_in6* a, need_sep = false; i += longest_length; } else { - unsigned v = ntohs(a->sin6_addr.s6_addr16[i]); + unsigned v = base::NetToHost16(a->sin6_addr.s6_addr16[i]); base::StringAppendF(&description, need_sep ? ":%x" : "%x", v); need_sep = true; i++; @@ -315,12 +315,14 @@ PP_Bool ReplacePort(const struct PP_NetAddress_Private* src_addr, switch (GetFamilyInternal(src_addr)) { case AF_INET: { memmove(dest_addr, src_addr, sizeof(*src_addr)); - reinterpret_cast<sockaddr_in*>(dest_addr->data)->sin_port = htons(port); + reinterpret_cast<sockaddr_in*>(dest_addr->data)->sin_port = + base::HostToNet16(port); return PP_TRUE; } case AF_INET6: { memmove(dest_addr, src_addr, sizeof(*src_addr)); - reinterpret_cast<sockaddr_in6*>(dest_addr->data)->sin6_port = htons(port); + reinterpret_cast<sockaddr_in6*>(dest_addr->data)->sin6_port = + base::HostToNet16(port); return PP_TRUE; } default: diff --git a/sync/util/nigori.cc b/sync/util/nigori.cc index 1b0e3c1..0b96b37 100644 --- a/sync/util/nigori.cc +++ b/sync/util/nigori.cc @@ -31,7 +31,7 @@ class NigoriStream { // Append the big-endian representation of the length of |value| with 32 bits, // followed by |value| itself to the stream. NigoriStream& operator<<(const std::string& value) { - uint32 size = htonl(value.size()); + uint32 size = base::HostToNet32(value.size()); stream_.write((char *) &size, sizeof(uint32)); stream_ << value; return *this; @@ -41,9 +41,9 @@ class NigoriStream { // followed by the big-endian representation of the value of |type|, with 32 // bits, to the stream. NigoriStream& operator<<(const Nigori::Type type) { - uint32 size = htonl(sizeof(uint32)); + uint32 size = base::HostToNet32(sizeof(uint32)); stream_.write((char *) &size, sizeof(uint32)); - uint32 value = htonl(type); + uint32 value = base::HostToNet32(type); stream_.write((char *) &value, sizeof(uint32)); return *this; } |