address_info.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (c) 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_DNS_ADDRESS_INFO_H_
  5. #define NET_DNS_ADDRESS_INFO_H_
  6. #include <memory>
  7. #include <string>
  8. #include <tuple>
  9. #include "base/memory/raw_ptr.h"
  10. #include "build/build_config.h"
  11. #include "net/base/address_family.h"
  12. #include "net/base/net_export.h"
  13. #include "net/base/network_handle.h"
  14. #include "net/base/sys_addrinfo.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. namespace net {
  17. class AddressList;
  18. class AddrInfoGetter;
  19. using FreeAddrInfoFunc = void (*)(addrinfo*);
  20. // AddressInfo -- this encapsulates the system call to getaddrinfo and the
  21. // data structure that it populates and returns.
  22. class NET_EXPORT_PRIVATE AddressInfo {
  23. public:
  24. // Types
  25. class NET_EXPORT_PRIVATE const_iterator {
  26. public:
  27. using iterator_category = std::forward_iterator_tag;
  28. using value_type = const addrinfo;
  29. using difference_type = std::ptrdiff_t;
  30. using pointer = const addrinfo*;
  31. using reference = const addrinfo&;
  32. const_iterator(const const_iterator& other) = default;
  33. explicit const_iterator(const addrinfo* ai);
  34. bool operator!=(const const_iterator& o) const;
  35. const_iterator& operator++(); // prefix
  36. const addrinfo* operator->() const;
  37. const addrinfo& operator*() const;
  38. private:
  39. // Owned by AddressInfo.
  40. raw_ptr<const addrinfo> ai_;
  41. };
  42. // Constructors
  43. using AddressInfoAndResult = std::
  44. tuple<absl::optional<AddressInfo>, int /* err */, int /* os_error */>;
  45. // Invokes AddrInfoGetter with provided `host` and `hints`. If `getter` is
  46. // null, the system's getaddrinfo will be invoked. (A non-null `getter` is
  47. // primarily for tests).
  48. // `network` is an optional parameter, when specified (!=
  49. // handles::kInvalidNetworkHandle) the lookup will be performed specifically
  50. // for `network` (currently only supported on Android platforms).
  51. static AddressInfoAndResult Get(
  52. const std::string& host,
  53. const addrinfo& hints,
  54. std::unique_ptr<AddrInfoGetter> getter = nullptr,
  55. handles::NetworkHandle network = handles::kInvalidNetworkHandle);
  56. AddressInfo(const AddressInfo&) = delete;
  57. AddressInfo& operator=(const AddressInfo&) = delete;
  58. AddressInfo(AddressInfo&& other);
  59. AddressInfo& operator=(AddressInfo&& other);
  60. ~AddressInfo();
  61. // Accessors
  62. const_iterator begin() const;
  63. const_iterator end() const;
  64. // Methods
  65. absl::optional<std::string> GetCanonicalName() const;
  66. bool IsAllLocalhostOfOneFamily() const;
  67. AddressList CreateAddressList() const;
  68. private:
  69. // Constructors
  70. AddressInfo(std::unique_ptr<addrinfo, FreeAddrInfoFunc> ai,
  71. std::unique_ptr<AddrInfoGetter> getter);
  72. // Data.
  73. std::unique_ptr<addrinfo, FreeAddrInfoFunc>
  74. ai_; // Never null (except after move)
  75. std::unique_ptr<AddrInfoGetter> getter_;
  76. };
  77. // Encapsulates calls to getaddrinfo and freeaddrinfo for tests.
  78. class NET_EXPORT_PRIVATE AddrInfoGetter {
  79. public:
  80. AddrInfoGetter();
  81. AddrInfoGetter(const AddrInfoGetter&) = delete;
  82. AddrInfoGetter& operator=(const AddrInfoGetter&) = delete;
  83. // Virtual for tests.
  84. virtual ~AddrInfoGetter();
  85. virtual std::unique_ptr<addrinfo, FreeAddrInfoFunc> getaddrinfo(
  86. const std::string& host,
  87. const addrinfo* hints,
  88. int* out_os_error,
  89. handles::NetworkHandle network);
  90. };
  91. } // namespace net
  92. #endif // NET_DNS_ADDRESS_INFO_H_