1 /*
2 * Copyright (C) 2017 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
17 #include "sysdeps/network.h"
18
19 #include <errno.h>
20 #include <netdb.h>
21 #include <netinet/in.h>
22 #include <sys/socket.h>
23
24 #include <string>
25
26 #include <android-base/logging.h>
27 #include <android-base/stringprintf.h>
28 #include <cutils/sockets.h>
29
30 #include "adb_unique_fd.h"
31
set_error(std::string * error)32 static void set_error(std::string* error) {
33 if (error) {
34 *error = strerror(errno);
35 }
36 }
37
loopback_addr4(sockaddr_storage * addr,socklen_t * addrlen,int port)38 static sockaddr* loopback_addr4(sockaddr_storage* addr, socklen_t* addrlen, int port) {
39 struct sockaddr_in* addr4 = reinterpret_cast<sockaddr_in*>(addr);
40 *addrlen = sizeof(*addr4);
41
42 addr4->sin_family = AF_INET;
43 addr4->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
44 addr4->sin_port = htons(port);
45 return reinterpret_cast<sockaddr*>(addr);
46 }
47
loopback_addr6(sockaddr_storage * addr,socklen_t * addrlen,int port)48 static sockaddr* loopback_addr6(sockaddr_storage* addr, socklen_t* addrlen, int port) {
49 struct sockaddr_in6* addr6 = reinterpret_cast<sockaddr_in6*>(addr);
50 *addrlen = sizeof(*addr6);
51
52 addr6->sin6_family = AF_INET6;
53 addr6->sin6_addr = in6addr_loopback;
54 addr6->sin6_port = htons(port);
55 return reinterpret_cast<sockaddr*>(addr);
56 }
57
_network_loopback_client(bool ipv6,int port,int type,std::string * error)58 static int _network_loopback_client(bool ipv6, int port, int type, std::string* error) {
59 unique_fd s(socket(ipv6 ? AF_INET6 : AF_INET, type, 0));
60 if (s == -1) {
61 set_error(error);
62 return -1;
63 }
64
65 struct sockaddr_storage addr_storage = {};
66 socklen_t addrlen = sizeof(addr_storage);
67 sockaddr* addr = (ipv6 ? loopback_addr6 : loopback_addr4)(&addr_storage, &addrlen, 0);
68
69 if (bind(s.get(), addr, addrlen) != 0) {
70 set_error(error);
71 return -1;
72 }
73
74 addr = (ipv6 ? loopback_addr6 : loopback_addr4)(&addr_storage, &addrlen, port);
75
76 if (connect(s.get(), addr, addrlen) != 0) {
77 set_error(error);
78 return -1;
79 }
80
81 return s.release();
82 }
83
network_loopback_client(int port,int type,std::string * error)84 int network_loopback_client(int port, int type, std::string* error) {
85 // Try IPv4 first, use IPv6 as a fallback.
86 int rc = _network_loopback_client(false, port, type, error);
87 if (rc == -1) {
88 return _network_loopback_client(true, port, type, error);
89 }
90 return rc;
91 }
92
_network_loopback_server(bool ipv6,int port,int type,std::string * error)93 static int _network_loopback_server(bool ipv6, int port, int type, std::string* error) {
94 unique_fd s(socket(ipv6 ? AF_INET6 : AF_INET, type, 0));
95 if (s == -1) {
96 set_error(error);
97 return -1;
98 }
99
100 int n = 1;
101 setsockopt(s.get(), SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n));
102
103 struct sockaddr_storage addr_storage = {};
104 socklen_t addrlen = sizeof(addr_storage);
105 sockaddr* addr = (ipv6 ? loopback_addr6 : loopback_addr4)(&addr_storage, &addrlen, port);
106
107 if (bind(s.get(), addr, addrlen) != 0) {
108 set_error(error);
109 return -1;
110 }
111
112 if (type == SOCK_STREAM || type == SOCK_SEQPACKET) {
113 if (listen(s.get(), SOMAXCONN) != 0) {
114 set_error(error);
115 return -1;
116 }
117 }
118
119 return s.release();
120 }
121
network_loopback_server(int port,int type,std::string * error,bool prefer_ipv4)122 int network_loopback_server(int port, int type, std::string* error, bool prefer_ipv4) {
123 int rc = -1;
124 if (prefer_ipv4) {
125 rc = _network_loopback_server(false, port, type, error);
126 }
127
128 // Only attempt to listen on IPv6 if IPv4 is unavailable or prefer_ipv4 is false
129 // We don't want to start an IPv6 server if there's already an IPv4 one running.
130 if (rc == -1 && (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || !prefer_ipv4)) {
131 return _network_loopback_server(true, port, type, error);
132 }
133 return rc;
134 }
135
network_connect(const std::string & host,int port,int type,int timeout,std::string * error)136 int network_connect(const std::string& host, int port, int type, int timeout, std::string* error) {
137 int getaddrinfo_error = 0;
138 int fd = socket_network_client_timeout(host.c_str(), port, type, timeout, &getaddrinfo_error);
139 if (fd != -1) {
140 return fd;
141 }
142 if (getaddrinfo_error != 0) {
143 *error = android::base::StringPrintf("failed to resolve host: '%s': %s", host.c_str(),
144 gai_strerror(getaddrinfo_error));
145 LOG(WARNING) << *error;
146 } else {
147 *error = android::base::StringPrintf("failed to connect to '%s:%d': %s", host.c_str(), port,
148 strerror(errno));
149 LOG(WARNING) << *error;
150 }
151 return -1;
152 }
153