OLD | NEW |
---|---|
1 // Copyright (c) 2006-2008 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 #include "net/base/address_list.h" | 5 #include "net/base/address_list.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "net/base/sys_addrinfo.h" | 10 #include "net/base/sys_addrinfo.h" |
11 | 11 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
98 | 98 |
99 void AddressList::Adopt(struct addrinfo* head) { | 99 void AddressList::Adopt(struct addrinfo* head) { |
100 data_ = new Data(head, true /*is_system_created*/); | 100 data_ = new Data(head, true /*is_system_created*/); |
101 } | 101 } |
102 | 102 |
103 void AddressList::Copy(const struct addrinfo* head, bool recursive) { | 103 void AddressList::Copy(const struct addrinfo* head, bool recursive) { |
104 data_ = new Data(CreateCopyOfAddrinfo(head, recursive), | 104 data_ = new Data(CreateCopyOfAddrinfo(head, recursive), |
105 false /*is_system_created*/); | 105 false /*is_system_created*/); |
106 } | 106 } |
107 | 107 |
108 void AddressList::Append(const struct addrinfo* head) { | 108 void AddressList::Append(const struct addrinfo* head) { |
wtc
2010/04/08 15:09:40
Since we assume ai_cannonname is in the first elem
eroman
2010/04/08 19:10:50
Append() was only exposed for use by a unittest (h
wtc
2010/04/08 20:11:12
This sounds good.
| |
109 struct addrinfo* new_head; | 109 struct addrinfo* new_head; |
110 if (data_->is_system_created) { | 110 if (data_->is_system_created) { |
111 new_head = CreateCopyOfAddrinfo(data_->head, true); | 111 new_head = CreateCopyOfAddrinfo(data_->head, true); |
112 data_ = new Data(new_head, false /*is_system_created*/); | 112 data_ = new Data(new_head, false /*is_system_created*/); |
113 } else { | 113 } else { |
114 new_head = data_->head; | 114 new_head = data_->head; |
115 } | 115 } |
116 | 116 |
117 // Find the end of current linked list and append new data there. | 117 // Find the end of current linked list and append new data there. |
118 struct addrinfo* copy_ptr = new_head; | 118 struct addrinfo* copy_ptr = new_head; |
119 while (copy_ptr->ai_next) | 119 while (copy_ptr->ai_next) |
120 copy_ptr = copy_ptr->ai_next; | 120 copy_ptr = copy_ptr->ai_next; |
121 copy_ptr->ai_next = CreateCopyOfAddrinfo(head, true); | 121 copy_ptr->ai_next = CreateCopyOfAddrinfo(head, true); |
122 } | 122 } |
123 | 123 |
124 void AddressList::SetPort(int port) { | 124 void AddressList::SetPort(int port) { |
125 SetPortRecursive(data_->head, port); | 125 SetPortRecursive(data_->head, port); |
126 } | 126 } |
127 | 127 |
128 int AddressList::GetPort() const { | 128 int AddressList::GetPort() const { |
129 uint16* port_field = GetPortField(data_->head); | 129 uint16* port_field = GetPortField(data_->head); |
130 if (!port_field) | 130 if (!port_field) |
131 return -1; | 131 return -1; |
132 | 132 |
133 return ntohs(*port_field); | 133 return ntohs(*port_field); |
134 } | 134 } |
135 | 135 |
136 bool AddressList::GetCanonicalName(std::string* canonical_name) const { | |
137 DCHECK(canonical_name); | |
138 if (!data_->head || !data_->head->ai_canonname) | |
139 return false; | |
140 canonical_name->assign(data_->head->ai_canonname); | |
141 return true; | |
142 } | |
143 | |
136 void AddressList::SetFrom(const AddressList& src, int port) { | 144 void AddressList::SetFrom(const AddressList& src, int port) { |
137 if (src.GetPort() == port) { | 145 if (src.GetPort() == port) { |
138 // We can reference the data from |src| directly. | 146 // We can reference the data from |src| directly. |
139 *this = src; | 147 *this = src; |
140 } else { | 148 } else { |
141 // Otherwise we need to make a copy in order to change the port number. | 149 // Otherwise we need to make a copy in order to change the port number. |
142 Copy(src.head(), true); | 150 Copy(src.head(), true); |
143 SetPort(port); | 151 SetPort(port); |
144 } | 152 } |
145 } | 153 } |
(...skipping 25 matching lines...) Expand all Loading... | |
171 AddressList::Data::~Data() { | 179 AddressList::Data::~Data() { |
172 // Call either freeaddrinfo(head), or FreeMyAddrinfo(head), depending who | 180 // Call either freeaddrinfo(head), or FreeMyAddrinfo(head), depending who |
173 // created the data. | 181 // created the data. |
174 if (is_system_created) | 182 if (is_system_created) |
175 freeaddrinfo(head); | 183 freeaddrinfo(head); |
176 else | 184 else |
177 FreeMyAddrinfo(head); | 185 FreeMyAddrinfo(head); |
178 } | 186 } |
179 | 187 |
180 } // namespace net | 188 } // namespace net |
OLD | NEW |