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 // Parses an IP block specifier from CIDR notation to an |
| 153 // (IP address, prefix length) pair. Returns true on success and fills |
| 154 // |*ip_address| with the numeric value of the IP address and sets |
| 155 // |*prefix_length_in_bits| with the length of the prefix. |
| 156 // |
| 157 // CIDR notation literals can use either IPv4 or IPv6 literals. Some examples: |
| 158 // |
| 159 // 10.10.3.1/20 |
| 160 // a:b:c::/46 |
| 161 // ::1/128 |
| 162 NET_EXPORT bool ParseCIDRBlock(const std::string& cidr_literal, |
| 163 IPAddress* ip_address, |
| 164 size_t* prefix_length_in_bits); |
| 165 |
152 // Returns number of matching initial bits between the addresses |a1| and |a2|. | 166 // Returns number of matching initial bits between the addresses |a1| and |a2|. |
153 unsigned CommonPrefixLength(const IPAddress& a1, const IPAddress& a2); | 167 unsigned CommonPrefixLength(const IPAddress& a1, const IPAddress& a2); |
154 | 168 |
155 // Computes the number of leading 1-bits in |mask|. | 169 // Computes the number of leading 1-bits in |mask|. |
156 unsigned MaskPrefixLength(const IPAddress& mask); | 170 unsigned MaskPrefixLength(const IPAddress& mask); |
157 | 171 |
158 // Checks whether |address| starts with |prefix|. This provides similar | 172 // Checks whether |address| starts with |prefix|. This provides similar |
159 // functionality as IPAddressMatchesPrefix() but doesn't perform automatic IPv4 | 173 // functionality as IPAddressMatchesPrefix() but doesn't perform automatic IPv4 |
160 // to IPv4MappedIPv6 conversions and only checks against full bytes. | 174 // to IPv4MappedIPv6 conversions and only checks against full bytes. |
161 template <size_t N> | 175 template <size_t N> |
162 bool IPAddressStartsWith(const IPAddress& address, const uint8_t (&prefix)[N]) { | 176 bool IPAddressStartsWith(const IPAddress& address, const uint8_t (&prefix)[N]) { |
163 if (address.size() < N) | 177 if (address.size() < N) |
164 return false; | 178 return false; |
165 return std::equal(prefix, prefix + N, address.bytes().begin()); | 179 return std::equal(prefix, prefix + N, address.bytes().begin()); |
166 } | 180 } |
167 | 181 |
168 } // namespace net | 182 } // namespace net |
169 | 183 |
170 #endif // NET_BASE_IP_ADDRESS_NET_H_ | 184 #endif // NET_BASE_IP_ADDRESS_NET_H_ |
OLD | NEW |