address_list.h 4.0 KB

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