• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 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_BASE_ADDRESS_LIST_H_
6 #define NET_BASE_ADDRESS_LIST_H_
7 
8 #include <stdint.h>
9 
10 #include <string>
11 #include <tuple>
12 #include <utility>
13 #include <vector>
14 
15 #include "base/compiler_specific.h"
16 #include "base/values.h"
17 #include "net/base/ip_endpoint.h"
18 #include "net/base/net_export.h"
19 
20 struct addrinfo;
21 
22 namespace base {
23 class Value;
24 }
25 
26 namespace net {
27 
28 class IPAddress;
29 
30 class NET_EXPORT AddressList {
31  public:
32   AddressList();
33   AddressList(const AddressList&);
34   AddressList& operator=(const AddressList&);
35   AddressList(AddressList&&);
36   AddressList& operator=(AddressList&&);
37   ~AddressList();
38 
39   // Creates an address list for a single IP endpoint.
40   explicit AddressList(const IPEndPoint& endpoint);
41 
42   // Creates an address list for a single IP endpoint and a list of DNS aliases.
43   AddressList(const IPEndPoint& endpoint, std::vector<std::string> aliases);
44 
45   // Creates an address list for a list of IP endpoints.
46   explicit AddressList(std::vector<IPEndPoint> endpoints);
47 
48   static AddressList CreateFromIPAddress(const IPAddress& address,
49                                          uint16_t port);
50 
51   static AddressList CreateFromIPAddressList(const IPAddressList& addresses,
52                                              std::vector<std::string> aliases);
53 
54   // Copies the data from `head` and the chained list into an AddressList.
55   static AddressList CreateFromAddrinfo(const struct addrinfo* head);
56 
57   // Returns a copy of `list` with port on each element set to |port|.
58   static AddressList CopyWithPort(const AddressList& list, uint16_t port);
59 
60   bool operator==(const AddressList& other) const {
61     return std::tie(endpoints_, dns_aliases_) ==
62            std::tie(other.endpoints_, other.dns_aliases_);
63   }
64   bool operator!=(const AddressList& other) const { return !(*this == other); }
65 
66   // Sets the first entry of `dns_aliases_` to the literal of the first IP
67   // address on the list. Assumes that `dns_aliases_` is empty.
68   void SetDefaultCanonicalName();
69 
70   // The alias chain in no particular order.
dns_aliases()71   const std::vector<std::string>& dns_aliases() const { return dns_aliases_; }
72 
73   void SetDnsAliases(std::vector<std::string> aliases);
74 
75   void AppendDnsAliases(std::vector<std::string> aliases);
76 
77   // Creates a value representation of the address list, appropriate for
78   // inclusion in a NetLog.
79   base::Value::Dict NetLogParams() const;
80 
81   // Deduplicates the stored addresses while otherwise preserving their order.
82   void Deduplicate();
83 
84   using iterator = std::vector<IPEndPoint>::iterator;
85   using const_iterator = std::vector<IPEndPoint>::const_iterator;
86 
size()87   size_t size() const { return endpoints_.size(); }
empty()88   bool empty() const { return endpoints_.empty(); }
clear()89   void clear() { endpoints_.clear(); }
reserve(size_t count)90   void reserve(size_t count) { endpoints_.reserve(count); }
capacity()91   size_t capacity() const { return endpoints_.capacity(); }
92   IPEndPoint& operator[](size_t index) { return endpoints_[index]; }
93   const IPEndPoint& operator[](size_t index) const { return endpoints_[index]; }
front()94   IPEndPoint& front() { return endpoints_.front(); }
front()95   const IPEndPoint& front() const { return endpoints_.front(); }
back()96   IPEndPoint& back() { return endpoints_.back(); }
back()97   const IPEndPoint& back() const { return endpoints_.back(); }
push_back(const IPEndPoint & val)98   void push_back(const IPEndPoint& val) { endpoints_.push_back(val); }
99 
100   template <typename InputIt>
insert(iterator pos,InputIt first,InputIt last)101   void insert(iterator pos, InputIt first, InputIt last) {
102     endpoints_.insert(pos, first, last);
103   }
begin()104   iterator begin() { return endpoints_.begin(); }
begin()105   const_iterator begin() const { return endpoints_.begin(); }
end()106   iterator end() { return endpoints_.end(); }
end()107   const_iterator end() const { return endpoints_.end(); }
108 
endpoints()109   const std::vector<net::IPEndPoint>& endpoints() const { return endpoints_; }
endpoints()110   std::vector<net::IPEndPoint>& endpoints() { return endpoints_; }
111 
112  private:
113   std::vector<IPEndPoint> endpoints_;
114 
115   // In no particular order.
116   std::vector<std::string> dns_aliases_;
117 };
118 
119 }  // namespace net
120 
121 #endif  // NET_BASE_ADDRESS_LIST_H_
122