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
5#ifndef NET_BASE_ADDRESS_LIST_H_
6#define NET_BASE_ADDRESS_LIST_H_
7
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/compiler_specific.h"
13#include "net/base/ip_endpoint.h"
14#include "net/base/net_export.h"
15#include "net/base/net_log.h"
16#include "net/base/net_util.h"
17
18struct addrinfo;
19
20namespace net {
21
22class NET_EXPORT AddressList
23    : NON_EXPORTED_BASE(private std::vector<IPEndPoint>) {
24 public:
25  AddressList();
26  ~AddressList();
27
28  // Creates an address list for a single IP literal.
29  explicit AddressList(const IPEndPoint& endpoint);
30
31  static AddressList CreateFromIPAddress(const IPAddressNumber& address,
32                                         uint16 port);
33
34  static AddressList CreateFromIPAddressList(
35      const IPAddressList& addresses,
36      const std::string& canonical_name);
37
38  // Copies the data from |head| and the chained list into an AddressList.
39  static AddressList CreateFromAddrinfo(const struct addrinfo* head);
40
41  // Returns a copy of |list| with port on each element set to |port|.
42  static AddressList CopyWithPort(const AddressList& list, uint16 port);
43
44  // TODO(szym): Remove all three. http://crbug.com/126134
45  const std::string& canonical_name() const {
46    return canonical_name_;
47  }
48
49  void set_canonical_name(const std::string& canonical_name) {
50    canonical_name_ = canonical_name;
51  }
52
53  // Sets canonical name to the literal of the first IP address on the list.
54  void SetDefaultCanonicalName();
55
56  // Creates a callback for use with the NetLog that returns a Value
57  // representation of the address list.  The callback must be destroyed before
58  // |this| is.
59  NetLog::ParametersCallback CreateNetLogCallback() const;
60
61  // Exposed methods from std::vector.
62  using std::vector<IPEndPoint>::size;
63  using std::vector<IPEndPoint>::empty;
64  using std::vector<IPEndPoint>::clear;
65  using std::vector<IPEndPoint>::reserve;
66  using std::vector<IPEndPoint>::capacity;
67  using std::vector<IPEndPoint>::operator[];
68  using std::vector<IPEndPoint>::front;
69  using std::vector<IPEndPoint>::back;
70  using std::vector<IPEndPoint>::push_back;
71  using std::vector<IPEndPoint>::insert;
72  using std::vector<IPEndPoint>::erase;
73  using std::vector<IPEndPoint>::iterator;
74  using std::vector<IPEndPoint>::const_iterator;
75  using std::vector<IPEndPoint>::begin;
76  using std::vector<IPEndPoint>::end;
77  using std::vector<IPEndPoint>::rbegin;
78  using std::vector<IPEndPoint>::rend;
79
80 private:
81  // TODO(szym): Remove. http://crbug.com/126134
82  std::string canonical_name_;
83};
84
85}  // namespace net
86
87#endif  // NET_BASE_ADDRESS_LIST_H_
88