xref: /aosp_15_r20/external/cronet/net/base/ip_endpoint.cc (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 #include "net/base/ip_endpoint.h"
6 
7 #include <string.h>
8 
9 #include <optional>
10 #include <ostream>
11 #include <tuple>
12 #include <utility>
13 
14 #include "base/check.h"
15 #include "base/check_op.h"
16 #include "base/containers/span.h"
17 #include "base/notreached.h"
18 #include "base/numerics/safe_conversions.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/sys_byteorder.h"
21 #include "base/values.h"
22 #include "build/build_config.h"
23 #include "net/base/ip_address.h"
24 #include "net/base/sys_addrinfo.h"
25 
26 #if BUILDFLAG(IS_WIN)
27 #include <winsock2.h>
28 
29 #include <ws2bth.h>
30 
31 #include "net/base/winsock_util.h"  // For kBluetoothAddressSize
32 #endif
33 
34 namespace net {
35 
36 namespace {
37 
38 // Value dictionary keys
39 constexpr std::string_view kValueAddressKey = "address";
40 constexpr std::string_view kValuePortKey = "port";
41 
42 }  // namespace
43 
44 // static
FromValue(const base::Value & value)45 std::optional<IPEndPoint> IPEndPoint::FromValue(const base::Value& value) {
46   const base::Value::Dict* dict = value.GetIfDict();
47   if (!dict)
48     return std::nullopt;
49 
50   const base::Value* address_value = dict->Find(kValueAddressKey);
51   if (!address_value)
52     return std::nullopt;
53   std::optional<IPAddress> address = IPAddress::FromValue(*address_value);
54   if (!address.has_value())
55     return std::nullopt;
56   // Expect IPAddress to only allow deserializing valid addresses.
57   DCHECK(address.value().IsValid());
58 
59   std::optional<int> port = dict->FindInt(kValuePortKey);
60   if (!port.has_value() ||
61       !base::IsValueInRangeForNumericType<uint16_t>(port.value())) {
62     return std::nullopt;
63   }
64 
65   return IPEndPoint(address.value(),
66                     base::checked_cast<uint16_t>(port.value()));
67 }
68 
69 IPEndPoint::IPEndPoint() = default;
70 
71 IPEndPoint::~IPEndPoint() = default;
72 
IPEndPoint(const IPAddress & address,uint16_t port)73 IPEndPoint::IPEndPoint(const IPAddress& address, uint16_t port)
74     : address_(address), port_(port) {}
75 
76 IPEndPoint::IPEndPoint(const IPEndPoint& endpoint) = default;
77 
port() const78 uint16_t IPEndPoint::port() const {
79 #if BUILDFLAG(IS_WIN)
80   DCHECK_NE(address_.size(), kBluetoothAddressSize);
81 #endif
82   return port_;
83 }
84 
GetFamily() const85 AddressFamily IPEndPoint::GetFamily() const {
86   return GetAddressFamily(address_);
87 }
88 
GetSockAddrFamily() const89 int IPEndPoint::GetSockAddrFamily() const {
90   switch (address_.size()) {
91     case IPAddress::kIPv4AddressSize:
92       return AF_INET;
93     case IPAddress::kIPv6AddressSize:
94       return AF_INET6;
95 #if BUILDFLAG(IS_WIN)
96     case kBluetoothAddressSize:
97       return AF_BTH;
98 #endif
99     default:
100       NOTREACHED() << "Bad IP address";
101       return AF_UNSPEC;
102   }
103 }
104 
ToSockAddr(struct sockaddr * address,socklen_t * address_length) const105 bool IPEndPoint::ToSockAddr(struct sockaddr* address,
106                             socklen_t* address_length) const {
107   // By definition, socklen_t is large enough to hold both sizes.
108   constexpr socklen_t kSockaddrInSize =
109       static_cast<socklen_t>(sizeof(struct sockaddr_in));
110   constexpr socklen_t kSockaddrIn6Size =
111       static_cast<socklen_t>(sizeof(struct sockaddr_in6));
112 
113   DCHECK(address);
114   DCHECK(address_length);
115 #if BUILDFLAG(IS_WIN)
116   DCHECK_NE(address_.size(), kBluetoothAddressSize);
117 #endif
118   switch (address_.size()) {
119     case IPAddress::kIPv4AddressSize: {
120       if (*address_length < kSockaddrInSize)
121         return false;
122       *address_length = kSockaddrInSize;
123       struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*>(address);
124       memset(addr, 0, sizeof(struct sockaddr_in));
125       addr->sin_family = AF_INET;
126       addr->sin_port = base::HostToNet16(port_);
127       memcpy(&addr->sin_addr, address_.bytes().data(),
128              IPAddress::kIPv4AddressSize);
129       break;
130     }
131     case IPAddress::kIPv6AddressSize: {
132       if (*address_length < kSockaddrIn6Size)
133         return false;
134       *address_length = kSockaddrIn6Size;
135       struct sockaddr_in6* addr6 =
136           reinterpret_cast<struct sockaddr_in6*>(address);
137       memset(addr6, 0, sizeof(struct sockaddr_in6));
138       addr6->sin6_family = AF_INET6;
139       addr6->sin6_port = base::HostToNet16(port_);
140       memcpy(&addr6->sin6_addr, address_.bytes().data(),
141              IPAddress::kIPv6AddressSize);
142       break;
143     }
144     default:
145       return false;
146   }
147   return true;
148 }
149 
FromSockAddr(const struct sockaddr * sock_addr,socklen_t sock_addr_len)150 bool IPEndPoint::FromSockAddr(const struct sockaddr* sock_addr,
151                               socklen_t sock_addr_len) {
152   DCHECK(sock_addr);
153   switch (sock_addr->sa_family) {
154     case AF_INET: {
155       if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in)))
156         return false;
157       const struct sockaddr_in* addr =
158           reinterpret_cast<const struct sockaddr_in*>(sock_addr);
159       *this = IPEndPoint(
160           // `s_addr` is a `uint32_t`, but it is already in network byte order.
161           IPAddress(base::as_bytes(base::span_from_ref(addr->sin_addr.s_addr))),
162           base::NetToHost16(addr->sin_port));
163       return true;
164     }
165     case AF_INET6: {
166       if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in6)))
167         return false;
168       const struct sockaddr_in6* addr =
169           reinterpret_cast<const struct sockaddr_in6*>(sock_addr);
170       *this = IPEndPoint(IPAddress(addr->sin6_addr.s6_addr),
171                          base::NetToHost16(addr->sin6_port));
172       return true;
173     }
174 #if BUILDFLAG(IS_WIN)
175     case AF_BTH: {
176       if (sock_addr_len < static_cast<socklen_t>(sizeof(SOCKADDR_BTH)))
177         return false;
178       const SOCKADDR_BTH* addr =
179           reinterpret_cast<const SOCKADDR_BTH*>(sock_addr);
180       *this = IPEndPoint();
181       // A bluetooth address is 6 bytes, but btAddr is a ULONGLONG, so we take a
182       // prefix of it.
183       address_ = IPAddress(base::as_bytes(base::span_from_ref(addr->btAddr))
184                                .first(kBluetoothAddressSize));
185       // Intentionally ignoring Bluetooth port. It is a ULONG, but
186       // `IPEndPoint::port_` is a uint16_t. See https://crbug.com/1231273.
187       return true;
188     }
189 #endif
190   }
191   return false;  // Unrecognized |sa_family|.
192 }
193 
ToString() const194 std::string IPEndPoint::ToString() const {
195 #if BUILDFLAG(IS_WIN)
196   DCHECK_NE(address_.size(), kBluetoothAddressSize);
197 #endif
198   return IPAddressToStringWithPort(address_, port_);
199 }
200 
ToStringWithoutPort() const201 std::string IPEndPoint::ToStringWithoutPort() const {
202 #if BUILDFLAG(IS_WIN)
203   DCHECK_NE(address_.size(), kBluetoothAddressSize);
204 #endif
205   return address_.ToString();
206 }
207 
operator <(const IPEndPoint & other) const208 bool IPEndPoint::operator<(const IPEndPoint& other) const {
209   // Sort IPv4 before IPv6.
210   if (address_.size() != other.address_.size()) {
211     return address_.size() < other.address_.size();
212   }
213   return std::tie(address_, port_) < std::tie(other.address_, other.port_);
214 }
215 
operator ==(const IPEndPoint & other) const216 bool IPEndPoint::operator==(const IPEndPoint& other) const {
217   return address_ == other.address_ && port_ == other.port_;
218 }
219 
operator !=(const IPEndPoint & that) const220 bool IPEndPoint::operator!=(const IPEndPoint& that) const {
221   return !(*this == that);
222 }
223 
ToValue() const224 base::Value IPEndPoint::ToValue() const {
225   base::Value::Dict dict;
226 
227   DCHECK(address_.IsValid());
228   dict.Set(kValueAddressKey, address_.ToValue());
229   dict.Set(kValuePortKey, port_);
230 
231   return base::Value(std::move(dict));
232 }
233 
operator <<(std::ostream & os,const IPEndPoint & ip_endpoint)234 std::ostream& operator<<(std::ostream& os, const IPEndPoint& ip_endpoint) {
235   return os << ip_endpoint.ToString();
236 }
237 
238 }  // namespace net
239