OLD | NEW |
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/sys_addrinfo.h" | 10 #include "net/base/sys_addrinfo.h" |
11 | 11 |
12 namespace net { | 12 namespace net { |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
| 16 char* do_strdup(const char* src) { |
| 17 #if defined(OS_WIN) |
| 18 return _strdup(src); |
| 19 #else |
| 20 return strdup(src); |
| 21 #endif |
| 22 } |
| 23 |
16 // Make a copy of |info| (the dynamically-allocated parts are copied as well). | 24 // Make a copy of |info| (the dynamically-allocated parts are copied as well). |
17 // If |recursive| is true, chained entries via ai_next are copied too. | 25 // If |recursive| is true, chained entries via ai_next are copied too. |
18 // Copy returned by this function should be deleted using | 26 // Copy returned by this function should be deleted using |
19 // DeleteCopyOfAddrinfo(), and NOT freeaddrinfo(). | 27 // DeleteCopyOfAddrinfo(), and NOT freeaddrinfo(). |
20 struct addrinfo* CreateCopyOfAddrinfo(const struct addrinfo* info, | 28 struct addrinfo* CreateCopyOfAddrinfo(const struct addrinfo* info, |
21 bool recursive) { | 29 bool recursive) { |
22 DCHECK(info); | 30 DCHECK(info); |
23 struct addrinfo* copy = new addrinfo; | 31 struct addrinfo* copy = new addrinfo; |
24 | 32 |
25 // Copy all the fields (some of these are pointers, we will fix that next). | 33 // Copy all the fields (some of these are pointers, we will fix that next). |
26 memcpy(copy, info, sizeof(addrinfo)); | 34 memcpy(copy, info, sizeof(addrinfo)); |
27 | 35 |
28 // ai_canonname is a NULL-terminated string. | 36 // ai_canonname is a NULL-terminated string. |
29 if (info->ai_canonname) { | 37 if (info->ai_canonname) { |
30 #ifdef OS_WIN | 38 copy->ai_canonname = do_strdup(info->ai_canonname); |
31 copy->ai_canonname = _strdup(info->ai_canonname); | |
32 #else | |
33 copy->ai_canonname = strdup(info->ai_canonname); | |
34 #endif | |
35 } | 39 } |
36 | 40 |
37 // ai_addr is a buffer of length ai_addrlen. | 41 // ai_addr is a buffer of length ai_addrlen. |
38 if (info->ai_addr) { | 42 if (info->ai_addr) { |
39 copy->ai_addr = reinterpret_cast<sockaddr *>(new char[info->ai_addrlen]); | 43 copy->ai_addr = reinterpret_cast<sockaddr *>(new char[info->ai_addrlen]); |
40 memcpy(copy->ai_addr, info->ai_addr, info->ai_addrlen); | 44 memcpy(copy->ai_addr, info->ai_addr, info->ai_addrlen); |
41 } | 45 } |
42 | 46 |
43 // Recursive copy. | 47 // Recursive copy. |
44 if (recursive && info->ai_next) | 48 if (recursive && info->ai_next) |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 Copy(src.head(), true); | 159 Copy(src.head(), true); |
156 SetPort(port); | 160 SetPort(port); |
157 } | 161 } |
158 } | 162 } |
159 | 163 |
160 void AddressList::Reset() { | 164 void AddressList::Reset() { |
161 data_ = NULL; | 165 data_ = NULL; |
162 } | 166 } |
163 | 167 |
164 // static | 168 // static |
165 AddressList AddressList::CreateIPv6Address(unsigned char data[16]) { | 169 AddressList AddressList::CreateIPv4Address(unsigned char data[4], |
| 170 const std::string& canonical_name) { |
166 struct addrinfo* ai = new addrinfo; | 171 struct addrinfo* ai = new addrinfo; |
167 memset(ai, 0, sizeof(addrinfo)); | 172 memset(ai, 0, sizeof(addrinfo)); |
| 173 ai->ai_family = AF_INET; |
| 174 ai->ai_socktype = SOCK_STREAM; |
| 175 const size_t sockaddr_in_size = sizeof(struct sockaddr_in); |
| 176 ai->ai_addrlen = sockaddr_in_size; |
| 177 if (!canonical_name.empty()) |
| 178 ai->ai_canonname = do_strdup(canonical_name.c_str()); |
168 | 179 |
169 ai->ai_family = AF_INET6; | 180 struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*>( |
170 ai->ai_socktype = SOCK_STREAM; | 181 new char[sockaddr_in_size]); |
171 ai->ai_addrlen = sizeof(struct sockaddr_in6); | 182 memset(addr, 0, sockaddr_in_size); |
172 | 183 addr->sin_family = AF_INET; |
173 struct sockaddr_in6* addr6 = reinterpret_cast<struct sockaddr_in6*>( | 184 memcpy(&addr->sin_addr, data, 4); |
174 new char[ai->ai_addrlen]); | 185 ai->ai_addr = reinterpret_cast<struct sockaddr*>(addr); |
175 memset(addr6, 0, sizeof(struct sockaddr_in6)); | |
176 | |
177 ai->ai_addr = reinterpret_cast<struct sockaddr*>(addr6); | |
178 addr6->sin6_family = AF_INET6; | |
179 memcpy(&addr6->sin6_addr, data, 16); | |
180 | 186 |
181 return AddressList(new Data(ai, false /*is_system_created*/)); | 187 return AddressList(new Data(ai, false /*is_system_created*/)); |
182 } | 188 } |
| 189 |
| 190 // static |
| 191 AddressList AddressList::CreateIPv6Address(unsigned char data[16], |
| 192 const std::string& canonical_name) { |
| 193 struct addrinfo* ai = new addrinfo; |
| 194 memset(ai, 0, sizeof(addrinfo)); |
| 195 ai->ai_family = AF_INET6; |
| 196 ai->ai_socktype = SOCK_STREAM; |
| 197 const size_t sockaddr_in6_size = sizeof(struct sockaddr_in6); |
| 198 ai->ai_addrlen = sockaddr_in6_size; |
| 199 if (!canonical_name.empty()) |
| 200 ai->ai_canonname = do_strdup(canonical_name.c_str()); |
| 201 |
| 202 struct sockaddr_in6* addr6 = reinterpret_cast<struct sockaddr_in6*>( |
| 203 new char[sockaddr_in6_size]); |
| 204 memset(addr6, 0, sockaddr_in6_size); |
| 205 addr6->sin6_family = AF_INET6; |
| 206 memcpy(&addr6->sin6_addr, data, 16); |
| 207 ai->ai_addr = reinterpret_cast<struct sockaddr*>(addr6); |
| 208 |
| 209 return AddressList(new Data(ai, false /*is_system_created*/)); |
| 210 } |
183 | 211 |
184 AddressList::Data::Data(struct addrinfo* ai, bool is_system_created) | 212 AddressList::Data::Data(struct addrinfo* ai, bool is_system_created) |
185 : head(ai), is_system_created(is_system_created) { | 213 : head(ai), is_system_created(is_system_created) { |
186 DCHECK(head); | 214 DCHECK(head); |
187 } | 215 } |
188 | 216 |
189 AddressList::Data::~Data() { | 217 AddressList::Data::~Data() { |
190 // Call either freeaddrinfo(head), or FreeMyAddrinfo(head), depending who | 218 // Call either freeaddrinfo(head), or FreeMyAddrinfo(head), depending who |
191 // created the data. | 219 // created the data. |
192 if (is_system_created) | 220 if (is_system_created) |
193 freeaddrinfo(head); | 221 freeaddrinfo(head); |
194 else | 222 else |
195 FreeMyAddrinfo(head); | 223 FreeMyAddrinfo(head); |
196 } | 224 } |
197 | 225 |
198 } // namespace net | 226 } // namespace net |
OLD | NEW |