Chromium Code Reviews| 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 return false; | 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()); | |
|
Mads Ager (google)
2012/05/21 13:24:34
Return false?
Søren Gjesse
2012/05/21 13:28:57
Good catch, thanks.
| |
| 70 } | 76 } |
| 71 *port = ntohs(socket_address.sin_port); | |
| 72 return true; | 77 return true; |
| 73 } | 78 } |
| 74 | 79 |
| 75 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { | 80 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { |
| 76 SOCKET s = socket(AF_INET, SOCK_STREAM, 0); | 81 SOCKET s = socket(AF_INET, SOCK_STREAM, 0); |
| 77 if (s == INVALID_SOCKET) { | 82 if (s == INVALID_SOCKET) { |
| 78 return -1; | 83 return -1; |
| 79 } | 84 } |
| 80 | 85 |
| 81 linger l; | 86 linger l; |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 if (status == SOCKET_ERROR) { | 243 if (status == SOCKET_ERROR) { |
| 239 DWORD rc = WSAGetLastError(); | 244 DWORD rc = WSAGetLastError(); |
| 240 closesocket(s); | 245 closesocket(s); |
| 241 SetLastError(rc); | 246 SetLastError(rc); |
| 242 return -1; | 247 return -1; |
| 243 } | 248 } |
| 244 | 249 |
| 245 ListenSocket* listen_socket = new ListenSocket(s); | 250 ListenSocket* listen_socket = new ListenSocket(s); |
| 246 return reinterpret_cast<intptr_t>(listen_socket); | 251 return reinterpret_cast<intptr_t>(listen_socket); |
| 247 } | 252 } |
| OLD | NEW |