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

Side by Side Diff: ppapi/shared_impl/private/net_address_private_impl.cc

Issue 9716020: Add base::HostToNetXX() & NetToHostXX(), and use them to replace htonX() & ntohX() in Chrome. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase. Created 8 years, 8 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/socket/web_socket_server_socket.cc ('k') | sync/util/nigori.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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "ppapi/shared_impl/private/net_address_private_impl.h" 5 #include "ppapi/shared_impl/private/net_address_private_impl.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include <string> 9 #include <string>
10 10
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 return PP_NETADDRESSFAMILY_IPV6; 65 return PP_NETADDRESSFAMILY_IPV6;
66 default: 66 default:
67 return PP_NETADDRESSFAMILY_UNSPECIFIED; 67 return PP_NETADDRESSFAMILY_UNSPECIFIED;
68 } 68 }
69 } 69 }
70 70
71 uint16_t GetPort(const PP_NetAddress_Private* addr) { 71 uint16_t GetPort(const PP_NetAddress_Private* addr) {
72 switch (GetFamilyInternal(addr)) { 72 switch (GetFamilyInternal(addr)) {
73 case AF_INET: { 73 case AF_INET: {
74 const sockaddr_in* a = reinterpret_cast<const sockaddr_in*>(addr->data); 74 const sockaddr_in* a = reinterpret_cast<const sockaddr_in*>(addr->data);
75 return ntohs(a->sin_port); 75 return base::NetToHost16(a->sin_port);
76 } 76 }
77 case AF_INET6: { 77 case AF_INET6: {
78 const sockaddr_in6* a = reinterpret_cast<const sockaddr_in6*>(addr->data); 78 const sockaddr_in6* a = reinterpret_cast<const sockaddr_in6*>(addr->data);
79 return ntohs(a->sin6_port); 79 return base::NetToHost16(a->sin6_port);
80 } 80 }
81 default: 81 default:
82 return 0; 82 return 0;
83 } 83 }
84 } 84 }
85 85
86 PP_Bool GetAddress(const PP_NetAddress_Private* addr, 86 PP_Bool GetAddress(const PP_NetAddress_Private* addr,
87 void* address, 87 void* address,
88 uint16_t address_size) { 88 uint16_t address_size) {
89 switch (GetFamilyInternal(addr)) { 89 switch (GetFamilyInternal(addr)) {
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 return PP_FromBool(a1->sin6_port == a2->sin6_port); 174 return PP_FromBool(a1->sin6_port == a2->sin6_port);
175 } 175 }
176 default: 176 default:
177 return PP_FALSE; 177 return PP_FALSE;
178 } 178 }
179 } 179 }
180 180
181 #if defined(OS_WIN) || defined(OS_MACOSX) 181 #if defined(OS_WIN) || defined(OS_MACOSX)
182 std::string ConvertIPv4AddressToString(const sockaddr_in* a, 182 std::string ConvertIPv4AddressToString(const sockaddr_in* a,
183 bool include_port) { 183 bool include_port) {
184 unsigned ip = ntohl(a->sin_addr.s_addr); 184 unsigned ip = base::NetToHost32(a->sin_addr.s_addr);
185 unsigned port = ntohs(a->sin_port); 185 unsigned port = base::NetToHost16(a->sin_port);
186 std::string description = base::StringPrintf( 186 std::string description = base::StringPrintf(
187 "%u.%u.%u.%u", 187 "%u.%u.%u.%u",
188 (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff); 188 (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff);
189 if (include_port) 189 if (include_port)
190 base::StringAppendF(&description, ":%u", port); 190 base::StringAppendF(&description, ":%u", port);
191 return description; 191 return description;
192 } 192 }
193 193
194 // Format an IPv6 address for human consumption, basically according to RFC 194 // Format an IPv6 address for human consumption, basically according to RFC
195 // 5952. 195 // 5952.
196 // - If the scope is nonzero, it is appended to the address as "%<scope>" (this 196 // - If the scope is nonzero, it is appended to the address as "%<scope>" (this
197 // is not in RFC 5952, but consistent with |getnameinfo()| on Linux and 197 // is not in RFC 5952, but consistent with |getnameinfo()| on Linux and
198 // Windows). 198 // Windows).
199 // - If |include_port| is true, the address (possibly including the scope) is 199 // - If |include_port| is true, the address (possibly including the scope) is
200 // enclosed in square brackets and ":<port>" is appended, i.e., the overall 200 // enclosed in square brackets and ":<port>" is appended, i.e., the overall
201 // format is "[<address>]:<port>". 201 // format is "[<address>]:<port>".
202 // - If the address is an IPv4 address embedded IPv6 (per RFC 4291), then the 202 // - If the address is an IPv4 address embedded IPv6 (per RFC 4291), then the
203 // mixed format is used, e.g., "::ffff:192.168.1.2". This is optional per RFC 203 // mixed format is used, e.g., "::ffff:192.168.1.2". This is optional per RFC
204 // 5952, but consistent with |getnameinfo()|. 204 // 5952, but consistent with |getnameinfo()|.
205 std::string ConvertIPv6AddressToString(const sockaddr_in6* a, 205 std::string ConvertIPv6AddressToString(const sockaddr_in6* a,
206 bool include_port) { 206 bool include_port) {
207 unsigned port = ntohs(a->sin6_port); 207 unsigned port = base::NetToHost16(a->sin6_port);
208 unsigned scope = a->sin6_scope_id; 208 unsigned scope = a->sin6_scope_id;
209 std::string description(include_port ? "[" : ""); 209 std::string description(include_port ? "[" : "");
210 210
211 // IPv4 address embedded in IPv6. 211 // IPv4 address embedded in IPv6.
212 if (a->sin6_addr.s6_addr16[0] == 0 && a->sin6_addr.s6_addr16[1] == 0 && 212 if (a->sin6_addr.s6_addr16[0] == 0 && a->sin6_addr.s6_addr16[1] == 0 &&
213 a->sin6_addr.s6_addr16[2] == 0 && a->sin6_addr.s6_addr16[3] == 0 && 213 a->sin6_addr.s6_addr16[2] == 0 && a->sin6_addr.s6_addr16[3] == 0 &&
214 a->sin6_addr.s6_addr16[4] == 0 && 214 a->sin6_addr.s6_addr16[4] == 0 &&
215 (a->sin6_addr.s6_addr16[5] == 0 || a->sin6_addr.s6_addr16[5] == 0xffff)) { 215 (a->sin6_addr.s6_addr16[5] == 0 || a->sin6_addr.s6_addr16[5] == 0xffff)) {
216 base::StringAppendF( 216 base::StringAppendF(
217 &description, 217 &description,
218 a->sin6_addr.s6_addr16[5] == 0 ? "::%u.%u.%u.%u" : "::ffff:%u.%u.%u.%u", 218 a->sin6_addr.s6_addr16[5] == 0 ? "::%u.%u.%u.%u" : "::ffff:%u.%u.%u.%u",
219 static_cast<unsigned>(a->sin6_addr.s6_addr[12]), 219 static_cast<unsigned>(a->sin6_addr.s6_addr[12]),
220 static_cast<unsigned>(a->sin6_addr.s6_addr[13]), 220 static_cast<unsigned>(a->sin6_addr.s6_addr[13]),
221 static_cast<unsigned>(a->sin6_addr.s6_addr[14]), 221 static_cast<unsigned>(a->sin6_addr.s6_addr[14]),
222 static_cast<unsigned>(a->sin6_addr.s6_addr[15])); 222 static_cast<unsigned>(a->sin6_addr.s6_addr[15]));
223 223
224 // "Real" IPv6 addresses. 224 // "Real" IPv6 addresses.
225 } else { 225 } else {
226 // Find the first longest run of 0s (of length > 1), to collapse to "::". 226 // Find the first longest run of 0s (of length > 1), to collapse to "::".
227 int longest_start = 0; 227 int longest_start = 0;
228 int longest_length = 0; 228 int longest_length = 0;
229 int curr_start = 0; 229 int curr_start = 0;
230 int curr_length = 0; 230 int curr_length = 0;
231 for (int i = 0; i < 8; i++) { 231 for (int i = 0; i < 8; i++) {
232 if (ntohs(a->sin6_addr.s6_addr16[i]) != 0) { 232 if (base::NetToHost16(a->sin6_addr.s6_addr16[i]) != 0) {
233 curr_length = 0; 233 curr_length = 0;
234 } else { 234 } else {
235 if (!curr_length) 235 if (!curr_length)
236 curr_start = i; 236 curr_start = i;
237 curr_length++; 237 curr_length++;
238 if (curr_length > longest_length) { 238 if (curr_length > longest_length) {
239 longest_start = curr_start; 239 longest_start = curr_start;
240 longest_length = curr_length; 240 longest_length = curr_length;
241 } 241 }
242 } 242 }
243 } 243 }
244 244
245 bool need_sep = false; // Whether the next item needs a ':' to separate. 245 bool need_sep = false; // Whether the next item needs a ':' to separate.
246 for (int i = 0; i < 8;) { 246 for (int i = 0; i < 8;) {
247 if (longest_length > 1 && i == longest_start) { 247 if (longest_length > 1 && i == longest_start) {
248 description.append("::"); 248 description.append("::");
249 need_sep = false; 249 need_sep = false;
250 i += longest_length; 250 i += longest_length;
251 } else { 251 } else {
252 unsigned v = ntohs(a->sin6_addr.s6_addr16[i]); 252 unsigned v = base::NetToHost16(a->sin6_addr.s6_addr16[i]);
253 base::StringAppendF(&description, need_sep ? ":%x" : "%x", v); 253 base::StringAppendF(&description, need_sep ? ":%x" : "%x", v);
254 need_sep = true; 254 need_sep = true;
255 i++; 255 i++;
256 } 256 }
257 } 257 }
258 } 258 }
259 259
260 // Nonzero scopes, e.g., 123, are indicated by appending, e.g., "%123". 260 // Nonzero scopes, e.g., 123, are indicated by appending, e.g., "%123".
261 if (scope != 0) 261 if (scope != 0)
262 base::StringAppendF(&description, "%%%u", scope); 262 base::StringAppendF(&description, "%%%u", scope);
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 308
309 PP_Bool ReplacePort(const struct PP_NetAddress_Private* src_addr, 309 PP_Bool ReplacePort(const struct PP_NetAddress_Private* src_addr,
310 uint16_t port, 310 uint16_t port,
311 struct PP_NetAddress_Private* dest_addr) { 311 struct PP_NetAddress_Private* dest_addr) {
312 if (!NetAddressPrivateImpl::ValidateNetAddress(*src_addr)) 312 if (!NetAddressPrivateImpl::ValidateNetAddress(*src_addr))
313 return PP_FALSE; 313 return PP_FALSE;
314 314
315 switch (GetFamilyInternal(src_addr)) { 315 switch (GetFamilyInternal(src_addr)) {
316 case AF_INET: { 316 case AF_INET: {
317 memmove(dest_addr, src_addr, sizeof(*src_addr)); 317 memmove(dest_addr, src_addr, sizeof(*src_addr));
318 reinterpret_cast<sockaddr_in*>(dest_addr->data)->sin_port = htons(port); 318 reinterpret_cast<sockaddr_in*>(dest_addr->data)->sin_port =
319 base::HostToNet16(port);
319 return PP_TRUE; 320 return PP_TRUE;
320 } 321 }
321 case AF_INET6: { 322 case AF_INET6: {
322 memmove(dest_addr, src_addr, sizeof(*src_addr)); 323 memmove(dest_addr, src_addr, sizeof(*src_addr));
323 reinterpret_cast<sockaddr_in6*>(dest_addr->data)->sin6_port = htons(port); 324 reinterpret_cast<sockaddr_in6*>(dest_addr->data)->sin6_port =
325 base::HostToNet16(port);
324 return PP_TRUE; 326 return PP_TRUE;
325 } 327 }
326 default: 328 default:
327 return PP_FALSE; 329 return PP_FALSE;
328 } 330 }
329 } 331 }
330 332
331 void GetAnyAddress(PP_Bool is_ipv6, PP_NetAddress_Private* addr) { 333 void GetAnyAddress(PP_Bool is_ipv6, PP_NetAddress_Private* addr) {
332 memset(addr->data, 0, arraysize(addr->data) * sizeof(addr->data[0])); 334 memset(addr->data, 0, arraysize(addr->data) * sizeof(addr->data[0]));
333 if (is_ipv6) { 335 if (is_ipv6) {
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
511 net::IPEndPoint ip_end_point; 513 net::IPEndPoint ip_end_point;
512 if (!NetAddressToIPEndPoint(net_addr, &ip_end_point)) 514 if (!NetAddressToIPEndPoint(net_addr, &ip_end_point))
513 return false; 515 return false;
514 516
515 *address_list = net::AddressList::CreateFromIPAddress(ip_end_point.address(), 517 *address_list = net::AddressList::CreateFromIPAddress(ip_end_point.address(),
516 ip_end_point.port()); 518 ip_end_point.port());
517 return true; 519 return true;
518 } 520 }
519 521
520 } // namespace ppapi 522 } // namespace ppapi
OLDNEW
« no previous file with comments | « net/socket/web_socket_server_socket.cc ('k') | sync/util/nigori.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698