| 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 Linux. | 17 // Nothing to do on Linux. |
| 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 static const size_t kTempBufSize = 1024; | 36 static const size_t kTempBufSize = 1024; |
| 36 char temp_buf[kTempBufSize]; | 37 char temp_buf[kTempBufSize]; |
| 37 struct hostent *unused; | 38 struct hostent *unused; |
| 38 int err; | 39 int err; |
| 39 if (gethostbyname_r( | 40 if (gethostbyname_r( |
| 40 host, &server, temp_buf, kTempBufSize, &unused, &err) != 0) { | 41 host, &server, temp_buf, kTempBufSize, &unused, &err) != 0) { |
| 41 TEMP_FAILURE_RETRY(close(fd)); | 42 TEMP_FAILURE_RETRY(close(fd)); |
| 42 fprintf(stderr, "Error CreateConnect: %s\n", strerror(errno)); | 43 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); |
| 43 return -1; | 44 return -1; |
| 44 } | 45 } |
| 45 | 46 |
| 46 server_address.sin_family = AF_INET; | 47 server_address.sin_family = AF_INET; |
| 47 server_address.sin_port = htons(port); | 48 server_address.sin_port = htons(port); |
| 48 bcopy(server.h_addr, &server_address.sin_addr.s_addr, server.h_length); | 49 bcopy(server.h_addr, &server_address.sin_addr.s_addr, server.h_length); |
| 49 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); | 50 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); |
| 50 intptr_t result = TEMP_FAILURE_RETRY( | 51 intptr_t result = TEMP_FAILURE_RETRY( |
| 51 connect(fd, | 52 connect(fd, |
| 52 reinterpret_cast<struct sockaddr *>(&server_address), | 53 reinterpret_cast<struct sockaddr *>(&server_address), |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 | 91 |
| 91 | 92 |
| 92 intptr_t Socket::GetPort(intptr_t fd) { | 93 intptr_t Socket::GetPort(intptr_t fd) { |
| 93 ASSERT(fd >= 0); | 94 ASSERT(fd >= 0); |
| 94 struct sockaddr_in socket_address; | 95 struct sockaddr_in socket_address; |
| 95 socklen_t size = sizeof(socket_address); | 96 socklen_t size = sizeof(socket_address); |
| 96 if (TEMP_FAILURE_RETRY( | 97 if (TEMP_FAILURE_RETRY( |
| 97 getsockname(fd, | 98 getsockname(fd, |
| 98 reinterpret_cast<struct sockaddr *>(&socket_address), | 99 reinterpret_cast<struct sockaddr *>(&socket_address), |
| 99 &size))) { | 100 &size))) { |
| 100 fprintf(stderr, "Error getsockname: %s\n", strerror(errno)); | 101 Log::PrintErr("Error getsockname: %s\n", strerror(errno)); |
| 101 return 0; | 102 return 0; |
| 102 } | 103 } |
| 103 return ntohs(socket_address.sin_port); | 104 return ntohs(socket_address.sin_port); |
| 104 } | 105 } |
| 105 | 106 |
| 106 | 107 |
| 107 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { | 108 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { |
| 108 ASSERT(fd >= 0); | 109 ASSERT(fd >= 0); |
| 109 struct sockaddr_in socket_address; | 110 struct sockaddr_in socket_address; |
| 110 socklen_t size = sizeof(socket_address); | 111 socklen_t size = sizeof(socket_address); |
| 111 if (TEMP_FAILURE_RETRY( | 112 if (TEMP_FAILURE_RETRY( |
| 112 getpeername(fd, | 113 getpeername(fd, |
| 113 reinterpret_cast<struct sockaddr *>(&socket_address), | 114 reinterpret_cast<struct sockaddr *>(&socket_address), |
| 114 &size))) { | 115 &size))) { |
| 115 fprintf(stderr, "Error getpeername: %s\n", strerror(errno)); | 116 Log::PrintErr("Error getpeername: %s\n", strerror(errno)); |
| 116 return false; | 117 return false; |
| 117 } | 118 } |
| 118 if (inet_ntop(socket_address.sin_family, | 119 if (inet_ntop(socket_address.sin_family, |
| 119 reinterpret_cast<const void *>(&socket_address.sin_addr), | 120 reinterpret_cast<const void *>(&socket_address.sin_addr), |
| 120 host, | 121 host, |
| 121 INET_ADDRSTRLEN) == NULL) { | 122 INET_ADDRSTRLEN) == NULL) { |
| 122 fprintf(stderr, "Error inet_ntop: %s\n", strerror(errno)); | 123 Log::PrintErr("Error inet_ntop: %s\n", strerror(errno)); |
| 123 return false; | 124 return false; |
| 124 } | 125 } |
| 125 *port = ntohs(socket_address.sin_port); | 126 *port = ntohs(socket_address.sin_port); |
| 126 return true; | 127 return true; |
| 127 } | 128 } |
| 128 | 129 |
| 129 | 130 |
| 130 void Socket::GetError(intptr_t fd, OSError* os_error) { | 131 void Socket::GetError(intptr_t fd, OSError* os_error) { |
| 131 int len = sizeof(errno); | 132 int len = sizeof(errno); |
| 132 getsockopt(fd, | 133 getsockopt(fd, |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 intptr_t fd; | 182 intptr_t fd; |
| 182 struct sockaddr_in server_address; | 183 struct sockaddr_in server_address; |
| 183 | 184 |
| 184 in_addr_t s_addr = inet_addr(host); | 185 in_addr_t s_addr = inet_addr(host); |
| 185 if (s_addr == INADDR_NONE) { | 186 if (s_addr == INADDR_NONE) { |
| 186 return -5; | 187 return -5; |
| 187 } | 188 } |
| 188 | 189 |
| 189 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); | 190 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); |
| 190 if (fd < 0) { | 191 if (fd < 0) { |
| 191 fprintf(stderr, "Error CreateBind: %s\n", strerror(errno)); | 192 Log::PrintErr("Error CreateBind: %s\n", strerror(errno)); |
| 192 return -1; | 193 return -1; |
| 193 } | 194 } |
| 194 | 195 |
| 195 FDUtils::SetCloseOnExec(fd); | 196 FDUtils::SetCloseOnExec(fd); |
| 196 | 197 |
| 197 int optval = 1; | 198 int optval = 1; |
| 198 TEMP_FAILURE_RETRY( | 199 TEMP_FAILURE_RETRY( |
| 199 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 200 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
| 200 | 201 |
| 201 server_address.sin_family = AF_INET; | 202 server_address.sin_family = AF_INET; |
| 202 server_address.sin_port = htons(port); | 203 server_address.sin_port = htons(port); |
| 203 server_address.sin_addr.s_addr = s_addr; | 204 server_address.sin_addr.s_addr = s_addr; |
| 204 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); | 205 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); |
| 205 | 206 |
| 206 if (TEMP_FAILURE_RETRY( | 207 if (TEMP_FAILURE_RETRY( |
| 207 bind(fd, | 208 bind(fd, |
| 208 reinterpret_cast<struct sockaddr *>(&server_address), | 209 reinterpret_cast<struct sockaddr *>(&server_address), |
| 209 sizeof(server_address))) < 0) { | 210 sizeof(server_address))) < 0) { |
| 210 TEMP_FAILURE_RETRY(close(fd)); | 211 TEMP_FAILURE_RETRY(close(fd)); |
| 211 fprintf(stderr, "Error Bind: %s\n", strerror(errno)); | 212 Log::PrintErr("Error Bind: %s\n", strerror(errno)); |
| 212 return -1; | 213 return -1; |
| 213 } | 214 } |
| 214 | 215 |
| 215 if (TEMP_FAILURE_RETRY(listen(fd, backlog)) != 0) { | 216 if (TEMP_FAILURE_RETRY(listen(fd, backlog)) != 0) { |
| 216 fprintf(stderr, "Error Listen: %s\n", strerror(errno)); | 217 Log::PrintErr("Error Listen: %s\n", strerror(errno)); |
| 217 return -1; | 218 return -1; |
| 218 } | 219 } |
| 219 | 220 |
| 220 FDUtils::SetNonBlocking(fd); | 221 FDUtils::SetNonBlocking(fd); |
| 221 return fd; | 222 return fd; |
| 222 } | 223 } |
| 223 | 224 |
| 224 | 225 |
| 225 static bool IsTemporaryAcceptError(int error) { | 226 static bool IsTemporaryAcceptError(int error) { |
| 226 // On Linux a number of protocol errors should be treated as EAGAIN. | 227 // On Linux a number of protocol errors should be treated as EAGAIN. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 243 // error. We got woken up from the poll on the listening socket, | 244 // error. We got woken up from the poll on the listening socket, |
| 244 // but there is no connection ready to be accepted. | 245 // but there is no connection ready to be accepted. |
| 245 ASSERT(kTemporaryFailure != -1); | 246 ASSERT(kTemporaryFailure != -1); |
| 246 socket = kTemporaryFailure; | 247 socket = kTemporaryFailure; |
| 247 } | 248 } |
| 248 } else { | 249 } else { |
| 249 FDUtils::SetNonBlocking(socket); | 250 FDUtils::SetNonBlocking(socket); |
| 250 } | 251 } |
| 251 return socket; | 252 return socket; |
| 252 } | 253 } |
| OLD | NEW |