| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef NET_BASE_IP_ADDRESS_NET_H_ | 5 #ifndef NET_BASE_IP_ADDRESS_NET_H_ |
| 6 #define NET_BASE_IP_ADDRESS_NET_H_ | 6 #define NET_BASE_IP_ADDRESS_NET_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 // IP address whose |prefix_length_in_bits| most significant bits match | 142 // IP address whose |prefix_length_in_bits| most significant bits match |
| 143 // |ip_prefix| will be matched. | 143 // |ip_prefix| will be matched. |
| 144 // | 144 // |
| 145 // In cases when an IPv4 address is being compared to an IPv6 address prefix | 145 // In cases when an IPv4 address is being compared to an IPv6 address prefix |
| 146 // and vice versa, the IPv4 addresses will be converted to IPv4-mapped | 146 // and vice versa, the IPv4 addresses will be converted to IPv4-mapped |
| 147 // (IPv6) addresses. | 147 // (IPv6) addresses. |
| 148 NET_EXPORT bool IPAddressMatchesPrefix(const IPAddress& ip_address, | 148 NET_EXPORT bool IPAddressMatchesPrefix(const IPAddress& ip_address, |
| 149 const IPAddress& ip_prefix, | 149 const IPAddress& ip_prefix, |
| 150 size_t prefix_length_in_bits); | 150 size_t prefix_length_in_bits); |
| 151 | 151 |
| 152 NET_EXPORT bool ParseCIDRBlock(const std::string& cidr_literal, |
| 153 IPAddress& ip_address, |
| 154 size_t& prefix_length_in_bits); |
| 155 |
| 152 // Returns number of matching initial bits between the addresses |a1| and |a2|. | 156 // Returns number of matching initial bits between the addresses |a1| and |a2|. |
| 153 unsigned CommonPrefixLength(const IPAddress& a1, const IPAddress& a2); | 157 unsigned CommonPrefixLength(const IPAddress& a1, const IPAddress& a2); |
| 154 | 158 |
| 155 // Computes the number of leading 1-bits in |mask|. | 159 // Computes the number of leading 1-bits in |mask|. |
| 156 unsigned MaskPrefixLength(const IPAddress& mask); | 160 unsigned MaskPrefixLength(const IPAddress& mask); |
| 157 | 161 |
| 158 // Checks whether |address| starts with |prefix|. This provides similar | 162 // Checks whether |address| starts with |prefix|. This provides similar |
| 159 // functionality as IPAddressMatchesPrefix() but doesn't perform automatic IPv4 | 163 // functionality as IPAddressMatchesPrefix() but doesn't perform automatic IPv4 |
| 160 // to IPv4MappedIPv6 conversions and only checks against full bytes. | 164 // to IPv4MappedIPv6 conversions and only checks against full bytes. |
| 161 template <size_t N> | 165 template <size_t N> |
| 162 bool IPAddressStartsWith(const IPAddress& address, const uint8_t (&prefix)[N]) { | 166 bool IPAddressStartsWith(const IPAddress& address, const uint8_t (&prefix)[N]) { |
| 163 if (address.size() < N) | 167 if (address.size() < N) |
| 164 return false; | 168 return false; |
| 165 return std::equal(prefix, prefix + N, address.bytes().begin()); | 169 return std::equal(prefix, prefix + N, address.bytes().begin()); |
| 166 } | 170 } |
| 167 | 171 |
| 168 } // namespace net | 172 } // namespace net |
| 169 | 173 |
| 170 #endif // NET_BASE_IP_ADDRESS_NET_H_ | 174 #endif // NET_BASE_IP_ADDRESS_NET_H_ |
| OLD | NEW |