File: ip_endpoint.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (320 lines) | stat: -rw-r--r-- 9,344 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/base/ip_endpoint.h"

#include <string.h>

#include <optional>
#include <ostream>
#include <tuple>
#include <utility>

#include "base/check.h"
#include "base/check_op.h"
#include "base/containers/span.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "base/sys_byteorder.h"
#include "base/values.h"
#include "build/build_config.h"
#include "net/base/ip_address.h"
#include "net/base/ip_address_util.h"
#include "net/base/sys_addrinfo.h"

#if BUILDFLAG(IS_WIN)
#include <winsock2.h>
#include <winternl.h>

#include <netioapi.h>
#include <ntstatus.h>
#include <ws2bth.h>

#include "net/base/winsock_util.h"  // For kBluetoothAddressSize
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#include <net/if.h>
#endif

namespace net {

namespace {

// Value dictionary keys
constexpr std::string_view kValueAddressKey = "address";
constexpr std::string_view kValuePortKey = "port";
constexpr std::string_view kInterfaceName = "interface_name";

}  // namespace

IPEndPoint::IndexToNameFunc IPEndPoint::index_to_name_func_for_testing_ =
    nullptr;
IPEndPoint::NameToIndexFunc IPEndPoint::name_to_index_func_for_testing_ =
    nullptr;

// static
void IPEndPoint::SetNameToIndexFuncForTesting(NameToIndexFunc func) {
  name_to_index_func_for_testing_ = func;
}

void IPEndPoint::SetIndexToNameFuncForTesting(IndexToNameFunc func) {
  index_to_name_func_for_testing_ = func;
}

// static
std::optional<uint32_t> IPEndPoint::ScopeIdFromDict(
    const base::Value::Dict& dict) {
  const std::string* name = dict.FindString(kInterfaceName);
  if (!name) {
    return std::nullopt;
  }

  unsigned int index = 0;
  if (name_to_index_func_for_testing_) {
    index = name_to_index_func_for_testing_(name->c_str());
  } else {
    index = if_nametoindex(name->c_str());
  }

  return index;
}

// static
base::Value IPEndPoint::ScopeIdToValue(std::optional<uint32_t> scope_id) {
  if (!scope_id.has_value()) {
    return base::Value();
  }

  char* name = nullptr;
  char buf[IF_NAMESIZE + 1] = {0};
  if (index_to_name_func_for_testing_) {
    name = index_to_name_func_for_testing_(scope_id.value(), buf);
  } else {
    name = if_indextoname(scope_id.value(), buf);
  }

  if (!name) {
    return base::Value();
  }

  return base::Value(name);
}

// static
std::optional<IPEndPoint> IPEndPoint::FromValue(const base::Value& value) {
  const base::Value::Dict* dict = value.GetIfDict();
  if (!dict)
    return std::nullopt;

  const base::Value* address_value = dict->Find(kValueAddressKey);
  if (!address_value)
    return std::nullopt;
  std::optional<IPAddress> address = IPAddress::FromValue(*address_value);
  if (!address.has_value())
    return std::nullopt;
  // Expect IPAddress to only allow deserializing valid addresses.
  DCHECK(address.value().IsValid());

  std::optional<int> port = dict->FindInt(kValuePortKey);
  if (!port.has_value() ||
      !base::IsValueInRangeForNumericType<uint16_t>(port.value())) {
    return std::nullopt;
  }

  IPEndPoint endpoint(address.value(),
                      base::checked_cast<uint16_t>(port.value()));

  std::optional<uint32_t> scope_id = ScopeIdFromDict(*dict);
  if (scope_id.has_value()) {
    if (scope_id.value() == 0 || !endpoint.IsIPv6LinkLocal() ||
        !base::IsValueInRangeForNumericType<uint32_t>(scope_id.value())) {
      return std::nullopt;
    }
    endpoint.scope_id_ = scope_id.value();
  }

  return endpoint;
}

IPEndPoint::IPEndPoint() = default;

IPEndPoint::~IPEndPoint() = default;

IPEndPoint::IPEndPoint(const IPAddress& address,
                       uint16_t port,
                       std::optional<uint32_t> scope_id)
    : address_(address), port_(port), scope_id_(scope_id) {}

IPEndPoint::IPEndPoint(const IPEndPoint& endpoint) = default;

uint16_t IPEndPoint::port() const {
#if BUILDFLAG(IS_WIN)
  DCHECK_NE(address_.size(), kBluetoothAddressSize);
#endif
  return port_;
}

AddressFamily IPEndPoint::GetFamily() const {
  return GetAddressFamily(address_);
}

int IPEndPoint::GetSockAddrFamily() const {
  switch (address_.size()) {
    case IPAddress::kIPv4AddressSize:
      return AF_INET;
    case IPAddress::kIPv6AddressSize:
      return AF_INET6;
#if BUILDFLAG(IS_WIN)
    case kBluetoothAddressSize:
      return AF_BTH;
#endif
    default:
      NOTREACHED() << "Bad IP address";
  }
}

bool IPEndPoint::ToSockAddr(struct sockaddr* address,
                            socklen_t* address_length) const {
  // By definition, socklen_t is large enough to hold both sizes.
  constexpr socklen_t kSockaddrInSize =
      static_cast<socklen_t>(sizeof(struct sockaddr_in));
  constexpr socklen_t kSockaddrIn6Size =
      static_cast<socklen_t>(sizeof(struct sockaddr_in6));

  DCHECK(address);
  DCHECK(address_length);
#if BUILDFLAG(IS_WIN)
  DCHECK_NE(address_.size(), kBluetoothAddressSize);
#endif
  switch (address_.size()) {
    case IPAddress::kIPv4AddressSize: {
      if (*address_length < kSockaddrInSize)
        return false;
      *address_length = kSockaddrInSize;
      struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*>(address);
      // Zero out address struct.
      *addr = {};
      addr->sin_family = AF_INET;
      addr->sin_port = base::HostToNet16(port_);
      addr->sin_addr = ToInAddr(address_);
      break;
    }
    case IPAddress::kIPv6AddressSize: {
      if (*address_length < kSockaddrIn6Size)
        return false;
      *address_length = kSockaddrIn6Size;
      struct sockaddr_in6* addr6 =
          reinterpret_cast<struct sockaddr_in6*>(address);
      // Zero out address struct.
      *addr6 = {};
      addr6->sin6_family = AF_INET6;
      addr6->sin6_port = base::HostToNet16(port_);
      addr6->sin6_addr = ToIn6Addr(address_);
      if (IsIPv6LinkLocal() && scope_id_) {
        addr6->sin6_scope_id = *scope_id_;
      }
      break;
    }
    default:
      return false;
  }
  return true;
}

bool IPEndPoint::FromSockAddr(const struct sockaddr* sock_addr,
                              socklen_t sock_addr_len) {
  DCHECK(sock_addr);
  switch (sock_addr->sa_family) {
    case AF_INET: {
      if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in)))
        return false;
      const struct sockaddr_in* addr =
          reinterpret_cast<const struct sockaddr_in*>(sock_addr);
      *this = IPEndPoint(
          // `s_addr` is a `uint32_t`, but it is already in network byte order.
          IPAddress(base::as_bytes(base::span_from_ref(addr->sin_addr.s_addr))),
          base::NetToHost16(addr->sin_port));
      return true;
    }
    case AF_INET6: {
      if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in6)))
        return false;
      const struct sockaddr_in6* addr =
          reinterpret_cast<const struct sockaddr_in6*>(sock_addr);
      *this = IPEndPoint(IPAddress(addr->sin6_addr.s6_addr),
                         base::NetToHost16(addr->sin6_port));
      if (IsIPv6LinkLocal() && addr->sin6_scope_id != 0) {
        scope_id_ = addr->sin6_scope_id;
      }
      return true;
    }
