| 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 DWORD flags = GAA_FLAG_SKIP_ANYCAST | |
| 264 GAA_FLAG_SKIP_MULTICAST | |
| 265 GAA_FLAG_SKIP_DNS_SERVER; |
| 266 // Query the size needed. |
| 267 int status = GetAdaptersAddresses(SocketAddress::FromType(type), |
| 268 flags, |
| 269 NULL, |
| 270 NULL, |
| 271 &size); |
| 272 IP_ADAPTER_ADDRESSES* addrs = NULL; |
| 273 if (status == ERROR_BUFFER_OVERFLOW) { |
| 274 addrs = reinterpret_cast<IP_ADAPTER_ADDRESSES*>(malloc(size)); |
| 275 // Get the addresses now we have the right buffer. |
| 276 status = GetAdaptersAddresses(SocketAddress::FromType(type), |
| 277 flags, |
| 278 NULL, |
| 279 addrs, |
| 280 &size); |
| 281 } |
| 282 if (status != NO_ERROR) { |
| 283 ASSERT(*os_error == NULL); |
| 284 DWORD error_code = WSAGetLastError(); |
| 285 SetLastError(error_code); |
| 286 *os_error = new OSError(); |
| 287 return NULL; |
| 288 } |
| 289 intptr_t count = 0; |
| 290 for (IP_ADAPTER_ADDRESSES* a = addrs; a != NULL; a = a->Next) { |
| 291 for (IP_ADAPTER_UNICAST_ADDRESS* u = a->FirstUnicastAddress; |
| 292 u != NULL; u = u->Next) { |
| 293 count++; |
| 294 } |
| 295 } |
| 296 AddressList<InterfaceSocketAddress>* addresses = |
| 297 new AddressList<InterfaceSocketAddress>(count); |
| 298 intptr_t i = 0; |
| 299 for (IP_ADAPTER_ADDRESSES* a = addrs; a != NULL; a = a->Next) { |
| 300 for (IP_ADAPTER_UNICAST_ADDRESS* u = a->FirstUnicastAddress; |
| 301 u != NULL; u = u->Next) { |
| 302 addresses->SetAt(i, new InterfaceSocketAddress( |
| 303 u->Address.lpSockaddr, |
| 304 StringUtils::WideToUtf8(a->FriendlyName))); |
| 305 i++; |
| 306 } |
| 307 } |
| 308 free(addrs); |
| 309 return addresses; |
| 310 } |
| 311 |
| 312 |
| 257 intptr_t ServerSocket::CreateBindListen(RawAddr addr, | 313 intptr_t ServerSocket::CreateBindListen(RawAddr addr, |
| 258 intptr_t port, | 314 intptr_t port, |
| 259 intptr_t backlog, | 315 intptr_t backlog, |
| 260 bool v6_only) { | 316 bool v6_only) { |
| 261 SOCKET s = socket(addr.ss.ss_family, SOCK_STREAM, IPPROTO_TCP); | 317 SOCKET s = socket(addr.ss.ss_family, SOCK_STREAM, IPPROTO_TCP); |
| 262 if (s == INVALID_SOCKET) { | 318 if (s == INVALID_SOCKET) { |
| 263 return -1; | 319 return -1; |
| 264 } | 320 } |
| 265 | 321 |
| 266 BOOL optval = true; | 322 BOOL optval = true; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 281 setsockopt(s, | 337 setsockopt(s, |
| 282 IPPROTO_IPV6, | 338 IPPROTO_IPV6, |
| 283 IPV6_V6ONLY, | 339 IPV6_V6ONLY, |
| 284 reinterpret_cast<const char*>(&optval), | 340 reinterpret_cast<const char*>(&optval), |
| 285 sizeof(optval)); | 341 sizeof(optval)); |
| 286 } | 342 } |
| 287 | 343 |
| 288 SocketAddress::SetAddrPort(&addr, port); | 344 SocketAddress::SetAddrPort(&addr, port); |
| 289 status = bind(s, | 345 status = bind(s, |
| 290 &addr.addr, | 346 &addr.addr, |
| 291 SocketAddress::GetAddrLength(addr)); | 347 SocketAddress::GetAddrLength(&addr)); |
| 292 if (status == SOCKET_ERROR) { | 348 if (status == SOCKET_ERROR) { |
| 293 DWORD rc = WSAGetLastError(); | 349 DWORD rc = WSAGetLastError(); |
| 294 closesocket(s); | 350 closesocket(s); |
| 295 SetLastError(rc); | 351 SetLastError(rc); |
| 296 return -1; | 352 return -1; |
| 297 } | 353 } |
| 298 | 354 |
| 299 status = listen(s, backlog > 0 ? backlog : SOMAXCONN); | 355 status = listen(s, backlog > 0 ? backlog : SOMAXCONN); |
| 300 if (status == SOCKET_ERROR) { | 356 if (status == SOCKET_ERROR) { |
| 301 DWORD rc = WSAGetLastError(); | 357 DWORD rc = WSAGetLastError(); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 IPPROTO_TCP, | 400 IPPROTO_TCP, |
| 345 TCP_NODELAY, | 401 TCP_NODELAY, |
| 346 reinterpret_cast<char *>(&on), | 402 reinterpret_cast<char *>(&on), |
| 347 sizeof(on)) == 0; | 403 sizeof(on)) == 0; |
| 348 } | 404 } |
| 349 | 405 |
| 350 } // namespace bin | 406 } // namespace bin |
| 351 } // namespace dart | 407 } // namespace dart |
| 352 | 408 |
| 353 #endif // defined(TARGET_OS_WINDOWS) | 409 #endif // defined(TARGET_OS_WINDOWS) |
| OLD | NEW |