| 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 <arpa/inet.h> | 5 #include <arpa/inet.h> |
| 6 #include <errno.h> | 6 #include <errno.h> |
| 7 #include <netdb.h> | 7 #include <netdb.h> |
| 8 #include <stdio.h> | 8 #include <stdio.h> |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 #include <string.h> | 10 #include <string.h> |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 getsockname(fd, | 95 getsockname(fd, |
| 96 reinterpret_cast<struct sockaddr *>(&socket_address), | 96 reinterpret_cast<struct sockaddr *>(&socket_address), |
| 97 &size))) { | 97 &size))) { |
| 98 fprintf(stderr, "Error getsockname: %s\n", strerror(errno)); | 98 fprintf(stderr, "Error getsockname: %s\n", strerror(errno)); |
| 99 return 0; | 99 return 0; |
| 100 } | 100 } |
| 101 return ntohs(socket_address.sin_port); | 101 return ntohs(socket_address.sin_port); |
| 102 } | 102 } |
| 103 | 103 |
| 104 | 104 |
| 105 intptr_t Socket::GetRemotePort(intptr_t fd) { | 105 bool Socket::GetRemotePeer(intptr_t fd, const char **host, intptr_t *port) { |
| 106 ASSERT(fd >= 0); | 106 ASSERT(fd >= 0); |
| 107 struct sockaddr_in socket_address; | 107 struct sockaddr_in socket_address; |
| 108 socklen_t size = sizeof(socket_address); | 108 socklen_t size = sizeof(socket_address); |
| 109 if (TEMP_FAILURE_RETRY( | 109 if (TEMP_FAILURE_RETRY( |
| 110 getpeername(fd, | 110 getpeername(fd, |
| 111 reinterpret_cast<struct sockaddr *>(&socket_address), | 111 reinterpret_cast<struct sockaddr *>(&socket_address), |
| 112 &size))) { | 112 &size))) { |
| 113 fprintf(stderr, "Error getpeername: %s\n", strerror(errno)); | 113 fprintf(stderr, "Error getpeername: %s\n", strerror(errno)); |
| 114 return 0; | 114 return false; |
| 115 } | 115 } |
| 116 return ntohs(socket_address.sin_port); | 116 char* buffer = new char[INET_ADDRSTRLEN]; |
| 117 if (inet_ntop(socket_address.sin_family, |
| 118 reinterpret_cast<const void *>(&socket_address.sin_addr), |
| 119 buffer, |
| 120 INET_ADDRSTRLEN) == NULL) { |
| 121 fprintf(stderr, "Error inet_ntop: %s\n", strerror(errno)); |
| 122 delete[] buffer; |
| 123 return false; |
| 124 } |
| 125 *host = buffer; |
| 126 *port = ntohs(socket_address.sin_port); |
| 127 return true; |
| 117 } | 128 } |
| 118 | 129 |
| 119 | 130 |
| 120 void Socket::GetError(intptr_t fd, OSError* os_error) { | 131 void Socket::GetError(intptr_t fd, OSError* os_error) { |
| 121 int len = sizeof(errno); | 132 int len = sizeof(errno); |
| 122 getsockopt(fd, | 133 getsockopt(fd, |
| 123 SOL_SOCKET, | 134 SOL_SOCKET, |
| 124 SO_ERROR, | 135 SO_ERROR, |
| 125 &errno, | 136 &errno, |
| 126 reinterpret_cast<socklen_t*>(&len)); | 137 reinterpret_cast<socklen_t*>(&len)); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 // error. We got woken up from the poll on the listening socket, | 237 // error. We got woken up from the poll on the listening socket, |
| 227 // but there is no connection ready to be accepted. | 238 // but there is no connection ready to be accepted. |
| 228 ASSERT(kTemporaryFailure != -1); | 239 ASSERT(kTemporaryFailure != -1); |
| 229 socket = kTemporaryFailure; | 240 socket = kTemporaryFailure; |
| 230 } | 241 } |
| 231 } else { | 242 } else { |
| 232 FDUtils::SetNonBlocking(socket); | 243 FDUtils::SetNonBlocking(socket); |
| 233 } | 244 } |
| 234 return socket; | 245 return socket; |
| 235 } | 246 } |
| OLD | NEW |