| 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/log.h" | 7 #include "bin/log.h" |
| 8 #include "bin/socket.h" | 8 #include "bin/socket.h" |
| 9 | 9 |
| 10 bool Socket::Initialize() { | 10 bool Socket::Initialize() { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 if (getpeername(socket_handle->socket(), | 59 if (getpeername(socket_handle->socket(), |
| 60 reinterpret_cast<struct sockaddr *>(&socket_address), | 60 reinterpret_cast<struct sockaddr *>(&socket_address), |
| 61 &size)) { | 61 &size)) { |
| 62 Log::PrintErr("Error getpeername: %s\n", strerror(errno)); | 62 Log::PrintErr("Error getpeername: %s\n", strerror(errno)); |
| 63 return false; | 63 return false; |
| 64 } | 64 } |
| 65 *port = ntohs(socket_address.sin_port); | 65 *port = ntohs(socket_address.sin_port); |
| 66 // Clear the port before calling WSAAddressToString as WSAAddressToString | 66 // Clear the port before calling WSAAddressToString as WSAAddressToString |
| 67 // includes the port in the formatted string. | 67 // includes the port in the formatted string. |
| 68 socket_address.sin_port = 0; | 68 socket_address.sin_port = 0; |
| 69 wchar_t* unicode_host = StringUtils::Utf8ToWide(host); | |
| 70 DWORD len = INET_ADDRSTRLEN; | 69 DWORD len = INET_ADDRSTRLEN; |
| 71 int err = WSAAddressToStringW(reinterpret_cast<LPSOCKADDR>(&socket_address), | 70 int err = WSAAddressToString(reinterpret_cast<LPSOCKADDR>(&socket_address), |
| 72 sizeof(socket_address), | 71 sizeof(socket_address), |
| 73 NULL, | 72 NULL, |
| 74 unicode_host, | 73 host, |
| 75 &len); | 74 &len); |
| 76 free(unicode_host); | |
| 77 if (err != 0) { | 75 if (err != 0) { |
| 78 Log::PrintErr("Error WSAAddressToString: %d\n", WSAGetLastError()); | 76 Log::PrintErr("Error WSAAddressToString: %d\n", WSAGetLastError()); |
| 79 return false; | 77 return false; |
| 80 } | 78 } |
| 81 return true; | 79 return true; |
| 82 } | 80 } |
| 83 | 81 |
| 84 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { | 82 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { |
| 85 SOCKET s = socket(AF_INET, SOCK_STREAM, 0); | 83 SOCKET s = socket(AF_INET, SOCK_STREAM, 0); |
| 86 if (s == INVALID_SOCKET) { | 84 if (s == INVALID_SOCKET) { |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 SetLastError(error_code); | 188 SetLastError(error_code); |
| 191 *os_error = new OSError(); | 189 *os_error = new OSError(); |
| 192 return NULL; | 190 return NULL; |
| 193 } | 191 } |
| 194 // Convert the address into IPv4 dotted decimal notation. | 192 // Convert the address into IPv4 dotted decimal notation. |
| 195 char* buffer = reinterpret_cast<char*>(malloc(INET_ADDRSTRLEN)); | 193 char* buffer = reinterpret_cast<char*>(malloc(INET_ADDRSTRLEN)); |
| 196 sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(info->ai_addr); | 194 sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(info->ai_addr); |
| 197 | 195 |
| 198 // Clear the port before calling WSAAddressToString as WSAAddressToString | 196 // Clear the port before calling WSAAddressToString as WSAAddressToString |
| 199 // includes the port in the formatted string. | 197 // includes the port in the formatted string. |
| 200 wchar_t* unicode_buffer = StringUtils::Utf8ToWide(buffer); | |
| 201 DWORD len = INET_ADDRSTRLEN; | 198 DWORD len = INET_ADDRSTRLEN; |
| 202 int err = WSAAddressToStringW(reinterpret_cast<LPSOCKADDR>(sockaddr), | 199 int err = WSAAddressToString(reinterpret_cast<LPSOCKADDR>(sockaddr), |
| 203 sizeof(sockaddr_in), | 200 sizeof(sockaddr_in), |
| 204 NULL, | 201 NULL, |
| 205 unicode_buffer, | 202 buffer, |
| 206 &len); | 203 &len); |
| 207 free(unicode_buffer); | |
| 208 if (err != 0) { | 204 if (err != 0) { |
| 209 free(buffer); | 205 free(buffer); |
| 210 return NULL; | 206 return NULL; |
| 211 } | 207 } |
| 212 return buffer; | 208 return buffer; |
| 213 } | 209 } |
| 214 | 210 |
| 215 | 211 |
| 216 intptr_t ServerSocket::CreateBindListen(const char* host, | 212 intptr_t ServerSocket::CreateBindListen(const char* host, |
| 217 intptr_t port, | 213 intptr_t port, |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 if (status == SOCKET_ERROR) { | 254 if (status == SOCKET_ERROR) { |
| 259 DWORD rc = WSAGetLastError(); | 255 DWORD rc = WSAGetLastError(); |
| 260 closesocket(s); | 256 closesocket(s); |
| 261 SetLastError(rc); | 257 SetLastError(rc); |
| 262 return -1; | 258 return -1; |
| 263 } | 259 } |
| 264 | 260 |
| 265 ListenSocket* listen_socket = new ListenSocket(s); | 261 ListenSocket* listen_socket = new ListenSocket(s); |
| 266 return reinterpret_cast<intptr_t>(listen_socket); | 262 return reinterpret_cast<intptr_t>(listen_socket); |
| 267 } | 263 } |
| OLD | NEW |