xref: /aosp_15_r20/external/cronet/net/base/ip_endpoint.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2012 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef NET_BASE_IP_ENDPOINT_H_
6*6777b538SAndroid Build Coastguard Worker #define NET_BASE_IP_ENDPOINT_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <stdint.h>
9*6777b538SAndroid Build Coastguard Worker 
10*6777b538SAndroid Build Coastguard Worker #include <optional>
11*6777b538SAndroid Build Coastguard Worker #include <ostream>
12*6777b538SAndroid Build Coastguard Worker #include <string>
13*6777b538SAndroid Build Coastguard Worker 
14*6777b538SAndroid Build Coastguard Worker #include "base/values.h"
15*6777b538SAndroid Build Coastguard Worker #include "build/build_config.h"
16*6777b538SAndroid Build Coastguard Worker #include "net/base/address_family.h"
17*6777b538SAndroid Build Coastguard Worker #include "net/base/ip_address.h"
18*6777b538SAndroid Build Coastguard Worker #include "net/base/net_export.h"
19*6777b538SAndroid Build Coastguard Worker 
20*6777b538SAndroid Build Coastguard Worker // Replicate these from Windows headers to avoid pulling net/sys_addrinfo.h.
21*6777b538SAndroid Build Coastguard Worker // Doing that transitively brings in windows.h. Including windows.h pollutes the
22*6777b538SAndroid Build Coastguard Worker // global namespace with thousands of macro definitions. This file is
23*6777b538SAndroid Build Coastguard Worker // transitively included in enough files that including windows.h potentially
24*6777b538SAndroid Build Coastguard Worker // impacts build performance.
25*6777b538SAndroid Build Coastguard Worker // Similarly, just pull in the minimal header necessary on non-Windows platforms
26*6777b538SAndroid Build Coastguard Worker // to help with build performance.
27*6777b538SAndroid Build Coastguard Worker struct sockaddr;
28*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN)
29*6777b538SAndroid Build Coastguard Worker typedef int socklen_t;
30*6777b538SAndroid Build Coastguard Worker #else
31*6777b538SAndroid Build Coastguard Worker #include <sys/socket.h>
32*6777b538SAndroid Build Coastguard Worker #endif
33*6777b538SAndroid Build Coastguard Worker 
34*6777b538SAndroid Build Coastguard Worker namespace net {
35*6777b538SAndroid Build Coastguard Worker 
36*6777b538SAndroid Build Coastguard Worker // An IPEndPoint represents the address of a transport endpoint:
37*6777b538SAndroid Build Coastguard Worker //  * IP address (either v4 or v6)
38*6777b538SAndroid Build Coastguard Worker //  * Port
39*6777b538SAndroid Build Coastguard Worker class NET_EXPORT IPEndPoint {
40*6777b538SAndroid Build Coastguard Worker  public:
41*6777b538SAndroid Build Coastguard Worker   // Nullopt if `value` is malformed to be serialized to IPEndPoint.
42*6777b538SAndroid Build Coastguard Worker   static std::optional<IPEndPoint> FromValue(const base::Value& value);
43*6777b538SAndroid Build Coastguard Worker 
44*6777b538SAndroid Build Coastguard Worker   IPEndPoint();
45*6777b538SAndroid Build Coastguard Worker   ~IPEndPoint();
46*6777b538SAndroid Build Coastguard Worker   IPEndPoint(const IPAddress& address, uint16_t port);
47*6777b538SAndroid Build Coastguard Worker   IPEndPoint(const IPEndPoint& endpoint);
48*6777b538SAndroid Build Coastguard Worker 
address()49*6777b538SAndroid Build Coastguard Worker   const IPAddress& address() const { return address_; }
50*6777b538SAndroid Build Coastguard Worker 
51*6777b538SAndroid Build Coastguard Worker   // Returns the IPv4/IPv6 port if it has been set by the constructor or
52*6777b538SAndroid Build Coastguard Worker   // `FromSockAddr`. This function will crash if the IPEndPoint is for a
53*6777b538SAndroid Build Coastguard Worker   // Bluetooth socket.
54*6777b538SAndroid Build Coastguard Worker   uint16_t port() const;
55*6777b538SAndroid Build Coastguard Worker 
56*6777b538SAndroid Build Coastguard Worker   // Returns AddressFamily of the address. Returns ADDRESS_FAMILY_UNSPECIFIED if
57*6777b538SAndroid Build Coastguard Worker   // this is the IPEndPoint for a Bluetooth socket.
58*6777b538SAndroid Build Coastguard Worker   AddressFamily GetFamily() const;
59*6777b538SAndroid Build Coastguard Worker 
60*6777b538SAndroid Build Coastguard Worker   // Returns the sockaddr family of the address, AF_INET or AF_INET6. Returns
61*6777b538SAndroid Build Coastguard Worker   // AF_BTH if this is the IPEndPoint for a Bluetooth socket.
62*6777b538SAndroid Build Coastguard Worker   int GetSockAddrFamily() const;
63*6777b538SAndroid Build Coastguard Worker 
64*6777b538SAndroid Build Coastguard Worker   // Convert to a provided sockaddr struct. This function will crash if the
65*6777b538SAndroid Build Coastguard Worker   // IPEndPoint is for a Bluetooth socket.
66*6777b538SAndroid Build Coastguard Worker   // |address| is the sockaddr to copy into.  Should be at least
67*6777b538SAndroid Build Coastguard Worker   //    sizeof(struct sockaddr_storage) bytes.
68*6777b538SAndroid Build Coastguard Worker   // |address_length| is an input/output parameter.  On input, it is the
69*6777b538SAndroid Build Coastguard Worker   //    size of data in |address| available.  On output, it is the size of
70*6777b538SAndroid Build Coastguard Worker   //    the address that was copied into |address|.
71*6777b538SAndroid Build Coastguard Worker   // Returns true on success, false on failure.
72*6777b538SAndroid Build Coastguard Worker   [[nodiscard]] bool ToSockAddr(struct sockaddr* address,
73*6777b538SAndroid Build Coastguard Worker                                 socklen_t* address_length) const;
74*6777b538SAndroid Build Coastguard Worker 
75*6777b538SAndroid Build Coastguard Worker   // Convert from a sockaddr struct.
76*6777b538SAndroid Build Coastguard Worker   // |address| is the address.
77*6777b538SAndroid Build Coastguard Worker   // |address_length| is the length of |address|.
78*6777b538SAndroid Build Coastguard Worker   // Returns true on success, false on failure.
79*6777b538SAndroid Build Coastguard Worker   [[nodiscard]] bool FromSockAddr(const struct sockaddr* address,
80*6777b538SAndroid Build Coastguard Worker                                   socklen_t address_length);
81*6777b538SAndroid Build Coastguard Worker 
82*6777b538SAndroid Build Coastguard Worker   // Returns value as a string (e.g. "127.0.0.1:80"). Returns the empty string
83*6777b538SAndroid Build Coastguard Worker   // when |address_| is invalid (the port will be ignored). This function will
84*6777b538SAndroid Build Coastguard Worker   // crash if the IPEndPoint is for a Bluetooth socket.
85*6777b538SAndroid Build Coastguard Worker   std::string ToString() const;
86*6777b538SAndroid Build Coastguard Worker 
87*6777b538SAndroid Build Coastguard Worker   // As above, but without port. Returns the empty string when address_ is
88*6777b538SAndroid Build Coastguard Worker   // invalid. The function will crash if the IPEndPoint is for a Bluetooth
89*6777b538SAndroid Build Coastguard Worker   // socket.
90*6777b538SAndroid Build Coastguard Worker   std::string ToStringWithoutPort() const;
91*6777b538SAndroid Build Coastguard Worker 
92*6777b538SAndroid Build Coastguard Worker   bool operator<(const IPEndPoint& that) const;
93*6777b538SAndroid Build Coastguard Worker   bool operator==(const IPEndPoint& that) const;
94*6777b538SAndroid Build Coastguard Worker   bool operator!=(const IPEndPoint& that) const;
95*6777b538SAndroid Build Coastguard Worker 
96*6777b538SAndroid Build Coastguard Worker   base::Value ToValue() const;
97*6777b538SAndroid Build Coastguard Worker 
98*6777b538SAndroid Build Coastguard Worker  private:
99*6777b538SAndroid Build Coastguard Worker   IPAddress address_;
100*6777b538SAndroid Build Coastguard Worker   uint16_t port_ = 0;
101*6777b538SAndroid Build Coastguard Worker };
102*6777b538SAndroid Build Coastguard Worker 
103*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
104*6777b538SAndroid Build Coastguard Worker                                             const IPEndPoint& ip_endpoint);
105*6777b538SAndroid Build Coastguard Worker 
106*6777b538SAndroid Build Coastguard Worker }  // namespace net
107*6777b538SAndroid Build Coastguard Worker 
108*6777b538SAndroid Build Coastguard Worker #endif  // NET_BASE_IP_ENDPOINT_H_
109