1 /* 2 * Copyright (C) 2008 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 19 import java.io.FileWriter; 20 import java.io.IOException; 21 22 import android.os.Handler; 23 import android.os.Message; 24 import android.os.SystemProperties; 25 import android.content.Context; 26 import android.text.TextUtils; 27 import android.util.Config; 28 import android.util.Log; 29 30 31 /** 32 * Each subclass of this class keeps track of the state of connectivity 33 * of a network interface. All state information for a network should 34 * be kept in a Tracker class. This superclass manages the 35 * network-type-independent aspects of network state. 36 * 37 * {@hide} 38 */ 39 public abstract class NetworkStateTracker extends Handler { 40 41 protected NetworkInfo mNetworkInfo; 42 protected Context mContext; 43 protected Handler mTarget; 44 protected String mInterfaceName; 45 protected String[] mDnsPropNames; 46 private boolean mPrivateDnsRouteSet; 47 protected int mDefaultGatewayAddr; 48 private boolean mTeardownRequested; 49 50 private static boolean DBG = false; 51 private static final String TAG = "NetworkStateTracker"; 52 53 // Share the event space with ConnectivityService (which we can't see, but 54 // must send events to). If you change these, change ConnectivityService 55 // too. 56 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1; 57 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100; 58 59 public static final int EVENT_STATE_CHANGED = 1; 60 public static final int EVENT_SCAN_RESULTS_AVAILABLE = 2; 61 /** 62 * arg1: 1 to show, 0 to hide 63 * arg2: ID of the notification 64 * obj: Notification (if showing) 65 */ 66 public static final int EVENT_NOTIFICATION_CHANGED = 3; 67 public static final int EVENT_CONFIGURATION_CHANGED = 4; 68 public static final int EVENT_ROAMING_CHANGED = 5; 69 public static final int EVENT_NETWORK_SUBTYPE_CHANGED = 6; 70 71 public NetworkStateTracker(Context context, 72 Handler target, 73 int networkType, 74 int subType, 75 String typeName, 76 String subtypeName) { 77 super(); 78 mContext = context; 79 mTarget = target; 80 mTeardownRequested = false; 81 82 this.mNetworkInfo = new NetworkInfo(networkType, subType, typeName, subtypeName); 83 } 84 85 public NetworkInfo getNetworkInfo() { 86 return mNetworkInfo; 87 } 88 89 /** 90 * Return the system properties name associated with the tcp buffer sizes 91 * for this network. 92 */ 93 public abstract String getTcpBufferSizesPropName(); 94 95 /** 96 * Return the IP addresses of the DNS servers available for the mobile data 97 * network interface. 98 * @return a list of DNS addresses, with no holes. 99 */ 100 public String[] getNameServers() { 101 return getNameServerList(mDnsPropNames); 102 } 103 104 /** 105 * Return the IP addresses of the DNS servers available for this 106 * network interface. 107 * @param propertyNames the names of the system properties whose values 108 * give the IP addresses. Properties with no values are skipped. 109 * @return an array of {@code String}s containing the IP addresses 110 * of the DNS servers, in dot-notation. This may have fewer 111 * non-null entries than the list of names passed in, since 112 * some of the passed-in names may have empty values. 113 */ 114 static protected String[] getNameServerList(String[] propertyNames) { 115 String[] dnsAddresses = new String[propertyNames.length]; 116 int i, j; 117 118 for (i = 0, j = 0; i < propertyNames.length; i++) { 119 String value = SystemProperties.get(propertyNames[i]); 120 // The GSM layer sometimes sets a bogus DNS server address of 121 // 0.0.0.0 122 if (!TextUtils.isEmpty(value) && !TextUtils.equals(value, "0.0.0.0")) { 123 dnsAddresses[j++] = value; 124 } 125 } 126 return dnsAddresses; 127 } 128 129 public void addPrivateDnsRoutes() { 130 if (DBG) { 131 Log.d(TAG, "addPrivateDnsRoutes for " + this + 132 "(" + mInterfaceName + ") - mPrivateDnsRouteSet = "+mPrivateDnsRouteSet); 133 } 134 if (mInterfaceName != null && !mPrivateDnsRouteSet) { 135 for (String addrString : getNameServers()) { 136 int addr = NetworkUtils.lookupHost(addrString); 137 if (addr != -1 && addr != 0) { 138 if (DBG) Log.d(TAG, " adding "+addrString+" ("+addr+")"); 139 NetworkUtils.addHostRoute(mInterfaceName, addr); 140 } 141 } 142 mPrivateDnsRouteSet = true; 143 } 144 } 145 146 public void removePrivateDnsRoutes() { 147 // TODO - we should do this explicitly but the NetUtils api doesnt 148 // support this yet - must remove all. No worse than before 149 if (mInterfaceName != null && mPrivateDnsRouteSet) { 150 if (DBG) { 151 Log.d(TAG, "removePrivateDnsRoutes for " + mNetworkInfo.getTypeName() + 152 " (" + mInterfaceName + ")"); 153 } 154 NetworkUtils.removeHostRoutes(mInterfaceName); 155 mPrivateDnsRouteSet = false; 156 } 157 } 158 159 public void addDefaultRoute() { 160 if ((mInterfaceName != null) && (mDefaultGatewayAddr != 0)) { 161 if (DBG) { 162 Log.d(TAG, "addDefaultRoute for " + mNetworkInfo.getTypeName() + 163 " (" + mInterfaceName + "), GatewayAddr=" + mDefaultGatewayAddr); 164 } 165 NetworkUtils.setDefaultRoute(mInterfaceName, mDefaultGatewayAddr); 166 } 167 } 168 169 public void removeDefaultRoute() { 170 if (mInterfaceName != null) { 171 if (DBG) { 172 Log.d(TAG, "removeDefaultRoute for " + mNetworkInfo.getTypeName() + " (" + 173 mInterfaceName + ")"); 174 } 175 NetworkUtils.removeDefaultRoute(mInterfaceName); 176 } 177 } 178 179 /** 180 * Reads the network specific TCP buffer sizes from SystemProperties 181 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system 182 * wide use 183 */ 184 public void updateNetworkSettings() { 185 String key = getTcpBufferSizesPropName(); 186 String bufferSizes = SystemProperties.get(key); 187 188 if (bufferSizes.length() == 0) { 189 Log.w(TAG, key + " not found in system properties. Using defaults"); 190 191 // Setting to default values so we won't be stuck to previous values 192 key = "net.tcp.buffersize.default"; 193 bufferSizes = SystemProperties.get(key); 194 } 195 196 // Set values in kernel 197 if (bufferSizes.length() != 0) { 198 if (DBG) { 199 Log.v(TAG, "Setting TCP values: [" + bufferSizes 200 + "] which comes from [" + key + "]"); 201 } 202 setBufferSize(bufferSizes); 203 } 204 } 205 206 /** 207 * Release the wakelock, if any, that may be held while handling a 208 * disconnect operation. 209 */ 210 public void releaseWakeLock() { 211 } 212 213 /** 214 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max] 215 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem 216 * 217 * @param bufferSizes in the format of "readMin, readInitial, readMax, 218 * writeMin, writeInitial, writeMax" 219 */ 220 private void setBufferSize(String bufferSizes) { 221 try { 222 String[] values = bufferSizes.split(","); 223 224 if (values.length == 6) { 225 final String prefix = "/sys/kernel/ipv4/tcp_"; 226 stringToFile(prefix + "rmem_min", values[0]); 227 stringToFile(prefix + "rmem_def", values[1]); 228 stringToFile(prefix + "rmem_max", values[2]); 229 stringToFile(prefix + "wmem_min", values[3]); 230 stringToFile(prefix + "wmem_def", values[4]); 231 stringToFile(prefix + "wmem_max", values[5]); 232 } else { 233 Log.w(TAG, "Invalid buffersize string: " + bufferSizes); 234 } 235 } catch (IOException e) { 236 Log.w(TAG, "Can't set tcp buffer sizes:" + e); 237 } 238 } 239 240 /** 241 * Writes string to file. Basically same as "echo -n $string > $filename" 242 * 243 * @param filename 244 * @param string 245 * @throws IOException 246 */ 247 private void stringToFile(String filename, String string) throws IOException { 248 FileWriter out = new FileWriter(filename); 249 try { 250 out.write(string); 251 } finally { 252 out.close(); 253 } 254 } 255 256 /** 257 * Record the detailed state of a network, and if it is a 258 * change from the previous state, send a notification to 259 * any listeners. 260 * @param state the new @{code DetailedState} 261 */ 262 public void setDetailedState(NetworkInfo.DetailedState state) { 263 setDetailedState(state, null, null); 264 } 265 266 /** 267 * Record the detailed state of a network, and if it is a 268 * change from the previous state, send a notification to 269 * any listeners. 270 * @param state the new @{code DetailedState} 271 * @param reason a {@code String} indicating a reason for the state change, 272 * if one was supplied. May be {@code null}. 273 * @param extraInfo optional {@code String} providing extra information about the state change 274 */ 275 public void setDetailedState(NetworkInfo.DetailedState state, String reason, String extraInfo) { 276 if (DBG) Log.d(TAG, "setDetailed state, old ="+mNetworkInfo.getDetailedState()+" and new state="+state); 277 if (state != mNetworkInfo.getDetailedState()) { 278 boolean wasConnecting = (mNetworkInfo.getState() == NetworkInfo.State.CONNECTING); 279 String lastReason = mNetworkInfo.getReason(); 280 /* 281 * If a reason was supplied when the CONNECTING state was entered, and no 282 * reason was supplied for entering the CONNECTED state, then retain the 283 * reason that was supplied when going to CONNECTING. 284 */ 285 if (wasConnecting && state == NetworkInfo.DetailedState.CONNECTED && reason == null 286 && lastReason != null) 287 reason = lastReason; 288 mNetworkInfo.setDetailedState(state, reason, extraInfo); 289 Message msg = mTarget.obtainMessage(EVENT_STATE_CHANGED, mNetworkInfo); 290 msg.sendToTarget(); 291 } 292 } 293 294 protected void setDetailedStateInternal(NetworkInfo.DetailedState state) { 295 mNetworkInfo.setDetailedState(state, null, null); 296 } 297 298 public void setTeardownRequested(boolean isRequested) { 299 mTeardownRequested = isRequested; 300 } 301 302 public boolean isTeardownRequested() { 303 return mTeardownRequested; 304 } 305 306 /** 307 * Send a notification that the results of a scan for network access 308 * points has completed, and results are available. 309 */ 310 protected void sendScanResultsAvailable() { 311 Message msg = mTarget.obtainMessage(EVENT_SCAN_RESULTS_AVAILABLE, mNetworkInfo); 312 msg.sendToTarget(); 313 } 314 315 /** 316 * Record the roaming status of the device, and if it is a change from the previous 317 * status, send a notification to any listeners. 318 * @param isRoaming {@code true} if the device is now roaming, {@code false} 319 * if it is no longer roaming. 320 */ 321 protected void setRoamingStatus(boolean isRoaming) { 322 if (isRoaming != mNetworkInfo.isRoaming()) { 323 mNetworkInfo.setRoaming(isRoaming); 324 Message msg = mTarget.obtainMessage(EVENT_ROAMING_CHANGED, mNetworkInfo); 325 msg.sendToTarget(); 326 } 327 } 328 329 protected void setSubtype(int subtype, String subtypeName) { 330 if (mNetworkInfo.isConnected()) { 331 int oldSubtype = mNetworkInfo.getSubtype(); 332 if (subtype != oldSubtype) { 333 mNetworkInfo.setSubtype(subtype, subtypeName); 334 Message msg = mTarget.obtainMessage( 335 EVENT_NETWORK_SUBTYPE_CHANGED, oldSubtype, 0, mNetworkInfo); 336 msg.sendToTarget(); 337 } 338 } 339 } 340 341 public abstract void startMonitoring(); 342 343 /** 344 * Disable connectivity to a network 345 * @return {@code true} if a teardown occurred, {@code false} if the 346 * teardown did not occur. 347 */ 348 public abstract boolean teardown(); 349 350 /** 351 * Reenable connectivity to a network after a {@link #teardown()}. 352 */ 353 public abstract boolean reconnect(); 354 355 /** 356 * Turn the wireless radio off for a network. 357 * @param turnOn {@code true} to turn the radio on, {@code false} 358 */ 359 public abstract boolean setRadio(boolean turnOn); 360 361 /** 362 * Returns an indication of whether this network is available for 363 * connections. A value of {@code false} means that some quasi-permanent 364 * condition prevents connectivity to this network. 365 */ 366 public abstract boolean isAvailable(); 367 368 /** 369 * Tells the underlying networking system that the caller wants to 370 * begin using the named feature. The interpretation of {@code feature} 371 * is completely up to each networking implementation. 372 * @param feature the name of the feature to be used 373 * @param callingPid the process ID of the process that is issuing this request 374 * @param callingUid the user ID of the process that is issuing this request 375 * @return an integer value representing the outcome of the request. 376 * The interpretation of this value is specific to each networking 377 * implementation+feature combination, except that the value {@code -1} 378 * always indicates failure. 379 */ 380 public abstract int startUsingNetworkFeature(String feature, int callingPid, int callingUid); 381 382 /** 383 * Tells the underlying networking system that the caller is finished 384 * using the named feature. The interpretation of {@code feature} 385 * is completely up to each networking implementation. 386 * @param feature the name of the feature that is no longer needed. 387 * @param callingPid the process ID of the process that is issuing this request 388 * @param callingUid the user ID of the process that is issuing this request 389 * @return an integer value representing the outcome of the request. 390 * The interpretation of this value is specific to each networking 391 * implementation+feature combination, except that the value {@code -1} 392 * always indicates failure. 393 */ 394 public abstract int stopUsingNetworkFeature(String feature, int callingPid, int callingUid); 395 396 /** 397 * Ensure that a network route exists to deliver traffic to the specified 398 * host via this network interface. 399 * @param hostAddress the IP address of the host to which the route is desired 400 * @return {@code true} on success, {@code false} on failure 401 */ 402 public boolean requestRouteToHost(int hostAddress) { 403 return false; 404 } 405 406 /** 407 * Interprets scan results. This will be called at a safe time for 408 * processing, and from a safe thread. 409 */ 410 public void interpretScanResultsAvailable() { 411 } 412 413 } 414