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

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

Issue 7029049: Merge 84251, 85083, 85188, 85375. (Closed) Base URL: svn://svn.chromium.org/chrome/branches/742/src/
Patch Set: Upload before checkin Created 9 years, 7 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 | « no previous file | net/base/net_util.h » ('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) 2011 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"
11 #include "net/base/sys_addrinfo.h" 11 #include "net/base/sys_addrinfo.h"
12 12
13 namespace net { 13 namespace net {
14 14
15 namespace { 15 namespace {
16 16
17 char* do_strdup(const char* src) { 17 char* do_strdup(const char* src) {
18 #if defined(OS_WIN) 18 #if defined(OS_WIN)
19 return _strdup(src); 19 return _strdup(src);
20 #else 20 #else
21 return strdup(src); 21 return strdup(src);
22 #endif 22 #endif
23 } 23 }
24 24
25 // Make a copy of |info| (the dynamically-allocated parts are copied as well).
26 // If |recursive| is true, chained entries via ai_next are copied too.
27 // Copy returned by this function should be deleted using
28 // DeleteCopyOfAddrinfo(), and NOT freeaddrinfo().
29 struct addrinfo* CreateCopyOfAddrinfo(const struct addrinfo* info,
30 bool recursive) {
31 DCHECK(info);
32 struct addrinfo* copy = new addrinfo;
33
34 // Copy all the fields (some of these are pointers, we will fix that next).
35 memcpy(copy, info, sizeof(addrinfo));
36
37 // ai_canonname is a NULL-terminated string.
38 if (info->ai_canonname) {
39 copy->ai_canonname = do_strdup(info->ai_canonname);
40 }
41
42 // ai_addr is a buffer of length ai_addrlen.
43 if (info->ai_addr) {
44 copy->ai_addr = reinterpret_cast<sockaddr *>(new char[info->ai_addrlen]);
45 memcpy(copy->ai_addr, info->ai_addr, info->ai_addrlen);
46 }
47
48 // Recursive copy.
49 if (recursive && info->ai_next)
50 copy->ai_next = CreateCopyOfAddrinfo(info->ai_next, recursive);
51 else
52 copy->ai_next = NULL;
53
54 return copy;
55 }
56
57 // Free an addrinfo that was created by CreateCopyOfAddrinfo().
58 void FreeMyAddrinfo(struct addrinfo* info) {
59 DCHECK(info);
60 if (info->ai_canonname)
61 free(info->ai_canonname); // Allocated by strdup.
62
63 if (info->ai_addr)
64 delete [] reinterpret_cast<char*>(info->ai_addr);
65
66 struct addrinfo* next = info->ai_next;
67
68 delete info;
69
70 // Recursive free.
71 if (next)
72 FreeMyAddrinfo(next);
73 }
74
75 // Assign the port for all addresses in the list. 25 // Assign the port for all addresses in the list.
76 void SetPortRecursive(struct addrinfo* info, int port) { 26 void SetPortRecursive(struct addrinfo* info, int port) {
77 uint16* port_field = GetPortFieldFromAddrinfo(info); 27 uint16* port_field = GetPortFieldFromAddrinfo(info);
78 if (port_field) 28 if (port_field)
79 *port_field = htons(port); 29 *port_field = htons(port);
80 30
81 // Assign recursively. 31 // Assign recursively.
82 if (info->ai_next) 32 if (info->ai_next)
83 SetPortRecursive(info->ai_next, port); 33 SetPortRecursive(info->ai_next, port);
84 } 34 }
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 return new AddressList(new Data(ai, false /*is_system_created*/)); 226 return new AddressList(new Data(ai, false /*is_system_created*/));
277 } 227 }
278 228
279 229
280 AddressList::Data::Data(struct addrinfo* ai, bool is_system_created) 230 AddressList::Data::Data(struct addrinfo* ai, bool is_system_created)
281 : head(ai), is_system_created(is_system_created) { 231 : head(ai), is_system_created(is_system_created) {
282 DCHECK(head); 232 DCHECK(head);
283 } 233 }
284 234
285 AddressList::Data::~Data() { 235 AddressList::Data::~Data() {
286 // Call either freeaddrinfo(head), or FreeMyAddrinfo(head), depending who 236 // Call either freeaddrinfo(head), or FreeCopyOfAddrinfo(head), depending on
287 // created the data. 237 // who created the data.
288 if (is_system_created) 238 if (is_system_created)
289 freeaddrinfo(head); 239 freeaddrinfo(head);
290 else 240 else
291 FreeMyAddrinfo(head); 241 FreeCopyOfAddrinfo(head);
292 } 242 }
293 243
294 } // namespace net 244 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | net/base/net_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698