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

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: Switch a few remaining call-sites. Created 8 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) 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 return PP_NETADDRESSFAMILY_IPV6; 63 return PP_NETADDRESSFAMILY_IPV6;
64 default: 64 default:
65 return PP_NETADDRESSFAMILY_UNSPECIFIED; 65 return PP_NETADDRESSFAMILY_UNSPECIFIED;
66 } 66 }
67 } 67 }
68 68
69 uint16_t GetPort(const PP_NetAddress_Private* addr) { 69 uint16_t GetPort(const PP_NetAddress_Private* addr) {
70 switch (GetFamilyInternal(addr)) { 70 switch (GetFamilyInternal(addr)) {
71 case AF_INET: { 71 case AF_INET: {
72 const sockaddr_in* a = reinterpret_cast<const sockaddr_in*>(addr->data); 72 const sockaddr_in* a = reinterpret_cast<const sockaddr_in*>(addr->data);
73 return ntohs(a->sin_port); 73 return base::NetToHost16(a->sin_port);
74 } 74 }
75 case AF_INET6: { 75 case AF_INET6: {
76 const sockaddr_in6* a = reinterpret_cast<const sockaddr_in6*>(addr->data); 76 const sockaddr_in6* a = reinterpret_cast<const sockaddr_in6*>(addr->data);
77 return ntohs(a->sin6_port); 77 return base::NetToHost16(a->sin6_port);
78 } 78 }
79 default: 79 default:
80 return 0; 80 return 0;
81 } 81 }
82 } 82 }
83 83
84 PP_Bool GetAddress(const PP_NetAddress_Private* addr, 84 PP_Bool GetAddress(const PP_NetAddress_Private* addr,
85 void* address, 85 void* address,
86 uint16_t address_size) { 86 uint16_t address_size) {
87 switch (GetFamilyInternal(addr)) { 87 switch (GetFamilyInternal(addr)) {
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 return PP_FromBool(a1->sin6_port == a2->sin6_port); 161 return PP_FromBool(a1->sin6_port == a2->sin6_port);
162 } 162 }
163 default: 163 default:
164 return PP_FALSE; 164 return PP_FALSE;
165 } 165 }
166 } 166 }
167 167
168 #if defined(OS_WIN) || defined(OS_MACOSX) 168 #if defined(OS_WIN) || defined(OS_MACOSX)
169 std::string ConvertIPv4AddressToString(const sockaddr_in* a, 169 std::string ConvertIPv4AddressToString(const sockaddr_in* a,
170 bool include_port) { 170 bool include_port) {
171 unsigned ip = ntohl(a->sin_addr.s_addr); 171 unsigned ip = base::NetToHost32(a->sin_addr.s_addr);
172 unsigned port = ntohs(a->sin_port); 172 unsigned port = base::NetToHost16(a->sin_port);
173 std::string description = base::StringPrintf( 173 std::string description = base::StringPrintf(
174 "%u.%u.%u.%u", 174 "%u.%u.%u.%u",
175 (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff); 175 (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff);
176 if (include_port) 176 if (include_port)
177 base::StringAppendF(&description, ":%u", port); 177 base::StringAppendF(&description, ":%u", port);
178 return description; 178 return description;
179 } 179 }
180 180
181 // Format an IPv6 address for human consumption, basically according to RFC 181 // Format an IPv6 address for human consumption, basically according to RFC
182 // 5952. 182 // 5952.
183 // - If the scope is nonzero, it is appended to the address as "%<scope>" (this 183 // - If the scope is nonzero, it is appended to the address as "%<scope>" (this
184 // is not in RFC 5952, but consistent with |getnameinfo()| on Linux and 184 // is not in RFC 5952, but consistent with |getnameinfo()| on Linux and
185 // Windows). 185 // Windows).
186 // - If |include_port| is true, the address (possibly including the scope) is 186 // - If |include_port| is true, the address (possibly including the scope) is
187 // enclosed in square brackets and ":<port>" is appended, i.e., the overall 187 // enclosed in square brackets and ":<port>" is appended, i.e., the overall
188 // format is "[<address>]:<port>". 188 // format is "[<address>]:<port>".
189 // - If the address is an IPv4 address embedded IPv6 (per RFC 4291), then the 189 // - If the address is an IPv4 address embedded IPv6 (per RFC 4291), then the
190 // mixed format is used, e.g., "::ffff:192.168.1.2". This is optional per RFC 190 // mixed format is used, e.g., "::ffff:192.168.1.2". This is optional per RFC
191 // 5952, but consistent with |getnameinfo()|. 191 // 5952, but consistent with |getnameinfo()|.
192 std::string ConvertIPv6AddressToString(const sockaddr_in6* a, 192 std::string ConvertIPv6AddressToString(const sockaddr_in6* a,
193 bool include_port) { 193 bool include_port) {
194 unsigned port = ntohs(a->sin6_port); 194 unsigned port = base::NetToHost16(a->sin6_port);
195 unsigned scope = a->sin6_scope_id; 195 unsigned scope = a->sin6_scope_id;
196 std::string description(include_port ? "[" : ""); 196 std::string description(include_port ? "[" : "");
197 197
198 // IPv4 address embedded in IPv6. 198 // IPv4 address embedded in IPv6.
199 if (a->sin6_addr.s6_addr16[0] == 0 && a->sin6_addr.s6_addr16[1] == 0 && 199 if (a->sin6_addr.s6_addr16[0] == 0 && a->sin6_addr.s6_addr16[1] == 0 &&
200 a->sin6_addr.s6_addr16[2] == 0 && a->sin6_addr.s6_addr16[3] == 0 && 200 a->sin6_addr.s6_addr16[2] == 0 && a->sin6_addr.s6_addr16[3] == 0 &&
201 a->sin6_addr.s6_addr16[4] == 0 && 201 a->sin6_addr.s6_addr16[4] == 0 &&
202 (a->sin6_addr.s6_addr16[5] == 0 || a->sin6_addr.s6_addr16[5] == 0xffff)) { 202 (a->sin6_addr.s6_addr16[5] == 0 || a->sin6_addr.s6_addr16[5] == 0xffff)) {
203 base::StringAppendF( 203 base::StringAppendF(
204 &description, 204 &description,
205 a->sin6_addr.s6_addr16[5] == 0 ? "::%u.%u.%u.%u" : "::ffff:%u.%u.%u.%u", 205 a->sin6_addr.s6_addr16[5] == 0 ? "::%u.%u.%u.%u" : "::ffff:%u.%u.%u.%u",
206 static_cast<unsigned>(a->sin6_addr.s6_addr[12]), 206 static_cast<unsigned>(a->sin6_addr.s6_addr[12]),
207 static_cast<unsigned>(a->sin6_addr.s6_addr[13]), 207 static_cast<unsigned>(a->sin6_addr.s6_addr[13]),
208 static_cast<unsigned>(a->sin6_addr.s6_addr[14]), 208 static_cast<unsigned>(a->sin6_addr.s6_addr[14]),
209 static_cast<unsigned>(a->sin6_addr.s6_addr[15])); 209 static_cast<unsigned>(a->sin6_addr.s6_addr[15]));
210 210
211 // "Real" IPv6 addresses. 211 // "Real" IPv6 addresses.
212 } else { 212 } else {
213 // Find the first longest run of 0s (of length > 1), to collapse to "::". 213 // Find the first longest run of 0s (of length > 1), to collapse to "::".
214 int longest_start = 0; 214 int longest_start = 0;
215 int longest_length = 0; 215 int longest_length = 0;
216 int curr_start = 0; 216 int curr_start = 0;
217 int curr_length = 0; 217 int curr_length = 0;
218 for (int i = 0; i < 8; i++) { 218 for (int i = 0; i < 8; i++) {
219 if (ntohs(a->sin6_addr.s6_addr16[i]) != 0) { 219 if (base::NetToHost16(a->sin6_addr.s6_addr16[i]) != 0) {
220 curr_length = 0; 220 curr_length = 0;
221 } else { 221 } else {
222 if (!curr_length) 222 if (!curr_length)
223 curr_start = i; 223 curr_start = i;
224 curr_length++; 224 curr_length++;
225 if (curr_length > longest_length) { 225 if (curr_length > longest_length) {
226 longest_start = curr_start; 226 longest_start = curr_start;
227 longest_length = curr_length; 227 longest_length = curr_length;
228 } 228 }
229 } 229 }
230 } 230 }
231 231
232 bool need_sep = false; // Whether the next item needs a ':' to separate. 232 bool need_sep = false; // Whether the next item needs a ':' to separate.
233 for (int i = 0; i < 8;) { 233 for (int i = 0; i < 8;) {
234 if (longest_length > 1 && i == longest_start) { 234 if (longest_length > 1 && i == longest_start) {
235 description.append("::"); 235 description.append("::");
236 need_sep = false; 236 need_sep = false;
237 i += longest_length; 237 i += longest_length;
238 } else { 238 } else {
239 unsigned v = ntohs(a->sin6_addr.s6_addr16[i]); 239 unsigned v = base::NetToHost16(a->sin6_addr.s6_addr16[i]);
240 base::StringAppendF(&description, need_sep ? ":%x" : "%x", v); 240 base::StringAppendF(&description, need_sep ? ":%x" : "%x", v);
241 need_sep = true; 241 need_sep = true;
242 i++; 242 i++;
243 } 243 }
244 } 244 }
245 } 245 }
246 246
247 // Nonzero scopes, e.g., 123, are indicated by appending, e.g., "%123". 247 // Nonzero scopes, e.g., 123, are indicated by appending, e.g., "%123".
248 if (scope != 0) 248 if (scope != 0)
249 base::StringAppendF(&description, "%%%u", scope); 249 base::StringAppendF(&description, "%%%u", scope);
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 295
296 PP_Bool ReplacePort(const struct PP_NetAddress_Private* src_addr, 296 PP_Bool ReplacePort(const struct PP_NetAddress_Private* src_addr,
297 uint16_t port, 297 uint16_t port,
298 struct PP_NetAddress_Private* dest_addr) { 298 struct PP_NetAddress_Private* dest_addr) {
299 if (!NetAddressPrivateImpl::ValidateNetAddress(*src_addr)) 299 if (!NetAddressPrivateImpl::ValidateNetAddress(*src_addr))
300 return PP_FALSE; 300 return PP_FALSE;
301 301
302 switch (GetFamilyInternal(src_addr)) { 302 switch (GetFamilyInternal(src_addr)) {
303 case AF_INET: { 303 case AF_INET: {
304 memmove(dest_addr, src_addr, sizeof(*src_addr)); 304 memmove(dest_addr, src_addr, sizeof(*src_addr));
305 reinterpret_cast<sockaddr_in*>(dest_addr->data)->sin_port = htons(port); 305 reinterpret_cast<sockaddr_in*>(dest_addr->data)->sin_port =
306 base::HostToNet16(port);
306 return PP_TRUE; 307 return PP_TRUE;
307 } 308 }
308 case AF_INET6: { 309 case AF_INET6: {
309 memmove(dest_addr, src_addr, sizeof(*src_addr)); 310 memmove(dest_addr, src_addr, sizeof(*src_addr));
310 reinterpret_cast<sockaddr_in6*>(dest_addr->data)->sin6_port = htons(port); 311 reinterpret_cast<sockaddr_in6*>(dest_addr->data)->sin6_port =
312 base::HostToNet16(port);
311 return PP_TRUE; 313 return PP_TRUE;
312 } 314 }
313 default: 315 default:
314 return PP_FALSE; 316 return PP_FALSE;
315 } 317 }
316 } 318 }
317 319
318 void GetAnyAddress(PP_Bool is_ipv6, PP_NetAddress_Private* addr) { 320 void GetAnyAddress(PP_Bool is_ipv6, PP_NetAddress_Private* addr) {
319 memset(addr->data, 0, arraysize(addr->data) * sizeof(addr->data[0])); 321 memset(addr->data, 0, arraysize(addr->data) * sizeof(addr->data[0]));
320 if (is_ipv6) { 322 if (is_ipv6) {
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
453 net::IPEndPoint ip_end_point; 455 net::IPEndPoint ip_end_point;
454 if (!NetAddressToIPEndPoint(net_addr, &ip_end_point)) 456 if (!NetAddressToIPEndPoint(net_addr, &ip_end_point))
455 return false; 457 return false;
456 458
457 *address_list = net::AddressList::CreateFromIPAddress(ip_end_point.address(), 459 *address_list = net::AddressList::CreateFromIPAddress(ip_end_point.address(),
458 ip_end_point.port()); 460 ip_end_point.port());
459 return true; 461 return true;
460 } 462 }
461 463
462 } // namespace ppapi 464 } // namespace ppapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698