Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 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 "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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 | 81 |
| 82 if (GetFamily(*addr1) == AF_INET6) { | 82 if (GetFamily(*addr1) == AF_INET6) { |
| 83 const sockaddr_in6* a1 = reinterpret_cast<const sockaddr_in6*>(addr1->data); | 83 const sockaddr_in6* a1 = reinterpret_cast<const sockaddr_in6*>(addr1->data); |
| 84 const sockaddr_in6* a2 = reinterpret_cast<const sockaddr_in6*>(addr2->data); | 84 const sockaddr_in6* a2 = reinterpret_cast<const sockaddr_in6*>(addr2->data); |
| 85 return PP_FromBool(a1->sin6_port == a2->sin6_port); | 85 return PP_FromBool(a1->sin6_port == a2->sin6_port); |
| 86 } | 86 } |
| 87 | 87 |
| 88 return PP_FALSE; | 88 return PP_FALSE; |
| 89 } | 89 } |
| 90 | 90 |
| 91 #if defined(OS_WIN) | |
| 92 namespace { | |
| 93 | |
| 94 std::string ConvertIPv4AddressToString(const sockaddr_in* a, | |
| 95 bool include_port) { | |
| 96 unsigned ip = ntohl(a->sin_addr.s_addr); | |
| 97 unsigned port = ntohs(a->sin_port); | |
| 98 std::string description = base::StringPrintf( | |
| 99 "%u.%u.%u.%u", | |
| 100 (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff); | |
| 101 if (include_port) | |
| 102 base::StringAppendF(&description, ":%u", port); | |
| 103 return description; | |
| 104 } | |
| 105 | |
| 106 std::string ConvertIPv6AddressToString(const sockaddr_in6* a, | |
| 107 bool include_port) { | |
| 108 unsigned port = ntohs(a->sin6_port); | |
| 109 unsigned scope = a->sin6_scope_id; | |
|
yzshen1
2011/11/17 18:17:56
It is also in network-byte-order, isn't it?
| |
| 110 std::string description(include_port ? "[" : ""); | |
| 111 | |
| 112 // Find the first longest run of 0s (of length > 1), to collapse to "::". | |
| 113 int longest_start = 0; | |
| 114 int longest_length = 0; | |
| 115 int curr_start = 0; | |
| 116 int curr_length = 0; | |
| 117 for (int i = 0; i < 8; i++) { | |
| 118 if (ntohs(a->sin6_addr.s6_addr16[i]) != 0) { | |
| 119 curr_length = 0; | |
| 120 continue; | |
| 121 } | |
| 122 if (!curr_length) | |
| 123 curr_start = i; | |
| 124 curr_length++; | |
| 125 if (curr_length > longest_length) { | |
| 126 longest_start = curr_start; | |
| 127 longest_length = curr_length; | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 bool need_sep = false; // Whether the next item needs a ':' to separate. | |
| 132 for (int i = 0; i < 8;) { | |
| 133 if (longest_length > 1 && i == longest_start) { | |
| 134 description.append("::"); | |
| 135 need_sep = false; | |
| 136 i += longest_length; | |
| 137 } else { | |
| 138 unsigned v = ntohs(a->sin6_addr.s6_addr16[i]); | |
| 139 base::StringAppendF(&description, need_sep ? ":%x" : "%x", v); | |
| 140 need_sep = true; | |
| 141 i++; | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 // Nonzero scopes, e.g., 123, are indicated by appending, e.g., "%123". | |
| 146 if (scope != 0) | |
| 147 base::StringAppendF(&description, "%%%u", scope); | |
| 148 | |
| 149 if (include_port) | |
| 150 base::StringAppendF(&description, "]:%u", port); | |
| 151 | |
| 152 return description; | |
| 153 } | |
| 154 | |
| 155 } // namespace | |
| 156 #endif // OS_WIN | |
| 157 | |
| 91 PP_Var Describe(PP_Module module, | 158 PP_Var Describe(PP_Module module, |
| 92 const struct PP_NetAddress_Private* addr, | 159 const struct PP_NetAddress_Private* addr, |
| 93 PP_Bool include_port) { | 160 PP_Bool include_port) { |
| 94 if (!NetAddressPrivateImpl::ValidateNetAddress(*addr)) | 161 if (!NetAddressPrivateImpl::ValidateNetAddress(*addr)) |
| 95 return PP_MakeUndefined(); | 162 return PP_MakeUndefined(); |
| 96 | 163 |
| 97 #if defined(OS_WIN) | 164 #if defined(OS_WIN) |
| 98 // On Windows, |NetAddressToString()| doesn't work in the sandbox. | 165 // On Windows, |NetAddressToString()| doesn't work in the sandbox. |
| 99 // TODO(viettrungluu): Consider switching to this everywhere once it's fully | 166 // TODO(viettrungluu): Consider switching to this everywhere once it's fully |
| 100 // implemented. | 167 // implemented. |
| 101 switch (GetFamily(*addr)) { | 168 switch (GetFamily(*addr)) { |
| 102 case AF_INET: { | 169 case AF_INET: { |
| 103 const sockaddr_in* a = reinterpret_cast<const sockaddr_in*>(addr->data); | 170 const sockaddr_in* a = reinterpret_cast<const sockaddr_in*>(addr->data); |
| 104 unsigned ip = ntohl(a->sin_addr.s_addr); | 171 return StringVar::StringToPPVar( |
| 105 unsigned port = ntohs(a->sin_port); | 172 module, ConvertIPv4AddressToString(a, include_port)); |
| 106 std::string description = base::StringPrintf( | |
| 107 "%u.%u.%u.%u", | |
| 108 (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff); | |
| 109 if (include_port) | |
| 110 description.append(base::StringPrintf(":%u", port)); | |
| 111 return StringVar::StringToPPVar(module, description); | |
| 112 } | 173 } |
| 113 case AF_INET6: | 174 case AF_INET6: { |
| 114 // TODO(viettrungluu): crbug.com/103969 | 175 const sockaddr_in6* a = reinterpret_cast<const sockaddr_in6*>(addr->data); |
| 115 NOTIMPLEMENTED(); | 176 return StringVar::StringToPPVar( |
| 116 break; | 177 module, ConvertIPv6AddressToString(a, include_port)); |
| 178 } | |
| 117 default: | 179 default: |
| 118 NOTREACHED(); | 180 NOTREACHED(); |
| 119 break; | 181 break; |
| 120 } | 182 } |
| 121 return PP_MakeUndefined(); | 183 return PP_MakeUndefined(); |
| 122 #else | 184 #else |
| 123 const sockaddr* a = reinterpret_cast<const sockaddr*>(addr->data); | 185 const sockaddr* a = reinterpret_cast<const sockaddr*>(addr->data); |
| 124 socklen_t l = addr->size; | 186 socklen_t l = addr->size; |
| 125 std::string description = | 187 std::string description = |
| 126 include_port ? net::NetAddressToStringWithPort(a, l) : | 188 include_port ? net::NetAddressToStringWithPort(a, l) : |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 197 | 259 |
| 198 // Ditto for AF_INET6. | 260 // Ditto for AF_INET6. |
| 199 if (GetFamily(addr) == AF_INET6 && addr.size >= sizeof(sockaddr_in6)) | 261 if (GetFamily(addr) == AF_INET6 && addr.size >= sizeof(sockaddr_in6)) |
| 200 return true; | 262 return true; |
| 201 | 263 |
| 202 // Reject everything else. | 264 // Reject everything else. |
| 203 return false; | 265 return false; |
| 204 } | 266 } |
| 205 | 267 |
| 206 } // namespace ppapi | 268 } // namespace ppapi |
| OLD | NEW |