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