| 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 #ifndef NET_BASE_IP_ADDRESS_NUMBER_H_ | 5 #ifndef NET_BASE_IP_ADDRESS_NUMBER_H_ |
| 6 #define NET_BASE_IP_ADDRESS_NUMBER_H_ | 6 #define NET_BASE_IP_ADDRESS_NUMBER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 // IPv4 addresses will have length 4, whereas IPv6 address will have length 16. | 23 // IPv4 addresses will have length 4, whereas IPv6 address will have length 16. |
| 24 // | 24 // |
| 25 // TODO(Martijnc): Remove the IPAddressNumber typedef. New code should use | 25 // TODO(Martijnc): Remove the IPAddressNumber typedef. New code should use |
| 26 // IPAddress instead and existing code should be switched over. | 26 // IPAddress instead and existing code should be switched over. |
| 27 // https://crbug.com/496258 | 27 // https://crbug.com/496258 |
| 28 typedef std::vector<unsigned char> IPAddressNumber; | 28 typedef std::vector<unsigned char> IPAddressNumber; |
| 29 | 29 |
| 30 static const size_t kIPv4AddressSize = 4; | 30 static const size_t kIPv4AddressSize = 4; |
| 31 static const size_t kIPv6AddressSize = 16; | 31 static const size_t kIPv6AddressSize = 16; |
| 32 | 32 |
| 33 // Returns true if an IP address hostname is in a range reserved by the IANA. | |
| 34 // Works with both IPv4 and IPv6 addresses, and only compares against a given | |
| 35 // protocols's reserved ranges. | |
| 36 NET_EXPORT bool IsIPAddressReserved(const IPAddressNumber& address); | |
| 37 | |
| 38 // Returns the string representation of an IP address. | 33 // Returns the string representation of an IP address. |
| 39 // For example: "192.168.0.1" or "::1". Returns the empty string when |address| | 34 // For example: "192.168.0.1" or "::1". Returns the empty string when |address| |
| 40 // is invalid. | 35 // is invalid. |
| 41 NET_EXPORT std::string IPAddressToString(const uint8_t* address, | 36 NET_EXPORT std::string IPAddressToString(const uint8_t* address, |
| 42 size_t address_len); | 37 size_t address_len); |
| 43 | 38 |
| 44 // Returns the string representation of an IP address along with its port. | 39 // Returns the string representation of an IP address along with its port. |
| 45 // For example: "192.168.0.1:99" or "[::1]:80". Returns the empty string when | 40 // For example: "192.168.0.1:99" or "[::1]:80". Returns the empty string when |
| 46 // |address| is invalid (the port will be ignored). | 41 // |address| is invalid (the port will be ignored). |
| 47 NET_EXPORT std::string IPAddressToStringWithPort(const uint8_t* address, | 42 NET_EXPORT std::string IPAddressToStringWithPort(const uint8_t* address, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 69 const IPAddressNumber& ipv4_number); | 64 const IPAddressNumber& ipv4_number); |
| 70 | 65 |
| 71 // Returns true iff |address| is an IPv4-mapped IPv6 address. | 66 // Returns true iff |address| is an IPv4-mapped IPv6 address. |
| 72 NET_EXPORT_PRIVATE bool IsIPv4Mapped(const IPAddressNumber& address); | 67 NET_EXPORT_PRIVATE bool IsIPv4Mapped(const IPAddressNumber& address); |
| 73 | 68 |
| 74 // Converts an IPv4-mapped IPv6 address to IPv4 address. Should only be called | 69 // Converts an IPv4-mapped IPv6 address to IPv4 address. Should only be called |
| 75 // on IPv4-mapped IPv6 addresses. | 70 // on IPv4-mapped IPv6 addresses. |
| 76 NET_EXPORT_PRIVATE IPAddressNumber ConvertIPv4MappedToIPv4( | 71 NET_EXPORT_PRIVATE IPAddressNumber ConvertIPv4MappedToIPv4( |
| 77 const IPAddressNumber& address); | 72 const IPAddressNumber& address); |
| 78 | 73 |
| 79 // Compares an IP address to see if it falls within the specified IP block. | |
| 80 // Returns true if it does, false otherwise. | |
| 81 // | |
| 82 // The IP block is given by (|ip_prefix|, |prefix_length_in_bits|) -- any | |
| 83 // IP address whose |prefix_length_in_bits| most significant bits match | |
| 84 // |ip_prefix| will be matched. | |
| 85 // | |
| 86 // In cases when an IPv4 address is being compared to an IPv6 address prefix | |
| 87 // and vice versa, the IPv4 addresses will be converted to IPv4-mapped | |
| 88 // (IPv6) addresses. | |
| 89 NET_EXPORT_PRIVATE bool IPNumberMatchesPrefix(const IPAddressNumber& ip_number, | |
| 90 const IPAddressNumber& ip_prefix, | |
| 91 size_t prefix_length_in_bits); | |
| 92 | |
| 93 // Returns number of matching initial bits between the addresses |a1| and |a2|. | 74 // Returns number of matching initial bits between the addresses |a1| and |a2|. |
| 94 unsigned CommonPrefixLength(const IPAddressNumber& a1, | 75 unsigned CommonPrefixLength(const IPAddressNumber& a1, |
| 95 const IPAddressNumber& a2); | 76 const IPAddressNumber& a2); |
| 96 | 77 |
| 97 // Computes the number of leading 1-bits in |mask|. | 78 // Computes the number of leading 1-bits in |mask|. |
| 98 unsigned MaskPrefixLength(const IPAddressNumber& mask); | 79 unsigned MaskPrefixLength(const IPAddressNumber& mask); |
| 99 | 80 |
| 100 } // namespace net | 81 } // namespace net |
| 101 | 82 |
| 102 #endif // NET_BASE_IP_ADDRESS_NUMBER_H_ | 83 #endif // NET_BASE_IP_ADDRESS_NUMBER_H_ |
| OLD | NEW |