xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/common/quiche_ip_address.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef QUICHE_COMMON_QUICHE_IP_ADDRESS_H_
6 #define QUICHE_COMMON_QUICHE_IP_ADDRESS_H_
7 
8 #include <cstdint>
9 #if defined(_WIN32)
10 #include <winsock2.h>
11 #include <ws2tcpip.h>
12 #else
13 #include <arpa/inet.h>
14 #include <netinet/in.h>
15 #include <sys/socket.h>
16 #include <sys/types.h>
17 #endif
18 
19 #include <ostream>
20 #include <string>
21 
22 #include "quiche/common/platform/api/quiche_export.h"
23 #include "quiche/common/quiche_ip_address_family.h"
24 
25 namespace quiche {
26 
27 // Represents an IP address.
28 class QUICHE_EXPORT QuicheIpAddress {
29  public:
30   // Sizes of IP addresses of different types, in bytes.
31   enum : size_t {
32     kIPv4AddressSize = 32 / 8,
33     kIPv6AddressSize = 128 / 8,
34     kMaxAddressSize = kIPv6AddressSize,
35   };
36 
37   // TODO(fayang): Remove Loopback*() and use TestLoopback*() in tests.
38   static QuicheIpAddress Loopback4();
39   static QuicheIpAddress Loopback6();
40   static QuicheIpAddress Any4();
41   static QuicheIpAddress Any6();
42 
43   QuicheIpAddress();
44   QuicheIpAddress(const QuicheIpAddress& other) = default;
45   explicit QuicheIpAddress(const in_addr& ipv4_address);
46   explicit QuicheIpAddress(const in6_addr& ipv6_address);
47   QuicheIpAddress& operator=(const QuicheIpAddress& other) = default;
48   QuicheIpAddress& operator=(QuicheIpAddress&& other) = default;
49   QUICHE_EXPORT friend bool operator==(QuicheIpAddress lhs,
50                                        QuicheIpAddress rhs);
51   QUICHE_EXPORT friend bool operator!=(QuicheIpAddress lhs,
52                                        QuicheIpAddress rhs);
53 
54   bool IsInitialized() const;
55   IpAddressFamily address_family() const;
56   int AddressFamilyToInt() const;
57   // Returns the address as a sequence of bytes in network-byte-order. IPv4 will
58   // be 4 bytes. IPv6 will be 16 bytes.
59   std::string ToPackedString() const;
60   // Returns string representation of the address.
61   std::string ToString() const;
62   // Normalizes the address representation with respect to IPv4 addresses, i.e,
63   // mapped IPv4 addresses ("::ffff:X.Y.Z.Q") are converted to pure IPv4
64   // addresses.  All other IPv4, IPv6, and empty values are left unchanged.
65   QuicheIpAddress Normalized() const;
66   // Returns an address suitable for use in IPv6-aware contexts.  This is the
67   // opposite of NormalizeIPAddress() above.  IPv4 addresses are converted into
68   // their IPv4-mapped address equivalents (e.g. 192.0.2.1 becomes
69   // ::ffff:192.0.2.1).  IPv6 addresses are a noop (they are returned
70   // unchanged).
71   QuicheIpAddress DualStacked() const;
72   bool FromPackedString(const char* data, size_t length);
73   bool FromString(std::string str);
74   bool IsIPv4() const;
75   bool IsIPv6() const;
76   bool InSameSubnet(const QuicheIpAddress& other, int subnet_length);
77 
78   in_addr GetIPv4() const;
79   in6_addr GetIPv6() const;
80 
81  private:
82   union {
83     in_addr v4;
84     in6_addr v6;
85     uint8_t bytes[kMaxAddressSize];
86     char chars[kMaxAddressSize];
87   } address_;
88   IpAddressFamily family_;
89 };
90 
91 inline std::ostream& operator<<(std::ostream& os,
92                                 const QuicheIpAddress address) {
93   os << address.ToString();
94   return os;
95 }
96 
97 // Represents an IP prefix, which is an IP address and a prefix length in bits.
98 class QUICHE_EXPORT QuicheIpPrefix {
99  public:
100   QuicheIpPrefix();
101   explicit QuicheIpPrefix(const QuicheIpAddress& address);
102   explicit QuicheIpPrefix(const QuicheIpAddress& address,
103                           uint8_t prefix_length);
104 
address()105   QuicheIpAddress address() const { return address_; }
prefix_length()106   uint8_t prefix_length() const { return prefix_length_; }
107   // Human-readable string representation of the prefix suitable for logging.
108   std::string ToString() const;
109 
110   QuicheIpPrefix(const QuicheIpPrefix& other) = default;
111   QuicheIpPrefix& operator=(const QuicheIpPrefix& other) = default;
112   QuicheIpPrefix& operator=(QuicheIpPrefix&& other) = default;
113   QUICHE_EXPORT friend bool operator==(const QuicheIpPrefix& lhs,
114                                        const QuicheIpPrefix& rhs);
115   QUICHE_EXPORT friend bool operator!=(const QuicheIpPrefix& lhs,
116                                        const QuicheIpPrefix& rhs);
117 
118  private:
119   QuicheIpAddress address_;
120   uint8_t prefix_length_;
121 };
122 
123 inline std::ostream& operator<<(std::ostream& os, const QuicheIpPrefix prefix) {
124   os << prefix.ToString();
125   return os;
126 }
127 
128 }  // namespace quiche
129 
130 #endif  // QUICHE_COMMON_QUICHE_IP_ADDRESS_H_
131