| 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 "bin/builtin.h" | 5 #include "bin/builtin.h" |
| 6 #include "bin/eventhandler.h" | 6 #include "bin/eventhandler.h" |
| 7 #include "bin/socket.h" | 7 #include "bin/socket.h" |
| 8 | 8 |
| 9 bool Socket::Initialize() { | 9 bool Socket::Initialize() { |
| 10 int err; | 10 int err; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 ASSERT(reinterpret_cast<Handle*>(fd)->is_socket()); | 54 ASSERT(reinterpret_cast<Handle*>(fd)->is_socket()); |
| 55 SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd); | 55 SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd); |
| 56 struct sockaddr_in socket_address; | 56 struct sockaddr_in socket_address; |
| 57 socklen_t size = sizeof(socket_address); | 57 socklen_t size = sizeof(socket_address); |
| 58 if (getpeername(socket_handle->socket(), | 58 if (getpeername(socket_handle->socket(), |
| 59 reinterpret_cast<struct sockaddr *>(&socket_address), | 59 reinterpret_cast<struct sockaddr *>(&socket_address), |
| 60 &size)) { | 60 &size)) { |
| 61 fprintf(stderr, "Error getpeername: %s\n", strerror(errno)); | 61 fprintf(stderr, "Error getpeername: %s\n", strerror(errno)); |
| 62 return false; | 62 return false; |
| 63 } | 63 } |
| 64 if (inet_ntop(socket_address.sin_family, | 64 *port = ntohs(socket_address.sin_port); |
| 65 reinterpret_cast<void *>(&socket_address.sin_addr), | 65 // Clear the port before calling WSAAddressToString as WSAAddressToString |
| 66 host, | 66 // includes the port in the formatted string. |
| 67 INET_ADDRSTRLEN) == NULL) { | 67 socket_address.sin_port = 0; |
| 68 fprintf(stderr, "Error inet_ntop: %s\n", strerror(errno)); | 68 DWORD len = INET_ADDRSTRLEN; |
| 69 int err = WSAAddressToString(reinterpret_cast<LPSOCKADDR>(&socket_address), |
| 70 sizeof(socket_address), |
| 71 NULL, |
| 72 host, |
| 73 &len); |
| 74 if (err != 0) { |
| 75 fprintf(stderr, "Error WSAAddressToString: %d\n", WSAGetLastError()); |
| 69 return false; | 76 return false; |
| 70 } | 77 } |
| 71 *port = ntohs(socket_address.sin_port); | |
| 72 return true; | 78 return true; |
| 73 } | 79 } |
| 74 | 80 |
| 75 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { | 81 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { |
| 76 SOCKET s = socket(AF_INET, SOCK_STREAM, 0); | 82 SOCKET s = socket(AF_INET, SOCK_STREAM, 0); |
| 77 if (s == INVALID_SOCKET) { | 83 if (s == INVALID_SOCKET) { |
| 78 return -1; | 84 return -1; |
| 79 } | 85 } |
| 80 | 86 |
| 81 linger l; | 87 linger l; |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 if (status == SOCKET_ERROR) { | 244 if (status == SOCKET_ERROR) { |
| 239 DWORD rc = WSAGetLastError(); | 245 DWORD rc = WSAGetLastError(); |
| 240 closesocket(s); | 246 closesocket(s); |
| 241 SetLastError(rc); | 247 SetLastError(rc); |
| 242 return -1; | 248 return -1; |
| 243 } | 249 } |
| 244 | 250 |
| 245 ListenSocket* listen_socket = new ListenSocket(s); | 251 ListenSocket* listen_socket = new ListenSocket(s); |
| 246 return reinterpret_cast<intptr_t>(listen_socket); | 252 return reinterpret_cast<intptr_t>(listen_socket); |
| 247 } | 253 } |
| OLD | NEW |