| 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 <errno.h> | 5 #include <errno.h> |
| 6 #include <stdio.h> | 6 #include <stdio.h> |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 #include <unistd.h> | 9 #include <unistd.h> |
| 10 | 10 |
| 11 #include "bin/fdutils.h" | 11 #include "bin/fdutils.h" |
| 12 #include "bin/log.h" |
| 12 #include "bin/socket.h" | 13 #include "bin/socket.h" |
| 13 | 14 |
| 14 | 15 |
| 15 bool Socket::Initialize() { | 16 bool Socket::Initialize() { |
| 16 // Nothing to do on Mac OS. | 17 // Nothing to do on Mac OS. |
| 17 return true; | 18 return true; |
| 18 } | 19 } |
| 19 | 20 |
| 20 | 21 |
| 21 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { | 22 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { |
| 22 intptr_t fd; | 23 intptr_t fd; |
| 23 struct hostent* server; | 24 struct hostent* server; |
| 24 struct sockaddr_in server_address; | 25 struct sockaddr_in server_address; |
| 25 | 26 |
| 26 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); | 27 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); |
| 27 if (fd < 0) { | 28 if (fd < 0) { |
| 28 fprintf(stderr, "Error CreateConnect: %s\n", strerror(errno)); | 29 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); |
| 29 return -1; | 30 return -1; |
| 30 } | 31 } |
| 31 | 32 |
| 32 FDUtils::SetCloseOnExec(fd); | 33 FDUtils::SetCloseOnExec(fd); |
| 33 FDUtils::SetNonBlocking(fd); | 34 FDUtils::SetNonBlocking(fd); |
| 34 | 35 |
| 35 server = gethostbyname(host); | 36 server = gethostbyname(host); |
| 36 if (server == NULL) { | 37 if (server == NULL) { |
| 37 TEMP_FAILURE_RETRY(close(fd)); | 38 TEMP_FAILURE_RETRY(close(fd)); |
| 38 fprintf(stderr, "Error CreateConnect: %s\n", strerror(errno)); | 39 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); |
| 39 return -1; | 40 return -1; |
| 40 } | 41 } |
| 41 | 42 |
| 42 server_address.sin_family = AF_INET; | 43 server_address.sin_family = AF_INET; |
| 43 server_address.sin_port = htons(port); | 44 server_address.sin_port = htons(port); |
| 44 bcopy(server->h_addr, &server_address.sin_addr.s_addr, server->h_length); | 45 bcopy(server->h_addr, &server_address.sin_addr.s_addr, server->h_length); |
| 45 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); | 46 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); |
| 46 intptr_t result = TEMP_FAILURE_RETRY( | 47 intptr_t result = TEMP_FAILURE_RETRY( |
| 47 connect(fd, | 48 connect(fd, |
| 48 reinterpret_cast<struct sockaddr *>(&server_address), | 49 reinterpret_cast<struct sockaddr *>(&server_address), |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 | 87 |
| 87 | 88 |
| 88 intptr_t Socket::GetPort(intptr_t fd) { | 89 intptr_t Socket::GetPort(intptr_t fd) { |
| 89 ASSERT(fd >= 0); | 90 ASSERT(fd >= 0); |
| 90 struct sockaddr_in socket_address; | 91 struct sockaddr_in socket_address; |
| 91 socklen_t size = sizeof(socket_address); | 92 socklen_t size = sizeof(socket_address); |
| 92 if (TEMP_FAILURE_RETRY( | 93 if (TEMP_FAILURE_RETRY( |
| 93 getsockname(fd, | 94 getsockname(fd, |
| 94 reinterpret_cast<struct sockaddr *>(&socket_address), | 95 reinterpret_cast<struct sockaddr *>(&socket_address), |
| 95 &size))) { | 96 &size))) { |
| 96 fprintf(stderr, "Error getsockname: %s\n", strerror(errno)); | 97 Log::PrintErr("Error getsockname: %s\n", strerror(errno)); |
| 97 return 0; | 98 return 0; |
| 98 } | 99 } |
| 99 return ntohs(socket_address.sin_port); | 100 return ntohs(socket_address.sin_port); |
| 100 } | 101 } |
| 101 | 102 |
| 102 | 103 |
| 103 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { | 104 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { |
| 104 ASSERT(fd >= 0); | 105 ASSERT(fd >= 0); |
| 105 struct sockaddr_in socket_address; | 106 struct sockaddr_in socket_address; |
| 106 socklen_t size = sizeof(socket_address); | 107 socklen_t size = sizeof(socket_address); |
| 107 if (TEMP_FAILURE_RETRY( | 108 if (TEMP_FAILURE_RETRY( |
| 108 getpeername(fd, | 109 getpeername(fd, |
| 109 reinterpret_cast<struct sockaddr *>(&socket_address), | 110 reinterpret_cast<struct sockaddr *>(&socket_address), |
| 110 &size))) { | 111 &size))) { |
| 111 fprintf(stderr, "Error getpeername: %s\n", strerror(errno)); | 112 Log::PrintErr("Error getpeername: %s\n", strerror(errno)); |
| 112 return false; | 113 return false; |
| 113 } | 114 } |
| 114 if (inet_ntop(socket_address.sin_family, | 115 if (inet_ntop(socket_address.sin_family, |
| 115 reinterpret_cast<const void *>(&socket_address.sin_addr), | 116 reinterpret_cast<const void *>(&socket_address.sin_addr), |
| 116 host, | 117 host, |
| 117 INET_ADDRSTRLEN) == NULL) { | 118 INET_ADDRSTRLEN) == NULL) { |
| 118 fprintf(stderr, "Error inet_ntop: %s\n", strerror(errno)); | 119 Log::PrintErr("Error inet_ntop: %s\n", strerror(errno)); |
| 119 return false; | 120 return false; |
| 120 } | 121 } |
| 121 *port = ntohs(socket_address.sin_port); | 122 *port = ntohs(socket_address.sin_port); |
| 122 return true; | 123 return true; |
| 123 } | 124 } |
| 124 | 125 |
| 125 | 126 |
| 126 void Socket::GetError(intptr_t fd, OSError* os_error) { | 127 void Socket::GetError(intptr_t fd, OSError* os_error) { |
| 127 int len = sizeof(errno); | 128 int len = sizeof(errno); |
| 128 getsockopt(fd, | 129 getsockopt(fd, |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 intptr_t fd; | 178 intptr_t fd; |
| 178 struct sockaddr_in server_address; | 179 struct sockaddr_in server_address; |
| 179 | 180 |
| 180 in_addr_t s_addr = inet_addr(host); | 181 in_addr_t s_addr = inet_addr(host); |
| 181 if (s_addr == INADDR_NONE) { | 182 if (s_addr == INADDR_NONE) { |
| 182 return -5; | 183 return -5; |
| 183 } | 184 } |
| 184 | 185 |
| 185 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); | 186 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); |
| 186 if (fd < 0) { | 187 if (fd < 0) { |
| 187 fprintf(stderr, "Error CreateBind: %s\n", strerror(errno)); | 188 Log::PrintErr("Error CreateBind: %s\n", strerror(errno)); |
| 188 return -1; | 189 return -1; |
| 189 } | 190 } |
| 190 | 191 |
| 191 FDUtils::SetCloseOnExec(fd); | 192 FDUtils::SetCloseOnExec(fd); |
| 192 | 193 |
| 193 int optval = 1; | 194 int optval = 1; |
| 194 TEMP_FAILURE_RETRY( | 195 TEMP_FAILURE_RETRY( |
| 195 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 196 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
| 196 | 197 |
| 197 server_address.sin_family = AF_INET; | 198 server_address.sin_family = AF_INET; |
| 198 server_address.sin_port = htons(port); | 199 server_address.sin_port = htons(port); |
| 199 server_address.sin_addr.s_addr = s_addr; | 200 server_address.sin_addr.s_addr = s_addr; |
| 200 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); | 201 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); |
| 201 | 202 |
| 202 if (TEMP_FAILURE_RETRY( | 203 if (TEMP_FAILURE_RETRY( |
| 203 bind(fd, | 204 bind(fd, |
| 204 reinterpret_cast<struct sockaddr *>(&server_address), | 205 reinterpret_cast<struct sockaddr *>(&server_address), |
| 205 sizeof(server_address))) < 0) { | 206 sizeof(server_address))) < 0) { |
| 206 TEMP_FAILURE_RETRY(close(fd)); | 207 TEMP_FAILURE_RETRY(close(fd)); |
| 207 fprintf(stderr, "Error Bind: %s\n", strerror(errno)); | 208 Log::PrintErr("Error Bind: %s\n", strerror(errno)); |
| 208 return -1; | 209 return -1; |
| 209 } | 210 } |
| 210 | 211 |
| 211 if (TEMP_FAILURE_RETRY(listen(fd, backlog)) != 0) { | 212 if (TEMP_FAILURE_RETRY(listen(fd, backlog)) != 0) { |
| 212 fprintf(stderr, "Error Listen: %s\n", strerror(errno)); | 213 Log::PrintErr("Error Listen: %s\n", strerror(errno)); |
| 213 return -1; | 214 return -1; |
| 214 } | 215 } |
| 215 | 216 |
| 216 FDUtils::SetNonBlocking(fd); | 217 FDUtils::SetNonBlocking(fd); |
| 217 return fd; | 218 return fd; |
| 218 } | 219 } |
| 219 | 220 |
| 220 | 221 |
| 221 intptr_t ServerSocket::Accept(intptr_t fd) { | 222 intptr_t ServerSocket::Accept(intptr_t fd) { |
| 222 intptr_t socket; | 223 intptr_t socket; |
| 223 struct sockaddr clientaddr; | 224 struct sockaddr clientaddr; |
| 224 socklen_t addrlen = sizeof(clientaddr); | 225 socklen_t addrlen = sizeof(clientaddr); |
| 225 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); | 226 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); |
| 226 if (socket == -1) { | 227 if (socket == -1) { |
| 227 if (errno == EAGAIN) { | 228 if (errno == EAGAIN) { |
| 228 // We need to signal to the caller that this is actually not an | 229 // We need to signal to the caller that this is actually not an |
| 229 // error. We got woken up from the poll on the listening socket, | 230 // error. We got woken up from the poll on the listening socket, |
| 230 // but there is no connection ready to be accepted. | 231 // but there is no connection ready to be accepted. |
| 231 ASSERT(kTemporaryFailure != -1); | 232 ASSERT(kTemporaryFailure != -1); |
| 232 socket = kTemporaryFailure; | 233 socket = kTemporaryFailure; |
| 233 } | 234 } |
| 234 } else { | 235 } else { |
| 235 FDUtils::SetNonBlocking(socket); | 236 FDUtils::SetNonBlocking(socket); |
| 236 } | 237 } |
| 237 return socket; | 238 return socket; |
| 238 } | 239 } |
| OLD | NEW |