Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Unified Diff: net/base/ip_address.cc

Issue 1408803010: Add IPAddress class as a replacement for the IPAddressNumber typedef. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove duplicated implementations Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/base/ip_address.cc
diff --git a/net/base/ip_address.cc b/net/base/ip_address.cc
new file mode 100644
index 0000000000000000000000000000000000000000..99078b89ca47397a2d761fecbc71719a9058e019
--- /dev/null
+++ b/net/base/ip_address.cc
@@ -0,0 +1,67 @@
+// Copyright (c) 2015 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/ip_address.h"
+
+#include "net/base/ip_address_number.h"
+#include "url/gurl.h"
+#include "url/url_canon_ip.h"
+
+namespace net {
+
+const size_t IPAddress::kIPv4AddressSize = 4;
+const size_t IPAddress::kIPv6AddressSize = 16;
+
+IPAddress::IPAddress() {}
+
+IPAddress::~IPAddress() {}
+
+IPAddress::IPAddress(const uint8_t* address, size_t address_len)
+ : ip_address_(address, address + address_len) {}
+
+bool IPAddress::IsIPv4() const {
+ return ip_address_.size() == kIPv4AddressSize;
+}
+
+bool IPAddress::IsIPv6() const {
+ return ip_address_.size() == kIPv6AddressSize;
+}
+
+bool IPAddress::IsReserved() const {
+ return IsIPAddressReserved(ip_address_);
+}
+
+bool IPAddress::IsIPv4Mapped() const {
+ return net::IsIPv4Mapped(ip_address_);
+}
+
+std::string IPAddress::ToString() const {
+ return IPAddressToString(ip_address_);
+}
+
+// static
+bool IPAddress::FromIPLiteral(const base::StringPiece& ip_literal,
+ IPAddress* ip_address) {
+ std::vector<uint8_t> number;
+ if (!ParseIPLiteralToNumber(ip_literal, &number))
+ return false;
+
+ std::swap(number, ip_address->ip_address_);
+ return true;
+}
+
+bool IPAddress::operator==(const IPAddress& that) const {
+ return ip_address_ == that.ip_address_;
+}
+
+bool IPAddress::operator<(const IPAddress& that) const {
+ // Sort IPv4 before IPv6.
+ if (ip_address_.size() != that.ip_address_.size()) {
+ return ip_address_.size() < that.ip_address_.size();
+ }
+
+ return ip_address_ < that.ip_address_;
+}
+
+} // namespace net
« net/base/ip_address.h ('K') | « net/base/ip_address.h ('k') | net/base/ip_address_number.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698