summaryrefslogtreecommitdiff
path: root/chromium/net/dns/address_info.h
blob: 95bc277c30bae678124e98192323e1b9cb1f5ed1 (plain)
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
// Copyright (c) 2019 The Chromium Authors. All rights reserved.
// 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 <string>
#include <tuple>

#include "base/macros.h"
#include "base/optional.h"
#include "build/build_config.h"
#include "net/base/address_family.h"
#include "net/base/net_export.h"
#include "net/base/sys_addrinfo.h"

namespace net {

class AddressList;
class AddrInfoGetter;

// 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);
    bool operator!=(const const_iterator& o) const;
    const_iterator& operator++();  // prefix
    const addrinfo* operator->() const;
    const addrinfo& operator*() const;

   private:
    const addrinfo* ai_;
  };

  // Constructors
  using AddressInfoAndResult = std::
      tuple<base::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).
  static AddressInfoAndResult Get(
      const std::string& host,
      const addrinfo& hints,
      std::unique_ptr<AddrInfoGetter> getter = nullptr);

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

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

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

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

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

  DISALLOW_COPY_AND_ASSIGN(AddressInfo);
};

// Encapsulates calls to getaddrinfo and freeaddrinfo for tests.
class NET_EXPORT_PRIVATE AddrInfoGetter {
 public:
  AddrInfoGetter();
  // Virtual for tests.
  virtual ~AddrInfoGetter();
  virtual addrinfo* getaddrinfo(const std::string& host,
                                const addrinfo* hints,
                                int* out_os_error);
  virtual void freeaddrinfo(addrinfo* ai);

  DISALLOW_COPY_AND_ASSIGN(AddrInfoGetter);
};

}  // namespace net

#endif  // NET_DNS_ADDRESS_INFO_H_