// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef NET_BASE_NET_UTIL_H_ #define NET_BASE_NET_UTIL_H_ #pragma once #if defined(OS_WIN) #include #include #elif defined(OS_POSIX) #include #endif #include #include #include #include #include namespace net { // IPAddressNumber is used to represent an IP address's numeric value as an // array of bytes, from most significant to least significant. This is the // network byte ordering. // // IPv4 addresses will have length 4, whereas IPv6 address will have length 16. typedef std::vector IPAddressNumber; typedef std::vector IPAddressList; // Parses an IP address literal (either IPv4 or IPv6) to its numeric value. // Returns true on success and fills |ip_number| with the numeric value. bool ParseIPLiteralToNumber(const std::string& ip_literal, IPAddressNumber* ip_number); // Parses an IP block specifier from CIDR notation to an // (IP address, prefix length) pair. Returns true on success and fills // |*ip_number| with the numeric value of the IP address and sets // |*prefix_length_in_bits| with the length of the prefix. // // CIDR notation literals can use either IPv4 or IPv6 literals. Some examples: // // 10.10.3.1/20 // a:b:c::/46 // ::1/128 bool ParseCIDRBlock(const std::string& cidr_literal, IPAddressNumber* ip_number, size_t* prefix_length_in_bits); // Compares an IP address to see if it falls within the specified IP block. // Returns true if it does, false otherwise. // // The IP block is given by (|ip_prefix|, |prefix_length_in_bits|) -- any // IP address whose |prefix_length_in_bits| most significant bits match // |ip_prefix| will be matched. // // In cases when an IPv4 address is being compared to an IPv6 address prefix // and vice versa, the IPv4 addresses will be converted to IPv4-mapped // (IPv6) addresses. bool IPNumberMatchesPrefix(const IPAddressNumber& ip_number, const IPAddressNumber& ip_prefix, size_t prefix_length_in_bits); } // namespace net #endif // NET_BASE_NET_UTIL_H_