| 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> | 5 #include <winsock2.h> |
| 6 #include <ws2tcpip.h> | 6 #include <ws2tcpip.h> |
| 7 #include <mswsock.h> | 7 #include <mswsock.h> |
| 8 | 8 |
| 9 #include "bin/builtin.h" | 9 #include "bin/builtin.h" |
| 10 #include "bin/eventhandler.h" | 10 #include "bin/eventhandler.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 if (getsockname(socket_handle->socket(), | 47 if (getsockname(socket_handle->socket(), |
| 48 reinterpret_cast<struct sockaddr *>(&socket_address), | 48 reinterpret_cast<struct sockaddr *>(&socket_address), |
| 49 &size)) { | 49 &size)) { |
| 50 fprintf(stderr, "Error getsockname: %s\n", strerror(errno)); | 50 fprintf(stderr, "Error getsockname: %s\n", strerror(errno)); |
| 51 return 0; | 51 return 0; |
| 52 } | 52 } |
| 53 return ntohs(socket_address.sin_port); | 53 return ntohs(socket_address.sin_port); |
| 54 } | 54 } |
| 55 | 55 |
| 56 | 56 |
| 57 intptr_t Socket::GetRemotePort(intptr_t fd) { | 57 bool Socket::GetRemotePeer(intptr_t fd, const char **host, intptr_t *port) { |
| 58 ASSERT(reinterpret_cast<Handle*>(fd)->is_socket()); | 58 ASSERT(reinterpret_cast<Handle*>(fd)->is_socket()); |
| 59 SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd); | 59 SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd); |
| 60 struct sockaddr_in socket_address; | 60 struct sockaddr_in socket_address; |
| 61 socklen_t size = sizeof(socket_address); | 61 socklen_t size = sizeof(socket_address); |
| 62 if (getpeername(socket_handle->socket(), | 62 if (getpeername(socket_handle->socket(), |
| 63 reinterpret_cast<struct sockaddr *>(&socket_address), | 63 reinterpret_cast<struct sockaddr *>(&socket_address), |
| 64 &size)) { | 64 &size)) { |
| 65 fprintf(stderr, "Error getpeername: %s\n", strerror(errno)); | 65 fprintf(stderr, "Error getpeername: %s\n", strerror(errno)); |
| 66 return 0; | 66 return false; |
| 67 } | 67 } |
| 68 return ntohs(socket_address.sin_port); | 68 char* buffer = new char[INET_ADDRSTRLEN]; |
| 69 } | 69 if (inet_ntop(socket_address.sin_family, |
| 70 reinterpret_cast<const void *>(&socket_address.sin_addr), |
| 71 buffer, |
| 72 INET_ADDRSTRLEN) == NULL) { |
| 73 fprintf(stderr, "Error inet_ntop: %s\n", strerror(errno)); |
| 74 delete[] buffer; |
| 75 return false; |
| 76 } |
| 77 *host = buffer; |
| 78 *port = ntohs(socket_address.sin_port); |
| 79 return true; |
| 70 | 80 |
| 71 | 81 |
| 72 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { | 82 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { |
| 73 SOCKET s = socket(AF_INET, SOCK_STREAM, 0); | 83 SOCKET s = socket(AF_INET, SOCK_STREAM, 0); |
| 74 if (s == INVALID_SOCKET) { | 84 if (s == INVALID_SOCKET) { |
| 75 return -1; | 85 return -1; |
| 76 } | 86 } |
| 77 | 87 |
| 78 linger l; | 88 linger l; |
| 79 l.l_onoff = 1; | 89 l.l_onoff = 1; |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 if (status == SOCKET_ERROR) { | 245 if (status == SOCKET_ERROR) { |
| 236 DWORD rc = WSAGetLastError(); | 246 DWORD rc = WSAGetLastError(); |
| 237 closesocket(s); | 247 closesocket(s); |
| 238 SetLastError(rc); | 248 SetLastError(rc); |
| 239 return -1; | 249 return -1; |
| 240 } | 250 } |
| 241 | 251 |
| 242 ListenSocket* listen_socket = new ListenSocket(s); | 252 ListenSocket* listen_socket = new ListenSocket(s); |
| 243 return reinterpret_cast<intptr_t>(listen_socket); | 253 return reinterpret_cast<intptr_t>(listen_socket); |
| 244 } | 254 } |
| OLD | NEW |