OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_BASE_ADDRESS_LIST_H_ | 5 #ifndef NET_BASE_ADDRESS_LIST_H_ |
6 #define NET_BASE_ADDRESS_LIST_H_ | 6 #define NET_BASE_ADDRESS_LIST_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/ref_counted.h" | 11 #include "base/ref_counted.h" |
12 #include "net/base/net_util.h" | |
13 | 12 |
14 struct addrinfo; | 13 struct addrinfo; |
15 | 14 |
16 namespace net { | 15 namespace net { |
17 | 16 |
18 // An AddressList object contains a linked list of addrinfo structures. This | 17 // An AddressList object contains a linked list of addrinfo structures. This |
19 // class is designed to be copied around by value. | 18 // class is designed to be copied around by value. |
20 class AddressList { | 19 class AddressList { |
21 public: | 20 public: |
22 // Constructs an empty address list. | 21 // Constructs an empty address list. |
23 AddressList() {} | 22 AddressList() {} |
24 | 23 |
25 // Constructs an address list for a single IP literal. If | |
26 // |canonicalize_name| is true, fill the ai_canonname field with the | |
27 // canonicalized IP address. | |
28 AddressList(const IPAddressNumber& address, int port, bool canonicalize_name); | |
29 | |
30 // Adopt the given addrinfo list in place of the existing one if any. This | 24 // Adopt the given addrinfo list in place of the existing one if any. This |
31 // hands over responsibility for freeing the addrinfo list to the AddressList | 25 // hands over responsibility for freeing the addrinfo list to the AddressList |
32 // object. | 26 // object. |
33 void Adopt(struct addrinfo* head); | 27 void Adopt(struct addrinfo* head); |
34 | 28 |
35 // Copies the given addrinfo rather than adopting it. If |recursive| is true, | 29 // Copies the given addrinfo rather than adopting it. If |recursive| is true, |
36 // all linked struct addrinfos will be copied as well. Otherwise only the head | 30 // all linked struct addrinfos will be copied as well. Otherwise only the head |
37 // will be copied, and the rest of linked entries will be ignored. | 31 // will be copied, and the rest of linked entries will be ignored. |
38 void Copy(const struct addrinfo* head, bool recursive); | 32 void Copy(const struct addrinfo* head, bool recursive); |
39 | 33 |
(...skipping 19 matching lines...) Expand all Loading... |
59 // If the canonical name exists, |*canonical_name| is filled in with the | 53 // If the canonical name exists, |*canonical_name| is filled in with the |
60 // value and true is returned. If it does not exist, |*canonical_name| is | 54 // value and true is returned. If it does not exist, |*canonical_name| is |
61 // not altered and false is returned. | 55 // not altered and false is returned. |
62 // |canonical_name| must be a non-null value. | 56 // |canonical_name| must be a non-null value. |
63 bool GetCanonicalName(std::string* canonical_name) const; | 57 bool GetCanonicalName(std::string* canonical_name) const; |
64 | 58 |
65 // Clears all data from this address list. This leaves the list in the same | 59 // Clears all data from this address list. This leaves the list in the same |
66 // empty state as when first constructed. | 60 // empty state as when first constructed. |
67 void Reset(); | 61 void Reset(); |
68 | 62 |
| 63 // Used by unit-tests to manually create an IPv4 AddressList. |data| should |
| 64 // be an IPv4 address in network order (big endian). |
| 65 // If |canonical_name| is non-empty, it will be duplicated in the |
| 66 // ai_canonname field of the addrinfo struct. |
| 67 static AddressList CreateIPv4Address(unsigned char data[4], |
| 68 const std::string& canonical_name); |
| 69 |
| 70 // Used by unit-tests to manually create an IPv6 AddressList. |data| should |
| 71 // be an IPv6 address in network order (big endian). |
| 72 // If |canonical_name| is non-empty, it will be duplicated in the |
| 73 // ai_canonname field of the addrinfo struct. |
| 74 static AddressList CreateIPv6Address(unsigned char data[16], |
| 75 const std::string& canonical_name); |
| 76 |
69 // Get access to the head of the addrinfo list. | 77 // Get access to the head of the addrinfo list. |
70 const struct addrinfo* head() const { return data_->head; } | 78 const struct addrinfo* head() const { return data_->head; } |
71 | 79 |
72 private: | 80 private: |
73 struct Data : public base::RefCountedThreadSafe<Data> { | 81 struct Data : public base::RefCountedThreadSafe<Data> { |
74 Data(struct addrinfo* ai, bool is_system_created); | 82 Data(struct addrinfo* ai, bool is_system_created); |
75 struct addrinfo* head; | 83 struct addrinfo* head; |
76 | 84 |
77 // Indicates which free function to use for |head|. | 85 // Indicates which free function to use for |head|. |
78 bool is_system_created; | 86 bool is_system_created; |
79 | 87 |
80 private: | 88 private: |
81 friend class base::RefCountedThreadSafe<Data>; | 89 friend class base::RefCountedThreadSafe<Data>; |
82 | 90 |
83 ~Data(); | 91 ~Data(); |
84 }; | 92 }; |
85 | 93 |
86 explicit AddressList(Data* data) : data_(data) {} | 94 explicit AddressList(Data* data) : data_(data) {} |
87 | 95 |
88 scoped_refptr<Data> data_; | 96 scoped_refptr<Data> data_; |
89 }; | 97 }; |
90 | 98 |
91 } // namespace net | 99 } // namespace net |
92 | 100 |
93 #endif // NET_BASE_ADDRESS_LIST_H_ | 101 #endif // NET_BASE_ADDRESS_LIST_H_ |
OLD | NEW |