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

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

Powered by Google App Engine
This is Rietveld 408576698