Network.java revision 2d6f265d00db883616989788595ead4f3af902a0
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 @Override 98 public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException { 99 Socket socket = createSocket(); 100 socket.bind(new InetSocketAddress(localHost, localPort)); 101 socket.connect(new InetSocketAddress(host, port)); 102 return socket; 103 } 104 105 @Override 106 public Socket createSocket(InetAddress address, int port, InetAddress localAddress, 107 int localPort) throws IOException { 108 Socket socket = createSocket(); 109 socket.bind(new InetSocketAddress(localAddress, localPort)); 110 socket.connect(new InetSocketAddress(address, port)); 111 return socket; 112 } 113 114 @Override 115 public Socket createSocket(InetAddress host, int port) throws IOException { 116 Socket socket = createSocket(); 117 socket.connect(new InetSocketAddress(host, port)); 118 return socket; 119 } 120 121 @Override 122 public Socket createSocket(String host, int port) throws IOException { 123 Socket socket = createSocket(); 124 socket.connect(new InetSocketAddress(host, port)); 125 return socket; 126 } 127 128 @Override 129 public Socket createSocket() throws IOException { 130 Socket socket = new Socket(); 131 // Query a property of the underlying socket to ensure the underlying 132 // socket exists so a file descriptor is available to bind to a network. 133 socket.getReuseAddress(); 134 NetworkUtils.bindSocketToNetwork(socket.getFileDescriptor$().getInt$(), mNetId); 135 return socket; 136 } 137 } 138 139 /** 140 * Returns a {@link SocketFactory} bound to this network. Any {@link Socket} created by 141 * this factory will have its traffic sent over this {@code Network}. Note that if this 142 * {@code Network} ever disconnects, this factory and any {@link Socket} it produced in the 143 * past or future will cease to work. 144 * 145 * @return a {@link SocketFactory} which produces {@link Socket} instances bound to this 146 * {@code Network}. 147 */ 148 public SocketFactory socketFactory() { 149 if (mNetworkBoundSocketFactory == null) { 150 mNetworkBoundSocketFactory = new NetworkBoundSocketFactory(netId); 151 } 152 return mNetworkBoundSocketFactory; 153 } 154 155 /** 156 * Binds the current process to this network. All sockets created in the future (and not 157 * explicitly bound via a bound {@link SocketFactory} (see {@link Network#socketFactory}) 158 * will be bound to this network. Note that if this {@code Network} ever disconnects 159 * all sockets created in this way will cease to work. This is by design so an application 160 * doesn't accidentally use sockets it thinks are still bound to a particular {@code Network}. 161 */ 162 public void bindProcess() { 163 NetworkUtils.bindProcessToNetwork(netId); 164 } 165 166 /** 167 * Binds host resolutions performed by this process to this network. {@link #bindProcess} 168 * takes precedence over this setting. 169 * 170 * @hide 171 * @deprecated This is strictly for legacy usage to support startUsingNetworkFeature(). 172 */ 173 public void bindProcessForHostResolution() { 174 NetworkUtils.bindProcessToNetworkForHostResolution(netId); 175 } 176 177 /** 178 * Clears any process specific {@link Network} binding for host resolution. This does 179 * not clear bindings enacted via {@link #bindProcess}. 180 * 181 * @hide 182 * @deprecated This is strictly for legacy usage to support startUsingNetworkFeature(). 183 */ 184 public void unbindProcessForHostResolution() { 185 NetworkUtils.unbindProcessToNetworkForHostResolution(); 186 } 187 188 /** 189 * A static utility method to return any {@code Network} currently bound by this process. 190 * 191 * @return {@code Network} to which this process is bound. 192 */ 193 public static Network getProcessBoundNetwork() { 194 return new Network(NetworkUtils.getNetworkBoundToProcess()); 195 } 196 197 /** 198 * Clear any process specific {@code Network} binding. This reverts a call to 199 * {@link Network#bindProcess}. 200 */ 201 public static void unbindProcess() { 202 NetworkUtils.unbindProcessToNetwork(); 203 } 204 205 // implement the Parcelable interface 206 public int describeContents() { 207 return 0; 208 } 209 public void writeToParcel(Parcel dest, int flags) { 210 dest.writeInt(netId); 211 } 212 213 public static final Creator<Network> CREATOR = 214 new Creator<Network>() { 215 public Network createFromParcel(Parcel in) { 216 int netId = in.readInt(); 217 218 return new Network(netId); 219 } 220 221 public Network[] newArray(int size) { 222 return new Network[size]; 223 } 224 }; 225 226 public boolean equals(Object obj) { 227 if (obj instanceof Network == false) return false; 228 Network other = (Network)obj; 229 return this.netId == other.netId; 230 } 231 232 public int hashCode() { 233 return netId * 11; 234 } 235} 236