diff options
author | cbentzel@chromium.org <cbentzel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-07 13:19:10 +0000 |
---|---|---|
committer | cbentzel@chromium.org <cbentzel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-07 13:19:10 +0000 |
commit | 19bdc0036b036f4b33c8d81536a9848c6a95c9f7 (patch) | |
tree | f9803f03723dd6b218ed4a6fe08f3edc12572d5d /net/base | |
parent | ccf637be7edd3bdb14dc4157e6370e78c619a8f6 (diff) | |
download | chromium_src-19bdc0036b036f4b33c8d81536a9848c6a95c9f7.zip chromium_src-19bdc0036b036f4b33c8d81536a9848c6a95c9f7.tar.gz chromium_src-19bdc0036b036f4b33c8d81536a9848c6a95c9f7.tar.bz2 |
Revert 113282 - Isolates generic DnsClient from AsyncHostResolver.
There were a few memory issues including access of unadressable memory.
DnsClient provides a generic DNS client that allows fetching resource records.
DnsClient is very lightweight and does not support aggregation, queuing or
prioritization of requests.
This is the first CL in a series to merge AsyncHostResolver into
HostResolverImpl.
Also introduces general-purpose BigEndianReader/Writer.
BUG=90881
TEST=./net_unittests
Review URL: http://codereview.chromium.org/8762001
TBR=szym@chromium.org
Review URL: http://codereview.chromium.org/8835011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@113384 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-rw-r--r-- | net/base/big_endian.cc | 98 | ||||
-rw-r--r-- | net/base/big_endian.h | 107 | ||||
-rw-r--r-- | net/base/big_endian_unittest.cc | 100 | ||||
-rw-r--r-- | net/base/dns_util.cc | 17 | ||||
-rw-r--r-- | net/base/dns_util.h | 10 |
5 files changed, 11 insertions, 321 deletions
diff --git a/net/base/big_endian.cc b/net/base/big_endian.cc deleted file mode 100644 index 17acc59..0000000 --- a/net/base/big_endian.cc +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright (c) 2011 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. - -#include "net/base/big_endian.h" - -#include "base/string_piece.h" - -namespace net { - -BigEndianReader::BigEndianReader(const void* buf, size_t len) - : ptr_(reinterpret_cast<const char*>(buf)), end_(ptr_ + len) {} - -bool BigEndianReader::Skip(size_t len) { - if (ptr_ + len > end_) - return false; - ptr_ += len; - return true; -} - -bool BigEndianReader::ReadBytes(void* out, size_t len) { - if (ptr_ + len > end_) - return false; - memcpy(out, ptr_, len); - ptr_ += len; - return true; -} - -bool BigEndianReader::ReadPiece(base::StringPiece* out, size_t len) { - if (ptr_ + len > end_) - return false; - *out = base::StringPiece(ptr_, len); - ptr_ += len; - return true; -} - -template<typename T> -bool BigEndianReader::Read(T* value) { - if (ptr_ + sizeof(T) > end_) - return false; - ReadBigEndian<T>(ptr_, value); - ptr_ += sizeof(T); - return true; -} - -bool BigEndianReader::ReadU8(uint8* value) { - return Read(value); -} - -bool BigEndianReader::ReadU16(uint16* value) { - return Read(value); -} - -bool BigEndianReader::ReadU32(uint32* value) { - return Read(value); -} - -BigEndianWriter::BigEndianWriter(void* buf, size_t len) - : ptr_(reinterpret_cast<char*>(buf)), end_(ptr_ + len) {} - -bool BigEndianWriter::Skip(size_t len) { - if (ptr_ + len > end_) - return false; - ptr_ += len; - return true; -} - -bool BigEndianWriter::WriteBytes(const void* buf, size_t len) { - if (ptr_ + len > end_) - return false; - memcpy(ptr_, buf, len); - ptr_ += len; - return true; -} - -template<typename T> -bool BigEndianWriter::Write(T value) { - if (ptr_ + sizeof(T) > end_) - return false; - WriteBigEndian<T>(ptr_, value); - ptr_ += sizeof(T); - return true; -} - -bool BigEndianWriter::WriteU8(uint8 value) { - return Write(value); -} - -bool BigEndianWriter::WriteU16(uint16 value) { - return Write(value); -} - -bool BigEndianWriter::WriteU32(uint32 value) { - return Write(value); -} - -} // namespace net - diff --git a/net/base/big_endian.h b/net/base/big_endian.h deleted file mode 100644 index 0786f62..0000000 --- a/net/base/big_endian.h +++ /dev/null @@ -1,107 +0,0 @@ -// Copyright (c) 2011 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. - -#ifndef NET_BASE_BIG_ENDIAN_H_ -#define NET_BASE_BIG_ENDIAN_H_ -#pragma once - -#include "base/basictypes.h" -#include "net/base/net_export.h" - -namespace base { -class StringPiece; -} - -namespace net { - -// Read an integer (signed or unsigned) from |buf| in Big Endian order. -// Note: this loop is unrolled with -O1 and above. -// NOTE(szym): glibc dns-canon.c and SpdyFrameBuilder use -// ntohs(*(uint16_t*)ptr) which is potentially unaligned. -// This would cause SIGBUS on ARMv5 or earlier and ARMv6-M. -template<typename T> -inline void ReadBigEndian(const char buf[], T* out) { - *out = buf[0]; - for (size_t i = 1; i < sizeof(T); ++i) { - *out <<= 8; - // Must cast to uint8 to avoid clobbering by sign extension. - *out |= static_cast<uint8>(buf[i]); - } -} - -// Write an integer (signed or unsigned) |val| to |buf| in Big Endian order. -// Note: this loop is unrolled with -O1 and above. -template<typename T> -inline void WriteBigEndian(char buf[], T val) { - for (size_t i = 0; i < sizeof(T); ++i) { - buf[sizeof(T)-i-1] = static_cast<char>(val & 0xFF); - val >>= 8; - } -} - -// Specializations to make clang happy about the (dead code) shifts above. -template<> -inline void ReadBigEndian<uint8>(const char buf[], uint8* out) { - *out = buf[0]; -} - -template<> -inline void WriteBigEndian<uint8>(char buf[], uint8 val) { - buf[0] = static_cast<char>(val); -} - -// Allows reading integers in network order (big endian) while iterating over -// an underlying buffer. All the reading functions advance the internal pointer. -class NET_EXPORT BigEndianReader { - public: - BigEndianReader(const void* buf, size_t len); - - const char* ptr() const { return ptr_; } - int remaining() const { return end_ - ptr_; } - - bool Skip(size_t len); - bool ReadBytes(void* out, size_t len); - // Creates a StringPiece in |out| that points to the underlying buffer. - bool ReadPiece(base::StringPiece* out, size_t len); - bool ReadU8(uint8* value); - bool ReadU16(uint16* value); - bool ReadU32(uint32* value); - - private: - // Hidden to promote type safety. - template<typename T> - bool Read(T* v); - - const char* ptr_; - const char* end_; -}; - -// Allows writing integers in network order (big endian) while iterating over -// an underlying buffer. All the writing functions advance the internal pointer. -class NET_EXPORT BigEndianWriter { - public: - BigEndianWriter(void* buf, size_t len); - - char* ptr() const { return ptr_; } - int remaining() const { return end_ - ptr_; } - - bool Skip(size_t len); - bool WriteBytes(const void* buf, size_t len); - bool WriteU8(uint8 value); - bool WriteU16(uint16 value); - bool WriteU32(uint32 value); - - private: - // Hidden to promote type safety. - template<typename T> - bool Write(T v); - - char* ptr_; - char* end_; -}; - -} // namespace net - -#endif // NET_BASE_BIG_ENDIAN_H_ - diff --git a/net/base/big_endian_unittest.cc b/net/base/big_endian_unittest.cc deleted file mode 100644 index cb4a6d4..0000000 --- a/net/base/big_endian_unittest.cc +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright (c) 2011 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. - -#include "base/string_piece.h" -#include "net/base/big_endian.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace net { - -TEST(BigEndianReaderTest, ReadsValues) { - char data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC }; - char buf[2]; - uint8 u8; - uint16 u16; - uint32 u32; - base::StringPiece piece; - BigEndianReader reader(data, sizeof(data)); - - EXPECT_TRUE(reader.Skip(2)); - EXPECT_EQ(data + 2, reader.ptr()); - EXPECT_EQ(reader.remaining(), static_cast<int>(sizeof(data)) - 2); - EXPECT_TRUE(reader.ReadBytes(buf, sizeof(buf))); - EXPECT_EQ(0x2, buf[0]); - EXPECT_EQ(0x3, buf[1]); - EXPECT_TRUE(reader.ReadU8(&u8)); - EXPECT_EQ(0x4, u8); - EXPECT_TRUE(reader.ReadU16(&u16)); - EXPECT_EQ(0x0506, u16); - EXPECT_TRUE(reader.ReadU32(&u32)); - EXPECT_EQ(0x0708090Au, u32); - base::StringPiece expected(reader.ptr(), 2); - EXPECT_TRUE(reader.ReadPiece(&piece, 2)); - EXPECT_EQ(2u, piece.size()); - EXPECT_EQ(expected.data(), piece.data()); -} - -TEST(BigEndianReaderTest, RespectsLength) { - char data[4]; - char buf[2]; - uint8 u8; - uint16 u16; - uint32 u32; - base::StringPiece piece; - BigEndianReader reader(data, sizeof(data)); - // 4 left - EXPECT_FALSE(reader.Skip(6)); - EXPECT_TRUE(reader.Skip(1)); - // 3 left - EXPECT_FALSE(reader.ReadU32(&u32)); - EXPECT_FALSE(reader.ReadPiece(&piece, 4)); - EXPECT_TRUE(reader.Skip(2)); - // 1 left - EXPECT_FALSE(reader.ReadU16(&u16)); - EXPECT_FALSE(reader.ReadBytes(buf, 2)); - EXPECT_TRUE(reader.Skip(1)); - // 0 left - EXPECT_FALSE(reader.ReadU8(&u8)); - EXPECT_EQ(0, reader.remaining()); -} - -TEST(BigEndianWriterTest, WritesValues) { - char expected[] = { 0, 0, 2, 3, 4, 5, 6, 7, 8, 9, 0xA }; - char data[sizeof(expected)]; - char buf[] = { 0x2, 0x3 }; - memset(data, 0, sizeof(data)); - BigEndianWriter writer(data, sizeof(data)); - - EXPECT_TRUE(writer.Skip(2)); - EXPECT_TRUE(writer.WriteBytes(buf, sizeof(buf))); - EXPECT_TRUE(writer.WriteU8(0x4)); - EXPECT_TRUE(writer.WriteU16(0x0506)); - EXPECT_TRUE(writer.WriteU32(0x0708090A)); - EXPECT_EQ(0, memcmp(expected, data, sizeof(expected))); -} - -TEST(BigEndianWriterTest, RespectsLength) { - char data[4]; - char buf[2]; - uint8 u8 = 0; - uint16 u16 = 0; - uint32 u32 = 0; - BigEndianWriter writer(data, sizeof(data)); - // 4 left - EXPECT_FALSE(writer.Skip(6)); - EXPECT_TRUE(writer.Skip(1)); - // 3 left - EXPECT_FALSE(writer.WriteU32(u32)); - EXPECT_TRUE(writer.Skip(2)); - // 1 left - EXPECT_FALSE(writer.WriteU16(u16)); - EXPECT_FALSE(writer.WriteBytes(buf, 2)); - EXPECT_TRUE(writer.Skip(1)); - // 0 left - EXPECT_FALSE(writer.WriteU8(u8)); - EXPECT_EQ(0, writer.remaining()); -} - -} // namespace net - diff --git a/net/base/dns_util.cc b/net/base/dns_util.cc index a49ada8..93d789e 100644 --- a/net/base/dns_util.cc +++ b/net/base/dns_util.cc @@ -9,7 +9,7 @@ namespace net { // Based on DJB's public domain code. -bool DNSDomainFromDot(const base::StringPiece& dotted, std::string* out) { +bool DNSDomainFromDot(const std::string& dotted, std::string* out) { const char* buf = dotted.data(); unsigned n = dotted.size(); char label[63]; @@ -56,7 +56,7 @@ bool DNSDomainFromDot(const base::StringPiece& dotted, std::string* out) { return true; } -std::string DNSDomainToString(const base::StringPiece& domain) { +std::string DNSDomainToString(const std::string& domain) { std::string ret; for (unsigned i = 0; i < domain.size() && domain[i]; i += domain[i] + 1) { @@ -73,7 +73,7 @@ std::string DNSDomainToString(const base::StringPiece& domain) { if (static_cast<unsigned>(domain[i]) + i + 1 > domain.size()) return ""; - domain.substr(i + 1, domain[i]).AppendToString(&ret); + ret += domain.substr(i + 1, domain[i]); } return ret; } @@ -92,13 +92,12 @@ bool IsSTD3ASCIIValidCharacter(char c) { return true; } -std::string TrimEndingDot(const base::StringPiece& host) { - base::StringPiece host_trimmed = host; +std::string TrimEndingDot(const std::string& host) { + std::string host_trimmed = host; size_t len = host_trimmed.length(); - if (len > 1 && host_trimmed[len - 1] == '.') { - host_trimmed.remove_suffix(1); - } - return host_trimmed.as_string(); + if (len > 1 && host_trimmed[len - 1] == '.') + host_trimmed.erase(len - 1); + return host_trimmed; } bool DnsResponseBuffer::U8(uint8* v) { diff --git a/net/base/dns_util.h b/net/base/dns_util.h index c01b2a2..f09e906 100644 --- a/net/base/dns_util.h +++ b/net/base/dns_util.h @@ -19,22 +19,18 @@ namespace net { // // dotted: a string in dotted form: "www.google.com" // out: a result in DNS form: "\x03www\x06google\x03com\x00" -NET_EXPORT_PRIVATE bool DNSDomainFromDot(const base::StringPiece& dotted, +NET_EXPORT_PRIVATE bool DNSDomainFromDot(const std::string& dotted, std::string* out); // DNSDomainToString coverts a domain in DNS format to a dotted string. -NET_EXPORT_PRIVATE std::string DNSDomainToString( - const base::StringPiece& domain); +NET_EXPORT_PRIVATE std::string DNSDomainToString(const std::string& domain); // Returns true iff the given character is in the set of valid DNS label // characters as given in RFC 3490, 4.1, 3(a) NET_EXPORT_PRIVATE bool IsSTD3ASCIIValidCharacter(char c); // Returns the hostname by trimming the ending dot, if one exists. -NET_EXPORT std::string TrimEndingDot(const base::StringPiece& host); - -// TODO(szym): remove all definitions below once DnsRRResolver migrates to -// DnsClient +NET_EXPORT std::string TrimEndingDot(const std::string& host); // DNS class types. static const uint16 kClassIN = 1; |