// Copyright 2015 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifdef UNSAFE_BUFFERS_BUILD // TODO(crbug.com/40285824): Remove this and convert code to safer constructs. #pragma allow_unsafe_buffers #endif // This is a copy of net/base/ip_address.h circa 2023. It should be used only by // components/feedback/redaction_tool/. // We need a copy because the components/feedback/redaction_tool source code is // shared into ChromeOS and needs to have no dependencies outside of base/. #ifndef COMPONENTS_FEEDBACK_REDACTION_TOOL_IP_ADDRESS_H_ #define COMPONENTS_FEEDBACK_REDACTION_TOOL_IP_ADDRESS_H_ #include <stddef.h> #include <stdint.h> #include <algorithm> #include <array> #include <string> #include <string_view> #include <vector> #include "base/check_op.h" #include "base/values.h" namespace redaction_internal { // Helper class to represent the sequence of bytes in an IP address. // A vector<uint8_t> would be simpler but incurs heap allocation, so // IPAddressBytes uses a fixed size array. class IPAddressBytes { … }; class IPAddress { … }; IPAddressList; // Returns the canonical string representation of an IP address along with its // port. For example: "192.168.0.1:99" or "[::1]:80". std::string IPAddressToStringWithPort(const IPAddress& address, uint16_t port); // Returns the address as a sequence of bytes in network-byte-order. std::string IPAddressToPackedString(const IPAddress& address); // Converts an IPv4 address to an IPv4-mapped IPv6 address. // For example 192.168.0.1 would be converted to ::ffff:192.168.0.1. IPAddress ConvertIPv4ToIPv4MappedIPv6(const IPAddress& address); // Converts an IPv4-mapped IPv6 address to IPv4 address. Should only be called // on IPv4-mapped IPv6 addresses. IPAddress ConvertIPv4MappedIPv6ToIPv4(const IPAddress& address); // 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 IPAddressMatchesPrefix(const IPAddress& ip_address, const IPAddress& ip_prefix, size_t prefix_length_in_bits); // Checks whether |address| starts with |prefix|. This provides similar // functionality as IPAddressMatchesPrefix() but doesn't perform automatic IPv4 // to IPv4MappedIPv6 conversions and only checks against full bytes. template <size_t N> bool IPAddressStartsWith(const IPAddress& address, const uint8_t (&prefix)[N]) { … } // According to RFC6052 Section 2.2 IPv4-Embedded IPv6 Address Format. // https://www.rfc-editor.org/rfc/rfc6052#section-2.2 // +--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ // |PL| 0-------------32--40--48--56--64--72--80--88--96--104---------| // +--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ // |32| prefix |v4(32) | u | suffix | // +--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ // |40| prefix |v4(24) | u |(8)| suffix | // +--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ // |48| prefix |v4(16) | u | (16) | suffix | // +--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ // |56| prefix |(8)| u | v4(24) | suffix | // +--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ // |64| prefix | u | v4(32) | suffix | // +--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ // |96| prefix | v4(32) | // +--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ // // The NAT64/DNS64 translation prefixes has one of the following lengths. enum class Dns64PrefixLength { … }; // Extracts the NAT64 translation prefix from the IPv6 address using the well // known address ipv4only.arpa 192.0.0.170 and 192.0.0.171. // Returns prefix length on success, or Dns64PrefixLength::kInvalid on failure // (when the ipv4only.arpa IPv4 address is not found) Dns64PrefixLength ExtractPref64FromIpv4onlyArpaAAAA(const IPAddress& address); // Converts an IPv4 address to an IPv4-embedded IPv6 address using the given // prefix. For example 192.168.0.1 and 64:ff9b::/96 would be converted to // 64:ff9b::192.168.0.1 // Returns converted IPv6 address when prefix_length is not // Dns64PrefixLength::kInvalid, and returns the original IPv4 address when // prefix_length is Dns64PrefixLength::kInvalid. IPAddress ConvertIPv4ToIPv4EmbeddedIPv6(const IPAddress& ipv4_address, const IPAddress& ipv6_address, Dns64PrefixLength prefix_length); } // namespace redaction_internal #endif // COMPONENTS_FEEDBACK_REDACTION_TOOL_IP_ADDRESS_H_