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