File: address_info.h

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 (114 lines) | stat: -rw-r--r-- 3,478 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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_DNS_ADDRESS_INFO_H_
#define NET_DNS_ADDRESS_INFO_H_

#include <memory>
#include <optional>
#include <string>
#include <tuple>

#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#include "net/base/address_family.h"
#include "net/base/net_export.h"
#include "net/base/network_handle.h"
#include "net/base/sys_addrinfo.h"

namespace net {

class AddressList;

using FreeAddrInfoFunc = void (*)(addrinfo*);

// Encapsulates calls to getaddrinfo and freeaddrinfo for tests.
class NET_EXPORT_PRIVATE AddrInfoGetter {
 public:
  AddrInfoGetter();

  AddrInfoGetter(const AddrInfoGetter&) = delete;
  AddrInfoGetter& operator=(const AddrInfoGetter&) = delete;

  // Virtual for tests.
  virtual ~AddrInfoGetter();
  virtual std::unique_ptr<addrinfo, FreeAddrInfoFunc> getaddrinfo(
      const std::string& host,
      const addrinfo* hints,
      int* out_os_error,
      handles::NetworkHandle network);
};

// AddressInfo -- this encapsulates the system call to getaddrinfo and the
// data structure that it populates and returns.
class NET_EXPORT_PRIVATE AddressInfo {
 public:
  // Types
  class NET_EXPORT_PRIVATE const_iterator {
   public:
    using iterator_category = std::forward_iterator_tag;
    using value_type = const addrinfo;
    using difference_type = std::ptrdiff_t;
    using pointer = const addrinfo*;
    using reference = const addrinfo&;

    const_iterator(const const_iterator& other) = default;
    explicit const_iterator(const addrinfo* ai);
    friend bool operator==(const const_iterator&,
                           const const_iterator&) = default;
    const_iterator& operator++();  // prefix
    const addrinfo* operator->() const;
    const addrinfo& operator*() const;

   private:
    // Owned by AddressInfo.
    raw_ptr<const addrinfo> ai_;
  };

  // Constructors
  using AddressInfoAndResult =
      std::tuple<std::optional<AddressInfo>, int /* err */, int /* os_error */>;
  // Invokes AddrInfoGetter with provided `host` and `hints`. If `getter` is
  // null, the system's getaddrinfo will be invoked. (A non-null `getter` is
  // primarily for tests).
  // `network` is an optional parameter, when specified (!=
  // handles::kInvalidNetworkHandle) the lookup will be performed specifically
  // for `network` (currently only supported on Android platforms).
  static AddressInfoAndResult Get(
      const std::string& host,
      const addrinfo& hints,
      std::unique_ptr<AddrInfoGetter> getter = nullptr,
      handles::NetworkHandle network = handles::kInvalidNetworkHandle);

  AddressInfo(const AddressInfo&) = delete;
  AddressInfo& operator=(const AddressInfo&) = delete;

  AddressInfo(AddressInfo&& other);
  AddressInfo& operator=(AddressInfo&& other);

  ~AddressInfo();

  // Accessors
  const_iterator begin() const;
  const_iterator end() const;

  // Methods
  std::optional<std::string> GetCanonicalName() const;
  bool IsAllLocalhostOfOneFamily() const;
  AddressList CreateAddressList() const;

 private:
  // Constructors
  AddressInfo(std::unique_ptr<addrinfo, FreeAddrInfoFunc> ai,
              std::unique_ptr<AddrInfoGetter> getter);

  // Data.
  std::unique_ptr<addrinfo, FreeAddrInfoFunc>
      ai_;  // Never null (except after move)
  std::unique_ptr<AddrInfoGetter> getter_;
};

}  // namespace net

#endif  // NET_DNS_ADDRESS_INFO_H_