OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/ip_address_number.h" | 5 #include "net/base/ip_address_number.h" |
6 | 6 |
7 #include <limits.h> | 7 #include <limits.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 } | 95 } |
96 | 96 |
97 std::string IPAddressToString(const uint8_t* address, size_t address_len) { | 97 std::string IPAddressToString(const uint8_t* address, size_t address_len) { |
98 std::string str; | 98 std::string str; |
99 url::StdStringCanonOutput output(&str); | 99 url::StdStringCanonOutput output(&str); |
100 | 100 |
101 if (address_len == kIPv4AddressSize) { | 101 if (address_len == kIPv4AddressSize) { |
102 url::AppendIPv4Address(address, &output); | 102 url::AppendIPv4Address(address, &output); |
103 } else if (address_len == kIPv6AddressSize) { | 103 } else if (address_len == kIPv6AddressSize) { |
104 url::AppendIPv6Address(address, &output); | 104 url::AppendIPv6Address(address, &output); |
105 } else { | |
106 CHECK(false) << "Invalid IP address with length: " << address_len; | |
107 } | 105 } |
martijnc
2016/02/17 21:46:55
Also simplified this. The else clause wasn't neces
| |
108 | 106 |
109 output.Complete(); | 107 output.Complete(); |
110 return str; | 108 return str; |
111 } | 109 } |
112 | 110 |
113 std::string IPAddressToStringWithPort(const uint8_t* address, | 111 std::string IPAddressToStringWithPort(const uint8_t* address, |
114 size_t address_len, | 112 size_t address_len, |
115 uint16_t port) { | 113 uint16_t port) { |
116 std::string address_str = IPAddressToString(address, address_len); | 114 std::string address_str = IPAddressToString(address, address_len); |
115 if (address_str.empty()) | |
116 return address_str; | |
117 | 117 |
118 if (address_len == kIPv6AddressSize) { | 118 if (address_len == kIPv6AddressSize) { |
119 // Need to bracket IPv6 addresses since they contain colons. | 119 // Need to bracket IPv6 addresses since they contain colons. |
120 return base::StringPrintf("[%s]:%d", address_str.c_str(), port); | 120 return base::StringPrintf("[%s]:%d", address_str.c_str(), port); |
121 } | 121 } |
122 return base::StringPrintf("%s:%d", address_str.c_str(), port); | 122 return base::StringPrintf("%s:%d", address_str.c_str(), port); |
123 } | 123 } |
124 | 124 |
125 std::string IPAddressToString(const IPAddressNumber& addr) { | 125 std::string IPAddressToString(const IPAddressNumber& addr) { |
126 return IPAddressToString(&addr.front(), addr.size()); | 126 return IPAddressToString(addr.data(), addr.size()); |
127 } | 127 } |
128 | 128 |
129 std::string IPAddressToStringWithPort(const IPAddressNumber& addr, | 129 std::string IPAddressToStringWithPort(const IPAddressNumber& addr, |
130 uint16_t port) { | 130 uint16_t port) { |
131 return IPAddressToStringWithPort(&addr.front(), addr.size(), port); | 131 return IPAddressToStringWithPort(addr.data(), addr.size(), port); |
132 } | 132 } |
133 | 133 |
134 std::string IPAddressToPackedString(const IPAddressNumber& addr) { | 134 std::string IPAddressToPackedString(const IPAddressNumber& addr) { |
135 return std::string(reinterpret_cast<const char *>(&addr.front()), | 135 return std::string(reinterpret_cast<const char*>(addr.data()), addr.size()); |
136 addr.size()); | |
137 } | 136 } |
138 | 137 |
139 bool ParseURLHostnameToNumber(const std::string& hostname, | 138 bool ParseURLHostnameToNumber(const std::string& hostname, |
140 IPAddressNumber* ip_number) { | 139 IPAddressNumber* ip_number) { |
141 // |hostname| is an already canoncalized hostname, conforming to RFC 3986. | 140 // |hostname| is an already canoncalized hostname, conforming to RFC 3986. |
142 // For an IP address, this is defined in Section 3.2.2 of RFC 3986, with | 141 // For an IP address, this is defined in Section 3.2.2 of RFC 3986, with |
143 // the canonical form for IPv6 addresses defined in Section 4 of RFC 5952. | 142 // the canonical form for IPv6 addresses defined in Section 4 of RFC 5952. |
144 url::Component host_comp(0, hostname.size()); | 143 url::Component host_comp(0, hostname.size()); |
145 | 144 |
146 // If it has a bracket, try parsing it as an IPv6 address. | 145 // If it has a bracket, try parsing it as an IPv6 address. |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
290 } | 289 } |
291 return a1.size() * CHAR_BIT; | 290 return a1.size() * CHAR_BIT; |
292 } | 291 } |
293 | 292 |
294 unsigned MaskPrefixLength(const IPAddressNumber& mask) { | 293 unsigned MaskPrefixLength(const IPAddressNumber& mask) { |
295 IPAddressNumber all_ones(mask.size(), 0xFF); | 294 IPAddressNumber all_ones(mask.size(), 0xFF); |
296 return CommonPrefixLength(mask, all_ones); | 295 return CommonPrefixLength(mask, all_ones); |
297 } | 296 } |
298 | 297 |
299 } // namespace net | 298 } // namespace net |
OLD | NEW |