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