Network.java revision 8ef3401d2ae613ff0f14d75be860f5c833019494
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 17package android.net; 18 19import android.net.NetworkUtils; 20import android.os.Parcelable; 21import android.os.Parcel; 22 23import java.io.IOException; 24import java.net.InetAddress; 25import java.net.InetSocketAddress; 26import java.net.Socket; 27import java.net.UnknownHostException; 28import javax.net.SocketFactory; 29 30/** 31 * Identifies a {@code Network}. This is supplied to applications via 32 * {@link ConnectivityManager.NetworkCallbackListener} in response to 33 * {@link ConnectivityManager#requestNetwork} or {@link ConnectivityManager#listenForNetwork}. 34 * It is used to direct traffic to the given {@code Network}, either on a {@link Socket} basis 35 * through a targeted {@link SocketFactory} or process-wide via {@link #bindProcess}. 36 */ 37public class Network implements Parcelable { 38 39 /** 40 * @hide 41 */ 42 public final int netId; 43 44 private NetworkBoundSocketFactory mNetworkBoundSocketFactory = null; 45 46 /** 47 * @hide 48 */ 49 public Network(int netId) { 50 this.netId = netId; 51 } 52 53 /** 54 * @hide 55 */ 56 public Network(Network that) { 57 this.netId = that.netId; 58 } 59 60 /** 61 * Operates the same as {@code InetAddress.getAllByName} except that host 62 * resolution is done on this network. 63 * 64 * @param host the hostname or literal IP string to be resolved. 65 * @return the array of addresses associated with the specified host. 66 * @throws UnknownHostException if the address lookup fails. 67 */ 68 public InetAddress[] getAllByName(String host) throws UnknownHostException { 69 return InetAddress.getAllByNameOnNet(host, netId); 70 } 71 72 /** 73 * Operates the same as {@code InetAddress.getByName} except that host 74 * resolution is done on this network. 75 * 76 * @param host 77 * the hostName to be resolved to an address or {@code null}. 78 * @return the {@code InetAddress} instance representing the host. 79 * @throws UnknownHostException 80 * if the address lookup fails. 81 */ 82 public InetAddress getByName(String host) throws UnknownHostException { 83 return InetAddress.getByNameOnNet(host, netId); 84 } 85 86 /** 87 * A {@code SocketFactory} that produces {@code Socket}'s bound to this network. 88 */ 89 private class NetworkBoundSocketFactory extends SocketFactory { 90 private final int mNetId; 91 92 public NetworkBoundSocketFactory(int netId) { 93 super(); 94 mNetId = netId; 95 } 96 97 private void connectToHost(Socket socket, String host, int port) throws IOException { 98 // Lookup addresses only on this Network. 99 InetAddress[] hostAddresses = getAllByName(host); 100 // Try all but last address ignoring exceptions. 101 for (int i = 0; i < hostAddresses.length - 1; i++) { 102 try { 103 socket.connect(new InetSocketAddress(hostAddresses[i], port)); 104 return; 105 } catch (IOException e) { 106 } 107 } 108 // Try last address. Do throw exceptions. 109 socket.connect(new InetSocketAddress(hostAddresses[hostAddresses.length - 1], port)); 110 } 111 112 @Override 113 public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException { 114 Socket socket = createSocket(); 115 socket.bind(new InetSocketAddress(localHost, localPort)); 116 connectToHost(socket, host, port); 117 return socket; 118 } 119 120 @Override 121 public Socket createSocket(InetAddress address, int port, InetAddress localAddress, 122 int localPort) throws IOException { 123 Socket socket = createSocket(); 124 socket.bind(new InetSocketAddress(localAddress, localPort)); 125 socket.connect(new InetSocketAddress(address, port)); 126 return socket; 127 } 128 129 @Override 130 public Socket createSocket(InetAddress host, int port) throws IOException { 131 Socket socket = createSocket(); 132 socket.connect(new InetSocketAddress(host, port)); 133 return socket; 134 } 135 136 @Override 137 public Socket createSocket(String host, int port) throws IOException { 138 Socket socket = createSocket(); 139 connectToHost(socket, host, port); 140 return socket; 141 } 142 143 @Override 144 public Socket createSocket() throws IOException { 145 Socket socket = new Socket(); 146 // Query a property of the underlying socket to ensure the underlying 147 // socket exists so a file descriptor is available to bind to a network. 148 socket.getReuseAddress(); 149 NetworkUtils.bindSocketToNetwork(socket.getFileDescriptor$().getInt$(), mNetId); 150 return socket; 151 } 152 } 153 154 /** 155 * Returns a {@link SocketFactory} bound to this network. Any {@link Socket} created by 156 * this factory will have its traffic sent over this {@code Network}. Note that if this 157 * {@code Network} ever disconnects, this factory and any {@link Socket} it produced in the 158 * past or future will cease to work. 159 * 160 * @return a {@link SocketFactory} which produces {@link Socket} instances bound to this 161 * {@code Network}. 162 */ 163 public SocketFactory socketFactory() { 164 if (mNetworkBoundSocketFactory == null) { 165 mNetworkBoundSocketFactory = new NetworkBoundSocketFactory(netId); 166 } 167 return mNetworkBoundSocketFactory; 168 } 169 170 /** 171 * Binds the current process to this network. All sockets created in the future (and not 172 * explicitly bound via a bound {@link SocketFactory} (see {@link Network#socketFactory}) 173 * will be bound to this network. Note that if this {@code Network} ever disconnects 174 * all sockets created in this way will cease to work. This is by design so an application 175 * doesn't accidentally use sockets it thinks are still bound to a particular {@code Network}. 176 */ 177 public void bindProcess() { 178 NetworkUtils.bindProcessToNetwork(netId); 179 } 180 181 /** 182 * Binds host resolutions performed by this process to this network. {@link #bindProcess} 183 * takes precedence over this setting. 184 * 185 * @hide 186 * @deprecated This is strictly for legacy usage to support startUsingNetworkFeature(). 187 */ 188 public void bindProcessForHostResolution() { 189 NetworkUtils.bindProcessToNetworkForHostResolution(netId); 190 } 191 192 /** 193 * Clears any process specific {@link Network} binding for host resolution. This does 194 * not clear bindings enacted via {@link #bindProcess}. 195 * 196 * @hide 197 * @deprecated This is strictly for legacy usage to support startUsingNetworkFeature(). 198 */ 199 public void unbindProcessForHostResolution() { 200 NetworkUtils.unbindProcessToNetworkForHostResolution(); 201 } 202 203 /** 204 * A static utility method to return any {@code Network} currently bound by this process. 205 * 206 * @return {@code Network} to which this process is bound. 207 */ 208 public static Network getProcessBoundNetwork() { 209 return new Network(NetworkUtils.getNetworkBoundToProcess()); 210 } 211 212 /** 213 * Clear any process specific {@code Network} binding. This reverts a call to 214 * {@link Network#bindProcess}. 215 */ 216 public static void unbindProcess() { 217 NetworkUtils.unbindProcessToNetwork(); 218 } 219 220 // implement the Parcelable interface 221 public int describeContents() { 222 return 0; 223 } 224 public void writeToParcel(Parcel dest, int flags) { 225 dest.writeInt(netId); 226 } 227 228 public static final Creator<Network> CREATOR = 229 new Creator<Network>() { 230 public Network createFromParcel(Parcel in) { 231 int netId = in.readInt(); 232 233 return new Network(netId); 234 } 235 236 public Network[] newArray(int size) { 237 return new Network[size]; 238 } 239 }; 240 241 public boolean equals(Object obj) { 242 if (obj instanceof Network == false) return false; 243 Network other = (Network)obj; 244 return this.netId == other.netId; 245 } 246 247 public int hashCode() { 248 return netId * 11; 249 } 250} 251