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" |
12 | 13 |
13 struct addrinfo; | 14 struct addrinfo; |
14 | 15 |
15 namespace net { | 16 namespace net { |
16 | 17 |
17 // An AddressList object contains a linked list of addrinfo structures. This | 18 // An AddressList object contains a linked list of addrinfo structures. This |
18 // class is designed to be copied around by value. | 19 // class is designed to be copied around by value. |
19 class AddressList { | 20 class AddressList { |
20 public: | 21 public: |
21 // Constructs an empty address list. | 22 // Constructs an empty address list. |
22 AddressList() {} | 23 AddressList() {} |
23 | 24 |
| 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 |
24 // Adopt the given addrinfo list in place of the existing one if any. This | 30 // Adopt the given addrinfo list in place of the existing one if any. This |
25 // hands over responsibility for freeing the addrinfo list to the AddressList | 31 // hands over responsibility for freeing the addrinfo list to the AddressList |
26 // object. | 32 // object. |
27 void Adopt(struct addrinfo* head); | 33 void Adopt(struct addrinfo* head); |
28 | 34 |
29 // Copies the given addrinfo rather than adopting it. If |recursive| is true, | 35 // Copies the given addrinfo rather than adopting it. If |recursive| is true, |
30 // all linked struct addrinfos will be copied as well. Otherwise only the head | 36 // all linked struct addrinfos will be copied as well. Otherwise only the head |
31 // will be copied, and the rest of linked entries will be ignored. | 37 // will be copied, and the rest of linked entries will be ignored. |
32 void Copy(const struct addrinfo* head, bool recursive); | 38 void Copy(const struct addrinfo* head, bool recursive); |
33 | 39 |
(...skipping 19 matching lines...) Expand all Loading... |
53 // If the canonical name exists, |*canonical_name| is filled in with the | 59 // If the canonical name exists, |*canonical_name| is filled in with the |
54 // value and true is returned. If it does not exist, |*canonical_name| is | 60 // value and true is returned. If it does not exist, |*canonical_name| is |
55 // not altered and false is returned. | 61 // not altered and false is returned. |
56 // |canonical_name| must be a non-null value. | 62 // |canonical_name| must be a non-null value. |
57 bool GetCanonicalName(std::string* canonical_name) const; | 63 bool GetCanonicalName(std::string* canonical_name) const; |
58 | 64 |
59 // Clears all data from this address list. This leaves the list in the same | 65 // Clears all data from this address list. This leaves the list in the same |
60 // empty state as when first constructed. | 66 // empty state as when first constructed. |
61 void Reset(); | 67 void Reset(); |
62 | 68 |
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 | |
77 // Get access to the head of the addrinfo list. | 69 // Get access to the head of the addrinfo list. |
78 const struct addrinfo* head() const { return data_->head; } | 70 const struct addrinfo* head() const { return data_->head; } |
79 | 71 |
80 private: | 72 private: |
81 struct Data : public base::RefCountedThreadSafe<Data> { | 73 struct Data : public base::RefCountedThreadSafe<Data> { |
82 Data(struct addrinfo* ai, bool is_system_created); | 74 Data(struct addrinfo* ai, bool is_system_created); |
83 struct addrinfo* head; | 75 struct addrinfo* head; |
84 | 76 |
85 // Indicates which free function to use for |head|. | 77 // Indicates which free function to use for |head|. |
86 bool is_system_created; | 78 bool is_system_created; |
87 | 79 |
88 private: | 80 private: |
89 friend class base::RefCountedThreadSafe<Data>; | 81 friend class base::RefCountedThreadSafe<Data>; |
90 | 82 |
91 ~Data(); | 83 ~Data(); |
92 }; | 84 }; |
93 | 85 |
94 explicit AddressList(Data* data) : data_(data) {} | 86 explicit AddressList(Data* data) : data_(data) {} |
95 | 87 |
96 scoped_refptr<Data> data_; | 88 scoped_refptr<Data> data_; |
97 }; | 89 }; |
98 | 90 |
99 } // namespace net | 91 } // namespace net |
100 | 92 |
101 #endif // NET_BASE_ADDRESS_LIST_H_ | 93 #endif // NET_BASE_ADDRESS_LIST_H_ |
OLD | NEW |