blob: 95bc277c30bae678124e98192323e1b9cb1f5ed1 [file] [log] [blame]
// 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.
#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 {
// Types
class NET_EXPORT_PRIVATE const_iterator {
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;
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);
// Accessors
const_iterator begin() const;
const_iterator end() const;
// Methods
base::Optional<std::string> GetCanonicalName() const;
bool IsAllLocalhostOfOneFamily() const;
AddressList CreateAddressList() const;
// Constructors
AddressInfo(addrinfo* ai, std::unique_ptr<AddrInfoGetter> getter);
// Data.
addrinfo* ai_; // Never null (except after move)
std::unique_ptr<AddrInfoGetter> getter_;
// Encapsulates calls to getaddrinfo and freeaddrinfo for tests.
class NET_EXPORT_PRIVATE 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);
} // namespace net