blob: 3300b8fb197cc8d572c22f211fbf450a0776b181 [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
Mark Salyzyn12717162014-04-29 15:49:14 -070017#include <errno.h>
Ken Liermanaecc6a62013-06-20 09:27:13 -070018#include <fcntl.h>
Mark Salyzyn12717162014-04-29 15:49:14 -070019#include <stddef.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080020#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024#include <sys/socket.h>
25#include <sys/select.h>
26#include <sys/types.h>
27#include <netinet/in.h>
28#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
Mark Salyzyn12717162014-04-29 15:49:14 -070030#include <cutils/sockets.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
62 // TODO: try all the addresses if there's more than one?
63 int family = addrs[0].ai_family;
64 int protocol = addrs[0].ai_protocol;
65 socklen_t addr_len = addrs[0].ai_addrlen;
66 struct sockaddr_storage addr;
67 memcpy(&addr, addrs[0].ai_addr, addr_len);
68
69 freeaddrinfo(addrs);
70
Elliott Hughes0f8f70b2015-07-18 10:47:59 -070071 // The Mac doesn't have SOCK_NONBLOCK.
72 int s = socket(family, type, protocol);
73 if (s == -1 || toggle_O_NONBLOCK(s) == -1) return -1;
Elliott Hughes94ae4182015-07-17 15:54:09 -070074
75 int rc = connect(s, (const struct sockaddr*) &addr, addr_len);
76 if (rc == 0) {
Elliott Hughes0f8f70b2015-07-18 10:47:59 -070077 return toggle_O_NONBLOCK(s);
Elliott Hughes94ae4182015-07-17 15:54:09 -070078 } else if (rc == -1 && errno != EINPROGRESS) {
79 close(s);
80 return -1;
81 }
82
83 fd_set r_set;
84 FD_ZERO(&r_set);
85 FD_SET(s, &r_set);
86 fd_set w_set = r_set;
87
88 struct timeval ts;
Ken Liermanaecc6a62013-06-20 09:27:13 -070089 ts.tv_sec = timeout;
90 ts.tv_usec = 0;
Elliott Hughes94ae4182015-07-17 15:54:09 -070091 if ((rc = select(s + 1, &r_set, &w_set, NULL, (timeout != 0) ? &ts : NULL)) == -1) {
Ken Liermanaecc6a62013-06-20 09:27:13 -070092 close(s);
93 return -1;
94 }
Elliott Hughes94ae4182015-07-17 15:54:09 -070095 if (rc == 0) { // we had a timeout
Ken Liermanaecc6a62013-06-20 09:27:13 -070096 errno = ETIMEDOUT;
97 close(s);
98 return -1;
99 }
100
Elliott Hughes94ae4182015-07-17 15:54:09 -0700101 int error = 0;
102 socklen_t len = sizeof(error);
103 if (FD_ISSET(s, &r_set) || FD_ISSET(s, &w_set)) {
Ken Liermanaecc6a62013-06-20 09:27:13 -0700104 if (getsockopt(s, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
105 close(s);
106 return -1;
107 }
108 } else {
109 close(s);
110 return -1;
111 }
112
113 if (error) { // check if we had a socket error
114 errno = error;
115 close(s);
116 return -1;
117 }
118
Elliott Hughes0f8f70b2015-07-18 10:47:59 -0700119 return toggle_O_NONBLOCK(s);
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}