Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: net/base/address_list.cc

Issue 6598040: Add constructor for creating an AddressList from a struct sockaddr. This is... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #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/net_util.h" 10 #include "net/base/net_util.h"
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 } 141 }
142 142
143 if (canonicalize_name) { 143 if (canonicalize_name) {
144 std::string name = NetAddressToString(ai); 144 std::string name = NetAddressToString(ai);
145 ai->ai_canonname = do_strdup(name.c_str()); 145 ai->ai_canonname = do_strdup(name.c_str());
146 } 146 }
147 data_ = new Data(ai, false /*is_system_created*/); 147 data_ = new Data(ai, false /*is_system_created*/);
148 SetPort(port); 148 SetPort(port);
149 } 149 }
150 150
151 AddressList::AddressList(struct sockaddr* address,
152 socklen_t address_length,
153 int socket_type,
154 int protocol) {
155 // Do sanity checking on socket_type and protocol.
156 DCHECK(socket_type == SOCK_DGRAM || socket_type == SOCK_STREAM);
157 DCHECK(protocol == IPPROTO_TCP || protocol == IPPROTO_UDP);
158
159 struct addrinfo* ai = new addrinfo;
160 memset(ai, 0, sizeof(addrinfo));
161 switch (address_length) {
162 case sizeof(struct sockaddr_in):
163 {
164 struct sockaddr_in* sin =
165 reinterpret_cast<struct sockaddr_in*>(address);
166 ai->ai_family = sin->sin_family;
167 DCHECK_EQ(AF_INET, ai->ai_family);
168 }
169 break;
170 case sizeof(struct sockaddr_in6):
171 {
172 struct sockaddr_in6* sin6 =
173 reinterpret_cast<struct sockaddr_in6*>(address);
174 ai->ai_family = sin6->sin6_family;
175 DCHECK_EQ(AF_INET6, ai->ai_family);
176 }
177 break;
178 default:
179 NOTREACHED() << "Bad IP address";
180 break;
181 }
182 ai->ai_socktype = socket_type;
183 ai->ai_protocol = protocol;
184 ai->ai_addrlen = address_length;
185 ai->ai_addr = reinterpret_cast<struct sockaddr*>(new char[address_length]);
186 memcpy(ai->ai_addr, address, address_length);
187 data_ = new Data(ai, false /*is_system_created*/);
188 }
189
151 AddressList::AddressList(const AddressList& addresslist) 190 AddressList::AddressList(const AddressList& addresslist)
152 : data_(addresslist.data_) { 191 : data_(addresslist.data_) {
153 } 192 }
154 193
155 AddressList::~AddressList() { 194 AddressList::~AddressList() {
156 } 195 }
157 196
158 AddressList& AddressList::operator=(const AddressList& addresslist) { 197 AddressList& AddressList::operator=(const AddressList& addresslist) {
159 data_ = addresslist.data_; 198 data_ = addresslist.data_;
160 return *this; 199 return *this;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 return false; 251 return false;
213 canonical_name->assign(data_->head->ai_canonname); 252 canonical_name->assign(data_->head->ai_canonname);
214 return true; 253 return true;
215 } 254 }
216 255
217 void AddressList::Reset() { 256 void AddressList::Reset() {
218 data_ = NULL; 257 data_ = NULL;
219 } 258 }
220 259
221 const struct addrinfo* AddressList::head() const { 260 const struct addrinfo* AddressList::head() const {
261 if (!data_)
262 return NULL;
eroman 2011/03/01 02:52:33 under what circumstance is this valid? Seems like
Mike Belshe 2011/03/01 19:01:33 AddressList foo; if (foo->head()) blah blah Th
eroman 2011/03/01 22:11:53 I disagree. I think it is wrong for code to be in
222 return data_->head; 263 return data_->head;
223 } 264 }
224 265
225 AddressList::AddressList(Data* data) : data_(data) {} 266 AddressList::AddressList(Data* data) : data_(data) {}
226 267
227 AddressList::Data::Data(struct addrinfo* ai, bool is_system_created) 268 AddressList::Data::Data(struct addrinfo* ai, bool is_system_created)
228 : head(ai), is_system_created(is_system_created) { 269 : head(ai), is_system_created(is_system_created) {
229 DCHECK(head); 270 DCHECK(head);
230 } 271 }
231 272
232 AddressList::Data::~Data() { 273 AddressList::Data::~Data() {
233 // Call either freeaddrinfo(head), or FreeMyAddrinfo(head), depending who 274 // Call either freeaddrinfo(head), or FreeMyAddrinfo(head), depending who
234 // created the data. 275 // created the data.
235 if (is_system_created) 276 if (is_system_created)
236 freeaddrinfo(head); 277 freeaddrinfo(head);
237 else 278 else
238 FreeMyAddrinfo(head); 279 FreeMyAddrinfo(head);
239 } 280 }
240 281
241 } // namespace net 282 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698