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 #if !defined(DART_IO_DISABLED) | 5 #if !defined(DART_IO_DISABLED) |
6 | 6 |
7 #include "platform/globals.h" | 7 #include "platform/globals.h" |
8 #if defined(TARGET_OS_WINDOWS) | 8 #if defined(TARGET_OS_WINDOWS) |
9 | 9 |
10 #include "bin/socket.h" | 10 #include "bin/socket.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); | 26 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
27 RawAddr* raw = reinterpret_cast<RawAddr*>(sockaddr); | 27 RawAddr* raw = reinterpret_cast<RawAddr*>(sockaddr); |
28 | 28 |
29 // Clear the port before calling WSAAddressToString as WSAAddressToString | 29 // Clear the port before calling WSAAddressToString as WSAAddressToString |
30 // includes the port in the formatted string. | 30 // includes the port in the formatted string. |
31 int err = Socket::FormatNumericAddress(*raw, as_string_, INET6_ADDRSTRLEN); | 31 int err = Socket::FormatNumericAddress(*raw, as_string_, INET6_ADDRSTRLEN); |
32 | 32 |
33 if (err != 0) { | 33 if (err != 0) { |
34 as_string_[0] = 0; | 34 as_string_[0] = 0; |
35 } | 35 } |
36 memmove(reinterpret_cast<void *>(&addr_), | 36 memmove(reinterpret_cast<void*>(&addr_), sockaddr, |
37 sockaddr, | |
38 SocketAddress::GetAddrLength(*raw)); | 37 SocketAddress::GetAddrLength(*raw)); |
39 } | 38 } |
40 | 39 |
41 | 40 |
42 bool Socket::FormatNumericAddress(const RawAddr& addr, char* address, int len) { | 41 bool Socket::FormatNumericAddress(const RawAddr& addr, char* address, int len) { |
43 socklen_t salen = SocketAddress::GetAddrLength(addr); | 42 socklen_t salen = SocketAddress::GetAddrLength(addr); |
44 DWORD l = len; | 43 DWORD l = len; |
45 RawAddr& raw = const_cast<RawAddr&>(addr); | 44 RawAddr& raw = const_cast<RawAddr&>(addr); |
46 return WSAAddressToStringA(&raw.addr, | 45 return WSAAddressToStringA(&raw.addr, salen, NULL, address, &l) != 0; |
47 salen, | |
48 NULL, | |
49 address, | |
50 &l) != 0; | |
51 } | 46 } |
52 | 47 |
53 | 48 |
54 static Mutex* init_mutex = new Mutex(); | 49 static Mutex* init_mutex = new Mutex(); |
55 static bool socket_initialized = false; | 50 static bool socket_initialized = false; |
56 | 51 |
57 bool Socket::Initialize() { | 52 bool Socket::Initialize() { |
58 MutexLocker lock(init_mutex); | 53 MutexLocker lock(init_mutex); |
59 if (socket_initialized) { | 54 if (socket_initialized) { |
60 return true; | 55 return true; |
(...skipping 15 matching lines...) Expand all Loading... |
76 return client_socket->Available(); | 71 return client_socket->Available(); |
77 } | 72 } |
78 | 73 |
79 | 74 |
80 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { | 75 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { |
81 Handle* handle = reinterpret_cast<Handle*>(fd); | 76 Handle* handle = reinterpret_cast<Handle*>(fd); |
82 return handle->Read(buffer, num_bytes); | 77 return handle->Read(buffer, num_bytes); |
83 } | 78 } |
84 | 79 |
85 | 80 |
86 intptr_t Socket::RecvFrom( | 81 intptr_t Socket::RecvFrom(intptr_t fd, |
87 intptr_t fd, void* buffer, intptr_t num_bytes, RawAddr* addr) { | 82 void* buffer, |
| 83 intptr_t num_bytes, |
| 84 RawAddr* addr) { |
88 Handle* handle = reinterpret_cast<Handle*>(fd); | 85 Handle* handle = reinterpret_cast<Handle*>(fd); |
89 socklen_t addr_len = sizeof(addr->ss); | 86 socklen_t addr_len = sizeof(addr->ss); |
90 return handle->RecvFrom(buffer, num_bytes, &addr->addr, addr_len); | 87 return handle->RecvFrom(buffer, num_bytes, &addr->addr, addr_len); |
91 } | 88 } |
92 | 89 |
93 | 90 |
94 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { | 91 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { |
95 Handle* handle = reinterpret_cast<Handle*>(fd); | 92 Handle* handle = reinterpret_cast<Handle*>(fd); |
96 return handle->Write(buffer, num_bytes); | 93 return handle->Write(buffer, num_bytes); |
97 } | 94 } |
98 | 95 |
99 | 96 |
100 intptr_t Socket::SendTo( | 97 intptr_t Socket::SendTo(intptr_t fd, |
101 intptr_t fd, const void* buffer, intptr_t num_bytes, const RawAddr& addr) { | 98 const void* buffer, |
| 99 intptr_t num_bytes, |
| 100 const RawAddr& addr) { |
102 Handle* handle = reinterpret_cast<Handle*>(fd); | 101 Handle* handle = reinterpret_cast<Handle*>(fd); |
103 RawAddr& raw = const_cast<RawAddr&>(addr); | 102 RawAddr& raw = const_cast<RawAddr&>(addr); |
104 return handle->SendTo( | 103 return handle->SendTo(buffer, num_bytes, &raw.addr, |
105 buffer, num_bytes, &raw.addr, SocketAddress::GetAddrLength(addr)); | 104 SocketAddress::GetAddrLength(addr)); |
106 } | 105 } |
107 | 106 |
108 | 107 |
109 intptr_t Socket::GetPort(intptr_t fd) { | 108 intptr_t Socket::GetPort(intptr_t fd) { |
110 ASSERT(reinterpret_cast<Handle*>(fd)->is_socket()); | 109 ASSERT(reinterpret_cast<Handle*>(fd)->is_socket()); |
111 SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd); | 110 SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd); |
112 RawAddr raw; | 111 RawAddr raw; |
113 socklen_t size = sizeof(raw); | 112 socklen_t size = sizeof(raw); |
114 if (getsockname(socket_handle->socket(), &raw.addr, &size) == SOCKET_ERROR) { | 113 if (getsockname(socket_handle->socket(), &raw.addr, &size) == SOCKET_ERROR) { |
115 return 0; | 114 return 0; |
116 } | 115 } |
117 return SocketAddress::GetAddrPort(raw); | 116 return SocketAddress::GetAddrPort(raw); |
118 } | 117 } |
119 | 118 |
120 | 119 |
121 SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) { | 120 SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) { |
122 ASSERT(reinterpret_cast<Handle*>(fd)->is_socket()); | 121 ASSERT(reinterpret_cast<Handle*>(fd)->is_socket()); |
123 SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd); | 122 SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd); |
124 RawAddr raw; | 123 RawAddr raw; |
(...skipping 11 matching lines...) Expand all Loading... |
136 | 135 |
137 static intptr_t Create(const RawAddr& addr) { | 136 static intptr_t Create(const RawAddr& addr) { |
138 SOCKET s = socket(addr.ss.ss_family, SOCK_STREAM, 0); | 137 SOCKET s = socket(addr.ss.ss_family, SOCK_STREAM, 0); |
139 if (s == INVALID_SOCKET) { | 138 if (s == INVALID_SOCKET) { |
140 return -1; | 139 return -1; |
141 } | 140 } |
142 | 141 |
143 linger l; | 142 linger l; |
144 l.l_onoff = 1; | 143 l.l_onoff = 1; |
145 l.l_linger = 10; | 144 l.l_linger = 10; |
146 int status = setsockopt(s, | 145 int status = setsockopt(s, SOL_SOCKET, SO_LINGER, reinterpret_cast<char*>(&l), |
147 SOL_SOCKET, | |
148 SO_LINGER, | |
149 reinterpret_cast<char*>(&l), | |
150 sizeof(l)); | 146 sizeof(l)); |
151 if (status != NO_ERROR) { | 147 if (status != NO_ERROR) { |
152 FATAL("Failed setting SO_LINGER on socket"); | 148 FATAL("Failed setting SO_LINGER on socket"); |
153 } | 149 } |
154 | 150 |
155 ClientSocket* client_socket = new ClientSocket(s); | 151 ClientSocket* client_socket = new ClientSocket(s); |
156 return reinterpret_cast<intptr_t>(client_socket); | 152 return reinterpret_cast<intptr_t>(client_socket); |
157 } | 153 } |
158 | 154 |
159 | 155 |
160 static intptr_t Connect( | 156 static intptr_t Connect(intptr_t fd, |
161 intptr_t fd, const RawAddr& addr, const RawAddr& bind_addr) { | 157 const RawAddr& addr, |
| 158 const RawAddr& bind_addr) { |
162 ASSERT(reinterpret_cast<Handle*>(fd)->is_client_socket()); | 159 ASSERT(reinterpret_cast<Handle*>(fd)->is_client_socket()); |
163 ClientSocket* handle = reinterpret_cast<ClientSocket*>(fd); | 160 ClientSocket* handle = reinterpret_cast<ClientSocket*>(fd); |
164 SOCKET s = handle->socket(); | 161 SOCKET s = handle->socket(); |
165 | 162 |
166 int status = bind( | 163 int status = |
167 s, &bind_addr.addr, SocketAddress::GetAddrLength(bind_addr)); | 164 bind(s, &bind_addr.addr, SocketAddress::GetAddrLength(bind_addr)); |
168 if (status != NO_ERROR) { | 165 if (status != NO_ERROR) { |
169 int rc = WSAGetLastError(); | 166 int rc = WSAGetLastError(); |
170 handle->mark_closed(); // Destructor asserts that socket is marked closed. | 167 handle->mark_closed(); // Destructor asserts that socket is marked closed. |
171 delete handle; | 168 delete handle; |
172 closesocket(s); | 169 closesocket(s); |
173 SetLastError(rc); | 170 SetLastError(rc); |
174 return -1; | 171 return -1; |
175 } | 172 } |
176 | 173 |
177 LPFN_CONNECTEX connectEx = NULL; | 174 LPFN_CONNECTEX connectEx = NULL; |
178 GUID guid_connect_ex = WSAID_CONNECTEX; | 175 GUID guid_connect_ex = WSAID_CONNECTEX; |
179 DWORD bytes; | 176 DWORD bytes; |
180 status = WSAIoctl(s, | 177 status = WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid_connect_ex, |
181 SIO_GET_EXTENSION_FUNCTION_POINTER, | 178 sizeof(guid_connect_ex), &connectEx, sizeof(connectEx), |
182 &guid_connect_ex, | 179 &bytes, NULL, NULL); |
183 sizeof(guid_connect_ex), | |
184 &connectEx, | |
185 sizeof(connectEx), | |
186 &bytes, | |
187 NULL, | |
188 NULL); | |
189 DWORD rc; | 180 DWORD rc; |
190 if (status != SOCKET_ERROR) { | 181 if (status != SOCKET_ERROR) { |
191 handle->EnsureInitialized(EventHandler::delegate()); | 182 handle->EnsureInitialized(EventHandler::delegate()); |
192 | 183 |
193 OverlappedBuffer* overlapped = OverlappedBuffer::AllocateConnectBuffer(); | 184 OverlappedBuffer* overlapped = OverlappedBuffer::AllocateConnectBuffer(); |
194 | 185 |
195 status = connectEx(s, | 186 status = connectEx(s, &addr.addr, SocketAddress::GetAddrLength(addr), NULL, |
196 &addr.addr, | 187 0, NULL, overlapped->GetCleanOverlapped()); |
197 SocketAddress::GetAddrLength(addr), | |
198 NULL, | |
199 0, | |
200 NULL, | |
201 overlapped->GetCleanOverlapped()); | |
202 | 188 |
203 | 189 |
204 if (status == TRUE) { | 190 if (status == TRUE) { |
205 handle->ConnectComplete(overlapped); | 191 handle->ConnectComplete(overlapped); |
206 return fd; | 192 return fd; |
207 } else if (WSAGetLastError() == ERROR_IO_PENDING) { | 193 } else if (WSAGetLastError() == ERROR_IO_PENDING) { |
208 return fd; | 194 return fd; |
209 } | 195 } |
210 rc = WSAGetLastError(); | 196 rc = WSAGetLastError(); |
211 // Cleanup in case of error. | 197 // Cleanup in case of error. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 if (fd < 0) { | 231 if (fd < 0) { |
246 return fd; | 232 return fd; |
247 } | 233 } |
248 | 234 |
249 return Connect(fd, addr, source_addr); | 235 return Connect(fd, addr, source_addr); |
250 } | 236 } |
251 | 237 |
252 | 238 |
253 bool Socket::IsBindError(intptr_t error_number) { | 239 bool Socket::IsBindError(intptr_t error_number) { |
254 return error_number == WSAEADDRINUSE || error_number == WSAEADDRNOTAVAIL || | 240 return error_number == WSAEADDRINUSE || error_number == WSAEADDRNOTAVAIL || |
255 error_number == WSAEINVAL; | 241 error_number == WSAEINVAL; |
256 } | 242 } |
257 | 243 |
258 | 244 |
259 void Socket::GetError(intptr_t fd, OSError* os_error) { | 245 void Socket::GetError(intptr_t fd, OSError* os_error) { |
260 Handle* handle = reinterpret_cast<Handle*>(fd); | 246 Handle* handle = reinterpret_cast<Handle*>(fd); |
261 os_error->SetCodeAndMessage(OSError::kSystem, handle->last_error()); | 247 os_error->SetCodeAndMessage(OSError::kSystem, handle->last_error()); |
262 } | 248 } |
263 | 249 |
264 | 250 |
265 int Socket::GetType(intptr_t fd) { | 251 int Socket::GetType(intptr_t fd) { |
266 Handle* handle = reinterpret_cast<Handle*>(fd); | 252 Handle* handle = reinterpret_cast<Handle*>(fd); |
267 switch (GetFileType(handle->handle())) { | 253 switch (GetFileType(handle->handle())) { |
268 case FILE_TYPE_CHAR: return File::kTerminal; | 254 case FILE_TYPE_CHAR: |
269 case FILE_TYPE_PIPE: return File::kPipe; | 255 return File::kTerminal; |
270 case FILE_TYPE_DISK: return File::kFile; | 256 case FILE_TYPE_PIPE: |
271 default: return GetLastError == NO_ERROR ? File::kOther : -1; | 257 return File::kPipe; |
| 258 case FILE_TYPE_DISK: |
| 259 return File::kFile; |
| 260 default: |
| 261 return GetLastError == NO_ERROR ? File::kOther : -1; |
272 } | 262 } |
273 } | 263 } |
274 | 264 |
275 | 265 |
276 intptr_t Socket::GetStdioHandle(intptr_t num) { | 266 intptr_t Socket::GetStdioHandle(intptr_t num) { |
277 if (num != 0) { | 267 if (num != 0) { |
278 return -1; | 268 return -1; |
279 } | 269 } |
280 HANDLE handle = GetStdHandle(STD_INPUT_HANDLE); | 270 HANDLE handle = GetStdHandle(STD_INPUT_HANDLE); |
281 if (handle == INVALID_HANDLE_VALUE) { | 271 if (handle == INVALID_HANDLE_VALUE) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 freeaddrinfo(info); | 333 freeaddrinfo(info); |
344 return addresses; | 334 return addresses; |
345 } | 335 } |
346 | 336 |
347 | 337 |
348 bool Socket::ReverseLookup(const RawAddr& addr, | 338 bool Socket::ReverseLookup(const RawAddr& addr, |
349 char* host, | 339 char* host, |
350 intptr_t host_len, | 340 intptr_t host_len, |
351 OSError** os_error) { | 341 OSError** os_error) { |
352 ASSERT(host_len >= NI_MAXHOST); | 342 ASSERT(host_len >= NI_MAXHOST); |
353 int status = getnameinfo(&addr.addr, | 343 int status = getnameinfo(&addr.addr, SocketAddress::GetAddrLength(addr), host, |
354 SocketAddress::GetAddrLength(addr), | 344 host_len, NULL, 0, NI_NAMEREQD); |
355 host, | |
356 host_len, | |
357 NULL, | |
358 0, | |
359 NI_NAMEREQD); | |
360 if (status != 0) { | 345 if (status != 0) { |
361 ASSERT(*os_error == NULL); | 346 ASSERT(*os_error == NULL); |
362 DWORD error_code = WSAGetLastError(); | 347 DWORD error_code = WSAGetLastError(); |
363 SetLastError(error_code); | 348 SetLastError(error_code); |
364 *os_error = new OSError(); | 349 *os_error = new OSError(); |
365 return false; | 350 return false; |
366 } | 351 } |
367 return true; | 352 return true; |
368 } | 353 } |
369 | 354 |
(...skipping 13 matching lines...) Expand all Loading... |
383 | 368 |
384 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { | 369 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { |
385 SOCKET s = socket(addr.ss.ss_family, SOCK_DGRAM, IPPROTO_UDP); | 370 SOCKET s = socket(addr.ss.ss_family, SOCK_DGRAM, IPPROTO_UDP); |
386 if (s == INVALID_SOCKET) { | 371 if (s == INVALID_SOCKET) { |
387 return -1; | 372 return -1; |
388 } | 373 } |
389 | 374 |
390 int status; | 375 int status; |
391 if (reuseAddress) { | 376 if (reuseAddress) { |
392 BOOL optval = true; | 377 BOOL optval = true; |
393 status = setsockopt(s, | 378 status = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, |
394 SOL_SOCKET, | 379 reinterpret_cast<const char*>(&optval), sizeof(optval)); |
395 SO_REUSEADDR, | |
396 reinterpret_cast<const char*>(&optval), | |
397 sizeof(optval)); | |
398 if (status == SOCKET_ERROR) { | 380 if (status == SOCKET_ERROR) { |
399 DWORD rc = WSAGetLastError(); | 381 DWORD rc = WSAGetLastError(); |
400 closesocket(s); | 382 closesocket(s); |
401 SetLastError(rc); | 383 SetLastError(rc); |
402 return -1; | 384 return -1; |
403 } | 385 } |
404 } | 386 } |
405 | 387 |
406 status = bind(s, &addr.addr, SocketAddress::GetAddrLength(addr)); | 388 status = bind(s, &addr.addr, SocketAddress::GetAddrLength(addr)); |
407 if (status == SOCKET_ERROR) { | 389 if (status == SOCKET_ERROR) { |
408 DWORD rc = WSAGetLastError(); | 390 DWORD rc = WSAGetLastError(); |
409 closesocket(s); | 391 closesocket(s); |
410 SetLastError(rc); | 392 SetLastError(rc); |
411 return -1; | 393 return -1; |
412 } | 394 } |
413 | 395 |
414 DatagramSocket* datagram_socket = new DatagramSocket(s); | 396 DatagramSocket* datagram_socket = new DatagramSocket(s); |
415 datagram_socket->EnsureInitialized(EventHandler::delegate()); | 397 datagram_socket->EnsureInitialized(EventHandler::delegate()); |
416 return reinterpret_cast<intptr_t>(datagram_socket); | 398 return reinterpret_cast<intptr_t>(datagram_socket); |
417 } | 399 } |
418 | 400 |
419 | 401 |
420 bool Socket::ListInterfacesSupported() { | 402 bool Socket::ListInterfacesSupported() { |
421 return true; | 403 return true; |
422 } | 404 } |
423 | 405 |
424 | 406 |
425 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces( | 407 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces( |
426 int type, | 408 int type, |
427 OSError** os_error) { | 409 OSError** os_error) { |
428 Initialize(); | 410 Initialize(); |
429 | 411 |
430 ULONG size = 0; | 412 ULONG size = 0; |
431 DWORD flags = GAA_FLAG_SKIP_ANYCAST | | 413 DWORD flags = GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | |
432 GAA_FLAG_SKIP_MULTICAST | | |
433 GAA_FLAG_SKIP_DNS_SERVER; | 414 GAA_FLAG_SKIP_DNS_SERVER; |
434 // Query the size needed. | 415 // Query the size needed. |
435 int status = GetAdaptersAddresses(SocketAddress::FromType(type), | 416 int status = GetAdaptersAddresses(SocketAddress::FromType(type), flags, NULL, |
436 flags, | 417 NULL, &size); |
437 NULL, | |
438 NULL, | |
439 &size); | |
440 IP_ADAPTER_ADDRESSES* addrs = NULL; | 418 IP_ADAPTER_ADDRESSES* addrs = NULL; |
441 if (status == ERROR_BUFFER_OVERFLOW) { | 419 if (status == ERROR_BUFFER_OVERFLOW) { |
442 addrs = reinterpret_cast<IP_ADAPTER_ADDRESSES*>(malloc(size)); | 420 addrs = reinterpret_cast<IP_ADAPTER_ADDRESSES*>(malloc(size)); |
443 // Get the addresses now we have the right buffer. | 421 // Get the addresses now we have the right buffer. |
444 status = GetAdaptersAddresses(SocketAddress::FromType(type), | 422 status = GetAdaptersAddresses(SocketAddress::FromType(type), flags, NULL, |
445 flags, | 423 addrs, &size); |
446 NULL, | |
447 addrs, | |
448 &size); | |
449 } | 424 } |
450 if (status != NO_ERROR) { | 425 if (status != NO_ERROR) { |
451 ASSERT(*os_error == NULL); | 426 ASSERT(*os_error == NULL); |
452 DWORD error_code = WSAGetLastError(); | 427 DWORD error_code = WSAGetLastError(); |
453 SetLastError(error_code); | 428 SetLastError(error_code); |
454 *os_error = new OSError(); | 429 *os_error = new OSError(); |
455 return NULL; | 430 return NULL; |
456 } | 431 } |
457 intptr_t count = 0; | 432 intptr_t count = 0; |
458 for (IP_ADAPTER_ADDRESSES* a = addrs; a != NULL; a = a->Next) { | 433 for (IP_ADAPTER_ADDRESSES* a = addrs; a != NULL; a = a->Next) { |
459 for (IP_ADAPTER_UNICAST_ADDRESS* u = a->FirstUnicastAddress; | 434 for (IP_ADAPTER_UNICAST_ADDRESS* u = a->FirstUnicastAddress; u != NULL; |
460 u != NULL; u = u->Next) { | 435 u = u->Next) { |
461 count++; | 436 count++; |
462 } | 437 } |
463 } | 438 } |
464 AddressList<InterfaceSocketAddress>* addresses = | 439 AddressList<InterfaceSocketAddress>* addresses = |
465 new AddressList<InterfaceSocketAddress>(count); | 440 new AddressList<InterfaceSocketAddress>(count); |
466 intptr_t i = 0; | 441 intptr_t i = 0; |
467 for (IP_ADAPTER_ADDRESSES* a = addrs; a != NULL; a = a->Next) { | 442 for (IP_ADAPTER_ADDRESSES* a = addrs; a != NULL; a = a->Next) { |
468 for (IP_ADAPTER_UNICAST_ADDRESS* u = a->FirstUnicastAddress; | 443 for (IP_ADAPTER_UNICAST_ADDRESS* u = a->FirstUnicastAddress; u != NULL; |
469 u != NULL; u = u->Next) { | 444 u = u->Next) { |
470 addresses->SetAt(i, new InterfaceSocketAddress( | 445 addresses->SetAt( |
471 u->Address.lpSockaddr, | 446 i, new InterfaceSocketAddress( |
472 StringUtilsWin::WideToUtf8(a->FriendlyName), | 447 u->Address.lpSockaddr, |
473 a->Ipv6IfIndex)); | 448 StringUtilsWin::WideToUtf8(a->FriendlyName), a->Ipv6IfIndex)); |
474 i++; | 449 i++; |
475 } | 450 } |
476 } | 451 } |
477 free(addrs); | 452 free(addrs); |
478 return addresses; | 453 return addresses; |
479 } | 454 } |
480 | 455 |
481 | 456 |
482 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, | 457 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, |
483 intptr_t backlog, | 458 intptr_t backlog, |
484 bool v6_only) { | 459 bool v6_only) { |
485 SOCKET s = socket(addr.ss.ss_family, SOCK_STREAM, IPPROTO_TCP); | 460 SOCKET s = socket(addr.ss.ss_family, SOCK_STREAM, IPPROTO_TCP); |
486 if (s == INVALID_SOCKET) { | 461 if (s == INVALID_SOCKET) { |
487 return -1; | 462 return -1; |
488 } | 463 } |
489 | 464 |
490 BOOL optval = true; | 465 BOOL optval = true; |
491 int status = setsockopt(s, | 466 int status = |
492 SOL_SOCKET, | 467 setsockopt(s, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, |
493 SO_EXCLUSIVEADDRUSE, | 468 reinterpret_cast<const char*>(&optval), sizeof(optval)); |
494 reinterpret_cast<const char*>(&optval), | |
495 sizeof(optval)); | |
496 if (status == SOCKET_ERROR) { | 469 if (status == SOCKET_ERROR) { |
497 DWORD rc = WSAGetLastError(); | 470 DWORD rc = WSAGetLastError(); |
498 closesocket(s); | 471 closesocket(s); |
499 SetLastError(rc); | 472 SetLastError(rc); |
500 return -1; | 473 return -1; |
501 } | 474 } |
502 | 475 |
503 if (addr.ss.ss_family == AF_INET6) { | 476 if (addr.ss.ss_family == AF_INET6) { |
504 optval = v6_only; | 477 optval = v6_only; |
505 setsockopt(s, | 478 setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, |
506 IPPROTO_IPV6, | 479 reinterpret_cast<const char*>(&optval), sizeof(optval)); |
507 IPV6_V6ONLY, | |
508 reinterpret_cast<const char*>(&optval), | |
509 sizeof(optval)); | |
510 } | 480 } |
511 | 481 |
512 status = bind(s, &addr.addr, SocketAddress::GetAddrLength(addr)); | 482 status = bind(s, &addr.addr, SocketAddress::GetAddrLength(addr)); |
513 if (status == SOCKET_ERROR) { | 483 if (status == SOCKET_ERROR) { |
514 DWORD rc = WSAGetLastError(); | 484 DWORD rc = WSAGetLastError(); |
515 closesocket(s); | 485 closesocket(s); |
516 SetLastError(rc); | 486 SetLastError(rc); |
517 return -1; | 487 return -1; |
518 } | 488 } |
519 | 489 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
569 void Socket::Close(intptr_t fd) { | 539 void Socket::Close(intptr_t fd) { |
570 ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(fd); | 540 ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(fd); |
571 client_socket->Close(); | 541 client_socket->Close(); |
572 } | 542 } |
573 | 543 |
574 | 544 |
575 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { | 545 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { |
576 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); | 546 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); |
577 int on; | 547 int on; |
578 socklen_t len = sizeof(on); | 548 socklen_t len = sizeof(on); |
579 int err = getsockopt(handle->socket(), | 549 int err = getsockopt(handle->socket(), IPPROTO_TCP, TCP_NODELAY, |
580 IPPROTO_TCP, | 550 reinterpret_cast<char*>(&on), &len); |
581 TCP_NODELAY, | |
582 reinterpret_cast<char *>(&on), | |
583 &len); | |
584 if (err == 0) { | 551 if (err == 0) { |
585 *enabled = (on == 1); | 552 *enabled = (on == 1); |
586 } | 553 } |
587 return (err == 0); | 554 return (err == 0); |
588 } | 555 } |
589 | 556 |
590 | 557 |
591 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { | 558 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { |
592 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); | 559 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); |
593 int on = enabled ? 1 : 0; | 560 int on = enabled ? 1 : 0; |
594 return setsockopt(handle->socket(), | 561 return setsockopt(handle->socket(), IPPROTO_TCP, TCP_NODELAY, |
595 IPPROTO_TCP, | 562 reinterpret_cast<char*>(&on), sizeof(on)) == 0; |
596 TCP_NODELAY, | |
597 reinterpret_cast<char *>(&on), | |
598 sizeof(on)) == 0; | |
599 } | 563 } |
600 | 564 |
601 | 565 |
602 bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) { | 566 bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) { |
603 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); | 567 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); |
604 uint8_t on; | 568 uint8_t on; |
605 socklen_t len = sizeof(on); | 569 socklen_t len = sizeof(on); |
606 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 570 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
607 int optname = protocol == SocketAddress::TYPE_IPV4 | 571 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP |
608 ? IP_MULTICAST_LOOP : IPV6_MULTICAST_LOOP; | 572 : IPV6_MULTICAST_LOOP; |
609 if (getsockopt(handle->socket(), | 573 if (getsockopt(handle->socket(), level, optname, reinterpret_cast<char*>(&on), |
610 level, | |
611 optname, | |
612 reinterpret_cast<char *>(&on), | |
613 &len) == 0) { | 574 &len) == 0) { |
614 *enabled = (on == 1); | 575 *enabled = (on == 1); |
615 return true; | 576 return true; |
616 } | 577 } |
617 return false; | 578 return false; |
618 } | 579 } |
619 | 580 |
620 | 581 |
621 bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) { | 582 bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) { |
622 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); | 583 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); |
623 int on = enabled ? 1 : 0; | 584 int on = enabled ? 1 : 0; |
624 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 585 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
625 int optname = protocol == SocketAddress::TYPE_IPV4 | 586 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP |
626 ? IP_MULTICAST_LOOP : IPV6_MULTICAST_LOOP; | 587 : IPV6_MULTICAST_LOOP; |
627 return setsockopt(handle->socket(), | 588 return setsockopt(handle->socket(), level, optname, |
628 level, | 589 reinterpret_cast<char*>(&on), sizeof(on)) == 0; |
629 optname, | |
630 reinterpret_cast<char *>(&on), | |
631 sizeof(on)) == 0; | |
632 } | 590 } |
633 | 591 |
634 | 592 |
635 bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) { | 593 bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) { |
636 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); | 594 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); |
637 uint8_t v; | 595 uint8_t v; |
638 socklen_t len = sizeof(v); | 596 socklen_t len = sizeof(v); |
639 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 597 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
640 int optname = protocol == SocketAddress::TYPE_IPV4 | 598 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL |
641 ? IP_MULTICAST_TTL : IPV6_MULTICAST_HOPS; | 599 : IPV6_MULTICAST_HOPS; |
642 if (getsockopt(handle->socket(), | 600 if (getsockopt(handle->socket(), level, optname, reinterpret_cast<char*>(&v), |
643 level, | |
644 optname, | |
645 reinterpret_cast<char *>(&v), | |
646 &len) == 0) { | 601 &len) == 0) { |
647 *value = v; | 602 *value = v; |
648 return true; | 603 return true; |
649 } | 604 } |
650 return false; | 605 return false; |
651 } | 606 } |
652 | 607 |
653 | 608 |
654 bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) { | 609 bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) { |
655 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); | 610 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); |
656 int v = value; | 611 int v = value; |
657 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; | 612 int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6; |
658 int optname = protocol == SocketAddress::TYPE_IPV4 | 613 int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL |
659 ? IP_MULTICAST_TTL : IPV6_MULTICAST_HOPS; | 614 : IPV6_MULTICAST_HOPS; |
660 return setsockopt(handle->socket(), | 615 return setsockopt(handle->socket(), level, optname, |
661 level, | 616 reinterpret_cast<char*>(&v), sizeof(v)) == 0; |
662 optname, | |
663 reinterpret_cast<char *>(&v), | |
664 sizeof(v)) == 0; | |
665 } | 617 } |
666 | 618 |
667 | 619 |
668 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) { | 620 bool Socket::GetBroadcast(intptr_t fd, bool* enabled) { |
669 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); | 621 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); |
670 int on; | 622 int on; |
671 socklen_t len = sizeof(on); | 623 socklen_t len = sizeof(on); |
672 int err = getsockopt(handle->socket(), | 624 int err = getsockopt(handle->socket(), SOL_SOCKET, SO_BROADCAST, |
673 SOL_SOCKET, | 625 reinterpret_cast<char*>(&on), &len); |
674 SO_BROADCAST, | |
675 reinterpret_cast<char *>(&on), | |
676 &len); | |
677 if (err == 0) { | 626 if (err == 0) { |
678 *enabled = (on == 1); | 627 *enabled = (on == 1); |
679 } | 628 } |
680 return (err == 0); | 629 return (err == 0); |
681 } | 630 } |
682 | 631 |
683 | 632 |
684 bool Socket::SetBroadcast(intptr_t fd, bool enabled) { | 633 bool Socket::SetBroadcast(intptr_t fd, bool enabled) { |
685 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); | 634 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); |
686 int on = enabled ? 1 : 0; | 635 int on = enabled ? 1 : 0; |
687 return setsockopt(handle->socket(), | 636 return setsockopt(handle->socket(), SOL_SOCKET, SO_BROADCAST, |
688 SOL_SOCKET, | 637 reinterpret_cast<char*>(&on), sizeof(on)) == 0; |
689 SO_BROADCAST, | |
690 reinterpret_cast<char *>(&on), | |
691 sizeof(on)) == 0; | |
692 } | 638 } |
693 | 639 |
694 | 640 |
695 bool Socket::JoinMulticast( | 641 bool Socket::JoinMulticast(intptr_t fd, |
696 intptr_t fd, const RawAddr& addr, const RawAddr&, int interfaceIndex) { | 642 const RawAddr& addr, |
| 643 const RawAddr&, |
| 644 int interfaceIndex) { |
697 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); | 645 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); |
698 int proto = addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; | 646 int proto = addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; |
699 struct group_req mreq; | 647 struct group_req mreq; |
700 mreq.gr_interface = interfaceIndex; | 648 mreq.gr_interface = interfaceIndex; |
701 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); | 649 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); |
702 return setsockopt(handle->socket(), | 650 return setsockopt(handle->socket(), proto, MCAST_JOIN_GROUP, |
703 proto, | 651 reinterpret_cast<char*>(&mreq), sizeof(mreq)) == 0; |
704 MCAST_JOIN_GROUP, | |
705 reinterpret_cast<char *>(&mreq), | |
706 sizeof(mreq)) == 0; | |
707 } | 652 } |
708 | 653 |
709 | 654 |
710 bool Socket::LeaveMulticast( | 655 bool Socket::LeaveMulticast(intptr_t fd, |
711 intptr_t fd, const RawAddr& addr, const RawAddr&, int interfaceIndex) { | 656 const RawAddr& addr, |
| 657 const RawAddr&, |
| 658 int interfaceIndex) { |
712 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); | 659 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); |
713 int proto = addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; | 660 int proto = addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; |
714 struct group_req mreq; | 661 struct group_req mreq; |
715 mreq.gr_interface = interfaceIndex; | 662 mreq.gr_interface = interfaceIndex; |
716 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); | 663 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr)); |
717 return setsockopt(handle->socket(), | 664 return setsockopt(handle->socket(), proto, MCAST_LEAVE_GROUP, |
718 proto, | 665 reinterpret_cast<char*>(&mreq), sizeof(mreq)) == 0; |
719 MCAST_LEAVE_GROUP, | |
720 reinterpret_cast<char *>(&mreq), | |
721 sizeof(mreq)) == 0; | |
722 } | 666 } |
723 | 667 |
724 } // namespace bin | 668 } // namespace bin |
725 } // namespace dart | 669 } // namespace dart |
726 | 670 |
727 #endif // defined(TARGET_OS_WINDOWS) | 671 #endif // defined(TARGET_OS_WINDOWS) |
728 | 672 |
729 #endif // !defined(DART_IO_DISABLED) | 673 #endif // !defined(DART_IO_DISABLED) |
OLD | NEW |