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 | |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 19 | import android.net.NetworkUtils; |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 20 | import android.os.Parcelable; |
| 21 | import android.os.Parcel; |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 22 | import android.system.ErrnoException; |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 23 | |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame^] | 24 | import java.io.FileDescriptor; |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 25 | import java.io.IOException; |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame^] | 26 | import java.net.DatagramSocket; |
Paul Jensen | 3e32a4b | 2014-05-13 19:05:13 -0400 | [diff] [blame] | 27 | import java.net.InetAddress; |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 28 | import java.net.InetSocketAddress; |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 29 | import java.net.MalformedURLException; |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 30 | import java.net.Socket; |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 31 | import java.net.SocketAddress; |
Paul Jensen | 32a58f0 | 2014-06-20 13:58:14 -0400 | [diff] [blame] | 32 | import java.net.SocketException; |
Paul Jensen | 3e32a4b | 2014-05-13 19:05:13 -0400 | [diff] [blame] | 33 | import java.net.UnknownHostException; |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 34 | import java.net.URL; |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 35 | import java.net.URLConnection; |
Lorenzo Colitti | 5b37fa2 | 2014-07-29 22:51:27 +0900 | [diff] [blame] | 36 | import java.net.URLStreamHandler; |
| 37 | import java.util.concurrent.atomic.AtomicReference; |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 38 | import javax.net.SocketFactory; |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 39 | |
Lorenzo Colitti | 0ed0a28 | 2014-08-28 17:15:40 -0700 | [diff] [blame] | 40 | import com.android.okhttp.ConnectionPool; |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 41 | import com.android.okhttp.HostResolver; |
Paul Jensen | 07ed074 | 2014-09-08 14:59:09 -0400 | [diff] [blame] | 42 | import com.android.okhttp.HttpHandler; |
| 43 | import com.android.okhttp.HttpsHandler; |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 44 | import com.android.okhttp.OkHttpClient; |
| 45 | |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 46 | /** |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 47 | * Identifies a {@code Network}. This is supplied to applications via |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 48 | * {@link ConnectivityManager.NetworkCallback} in response to the active |
| 49 | * {@link ConnectivityManager#requestNetwork} or passive |
| 50 | * {@link ConnectivityManager#registerNetworkCallback} calls. |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 51 | * 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] | 52 | * through a targeted {@link SocketFactory} or process-wide via |
| 53 | * {@link ConnectivityManager#setProcessDefaultNetwork}. |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 54 | */ |
| 55 | public class Network implements Parcelable { |
| 56 | |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 57 | /** |
| 58 | * @hide |
| 59 | */ |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 60 | public final int netId; |
| 61 | |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 62 | // Objects used to perform per-network operations such as getSocketFactory |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 63 | // and openConnection, and a lock to protect access to them. |
Lorenzo Colitti | 5b37fa2 | 2014-07-29 22:51:27 +0900 | [diff] [blame] | 64 | private volatile NetworkBoundSocketFactory mNetworkBoundSocketFactory = null; |
Paul Jensen | 07ed074 | 2014-09-08 14:59:09 -0400 | [diff] [blame] | 65 | // mLock should be used to control write access to mConnectionPool and mHostResolver. |
| 66 | // maybeInitHttpClient() must be called prior to reading either variable. |
| 67 | private volatile ConnectionPool mConnectionPool = null; |
| 68 | private volatile HostResolver mHostResolver = null; |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 69 | private Object mLock = new Object(); |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 70 | |
Lorenzo Colitti | 0ed0a28 | 2014-08-28 17:15:40 -0700 | [diff] [blame] | 71 | // Default connection pool values. These are evaluated at startup, just |
| 72 | // like the OkHttp code. Also like the OkHttp code, we will throw parse |
| 73 | // exceptions at class loading time if the properties are set but are not |
| 74 | // valid integers. |
| 75 | private static final boolean httpKeepAlive = |
| 76 | Boolean.parseBoolean(System.getProperty("http.keepAlive", "true")); |
| 77 | private static final int httpMaxConnections = |
| 78 | httpKeepAlive ? Integer.parseInt(System.getProperty("http.maxConnections", "5")) : 0; |
| 79 | private static final long httpKeepAliveDurationMs = |
| 80 | Long.parseLong(System.getProperty("http.keepAliveDuration", "300000")); // 5 minutes. |
| 81 | |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 82 | /** |
| 83 | * @hide |
| 84 | */ |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 85 | public Network(int netId) { |
| 86 | this.netId = netId; |
| 87 | } |
| 88 | |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 89 | /** |
| 90 | * @hide |
| 91 | */ |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 92 | public Network(Network that) { |
| 93 | this.netId = that.netId; |
| 94 | } |
| 95 | |
Paul Jensen | 3e32a4b | 2014-05-13 19:05:13 -0400 | [diff] [blame] | 96 | /** |
| 97 | * Operates the same as {@code InetAddress.getAllByName} except that host |
| 98 | * resolution is done on this network. |
| 99 | * |
| 100 | * @param host the hostname or literal IP string to be resolved. |
| 101 | * @return the array of addresses associated with the specified host. |
| 102 | * @throws UnknownHostException if the address lookup fails. |
| 103 | */ |
| 104 | public InetAddress[] getAllByName(String host) throws UnknownHostException { |
| 105 | return InetAddress.getAllByNameOnNet(host, netId); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Operates the same as {@code InetAddress.getByName} except that host |
| 110 | * resolution is done on this network. |
| 111 | * |
| 112 | * @param host |
| 113 | * the hostName to be resolved to an address or {@code null}. |
| 114 | * @return the {@code InetAddress} instance representing the host. |
| 115 | * @throws UnknownHostException |
| 116 | * if the address lookup fails. |
| 117 | */ |
| 118 | public InetAddress getByName(String host) throws UnknownHostException { |
| 119 | return InetAddress.getByNameOnNet(host, netId); |
| 120 | } |
| 121 | |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 122 | /** |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 123 | * A {@code SocketFactory} that produces {@code Socket}'s bound to this network. |
| 124 | */ |
| 125 | private class NetworkBoundSocketFactory extends SocketFactory { |
| 126 | private final int mNetId; |
| 127 | |
| 128 | public NetworkBoundSocketFactory(int netId) { |
| 129 | super(); |
| 130 | mNetId = netId; |
| 131 | } |
| 132 | |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 133 | private Socket connectToHost(String host, int port, SocketAddress localAddress) |
| 134 | throws IOException { |
Paul Jensen | 8ef3401 | 2014-05-27 10:19:39 -0400 | [diff] [blame] | 135 | // Lookup addresses only on this Network. |
| 136 | InetAddress[] hostAddresses = getAllByName(host); |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 137 | // Try all addresses. |
| 138 | for (int i = 0; i < hostAddresses.length; i++) { |
Paul Jensen | 8ef3401 | 2014-05-27 10:19:39 -0400 | [diff] [blame] | 139 | try { |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 140 | Socket socket = createSocket(); |
| 141 | if (localAddress != null) socket.bind(localAddress); |
Paul Jensen | 8ef3401 | 2014-05-27 10:19:39 -0400 | [diff] [blame] | 142 | socket.connect(new InetSocketAddress(hostAddresses[i], port)); |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 143 | return socket; |
Paul Jensen | 8ef3401 | 2014-05-27 10:19:39 -0400 | [diff] [blame] | 144 | } catch (IOException e) { |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 145 | if (i == (hostAddresses.length - 1)) throw e; |
Paul Jensen | 8ef3401 | 2014-05-27 10:19:39 -0400 | [diff] [blame] | 146 | } |
| 147 | } |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 148 | throw new UnknownHostException(host); |
Paul Jensen | 8ef3401 | 2014-05-27 10:19:39 -0400 | [diff] [blame] | 149 | } |
| 150 | |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 151 | @Override |
| 152 | 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] | 153 | return connectToHost(host, port, new InetSocketAddress(localHost, localPort)); |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public Socket createSocket(InetAddress address, int port, InetAddress localAddress, |
| 158 | int localPort) throws IOException { |
| 159 | Socket socket = createSocket(); |
| 160 | socket.bind(new InetSocketAddress(localAddress, localPort)); |
| 161 | socket.connect(new InetSocketAddress(address, port)); |
| 162 | return socket; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public Socket createSocket(InetAddress host, int port) throws IOException { |
| 167 | Socket socket = createSocket(); |
| 168 | socket.connect(new InetSocketAddress(host, port)); |
| 169 | return socket; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public Socket createSocket(String host, int port) throws IOException { |
Lorenzo Colitti | f038289 | 2014-07-30 17:17:13 +0900 | [diff] [blame] | 174 | return connectToHost(host, port, null); |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public Socket createSocket() throws IOException { |
| 179 | Socket socket = new Socket(); |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 180 | bindSocket(socket); |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 181 | return socket; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 186 | * Returns a {@link SocketFactory} bound to this network. Any {@link Socket} created by |
| 187 | * this factory will have its traffic sent over this {@code Network}. Note that if this |
| 188 | * {@code Network} ever disconnects, this factory and any {@link Socket} it produced in the |
| 189 | * past or future will cease to work. |
| 190 | * |
| 191 | * @return a {@link SocketFactory} which produces {@link Socket} instances bound to this |
| 192 | * {@code Network}. |
| 193 | */ |
Paul Jensen | 0a363a3 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 194 | public SocketFactory getSocketFactory() { |
Lorenzo Colitti | 5b37fa2 | 2014-07-29 22:51:27 +0900 | [diff] [blame] | 195 | if (mNetworkBoundSocketFactory == null) { |
| 196 | synchronized (mLock) { |
| 197 | if (mNetworkBoundSocketFactory == null) { |
| 198 | mNetworkBoundSocketFactory = new NetworkBoundSocketFactory(netId); |
| 199 | } |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 200 | } |
Paul Jensen | 2d6f265 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 201 | } |
| 202 | return mNetworkBoundSocketFactory; |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Paul Jensen | 07ed074 | 2014-09-08 14:59:09 -0400 | [diff] [blame] | 205 | // TODO: This creates a connection pool and host resolver for |
Lorenzo Colitti | 0ed0a28 | 2014-08-28 17:15:40 -0700 | [diff] [blame] | 206 | // every Network object, instead of one for every NetId. This is |
| 207 | // suboptimal, because an app could potentially have more than one |
| 208 | // Network object for the same NetId, causing increased memory footprint |
| 209 | // and performance penalties due to lack of connection reuse (connection |
| 210 | // setup time, congestion window growth time, etc.). |
| 211 | // |
Paul Jensen | 07ed074 | 2014-09-08 14:59:09 -0400 | [diff] [blame] | 212 | // Instead, investigate only having one connection pool and host resolver |
| 213 | // for every NetId, perhaps by using a static HashMap of NetIds to |
| 214 | // connection pools and host resolvers. The tricky part is deciding when |
| 215 | // to remove a map entry; a WeakHashMap shouldn't be used because whether |
| 216 | // a Network is referenced doesn't correlate with whether a new Network |
| 217 | // will be instantiated in the near future with the same NetID. A good |
| 218 | // solution would involve purging empty (or when all connections are timed |
| 219 | // out) ConnectionPools. |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 220 | private void maybeInitHttpClient() { |
Paul Jensen | 07ed074 | 2014-09-08 14:59:09 -0400 | [diff] [blame] | 221 | synchronized (mLock) { |
| 222 | if (mHostResolver == null) { |
| 223 | mHostResolver = new HostResolver() { |
| 224 | @Override |
| 225 | public InetAddress[] getAllByName(String host) throws UnknownHostException { |
| 226 | return Network.this.getAllByName(host); |
| 227 | } |
| 228 | }; |
| 229 | } |
| 230 | if (mConnectionPool == null) { |
| 231 | mConnectionPool = new ConnectionPool(httpMaxConnections, |
| 232 | httpKeepAliveDurationMs); |
Lorenzo Colitti | 5b37fa2 | 2014-07-29 22:51:27 +0900 | [diff] [blame] | 233 | } |
Lorenzo Colitti | 5b37fa2 | 2014-07-29 22:51:27 +0900 | [diff] [blame] | 234 | } |
Lorenzo Colitti | 5b37fa2 | 2014-07-29 22:51:27 +0900 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /** |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 238 | * Opens the specified {@link URL} on this {@code Network}, such that all traffic will be sent |
| 239 | * on this Network. The URL protocol must be {@code HTTP} or {@code HTTPS}. |
| 240 | * |
| 241 | * @return a {@code URLConnection} to the resource referred to by this URL. |
| 242 | * @throws MalformedURLException if the URL protocol is not HTTP or HTTPS. |
| 243 | * @throws IOException if an error occurs while opening the connection. |
| 244 | * @see java.net.URL#openConnection() |
Lorenzo Colitti | 5b37fa2 | 2014-07-29 22:51:27 +0900 | [diff] [blame] | 245 | */ |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 246 | public URLConnection openConnection(URL url) throws IOException { |
| 247 | maybeInitHttpClient(); |
| 248 | String protocol = url.getProtocol(); |
Paul Jensen | 07ed074 | 2014-09-08 14:59:09 -0400 | [diff] [blame] | 249 | OkHttpClient client; |
| 250 | // TODO: HttpHandler creates OkHttpClients that share the default ResponseCache. |
| 251 | // Could this cause unexpected behavior? |
| 252 | // TODO: Should the network's proxy be specified? |
| 253 | if (protocol.equals("http")) { |
| 254 | client = HttpHandler.createHttpOkHttpClient(null /* proxy */); |
| 255 | } else if (protocol.equals("https")) { |
| 256 | client = HttpsHandler.createHttpsOkHttpClient(null /* proxy */); |
| 257 | } else { |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 258 | // OkHttpClient only supports HTTP and HTTPS and returns a null URLStreamHandler if |
| 259 | // passed another protocol. |
| 260 | throw new MalformedURLException("Invalid URL or unrecognized protocol " + protocol); |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 261 | } |
Paul Jensen | 07ed074 | 2014-09-08 14:59:09 -0400 | [diff] [blame] | 262 | return client.setSocketFactory(getSocketFactory()) |
| 263 | .setHostResolver(mHostResolver) |
| 264 | .setConnectionPool(mConnectionPool) |
| 265 | .open(url); |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /** |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame^] | 269 | * Binds the specified {@link DatagramSocket} to this {@code Network}. All data traffic on the |
| 270 | * socket will be sent on this {@code Network}, irrespective of any process-wide network binding |
| 271 | * set by {@link ConnectivityManager#setProcessDefaultNetwork}. The socket must not be |
| 272 | * connected. |
| 273 | */ |
| 274 | public void bindSocket(DatagramSocket socket) throws IOException { |
| 275 | // Apparently, the kernel doesn't update a connected UDP socket's routing upon mark changes. |
| 276 | if (socket.isConnected()) { |
| 277 | throw new SocketException("Socket is connected"); |
| 278 | } |
| 279 | // Query a property of the underlying socket to ensure that the socket's file descriptor |
| 280 | // exists, is available to bind to a network and is not closed. |
| 281 | socket.getReuseAddress(); |
| 282 | bindSocketFd(socket.getFileDescriptor$()); |
| 283 | } |
| 284 | |
| 285 | /** |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 286 | * Binds the specified {@link Socket} to this {@code Network}. All data traffic on the socket |
| 287 | * will be sent on this {@code Network}, irrespective of any process-wide network binding set by |
| 288 | * {@link ConnectivityManager#setProcessDefaultNetwork}. The socket must not be connected. |
| 289 | */ |
| 290 | public void bindSocket(Socket socket) throws IOException { |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame^] | 291 | // Apparently, the kernel doesn't update a connected TCP socket's routing upon mark changes. |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 292 | if (socket.isConnected()) { |
| 293 | throw new SocketException("Socket is connected"); |
| 294 | } |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame^] | 295 | // Query a property of the underlying socket to ensure that the socket's file descriptor |
| 296 | // exists, is available to bind to a network and is not closed. |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 297 | socket.getReuseAddress(); |
Sreeram Ramachandran | e15db7c | 2014-10-22 11:10:20 -0700 | [diff] [blame^] | 298 | bindSocketFd(socket.getFileDescriptor$()); |
| 299 | } |
| 300 | |
| 301 | private void bindSocketFd(FileDescriptor fd) throws IOException { |
| 302 | int err = NetworkUtils.bindSocketToNetwork(fd.getInt$(), netId); |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 303 | if (err != 0) { |
| 304 | // bindSocketToNetwork returns negative errno. |
| 305 | throw new ErrnoException("Binding socket to network " + netId, -err) |
| 306 | .rethrowAsSocketException(); |
| 307 | } |
Lorenzo Colitti | c473dc4 | 2014-07-18 01:34:19 +0900 | [diff] [blame] | 308 | } |
| 309 | |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 310 | // implement the Parcelable interface |
| 311 | public int describeContents() { |
| 312 | return 0; |
| 313 | } |
| 314 | public void writeToParcel(Parcel dest, int flags) { |
| 315 | dest.writeInt(netId); |
| 316 | } |
| 317 | |
| 318 | public static final Creator<Network> CREATOR = |
| 319 | new Creator<Network>() { |
| 320 | public Network createFromParcel(Parcel in) { |
| 321 | int netId = in.readInt(); |
| 322 | |
| 323 | return new Network(netId); |
| 324 | } |
| 325 | |
| 326 | public Network[] newArray(int size) { |
| 327 | return new Network[size]; |
| 328 | } |
| 329 | }; |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 330 | |
Robert Greenwalt | 2c7bf2c | 2014-06-23 16:07:43 -0700 | [diff] [blame] | 331 | @Override |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 332 | public boolean equals(Object obj) { |
| 333 | if (obj instanceof Network == false) return false; |
| 334 | Network other = (Network)obj; |
| 335 | return this.netId == other.netId; |
| 336 | } |
| 337 | |
Robert Greenwalt | 2c7bf2c | 2014-06-23 16:07:43 -0700 | [diff] [blame] | 338 | @Override |
Robert Greenwalt | 8595626 | 2014-05-18 20:29:39 -0700 | [diff] [blame] | 339 | public int hashCode() { |
| 340 | return netId * 11; |
| 341 | } |
Robert Greenwalt | 2c7bf2c | 2014-06-23 16:07:43 -0700 | [diff] [blame] | 342 | |
| 343 | @Override |
| 344 | public String toString() { |
| 345 | return Integer.toString(netId); |
| 346 | } |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 347 | } |