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" |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 intptr_t Socket::Create(RawAddr addr) { | 132 static intptr_t Create(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 intptr_t Socket::Connect(intptr_t fd, RawAddr addr, const intptr_t port) { | 155 static intptr_t Connect( |
| 156 intptr_t fd, RawAddr addr, const intptr_t port, RawAddr bind_addr) { |
156 ASSERT(reinterpret_cast<Handle*>(fd)->is_client_socket()); | 157 ASSERT(reinterpret_cast<Handle*>(fd)->is_client_socket()); |
157 ClientSocket* handle = reinterpret_cast<ClientSocket*>(fd); | 158 ClientSocket* handle = reinterpret_cast<ClientSocket*>(fd); |
158 SOCKET s = handle->socket(); | 159 SOCKET s = handle->socket(); |
159 | 160 |
160 RawAddr bind_addr; | |
161 memset(&bind_addr, 0, sizeof(bind_addr)); | |
162 bind_addr.ss.ss_family = addr.ss.ss_family; | |
163 if (addr.ss.ss_family == AF_INET) { | |
164 bind_addr.in.sin_addr.s_addr = INADDR_ANY; | |
165 } else { | |
166 bind_addr.in6.sin6_addr = in6addr_any; | |
167 } | |
168 int status = bind( | 161 int status = bind( |
169 s, &bind_addr.addr, SocketAddress::GetAddrLength(&bind_addr)); | 162 s, &bind_addr.addr, SocketAddress::GetAddrLength(&bind_addr)); |
170 if (status != NO_ERROR) { | 163 if (status != NO_ERROR) { |
171 int rc = WSAGetLastError(); | 164 int rc = WSAGetLastError(); |
| 165 handle->mark_closed(); // Destructor asserts that socket is marked closed. |
172 delete handle; | 166 delete handle; |
173 closesocket(s); | 167 closesocket(s); |
174 SetLastError(rc); | 168 SetLastError(rc); |
175 return -1; | 169 return -1; |
176 } | 170 } |
177 | 171 |
178 SocketAddress::SetAddrPort(&addr, port); | 172 SocketAddress::SetAddrPort(&addr, port); |
179 | 173 |
180 LPFN_CONNECTEX connectEx = NULL; | 174 LPFN_CONNECTEX connectEx = NULL; |
181 GUID guid_connect_ex = WSAID_CONNECTEX; | 175 GUID guid_connect_ex = WSAID_CONNECTEX; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 } else { | 210 } else { |
217 rc = WSAGetLastError(); | 211 rc = WSAGetLastError(); |
218 } | 212 } |
219 handle->Close(); | 213 handle->Close(); |
220 delete handle; | 214 delete handle; |
221 SetLastError(rc); | 215 SetLastError(rc); |
222 return -1; | 216 return -1; |
223 } | 217 } |
224 | 218 |
225 | 219 |
226 intptr_t Socket::CreateConnect(RawAddr addr, const intptr_t port) { | 220 intptr_t Socket::CreateConnect(const RawAddr& addr, const intptr_t port) { |
227 intptr_t fd = Socket::Create(addr); | 221 intptr_t fd = Create(addr); |
228 if (fd < 0) { | 222 if (fd < 0) { |
229 return fd; | 223 return fd; |
230 } | 224 } |
231 | 225 |
232 return Socket::Connect(fd, addr, port); | 226 RawAddr bind_addr; |
| 227 memset(&bind_addr, 0, sizeof(bind_addr)); |
| 228 bind_addr.ss.ss_family = addr.ss.ss_family; |
| 229 if (addr.ss.ss_family == AF_INET) { |
| 230 bind_addr.in.sin_addr.s_addr = INADDR_ANY; |
| 231 } else { |
| 232 bind_addr.in6.sin6_addr = in6addr_any; |
| 233 } |
| 234 |
| 235 return Connect(fd, addr, port, bind_addr); |
233 } | 236 } |
234 | 237 |
235 | 238 |
| 239 intptr_t Socket::CreateBindConnect(const RawAddr& addr, |
| 240 const intptr_t port, |
| 241 const RawAddr& source_addr) { |
| 242 intptr_t fd = Create(addr); |
| 243 if (fd < 0) { |
| 244 return fd; |
| 245 } |
| 246 |
| 247 return Connect(fd, addr, port, source_addr); |
| 248 } |
| 249 |
| 250 |
236 void Socket::GetError(intptr_t fd, OSError* os_error) { | 251 void Socket::GetError(intptr_t fd, OSError* os_error) { |
237 Handle* handle = reinterpret_cast<Handle*>(fd); | 252 Handle* handle = reinterpret_cast<Handle*>(fd); |
238 os_error->SetCodeAndMessage(OSError::kSystem, handle->last_error()); | 253 os_error->SetCodeAndMessage(OSError::kSystem, handle->last_error()); |
239 } | 254 } |
240 | 255 |
241 | 256 |
242 int Socket::GetType(intptr_t fd) { | 257 int Socket::GetType(intptr_t fd) { |
243 Handle* handle = reinterpret_cast<Handle*>(fd); | 258 Handle* handle = reinterpret_cast<Handle*>(fd); |
244 switch (GetFileType(handle->handle())) { | 259 switch (GetFileType(handle->handle())) { |
245 case FILE_TYPE_CHAR: return File::kTerminal; | 260 case FILE_TYPE_CHAR: return File::kTerminal; |
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
697 proto, | 712 proto, |
698 MCAST_LEAVE_GROUP, | 713 MCAST_LEAVE_GROUP, |
699 reinterpret_cast<char *>(&mreq), | 714 reinterpret_cast<char *>(&mreq), |
700 sizeof(mreq)) == 0; | 715 sizeof(mreq)) == 0; |
701 } | 716 } |
702 | 717 |
703 } // namespace bin | 718 } // namespace bin |
704 } // namespace dart | 719 } // namespace dart |
705 | 720 |
706 #endif // defined(TARGET_OS_WINDOWS) | 721 #endif // defined(TARGET_OS_WINDOWS) |
OLD | NEW |