OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 <winsock2.h> | |
6 #include <ws2tcpip.h> | |
7 #include <mswsock.h> | |
8 | |
9 #include "bin/builtin.h" | 5 #include "bin/builtin.h" |
10 #include "bin/eventhandler.h" | 6 #include "bin/eventhandler.h" |
11 #include "bin/socket.h" | 7 #include "bin/socket.h" |
12 | 8 |
13 bool Socket::Initialize() { | 9 bool Socket::Initialize() { |
14 int err; | 10 int err; |
15 WSADATA winsock_data; | 11 WSADATA winsock_data; |
16 WORD version_requested = MAKEWORD(1, 0); | 12 WORD version_requested = MAKEWORD(1, 0); |
17 err = WSAStartup(version_requested, &winsock_data); | 13 err = WSAStartup(version_requested, &winsock_data); |
18 if (err != 0) { | 14 if (err != 0) { |
(...skipping 29 matching lines...) Expand all Loading... |
48 reinterpret_cast<struct sockaddr *>(&socket_address), | 44 reinterpret_cast<struct sockaddr *>(&socket_address), |
49 &size)) { | 45 &size)) { |
50 fprintf(stderr, "Error getsockname: %s\n", strerror(errno)); | 46 fprintf(stderr, "Error getsockname: %s\n", strerror(errno)); |
51 return 0; | 47 return 0; |
52 } | 48 } |
53 return ntohs(socket_address.sin_port); | 49 return ntohs(socket_address.sin_port); |
54 } | 50 } |
55 | 51 |
56 | 52 |
57 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { | 53 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { |
58 ASSERT(DART_INET_ADDRSTRLEN >= INET_ADDRSTRLEN); | |
59 ASSERT(reinterpret_cast<Handle*>(fd)->is_socket()); | 54 ASSERT(reinterpret_cast<Handle*>(fd)->is_socket()); |
60 SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd); | 55 SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd); |
61 struct sockaddr_in socket_address; | 56 struct sockaddr_in socket_address; |
62 socklen_t size = sizeof(socket_address); | 57 socklen_t size = sizeof(socket_address); |
63 if (getpeername(socket_handle->socket(), | 58 if (getpeername(socket_handle->socket(), |
64 reinterpret_cast<struct sockaddr *>(&socket_address), | 59 reinterpret_cast<struct sockaddr *>(&socket_address), |
65 &size)) { | 60 &size)) { |
66 fprintf(stderr, "Error getpeername: %s\n", strerror(errno)); | 61 fprintf(stderr, "Error getpeername: %s\n", strerror(errno)); |
67 return false; | 62 return false; |
68 } | 63 } |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 if (status == SOCKET_ERROR) { | 238 if (status == SOCKET_ERROR) { |
244 DWORD rc = WSAGetLastError(); | 239 DWORD rc = WSAGetLastError(); |
245 closesocket(s); | 240 closesocket(s); |
246 SetLastError(rc); | 241 SetLastError(rc); |
247 return -1; | 242 return -1; |
248 } | 243 } |
249 | 244 |
250 ListenSocket* listen_socket = new ListenSocket(s); | 245 ListenSocket* listen_socket = new ListenSocket(s); |
251 return reinterpret_cast<intptr_t>(listen_socket); | 246 return reinterpret_cast<intptr_t>(listen_socket); |
252 } | 247 } |
OLD | NEW |