blob: be3c53575bf70b0d9c006d0e805c94cd6007a3b3 [file] [log] [blame]
Mark Salyzyn12717162014-04-29 15:49:14 -07001/*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08002** Copyright 2006, The Android Open Source Project
3**
4** Licensed under the Apache License, Version 2.0 (the "License");
5** you may not use this file except in compliance with the License.
6** You may obtain a copy of the License at
7**
8** http://www.apache.org/licenses/LICENSE-2.0
9**
10** Unless required by applicable law or agreed to in writing, software
11** distributed under the License is distributed on an "AS IS" BASIS,
12** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13** See the License for the specific language governing permissions and
14** limitations under the License.
15*/
16
Elliott Hughes8e9aeb92017-11-10 10:22:07 -080017#include <cutils/sockets.h>
18
Mark Salyzyn12717162014-04-29 15:49:14 -070019#include <errno.h>
Ken Liermanaecc6a62013-06-20 09:27:13 -070020#include <fcntl.h>
Mark Salyzyn12717162014-04-29 15:49:14 -070021#include <stddef.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026#include <sys/socket.h>
27#include <sys/select.h>
28#include <sys/types.h>
29#include <netinet/in.h>
30#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031
Elliott Hughes0f8f70b2015-07-18 10:47:59 -070032static int toggle_O_NONBLOCK(int s) {
Elliott Hughes94ae4182015-07-17 15:54:09 -070033 int flags = fcntl(s, F_GETFL);
Elliott Hughes0f8f70b2015-07-18 10:47:59 -070034 if (flags == -1 || fcntl(s, F_SETFL, flags ^ O_NONBLOCK) == -1) {
Elliott Hughes94ae4182015-07-17 15:54:09 -070035 close(s);
36 return -1;
37 }
Elliott Hughes94ae4182015-07-17 15:54:09 -070038 return s;
Ken Liermanaecc6a62013-06-20 09:27:13 -070039}
40
Elliott Hughes94ae4182015-07-17 15:54:09 -070041// Connect to the given host and port.
42// 'timeout' is in seconds (0 for no timeout).
43// Returns a file descriptor or -1 on error.
Elliott Hughes381cfa92015-07-23 17:12:58 -070044// On error, check *getaddrinfo_error (for use with gai_strerror) first;
45// if that's 0, use errno instead.
46int socket_network_client_timeout(const char* host, int port, int type, int timeout,
47 int* getaddrinfo_error) {
Elliott Hughes94ae4182015-07-17 15:54:09 -070048 struct addrinfo hints;
49 memset(&hints, 0, sizeof(hints));
50 hints.ai_family = AF_UNSPEC;
51 hints.ai_socktype = type;
Ken Liermanaecc6a62013-06-20 09:27:13 -070052
Elliott Hughes94ae4182015-07-17 15:54:09 -070053 char port_str[16];
54 snprintf(port_str, sizeof(port_str), "%d", port);
55
56 struct addrinfo* addrs;
Elliott Hughes381cfa92015-07-23 17:12:58 -070057 *getaddrinfo_error = getaddrinfo(host, port_str, &hints, &addrs);
Elliott Hugheseabe8af2015-07-24 18:54:02 -070058 if (*getaddrinfo_error != 0) {
Elliott Hughes94ae4182015-07-17 15:54:09 -070059 return -1;
60 }
61
Josh Gao78cc20f2016-09-01 14:54:18 -070062 int result = -1;
63 for (struct addrinfo* addr = addrs; addr != NULL; addr = addr->ai_next) {
64 // The Mac doesn't have SOCK_NONBLOCK.
65 int s = socket(addr->ai_family, type, addr->ai_protocol);
Elliott Hughes3ff453a2017-08-02 12:57:02 -070066 if (s == -1 || toggle_O_NONBLOCK(s) == -1) break;
Josh Gao78cc20f2016-09-01 14:54:18 -070067
68 int rc = connect(s, addr->ai_addr, addr->ai_addrlen);
69 if (rc == 0) {
70 result = toggle_O_NONBLOCK(s);
71 break;
72 } else if (rc == -1 && errno != EINPROGRESS) {
73 close(s);
74 continue;
75 }
76
77 fd_set r_set;
78 FD_ZERO(&r_set);
79 FD_SET(s, &r_set);
80 fd_set w_set = r_set;
81
82 struct timeval ts;
83 ts.tv_sec = timeout;
84 ts.tv_usec = 0;
85 if ((rc = select(s + 1, &r_set, &w_set, NULL, (timeout != 0) ? &ts : NULL)) == -1) {
86 close(s);
87 break;
88 }
89 if (rc == 0) { // we had a timeout
90 errno = ETIMEDOUT;
91 close(s);
92 break;
93 }
94
95 int error = 0;
96 socklen_t len = sizeof(error);
97 if (FD_ISSET(s, &r_set) || FD_ISSET(s, &w_set)) {
98 if (getsockopt(s, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
99 close(s);
100 break;
101 }
102 } else {
103 close(s);
104 break;
105 }
106
107 if (error) { // check if we had a socket error
108 // TODO: Update the timeout.
109 errno = error;
110 close(s);
111 continue;
112 }
113
114 result = toggle_O_NONBLOCK(s);
Tao Wu9b7341f2016-09-21 15:41:05 -0700115 break;
Josh Gao78cc20f2016-09-01 14:54:18 -0700116 }
Elliott Hughes94ae4182015-07-17 15:54:09 -0700117
118 freeaddrinfo(addrs);
Josh Gao78cc20f2016-09-01 14:54:18 -0700119 return result;
Elliott Hughes94ae4182015-07-17 15:54:09 -0700120}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121
Elliott Hughes94ae4182015-07-17 15:54:09 -0700122int socket_network_client(const char* host, int port, int type) {
Elliott Hughes381cfa92015-07-23 17:12:58 -0700123 int getaddrinfo_error;
124 return socket_network_client_timeout(host, port, type, 0, &getaddrinfo_error);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125}