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

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

Issue 908873002: Add support to specify the source address for socket connect (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix Windows build 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
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"
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
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();
172 delete handle; 165 delete handle;
173 closesocket(s); 166 closesocket(s);
174 SetLastError(rc); 167 SetLastError(rc);
175 return -1; 168 return -1;
176 } 169 }
177 170
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 rc = WSAGetLastError(); 210 rc = WSAGetLastError();
218 } 211 }
219 handle->Close(); 212 handle->Close();
220 delete handle; 213 delete handle;
221 SetLastError(rc); 214 SetLastError(rc);
222 return -1; 215 return -1;
223 } 216 }
224 217
225 218
226 intptr_t Socket::CreateConnect(RawAddr addr, const intptr_t port) { 219 intptr_t Socket::CreateConnect(RawAddr addr, const intptr_t port) {
227 intptr_t fd = Socket::Create(addr); 220 intptr_t fd = Create(addr);
228 if (fd < 0) { 221 if (fd < 0) {
229 return fd; 222 return fd;
230 } 223 }
231 224
232 return Socket::Connect(fd, addr, port); 225 RawAddr bind_addr;
226 memset(&bind_addr, 0, sizeof(bind_addr));
227 bind_addr.ss.ss_family = addr.ss.ss_family;
228 if (addr.ss.ss_family == AF_INET) {
229 bind_addr.in.sin_addr.s_addr = INADDR_ANY;
230 } else {
231 bind_addr.in6.sin6_addr = in6addr_any;
232 }
233
234 return Connect(fd, addr, port, bind_addr);
233 } 235 }
234 236
235 237
238 intptr_t Socket::CreateBindConnect(RawAddr addr,
239 const intptr_t port,
240 RawAddr source_addr) {
241 intptr_t fd = Create(addr);
242 if (fd < 0) {
243 return fd;
244 }
245
246 return Connect(fd, addr, port, source_addr);
247 }
248
249
236 void Socket::GetError(intptr_t fd, OSError* os_error) { 250 void Socket::GetError(intptr_t fd, OSError* os_error) {
237 Handle* handle = reinterpret_cast<Handle*>(fd); 251 Handle* handle = reinterpret_cast<Handle*>(fd);
238 os_error->SetCodeAndMessage(OSError::kSystem, handle->last_error()); 252 os_error->SetCodeAndMessage(OSError::kSystem, handle->last_error());
239 } 253 }
240 254
241 255
242 int Socket::GetType(intptr_t fd) { 256 int Socket::GetType(intptr_t fd) {
243 Handle* handle = reinterpret_cast<Handle*>(fd); 257 Handle* handle = reinterpret_cast<Handle*>(fd);
244 switch (GetFileType(handle->handle())) { 258 switch (GetFileType(handle->handle())) {
245 case FILE_TYPE_CHAR: return File::kTerminal; 259 case FILE_TYPE_CHAR: return File::kTerminal;
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after
718 proto, 732 proto,
719 MCAST_LEAVE_GROUP, 733 MCAST_LEAVE_GROUP,
720 reinterpret_cast<char *>(&mreq), 734 reinterpret_cast<char *>(&mreq),
721 sizeof(mreq)) == 0; 735 sizeof(mreq)) == 0;
722 } 736 }
723 737
724 } // namespace bin 738 } // namespace bin
725 } // namespace dart 739 } // namespace dart
726 740
727 #endif // defined(TARGET_OS_WINDOWS) 741 #endif // defined(TARGET_OS_WINDOWS)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698