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

Side by Side 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: Comments eroman 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 unified diff | Download patch
« no previous file with comments | « net/base/ip_address.h ('k') | net/base/ip_address_number.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/base/ip_address.h"
6
7 #include "net/base/ip_address_number.h"
8 #include "url/gurl.h"
9 #include "url/url_canon_ip.h"
10
11 namespace net {
12
13 namespace {
14 const unsigned char kIPv4MappedPrefix[] = {0, 0, 0, 0, 0, 0,
eroman 2015/12/01 23:00:39 Can remove this per the later comment.
martijnc 2015/12/02 21:28:07 Done.
15 0, 0, 0, 0, 0xFF, 0xFF};
16 }
17
18 const size_t IPAddress::kIPv4AddressSize = 4;
19 const size_t IPAddress::kIPv6AddressSize = 16;
20
21 IPAddress::IPAddress() {}
22
23 IPAddress::~IPAddress() {}
24
25 IPAddress::IPAddress(const uint8_t* address, size_t address_len)
26 : ip_address_(std::vector<uint8_t>(address, address + address_len)) {}
eroman 2015/12/01 23:00:40 std::vector<> is not needed here. How about just:
martijnc 2015/12/02 21:28:06 Done.
27
28 bool IPAddress::IsIPv4() const {
29 return ip_address_.size() == kIPv4AddressSize;
30 }
31
32 bool IPAddress::IsIPv6() const {
33 return ip_address_.size() == kIPv6AddressSize;
34 }
35
36 // Don't compare IPv4 and IPv6 addresses (they have different range
eroman 2015/12/01 23:00:39 Remove this comment (it is duplicated from ip_addr
martijnc 2015/12/02 21:28:07 Done.
37 // reservations). Keep separate reservation arrays for each IP type, and
38 // consolidate adjacent reserved ranges within a reservation array when
39 // possible.
40 // Sources for info:
41 // www.iana.org/assignments/ipv4-address-space/ipv4-address-space.xhtml
42 // www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml
43 // They're formatted here with the prefix as the last element. For example:
44 // 10.0.0.0/8 becomes 10,0,0,0,8 and fec0::/10 becomes 0xfe,0xc0,0,0,0...,10.
45 bool IPAddress::IsReserved() const {
46 return IsIPAddressReserved(ip_address_);
47 }
48
49 bool IPAddress::IsIPv4Mapped() const {
eroman 2015/12/01 23:00:40 Sorry I should have been more explicit about this
martijnc 2015/12/02 21:28:07 Done.
50 if (!IsIPv6())
51 return false;
52 return std::equal(ip_address_.begin(),
53 ip_address_.begin() + arraysize(kIPv4MappedPrefix),
54 kIPv4MappedPrefix);
55 }
56
57 std::string IPAddress::ToString() const {
eroman 2015/12/01 23:00:40 Same thing here: return IPAddressToString(ip_ad
martijnc 2015/12/02 21:28:07 Done.
58 std::string str;
59 url::StdStringCanonOutput output(&str);
60
61 if (IsIPv4()) {
62 url::AppendIPv4Address(&ip_address_.front(), &output);
63 } else if (IsIPv6()) {
64 url::AppendIPv6Address(&ip_address_.front(), &output);
65 } else {
66 CHECK(false) << "Invalid IP address with length: " << ip_address_.size();
67 }
68
69 output.Complete();
70 return str;
71 }
72
73 // static
74 bool IPAddress::FromIPLiteral(const base::StringPiece& ip_literal,
eroman 2015/12/01 23:00:40 Same here: std::vector<uint8_t> number; if (!Pars
martijnc 2015/12/02 21:28:07 Done.
75 IPAddress* ip_address) {
76 std::vector<unsigned char> ip;
77
78 // |ip_literal| could be either a IPv4 or an IPv6 literal. If it contains
79 // a colon however, it must be an IPv6 address.
80 if (ip_literal.find(':') != base::StringPiece::npos) {
81 // GURL expects IPv6 hostnames to be surrounded with brackets.
82 std::string host_brackets = "[";
83 ip_literal.AppendToString(&host_brackets);
84 host_brackets.push_back(']');
85 url::Component host_comp(0, host_brackets.size());
86
87 // Try parsing the hostname as an IPv6 literal.
88 ip.resize(16); // 128 bits.
89 if (!url::IPv6AddressToNumber(host_brackets.data(), host_comp, &(ip)[0]))
90 return false;
91 std::swap(ip, ip_address->ip_address_);
92 return true;
93 }
94
95 // Otherwise the string is an IPv4 address.
96 ip.resize(4); // 32 bits.
97 url::Component host_comp(0, ip_literal.size());
98 int num_components;
99 url::CanonHostInfo::Family family = url::IPv4AddressToNumber(
100 ip_literal.data(), host_comp, &(ip)[0], &num_components);
101 if (family != url::CanonHostInfo::IPV4)
102 return false;
103 std::swap(ip, ip_address->ip_address_);
104 return true;
105 }
106
107 bool IPAddress::operator==(const IPAddress& that) const {
108 return ip_address_ == that.ip_address_;
109 }
110
111 bool IPAddress::operator<(const IPAddress& that) const {
112 // Sort IPv4 before IPv6.
113 if (ip_address_.size() != that.ip_address_.size()) {
114 return ip_address_.size() < that.ip_address_.size();
115 }
116
117 return ip_address_ < that.ip_address_;
118 }
119
120 } // namespace net
OLDNEW
« no previous file with comments | « 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