#if BUILDFLAG(IS_WIN)
    case AF_BTH: {
      if (sock_addr_len < static_cast<socklen_t>(sizeof(SOCKADDR_BTH)))
        return false;
      const SOCKADDR_BTH* addr =
          reinterpret_cast<const SOCKADDR_BTH*>(sock_addr);
      *this = IPEndPoint();
      // A bluetooth address is 6 bytes, but btAddr is a ULONGLONG, so we take a
      // prefix of it.
      address_ = IPAddress(base::as_bytes(base::span_from_ref(addr->btAddr))
                               .first(kBluetoothAddressSize));
      // Intentionally ignoring Bluetooth port. It is a ULONG, but
      // `IPEndPoint::port_` is a uint16_t. See https://crbug.com/1231273.
      return true;
    }
#endif
  }
  return false;  // Unrecognized |sa_family|.
}

std::string IPEndPoint::ToString() const {
#if BUILDFLAG(IS_WIN)
  DCHECK_NE(address_.size(), kBluetoothAddressSize);
#endif
  return IPAddressToStringWithPort(address_, port_);
}

std::string IPEndPoint::ToStringWithoutPort() const {
#if BUILDFLAG(IS_WIN)
  DCHECK_NE(address_.size(), kBluetoothAddressSize);
#endif
  return address_.ToString();
}

bool IPEndPoint::operator<(const IPEndPoint& other) const {
  // Sort IPv4 before IPv6.
  if (address_.size() != other.address_.size()) {
    return address_.size() < other.address_.size();
  }
  return std::tie(address_, port_, scope_id_) <
         std::tie(other.address_, other.port_, other.scope_id_);
}

base::Value IPEndPoint::ToValue() const {
  base::Value::Dict dict;

  DCHECK(address_.IsValid());
  dict.Set(kValueAddressKey, address_.ToValue());
  dict.Set(kValuePortKey, port_);

  base::Value interface_name = ScopeIdToValue(scope_id_);
  if (!interface_name.is_none()) {
    DCHECK(IsIPv6LinkLocal());
    dict.Set(kInterfaceName, std::move(interface_name));
  }

  return base::Value(std::move(dict));
}

bool IPEndPoint::IsIPv6LinkLocal() const {
  return address_.IsValid() && address_.IsIPv6() &&
         !address_.IsIPv4MappedIPv6() && address_.IsLinkLocal();
}

std::ostream& operator<<(std::ostream& os, const IPEndPoint& ip_endpoint) {
  return os << ip_endpoint.ToString();
}

}  // namespace net