| 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/socket.h" | 12 #include "bin/socket.h" |
| 13 | 13 |
| 14 | 14 |
| 15 bool Socket::Initialize() { | 15 bool Socket::Initialize() { |
| 16 // Nothing to do on Linux. | 16 // Nothing to do on Linux. |
| 17 return true; | 17 return true; |
| 18 } | 18 } |
| 19 | 19 |
| 20 | 20 |
| 21 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { | 21 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { |
| 22 intptr_t fd; | 22 intptr_t fd; |
| 23 struct hostent* server; | 23 struct hostent server; |
| 24 struct sockaddr_in server_address; | 24 struct sockaddr_in server_address; |
| 25 | 25 |
| 26 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); | 26 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); |
| 27 if (fd < 0) { | 27 if (fd < 0) { |
| 28 fprintf(stderr, "Error CreateConnect: %s\n", strerror(errno)); | 28 fprintf(stderr, "Error CreateConnect: %s\n", strerror(errno)); |
| 29 return -1; | 29 return -1; |
| 30 } | 30 } |
| 31 | 31 |
| 32 FDUtils::SetNonBlocking(fd); | 32 FDUtils::SetNonBlocking(fd); |
| 33 | 33 |
| 34 server = gethostbyname(host); | 34 static const size_t kTempBufSize = 1024; |
| 35 if (server == NULL) { | 35 char temp_buf[kTempBufSize]; |
| 36 struct hostent *unused; |
| 37 int err; |
| 38 if (gethostbyname_r( |
| 39 host, &server, temp_buf, kTempBufSize, &unused, &err) != 0) { |
| 36 TEMP_FAILURE_RETRY(close(fd)); | 40 TEMP_FAILURE_RETRY(close(fd)); |
| 37 fprintf(stderr, "Error CreateConnect: %s\n", strerror(errno)); | 41 fprintf(stderr, "Error CreateConnect: %s\n", strerror(errno)); |
| 38 return -1; | 42 return -1; |
| 39 } | 43 } |
| 40 | 44 |
| 41 server_address.sin_family = AF_INET; | 45 server_address.sin_family = AF_INET; |
| 42 server_address.sin_port = htons(port); | 46 server_address.sin_port = htons(port); |
| 43 bcopy(server->h_addr, &server_address.sin_addr.s_addr, server->h_length); | 47 bcopy(server.h_addr, &server_address.sin_addr.s_addr, server.h_length); |
| 44 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); | 48 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); |
| 45 intptr_t result = TEMP_FAILURE_RETRY( | 49 intptr_t result = TEMP_FAILURE_RETRY( |
| 46 connect(fd, | 50 connect(fd, |
| 47 reinterpret_cast<struct sockaddr *>(&server_address), | 51 reinterpret_cast<struct sockaddr *>(&server_address), |
| 48 sizeof(server_address))); | 52 sizeof(server_address))); |
| 49 if (result == 0 || errno == EINPROGRESS) { | 53 if (result == 0 || errno == EINPROGRESS) { |
| 50 return fd; | 54 return fd; |
| 51 } | 55 } |
| 52 return -1; | 56 return -1; |
| 53 } | 57 } |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 // error. We got woken up from the poll on the listening socket, | 240 // error. We got woken up from the poll on the listening socket, |
| 237 // but there is no connection ready to be accepted. | 241 // but there is no connection ready to be accepted. |
| 238 ASSERT(kTemporaryFailure != -1); | 242 ASSERT(kTemporaryFailure != -1); |
| 239 socket = kTemporaryFailure; | 243 socket = kTemporaryFailure; |
| 240 } | 244 } |
| 241 } else { | 245 } else { |
| 242 FDUtils::SetNonBlocking(socket); | 246 FDUtils::SetNonBlocking(socket); |
| 243 } | 247 } |
| 244 return socket; | 248 return socket; |
| 245 } | 249 } |
| OLD | NEW |