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 | |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Remi NGUYEN VAN | 31f1d0c | 2019-01-20 12:52:43 +0900 | [diff] [blame] | 20 | import android.annotation.SystemApi; |
| 21 | import android.annotation.TestApi; |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 22 | import android.annotation.UnsupportedAppUsage; |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 23 | import android.os.Parcel; |
Tobias Thierer | 4c6d2ec | 2017-10-25 00:58:29 +0100 | [diff] [blame] | 24 | import android.os.Parcelable; |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 25 | import android.system.ErrnoException; |
Erik Kline | d895999 | 2015-06-05 17:37:45 +0900 | [diff] [blame] | 26 | import android.system.Os; |
| 27 | import android.system.OsConstants; |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 28 | import android.util.proto.ProtoOutputStream; |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 29 | |
Tobias Thierer | 4c6d2ec | 2017-10-25 00:58:29 +0100 | [diff] [blame] | 30 | import com.android.okhttp.internalandroidapi.Dns; |
| 31 | import com.android.okhttp.internalandroidapi.HttpURLConnectionFactory; |
Tobias Thierer | 69b8b29 | 2017-04-11 22:16:53 +0100 | [diff] [blame] | 32 | |
Paul Jensen | 89bb993 | 2018-03-29 07:41:43 -0400 | [diff] [blame] | 33 | import libcore.io.IoUtils; |
| 34 | |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame] | 35 | import java.io.FileDescriptor; |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 36 | import java.io.IOException; |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame] | 37 | import java.net.DatagramSocket; |
Paul Jensen | 3e32a4b | 2014-05-13 19:05:13 -0400 | [diff] [blame] | 38 | import java.net.InetAddress; |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 39 | import java.net.InetSocketAddress; |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 40 | import java.net.MalformedURLException; |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 41 | import java.net.Socket; |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 42 | import java.net.SocketAddress; |
Paul Jensen | 32a58f0 | 2014-06-20 13:58:14 -0400 | [diff] [blame] | 43 | import java.net.SocketException; |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 44 | import java.net.URL; |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 45 | import java.net.URLConnection; |
Tobias Thierer | 4c6d2ec | 2017-10-25 00:58:29 +0100 | [diff] [blame] | 46 | import java.net.UnknownHostException; |
Tobias Thierer | 851d806 | 2016-06-01 20:51:45 +0100 | [diff] [blame] | 47 | import java.util.Arrays; |
Tobias Thierer | 851d806 | 2016-06-01 20:51:45 +0100 | [diff] [blame] | 48 | import java.util.concurrent.TimeUnit; |
Tobias Thierer | 4c6d2ec | 2017-10-25 00:58:29 +0100 | [diff] [blame] | 49 | |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 50 | import javax.net.SocketFactory; |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 53 | * Identifies a {@code Network}. This is supplied to applications via |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 54 | * {@link ConnectivityManager.NetworkCallback} in response to the active |
| 55 | * {@link ConnectivityManager#requestNetwork} or passive |
| 56 | * {@link ConnectivityManager#registerNetworkCallback} calls. |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 57 | * 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] | 58 | * through a targeted {@link SocketFactory} or process-wide via |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 59 | * {@link ConnectivityManager#bindProcessToNetwork}. |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 60 | */ |
| 61 | public class Network implements Parcelable { |
| 62 | |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 63 | /** |
| 64 | * @hide |
| 65 | */ |
markchien | 665a2cc | 2019-12-04 14:01:46 +0800 | [diff] [blame] | 66 | @SystemApi |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 67 | public final int netId; |
| 68 | |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 69 | // Objects used to perform per-network operations such as getSocketFactory |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 70 | // and openConnection, and a lock to protect access to them. |
Lorenzo Colitti | 5b37fa2 | 2014-07-29 22:51:27 +0900 | [diff] [blame] | 71 | private volatile NetworkBoundSocketFactory mNetworkBoundSocketFactory = null; |
Tobias Thierer | 69b8b29 | 2017-04-11 22:16:53 +0100 | [diff] [blame] | 72 | // mLock should be used to control write access to mUrlConnectionFactory. |
| 73 | // maybeInitUrlConnectionFactory() must be called prior to reading this field. |
| 74 | private volatile HttpURLConnectionFactory mUrlConnectionFactory; |
Erik Kline | d895999 | 2015-06-05 17:37:45 +0900 | [diff] [blame] | 75 | private final Object mLock = new Object(); |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 76 | |
Lorenzo Colitti | 0ed0a28 | 2014-08-28 17:15:40 -0700 | [diff] [blame] | 77 | // Default connection pool values. These are evaluated at startup, just |
| 78 | // like the OkHttp code. Also like the OkHttp code, we will throw parse |
| 79 | // exceptions at class loading time if the properties are set but are not |
| 80 | // valid integers. |
| 81 | private static final boolean httpKeepAlive = |
| 82 | Boolean.parseBoolean(System.getProperty("http.keepAlive", "true")); |
| 83 | private static final int httpMaxConnections = |
| 84 | httpKeepAlive ? Integer.parseInt(System.getProperty("http.maxConnections", "5")) : 0; |
| 85 | private static final long httpKeepAliveDurationMs = |
| 86 | Long.parseLong(System.getProperty("http.keepAliveDuration", "300000")); // 5 minutes. |
Paul Jensen | 4af1781 | 2018-03-28 09:19:58 -0400 | [diff] [blame] | 87 | // Value used to obfuscate network handle longs. |
| 88 | // The HANDLE_MAGIC value MUST be kept in sync with the corresponding |
| 89 | // value in the native/android/net.c NDK implementation. |
| 90 | private static final long HANDLE_MAGIC = 0xcafed00dL; |
| 91 | private static final int HANDLE_MAGIC_SIZE = 32; |
Lorenzo Colitti | 0ed0a28 | 2014-08-28 17:15:40 -0700 | [diff] [blame] | 92 | |
Erik Kline | 7eccfeb | 2018-05-22 21:15:49 +0900 | [diff] [blame] | 93 | // A boolean to control how getAllByName()/getByName() behaves in the face |
| 94 | // of Private DNS. |
| 95 | // |
| 96 | // When true, these calls will request that DNS resolution bypass any |
| 97 | // Private DNS that might otherwise apply. Use of this feature is restricted |
| 98 | // and permission checks are made by netd (attempts to bypass Private DNS |
| 99 | // without appropriate permission are silently turned into vanilla DNS |
| 100 | // requests). This only affects DNS queries made using this network object. |
| 101 | // |
| 102 | // It it not parceled to receivers because (a) it can be set or cleared at |
| 103 | // anytime and (b) receivers should be explicit about attempts to bypass |
| 104 | // Private DNS so that the intent of the code is easily determined and |
| 105 | // code search audits are possible. |
Erik Kline | f4fa982 | 2018-04-27 22:48:33 +0900 | [diff] [blame] | 106 | private final transient boolean mPrivateDnsBypass; |
Erik Kline | 7eccfeb | 2018-05-22 21:15:49 +0900 | [diff] [blame] | 107 | |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 108 | /** |
| 109 | * @hide |
| 110 | */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 111 | @UnsupportedAppUsage |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 112 | public Network(int netId) { |
Erik Kline | f4fa982 | 2018-04-27 22:48:33 +0900 | [diff] [blame] | 113 | this(netId, false); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @hide |
| 118 | */ |
| 119 | public Network(int netId, boolean privateDnsBypass) { |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 120 | this.netId = netId; |
Erik Kline | f4fa982 | 2018-04-27 22:48:33 +0900 | [diff] [blame] | 121 | this.mPrivateDnsBypass = privateDnsBypass; |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 124 | /** |
| 125 | * @hide |
| 126 | */ |
Remi NGUYEN VAN | 5c5f1ba | 2019-01-29 12:08:43 +0900 | [diff] [blame] | 127 | @SystemApi |
| 128 | @TestApi |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 129 | public Network(@NonNull Network that) { |
Erik Kline | f4fa982 | 2018-04-27 22:48:33 +0900 | [diff] [blame] | 130 | this(that.netId, that.mPrivateDnsBypass); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Paul Jensen | 3e32a4b | 2014-05-13 19:05:13 -0400 | [diff] [blame] | 133 | /** |
| 134 | * Operates the same as {@code InetAddress.getAllByName} except that host |
| 135 | * resolution is done on this network. |
| 136 | * |
| 137 | * @param host the hostname or literal IP string to be resolved. |
| 138 | * @return the array of addresses associated with the specified host. |
| 139 | * @throws UnknownHostException if the address lookup fails. |
| 140 | */ |
| 141 | public InetAddress[] getAllByName(String host) throws UnknownHostException { |
Erik Kline | 7eccfeb | 2018-05-22 21:15:49 +0900 | [diff] [blame] | 142 | return InetAddress.getAllByNameOnNet(host, getNetIdForResolv()); |
Paul Jensen | 3e32a4b | 2014-05-13 19:05:13 -0400 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Operates the same as {@code InetAddress.getByName} except that host |
| 147 | * resolution is done on this network. |
| 148 | * |
Erik Kline | f4fa982 | 2018-04-27 22:48:33 +0900 | [diff] [blame] | 149 | * @param host the hostname to be resolved to an address or {@code null}. |
Paul Jensen | 3e32a4b | 2014-05-13 19:05:13 -0400 | [diff] [blame] | 150 | * @return the {@code InetAddress} instance representing the host. |
| 151 | * @throws UnknownHostException |
| 152 | * if the address lookup fails. |
| 153 | */ |
| 154 | public InetAddress getByName(String host) throws UnknownHostException { |
Erik Kline | 7eccfeb | 2018-05-22 21:15:49 +0900 | [diff] [blame] | 155 | return InetAddress.getByNameOnNet(host, getNetIdForResolv()); |
| 156 | } |
| 157 | |
| 158 | /** |
Erik Kline | f4fa982 | 2018-04-27 22:48:33 +0900 | [diff] [blame] | 159 | * Obtain a Network object for which Private DNS is to be bypassed when attempting |
Chalard Jean | 6823744 | 2018-06-07 13:46:52 +0900 | [diff] [blame] | 160 | * to use {@link #getAllByName(String)}/{@link #getByName(String)} methods on the given |
Erik Kline | 7eccfeb | 2018-05-22 21:15:49 +0900 | [diff] [blame] | 161 | * instance for hostname resolution. |
| 162 | * |
| 163 | * @hide |
| 164 | */ |
Remi NGUYEN VAN | 31f1d0c | 2019-01-20 12:52:43 +0900 | [diff] [blame] | 165 | @TestApi |
| 166 | @SystemApi |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 167 | public @NonNull Network getPrivateDnsBypassingCopy() { |
Erik Kline | f4fa982 | 2018-04-27 22:48:33 +0900 | [diff] [blame] | 168 | return new Network(netId, true); |
Erik Kline | 7eccfeb | 2018-05-22 21:15:49 +0900 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Returns a netid marked with the Private DNS bypass flag. |
| 173 | * |
| 174 | * This flag must be kept in sync with the NETID_USE_LOCAL_NAMESERVERS flag |
| 175 | * in system/netd/include/NetdClient.h. |
| 176 | * |
| 177 | * @hide |
| 178 | */ |
| 179 | public int getNetIdForResolv() { |
| 180 | return mPrivateDnsBypass |
| 181 | ? (int) (0x80000000L | (long) netId) // Non-portable DNS resolution flag. |
| 182 | : netId; |
Paul Jensen | 3e32a4b | 2014-05-13 19:05:13 -0400 | [diff] [blame] | 183 | } |
| 184 | |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 185 | /** |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 186 | * A {@code SocketFactory} that produces {@code Socket}'s bound to this network. |
| 187 | */ |
| 188 | private class NetworkBoundSocketFactory extends SocketFactory { |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 189 | private Socket connectToHost(String host, int port, SocketAddress localAddress) |
| 190 | throws IOException { |
Paul Jensen | 8ef3401 | 2014-05-27 10:19:39 -0400 | [diff] [blame] | 191 | // Lookup addresses only on this Network. |
| 192 | InetAddress[] hostAddresses = getAllByName(host); |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 193 | // Try all addresses. |
| 194 | for (int i = 0; i < hostAddresses.length; i++) { |
Paul Jensen | 8ef3401 | 2014-05-27 10:19:39 -0400 | [diff] [blame] | 195 | try { |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 196 | Socket socket = createSocket(); |
Paul Jensen | 89bb993 | 2018-03-29 07:41:43 -0400 | [diff] [blame] | 197 | boolean failed = true; |
| 198 | try { |
| 199 | if (localAddress != null) socket.bind(localAddress); |
| 200 | socket.connect(new InetSocketAddress(hostAddresses[i], port)); |
| 201 | failed = false; |
| 202 | return socket; |
| 203 | } finally { |
| 204 | if (failed) IoUtils.closeQuietly(socket); |
| 205 | } |
Paul Jensen | 8ef3401 | 2014-05-27 10:19:39 -0400 | [diff] [blame] | 206 | } catch (IOException e) { |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 207 | if (i == (hostAddresses.length - 1)) throw e; |
Paul Jensen | 8ef3401 | 2014-05-27 10:19:39 -0400 | [diff] [blame] | 208 | } |
| 209 | } |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 210 | throw new UnknownHostException(host); |
Paul Jensen | 8ef3401 | 2014-05-27 10:19:39 -0400 | [diff] [blame] | 211 | } |
| 212 | |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 213 | @Override |
Chalard Jean | 6823744 | 2018-06-07 13:46:52 +0900 | [diff] [blame] | 214 | public Socket createSocket(String host, int port, InetAddress localHost, int localPort) |
| 215 | throws IOException { |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 216 | return connectToHost(host, port, new InetSocketAddress(localHost, localPort)); |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public Socket createSocket(InetAddress address, int port, InetAddress localAddress, |
| 221 | int localPort) throws IOException { |
| 222 | Socket socket = createSocket(); |
Paul Jensen | 89bb993 | 2018-03-29 07:41:43 -0400 | [diff] [blame] | 223 | boolean failed = true; |
| 224 | try { |
| 225 | socket.bind(new InetSocketAddress(localAddress, localPort)); |
| 226 | socket.connect(new InetSocketAddress(address, port)); |
| 227 | failed = false; |
| 228 | } finally { |
| 229 | if (failed) IoUtils.closeQuietly(socket); |
| 230 | } |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 231 | return socket; |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public Socket createSocket(InetAddress host, int port) throws IOException { |
| 236 | Socket socket = createSocket(); |
Paul Jensen | 89bb993 | 2018-03-29 07:41:43 -0400 | [diff] [blame] | 237 | boolean failed = true; |
| 238 | try { |
| 239 | socket.connect(new InetSocketAddress(host, port)); |
| 240 | failed = false; |
| 241 | } finally { |
| 242 | if (failed) IoUtils.closeQuietly(socket); |
| 243 | } |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 244 | return socket; |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public Socket createSocket(String host, int port) throws IOException { |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 249 | return connectToHost(host, port, null); |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public Socket createSocket() throws IOException { |
| 254 | Socket socket = new Socket(); |
Paul Jensen | 89bb993 | 2018-03-29 07:41:43 -0400 | [diff] [blame] | 255 | boolean failed = true; |
| 256 | try { |
| 257 | bindSocket(socket); |
| 258 | failed = false; |
| 259 | } finally { |
| 260 | if (failed) IoUtils.closeQuietly(socket); |
| 261 | } |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 262 | return socket; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /** |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 267 | * Returns a {@link SocketFactory} bound to this network. Any {@link Socket} created by |
| 268 | * this factory will have its traffic sent over this {@code Network}. Note that if this |
| 269 | * {@code Network} ever disconnects, this factory and any {@link Socket} it produced in the |
| 270 | * past or future will cease to work. |
| 271 | * |
| 272 | * @return a {@link SocketFactory} which produces {@link Socket} instances bound to this |
| 273 | * {@code Network}. |
| 274 | */ |
Paul Jensen | 0a363a3 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 275 | public SocketFactory getSocketFactory() { |
Lorenzo Colitti | 5b37fa2 | 2014-07-29 22:51:27 +0900 | [diff] [blame] | 276 | if (mNetworkBoundSocketFactory == null) { |
| 277 | synchronized (mLock) { |
| 278 | if (mNetworkBoundSocketFactory == null) { |
Chalard Jean | 6823744 | 2018-06-07 13:46:52 +0900 | [diff] [blame] | 279 | mNetworkBoundSocketFactory = new NetworkBoundSocketFactory(); |
Lorenzo Colitti | 5b37fa2 | 2014-07-29 22:51:27 +0900 | [diff] [blame] | 280 | } |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 281 | } |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 282 | } |
| 283 | return mNetworkBoundSocketFactory; |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Paul Jensen | 07ed074 | 2014-09-08 14:59:09 -0400 | [diff] [blame] | 286 | // TODO: This creates a connection pool and host resolver for |
Lorenzo Colitti | 0ed0a28 | 2014-08-28 17:15:40 -0700 | [diff] [blame] | 287 | // every Network object, instead of one for every NetId. This is |
| 288 | // suboptimal, because an app could potentially have more than one |
| 289 | // Network object for the same NetId, causing increased memory footprint |
| 290 | // and performance penalties due to lack of connection reuse (connection |
| 291 | // setup time, congestion window growth time, etc.). |
| 292 | // |
Paul Jensen | 07ed074 | 2014-09-08 14:59:09 -0400 | [diff] [blame] | 293 | // Instead, investigate only having one connection pool and host resolver |
| 294 | // for every NetId, perhaps by using a static HashMap of NetIds to |
| 295 | // connection pools and host resolvers. The tricky part is deciding when |
| 296 | // to remove a map entry; a WeakHashMap shouldn't be used because whether |
| 297 | // a Network is referenced doesn't correlate with whether a new Network |
| 298 | // will be instantiated in the near future with the same NetID. A good |
| 299 | // solution would involve purging empty (or when all connections are timed |
| 300 | // out) ConnectionPools. |
Tobias Thierer | 69b8b29 | 2017-04-11 22:16:53 +0100 | [diff] [blame] | 301 | private void maybeInitUrlConnectionFactory() { |
Paul Jensen | 07ed074 | 2014-09-08 14:59:09 -0400 | [diff] [blame] | 302 | synchronized (mLock) { |
Tobias Thierer | 69b8b29 | 2017-04-11 22:16:53 +0100 | [diff] [blame] | 303 | if (mUrlConnectionFactory == null) { |
| 304 | // Set configuration on the HttpURLConnectionFactory that will be good for all |
| 305 | // connections created by this Network. Configuration that might vary is left |
| 306 | // until openConnection() and passed as arguments. |
| 307 | Dns dnsLookup = hostname -> Arrays.asList(Network.this.getAllByName(hostname)); |
| 308 | HttpURLConnectionFactory urlConnectionFactory = new HttpURLConnectionFactory(); |
| 309 | urlConnectionFactory.setDns(dnsLookup); // Let traffic go via dnsLookup |
| 310 | // A private connection pool just for this Network. |
| 311 | urlConnectionFactory.setNewConnectionPool(httpMaxConnections, |
Tobias Thierer | 851d806 | 2016-06-01 20:51:45 +0100 | [diff] [blame] | 312 | httpKeepAliveDurationMs, TimeUnit.MILLISECONDS); |
Tobias Thierer | 69b8b29 | 2017-04-11 22:16:53 +0100 | [diff] [blame] | 313 | mUrlConnectionFactory = urlConnectionFactory; |
Lorenzo Colitti | 5b37fa2 | 2014-07-29 22:51:27 +0900 | [diff] [blame] | 314 | } |
Lorenzo Colitti | 5b37fa2 | 2014-07-29 22:51:27 +0900 | [diff] [blame] | 315 | } |
Lorenzo Colitti | 5b37fa2 | 2014-07-29 22:51:27 +0900 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /** |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 319 | * Opens the specified {@link URL} on this {@code Network}, such that all traffic will be sent |
| 320 | * on this Network. The URL protocol must be {@code HTTP} or {@code HTTPS}. |
| 321 | * |
| 322 | * @return a {@code URLConnection} to the resource referred to by this URL. |
| 323 | * @throws MalformedURLException if the URL protocol is not HTTP or HTTPS. |
| 324 | * @throws IOException if an error occurs while opening the connection. |
| 325 | * @see java.net.URL#openConnection() |
Lorenzo Colitti | 5b37fa2 | 2014-07-29 22:51:27 +0900 | [diff] [blame] | 326 | */ |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 327 | public URLConnection openConnection(URL url) throws IOException { |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 328 | final ConnectivityManager cm = ConnectivityManager.getInstanceOrNull(); |
| 329 | if (cm == null) { |
| 330 | throw new IOException("No ConnectivityManager yet constructed, please construct one"); |
| 331 | } |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 332 | // 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] | 333 | final ProxyInfo proxyInfo = cm.getProxyForNetwork(this); |
Tobias Thierer | 69b8b29 | 2017-04-11 22:16:53 +0100 | [diff] [blame] | 334 | final java.net.Proxy proxy; |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 335 | if (proxyInfo != null) { |
| 336 | proxy = proxyInfo.makeProxy(); |
| 337 | } else { |
| 338 | proxy = java.net.Proxy.NO_PROXY; |
| 339 | } |
| 340 | return openConnection(url, proxy); |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Opens the specified {@link URL} on this {@code Network}, such that all traffic will be sent |
| 345 | * on this Network. The URL protocol must be {@code HTTP} or {@code HTTPS}. |
| 346 | * |
| 347 | * @param proxy the proxy through which the connection will be established. |
| 348 | * @return a {@code URLConnection} to the resource referred to by this URL. |
| 349 | * @throws MalformedURLException if the URL protocol is not HTTP or HTTPS. |
| 350 | * @throws IllegalArgumentException if the argument proxy is null. |
| 351 | * @throws IOException if an error occurs while opening the connection. |
| 352 | * @see java.net.URL#openConnection() |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 353 | */ |
| 354 | public URLConnection openConnection(URL url, java.net.Proxy proxy) throws IOException { |
| 355 | if (proxy == null) throw new IllegalArgumentException("proxy is null"); |
Tobias Thierer | 69b8b29 | 2017-04-11 22:16:53 +0100 | [diff] [blame] | 356 | maybeInitUrlConnectionFactory(); |
| 357 | SocketFactory socketFactory = getSocketFactory(); |
| 358 | return mUrlConnectionFactory.openConnection(url, socketFactory, proxy); |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /** |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame] | 362 | * Binds the specified {@link DatagramSocket} to this {@code Network}. All data traffic on the |
| 363 | * 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] | 364 | * set by {@link ConnectivityManager#bindProcessToNetwork}. The socket must not be |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame] | 365 | * connected. |
| 366 | */ |
| 367 | public void bindSocket(DatagramSocket socket) throws IOException { |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame] | 368 | // Query a property of the underlying socket to ensure that the socket's file descriptor |
| 369 | // exists, is available to bind to a network and is not closed. |
| 370 | socket.getReuseAddress(); |
Erik Kline | d895999 | 2015-06-05 17:37:45 +0900 | [diff] [blame] | 371 | bindSocket(socket.getFileDescriptor$()); |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /** |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 375 | * Binds the specified {@link Socket} to this {@code Network}. All data traffic on the socket |
| 376 | * 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] | 377 | * {@link ConnectivityManager#bindProcessToNetwork}. The socket must not be connected. |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 378 | */ |
| 379 | public void bindSocket(Socket socket) throws IOException { |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame] | 380 | // Query a property of the underlying socket to ensure that the socket's file descriptor |
| 381 | // exists, is available to bind to a network and is not closed. |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 382 | socket.getReuseAddress(); |
Erik Kline | d895999 | 2015-06-05 17:37:45 +0900 | [diff] [blame] | 383 | bindSocket(socket.getFileDescriptor$()); |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Erik Kline | d895999 | 2015-06-05 17:37:45 +0900 | [diff] [blame] | 386 | /** |
| 387 | * Binds the specified {@link FileDescriptor} to this {@code Network}. All data traffic on the |
| 388 | * socket represented by this file descriptor will be sent on this {@code Network}, |
| 389 | * irrespective of any process-wide network binding set by |
| 390 | * {@link ConnectivityManager#bindProcessToNetwork}. The socket must not be connected. |
| 391 | */ |
| 392 | public void bindSocket(FileDescriptor fd) throws IOException { |
| 393 | try { |
| 394 | final SocketAddress peer = Os.getpeername(fd); |
| 395 | final InetAddress inetPeer = ((InetSocketAddress) peer).getAddress(); |
| 396 | if (!inetPeer.isAnyLocalAddress()) { |
| 397 | // Apparently, the kernel doesn't update a connected UDP socket's |
| 398 | // routing upon mark changes. |
| 399 | throw new SocketException("Socket is connected"); |
| 400 | } |
| 401 | } catch (ErrnoException e) { |
| 402 | // getpeername() failed. |
| 403 | if (e.errno != OsConstants.ENOTCONN) { |
| 404 | throw e.rethrowAsSocketException(); |
| 405 | } |
| 406 | } catch (ClassCastException e) { |
| 407 | // Wasn't an InetSocketAddress. |
| 408 | throw new SocketException("Only AF_INET/AF_INET6 sockets supported"); |
| 409 | } |
| 410 | |
| 411 | final int err = NetworkUtils.bindSocketToNetwork(fd.getInt$(), netId); |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 412 | if (err != 0) { |
| 413 | // bindSocketToNetwork returns negative errno. |
| 414 | throw new ErrnoException("Binding socket to network " + netId, -err) |
| 415 | .rethrowAsSocketException(); |
| 416 | } |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 417 | } |
| 418 | |
Erik Kline | 25f3b7b | 2015-03-05 15:13:37 +0900 | [diff] [blame] | 419 | /** |
Paul Jensen | 4af1781 | 2018-03-28 09:19:58 -0400 | [diff] [blame] | 420 | * Returns a {@link Network} object given a handle returned from {@link #getNetworkHandle}. |
| 421 | * |
| 422 | * @param networkHandle a handle returned from {@link #getNetworkHandle}. |
| 423 | * @return A {@link Network} object derived from {@code networkHandle}. |
| 424 | */ |
| 425 | public static Network fromNetworkHandle(long networkHandle) { |
| 426 | if (networkHandle == 0) { |
| 427 | throw new IllegalArgumentException( |
| 428 | "Network.fromNetworkHandle refusing to instantiate NETID_UNSET Network."); |
| 429 | } |
| 430 | if ((networkHandle & ((1L << HANDLE_MAGIC_SIZE) - 1)) != HANDLE_MAGIC |
| 431 | || networkHandle < 0) { |
| 432 | throw new IllegalArgumentException( |
| 433 | "Value passed to fromNetworkHandle() is not a network handle."); |
| 434 | } |
| 435 | return new Network((int) (networkHandle >> HANDLE_MAGIC_SIZE)); |
| 436 | } |
| 437 | |
| 438 | /** |
Erik Kline | 25f3b7b | 2015-03-05 15:13:37 +0900 | [diff] [blame] | 439 | * Returns a handle representing this {@code Network}, for use with the NDK API. |
| 440 | */ |
| 441 | public long getNetworkHandle() { |
| 442 | // The network handle is explicitly not the same as the netId. |
| 443 | // |
| 444 | // The netId is an implementation detail which might be changed in the |
| 445 | // future, or which alone (i.e. in the absence of some additional |
| 446 | // context) might not be sufficient to fully identify a Network. |
| 447 | // |
| 448 | // As such, the intention is to prevent accidental misuse of the API |
| 449 | // that might result if a developer assumed that handles and netIds |
| 450 | // were identical and passing a netId to a call expecting a handle |
| 451 | // "just worked". Such accidental misuse, if widely deployed, might |
| 452 | // prevent future changes to the semantics of the netId field or |
| 453 | // inhibit the expansion of state required for Network objects. |
| 454 | // |
| 455 | // This extra layer of indirection might be seen as paranoia, and might |
| 456 | // never end up being necessary, but the added complexity is trivial. |
| 457 | // At some future date it may be desirable to realign the handle with |
| 458 | // Multiple Provisioning Domains API recommendations, as made by the |
| 459 | // IETF mif working group. |
Erik Kline | e1a6cf2 | 2015-05-14 15:40:07 +0900 | [diff] [blame] | 460 | if (netId == 0) { |
| 461 | return 0L; // make this zero condition obvious for debugging |
| 462 | } |
Paul Jensen | 4af1781 | 2018-03-28 09:19:58 -0400 | [diff] [blame] | 463 | return (((long) netId) << HANDLE_MAGIC_SIZE) | HANDLE_MAGIC; |
Erik Kline | 25f3b7b | 2015-03-05 15:13:37 +0900 | [diff] [blame] | 464 | } |
| 465 | |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 466 | // implement the Parcelable interface |
| 467 | public int describeContents() { |
| 468 | return 0; |
| 469 | } |
| 470 | public void writeToParcel(Parcel dest, int flags) { |
| 471 | dest.writeInt(netId); |
| 472 | } |
| 473 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 474 | public static final @android.annotation.NonNull Creator<Network> CREATOR = |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 475 | new Creator<Network>() { |
| 476 | public Network createFromParcel(Parcel in) { |
| 477 | int netId = in.readInt(); |
| 478 | |
| 479 | return new Network(netId); |
| 480 | } |
| 481 | |
| 482 | public Network[] newArray(int size) { |
| 483 | return new Network[size]; |
| 484 | } |
| 485 | }; |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 486 | |
Robert Greenwalt | 2c7bf2c | 2014-06-23 16:07:43 -0700 | [diff] [blame] | 487 | @Override |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 488 | public boolean equals(Object obj) { |
Chalard Jean | 6823744 | 2018-06-07 13:46:52 +0900 | [diff] [blame] | 489 | if (!(obj instanceof Network)) return false; |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 490 | Network other = (Network)obj; |
| 491 | return this.netId == other.netId; |
| 492 | } |
| 493 | |
Robert Greenwalt | 2c7bf2c | 2014-06-23 16:07:43 -0700 | [diff] [blame] | 494 | @Override |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 495 | public int hashCode() { |
| 496 | return netId * 11; |
| 497 | } |
Robert Greenwalt | 2c7bf2c | 2014-06-23 16:07:43 -0700 | [diff] [blame] | 498 | |
| 499 | @Override |
| 500 | public String toString() { |
| 501 | return Integer.toString(netId); |
| 502 | } |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 503 | |
| 504 | /** @hide */ |
| 505 | public void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 506 | final long token = proto.start(fieldId); |
| 507 | proto.write(NetworkProto.NET_ID, netId); |
| 508 | proto.end(token); |
| 509 | } |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 510 | } |