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

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
« no previous file with comments | « net/base/address_list.h ('k') | net/base/address_list_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 return false; 212 return false;
213 canonical_name->assign(data_->head->ai_canonname); 213 canonical_name->assign(data_->head->ai_canonname);
214 return true; 214 return true;
215 } 215 }
216 216
217 void AddressList::Reset() { 217 void AddressList::Reset() {
218 data_ = NULL; 218 data_ = NULL;
219 } 219 }
220 220
221 const struct addrinfo* AddressList::head() const { 221 const struct addrinfo* AddressList::head() const {
222 if (!data_)
223 return NULL;
222 return data_->head; 224 return data_->head;
223 } 225 }
224 226
225 AddressList::AddressList(Data* data) : data_(data) {} 227 AddressList::AddressList(Data* data) : data_(data) {}
226 228
229 // static
230 AddressList* AddressList::CreateAddressListFromSockaddr(
231 const struct sockaddr* address,
232 socklen_t address_length,
233 int socket_type,
234 int protocol) {
235 // Do sanity checking on socket_type and protocol.
236 DCHECK(socket_type == SOCK_DGRAM || socket_type == SOCK_STREAM);
237 DCHECK(protocol == IPPROTO_TCP || protocol == IPPROTO_UDP);
238
239 struct addrinfo* ai = new addrinfo;
240 memset(ai, 0, sizeof(addrinfo));
241 switch (address_length) {
242 case sizeof(struct sockaddr_in):
243 {
244 const struct sockaddr_in* sin =
245 reinterpret_cast<const struct sockaddr_in*>(address);
246 ai->ai_family = sin->sin_family;
247 DCHECK_EQ(AF_INET, ai->ai_family);
248 }
249 break;
250 case sizeof(struct sockaddr_in6):
251 {
252 const struct sockaddr_in6* sin6 =
253 reinterpret_cast<const struct sockaddr_in6*>(address);
254 ai->ai_family = sin6->sin6_family;
255 DCHECK_EQ(AF_INET6, ai->ai_family);
256 }
257 break;
258 default:
259 NOTREACHED() << "Bad IP address";
260 break;
261 }
262 ai->ai_socktype = socket_type;
263 ai->ai_protocol = protocol;
264 ai->ai_addrlen = address_length;
265 ai->ai_addr = reinterpret_cast<struct sockaddr*>(new char[address_length]);
266 memcpy(ai->ai_addr, address, address_length);
267 return new AddressList(new Data(ai, false /*is_system_created*/));
268 }
269
270
227 AddressList::Data::Data(struct addrinfo* ai, bool is_system_created) 271 AddressList::Data::Data(struct addrinfo* ai, bool is_system_created)
228 : head(ai), is_system_created(is_system_created) { 272 : head(ai), is_system_created(is_system_created) {
229 DCHECK(head); 273 DCHECK(head);
230 } 274 }
231 275
232 AddressList::Data::~Data() { 276 AddressList::Data::~Data() {
233 // Call either freeaddrinfo(head), or FreeMyAddrinfo(head), depending who 277 // Call either freeaddrinfo(head), or FreeMyAddrinfo(head), depending who
234 // created the data. 278 // created the data.
235 if (is_system_created) 279 if (is_system_created)
236 freeaddrinfo(head); 280 freeaddrinfo(head);
237 else 281 else
238 FreeMyAddrinfo(head); 282 FreeMyAddrinfo(head);
239 } 283 }
240 284
241 } // namespace net 285 } // namespace net
OLDNEW
« no previous file with comments | « net/base/address_list.h ('k') | net/base/address_list_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698