Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | package android.net; |
| 18 | |
| 19 | import android.os.Parcelable; |
| 20 | import android.os.Parcel; |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 21 | import android.system.ErrnoException; |
Erik Kline | d895999 | 2015-06-05 17:37:45 +0900 | [diff] [blame] | 22 | import android.system.Os; |
| 23 | import android.system.OsConstants; |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 24 | |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame] | 25 | import java.io.FileDescriptor; |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 26 | import java.io.IOException; |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame] | 27 | import java.net.DatagramSocket; |
Paul Jensen | 3e32a4b | 2014-05-13 19:05:13 -0400 | [diff] [blame] | 28 | import java.net.InetAddress; |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 29 | import java.net.InetSocketAddress; |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 30 | import java.net.MalformedURLException; |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 31 | import java.net.Socket; |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 32 | import java.net.SocketAddress; |
Paul Jensen | 32a58f0 | 2014-06-20 13:58:14 -0400 | [diff] [blame] | 33 | import java.net.SocketException; |
Paul Jensen | 3e32a4b | 2014-05-13 19:05:13 -0400 | [diff] [blame] | 34 | import java.net.UnknownHostException; |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 35 | import java.net.URL; |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 36 | import java.net.URLConnection; |
Tobias Thierer | 851d806 | 2016-06-01 20:51:45 +0100 | [diff] [blame] | 37 | import java.util.Arrays; |
| 38 | import java.util.List; |
| 39 | import java.util.concurrent.TimeUnit; |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 40 | import javax.net.SocketFactory; |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 41 | |
Lorenzo Colitti | 0ed0a28 | 2014-08-28 17:15:40 -0700 | [diff] [blame] | 42 | import com.android.okhttp.ConnectionPool; |
Tobias Thierer | 851d806 | 2016-06-01 20:51:45 +0100 | [diff] [blame] | 43 | import com.android.okhttp.Dns; |
Paul Jensen | 07ed074 | 2014-09-08 14:59:09 -0400 | [diff] [blame] | 44 | import com.android.okhttp.HttpHandler; |
| 45 | import com.android.okhttp.HttpsHandler; |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 46 | import com.android.okhttp.OkHttpClient; |
Neil Fuller | 50a01d8 | 2015-01-12 16:49:06 +0000 | [diff] [blame] | 47 | import com.android.okhttp.OkUrlFactory; |
| 48 | import com.android.okhttp.internal.Internal; |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 49 | |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 50 | /** |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 51 | * Identifies a {@code Network}. This is supplied to applications via |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 52 | * {@link ConnectivityManager.NetworkCallback} in response to the active |
| 53 | * {@link ConnectivityManager#requestNetwork} or passive |
| 54 | * {@link ConnectivityManager#registerNetworkCallback} calls. |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 55 | * It is used to direct traffic to the given {@code Network}, either on a {@link Socket} basis |
Paul Jensen | 0a363a3 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 56 | * through a targeted {@link SocketFactory} or process-wide via |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 57 | * {@link ConnectivityManager#bindProcessToNetwork}. |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 58 | */ |
| 59 | public class Network implements Parcelable { |
| 60 | |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 61 | /** |
| 62 | * @hide |
| 63 | */ |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 64 | public final int netId; |
| 65 | |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 66 | // Objects used to perform per-network operations such as getSocketFactory |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 67 | // and openConnection, and a lock to protect access to them. |
Lorenzo Colitti | 5b37fa2 | 2014-07-29 22:51:27 +0900 | [diff] [blame] | 68 | private volatile NetworkBoundSocketFactory mNetworkBoundSocketFactory = null; |
Tobias Thierer | 851d806 | 2016-06-01 20:51:45 +0100 | [diff] [blame] | 69 | // mLock should be used to control write access to mConnectionPool and mDns. |
Paul Jensen | 07ed074 | 2014-09-08 14:59:09 -0400 | [diff] [blame] | 70 | // maybeInitHttpClient() must be called prior to reading either variable. |
| 71 | private volatile ConnectionPool mConnectionPool = null; |
Tobias Thierer | 851d806 | 2016-06-01 20:51:45 +0100 | [diff] [blame] | 72 | private volatile Dns mDns = null; |
Erik Kline | d895999 | 2015-06-05 17:37:45 +0900 | [diff] [blame] | 73 | private final Object mLock = new Object(); |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 74 | |
Lorenzo Colitti | 0ed0a28 | 2014-08-28 17:15:40 -0700 | [diff] [blame] | 75 | // Default connection pool values. These are evaluated at startup, just |
| 76 | // like the OkHttp code. Also like the OkHttp code, we will throw parse |
| 77 | // exceptions at class loading time if the properties are set but are not |
| 78 | // valid integers. |
| 79 | private static final boolean httpKeepAlive = |
| 80 | Boolean.parseBoolean(System.getProperty("http.keepAlive", "true")); |
| 81 | private static final int httpMaxConnections = |
| 82 | httpKeepAlive ? Integer.parseInt(System.getProperty("http.maxConnections", "5")) : 0; |
| 83 | private static final long httpKeepAliveDurationMs = |
| 84 | Long.parseLong(System.getProperty("http.keepAliveDuration", "300000")); // 5 minutes. |
| 85 | |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 86 | /** |
| 87 | * @hide |
| 88 | */ |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 89 | public Network(int netId) { |
| 90 | this.netId = netId; |
| 91 | } |
| 92 | |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 93 | /** |
| 94 | * @hide |
| 95 | */ |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 96 | public Network(Network that) { |
| 97 | this.netId = that.netId; |
| 98 | } |
| 99 | |
Paul Jensen | 3e32a4b | 2014-05-13 19:05:13 -0400 | [diff] [blame] | 100 | /** |
| 101 | * Operates the same as {@code InetAddress.getAllByName} except that host |
| 102 | * resolution is done on this network. |
| 103 | * |
| 104 | * @param host the hostname or literal IP string to be resolved. |
| 105 | * @return the array of addresses associated with the specified host. |
| 106 | * @throws UnknownHostException if the address lookup fails. |
| 107 | */ |
| 108 | public InetAddress[] getAllByName(String host) throws UnknownHostException { |
| 109 | return InetAddress.getAllByNameOnNet(host, netId); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Operates the same as {@code InetAddress.getByName} except that host |
| 114 | * resolution is done on this network. |
| 115 | * |
| 116 | * @param host |
| 117 | * the hostName to be resolved to an address or {@code null}. |
| 118 | * @return the {@code InetAddress} instance representing the host. |
| 119 | * @throws UnknownHostException |
| 120 | * if the address lookup fails. |
| 121 | */ |
| 122 | public InetAddress getByName(String host) throws UnknownHostException { |
| 123 | return InetAddress.getByNameOnNet(host, netId); |
| 124 | } |
| 125 | |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 126 | /** |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 127 | * A {@code SocketFactory} that produces {@code Socket}'s bound to this network. |
| 128 | */ |
| 129 | private class NetworkBoundSocketFactory extends SocketFactory { |
| 130 | private final int mNetId; |
| 131 | |
| 132 | public NetworkBoundSocketFactory(int netId) { |
| 133 | super(); |
| 134 | mNetId = netId; |
| 135 | } |
| 136 | |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 137 | private Socket connectToHost(String host, int port, SocketAddress localAddress) |
| 138 | throws IOException { |
Paul Jensen | 8ef3401 | 2014-05-27 10:19:39 -0400 | [diff] [blame] | 139 | // Lookup addresses only on this Network. |
| 140 | InetAddress[] hostAddresses = getAllByName(host); |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 141 | // Try all addresses. |
| 142 | for (int i = 0; i < hostAddresses.length; i++) { |
Paul Jensen | 8ef3401 | 2014-05-27 10:19:39 -0400 | [diff] [blame] | 143 | try { |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 144 | Socket socket = createSocket(); |
| 145 | if (localAddress != null) socket.bind(localAddress); |
Paul Jensen | 8ef3401 | 2014-05-27 10:19:39 -0400 | [diff] [blame] | 146 | socket.connect(new InetSocketAddress(hostAddresses[i], port)); |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 147 | return socket; |
Paul Jensen | 8ef3401 | 2014-05-27 10:19:39 -0400 | [diff] [blame] | 148 | } catch (IOException e) { |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 149 | if (i == (hostAddresses.length - 1)) throw e; |
Paul Jensen | 8ef3401 | 2014-05-27 10:19:39 -0400 | [diff] [blame] | 150 | } |
| 151 | } |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 152 | throw new UnknownHostException(host); |
Paul Jensen | 8ef3401 | 2014-05-27 10:19:39 -0400 | [diff] [blame] | 153 | } |
| 154 | |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 155 | @Override |
| 156 | public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException { |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 157 | return connectToHost(host, port, new InetSocketAddress(localHost, localPort)); |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public Socket createSocket(InetAddress address, int port, InetAddress localAddress, |
| 162 | int localPort) throws IOException { |
| 163 | Socket socket = createSocket(); |
| 164 | socket.bind(new InetSocketAddress(localAddress, localPort)); |
| 165 | socket.connect(new InetSocketAddress(address, port)); |
| 166 | return socket; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public Socket createSocket(InetAddress host, int port) throws IOException { |
| 171 | Socket socket = createSocket(); |
| 172 | socket.connect(new InetSocketAddress(host, port)); |
| 173 | return socket; |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public Socket createSocket(String host, int port) throws IOException { |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 178 | return connectToHost(host, port, null); |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public Socket createSocket() throws IOException { |
| 183 | Socket socket = new Socket(); |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 184 | bindSocket(socket); |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 185 | return socket; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 190 | * Returns a {@link SocketFactory} bound to this network. Any {@link Socket} created by |
| 191 | * this factory will have its traffic sent over this {@code Network}. Note that if this |
| 192 | * {@code Network} ever disconnects, this factory and any {@link Socket} it produced in the |
| 193 | * past or future will cease to work. |
| 194 | * |
| 195 | * @return a {@link SocketFactory} which produces {@link Socket} instances bound to this |
| 196 | * {@code Network}. |
| 197 | */ |
Paul Jensen | 0a363a3 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 198 | public SocketFactory getSocketFactory() { |
Lorenzo Colitti | 5b37fa2 | 2014-07-29 22:51:27 +0900 | [diff] [blame] | 199 | if (mNetworkBoundSocketFactory == null) { |
| 200 | synchronized (mLock) { |
| 201 | if (mNetworkBoundSocketFactory == null) { |
| 202 | mNetworkBoundSocketFactory = new NetworkBoundSocketFactory(netId); |
| 203 | } |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 204 | } |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 205 | } |
| 206 | return mNetworkBoundSocketFactory; |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Paul Jensen | 07ed074 | 2014-09-08 14:59:09 -0400 | [diff] [blame] | 209 | // TODO: This creates a connection pool and host resolver for |
Lorenzo Colitti | 0ed0a28 | 2014-08-28 17:15:40 -0700 | [diff] [blame] | 210 | // every Network object, instead of one for every NetId. This is |
| 211 | // suboptimal, because an app could potentially have more than one |
| 212 | // Network object for the same NetId, causing increased memory footprint |
| 213 | // and performance penalties due to lack of connection reuse (connection |
| 214 | // setup time, congestion window growth time, etc.). |
| 215 | // |
Paul Jensen | 07ed074 | 2014-09-08 14:59:09 -0400 | [diff] [blame] | 216 | // Instead, investigate only having one connection pool and host resolver |
| 217 | // for every NetId, perhaps by using a static HashMap of NetIds to |
| 218 | // connection pools and host resolvers. The tricky part is deciding when |
| 219 | // to remove a map entry; a WeakHashMap shouldn't be used because whether |
| 220 | // a Network is referenced doesn't correlate with whether a new Network |
| 221 | // will be instantiated in the near future with the same NetID. A good |
| 222 | // solution would involve purging empty (or when all connections are timed |
| 223 | // out) ConnectionPools. |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 224 | private void maybeInitHttpClient() { |
Paul Jensen | 07ed074 | 2014-09-08 14:59:09 -0400 | [diff] [blame] | 225 | synchronized (mLock) { |
Tobias Thierer | 851d806 | 2016-06-01 20:51:45 +0100 | [diff] [blame] | 226 | if (mDns == null) { |
| 227 | mDns = new Dns() { |
Paul Jensen | 07ed074 | 2014-09-08 14:59:09 -0400 | [diff] [blame] | 228 | @Override |
Tobias Thierer | 851d806 | 2016-06-01 20:51:45 +0100 | [diff] [blame] | 229 | public List<InetAddress> lookup(String hostname) throws UnknownHostException { |
| 230 | return Arrays.asList(Network.this.getAllByName(hostname)); |
Paul Jensen | 07ed074 | 2014-09-08 14:59:09 -0400 | [diff] [blame] | 231 | } |
| 232 | }; |
| 233 | } |
| 234 | if (mConnectionPool == null) { |
| 235 | mConnectionPool = new ConnectionPool(httpMaxConnections, |
Tobias Thierer | 851d806 | 2016-06-01 20:51:45 +0100 | [diff] [blame] | 236 | httpKeepAliveDurationMs, TimeUnit.MILLISECONDS); |
Lorenzo Colitti | 5b37fa2 | 2014-07-29 22:51:27 +0900 | [diff] [blame] | 237 | } |
Lorenzo Colitti | 5b37fa2 | 2014-07-29 22:51:27 +0900 | [diff] [blame] | 238 | } |
Lorenzo Colitti | 5b37fa2 | 2014-07-29 22:51:27 +0900 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /** |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 242 | * Opens the specified {@link URL} on this {@code Network}, such that all traffic will be sent |
| 243 | * on this Network. The URL protocol must be {@code HTTP} or {@code HTTPS}. |
| 244 | * |
| 245 | * @return a {@code URLConnection} to the resource referred to by this URL. |
| 246 | * @throws MalformedURLException if the URL protocol is not HTTP or HTTPS. |
| 247 | * @throws IOException if an error occurs while opening the connection. |
| 248 | * @see java.net.URL#openConnection() |
Lorenzo Colitti | 5b37fa2 | 2014-07-29 22:51:27 +0900 | [diff] [blame] | 249 | */ |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 250 | public URLConnection openConnection(URL url) throws IOException { |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 251 | final ConnectivityManager cm = ConnectivityManager.getInstanceOrNull(); |
| 252 | if (cm == null) { |
| 253 | throw new IOException("No ConnectivityManager yet constructed, please construct one"); |
| 254 | } |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 255 | // TODO: Should this be optimized to avoid fetching the global proxy for every request? |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 256 | final ProxyInfo proxyInfo = cm.getProxyForNetwork(this); |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 257 | java.net.Proxy proxy = null; |
| 258 | if (proxyInfo != null) { |
| 259 | proxy = proxyInfo.makeProxy(); |
| 260 | } else { |
| 261 | proxy = java.net.Proxy.NO_PROXY; |
| 262 | } |
| 263 | return openConnection(url, proxy); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Opens the specified {@link URL} on this {@code Network}, such that all traffic will be sent |
| 268 | * on this Network. The URL protocol must be {@code HTTP} or {@code HTTPS}. |
| 269 | * |
| 270 | * @param proxy the proxy through which the connection will be established. |
| 271 | * @return a {@code URLConnection} to the resource referred to by this URL. |
| 272 | * @throws MalformedURLException if the URL protocol is not HTTP or HTTPS. |
| 273 | * @throws IllegalArgumentException if the argument proxy is null. |
| 274 | * @throws IOException if an error occurs while opening the connection. |
| 275 | * @see java.net.URL#openConnection() |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 276 | */ |
| 277 | public URLConnection openConnection(URL url, java.net.Proxy proxy) throws IOException { |
| 278 | if (proxy == null) throw new IllegalArgumentException("proxy is null"); |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 279 | maybeInitHttpClient(); |
| 280 | String protocol = url.getProtocol(); |
Neil Fuller | 50a01d8 | 2015-01-12 16:49:06 +0000 | [diff] [blame] | 281 | OkUrlFactory okUrlFactory; |
| 282 | // TODO: HttpHandler creates OkUrlFactory instances that share the default ResponseCache. |
Paul Jensen | 07ed074 | 2014-09-08 14:59:09 -0400 | [diff] [blame] | 283 | // Could this cause unexpected behavior? |
Paul Jensen | 07ed074 | 2014-09-08 14:59:09 -0400 | [diff] [blame] | 284 | if (protocol.equals("http")) { |
Neil Fuller | 7014da7 | 2015-01-20 13:21:58 +0000 | [diff] [blame] | 285 | okUrlFactory = HttpHandler.createHttpOkUrlFactory(proxy); |
Paul Jensen | 07ed074 | 2014-09-08 14:59:09 -0400 | [diff] [blame] | 286 | } else if (protocol.equals("https")) { |
Neil Fuller | 7014da7 | 2015-01-20 13:21:58 +0000 | [diff] [blame] | 287 | okUrlFactory = HttpsHandler.createHttpsOkUrlFactory(proxy); |
Paul Jensen | 07ed074 | 2014-09-08 14:59:09 -0400 | [diff] [blame] | 288 | } else { |
Neil Fuller | 50a01d8 | 2015-01-12 16:49:06 +0000 | [diff] [blame] | 289 | // OkHttp only supports HTTP and HTTPS and returns a null URLStreamHandler if |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 290 | // passed another protocol. |
| 291 | throw new MalformedURLException("Invalid URL or unrecognized protocol " + protocol); |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 292 | } |
Neil Fuller | 50a01d8 | 2015-01-12 16:49:06 +0000 | [diff] [blame] | 293 | OkHttpClient client = okUrlFactory.client(); |
| 294 | client.setSocketFactory(getSocketFactory()).setConnectionPool(mConnectionPool); |
Tobias Thierer | 851d806 | 2016-06-01 20:51:45 +0100 | [diff] [blame] | 295 | // Let network traffic go via mDns |
| 296 | client.setDns(mDns); |
Neil Fuller | 50a01d8 | 2015-01-12 16:49:06 +0000 | [diff] [blame] | 297 | |
| 298 | return okUrlFactory.open(url); |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /** |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame] | 302 | * Binds the specified {@link DatagramSocket} to this {@code Network}. All data traffic on the |
| 303 | * socket will be sent on this {@code Network}, irrespective of any process-wide network binding |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 304 | * set by {@link ConnectivityManager#bindProcessToNetwork}. The socket must not be |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame] | 305 | * connected. |
| 306 | */ |
| 307 | public void bindSocket(DatagramSocket socket) throws IOException { |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame] | 308 | // Query a property of the underlying socket to ensure that the socket's file descriptor |
| 309 | // exists, is available to bind to a network and is not closed. |
| 310 | socket.getReuseAddress(); |
Erik Kline | d895999 | 2015-06-05 17:37:45 +0900 | [diff] [blame] | 311 | bindSocket(socket.getFileDescriptor$()); |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /** |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 315 | * Binds the specified {@link Socket} to this {@code Network}. All data traffic on the socket |
| 316 | * will be sent on this {@code Network}, irrespective of any process-wide network binding set by |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 317 | * {@link ConnectivityManager#bindProcessToNetwork}. The socket must not be connected. |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 318 | */ |
| 319 | public void bindSocket(Socket socket) throws IOException { |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame] | 320 | // Query a property of the underlying socket to ensure that the socket's file descriptor |
| 321 | // exists, is available to bind to a network and is not closed. |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 322 | socket.getReuseAddress(); |
Erik Kline | d895999 | 2015-06-05 17:37:45 +0900 | [diff] [blame] | 323 | bindSocket(socket.getFileDescriptor$()); |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Erik Kline | d895999 | 2015-06-05 17:37:45 +0900 | [diff] [blame] | 326 | /** |
| 327 | * Binds the specified {@link FileDescriptor} to this {@code Network}. All data traffic on the |
| 328 | * socket represented by this file descriptor will be sent on this {@code Network}, |
| 329 | * irrespective of any process-wide network binding set by |
| 330 | * {@link ConnectivityManager#bindProcessToNetwork}. The socket must not be connected. |
| 331 | */ |
| 332 | public void bindSocket(FileDescriptor fd) throws IOException { |
| 333 | try { |
| 334 | final SocketAddress peer = Os.getpeername(fd); |
| 335 | final InetAddress inetPeer = ((InetSocketAddress) peer).getAddress(); |
| 336 | if (!inetPeer.isAnyLocalAddress()) { |
| 337 | // Apparently, the kernel doesn't update a connected UDP socket's |
| 338 | // routing upon mark changes. |
| 339 | throw new SocketException("Socket is connected"); |
| 340 | } |
| 341 | } catch (ErrnoException e) { |
| 342 | // getpeername() failed. |
| 343 | if (e.errno != OsConstants.ENOTCONN) { |
| 344 | throw e.rethrowAsSocketException(); |
| 345 | } |
| 346 | } catch (ClassCastException e) { |
| 347 | // Wasn't an InetSocketAddress. |
| 348 | throw new SocketException("Only AF_INET/AF_INET6 sockets supported"); |
| 349 | } |
| 350 | |
| 351 | final int err = NetworkUtils.bindSocketToNetwork(fd.getInt$(), netId); |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 352 | if (err != 0) { |
| 353 | // bindSocketToNetwork returns negative errno. |
| 354 | throw new ErrnoException("Binding socket to network " + netId, -err) |
| 355 | .rethrowAsSocketException(); |
| 356 | } |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 357 | } |
| 358 | |
Erik Kline | 25f3b7b | 2015-03-05 15:13:37 +0900 | [diff] [blame] | 359 | /** |
| 360 | * Returns a handle representing this {@code Network}, for use with the NDK API. |
| 361 | */ |
| 362 | public long getNetworkHandle() { |
| 363 | // The network handle is explicitly not the same as the netId. |
| 364 | // |
| 365 | // The netId is an implementation detail which might be changed in the |
| 366 | // future, or which alone (i.e. in the absence of some additional |
| 367 | // context) might not be sufficient to fully identify a Network. |
| 368 | // |
| 369 | // As such, the intention is to prevent accidental misuse of the API |
| 370 | // that might result if a developer assumed that handles and netIds |
| 371 | // were identical and passing a netId to a call expecting a handle |
| 372 | // "just worked". Such accidental misuse, if widely deployed, might |
| 373 | // prevent future changes to the semantics of the netId field or |
| 374 | // inhibit the expansion of state required for Network objects. |
| 375 | // |
| 376 | // This extra layer of indirection might be seen as paranoia, and might |
| 377 | // never end up being necessary, but the added complexity is trivial. |
| 378 | // At some future date it may be desirable to realign the handle with |
| 379 | // Multiple Provisioning Domains API recommendations, as made by the |
| 380 | // IETF mif working group. |
| 381 | // |
| 382 | // The HANDLE_MAGIC value MUST be kept in sync with the corresponding |
| 383 | // value in the native/android/net.c NDK implementation. |
Erik Kline | e1a6cf2 | 2015-05-14 15:40:07 +0900 | [diff] [blame] | 384 | if (netId == 0) { |
| 385 | return 0L; // make this zero condition obvious for debugging |
| 386 | } |
Erik Kline | 25f3b7b | 2015-03-05 15:13:37 +0900 | [diff] [blame] | 387 | final long HANDLE_MAGIC = 0xfacade; |
| 388 | return (((long) netId) << 32) | HANDLE_MAGIC; |
| 389 | } |
| 390 | |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 391 | // implement the Parcelable interface |
| 392 | public int describeContents() { |
| 393 | return 0; |
| 394 | } |
| 395 | public void writeToParcel(Parcel dest, int flags) { |
| 396 | dest.writeInt(netId); |
| 397 | } |
| 398 | |
| 399 | public static final Creator<Network> CREATOR = |
| 400 | new Creator<Network>() { |
| 401 | public Network createFromParcel(Parcel in) { |
| 402 | int netId = in.readInt(); |
| 403 | |
| 404 | return new Network(netId); |
| 405 | } |
| 406 | |
| 407 | public Network[] newArray(int size) { |
| 408 | return new Network[size]; |
| 409 | } |
| 410 | }; |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 411 | |
Robert Greenwalt | 2c7bf2c | 2014-06-23 16:07:43 -0700 | [diff] [blame] | 412 | @Override |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 413 | public boolean equals(Object obj) { |
| 414 | if (obj instanceof Network == false) return false; |
| 415 | Network other = (Network)obj; |
| 416 | return this.netId == other.netId; |
| 417 | } |
| 418 | |
Robert Greenwalt | 2c7bf2c | 2014-06-23 16:07:43 -0700 | [diff] [blame] | 419 | @Override |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 420 | public int hashCode() { |
| 421 | return netId * 11; |
| 422 | } |
Robert Greenwalt | 2c7bf2c | 2014-06-23 16:07:43 -0700 | [diff] [blame] | 423 | |
| 424 | @Override |
| 425 | public String toString() { |
| 426 | return Integer.toString(netId); |
| 427 | } |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 428 | } |