summaryrefslogtreecommitdiffstats
path: root/net/base/address_list.h
blob: ce176451f6b8178f30b79b25e7d1f09902b765b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) 2010 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.

#ifndef NET_BASE_ADDRESS_LIST_H_
#define NET_BASE_ADDRESS_LIST_H_

#include <string>

#include "base/ref_counted.h"

struct addrinfo;

namespace net {

// An AddressList object contains a linked list of addrinfo structures.  This
// class is designed to be copied around by value.
class AddressList {
 public:
  // Constructs an empty address list.
  AddressList() {}

  // Adopt the given addrinfo list in place of the existing one if any.  This
  // hands over responsibility for freeing the addrinfo list to the AddressList
  // object.
  void Adopt(struct addrinfo* head);

  // Copies the given addrinfo rather than adopting it. If |recursive| is true,
  // all linked struct addrinfos will be copied as well. Otherwise only the head
  // will be copied, and the rest of linked entries will be ignored.
  void Copy(const struct addrinfo* head, bool recursive);

  // Appends a copy of |head| and all its linked addrinfos to the stored
  // addrinfo.
  void Append(const struct addrinfo* head);

  // Sets the port of all addresses in the list to |port| (that is the
  // sin[6]_port field for the sockaddrs).
  void SetPort(int port);

  // Retrieves the port number of the first sockaddr in the list. (If SetPort()
  // was previously used on this list, then all the addresses will have this
  // same port number.)
  int GetPort() const;

  // Sets the address to match |src|, and have each sockaddr's port be |port|.
  // If |src| already has the desired port this operation is cheap (just adds
  // a reference to |src|'s data.) Otherwise we will make a copy.
  void SetFrom(const AddressList& src, int port);

  // Gets the canonical name for the address.
  // If the canonical name exists, |*canonical_name| is filled in with the
  // value and true is returned. If it does not exist, |*canonical_name| is
  // not altered and false is returned.
  // |canonical_name| must be a non-null value.
  bool GetCanonicalName(std::string* canonical_name) const;

  // Clears all data from this address list. This leaves the list in the same
  // empty state as when first constructed.
  void Reset();

  // Used by unit-tests to manually create an IPv4 AddressList. |data| should
  // be an IPv4 address in network order (big endian).
  // If |canonical_name| is non-empty, it will be duplicated in the
  // ai_canonname field of the addrinfo struct.
  static AddressList CreateIPv4Address(unsigned char data[4],
                                       const std::string& canonical_name);

  // Used by unit-tests to manually create an IPv6 AddressList. |data| should
  // be an IPv6 address in network order (big endian).
  // If |canonical_name| is non-empty, it will be duplicated in the
  // ai_canonname field of the addrinfo struct.
  static AddressList CreateIPv6Address(unsigned char data[16],
                                       const std::string& canonical_name);

  // Get access to the head of the addrinfo list.
  const struct addrinfo* head() const { return data_->head; }

 private:
  struct Data : public base::RefCountedThreadSafe<Data> {
    Data(struct addrinfo* ai, bool is_system_created);
    struct addrinfo* head;

    // Indicates which free function to use for |head|.
    bool is_system_created;

   private:
    friend class base::RefCountedThreadSafe<Data>;

    ~Data();
  };

  explicit AddressList(Data* data) : data_(data) {}

  scoped_refptr<Data> data_;
};

}  // namespace net

#endif  // NET_BASE_ADDRESS_LIST_H_