OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 | 7 |
8 #include "base/ref_counted.h" | 8 #include "base/ref_counted.h" |
9 | 9 |
10 struct addrinfo; | 10 struct addrinfo; |
11 | 11 |
12 namespace net { | 12 namespace net { |
13 | 13 |
14 // An AddressList object contains a linked list of addrinfo structures. This | 14 // An AddressList object contains a linked list of addrinfo structures. This |
15 // class is designed to be copied around by value. | 15 // class is designed to be copied around by value. |
16 class AddressList { | 16 class AddressList { |
17 public: | 17 public: |
| 18 // Constructs an empty address list. |
| 19 AddressList() {} |
| 20 |
18 // Adopt the given addrinfo list in place of the existing one if any. This | 21 // Adopt the given addrinfo list in place of the existing one if any. This |
19 // hands over responsibility for freeing the addrinfo list to the AddressList | 22 // hands over responsibility for freeing the addrinfo list to the AddressList |
20 // object. | 23 // object. |
21 void Adopt(struct addrinfo* head); | 24 void Adopt(struct addrinfo* head); |
22 | 25 |
23 // Copies the given addrinfo rather than adopting it. | 26 // Copies the given addrinfo rather than adopting it. |
24 void Copy(const struct addrinfo* head); | 27 void Copy(const struct addrinfo* head); |
25 | 28 |
26 // Sets the port of all addresses in the list to |port| (that is the | 29 // Sets the port of all addresses in the list to |port| (that is the |
27 // sin[6]_port field for the sockaddrs). | 30 // sin[6]_port field for the sockaddrs). |
28 void SetPort(int port); | 31 void SetPort(int port); |
29 | 32 |
30 // Retrieves the port number of the first sockaddr in the list. (If SetPort() | 33 // Retrieves the port number of the first sockaddr in the list. (If SetPort() |
31 // was previously used on this list, then all the addresses will have this | 34 // was previously used on this list, then all the addresses will have this |
32 // same port number.) | 35 // same port number.) |
33 int GetPort() const; | 36 int GetPort() const; |
34 | 37 |
35 // Sets the address to match |src|, and have each sockaddr's port be |port|. | 38 // Sets the address to match |src|, and have each sockaddr's port be |port|. |
36 // If |src| already has the desired port this operation is cheap (just adds | 39 // If |src| already has the desired port this operation is cheap (just adds |
37 // a reference to |src|'s data.) Otherwise we will make a copy. | 40 // a reference to |src|'s data.) Otherwise we will make a copy. |
38 void SetFrom(const AddressList& src, int port); | 41 void SetFrom(const AddressList& src, int port); |
39 | 42 |
40 // Clears all data from this address list. This leaves the list in the same | 43 // Clears all data from this address list. This leaves the list in the same |
41 // empty state as when first constructed. | 44 // empty state as when first constructed. |
42 void Reset(); | 45 void Reset(); |
43 | 46 |
| 47 // Used by unit-tests to manually set the TCP socket address. |
| 48 static AddressList CreateIPv6Address(unsigned char data[16]); |
| 49 |
44 // Get access to the head of the addrinfo list. | 50 // Get access to the head of the addrinfo list. |
45 const struct addrinfo* head() const { return data_->head; } | 51 const struct addrinfo* head() const { return data_->head; } |
46 | 52 |
47 private: | 53 private: |
48 struct Data : public base::RefCountedThreadSafe<Data> { | 54 struct Data : public base::RefCountedThreadSafe<Data> { |
49 Data(struct addrinfo* ai, bool is_system_created) | 55 Data(struct addrinfo* ai, bool is_system_created) |
50 : head(ai), is_system_created(is_system_created) {} | 56 : head(ai), is_system_created(is_system_created) {} |
51 ~Data(); | 57 ~Data(); |
52 struct addrinfo* head; | 58 struct addrinfo* head; |
53 | 59 |
54 // Indicates which free function to use for |head|. | 60 // Indicates which free function to use for |head|. |
55 bool is_system_created; | 61 bool is_system_created; |
56 }; | 62 }; |
| 63 |
| 64 explicit AddressList(Data* data) : data_(data) {} |
| 65 |
57 scoped_refptr<Data> data_; | 66 scoped_refptr<Data> data_; |
58 }; | 67 }; |
59 | 68 |
60 } // namespace net | 69 } // namespace net |
61 | 70 |
62 #endif // NET_BASE_ADDRESS_LIST_H_ | 71 #endif // NET_BASE_ADDRESS_LIST_H_ |
OLD | NEW |