Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
| 6 #if defined(TARGET_OS_WINDOWS) | 6 #if defined(TARGET_OS_WINDOWS) |
| 7 | 7 |
| 8 #include "bin/builtin.h" | 8 #include "bin/builtin.h" |
| 9 #include "bin/eventhandler.h" | 9 #include "bin/eventhandler.h" |
| 10 #include "bin/file.h" | 10 #include "bin/file.h" |
| 11 #include "bin/log.h" | 11 #include "bin/log.h" |
| 12 #include "bin/socket.h" | 12 #include "bin/socket.h" |
| 13 #include "bin/utils.h" | 13 #include "bin/utils.h" |
| 14 | 14 |
| 15 | 15 |
| 16 namespace dart { | 16 namespace dart { |
| 17 namespace bin { | 17 namespace bin { |
| 18 | 18 |
| 19 SocketAddress::SocketAddress(struct addrinfo* addrinfo) { | 19 SocketAddress::SocketAddress(struct sockaddr* sockaddr) { |
| 20 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); | 20 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
| 21 RawAddr* raw = reinterpret_cast<RawAddr*>(addrinfo->ai_addr); | 21 RawAddr* raw = reinterpret_cast<RawAddr*>(sockaddr); |
| 22 | 22 |
| 23 // Clear the port before calling WSAAddressToString as WSAAddressToString | 23 // Clear the port before calling WSAAddressToString as WSAAddressToString |
| 24 // includes the port in the formatted string. | 24 // includes the port in the formatted string. |
| 25 DWORD len = INET6_ADDRSTRLEN; | 25 DWORD len = INET6_ADDRSTRLEN; |
| 26 int err = WSAAddressToStringA(&raw->addr, | 26 int err = WSAAddressToStringA(&raw->addr, |
| 27 sizeof(RawAddr), | 27 sizeof(RawAddr), |
| 28 NULL, | 28 NULL, |
| 29 as_string_, | 29 as_string_, |
| 30 &len); | 30 &len); |
| 31 | 31 |
| 32 if (err != 0) { | 32 if (err != 0) { |
| 33 as_string_[0] = 0; | 33 as_string_[0] = 0; |
| 34 } | 34 } |
| 35 memmove(reinterpret_cast<void *>(&addr_), | 35 memmove(reinterpret_cast<void *>(&addr_), |
| 36 addrinfo->ai_addr, | 36 sockaddr, |
| 37 addrinfo->ai_addrlen); | 37 SocketAddress::GetAddrLength(raw)); |
| 38 } | 38 } |
| 39 | 39 |
| 40 bool Socket::Initialize() { | 40 bool Socket::Initialize() { |
| 41 static bool socket_initialized = false; | 41 static bool socket_initialized = false; |
| 42 if (socket_initialized) return true; | 42 if (socket_initialized) return true; |
| 43 int err; | 43 int err; |
| 44 WSADATA winsock_data; | 44 WSADATA winsock_data; |
| 45 WORD version_requested = MAKEWORD(2, 2); | 45 WORD version_requested = MAKEWORD(2, 2); |
| 46 err = WSAStartup(version_requested, &winsock_data); | 46 err = WSAStartup(version_requested, &winsock_data); |
| 47 if (err == 0) { | 47 if (err == 0) { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 ClientSocket* client_socket = new ClientSocket(s); | 135 ClientSocket* client_socket = new ClientSocket(s); |
| 136 return reinterpret_cast<intptr_t>(client_socket); | 136 return reinterpret_cast<intptr_t>(client_socket); |
| 137 } | 137 } |
| 138 | 138 |
| 139 | 139 |
| 140 intptr_t Socket::Connect(intptr_t fd, RawAddr addr, const intptr_t port) { | 140 intptr_t Socket::Connect(intptr_t fd, RawAddr addr, const intptr_t port) { |
| 141 ASSERT(reinterpret_cast<Handle*>(fd)->is_socket()); | 141 ASSERT(reinterpret_cast<Handle*>(fd)->is_socket()); |
| 142 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); | 142 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); |
| 143 SOCKET s = handle->socket(); | 143 SOCKET s = handle->socket(); |
| 144 SocketAddress::SetAddrPort(&addr, port); | 144 SocketAddress::SetAddrPort(&addr, port); |
| 145 int status = connect(s, &addr.addr, SocketAddress::GetAddrLength(addr)); | 145 int status = connect(s, &addr.addr, SocketAddress::GetAddrLength(&addr)); |
| 146 if (status == SOCKET_ERROR) { | 146 if (status == SOCKET_ERROR) { |
| 147 DWORD rc = WSAGetLastError(); | 147 DWORD rc = WSAGetLastError(); |
| 148 ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(fd); | 148 ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(fd); |
| 149 client_socket->Close(); | 149 client_socket->Close(); |
| 150 SetLastError(rc); | 150 SetLastError(rc); |
| 151 return -1; | 151 return -1; |
| 152 } | 152 } |
| 153 return fd; | 153 return fd; |
| 154 } | 154 } |
| 155 | 155 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 ListenSocket* listen_socket = reinterpret_cast<ListenSocket*>(fd); | 209 ListenSocket* listen_socket = reinterpret_cast<ListenSocket*>(fd); |
| 210 ClientSocket* client_socket = listen_socket->Accept(); | 210 ClientSocket* client_socket = listen_socket->Accept(); |
| 211 if (client_socket != NULL) { | 211 if (client_socket != NULL) { |
| 212 return reinterpret_cast<intptr_t>(client_socket); | 212 return reinterpret_cast<intptr_t>(client_socket); |
| 213 } else { | 213 } else { |
| 214 return -1; | 214 return -1; |
| 215 } | 215 } |
| 216 } | 216 } |
| 217 | 217 |
| 218 | 218 |
| 219 SocketAddresses* Socket::LookupAddress(const char* host, | 219 AddressList<SocketAddress>* Socket::LookupAddress(const char* host, |
| 220 int type, | 220 int type, |
| 221 OSError** os_error) { | 221 OSError** os_error) { |
| 222 Initialize(); | 222 Initialize(); |
| 223 | 223 |
| 224 // Perform a name lookup for a host name. | 224 // Perform a name lookup for a host name. |
| 225 struct addrinfo hints; | 225 struct addrinfo hints; |
| 226 memset(&hints, 0, sizeof(hints)); | 226 memset(&hints, 0, sizeof(hints)); |
| 227 hints.ai_family = SocketAddress::FromType(type); | 227 hints.ai_family = SocketAddress::FromType(type); |
| 228 hints.ai_socktype = SOCK_STREAM; | 228 hints.ai_socktype = SOCK_STREAM; |
| 229 hints.ai_flags = 0; | 229 hints.ai_flags = 0; |
| 230 hints.ai_protocol = IPPROTO_TCP; | 230 hints.ai_protocol = IPPROTO_TCP; |
| 231 struct addrinfo* info = NULL; | 231 struct addrinfo* info = NULL; |
| 232 int status = getaddrinfo(host, 0, &hints, &info); | 232 int status = getaddrinfo(host, 0, &hints, &info); |
| 233 if (status != 0) { | 233 if (status != 0) { |
| 234 ASSERT(*os_error == NULL); | 234 ASSERT(*os_error == NULL); |
| 235 DWORD error_code = WSAGetLastError(); | 235 DWORD error_code = WSAGetLastError(); |
| 236 SetLastError(error_code); | 236 SetLastError(error_code); |
| 237 *os_error = new OSError(); | 237 *os_error = new OSError(); |
| 238 return NULL; | 238 return NULL; |
| 239 } | 239 } |
| 240 intptr_t count = 0; | 240 intptr_t count = 0; |
| 241 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { | 241 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { |
| 242 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) count++; | 242 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) count++; |
| 243 } | 243 } |
| 244 SocketAddresses* addresses = new SocketAddresses(count); | 244 AddressList<SocketAddress>* addresses = new AddressList<SocketAddress>(count); |
| 245 intptr_t i = 0; | 245 intptr_t i = 0; |
| 246 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { | 246 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { |
| 247 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) { | 247 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) { |
| 248 addresses->SetAt(i, new SocketAddress(c)); | 248 addresses->SetAt(i, new SocketAddress(c->ai_addr)); |
| 249 i++; | 249 i++; |
| 250 } | 250 } |
| 251 } | 251 } |
| 252 freeaddrinfo(info); | 252 freeaddrinfo(info); |
| 253 return addresses; | 253 return addresses; |
| 254 } | 254 } |
| 255 | 255 |
| 256 | 256 |
| 257 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces( | |
| 258 int type, | |
| 259 OSError** os_error) { | |
| 260 Initialize(); | |
| 261 | |
| 262 ULONG size = 0; | |
| 263 // Query the size needed. | |
| 264 GetAdaptersAddresses(SocketAddress::FromType(type), | |
|
Søren Gjesse
2013/06/20 18:22:05
You could consider adding the flags:
GAA_FLAG_SKI
Anders Johnsen
2013/06/21 07:58:35
Done.
| |
| 265 0, | |
| 266 NULL, | |
| 267 NULL, | |
| 268 &size); | |
| 269 PIP_ADAPTER_ADDRESSES addrs = | |
|
Søren Gjesse
2013/06/20 18:22:05
We normally use the *. That is IP_ADAPTER_ADDRESSE
Anders Johnsen
2013/06/21 07:58:35
Done.
| |
| 270 reinterpret_cast<PIP_ADAPTER_ADDRESSES>(malloc(size)); | |
|
Søren Gjesse
2013/06/20 18:22:05
Ditto and more below.
Anders Johnsen
2013/06/21 07:58:35
Done.
| |
| 271 // Get the addresses now we have the right buffer. | |
|
Søren Gjesse
2013/06/20 18:22:05
Add an if (status == ERROR_BUFFER_OVERFLOW) around
Anders Johnsen
2013/06/21 07:58:35
Done.
| |
| 272 int status = GetAdaptersAddresses(SocketAddress::FromType(type), | |
| 273 0, | |
| 274 NULL, | |
| 275 addrs, | |
| 276 &size); | |
| 277 if (status != NO_ERROR) { | |
| 278 ASSERT(*os_error == NULL); | |
| 279 DWORD error_code = WSAGetLastError(); | |
| 280 SetLastError(error_code); | |
| 281 *os_error = new OSError(); | |
| 282 return NULL; | |
| 283 } | |
| 284 intptr_t count = 0; | |
| 285 for (PIP_ADAPTER_ADDRESSES a = addrs; a != NULL; a = a->Next) { | |
| 286 for (PIP_ADAPTER_UNICAST_ADDRESS u = a->FirstUnicastAddress; | |
| 287 u != NULL; u = u->Next) { | |
| 288 count++; | |
| 289 } | |
| 290 } | |
| 291 AddressList<InterfaceSocketAddress>* addresses = | |
| 292 new AddressList<InterfaceSocketAddress>(count); | |
| 293 intptr_t i = 0; | |
| 294 for (PIP_ADAPTER_ADDRESSES a = addrs; a != NULL; a = a->Next) { | |
| 295 for (PIP_ADAPTER_UNICAST_ADDRESS u = a->FirstUnicastAddress; | |
| 296 u != NULL; u = u->Next) { | |
| 297 addresses->SetAt(i, new InterfaceSocketAddress( | |
| 298 u->Address.lpSockaddr, | |
| 299 StringUtils::WideToUtf8(a->FriendlyName))); | |
| 300 i++; | |
| 301 } | |
| 302 } | |
| 303 free(addrs); | |
| 304 return addresses; | |
| 305 } | |
| 306 | |
| 307 | |
| 257 intptr_t ServerSocket::CreateBindListen(RawAddr addr, | 308 intptr_t ServerSocket::CreateBindListen(RawAddr addr, |
| 258 intptr_t port, | 309 intptr_t port, |
| 259 intptr_t backlog, | 310 intptr_t backlog, |
| 260 bool v6_only) { | 311 bool v6_only) { |
| 261 SOCKET s = socket(addr.ss.ss_family, SOCK_STREAM, IPPROTO_TCP); | 312 SOCKET s = socket(addr.ss.ss_family, SOCK_STREAM, IPPROTO_TCP); |
| 262 if (s == INVALID_SOCKET) { | 313 if (s == INVALID_SOCKET) { |
| 263 return -1; | 314 return -1; |
| 264 } | 315 } |
| 265 | 316 |
| 266 BOOL optval = true; | 317 BOOL optval = true; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 281 setsockopt(s, | 332 setsockopt(s, |
| 282 IPPROTO_IPV6, | 333 IPPROTO_IPV6, |
| 283 IPV6_V6ONLY, | 334 IPV6_V6ONLY, |
| 284 reinterpret_cast<const char*>(&optval), | 335 reinterpret_cast<const char*>(&optval), |
| 285 sizeof(optval)); | 336 sizeof(optval)); |
| 286 } | 337 } |
| 287 | 338 |
| 288 SocketAddress::SetAddrPort(&addr, port); | 339 SocketAddress::SetAddrPort(&addr, port); |
| 289 status = bind(s, | 340 status = bind(s, |
| 290 &addr.addr, | 341 &addr.addr, |
| 291 SocketAddress::GetAddrLength(addr)); | 342 SocketAddress::GetAddrLength(&addr)); |
| 292 if (status == SOCKET_ERROR) { | 343 if (status == SOCKET_ERROR) { |
| 293 DWORD rc = WSAGetLastError(); | 344 DWORD rc = WSAGetLastError(); |
| 294 closesocket(s); | 345 closesocket(s); |
| 295 SetLastError(rc); | 346 SetLastError(rc); |
| 296 return -1; | 347 return -1; |
| 297 } | 348 } |
| 298 | 349 |
| 299 status = listen(s, backlog > 0 ? backlog : SOMAXCONN); | 350 status = listen(s, backlog > 0 ? backlog : SOMAXCONN); |
| 300 if (status == SOCKET_ERROR) { | 351 if (status == SOCKET_ERROR) { |
| 301 DWORD rc = WSAGetLastError(); | 352 DWORD rc = WSAGetLastError(); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 344 IPPROTO_TCP, | 395 IPPROTO_TCP, |
| 345 TCP_NODELAY, | 396 TCP_NODELAY, |
| 346 reinterpret_cast<char *>(&on), | 397 reinterpret_cast<char *>(&on), |
| 347 sizeof(on)) == 0; | 398 sizeof(on)) == 0; |
| 348 } | 399 } |
| 349 | 400 |
| 350 } // namespace bin | 401 } // namespace bin |
| 351 } // namespace dart | 402 } // namespace dart |
| 352 | 403 |
| 353 #endif // defined(TARGET_OS_WINDOWS) | 404 #endif // defined(TARGET_OS_WINDOWS) |
| OLD | NEW |