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

Side by Side Diff: runtime/bin/socket_win.cc

Issue 910183003: Reafctor to use 'const RawAddr&' or 'RawAddr*' in the eventhandler code (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Minor change Created 5 years, 10 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
« runtime/bin/socket_linux.cc ('K') | « runtime/bin/socket_macos.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/lockers.h" 11 #include "bin/lockers.h"
12 #include "bin/log.h" 12 #include "bin/log.h"
13 #include "bin/socket.h" 13 #include "bin/socket.h"
14 #include "bin/thread.h" 14 #include "bin/thread.h"
15 #include "bin/utils.h" 15 #include "bin/utils.h"
16 16
17 namespace dart { 17 namespace dart {
18 namespace bin { 18 namespace bin {
19 19
20 SocketAddress::SocketAddress(struct sockaddr* sockaddr) { 20 SocketAddress::SocketAddress(struct sockaddr* sockaddr) {
21 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); 21 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN);
22 RawAddr* raw = reinterpret_cast<RawAddr*>(sockaddr); 22 RawAddr* raw = reinterpret_cast<RawAddr*>(sockaddr);
23 23
24 // Clear the port before calling WSAAddressToString as WSAAddressToString 24 // Clear the port before calling WSAAddressToString as WSAAddressToString
25 // includes the port in the formatted string. 25 // includes the port in the formatted string.
26 int err = Socket::FormatNumericAddress(raw, as_string_, INET6_ADDRSTRLEN); 26 int err = Socket::FormatNumericAddress(*raw, as_string_, INET6_ADDRSTRLEN);
27 27
28 if (err != 0) { 28 if (err != 0) {
29 as_string_[0] = 0; 29 as_string_[0] = 0;
30 } 30 }
31 memmove(reinterpret_cast<void *>(&addr_), 31 memmove(reinterpret_cast<void *>(&addr_),
32 sockaddr, 32 sockaddr,
33 SocketAddress::GetAddrLength(raw)); 33 SocketAddress::GetAddrLength(*raw));
34 } 34 }
35 35
36 36
37 bool Socket::FormatNumericAddress(RawAddr* addr, char* address, int len) { 37 bool Socket::FormatNumericAddress(const RawAddr& addr, char* address, int len) {
38 socklen_t salen = SocketAddress::GetAddrLength(addr); 38 socklen_t salen = SocketAddress::GetAddrLength(addr);
39 DWORD l = len; 39 DWORD l = len;
40 return WSAAddressToStringA(&addr->addr, 40 RawAddr& raw = const_cast<RawAddr&>(addr);
41 salen, 41 return WSAAddressToStringA(&raw.addr,
42 NULL, 42 salen,
43 address, 43 NULL,
44 &l) != 0; 44 address,
45 &l) != 0;
45 } 46 }
46 47
47 48
48 static Mutex* init_mutex = new Mutex(); 49 static Mutex* init_mutex = new Mutex();
49 static bool socket_initialized = false; 50 static bool socket_initialized = false;
50 51
51 bool Socket::Initialize() { 52 bool Socket::Initialize() {
52 MutexLocker lock(init_mutex); 53 MutexLocker lock(init_mutex);
53 if (socket_initialized) return true; 54 if (socket_initialized) return true;
54 int err; 55 int err;
(...skipping 13 matching lines...) Expand all
68 return client_socket->Available(); 69 return client_socket->Available();
69 } 70 }
70 71
71 72
72 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { 73 intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) {
73 Handle* handle = reinterpret_cast<Handle*>(fd); 74 Handle* handle = reinterpret_cast<Handle*>(fd);
74 return handle->Read(buffer, num_bytes); 75 return handle->Read(buffer, num_bytes);
75 } 76 }
76 77
77 78
78 intptr_t Socket::RecvFrom(intptr_t fd, void* buffer, intptr_t num_bytes, 79 intptr_t Socket::RecvFrom(
79 RawAddr* addr) { 80 intptr_t fd, void* buffer, intptr_t num_bytes, RawAddr* addr) {
80 Handle* handle = reinterpret_cast<Handle*>(fd); 81 Handle* handle = reinterpret_cast<Handle*>(fd);
81 socklen_t addr_len = sizeof(addr->ss); 82 socklen_t addr_len = sizeof(addr->ss);
82 return handle->RecvFrom(buffer, num_bytes, &addr->addr, addr_len); 83 return handle->RecvFrom(buffer, num_bytes, &addr->addr, addr_len);
83 } 84 }
84 85
85 86
86 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { 87 intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) {
87 Handle* handle = reinterpret_cast<Handle*>(fd); 88 Handle* handle = reinterpret_cast<Handle*>(fd);
88 return handle->Write(buffer, num_bytes); 89 return handle->Write(buffer, num_bytes);
89 } 90 }
90 91
91 92
92 intptr_t Socket::SendTo( 93 intptr_t Socket::SendTo(
93 intptr_t fd, const void* buffer, intptr_t num_bytes, RawAddr addr) { 94 intptr_t fd, const void* buffer, intptr_t num_bytes, const RawAddr& addr) {
94 Handle* handle = reinterpret_cast<Handle*>(fd); 95 Handle* handle = reinterpret_cast<Handle*>(fd);
96 RawAddr& raw = const_cast<RawAddr&>(addr);
95 return handle->SendTo( 97 return handle->SendTo(
96 buffer, num_bytes, &addr.addr, SocketAddress::GetAddrLength(&addr)); 98 buffer, num_bytes, &raw.addr, SocketAddress::GetAddrLength(addr));
97 } 99 }
98 100
99 101
100 intptr_t Socket::GetPort(intptr_t fd) { 102 intptr_t Socket::GetPort(intptr_t fd) {
101 ASSERT(reinterpret_cast<Handle*>(fd)->is_socket()); 103 ASSERT(reinterpret_cast<Handle*>(fd)->is_socket());
102 SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd); 104 SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd);
103 RawAddr raw; 105 RawAddr raw;
104 socklen_t size = sizeof(raw); 106 socklen_t size = sizeof(raw);
105 if (getsockname(socket_handle->socket(), 107 if (getsockname(socket_handle->socket(),
106 &raw.addr, 108 &raw.addr,
107 &size) == SOCKET_ERROR) { 109 &size) == SOCKET_ERROR) {
108 return 0; 110 return 0;
109 } 111 }
110 return SocketAddress::GetAddrPort(&raw); 112 return SocketAddress::GetAddrPort(raw);
111 } 113 }
112 114
113 115
114 SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) { 116 SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) {
115 ASSERT(reinterpret_cast<Handle*>(fd)->is_socket()); 117 ASSERT(reinterpret_cast<Handle*>(fd)->is_socket());
116 SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd); 118 SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd);
117 RawAddr raw; 119 RawAddr raw;
118 socklen_t size = sizeof(raw); 120 socklen_t size = sizeof(raw);
119 if (getpeername(socket_handle->socket(), 121 if (getpeername(socket_handle->socket(),
120 &raw.addr, 122 &raw.addr,
121 &size)) { 123 &size)) {
122 return NULL; 124 return NULL;
123 } 125 }
124 *port = SocketAddress::GetAddrPort(&raw); 126 *port = SocketAddress::GetAddrPort(raw);
125 // Clear the port before calling WSAAddressToString as WSAAddressToString 127 // Clear the port before calling WSAAddressToString as WSAAddressToString
126 // includes the port in the formatted string. 128 // includes the port in the formatted string.
127 SocketAddress::SetAddrPort(&raw, 0); 129 SocketAddress::SetAddrPort(&raw, 0);
128 return new SocketAddress(&raw.addr); 130 return new SocketAddress(&raw.addr);
129 } 131 }
130 132
131 133
132 static intptr_t Create(RawAddr addr) { 134 static intptr_t Create(const RawAddr& addr) {
133 SOCKET s = socket(addr.ss.ss_family, SOCK_STREAM, 0); 135 SOCKET s = socket(addr.ss.ss_family, SOCK_STREAM, 0);
134 if (s == INVALID_SOCKET) { 136 if (s == INVALID_SOCKET) {
135 return -1; 137 return -1;
136 } 138 }
137 139
138 linger l; 140 linger l;
139 l.l_onoff = 1; 141 l.l_onoff = 1;
140 l.l_linger = 10; 142 l.l_linger = 10;
141 int status = setsockopt(s, 143 int status = setsockopt(s,
142 SOL_SOCKET, 144 SOL_SOCKET,
143 SO_LINGER, 145 SO_LINGER,
144 reinterpret_cast<char*>(&l), 146 reinterpret_cast<char*>(&l),
145 sizeof(l)); 147 sizeof(l));
146 if (status != NO_ERROR) { 148 if (status != NO_ERROR) {
147 FATAL("Failed setting SO_LINGER on socket"); 149 FATAL("Failed setting SO_LINGER on socket");
148 } 150 }
149 151
150 ClientSocket* client_socket = new ClientSocket(s); 152 ClientSocket* client_socket = new ClientSocket(s);
151 return reinterpret_cast<intptr_t>(client_socket); 153 return reinterpret_cast<intptr_t>(client_socket);
152 } 154 }
153 155
154 156
155 static intptr_t Connect( 157 static intptr_t Connect(
156 intptr_t fd, RawAddr addr, const intptr_t port, RawAddr bind_addr) { 158 intptr_t fd, const RawAddr& addr, const RawAddr& bind_addr) {
157 ASSERT(reinterpret_cast<Handle*>(fd)->is_client_socket()); 159 ASSERT(reinterpret_cast<Handle*>(fd)->is_client_socket());
158 ClientSocket* handle = reinterpret_cast<ClientSocket*>(fd); 160 ClientSocket* handle = reinterpret_cast<ClientSocket*>(fd);
159 SOCKET s = handle->socket(); 161 SOCKET s = handle->socket();
160 162
161 int status = bind( 163 int status = bind(
162 s, &bind_addr.addr, SocketAddress::GetAddrLength(&bind_addr)); 164 s, &bind_addr.addr, SocketAddress::GetAddrLength(bind_addr));
163 if (status != NO_ERROR) { 165 if (status != NO_ERROR) {
164 int rc = WSAGetLastError(); 166 int rc = WSAGetLastError();
165 handle->mark_closed(); // Destructor asserts that socket is marked closed. 167 handle->mark_closed(); // Destructor asserts that socket is marked closed.
166 delete handle; 168 delete handle;
167 closesocket(s); 169 closesocket(s);
168 SetLastError(rc); 170 SetLastError(rc);
169 return -1; 171 return -1;
170 } 172 }
171 173
172 SocketAddress::SetAddrPort(&addr, port);
173
174 LPFN_CONNECTEX connectEx = NULL; 174 LPFN_CONNECTEX connectEx = NULL;
175 GUID guid_connect_ex = WSAID_CONNECTEX; 175 GUID guid_connect_ex = WSAID_CONNECTEX;
176 DWORD bytes; 176 DWORD bytes;
177 status = WSAIoctl(s, 177 status = WSAIoctl(s,
178 SIO_GET_EXTENSION_FUNCTION_POINTER, 178 SIO_GET_EXTENSION_FUNCTION_POINTER,
179 &guid_connect_ex, 179 &guid_connect_ex,
180 sizeof(guid_connect_ex), 180 sizeof(guid_connect_ex),
181 &connectEx, 181 &connectEx,
182 sizeof(connectEx), 182 sizeof(connectEx),
183 &bytes, 183 &bytes,
184 NULL, 184 NULL,
185 NULL); 185 NULL);
186 DWORD rc; 186 DWORD rc;
187 if (status != SOCKET_ERROR) { 187 if (status != SOCKET_ERROR) {
188 handle->EnsureInitialized(EventHandler::delegate()); 188 handle->EnsureInitialized(EventHandler::delegate());
189 189
190 OverlappedBuffer* overlapped = OverlappedBuffer::AllocateConnectBuffer(); 190 OverlappedBuffer* overlapped = OverlappedBuffer::AllocateConnectBuffer();
191 191
192 status = connectEx(s, 192 status = connectEx(s,
193 &addr.addr, 193 &addr.addr,
194 SocketAddress::GetAddrLength(&addr), 194 SocketAddress::GetAddrLength(addr),
195 NULL, 195 NULL,
196 0, 196 0,
197 NULL, 197 NULL,
198 overlapped->GetCleanOverlapped()); 198 overlapped->GetCleanOverlapped());
199 199
200 200
201 if (status == TRUE) { 201 if (status == TRUE) {
202 handle->ConnectComplete(overlapped); 202 handle->ConnectComplete(overlapped);
203 return fd; 203 return fd;
204 } else if (WSAGetLastError() == ERROR_IO_PENDING) { 204 } else if (WSAGetLastError() == ERROR_IO_PENDING) {
205 return fd; 205 return fd;
206 } 206 }
207 rc = WSAGetLastError(); 207 rc = WSAGetLastError();
208 // Cleanup in case of error. 208 // Cleanup in case of error.
209 OverlappedBuffer::DisposeBuffer(overlapped); 209 OverlappedBuffer::DisposeBuffer(overlapped);
210 } else { 210 } else {
211 rc = WSAGetLastError(); 211 rc = WSAGetLastError();
212 } 212 }
213 handle->Close(); 213 handle->Close();
214 delete handle; 214 delete handle;
215 SetLastError(rc); 215 SetLastError(rc);
216 return -1; 216 return -1;
217 } 217 }
218 218
219 219
220 intptr_t Socket::CreateConnect(const RawAddr& addr, const intptr_t port) { 220 intptr_t Socket::CreateConnect(const RawAddr& addr) {
221 intptr_t fd = Create(addr); 221 intptr_t fd = Create(addr);
222 if (fd < 0) { 222 if (fd < 0) {
223 return fd; 223 return fd;
224 } 224 }
225 225
226 RawAddr bind_addr; 226 RawAddr bind_addr;
227 memset(&bind_addr, 0, sizeof(bind_addr)); 227 memset(&bind_addr, 0, sizeof(bind_addr));
228 bind_addr.ss.ss_family = addr.ss.ss_family; 228 bind_addr.ss.ss_family = addr.ss.ss_family;
229 if (addr.ss.ss_family == AF_INET) { 229 if (addr.ss.ss_family == AF_INET) {
230 bind_addr.in.sin_addr.s_addr = INADDR_ANY; 230 bind_addr.in.sin_addr.s_addr = INADDR_ANY;
231 } else { 231 } else {
232 bind_addr.in6.sin6_addr = in6addr_any; 232 bind_addr.in6.sin6_addr = in6addr_any;
233 } 233 }
234 234
235 return Connect(fd, addr, port, bind_addr); 235 return Connect(fd, addr, bind_addr);
236 } 236 }
237 237
238 238
239 intptr_t Socket::CreateBindConnect(const RawAddr& addr, 239 intptr_t Socket::CreateBindConnect(const RawAddr& addr,
240 const intptr_t port,
241 const RawAddr& source_addr) { 240 const RawAddr& source_addr) {
242 intptr_t fd = Create(addr); 241 intptr_t fd = Create(addr);
243 if (fd < 0) { 242 if (fd < 0) {
244 return fd; 243 return fd;
245 } 244 }
246 245
247 return Connect(fd, addr, port, source_addr); 246 return Connect(fd, addr, source_addr);
248 } 247 }
249 248
250 249
251 void Socket::GetError(intptr_t fd, OSError* os_error) { 250 void Socket::GetError(intptr_t fd, OSError* os_error) {
252 Handle* handle = reinterpret_cast<Handle*>(fd); 251 Handle* handle = reinterpret_cast<Handle*>(fd);
253 os_error->SetCodeAndMessage(OSError::kSystem, handle->last_error()); 252 os_error->SetCodeAndMessage(OSError::kSystem, handle->last_error());
254 } 253 }
255 254
256 255
257 int Socket::GetType(intptr_t fd) { 256 int Socket::GetType(intptr_t fd) {
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) { 325 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) {
327 addresses->SetAt(i, new SocketAddress(c->ai_addr)); 326 addresses->SetAt(i, new SocketAddress(c->ai_addr));
328 i++; 327 i++;
329 } 328 }
330 } 329 }
331 freeaddrinfo(info); 330 freeaddrinfo(info);
332 return addresses; 331 return addresses;
333 } 332 }
334 333
335 334
336 bool Socket::ReverseLookup(RawAddr addr, 335 bool Socket::ReverseLookup(const RawAddr& addr,
337 char* host, 336 char* host,
338 intptr_t host_len, 337 intptr_t host_len,
339 OSError** os_error) { 338 OSError** os_error) {
340 ASSERT(host_len >= NI_MAXHOST); 339 ASSERT(host_len >= NI_MAXHOST);
341 int status = getnameinfo(&addr.addr, 340 int status = getnameinfo(&addr.addr,
342 SocketAddress::GetAddrLength(&addr), 341 SocketAddress::GetAddrLength(addr),
343 host, 342 host,
344 host_len, 343 host_len,
345 NULL, 344 NULL,
346 0, 345 0,
347 NI_NAMEREQD); 346 NI_NAMEREQD);
348 if (status != 0) { 347 if (status != 0) {
349 ASSERT(*os_error == NULL); 348 ASSERT(*os_error == NULL);
350 DWORD error_code = WSAGetLastError(); 349 DWORD error_code = WSAGetLastError();
351 SetLastError(error_code); 350 SetLastError(error_code);
352 *os_error = new OSError(); 351 *os_error = new OSError();
(...skipping 10 matching lines...) Expand all
363 result = InetPton(AF_INET, system_address, &addr->in.sin_addr); 362 result = InetPton(AF_INET, system_address, &addr->in.sin_addr);
364 } else { 363 } else {
365 ASSERT(type == SocketAddress::TYPE_IPV6); 364 ASSERT(type == SocketAddress::TYPE_IPV6);
366 result = InetPton(AF_INET6, system_address, &addr->in6.sin6_addr); 365 result = InetPton(AF_INET6, system_address, &addr->in6.sin6_addr);
367 } 366 }
368 free(const_cast<wchar_t*>(system_address)); 367 free(const_cast<wchar_t*>(system_address));
369 return result == 1; 368 return result == 1;
370 } 369 }
371 370
372 371
373 intptr_t Socket::CreateBindDatagram( 372 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) {
374 RawAddr* addr, intptr_t port, bool reuseAddress) { 373 SOCKET s = socket(addr.ss.ss_family, SOCK_DGRAM, IPPROTO_UDP);
375 SOCKET s = socket(addr->ss.ss_family, SOCK_DGRAM, IPPROTO_UDP);
376 if (s == INVALID_SOCKET) { 374 if (s == INVALID_SOCKET) {
377 return -1; 375 return -1;
378 } 376 }
379 377
380 int status; 378 int status;
381 if (reuseAddress) { 379 if (reuseAddress) {
382 BOOL optval = true; 380 BOOL optval = true;
383 status = setsockopt(s, 381 status = setsockopt(s,
384 SOL_SOCKET, 382 SOL_SOCKET,
385 SO_REUSEADDR, 383 SO_REUSEADDR,
386 reinterpret_cast<const char*>(&optval), 384 reinterpret_cast<const char*>(&optval),
387 sizeof(optval)); 385 sizeof(optval));
388 if (status == SOCKET_ERROR) { 386 if (status == SOCKET_ERROR) {
389 DWORD rc = WSAGetLastError(); 387 DWORD rc = WSAGetLastError();
390 closesocket(s); 388 closesocket(s);
391 SetLastError(rc); 389 SetLastError(rc);
392 return -1; 390 return -1;
393 } 391 }
394 } 392 }
395 393
396 SocketAddress::SetAddrPort(addr, port);
397
398 status = bind(s, 394 status = bind(s,
399 &addr->addr, 395 &addr.addr,
400 SocketAddress::GetAddrLength(addr)); 396 SocketAddress::GetAddrLength(addr));
401 if (status == SOCKET_ERROR) { 397 if (status == SOCKET_ERROR) {
402 DWORD rc = WSAGetLastError(); 398 DWORD rc = WSAGetLastError();
403 closesocket(s); 399 closesocket(s);
404 SetLastError(rc); 400 SetLastError(rc);
405 return -1; 401 return -1;
406 } 402 }
407 403
408 DatagramSocket* datagram_socket = new DatagramSocket(s); 404 DatagramSocket* datagram_socket = new DatagramSocket(s);
409 datagram_socket->EnsureInitialized(EventHandler::delegate()); 405 datagram_socket->EnsureInitialized(EventHandler::delegate());
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 StringUtils::WideToUtf8(a->FriendlyName), 457 StringUtils::WideToUtf8(a->FriendlyName),
462 a->Ipv6IfIndex)); 458 a->Ipv6IfIndex));
463 i++; 459 i++;
464 } 460 }
465 } 461 }
466 free(addrs); 462 free(addrs);
467 return addresses; 463 return addresses;
468 } 464 }
469 465
470 466
471 intptr_t ServerSocket::CreateBindListen(RawAddr addr, 467 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr,
472 intptr_t port,
473 intptr_t backlog, 468 intptr_t backlog,
474 bool v6_only) { 469 bool v6_only) {
475 SOCKET s = socket(addr.ss.ss_family, SOCK_STREAM, IPPROTO_TCP); 470 SOCKET s = socket(addr.ss.ss_family, SOCK_STREAM, IPPROTO_TCP);
476 if (s == INVALID_SOCKET) { 471 if (s == INVALID_SOCKET) {
477 return -1; 472 return -1;
478 } 473 }
479 474
480 BOOL optval = true; 475 BOOL optval = true;
481 int status = setsockopt(s, 476 int status = setsockopt(s,
482 SOL_SOCKET, 477 SOL_SOCKET,
483 SO_EXCLUSIVEADDRUSE, 478 SO_EXCLUSIVEADDRUSE,
484 reinterpret_cast<const char*>(&optval), 479 reinterpret_cast<const char*>(&optval),
485 sizeof(optval)); 480 sizeof(optval));
486 if (status == SOCKET_ERROR) { 481 if (status == SOCKET_ERROR) {
487 DWORD rc = WSAGetLastError(); 482 DWORD rc = WSAGetLastError();
488 closesocket(s); 483 closesocket(s);
489 SetLastError(rc); 484 SetLastError(rc);
490 return -1; 485 return -1;
491 } 486 }
492 487
493 if (addr.ss.ss_family == AF_INET6) { 488 if (addr.ss.ss_family == AF_INET6) {
494 optval = v6_only; 489 optval = v6_only;
495 setsockopt(s, 490 setsockopt(s,
496 IPPROTO_IPV6, 491 IPPROTO_IPV6,
497 IPV6_V6ONLY, 492 IPV6_V6ONLY,
498 reinterpret_cast<const char*>(&optval), 493 reinterpret_cast<const char*>(&optval),
499 sizeof(optval)); 494 sizeof(optval));
500 } 495 }
501 496
502 SocketAddress::SetAddrPort(&addr, port);
503 status = bind(s, 497 status = bind(s,
504 &addr.addr, 498 &addr.addr,
505 SocketAddress::GetAddrLength(&addr)); 499 SocketAddress::GetAddrLength(addr));
506 if (status == SOCKET_ERROR) { 500 if (status == SOCKET_ERROR) {
507 DWORD rc = WSAGetLastError(); 501 DWORD rc = WSAGetLastError();
508 closesocket(s); 502 closesocket(s);
509 SetLastError(rc); 503 SetLastError(rc);
510 return -1; 504 return -1;
511 } 505 }
512 506
513 ListenSocket* listen_socket = new ListenSocket(s); 507 ListenSocket* listen_socket = new ListenSocket(s);
514 508
515 // Test for invalid socket port 65535 (some browsers disallow it). 509 // Test for invalid socket port 65535 (some browsers disallow it).
516 if (port == 0 && 510 if (SocketAddress::GetAddrPort(addr) == 0 &&
517 Socket::GetPort(reinterpret_cast<intptr_t>(listen_socket)) == 65535) { 511 Socket::GetPort(reinterpret_cast<intptr_t>(listen_socket)) == 65535) {
518 // Don't close fd until we have created new. By doing that we ensure another 512 // Don't close fd until we have created new. By doing that we ensure another
519 // port. 513 // port.
520 intptr_t new_s = CreateBindListen(addr, 0, backlog, v6_only); 514 intptr_t new_s = CreateBindListen(addr, backlog, v6_only);
521 DWORD rc = WSAGetLastError(); 515 DWORD rc = WSAGetLastError();
522 closesocket(s); 516 closesocket(s);
523 delete listen_socket; 517 delete listen_socket;
524 SetLastError(rc); 518 SetLastError(rc);
525 return new_s; 519 return new_s;
526 } 520 }
527 521
528 status = listen(s, backlog > 0 ? backlog : SOMAXCONN); 522 status = listen(s, backlog > 0 ? backlog : SOMAXCONN);
529 if (status == SOCKET_ERROR) { 523 if (status == SOCKET_ERROR) {
530 DWORD rc = WSAGetLastError(); 524 DWORD rc = WSAGetLastError();
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
680 int on = enabled ? 1 : 0; 674 int on = enabled ? 1 : 0;
681 return setsockopt(handle->socket(), 675 return setsockopt(handle->socket(),
682 SOL_SOCKET, 676 SOL_SOCKET,
683 SO_BROADCAST, 677 SO_BROADCAST,
684 reinterpret_cast<char *>(&on), 678 reinterpret_cast<char *>(&on),
685 sizeof(on)) == 0; 679 sizeof(on)) == 0;
686 } 680 }
687 681
688 682
689 bool Socket::JoinMulticast( 683 bool Socket::JoinMulticast(
690 intptr_t fd, RawAddr* addr, RawAddr*, int interfaceIndex) { 684 intptr_t fd, const RawAddr& addr, const RawAddr&, int interfaceIndex) {
691 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); 685 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd);
692 int proto = addr->addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; 686 int proto = addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
693 struct group_req mreq; 687 struct group_req mreq;
694 mreq.gr_interface = interfaceIndex; 688 mreq.gr_interface = interfaceIndex;
695 memmove(&mreq.gr_group, &addr->ss, SocketAddress::GetAddrLength(addr)); 689 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr));
696 return setsockopt(handle->socket(), 690 return setsockopt(handle->socket(),
697 proto, 691 proto,
698 MCAST_JOIN_GROUP, 692 MCAST_JOIN_GROUP,
699 reinterpret_cast<char *>(&mreq), 693 reinterpret_cast<char *>(&mreq),
700 sizeof(mreq)) == 0; 694 sizeof(mreq)) == 0;
701 } 695 }
702 696
703 697
704 bool Socket::LeaveMulticast( 698 bool Socket::LeaveMulticast(
705 intptr_t fd, RawAddr* addr, RawAddr*, int interfaceIndex) { 699 intptr_t fd, const RawAddr& addr, const RawAddr&, int interfaceIndex) {
706 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd); 700 SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd);
707 int proto = addr->addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; 701 int proto = addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
708 struct group_req mreq; 702 struct group_req mreq;
709 mreq.gr_interface = interfaceIndex; 703 mreq.gr_interface = interfaceIndex;
710 memmove(&mreq.gr_group, &addr->ss, SocketAddress::GetAddrLength(addr)); 704 memmove(&mreq.gr_group, &addr.ss, SocketAddress::GetAddrLength(addr));
711 return setsockopt(handle->socket(), 705 return setsockopt(handle->socket(),
712 proto, 706 proto,
713 MCAST_LEAVE_GROUP, 707 MCAST_LEAVE_GROUP,
714 reinterpret_cast<char *>(&mreq), 708 reinterpret_cast<char *>(&mreq),
715 sizeof(mreq)) == 0; 709 sizeof(mreq)) == 0;
716 } 710 }
717 711
718 } // namespace bin 712 } // namespace bin
719 } // namespace dart 713 } // namespace dart
720 714
721 #endif // defined(TARGET_OS_WINDOWS) 715 #endif // defined(TARGET_OS_WINDOWS)
OLDNEW
« runtime/bin/socket_linux.cc ('K') | « runtime/bin/socket_macos.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698