blob: bfca851dcacae120035ecd4e3ee63276b34d899d [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
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
17package com.android.server;
18
Jeff Sharkey4434b0b2011-06-16 13:04:20 -070019import static android.Manifest.permission.MANAGE_NETWORK_POLICY;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070020import static android.net.ConnectivityManager.isNetworkTypeValid;
21import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL;
Jeff Sharkeya47d7a12011-06-16 15:07:48 -070022import static android.net.NetworkPolicyManager.RULE_REJECT_METERED;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070023
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080024import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080025import android.content.ContentResolver;
26import android.content.Context;
27import android.content.Intent;
28import android.content.pm.PackageManager;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070029import android.database.ContentObserver;
The Android Open Source Project28527d22009-03-03 19:31:44 -080030import android.net.ConnectivityManager;
Robert Greenwalteb123ac2010-12-06 13:56:24 -080031import android.net.DummyDataStateTracker;
Benoit Goby211b5692010-12-22 14:29:40 -080032import android.net.EthernetDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080033import android.net.IConnectivityManager;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070034import android.net.INetworkPolicyListener;
35import android.net.INetworkPolicyManager;
Wink Savilledc5d1ba2011-07-14 12:23:28 -070036import android.net.LinkAddress;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080037import android.net.LinkProperties;
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -070038import android.net.LinkProperties.CompareResult;
The Android Open Source Project28527d22009-03-03 19:31:44 -080039import android.net.MobileDataStateTracker;
Robert Greenwalt34848c02011-03-25 13:09:25 -070040import android.net.NetworkConfig;
The Android Open Source Project28527d22009-03-03 19:31:44 -080041import android.net.NetworkInfo;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070042import android.net.NetworkInfo.DetailedState;
Jeff Sharkey66fa9682011-08-02 17:22:34 -070043import android.net.NetworkQuotaInfo;
Jeff Sharkey21062e72011-05-28 20:56:34 -070044import android.net.NetworkState;
The Android Open Source Project28527d22009-03-03 19:31:44 -080045import android.net.NetworkStateTracker;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070046import android.net.NetworkUtils;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070047import android.net.Proxy;
48import android.net.ProxyProperties;
Robert Greenwalt5a901292011-04-28 14:28:50 -070049import android.net.RouteInfo;
The Android Open Source Project28527d22009-03-03 19:31:44 -080050import android.net.wifi.WifiStateTracker;
51import android.os.Binder;
Mike Lockwood0d5916c2011-05-28 13:24:04 -040052import android.os.FileUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080053import android.os.Handler;
Wink Saville775aad62010-09-02 19:23:52 -070054import android.os.HandlerThread;
Robert Greenwalt2034b912009-08-12 16:08:25 -070055import android.os.IBinder;
Chia-chi Yeh4df51322011-05-11 16:35:13 -070056import android.os.INetworkManagementService;
The Android Open Source Project28527d22009-03-03 19:31:44 -080057import android.os.Looper;
58import android.os.Message;
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -070059import android.os.ParcelFileDescriptor;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070060import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070061import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080062import android.os.ServiceManager;
63import android.os.SystemProperties;
64import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070065import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080066import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080067import android.util.Slog;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070068import android.util.SparseIntArray;
The Android Open Source Project28527d22009-03-03 19:31:44 -080069
Chia-chi Yehbded3eb2011-07-04 03:23:12 -070070import com.android.internal.net.LegacyVpnInfo;
Chia-chi Yeh75cacd52011-06-15 17:07:27 -070071import com.android.internal.net.VpnConfig;
Robert Greenwalt2034b912009-08-12 16:08:25 -070072import com.android.internal.telephony.Phone;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080073import com.android.server.connectivity.Tethering;
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -070074import com.android.server.connectivity.Vpn;
Jeff Sharkey21062e72011-05-28 20:56:34 -070075import com.google.android.collect.Lists;
Jeff Sharkeya47d7a12011-06-16 15:07:48 -070076import com.google.android.collect.Sets;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080077
The Android Open Source Project28527d22009-03-03 19:31:44 -080078import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070079import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080080import java.io.PrintWriter;
Wink Savilledc5d1ba2011-07-14 12:23:28 -070081import java.net.Inet4Address;
Wink Saville051a6642011-07-13 13:44:13 -070082import java.net.Inet6Address;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070083import java.net.InetAddress;
84import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070085import java.util.ArrayList;
Jeff Sharkeya47d7a12011-06-16 15:07:48 -070086import java.util.Arrays;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070087import java.util.Collection;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070088import java.util.GregorianCalendar;
Jeff Sharkeya47d7a12011-06-16 15:07:48 -070089import java.util.HashSet;
Robert Greenwalt2034b912009-08-12 16:08:25 -070090import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080091
92/**
93 * @hide
94 */
95public class ConnectivityService extends IConnectivityManager.Stub {
96
Robert Greenwalt063dc7d2010-10-05 19:12:26 -070097 private static final boolean DBG = true;
Wink Saville051a6642011-07-13 13:44:13 -070098 private static final boolean VDBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080099 private static final String TAG = "ConnectivityService";
100
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700101 private static final boolean LOGD_RULES = false;
102
Robert Greenwalt2034b912009-08-12 16:08:25 -0700103 // how long to wait before switching back to a radio's default network
104 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
105 // system property that can override the above value
106 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
107 "android.telephony.apn-restore";
108
Robert Greenwaltbd492212011-05-06 17:10:53 -0700109 // used in recursive route setting to add gateways for the host for which
110 // a host route was requested.
111 private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10;
112
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800113 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800114 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800115
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -0700116 private Vpn mVpn;
117
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700118 /** Lock around {@link #mUidRules} and {@link #mMeteredIfaces}. */
119 private Object mRulesLock = new Object();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700120 /** Currently active network rules by UID. */
121 private SparseIntArray mUidRules = new SparseIntArray();
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700122 /** Set of ifaces that are costly. */
123 private HashSet<String> mMeteredIfaces = Sets.newHashSet();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700124
The Android Open Source Project28527d22009-03-03 19:31:44 -0800125 /**
126 * Sometimes we want to refer to the individual network state
127 * trackers separately, and sometimes we just want to treat them
128 * abstractly.
129 */
130 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -0700131
132 /**
Wink Saville051a6642011-07-13 13:44:13 -0700133 * The link properties that define the current links
134 */
135 private LinkProperties mCurrentLinkProperties[];
136
137 /**
Robert Greenwalt2034b912009-08-12 16:08:25 -0700138 * A per Net list of the PID's that requested access to the net
139 * used both as a refcount and for per-PID DNS selection
140 */
141 private List mNetRequestersPids[];
142
Robert Greenwalt2034b912009-08-12 16:08:25 -0700143 // priority order of the nettrackers
144 // (excluding dynamically set mNetworkPreference)
145 // TODO - move mNetworkTypePreference into this
146 private int[] mPriorityList;
147
The Android Open Source Project28527d22009-03-03 19:31:44 -0800148 private Context mContext;
149 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700150 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -0700151 // 0 is full bad, 100 is full good
152 private int mDefaultInetCondition = 0;
153 private int mDefaultInetConditionPublished = 0;
154 private boolean mInetConditionChangeInFlight = false;
155 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800156
Chia-chi Yehcc844502011-07-14 18:01:57 -0700157 private Object mDnsLock = new Object();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800158 private int mNumDnsEntries;
Chia-chi Yehcc844502011-07-14 18:01:57 -0700159 private boolean mDnsOverridden = false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800160
161 private boolean mTestMode;
Joe Onorato56023ad2010-09-01 21:18:22 -0700162 private static ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800163
Robert Greenwalt355205c2011-05-10 15:05:02 -0700164 private INetworkManagementService mNetd;
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700165 private INetworkPolicyManager mPolicyManager;
Robert Greenwalt355205c2011-05-10 15:05:02 -0700166
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700167 private static final int ENABLED = 1;
168 private static final int DISABLED = 0;
169
170 // Share the event space with NetworkStateTracker (which can't see this
171 // internal class but sends us events). If you change these, change
172 // NetworkStateTracker.java too.
173 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
174 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
175
176 /**
177 * used internally as a delayed event to make us switch back to the
178 * default network
179 */
180 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
181 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
182
183 /**
184 * used internally to change our mobile data enabled flag
185 */
186 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
187 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
188
189 /**
190 * used internally to change our network preference setting
191 * arg1 = networkType to prefer
192 */
193 private static final int EVENT_SET_NETWORK_PREFERENCE =
194 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
195
196 /**
197 * used internally to synchronize inet condition reports
198 * arg1 = networkType
199 * arg2 = condition (0 bad, 100 good)
200 */
201 private static final int EVENT_INET_CONDITION_CHANGE =
202 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
203
204 /**
205 * used internally to mark the end of inet condition hold periods
206 * arg1 = networkType
207 */
208 private static final int EVENT_INET_CONDITION_HOLD_END =
209 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
210
211 /**
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700212 * used internally to set enable/disable cellular data
213 * arg1 = ENBALED or DISABLED
214 */
215 private static final int EVENT_SET_MOBILE_DATA =
216 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
217
Robert Greenwaltccb36f92010-09-24 14:32:21 -0700218 /**
219 * used internally to clear a wakelock when transitioning
220 * from one net to another
221 */
222 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
223 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
224
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700225 /**
226 * used internally to reload global proxy settings
227 */
228 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
229 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
230
Robert Greenwalt34848c02011-03-25 13:09:25 -0700231 /**
232 * used internally to set external dependency met/unmet
233 * arg1 = ENABLED (met) or DISABLED (unmet)
234 * arg2 = NetworkType
235 */
236 private static final int EVENT_SET_DEPENDENCY_MET =
237 MAX_NETWORK_STATE_TRACKER_EVENT + 10;
238
Chia-chi Yehcc844502011-07-14 18:01:57 -0700239 /**
240 * used internally to restore DNS properties back to the
241 * default network
242 */
243 private static final int EVENT_RESTORE_DNS =
244 MAX_NETWORK_STATE_TRACKER_EVENT + 11;
245
Wink Saville7e4333c2011-08-05 11:40:22 -0700246 /**
Wink Saville4f0de1e2011-08-04 15:01:58 -0700247 * used internally to send a sticky broadcast delayed.
248 */
249 private static final int EVENT_SEND_STICKY_BROADCAST_INTENT =
Wink Saville7e4333c2011-08-05 11:40:22 -0700250 MAX_NETWORK_STATE_TRACKER_EVENT + 12;
Wink Saville4f0de1e2011-08-04 15:01:58 -0700251
Jeff Sharkey805662d2011-08-19 02:24:24 -0700252 /**
253 * Used internally to
254 * {@link NetworkStateTracker#setPolicyDataEnable(boolean)}.
255 */
256 private static final int EVENT_SET_POLICY_DATA_ENABLE = MAX_NETWORK_STATE_TRACKER_EVENT + 13;
257
Robert Greenwalt2034b912009-08-12 16:08:25 -0700258 private Handler mHandler;
259
260 // list of DeathRecipients used to make sure features are turned off when
261 // a process dies
Kazuhiro Ondocae18f12011-07-19 11:23:37 -0500262 private List<FeatureUser> mFeatureUsers;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700263
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400264 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800265 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400266
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700267 private PowerManager.WakeLock mNetTransitionWakeLock;
268 private String mNetTransitionWakeLockCausedBy = "";
269 private int mNetTransitionWakeLockSerialNumber;
270 private int mNetTransitionWakeLockTimeout;
271
Robert Greenwalt94daa182010-09-01 11:34:05 -0700272 private InetAddress mDefaultDns;
273
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -0700274 // this collection is used to refcount the added routes - if there are none left
275 // it's time to remove the route from the route table
276 private Collection<RouteInfo> mAddedRoutes = new ArrayList<RouteInfo>();
277
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700278 // used in DBG mode to track inet condition reports
279 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
280 private ArrayList mInetLog;
281
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700282 // track the current default http proxy - tell the world if we get a new one (real change)
283 private ProxyProperties mDefaultProxy = null;
284 // track the global proxy.
285 private ProxyProperties mGlobalProxy = null;
286 private final Object mGlobalProxyLock = new Object();
287
288 private SettingsObserver mSettingsObserver;
289
Robert Greenwalt34848c02011-03-25 13:09:25 -0700290 NetworkConfig[] mNetConfigs;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700291 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700292
Robert Greenwalt12c44552009-12-07 11:33:18 -0800293 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700294 public int mSimultaneity;
295 public int mType;
296 public RadioAttributes(String init) {
297 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700298 mType = Integer.parseInt(fragments[0]);
299 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700300 }
301 }
302 RadioAttributes[] mRadioAttributes;
303
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700304 // the set of network types that can only be enabled by system/sig apps
305 List mProtectedNetworks;
306
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700307 public ConnectivityService(
308 Context context, INetworkManagementService netd, INetworkPolicyManager policyManager) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800309 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800310
Wink Saville775aad62010-09-02 19:23:52 -0700311 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
312 handlerThread.start();
313 mHandler = new MyHandler(handlerThread.getLooper());
314
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800315 // setup our unique device name
Robert Greenwalt82cde132010-12-06 09:30:17 -0800316 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
317 String id = Settings.Secure.getString(context.getContentResolver(),
318 Settings.Secure.ANDROID_ID);
319 if (id != null && id.length() > 0) {
320 String name = new String("android_").concat(id);
321 SystemProperties.set("net.hostname", name);
322 }
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800323 }
324
Robert Greenwalt94daa182010-09-01 11:34:05 -0700325 // read our default dns server ip
326 String dns = Settings.Secure.getString(context.getContentResolver(),
327 Settings.Secure.DEFAULT_DNS_SERVER);
328 if (dns == null || dns.length() == 0) {
329 dns = context.getResources().getString(
330 com.android.internal.R.string.config_default_dns_server);
331 }
332 try {
Robert Greenwalt35e34d12011-02-22 16:00:42 -0800333 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
334 } catch (IllegalArgumentException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800335 loge("Error setting defaultDns using " + dns);
Robert Greenwalt94daa182010-09-01 11:34:05 -0700336 }
337
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700338 mContext = checkNotNull(context, "missing Context");
339 mNetd = checkNotNull(netd, "missing INetworkManagementService");
340 mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager");
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700341
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700342 try {
343 mPolicyManager.registerListener(mPolicyListener);
344 } catch (RemoteException e) {
345 // ouch, no rules updates means some processes may never get network
Robert Greenwalt78f28112011-08-02 17:18:41 -0700346 loge("unable to register INetworkPolicyListener" + e.toString());
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700347 }
348
349 final PowerManager powerManager = (PowerManager) context.getSystemService(
350 Context.POWER_SERVICE);
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700351 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
352 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
353 com.android.internal.R.integer.config_networkTransitionTimeout);
354
Robert Greenwalt2034b912009-08-12 16:08:25 -0700355 mNetTrackers = new NetworkStateTracker[
356 ConnectivityManager.MAX_NETWORK_TYPE+1];
Wink Saville051a6642011-07-13 13:44:13 -0700357 mCurrentLinkProperties = new LinkProperties[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt0659da32009-07-16 17:21:39 -0700358
The Android Open Source Project28527d22009-03-03 19:31:44 -0800359 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700360
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700361 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwalt34848c02011-03-25 13:09:25 -0700362 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700363
Robert Greenwalt2034b912009-08-12 16:08:25 -0700364 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700365 String[] raStrings = context.getResources().getStringArray(
366 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700367 for (String raString : raStrings) {
368 RadioAttributes r = new RadioAttributes(raString);
369 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800370 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700371 continue;
372 }
373 if (mRadioAttributes[r.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800374 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700375 r.mType);
376 continue;
377 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700378 mRadioAttributes[r.mType] = r;
379 }
380
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700381 String[] naStrings = context.getResources().getStringArray(
382 com.android.internal.R.array.networkAttributes);
383 for (String naString : naStrings) {
384 try {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700385 NetworkConfig n = new NetworkConfig(naString);
Wink Savillef2a62832011-04-07 14:23:45 -0700386 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800387 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700388 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700389 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700390 }
Wink Savillef2a62832011-04-07 14:23:45 -0700391 if (mNetConfigs[n.type] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800392 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700393 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700394 continue;
395 }
Wink Savillef2a62832011-04-07 14:23:45 -0700396 if (mRadioAttributes[n.radio] == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800397 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Savillef2a62832011-04-07 14:23:45 -0700398 "radio " + n.radio + " in network type " + n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700399 continue;
400 }
Wink Savillef2a62832011-04-07 14:23:45 -0700401 mNetConfigs[n.type] = n;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700402 mNetworksDefined++;
403 } catch(Exception e) {
404 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700405 }
406 }
407
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700408 mProtectedNetworks = new ArrayList<Integer>();
409 int[] protectedNetworks = context.getResources().getIntArray(
410 com.android.internal.R.array.config_protectedNetworks);
411 for (int p : protectedNetworks) {
412 if ((mNetConfigs[p] != null) && (mProtectedNetworks.contains(p) == false)) {
413 mProtectedNetworks.add(p);
414 } else {
415 if (DBG) loge("Ignoring protectedNetwork " + p);
416 }
417 }
418
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700419 // high priority first
420 mPriorityList = new int[mNetworksDefined];
421 {
422 int insertionPoint = mNetworksDefined-1;
423 int currentLowest = 0;
424 int nextLowest = 0;
425 while (insertionPoint > -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700426 for (NetworkConfig na : mNetConfigs) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700427 if (na == null) continue;
Wink Savillef2a62832011-04-07 14:23:45 -0700428 if (na.priority < currentLowest) continue;
429 if (na.priority > currentLowest) {
430 if (na.priority < nextLowest || nextLowest == 0) {
431 nextLowest = na.priority;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700432 }
433 continue;
434 }
Wink Savillef2a62832011-04-07 14:23:45 -0700435 mPriorityList[insertionPoint--] = na.type;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700436 }
437 currentLowest = nextLowest;
438 nextLowest = 0;
439 }
440 }
441
442 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
443 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700444 mNetRequestersPids[i] = new ArrayList();
445 }
446
Kazuhiro Ondocae18f12011-07-19 11:23:37 -0500447 mFeatureUsers = new ArrayList<FeatureUser>();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700448
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700449 mNumDnsEntries = 0;
450
451 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
452 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800453 /*
454 * Create the network state trackers for Wi-Fi and mobile
455 * data. Maybe this could be done with a factory class,
456 * but it's not clear that it's worth it, given that
457 * the number of different network types is not going
458 * to change very often.
459 */
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700460 for (int netType : mPriorityList) {
Wink Savillef2a62832011-04-07 14:23:45 -0700461 switch (mNetConfigs[netType].radio) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700462 case ConnectivityManager.TYPE_WIFI:
repo syncf5de5572011-07-29 23:55:49 -0700463 mNetTrackers[netType] = new WifiStateTracker(netType,
464 mNetConfigs[netType].name);
465 mNetTrackers[netType].startMonitoring(context, mHandler);
466 break;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700467 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700468 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700469 mNetConfigs[netType].name);
Wink Saville7fabfa22010-08-13 16:11:42 -0700470 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700471 break;
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800472 case ConnectivityManager.TYPE_DUMMY:
473 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700474 mNetConfigs[netType].name);
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800475 mNetTrackers[netType].startMonitoring(context, mHandler);
476 break;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800477 case ConnectivityManager.TYPE_BLUETOOTH:
478 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
479 mNetTrackers[netType].startMonitoring(context, mHandler);
480 break;
Benoit Goby211b5692010-12-22 14:29:40 -0800481 case ConnectivityManager.TYPE_ETHERNET:
482 mNetTrackers[netType] = EthernetDataTracker.getInstance();
483 mNetTrackers[netType].startMonitoring(context, mHandler);
484 break;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700485 default:
Wink Savillee70c6f52010-12-03 12:01:38 -0800486 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700487 mNetConfigs[netType].radio);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700488 continue;
489 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -0700490 mCurrentLinkProperties[netType] = null;
Robert Greenwalt497ff5e72011-07-15 11:16:45 -0700491 if (mNetConfigs[netType].isDefault()) mNetTrackers[netType].reconnect();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700492 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800493
Chia-chi Yeh4df51322011-05-11 16:35:13 -0700494 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
495 INetworkManagementService nmService = INetworkManagementService.Stub.asInterface(b);
496
497 mTethering = new Tethering(mContext, nmService, mHandler.getLooper());
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700498 mTetheringConfigValid = ((mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700499 mTethering.getTetherableWifiRegexs().length != 0 ||
500 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700501 mTethering.getUpstreamIfaceTypes().length != 0);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800502
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -0700503 mVpn = new Vpn(mContext, new VpnCallback());
504
Chia-chi Yehf3204aa2011-05-23 15:08:29 -0700505 try {
506 nmService.registerObserver(mTethering);
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -0700507 nmService.registerObserver(mVpn);
Chia-chi Yehf3204aa2011-05-23 15:08:29 -0700508 } catch (RemoteException e) {
509 loge("Error registering observer :" + e);
510 }
511
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700512 if (DBG) {
513 mInetLog = new ArrayList();
514 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700515
516 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
517 mSettingsObserver.observe(mContext);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800518
519 loadGlobalProxy();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800520 }
521
522 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700523 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800524 * @param preference the new preference
525 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700526 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800527 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700528
529 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800530 }
531
532 public int getNetworkPreference() {
533 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700534 int preference;
535 synchronized(this) {
536 preference = mNetworkPreference;
537 }
538 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800539 }
540
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700541 private void handleSetNetworkPreference(int preference) {
542 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt34848c02011-03-25 13:09:25 -0700543 mNetConfigs[preference] != null &&
544 mNetConfigs[preference].isDefault()) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700545 if (mNetworkPreference != preference) {
546 final ContentResolver cr = mContext.getContentResolver();
547 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
548 synchronized(this) {
549 mNetworkPreference = preference;
550 }
551 enforcePreference();
552 }
553 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800554 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700555
Wink Saville4f0de1e2011-08-04 15:01:58 -0700556 private int getConnectivityChangeDelay() {
557 final ContentResolver cr = mContext.getContentResolver();
558
559 /** Check system properties for the default value then use secure settings value, if any. */
560 int defaultDelay = SystemProperties.getInt(
561 "conn." + Settings.Secure.CONNECTIVITY_CHANGE_DELAY,
562 Settings.Secure.CONNECTIVITY_CHANGE_DELAY_DEFAULT);
563 return Settings.Secure.getInt(cr, Settings.Secure.CONNECTIVITY_CHANGE_DELAY,
564 defaultDelay);
565 }
566
The Android Open Source Project28527d22009-03-03 19:31:44 -0800567 private int getPersistedNetworkPreference() {
568 final ContentResolver cr = mContext.getContentResolver();
569
570 final int networkPrefSetting = Settings.Secure
571 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
572 if (networkPrefSetting != -1) {
573 return networkPrefSetting;
574 }
575
576 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
577 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700578
The Android Open Source Project28527d22009-03-03 19:31:44 -0800579 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700580 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800581 * In this method, we only tear down a non-preferred network. Establishing
582 * a connection to the preferred network is taken care of when we handle
583 * the disconnect event from the non-preferred network
584 * (see {@link #handleDisconnect(NetworkInfo)}).
585 */
586 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700587 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800588 return;
589
Robert Greenwalt2034b912009-08-12 16:08:25 -0700590 if (!mNetTrackers[mNetworkPreference].isAvailable())
591 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800592
Robert Greenwalt2034b912009-08-12 16:08:25 -0700593 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700594 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700595 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700596 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800597 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700598 " in enforcePreference");
599 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700600 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800601 }
602 }
603 }
604
605 private boolean teardown(NetworkStateTracker netTracker) {
606 if (netTracker.teardown()) {
607 netTracker.setTeardownRequested(true);
608 return true;
609 } else {
610 return false;
611 }
612 }
613
614 /**
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700615 * Check if UID should be blocked from using the network represented by the
616 * given {@link NetworkStateTracker}.
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700617 */
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700618 private boolean isNetworkBlocked(NetworkStateTracker tracker, int uid) {
619 final String iface = tracker.getLinkProperties().getInterfaceName();
Jeff Sharkey21062e72011-05-28 20:56:34 -0700620
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700621 final boolean networkCostly;
622 final int uidRules;
623 synchronized (mRulesLock) {
624 networkCostly = mMeteredIfaces.contains(iface);
625 uidRules = mUidRules.get(uid, RULE_ALLOW_ALL);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700626 }
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700627
628 if (networkCostly && (uidRules & RULE_REJECT_METERED) != 0) {
629 return true;
630 }
631
632 // no restrictive rules; network is visible
633 return false;
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700634 }
635
636 /**
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700637 * Return a filtered {@link NetworkInfo}, potentially marked
638 * {@link DetailedState#BLOCKED} based on
639 * {@link #isNetworkBlocked(NetworkStateTracker, int)}.
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700640 */
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700641 private NetworkInfo getFilteredNetworkInfo(NetworkStateTracker tracker, int uid) {
642 NetworkInfo info = tracker.getNetworkInfo();
643 if (isNetworkBlocked(tracker, uid)) {
Jeff Sharkey21062e72011-05-28 20:56:34 -0700644 // network is blocked; clone and override state
645 info = new NetworkInfo(info);
646 info.setDetailedState(DetailedState.BLOCKED, null, null);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700647 }
Jeff Sharkey21062e72011-05-28 20:56:34 -0700648 return info;
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700649 }
650
651 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -0800652 * Return NetworkInfo for the active (i.e., connected) network interface.
653 * It is assumed that at most one network is active at a time. If more
654 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700655 * @return the info for the active network, or {@code null} if none is
656 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800657 */
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700658 @Override
The Android Open Source Project28527d22009-03-03 19:31:44 -0800659 public NetworkInfo getActiveNetworkInfo() {
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700660 enforceAccessPermission();
661 final int uid = Binder.getCallingUid();
662 return getNetworkInfo(mActiveDefaultNetwork, uid);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800663 }
664
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700665 @Override
666 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
667 enforceConnectivityInternalPermission();
668 return getNetworkInfo(mActiveDefaultNetwork, uid);
669 }
670
671 @Override
The Android Open Source Project28527d22009-03-03 19:31:44 -0800672 public NetworkInfo getNetworkInfo(int networkType) {
673 enforceAccessPermission();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700674 final int uid = Binder.getCallingUid();
675 return getNetworkInfo(networkType, uid);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800676 }
677
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700678 private NetworkInfo getNetworkInfo(int networkType, int uid) {
679 NetworkInfo info = null;
680 if (isNetworkTypeValid(networkType)) {
681 final NetworkStateTracker tracker = mNetTrackers[networkType];
682 if (tracker != null) {
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700683 info = getFilteredNetworkInfo(tracker, uid);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700684 }
685 }
686 return info;
687 }
688
689 @Override
The Android Open Source Project28527d22009-03-03 19:31:44 -0800690 public NetworkInfo[] getAllNetworkInfo() {
691 enforceAccessPermission();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700692 final int uid = Binder.getCallingUid();
Jeff Sharkey21062e72011-05-28 20:56:34 -0700693 final ArrayList<NetworkInfo> result = Lists.newArrayList();
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700694 synchronized (mRulesLock) {
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700695 for (NetworkStateTracker tracker : mNetTrackers) {
696 if (tracker != null) {
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700697 result.add(getFilteredNetworkInfo(tracker, uid));
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700698 }
699 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800700 }
Jeff Sharkey21062e72011-05-28 20:56:34 -0700701 return result.toArray(new NetworkInfo[result.size()]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800702 }
703
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700704 /**
705 * Return LinkProperties for the active (i.e., connected) default
706 * network interface. It is assumed that at most one default network
707 * is active at a time. If more than one is active, it is indeterminate
708 * which will be returned.
709 * @return the ip properties for the active network, or {@code null} if
710 * none is active
711 */
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700712 @Override
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700713 public LinkProperties getActiveLinkProperties() {
Robert Greenwalte1544bb2011-05-20 12:23:41 -0700714 return getLinkProperties(mActiveDefaultNetwork);
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700715 }
716
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700717 @Override
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700718 public LinkProperties getLinkProperties(int networkType) {
719 enforceAccessPermission();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700720 if (isNetworkTypeValid(networkType)) {
721 final NetworkStateTracker tracker = mNetTrackers[networkType];
722 if (tracker != null) {
723 return tracker.getLinkProperties();
724 }
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700725 }
726 return null;
727 }
728
Jeff Sharkey21062e72011-05-28 20:56:34 -0700729 @Override
730 public NetworkState[] getAllNetworkState() {
731 enforceAccessPermission();
732 final int uid = Binder.getCallingUid();
733 final ArrayList<NetworkState> result = Lists.newArrayList();
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700734 synchronized (mRulesLock) {
Jeff Sharkey21062e72011-05-28 20:56:34 -0700735 for (NetworkStateTracker tracker : mNetTrackers) {
736 if (tracker != null) {
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700737 final NetworkInfo info = getFilteredNetworkInfo(tracker, uid);
Jeff Sharkey21062e72011-05-28 20:56:34 -0700738 result.add(new NetworkState(
739 info, tracker.getLinkProperties(), tracker.getLinkCapabilities()));
740 }
741 }
742 }
743 return result.toArray(new NetworkState[result.size()]);
744 }
745
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700746 private NetworkState getNetworkStateUnchecked(int networkType) {
747 if (isNetworkTypeValid(networkType)) {
748 final NetworkStateTracker tracker = mNetTrackers[networkType];
749 if (tracker != null) {
750 return new NetworkState(tracker.getNetworkInfo(), tracker.getLinkProperties(),
751 tracker.getLinkCapabilities());
752 }
753 }
754 return null;
755 }
756
757 @Override
758 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
759 enforceAccessPermission();
760 final NetworkState state = getNetworkStateUnchecked(mActiveDefaultNetwork);
761 if (state != null) {
762 try {
763 return mPolicyManager.getNetworkQuotaInfo(state);
764 } catch (RemoteException e) {
765 }
766 }
767 return null;
768 }
769
The Android Open Source Project28527d22009-03-03 19:31:44 -0800770 public boolean setRadios(boolean turnOn) {
771 boolean result = true;
772 enforceChangePermission();
773 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700774 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800775 }
776 return result;
777 }
778
779 public boolean setRadio(int netType, boolean turnOn) {
780 enforceChangePermission();
781 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
782 return false;
783 }
784 NetworkStateTracker tracker = mNetTrackers[netType];
785 return tracker != null && tracker.setRadio(turnOn);
786 }
787
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700788 /**
789 * Used to notice when the calling process dies so we can self-expire
790 *
791 * Also used to know if the process has cleaned up after itself when
792 * our auto-expire timer goes off. The timer has a link to an object.
793 *
794 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700795 private class FeatureUser implements IBinder.DeathRecipient {
796 int mNetworkType;
797 String mFeature;
798 IBinder mBinder;
799 int mPid;
800 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800801 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700802
803 FeatureUser(int type, String feature, IBinder binder) {
804 super();
805 mNetworkType = type;
806 mFeature = feature;
807 mBinder = binder;
808 mPid = getCallingPid();
809 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800810 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700811
Robert Greenwalt2034b912009-08-12 16:08:25 -0700812 try {
813 mBinder.linkToDeath(this, 0);
814 } catch (RemoteException e) {
815 binderDied();
816 }
817 }
818
819 void unlinkDeathRecipient() {
820 mBinder.unlinkToDeath(this, 0);
821 }
822
823 public void binderDied() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800824 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800825 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
826 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700827 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700828 }
829
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700830 public void expire() {
Robert Greenwalt78f28112011-08-02 17:18:41 -0700831 if (VDBG) {
832 log("ConnectivityService FeatureUser expire(" +
833 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
834 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
835 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700836 stopUsingNetworkFeature(this, false);
837 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800838
Kazuhiro Ondocae18f12011-07-19 11:23:37 -0500839 public boolean isSameUser(FeatureUser u) {
840 if (u == null) return false;
841
842 return isSameUser(u.mPid, u.mUid, u.mNetworkType, u.mFeature);
843 }
844
845 public boolean isSameUser(int pid, int uid, int networkType, String feature) {
846 if ((mPid == pid) && (mUid == uid) && (mNetworkType == networkType) &&
847 TextUtils.equals(mFeature, feature)) {
848 return true;
849 }
850 return false;
851 }
852
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800853 public String toString() {
854 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
855 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
856 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700857 }
858
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700859 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700860 public int startUsingNetworkFeature(int networkType, String feature,
861 IBinder binder) {
862 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800863 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700864 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800865 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700866 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwalt34848c02011-03-25 13:09:25 -0700867 mNetConfigs[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700868 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800869 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700870
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700871 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700872
repo syncf5de5572011-07-29 23:55:49 -0700873 // TODO - move this into individual networktrackers
874 int usedNetworkType = convertFeatureToNetworkType(networkType, feature);
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700875
876 if (mProtectedNetworks.contains(usedNetworkType)) {
877 enforceConnectivityInternalPermission();
878 }
879
Robert Greenwalt2034b912009-08-12 16:08:25 -0700880 NetworkStateTracker network = mNetTrackers[usedNetworkType];
881 if (network != null) {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800882 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt2034b912009-08-12 16:08:25 -0700883 if (usedNetworkType != networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700884 NetworkInfo ni = network.getNetworkInfo();
885
886 if (ni.isAvailable() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800887 if (DBG) log("special network not available");
Robert Greenwalt2cc87442010-12-29 14:35:21 -0800888 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
889 return Phone.APN_TYPE_NOT_AVAILABLE;
890 } else {
891 // else make the attempt anyway - probably giving REQUEST_STARTED below
892 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700893 }
894
Kazuhiro Ondocae18f12011-07-19 11:23:37 -0500895 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
896
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700897 synchronized(this) {
Kazuhiro Ondocae18f12011-07-19 11:23:37 -0500898 boolean addToList = true;
899 if (restoreTimer < 0) {
900 // In case there is no timer is specified for the feature,
901 // make sure we don't add duplicate entry with the same request.
902 for (FeatureUser u : mFeatureUsers) {
903 if (u.isSameUser(f)) {
904 // Duplicate user is found. Do not add.
905 addToList = false;
906 break;
907 }
908 }
909 }
910
911 if (addToList) mFeatureUsers.add(f);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700912 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
913 // this gets used for per-pid dns when connected
914 mNetRequestersPids[usedNetworkType].add(currentPid);
915 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700916 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700917
Robert Greenwalt20f819c2011-05-03 19:02:44 -0700918 if (restoreTimer >= 0) {
919 mHandler.sendMessageDelayed(
920 mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
921 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700922
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700923 if ((ni.isConnectedOrConnecting() == true) &&
924 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700925 if (ni.isConnected() == true) {
926 // add the pid-specific dns
repo sync3035f652011-08-02 13:39:06 -0700927 handleDnsConfigurationChange(usedNetworkType);
Wink Savillee70c6f52010-12-03 12:01:38 -0800928 if (DBG) log("special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700929 return Phone.APN_ALREADY_ACTIVE;
930 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800931 if (DBG) log("special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700932 return Phone.APN_REQUEST_STARTED;
933 }
934
935 // check if the radio in play can make another contact
936 // assume if cannot for now
937
Wink Savillee70c6f52010-12-03 12:01:38 -0800938 if (DBG) log("reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700939 network.reconnect();
940 return Phone.APN_REQUEST_STARTED;
941 } else {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800942 // need to remember this unsupported request so we respond appropriately on stop
943 synchronized(this) {
944 mFeatureUsers.add(f);
945 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
946 // this gets used for per-pid dns when connected
947 mNetRequestersPids[usedNetworkType].add(currentPid);
948 }
949 }
Robert Greenwaltd391e892010-05-18 10:52:51 -0700950 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700951 }
952 }
953 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800954 }
955
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700956 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800957 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700958 enforceChangePermission();
959
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700960 int pid = getCallingPid();
961 int uid = getCallingUid();
962
963 FeatureUser u = null;
964 boolean found = false;
965
966 synchronized(this) {
Kazuhiro Ondocae18f12011-07-19 11:23:37 -0500967 for (FeatureUser x : mFeatureUsers) {
968 if (x.isSameUser(pid, uid, networkType, feature)) {
969 u = x;
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700970 found = true;
971 break;
972 }
973 }
974 }
975 if (found && u != null) {
976 // stop regardless of how many other time this proc had called start
977 return stopUsingNetworkFeature(u, true);
978 } else {
979 // none found!
Robert Greenwalt78f28112011-08-02 17:18:41 -0700980 if (VDBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700981 return 1;
982 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700983 }
984
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700985 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
986 int networkType = u.mNetworkType;
987 String feature = u.mFeature;
988 int pid = u.mPid;
989 int uid = u.mUid;
990
991 NetworkStateTracker tracker = null;
992 boolean callTeardown = false; // used to carry our decision outside of sync block
993
Robert Greenwalt2034b912009-08-12 16:08:25 -0700994 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800995 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700996 ": " + feature);
997 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700998
The Android Open Source Project28527d22009-03-03 19:31:44 -0800999 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
1000 return -1;
1001 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001002
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001003 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
1004 // sync block
1005 synchronized(this) {
1006 // check if this process still has an outstanding start request
1007 if (!mFeatureUsers.contains(u)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001008 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001009 return 1;
1010 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001011 u.unlinkDeathRecipient();
1012 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
1013 // If we care about duplicate requests, check for that here.
1014 //
1015 // This is done to support the extension of a request - the app
1016 // can request we start the network feature again and renew the
1017 // auto-shutoff delay. Normal "stop" calls from the app though
1018 // do not pay attention to duplicate requests - in effect the
1019 // API does not refcount and a single stop will counter multiple starts.
1020 if (ignoreDups == false) {
Kazuhiro Ondocae18f12011-07-19 11:23:37 -05001021 for (FeatureUser x : mFeatureUsers) {
1022 if (x.isSameUser(u)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001023 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001024 return 1;
1025 }
1026 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001027 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001028
repo syncf5de5572011-07-29 23:55:49 -07001029 // TODO - move to individual network trackers
1030 int usedNetworkType = convertFeatureToNetworkType(networkType, feature);
1031
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001032 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001033 if (tracker == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001034 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001035 return -1;
1036 }
1037 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001038 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001039 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -08001040 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001041 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001042 if (DBG) log("not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001043 "others still using it");
1044 return 1;
1045 }
1046 callTeardown = true;
Robert Greenwalt9f3be4c2011-01-10 11:58:31 -08001047 } else {
1048 if (DBG) log("not a known feature - dropping");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001049 }
1050 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001051 if (DBG) log("Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001052 if (callTeardown) {
1053 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001054 return 1;
1055 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -07001056 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001057 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001058 }
1059
1060 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001061 * @deprecated use requestRouteToHostAddress instead
1062 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08001063 * Ensure that a network route exists to deliver traffic to the specified
1064 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001065 * @param networkType the type of the network over which traffic to the
1066 * specified host is to be routed
1067 * @param hostAddress the IP address of the host to which the route is
1068 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -08001069 * @return {@code true} on success, {@code false} on failure
1070 */
1071 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001072 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
1073
1074 if (inetAddress == null) {
1075 return false;
1076 }
1077
1078 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
1079 }
1080
1081 /**
1082 * Ensure that a network route exists to deliver traffic to the specified
1083 * host via the specified network interface.
1084 * @param networkType the type of the network over which traffic to the
1085 * specified host is to be routed
1086 * @param hostAddress the IP address of the host to which the route is
1087 * desired
1088 * @return {@code true} on success, {@code false} on failure
1089 */
1090 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001091 enforceChangePermission();
Robert Greenwalt6cac0742011-06-21 17:26:14 -07001092 if (mProtectedNetworks.contains(networkType)) {
1093 enforceConnectivityInternalPermission();
1094 }
1095
The Android Open Source Project28527d22009-03-03 19:31:44 -08001096 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
1097 return false;
1098 }
1099 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -07001100
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001101 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
1102 tracker.isTeardownRequested()) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001103 if (VDBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001104 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001105 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -07001106 }
1107 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001108 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001109 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001110 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001111 LinkProperties lp = tracker.getLinkProperties();
Robert Greenwalt98107422011-07-22 11:55:33 -07001112 return addRouteToAddress(lp, addr);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001113 } catch (UnknownHostException e) {}
1114 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001115 }
1116
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001117 private boolean addRoute(LinkProperties p, RouteInfo r) {
1118 return modifyRoute(p.getInterfaceName(), p, r, 0, true);
Robert Greenwaltbd492212011-05-06 17:10:53 -07001119 }
1120
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001121 private boolean removeRoute(LinkProperties p, RouteInfo r) {
1122 return modifyRoute(p.getInterfaceName(), p, r, 0, false);
1123 }
1124
Robert Greenwalt98107422011-07-22 11:55:33 -07001125 private boolean addRouteToAddress(LinkProperties lp, InetAddress addr) {
1126 return modifyRouteToAddress(lp, addr, true);
1127 }
1128
1129 private boolean removeRouteToAddress(LinkProperties lp, InetAddress addr) {
1130 return modifyRouteToAddress(lp, addr, false);
1131 }
1132
1133 private boolean modifyRouteToAddress(LinkProperties lp, InetAddress addr, boolean doAdd) {
1134 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), addr);
1135 if (bestRoute == null) {
1136 bestRoute = RouteInfo.makeHostRoute(addr);
1137 } else {
1138 if (bestRoute.getGateway().equals(addr)) {
1139 // if there is no better route, add the implied hostroute for our gateway
1140 bestRoute = RouteInfo.makeHostRoute(addr);
1141 } else {
1142 // if we will connect to this through another route, add a direct route
1143 // to it's gateway
1144 bestRoute = RouteInfo.makeHostRoute(addr, bestRoute.getGateway());
1145 }
1146 }
1147 return modifyRoute(lp.getInterfaceName(), lp, bestRoute, 0, doAdd);
1148 }
1149
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001150 private boolean modifyRoute(String ifaceName, LinkProperties lp, RouteInfo r, int cycleCount,
1151 boolean doAdd) {
1152 if ((ifaceName == null) || (lp == null) || (r == null)) return false;
1153
1154 if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
1155 loge("Error adding route - too much recursion");
1156 return false;
1157 }
1158
1159 if (r.isHostRoute() == false) {
1160 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), r.getGateway());
1161 if (bestRoute != null) {
Robert Greenwalt57c83bb2011-07-15 09:45:08 -07001162 if (bestRoute.getGateway().equals(r.getGateway())) {
1163 // if there is no better route, add the implied hostroute for our gateway
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001164 bestRoute = RouteInfo.makeHostRoute(r.getGateway());
Robert Greenwalt57c83bb2011-07-15 09:45:08 -07001165 } else {
1166 // if we will connect to our gateway through another route, add a direct
1167 // route to it's gateway
1168 bestRoute = RouteInfo.makeHostRoute(r.getGateway(), bestRoute.getGateway());
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001169 }
Robert Greenwalt57c83bb2011-07-15 09:45:08 -07001170 modifyRoute(ifaceName, lp, bestRoute, cycleCount+1, doAdd);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001171 }
1172 }
1173 if (doAdd) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001174 if (VDBG) log("Adding " + r + " for interface " + ifaceName);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001175 mAddedRoutes.add(r);
1176 try {
1177 mNetd.addRoute(ifaceName, r);
1178 } catch (Exception e) {
1179 // never crash - catch them all
Robert Greenwalt78f28112011-08-02 17:18:41 -07001180 if (VDBG) loge("Exception trying to add a route: " + e);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001181 return false;
1182 }
1183 } else {
1184 // if we remove this one and there are no more like it, then refcount==0 and
1185 // we can remove it from the table
1186 mAddedRoutes.remove(r);
1187 if (mAddedRoutes.contains(r) == false) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001188 if (VDBG) log("Removing " + r + " for interface " + ifaceName);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001189 try {
1190 mNetd.removeRoute(ifaceName, r);
1191 } catch (Exception e) {
1192 // never crash - catch them all
Robert Greenwalt78f28112011-08-02 17:18:41 -07001193 if (VDBG) loge("Exception trying to remove a route: " + e);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001194 return false;
1195 }
1196 } else {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001197 if (VDBG) log("not removing " + r + " as it's still in use");
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001198 }
1199 }
1200 return true;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001201 }
1202
1203 /**
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001204 * @see ConnectivityManager#getMobileDataEnabled()
1205 */
1206 public boolean getMobileDataEnabled() {
Wink Savilleb9024c62010-12-07 10:31:02 -08001207 // TODO: This detail should probably be in DataConnectionTracker's
1208 // which is where we store the value and maybe make this
1209 // asynchronous.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001210 enforceAccessPermission();
1211 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1212 Settings.Secure.MOBILE_DATA, 1) == 1;
Robert Greenwalt78f28112011-08-02 17:18:41 -07001213 if (VDBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001214 return retVal;
1215 }
1216
Robert Greenwalt34848c02011-03-25 13:09:25 -07001217 public void setDataDependency(int networkType, boolean met) {
Robert Greenwalt6cac0742011-06-21 17:26:14 -07001218 enforceConnectivityInternalPermission();
1219
Robert Greenwalt34848c02011-03-25 13:09:25 -07001220 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1221 (met ? ENABLED : DISABLED), networkType));
1222 }
1223
1224 private void handleSetDependencyMet(int networkType, boolean met) {
1225 if (mNetTrackers[networkType] != null) {
1226 if (DBG) {
1227 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1228 }
1229 mNetTrackers[networkType].setDependencyMet(met);
1230 }
1231 }
1232
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001233 private INetworkPolicyListener mPolicyListener = new INetworkPolicyListener.Stub() {
1234 @Override
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001235 public void onUidRulesChanged(int uid, int uidRules) {
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001236 // only someone like NPMS should only be calling us
Jeff Sharkey4434b0b2011-06-16 13:04:20 -07001237 mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001238
1239 if (LOGD_RULES) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001240 log("onUidRulesChanged(uid=" + uid + ", uidRules=" + uidRules + ")");
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001241 }
1242
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001243 synchronized (mRulesLock) {
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001244 // skip update when we've already applied rules
1245 final int oldRules = mUidRules.get(uid, RULE_ALLOW_ALL);
1246 if (oldRules == uidRules) return;
1247
1248 mUidRules.put(uid, uidRules);
1249 }
1250
1251 // TODO: dispatch into NMS to push rules towards kernel module
1252 // TODO: notify UID when it has requested targeted updates
1253 }
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001254
1255 @Override
1256 public void onMeteredIfacesChanged(String[] meteredIfaces) {
1257 // only someone like NPMS should only be calling us
1258 mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
1259
1260 if (LOGD_RULES) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001261 log("onMeteredIfacesChanged(ifaces=" + Arrays.toString(meteredIfaces) + ")");
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001262 }
1263
1264 synchronized (mRulesLock) {
1265 mMeteredIfaces.clear();
1266 for (String iface : meteredIfaces) {
1267 mMeteredIfaces.add(iface);
1268 }
1269 }
1270 }
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001271 };
1272
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001273 /**
1274 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1275 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001276 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001277 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -08001278 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001279
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001280 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwalt34848c02011-03-25 13:09:25 -07001281 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001282 }
1283
1284 private void handleSetMobileData(boolean enabled) {
Wink Savilleb9024c62010-12-07 10:31:02 -08001285 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001286 if (VDBG) {
1287 log(mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001288 }
Jeff Sharkey805662d2011-08-19 02:24:24 -07001289 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setUserDataEnable(enabled);
1290 }
1291 }
1292
1293 @Override
1294 public void setPolicyDataEnable(int networkType, boolean enabled) {
1295 // only someone like NPMS should only be calling us
1296 mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
1297
1298 mHandler.sendMessage(mHandler.obtainMessage(
1299 EVENT_SET_POLICY_DATA_ENABLE, networkType, (enabled ? ENABLED : DISABLED)));
1300 }
1301
1302 private void handleSetPolicyDataEnable(int networkType, boolean enabled) {
1303 if (isNetworkTypeValid(networkType)) {
1304 final NetworkStateTracker tracker = mNetTrackers[networkType];
1305 if (tracker != null) {
1306 tracker.setPolicyDataEnable(enabled);
1307 }
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001308 }
1309 }
1310
The Android Open Source Project28527d22009-03-03 19:31:44 -08001311 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001312 mContext.enforceCallingOrSelfPermission(
1313 android.Manifest.permission.ACCESS_NETWORK_STATE,
1314 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001315 }
1316
1317 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001318 mContext.enforceCallingOrSelfPermission(
1319 android.Manifest.permission.CHANGE_NETWORK_STATE,
1320 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001321 }
1322
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001323 // TODO Make this a special check when it goes public
1324 private void enforceTetherChangePermission() {
1325 mContext.enforceCallingOrSelfPermission(
1326 android.Manifest.permission.CHANGE_NETWORK_STATE,
1327 "ConnectivityService");
1328 }
1329
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001330 private void enforceTetherAccessPermission() {
1331 mContext.enforceCallingOrSelfPermission(
1332 android.Manifest.permission.ACCESS_NETWORK_STATE,
1333 "ConnectivityService");
1334 }
1335
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001336 private void enforceConnectivityInternalPermission() {
1337 mContext.enforceCallingOrSelfPermission(
1338 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1339 "ConnectivityService");
1340 }
1341
The Android Open Source Project28527d22009-03-03 19:31:44 -08001342 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001343 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1344 * network, we ignore it. If it is for the active network, we send out a
1345 * broadcast. But first, we check whether it might be possible to connect
1346 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001347 * @param info the {@code NetworkInfo} for the network
1348 */
1349 private void handleDisconnect(NetworkInfo info) {
1350
Robert Greenwalt2034b912009-08-12 16:08:25 -07001351 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001352
Robert Greenwalt2034b912009-08-12 16:08:25 -07001353 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001354 /*
1355 * If the disconnected network is not the active one, then don't report
1356 * this as a loss of connectivity. What probably happened is that we're
1357 * getting the disconnect for a network that we explicitly disabled
1358 * in accordance with network preference policies.
1359 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001360 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001361 List pids = mNetRequestersPids[prevNetType];
1362 for (int i = 0; i<pids.size(); i++) {
1363 Integer pid = (Integer)pids.get(i);
1364 // will remove them because the net's no longer connected
1365 // need to do this now as only now do we know the pids and
1366 // can properly null things that are no longer referenced.
1367 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001368 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001369 }
1370
The Android Open Source Project28527d22009-03-03 19:31:44 -08001371 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1372 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1373 if (info.isFailover()) {
1374 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1375 info.setFailover(false);
1376 }
1377 if (info.getReason() != null) {
1378 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1379 }
1380 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001381 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1382 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001383 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001384
Robert Greenwalt34848c02011-03-25 13:09:25 -07001385 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001386 tryFailover(prevNetType);
1387 if (mActiveDefaultNetwork != -1) {
1388 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001389 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1390 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001391 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001392 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1393 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001394 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001395 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001396
1397 // Reset interface if no other connections are using the same interface
1398 boolean doReset = true;
1399 LinkProperties linkProperties = mNetTrackers[prevNetType].getLinkProperties();
1400 if (linkProperties != null) {
1401 String oldIface = linkProperties.getInterfaceName();
1402 if (TextUtils.isEmpty(oldIface) == false) {
1403 for (NetworkStateTracker networkStateTracker : mNetTrackers) {
1404 if (networkStateTracker == null) continue;
1405 NetworkInfo networkInfo = networkStateTracker.getNetworkInfo();
1406 if (networkInfo.isConnected() && networkInfo.getType() != prevNetType) {
1407 LinkProperties l = networkStateTracker.getLinkProperties();
1408 if (l == null) continue;
1409 if (oldIface.equals(l.getInterfaceName())) {
1410 doReset = false;
1411 break;
1412 }
1413 }
1414 }
1415 }
1416 }
1417
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001418 // do this before we broadcast the change
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001419 handleConnectivityChange(prevNetType, doReset);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001420
Wink Saville4f0de1e2011-08-04 15:01:58 -07001421 sendStickyBroadcastDelayed(intent, getConnectivityChangeDelay());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001422 /*
1423 * If the failover network is already connected, then immediately send
1424 * out a followup broadcast indicating successful failover
1425 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001426 if (mActiveDefaultNetwork != -1) {
Wink Saville4f0de1e2011-08-04 15:01:58 -07001427 sendConnectedBroadcastDelayed(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(),
1428 getConnectivityChangeDelay());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001429 }
1430 }
1431
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001432 private void tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001433 /*
Robert Greenwalt92564852011-01-06 15:41:07 -08001434 * If this is a default network, check if other defaults are available.
1435 * Try to reconnect on all available and let them hash it out when
1436 * more than one connects.
Robert Greenwalt2034b912009-08-12 16:08:25 -07001437 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001438 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001439 if (mActiveDefaultNetwork == prevNetType) {
1440 mActiveDefaultNetwork = -1;
1441 }
1442
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001443 // don't signal a reconnect for anything lower or equal priority than our
1444 // current connected default
1445 // TODO - don't filter by priority now - nice optimization but risky
1446// int currentPriority = -1;
1447// if (mActiveDefaultNetwork != -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001448// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001449// }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001450 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001451 if (checkType == prevNetType) continue;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001452 if (mNetConfigs[checkType] == null) continue;
1453 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville72a95b92011-01-26 15:43:49 -08001454
1455// Enabling the isAvailable() optimization caused mobile to not get
1456// selected if it was in the middle of error handling. Specifically
1457// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1458// would not be available and we wouldn't get connected to anything.
1459// So removing the isAvailable() optimization below for now. TODO: This
1460// optimization should work and we need to investigate why it doesn't work.
1461// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1462// complete before it is really complete.
1463// if (!mNetTrackers[checkType].isAvailable()) continue;
1464
Robert Greenwalt34848c02011-03-25 13:09:25 -07001465// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001466
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001467 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1468 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1469 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1470 checkInfo.setFailover(true);
1471 checkTracker.reconnect();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001472 }
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001473 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001474 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001475 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001476 }
1477
1478 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001479 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1480 }
1481
Wink Saville4f0de1e2011-08-04 15:01:58 -07001482 private void sendConnectedBroadcastDelayed(NetworkInfo info, int delayMs) {
1483 sendGeneralBroadcastDelayed(info, ConnectivityManager.CONNECTIVITY_ACTION, delayMs);
1484 }
1485
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001486 private void sendInetConditionBroadcast(NetworkInfo info) {
1487 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1488 }
1489
Wink Saville4f0de1e2011-08-04 15:01:58 -07001490 private Intent makeGeneralIntent(NetworkInfo info, String bcastType) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001491 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001492 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1493 if (info.isFailover()) {
1494 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1495 info.setFailover(false);
1496 }
1497 if (info.getReason() != null) {
1498 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1499 }
1500 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001501 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1502 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001503 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001504 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Wink Saville4f0de1e2011-08-04 15:01:58 -07001505 return intent;
1506 }
1507
1508 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1509 sendStickyBroadcast(makeGeneralIntent(info, bcastType));
1510 }
1511
1512 private void sendGeneralBroadcastDelayed(NetworkInfo info, String bcastType, int delayMs) {
1513 sendStickyBroadcastDelayed(makeGeneralIntent(info, bcastType), delayMs);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001514 }
1515
1516 /**
1517 * Called when an attempt to fail over to another network has failed.
1518 * @param info the {@link NetworkInfo} for the failed network
1519 */
1520 private void handleConnectionFailure(NetworkInfo info) {
1521 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001522
Robert Greenwalt2034b912009-08-12 16:08:25 -07001523 String reason = info.getReason();
1524 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001525
Robert Greenwalte981bc52010-10-08 16:35:52 -07001526 String reasonText;
1527 if (reason == null) {
1528 reasonText = ".";
1529 } else {
1530 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001531 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001532 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001533
1534 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1535 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1536 if (getActiveNetworkInfo() == null) {
1537 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1538 }
1539 if (reason != null) {
1540 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1541 }
1542 if (extraInfo != null) {
1543 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1544 }
1545 if (info.isFailover()) {
1546 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1547 info.setFailover(false);
1548 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001549
Robert Greenwalt34848c02011-03-25 13:09:25 -07001550 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001551 tryFailover(info.getType());
1552 if (mActiveDefaultNetwork != -1) {
1553 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001554 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1555 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001556 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001557 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1558 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001559 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001560
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001561 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001562 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001563 /*
1564 * If the failover network is already connected, then immediately send
1565 * out a followup broadcast indicating successful failover
1566 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001567 if (mActiveDefaultNetwork != -1) {
1568 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001569 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001570 }
1571
1572 private void sendStickyBroadcast(Intent intent) {
1573 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001574 if (!mSystemReady) {
1575 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001576 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001577 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Wink Saville4f0de1e2011-08-04 15:01:58 -07001578 if (DBG) {
1579 log("sendStickyBroadcast: NetworkInfo=" +
1580 intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO));
1581 }
1582
Dianne Hackborna417ff82009-12-08 19:45:14 -08001583 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001584 }
1585 }
1586
Wink Saville4f0de1e2011-08-04 15:01:58 -07001587 private void sendStickyBroadcastDelayed(Intent intent, int delayMs) {
1588 if (delayMs <= 0) {
1589 sendStickyBroadcast(intent);
1590 } else {
1591 if (DBG) log("sendStickyBroadcastDelayed: delayMs=" + delayMs + " intent=" + intent);
1592 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1593 EVENT_SEND_STICKY_BROADCAST_INTENT, intent), delayMs);
1594 }
1595 }
1596
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001597 void systemReady() {
1598 synchronized(this) {
1599 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001600 if (mInitialBroadcast != null) {
1601 mContext.sendStickyBroadcast(mInitialBroadcast);
1602 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001603 }
1604 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001605 // load the global proxy at startup
1606 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001607 }
1608
1609 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001610 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001611
1612 // snapshot isFailover, because sendConnectedBroadcast() resets it
1613 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001614 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001615
Robert Greenwalt2034b912009-08-12 16:08:25 -07001616 // if this is a default net and other default is running
1617 // kill the one not preferred
Robert Greenwalt34848c02011-03-25 13:09:25 -07001618 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001619 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1620 if ((type != mNetworkPreference &&
Wink Savillef2a62832011-04-07 14:23:45 -07001621 mNetConfigs[mActiveDefaultNetwork].priority >
1622 mNetConfigs[type].priority) ||
Robert Greenwalt2034b912009-08-12 16:08:25 -07001623 mNetworkPreference == mActiveDefaultNetwork) {
1624 // don't accept this one
Robert Greenwalt78f28112011-08-02 17:18:41 -07001625 if (VDBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001626 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001627 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001628 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001629 teardown(thisNet);
1630 return;
1631 } else {
1632 // tear down the other
1633 NetworkStateTracker otherNet =
1634 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001635 if (DBG) {
1636 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001637 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001638 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001639 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001640 loge("Network declined teardown request");
Robert Greenwalt99910172011-03-29 11:36:28 -07001641 teardown(thisNet);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001642 return;
1643 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001644 }
1645 }
1646 synchronized (ConnectivityService.this) {
1647 // have a new default network, release the transition wakelock in a second
1648 // if it's held. The second pause is to allow apps to reconnect over the
1649 // new network
1650 if (mNetTransitionWakeLock.isHeld()) {
1651 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001652 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001653 mNetTransitionWakeLockSerialNumber, 0),
1654 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001655 }
1656 }
1657 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001658 // this will cause us to come up initially as unconnected and switching
1659 // to connected after our normal pause unless somebody reports us as reall
1660 // disconnected
1661 mDefaultInetConditionPublished = 0;
1662 mDefaultConnectionSequence++;
1663 mInetConditionChangeInFlight = false;
1664 // Don't do this - if we never sign in stay, grey
1665 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001666 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001667 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001668 updateNetworkSettings(thisNet);
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001669 handleConnectivityChange(type, false);
Wink Saville4f0de1e2011-08-04 15:01:58 -07001670 sendConnectedBroadcastDelayed(info, getConnectivityChangeDelay());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001671 }
1672
The Android Open Source Project28527d22009-03-03 19:31:44 -08001673 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001674 * After a change in the connectivity state of a network. We're mainly
1675 * concerned with making sure that the list of DNS servers is set up
1676 * according to which networks are connected, and ensuring that the
1677 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001678 */
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001679 private void handleConnectivityChange(int netType, boolean doReset) {
Wink Saville051a6642011-07-13 13:44:13 -07001680 int resetMask = doReset ? NetworkUtils.RESET_ALL_ADDRESSES : 0;
1681
The Android Open Source Project28527d22009-03-03 19:31:44 -08001682 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001683 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001684 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001685 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001686 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001687
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001688 LinkProperties curLp = mCurrentLinkProperties[netType];
1689 LinkProperties newLp = null;
1690
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001691 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001692 newLp = mNetTrackers[netType].getLinkProperties();
Wink Saville051a6642011-07-13 13:44:13 -07001693 if (VDBG) {
1694 log("handleConnectivityChange: changed linkProperty[" + netType + "]:" +
1695 " doReset=" + doReset + " resetMask=" + resetMask +
1696 "\n curLp=" + curLp +
1697 "\n newLp=" + newLp);
1698 }
1699
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001700 if (curLp != null) {
1701 if (curLp.isIdenticalInterfaceName(newLp)) {
1702 CompareResult<LinkAddress> car = curLp.compareAddresses(newLp);
1703 if ((car.removed.size() != 0) || (car.added.size() != 0)) {
1704 for (LinkAddress linkAddr : car.removed) {
1705 if (linkAddr.getAddress() instanceof Inet4Address) {
1706 resetMask |= NetworkUtils.RESET_IPV4_ADDRESSES;
1707 }
1708 if (linkAddr.getAddress() instanceof Inet6Address) {
1709 resetMask |= NetworkUtils.RESET_IPV6_ADDRESSES;
1710 }
Wink Saville051a6642011-07-13 13:44:13 -07001711 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001712 if (DBG) {
1713 log("handleConnectivityChange: addresses changed" +
1714 " linkProperty[" + netType + "]:" + " resetMask=" + resetMask +
1715 "\n car=" + car);
Wink Saville051a6642011-07-13 13:44:13 -07001716 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001717 } else {
1718 if (DBG) {
1719 log("handleConnectivityChange: address are the same reset per doReset" +
1720 " linkProperty[" + netType + "]:" +
1721 " resetMask=" + resetMask);
1722 }
Wink Saville051a6642011-07-13 13:44:13 -07001723 }
1724 } else {
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001725 resetMask = NetworkUtils.RESET_ALL_ADDRESSES;
Robert Greenwalt78f28112011-08-02 17:18:41 -07001726 if (DBG) {
1727 log("handleConnectivityChange: interface not not equivalent reset both" +
1728 " linkProperty[" + netType + "]:" +
1729 " resetMask=" + resetMask);
1730 }
Wink Saville051a6642011-07-13 13:44:13 -07001731 }
Wink Saville051a6642011-07-13 13:44:13 -07001732 }
Robert Greenwalt34848c02011-03-25 13:09:25 -07001733 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001734 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001735 }
1736 } else {
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001737 if (VDBG) {
1738 log("handleConnectivityChange: changed linkProperty[" + netType + "]:" +
1739 " doReset=" + doReset + " resetMask=" + resetMask +
1740 "\n curLp=" + curLp +
1741 "\n newLp= null");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001742 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001743 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001744 mCurrentLinkProperties[netType] = newLp;
Robert Greenwalt8d777252011-08-15 12:31:55 -07001745 boolean resetDns = updateRoutes(newLp, curLp, mNetConfigs[netType].isDefault());
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001746
Chia-chi Yeh4a243f52011-08-15 15:19:40 -07001747 if (resetMask != 0 || resetDns) {
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001748 LinkProperties linkProperties = mNetTrackers[netType].getLinkProperties();
1749 if (linkProperties != null) {
1750 String iface = linkProperties.getInterfaceName();
1751 if (TextUtils.isEmpty(iface) == false) {
Chia-chi Yeh4a243f52011-08-15 15:19:40 -07001752 if (resetMask != 0) {
Robert Greenwalt8d777252011-08-15 12:31:55 -07001753 if (DBG) log("resetConnections(" + iface + ", " + resetMask + ")");
1754 NetworkUtils.resetConnections(iface, resetMask);
Chia-chi Yeh4a243f52011-08-15 15:19:40 -07001755
1756 // Tell VPN the interface is down. It is a temporary
1757 // but effective fix to make VPN aware of the change.
1758 if ((resetMask & NetworkUtils.RESET_IPV4_ADDRESSES) != 0) {
1759 mVpn.interfaceStatusChanged(iface, false);
1760 }
Robert Greenwalt8d777252011-08-15 12:31:55 -07001761 }
1762 if (resetDns) {
1763 if (DBG) log("resetting DNS cache for " + iface);
1764 try {
1765 mNetd.flushInterfaceDnsCache(iface);
1766 } catch (Exception e) {
1767 // never crash - catch them all
1768 loge("Exception resetting dns cache: " + e);
1769 }
1770 }
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001771 }
1772 }
1773 }
Kazuhiro Ondo07680062011-06-22 21:10:34 -05001774
1775 // TODO: Temporary notifying upstread change to Tethering.
1776 // @see bug/4455071
1777 /** Notify TetheringService if interface name has been changed. */
1778 if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
1779 Phone.REASON_LINK_PROPERTIES_CHANGED)) {
1780 if (isTetheringSupported()) {
1781 mTethering.handleTetherIfaceChange();
1782 }
1783 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001784 }
1785
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001786 /**
1787 * Add and remove routes using the old properties (null if not previously connected),
1788 * new properties (null if becoming disconnected). May even be double null, which
1789 * is a noop.
1790 * Uses isLinkDefault to determine if default routes should be set or conversely if
1791 * host routes should be set to the dns servers
Robert Greenwalt8d777252011-08-15 12:31:55 -07001792 * returns a boolean indicating the routes changed
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001793 */
Robert Greenwalt8d777252011-08-15 12:31:55 -07001794 private boolean updateRoutes(LinkProperties newLp, LinkProperties curLp,
1795 boolean isLinkDefault) {
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001796 Collection<RouteInfo> routesToAdd = null;
Robert Greenwalt98107422011-07-22 11:55:33 -07001797 CompareResult<InetAddress> dnsDiff = new CompareResult<InetAddress>();
1798 CompareResult<RouteInfo> routeDiff = new CompareResult<RouteInfo>();
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001799 if (curLp != null) {
1800 // check for the delta between the current set and the new
Robert Greenwalt98107422011-07-22 11:55:33 -07001801 routeDiff = curLp.compareRoutes(newLp);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001802 dnsDiff = curLp.compareDnses(newLp);
Robert Greenwalt98107422011-07-22 11:55:33 -07001803 } else if (newLp != null) {
1804 routeDiff.added = newLp.getRoutes();
1805 dnsDiff.added = newLp.getDnses();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001806 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001807
Robert Greenwalt8d777252011-08-15 12:31:55 -07001808 boolean routesChanged = (routeDiff.removed.size() != 0 || routeDiff.added.size() != 0);
1809
Robert Greenwalt98107422011-07-22 11:55:33 -07001810 for (RouteInfo r : routeDiff.removed) {
1811 if (isLinkDefault || ! r.isDefaultRoute()) {
1812 removeRoute(curLp, r);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001813 }
Robert Greenwalt98107422011-07-22 11:55:33 -07001814 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001815
Robert Greenwalt98107422011-07-22 11:55:33 -07001816 for (RouteInfo r : routeDiff.added) {
1817 if (isLinkDefault || ! r.isDefaultRoute()) {
1818 addRoute(newLp, r);
Robert Greenwalt08ae9bc2011-08-09 16:52:58 -07001819 } else {
1820 // many radios add a default route even when we don't want one.
1821 // remove the default route unless somebody else has asked for it
1822 String ifaceName = newLp.getInterfaceName();
1823 if (TextUtils.isEmpty(ifaceName) == false && mAddedRoutes.contains(r) == false) {
1824 if (DBG) log("Removing " + r + " for interface " + ifaceName);
1825 try {
1826 mNetd.removeRoute(ifaceName, r);
1827 } catch (Exception e) {
1828 // never crash - catch them all
1829 loge("Exception trying to remove a route: " + e);
1830 }
1831 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001832 }
1833 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001834
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001835 if (!isLinkDefault) {
1836 // handle DNS routes
Robert Greenwalt8d777252011-08-15 12:31:55 -07001837 if (routesChanged) {
Robert Greenwalt98107422011-07-22 11:55:33 -07001838 // routes changed - remove all old dns entries and add new
1839 if (curLp != null) {
1840 for (InetAddress oldDns : curLp.getDnses()) {
1841 removeRouteToAddress(curLp, oldDns);
1842 }
1843 }
1844 if (newLp != null) {
1845 for (InetAddress newDns : newLp.getDnses()) {
1846 addRouteToAddress(newLp, newDns);
1847 }
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001848 }
Robert Greenwalt8d777252011-08-15 12:31:55 -07001849 } else {
1850 // no change in routes, check for change in dns themselves
1851 for (InetAddress oldDns : dnsDiff.removed) {
1852 removeRouteToAddress(curLp, oldDns);
1853 }
1854 for (InetAddress newDns : dnsDiff.added) {
1855 addRouteToAddress(newLp, newDns);
1856 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001857 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001858 }
Robert Greenwalt8d777252011-08-15 12:31:55 -07001859 return routesChanged;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001860 }
1861
1862
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001863 /**
1864 * Reads the network specific TCP buffer sizes from SystemProperties
1865 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1866 * wide use
1867 */
1868 public void updateNetworkSettings(NetworkStateTracker nt) {
1869 String key = nt.getTcpBufferSizesPropName();
1870 String bufferSizes = SystemProperties.get(key);
1871
1872 if (bufferSizes.length() == 0) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001873 if (VDBG) log(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001874
1875 // Setting to default values so we won't be stuck to previous values
1876 key = "net.tcp.buffersize.default";
1877 bufferSizes = SystemProperties.get(key);
1878 }
1879
1880 // Set values in kernel
1881 if (bufferSizes.length() != 0) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001882 if (VDBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001883 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001884 + "] which comes from [" + key + "]");
1885 }
1886 setBufferSize(bufferSizes);
1887 }
1888 }
1889
1890 /**
1891 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1892 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1893 *
1894 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1895 * writeMin, writeInitial, writeMax"
1896 */
1897 private void setBufferSize(String bufferSizes) {
1898 try {
1899 String[] values = bufferSizes.split(",");
1900
1901 if (values.length == 6) {
1902 final String prefix = "/sys/kernel/ipv4/tcp_";
Mike Lockwood0d5916c2011-05-28 13:24:04 -04001903 FileUtils.stringToFile(prefix + "rmem_min", values[0]);
1904 FileUtils.stringToFile(prefix + "rmem_def", values[1]);
1905 FileUtils.stringToFile(prefix + "rmem_max", values[2]);
1906 FileUtils.stringToFile(prefix + "wmem_min", values[3]);
1907 FileUtils.stringToFile(prefix + "wmem_def", values[4]);
1908 FileUtils.stringToFile(prefix + "wmem_max", values[5]);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001909 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001910 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001911 }
1912 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001913 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001914 }
1915 }
1916
Robert Greenwalt2034b912009-08-12 16:08:25 -07001917 /**
1918 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1919 * on the highest priority active net which this process requested.
1920 * If there aren't any, clear it out
1921 */
1922 private void reassessPidDns(int myPid, boolean doBump)
1923 {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001924 if (VDBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001925 for(int i : mPriorityList) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001926 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001927 continue;
1928 }
1929 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001930 if (nt.getNetworkInfo().isConnected() &&
1931 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001932 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001933 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001934 List pids = mNetRequestersPids[i];
1935 for (int j=0; j<pids.size(); j++) {
1936 Integer pid = (Integer)pids.get(j);
1937 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001938 Collection<InetAddress> dnses = p.getDnses();
1939 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001940 if (doBump) {
1941 bumpDns();
1942 }
1943 return;
1944 }
1945 }
1946 }
1947 }
1948 // nothing found - delete
1949 for (int i = 1; ; i++) {
1950 String prop = "net.dns" + i + "." + myPid;
1951 if (SystemProperties.get(prop).length() == 0) {
1952 if (doBump) {
1953 bumpDns();
1954 }
1955 return;
1956 }
1957 SystemProperties.set(prop, "");
1958 }
1959 }
1960
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001961 // return true if results in a change
1962 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001963 int j = 1;
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001964 boolean changed = false;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001965 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001966 String dnsString = dns.getHostAddress();
1967 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1968 changed = true;
1969 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1970 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001971 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001972 return changed;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001973 }
1974
1975 private void bumpDns() {
1976 /*
1977 * Bump the property that tells the name resolver library to reread
1978 * the DNS server list from the properties.
1979 */
1980 String propVal = SystemProperties.get("net.dnschange");
1981 int n = 0;
1982 if (propVal.length() != 0) {
1983 try {
1984 n = Integer.parseInt(propVal);
1985 } catch (NumberFormatException e) {}
1986 }
1987 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001988 /*
1989 * Tell the VMs to toss their DNS caches
1990 */
1991 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1992 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnuttf444f502011-01-05 17:14:03 -08001993 /*
1994 * Connectivity events can happen before boot has completed ...
1995 */
1996 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt051642b2010-11-02 14:08:23 -07001997 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001998 }
1999
Chia-chi Yehcc844502011-07-14 18:01:57 -07002000 // Caller must grab mDnsLock.
Robert Greenwaltfce71862011-07-25 16:06:25 -07002001 private boolean updateDns(String network, String iface,
2002 Collection<InetAddress> dnses, String domains) {
Chia-chi Yehcc844502011-07-14 18:01:57 -07002003 boolean changed = false;
2004 int last = 0;
2005 if (dnses.size() == 0 && mDefaultDns != null) {
2006 ++last;
2007 String value = mDefaultDns.getHostAddress();
2008 if (!value.equals(SystemProperties.get("net.dns1"))) {
2009 if (DBG) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07002010 loge("no dns provided for " + network + " - using " + value);
Chia-chi Yehcc844502011-07-14 18:01:57 -07002011 }
2012 changed = true;
2013 SystemProperties.set("net.dns1", value);
2014 }
2015 } else {
2016 for (InetAddress dns : dnses) {
2017 ++last;
2018 String key = "net.dns" + last;
2019 String value = dns.getHostAddress();
2020 if (!changed && value.equals(SystemProperties.get(key))) {
2021 continue;
2022 }
Robert Greenwalt78f28112011-08-02 17:18:41 -07002023 if (VDBG) {
Chia-chi Yehcc844502011-07-14 18:01:57 -07002024 log("adding dns " + value + " for " + network);
2025 }
2026 changed = true;
2027 SystemProperties.set(key, value);
2028 }
2029 }
2030 for (int i = last + 1; i <= mNumDnsEntries; ++i) {
2031 String key = "net.dns" + i;
Robert Greenwalt78f28112011-08-02 17:18:41 -07002032 if (VDBG) log("erasing " + key);
Chia-chi Yehcc844502011-07-14 18:01:57 -07002033 changed = true;
2034 SystemProperties.set(key, "");
2035 }
2036 mNumDnsEntries = last;
2037
Robert Greenwaltfce71862011-07-25 16:06:25 -07002038 if (changed) {
2039 try {
2040 mNetd.setDnsServersForInterface(iface, NetworkUtils.makeStrings(dnses));
2041 mNetd.setDefaultInterfaceForDns(iface);
2042 } catch (Exception e) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07002043 loge("exception setting default dns interface: " + e);
Robert Greenwaltfce71862011-07-25 16:06:25 -07002044 }
2045 }
Chia-chi Yehcc844502011-07-14 18:01:57 -07002046 if (!domains.equals(SystemProperties.get("net.dns.search"))) {
2047 SystemProperties.set("net.dns.search", domains);
2048 changed = true;
2049 }
2050 return changed;
2051 }
2052
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002053 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07002054 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002055 NetworkStateTracker nt = mNetTrackers[netType];
2056 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07002057 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002058 if (p == null) return;
2059 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt8ca88762010-12-17 15:20:36 -08002060 boolean changed = false;
Robert Greenwalt34848c02011-03-25 13:09:25 -07002061 if (mNetConfigs[netType].isDefault()) {
Chia-chi Yehcc844502011-07-14 18:01:57 -07002062 String network = nt.getNetworkInfo().getTypeName();
2063 synchronized (mDnsLock) {
2064 if (!mDnsOverridden) {
Robert Greenwaltfce71862011-07-25 16:06:25 -07002065 changed = updateDns(network, p.getInterfaceName(), dnses, "");
Robert Greenwalt94daa182010-09-01 11:34:05 -07002066 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002067 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002068 } else {
Robert Greenwaltfce71862011-07-25 16:06:25 -07002069 try {
Robert Greenwalt49f762e2011-07-27 10:00:36 -07002070 mNetd.setDnsServersForInterface(p.getInterfaceName(),
Robert Greenwaltfce71862011-07-25 16:06:25 -07002071 NetworkUtils.makeStrings(dnses));
2072 } catch (Exception e) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07002073 loge("exception setting dns servers: " + e);
Robert Greenwaltfce71862011-07-25 16:06:25 -07002074 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002075 // set per-pid dns for attached secondary nets
2076 List pids = mNetRequestersPids[netType];
2077 for (int y=0; y< pids.size(); y++) {
2078 Integer pid = (Integer)pids.get(y);
Robert Greenwalt8ca88762010-12-17 15:20:36 -08002079 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08002080 }
2081 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08002082 if (changed) bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08002083 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07002084 }
2085
Robert Greenwalt20f819c2011-05-03 19:02:44 -07002086 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07002087 String restoreDefaultNetworkDelayStr = SystemProperties.get(
2088 NETWORK_RESTORE_DELAY_PROP_NAME);
2089 if(restoreDefaultNetworkDelayStr != null &&
2090 restoreDefaultNetworkDelayStr.length() != 0) {
2091 try {
2092 return Integer.valueOf(restoreDefaultNetworkDelayStr);
2093 } catch (NumberFormatException e) {
2094 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002095 }
Robert Greenwalt20f819c2011-05-03 19:02:44 -07002096 // if the system property isn't set, use the value for the apn type
2097 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
2098
2099 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
2100 (mNetConfigs[networkType] != null)) {
2101 ret = mNetConfigs[networkType].restoreTime;
2102 }
2103 return ret;
The Android Open Source Project28527d22009-03-03 19:31:44 -08002104 }
2105
2106 @Override
2107 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07002108 if (mContext.checkCallingOrSelfPermission(
2109 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08002110 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07002111 pw.println("Permission Denial: can't dump ConnectivityService " +
2112 "from from pid=" + Binder.getCallingPid() + ", uid=" +
2113 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08002114 return;
2115 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002116 pw.println();
2117 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08002118 if (nst != null) {
2119 if (nst.getNetworkInfo().isConnected()) {
2120 pw.println("Active network: " + nst.getNetworkInfo().
2121 getTypeName());
2122 }
2123 pw.println(nst.getNetworkInfo());
2124 pw.println(nst);
2125 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07002126 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002127 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08002128
2129 pw.println("Network Requester Pids:");
2130 for (int net : mPriorityList) {
2131 String pidString = net + ": ";
2132 for (Object pid : mNetRequestersPids[net]) {
2133 pidString = pidString + pid.toString() + ", ";
2134 }
2135 pw.println(pidString);
2136 }
2137 pw.println();
2138
2139 pw.println("FeatureUsers:");
2140 for (Object requester : mFeatureUsers) {
2141 pw.println(requester.toString());
2142 }
2143 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002144
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002145 synchronized (this) {
2146 pw.println("NetworkTranstionWakeLock is currently " +
2147 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
2148 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
2149 }
2150 pw.println();
2151
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002152 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07002153
2154 if (mInetLog != null) {
2155 pw.println();
2156 pw.println("Inet condition reports:");
2157 for(int i = 0; i < mInetLog.size(); i++) {
2158 pw.println(mInetLog.get(i));
2159 }
2160 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002161 }
2162
Robert Greenwalt2034b912009-08-12 16:08:25 -07002163 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08002164 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07002165 public MyHandler(Looper looper) {
2166 super(looper);
2167 }
2168
The Android Open Source Project28527d22009-03-03 19:31:44 -08002169 @Override
2170 public void handleMessage(Message msg) {
2171 NetworkInfo info;
2172 switch (msg.what) {
2173 case NetworkStateTracker.EVENT_STATE_CHANGED:
2174 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08002175 int type = info.getType();
2176 NetworkInfo.State state = info.getState();
Robert Greenwalt12c44552009-12-07 11:33:18 -08002177
Wink Savillee70c6f52010-12-03 12:01:38 -08002178 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07002179 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08002180 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08002181
2182 // Connectivity state changed:
2183 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07002184 // [12-9] Network subtype (for mobile network, as defined
2185 // by TelephonyManager)
2186 // [8-3] Detailed state ordinal (as defined by
2187 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08002188 // [2-0] Network type (as defined by ConnectivityManager)
2189 int eventLogParam = (info.getType() & 0x7) |
2190 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
2191 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08002192 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07002193 eventLogParam);
2194
2195 if (info.getDetailedState() ==
2196 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002197 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08002198 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002199 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08002200 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002201 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07002202 // the logic here is, handle SUSPENDED the same as
2203 // DISCONNECTED. The only difference being we are
2204 // broadcasting an intent with NetworkInfo that's
2205 // suspended. This allows the applications an
2206 // opportunity to handle DISCONNECTED and SUSPENDED
2207 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08002208 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08002209 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002210 handleConnect(info);
2211 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002212 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08002213 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002214 info = (NetworkInfo) msg.obj;
Kazuhiro Ondo07680062011-06-22 21:10:34 -05002215 // TODO: Temporary allowing network configuration
2216 // change not resetting sockets.
2217 // @see bug/4455071
2218 handleConnectivityChange(info.getType(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08002219 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07002220 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002221 String causedBy = null;
2222 synchronized (ConnectivityService.this) {
2223 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
2224 mNetTransitionWakeLock.isHeld()) {
2225 mNetTransitionWakeLock.release();
2226 causedBy = mNetTransitionWakeLockCausedBy;
2227 }
2228 }
2229 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002230 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002231 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07002232 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002233 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07002234 FeatureUser u = (FeatureUser)msg.obj;
2235 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07002236 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002237 case EVENT_INET_CONDITION_CHANGE:
2238 {
2239 int netType = msg.arg1;
2240 int condition = msg.arg2;
2241 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002242 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002243 }
2244 case EVENT_INET_CONDITION_HOLD_END:
2245 {
2246 int netType = msg.arg1;
2247 int sequence = msg.arg2;
2248 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07002249 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002250 }
2251 case EVENT_SET_NETWORK_PREFERENCE:
2252 {
2253 int preference = msg.arg1;
2254 handleSetNetworkPreference(preference);
2255 break;
2256 }
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002257 case EVENT_SET_MOBILE_DATA:
2258 {
2259 boolean enabled = (msg.arg1 == ENABLED);
2260 handleSetMobileData(enabled);
2261 break;
2262 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002263 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
2264 {
2265 handleDeprecatedGlobalHttpProxy();
Robert Greenwalt34848c02011-03-25 13:09:25 -07002266 break;
2267 }
2268 case EVENT_SET_DEPENDENCY_MET:
2269 {
2270 boolean met = (msg.arg1 == ENABLED);
2271 handleSetDependencyMet(msg.arg2, met);
2272 break;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002273 }
Chia-chi Yehcc844502011-07-14 18:01:57 -07002274 case EVENT_RESTORE_DNS:
2275 {
2276 if (mActiveDefaultNetwork != -1) {
2277 handleDnsConfigurationChange(mActiveDefaultNetwork);
2278 }
2279 break;
2280 }
Wink Saville4f0de1e2011-08-04 15:01:58 -07002281 case EVENT_SEND_STICKY_BROADCAST_INTENT:
2282 {
2283 Intent intent = (Intent)msg.obj;
2284 log("EVENT_SEND_STICKY_BROADCAST_INTENT: sendStickyBroadcast intent=" + intent);
2285 sendStickyBroadcast(intent);
2286 break;
2287 }
Jeff Sharkey805662d2011-08-19 02:24:24 -07002288 case EVENT_SET_POLICY_DATA_ENABLE: {
2289 final int networkType = msg.arg1;
2290 final boolean enabled = msg.arg2 == ENABLED;
2291 handleSetPolicyDataEnable(networkType, enabled);
2292 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002293 }
2294 }
2295 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002296
2297 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002298 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002299 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002300
2301 if (isTetheringSupported()) {
2302 return mTethering.tether(iface);
2303 } else {
2304 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2305 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002306 }
2307
2308 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002309 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002310 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002311
2312 if (isTetheringSupported()) {
2313 return mTethering.untether(iface);
2314 } else {
2315 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2316 }
2317 }
2318
2319 // javadoc from interface
2320 public int getLastTetherError(String iface) {
2321 enforceTetherAccessPermission();
2322
2323 if (isTetheringSupported()) {
2324 return mTethering.getLastTetherError(iface);
2325 } else {
2326 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2327 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002328 }
2329
2330 // TODO - proper iface API for selection by property, inspection, etc
2331 public String[] getTetherableUsbRegexs() {
2332 enforceTetherAccessPermission();
2333 if (isTetheringSupported()) {
2334 return mTethering.getTetherableUsbRegexs();
2335 } else {
2336 return new String[0];
2337 }
2338 }
2339
2340 public String[] getTetherableWifiRegexs() {
2341 enforceTetherAccessPermission();
2342 if (isTetheringSupported()) {
2343 return mTethering.getTetherableWifiRegexs();
2344 } else {
2345 return new String[0];
2346 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002347 }
2348
Danica Chang96567052010-08-11 14:54:43 -07002349 public String[] getTetherableBluetoothRegexs() {
2350 enforceTetherAccessPermission();
2351 if (isTetheringSupported()) {
2352 return mTethering.getTetherableBluetoothRegexs();
2353 } else {
2354 return new String[0];
2355 }
2356 }
2357
Mike Lockwooded4a1742011-07-19 13:04:47 -07002358 public int setUsbTethering(boolean enable) {
2359 enforceTetherAccessPermission();
2360 if (isTetheringSupported()) {
2361 return mTethering.setUsbTethering(enable);
2362 } else {
2363 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2364 }
2365 }
2366
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002367 // TODO - move iface listing, queries, etc to new module
2368 // javadoc from interface
2369 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002370 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002371 return mTethering.getTetherableIfaces();
2372 }
2373
2374 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002375 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002376 return mTethering.getTetheredIfaces();
2377 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002378
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002379 public String[] getTetheringErroredIfaces() {
2380 enforceTetherAccessPermission();
2381 return mTethering.getErroredIfaces();
2382 }
2383
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002384 // if ro.tether.denied = true we default to no tethering
2385 // gservices could set the secure setting to 1 though to enable it on a build where it
2386 // had previously been turned off.
2387 public boolean isTetheringSupported() {
2388 enforceTetherAccessPermission();
2389 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08002390 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
2391 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
2392 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002393 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002394
2395 // An API NetworkStateTrackers can call when they lose their network.
2396 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
2397 // whichever happens first. The timer is started by the first caller and not
2398 // restarted by subsequent callers.
2399 public void requestNetworkTransitionWakelock(String forWhom) {
2400 enforceConnectivityInternalPermission();
2401 synchronized (this) {
2402 if (mNetTransitionWakeLock.isHeld()) return;
2403 mNetTransitionWakeLockSerialNumber++;
2404 mNetTransitionWakeLock.acquire();
2405 mNetTransitionWakeLockCausedBy = forWhom;
2406 }
2407 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07002408 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002409 mNetTransitionWakeLockSerialNumber, 0),
2410 mNetTransitionWakeLockTimeout);
2411 return;
2412 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07002413
Robert Greenwalt986c7412010-09-08 15:24:47 -07002414 // 100 percent is full good, 0 is full bad.
2415 public void reportInetCondition(int networkType, int percentage) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07002416 if (VDBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07002417 mContext.enforceCallingOrSelfPermission(
2418 android.Manifest.permission.STATUS_BAR,
2419 "ConnectivityService");
2420
Robert Greenwalt0e80be12010-09-20 14:35:25 -07002421 if (DBG) {
2422 int pid = getCallingPid();
2423 int uid = getCallingUid();
2424 String s = pid + "(" + uid + ") reports inet is " +
2425 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2426 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2427 mInetLog.add(s);
2428 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2429 mInetLog.remove(0);
2430 }
2431 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07002432 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002433 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2434 }
2435
2436 private void handleInetConditionChange(int netType, int condition) {
2437 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002438 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002439 netType + ", condition=" + condition +
2440 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2441 }
2442 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002443 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002444 return;
2445 }
2446 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002447 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002448 return;
2449 }
2450 mDefaultInetCondition = condition;
2451 int delay;
2452 if (mInetConditionChangeInFlight == false) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07002453 if (VDBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002454 // setup a new hold to debounce this
2455 if (mDefaultInetCondition > 50) {
2456 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2457 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2458 } else {
2459 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2460 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2461 }
2462 mInetConditionChangeInFlight = true;
2463 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2464 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2465 } else {
2466 // we've set the new condition, when this hold ends that will get
2467 // picked up
Robert Greenwalt78f28112011-08-02 17:18:41 -07002468 if (VDBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002469 }
2470 }
2471
2472 private void handleInetConditionHoldEnd(int netType, int sequence) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07002473 if (VDBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002474 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002475 ", condition =" + mDefaultInetCondition +
2476 ", published condition =" + mDefaultInetConditionPublished);
2477 }
2478 mInetConditionChangeInFlight = false;
2479
2480 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002481 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002482 return;
2483 }
2484 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002485 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002486 return;
2487 }
Wink Saville4f0de1e2011-08-04 15:01:58 -07002488 // TODO: Figure out why this optimization sometimes causes a
2489 // change in mDefaultInetCondition to be missed and the
2490 // UI to not be updated.
2491 //if (mDefaultInetConditionPublished == mDefaultInetCondition) {
2492 // if (DBG) log("no change in condition - aborting");
2493 // return;
2494 //}
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002495 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2496 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002497 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002498 return;
2499 }
2500 mDefaultInetConditionPublished = mDefaultInetCondition;
2501 sendInetConditionBroadcast(networkInfo);
2502 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002503 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002504
2505 public synchronized ProxyProperties getProxy() {
2506 if (mGlobalProxy != null) return mGlobalProxy;
2507 if (mDefaultProxy != null) return mDefaultProxy;
2508 return null;
2509 }
2510
2511 public void setGlobalProxy(ProxyProperties proxyProperties) {
2512 enforceChangePermission();
2513 synchronized (mGlobalProxyLock) {
2514 if (proxyProperties == mGlobalProxy) return;
2515 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2516 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2517
2518 String host = "";
2519 int port = 0;
2520 String exclList = "";
2521 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2522 mGlobalProxy = new ProxyProperties(proxyProperties);
2523 host = mGlobalProxy.getHost();
2524 port = mGlobalProxy.getPort();
2525 exclList = mGlobalProxy.getExclusionList();
2526 } else {
2527 mGlobalProxy = null;
2528 }
2529 ContentResolver res = mContext.getContentResolver();
2530 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2531 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002532 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002533 exclList);
2534 }
2535
2536 if (mGlobalProxy == null) {
2537 proxyProperties = mDefaultProxy;
2538 }
2539 sendProxyBroadcast(proxyProperties);
2540 }
2541
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002542 private void loadGlobalProxy() {
2543 ContentResolver res = mContext.getContentResolver();
2544 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2545 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2546 String exclList = Settings.Secure.getString(res,
2547 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2548 if (!TextUtils.isEmpty(host)) {
2549 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2550 synchronized (mGlobalProxyLock) {
2551 mGlobalProxy = proxyProperties;
2552 }
2553 }
2554 }
2555
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002556 public ProxyProperties getGlobalProxy() {
2557 synchronized (mGlobalProxyLock) {
2558 return mGlobalProxy;
2559 }
2560 }
2561
2562 private void handleApplyDefaultProxy(int type) {
2563 // check if new default - push it out to all VM if so
2564 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2565 synchronized (this) {
2566 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2567 if (mDefaultProxy == proxy) return;
Robert Greenwalt027f5052011-07-29 10:03:37 -07002568 if (proxy != null && !TextUtils.isEmpty(proxy.getHost())) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002569 mDefaultProxy = proxy;
2570 } else {
2571 mDefaultProxy = null;
2572 }
2573 }
Robert Greenwalt78f28112011-08-02 17:18:41 -07002574 if (VDBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002575 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2576 if (mGlobalProxy != null) return;
2577 sendProxyBroadcast(proxy);
2578 }
2579
2580 private void handleDeprecatedGlobalHttpProxy() {
2581 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2582 Settings.Secure.HTTP_PROXY);
2583 if (!TextUtils.isEmpty(proxy)) {
2584 String data[] = proxy.split(":");
2585 String proxyHost = data[0];
2586 int proxyPort = 8080;
2587 if (data.length > 1) {
2588 try {
2589 proxyPort = Integer.parseInt(data[1]);
2590 } catch (NumberFormatException e) {
2591 return;
2592 }
2593 }
2594 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2595 setGlobalProxy(p);
2596 }
2597 }
2598
2599 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt611291c2010-12-23 15:51:10 -08002600 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Robert Greenwalt78f28112011-08-02 17:18:41 -07002601 if (DBG) log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002602 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnutt1f2a2ac2011-01-06 11:00:19 -08002603 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2604 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002605 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002606 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002607 }
2608
2609 private static class SettingsObserver extends ContentObserver {
2610 private int mWhat;
2611 private Handler mHandler;
2612 SettingsObserver(Handler handler, int what) {
2613 super(handler);
2614 mHandler = handler;
2615 mWhat = what;
2616 }
2617
2618 void observe(Context context) {
2619 ContentResolver resolver = context.getContentResolver();
2620 resolver.registerContentObserver(Settings.Secure.getUriFor(
2621 Settings.Secure.HTTP_PROXY), false, this);
2622 }
2623
2624 @Override
2625 public void onChange(boolean selfChange) {
2626 mHandler.obtainMessage(mWhat).sendToTarget();
2627 }
2628 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002629
2630 private void log(String s) {
2631 Slog.d(TAG, s);
2632 }
2633
2634 private void loge(String s) {
2635 Slog.e(TAG, s);
2636 }
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002637
repo syncf5de5572011-07-29 23:55:49 -07002638 int convertFeatureToNetworkType(int networkType, String feature) {
2639 int usedNetworkType = networkType;
2640
2641 if(networkType == ConnectivityManager.TYPE_MOBILE) {
2642 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2643 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
2644 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2645 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2646 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2647 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2648 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
2649 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2650 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2651 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2652 usedNetworkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2653 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2654 usedNetworkType = ConnectivityManager.TYPE_MOBILE_IMS;
2655 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2656 usedNetworkType = ConnectivityManager.TYPE_MOBILE_CBS;
2657 } else {
2658 Slog.e(TAG, "Can't match any mobile netTracker!");
2659 }
2660 } else if (networkType == ConnectivityManager.TYPE_WIFI) {
2661 if (TextUtils.equals(feature, "p2p")) {
2662 usedNetworkType = ConnectivityManager.TYPE_WIFI_P2P;
2663 } else {
2664 Slog.e(TAG, "Can't match any wifi netTracker!");
2665 }
2666 } else {
2667 Slog.e(TAG, "Unexpected network type");
Wink Savillef6b76692011-02-24 17:58:51 -08002668 }
repo syncf5de5572011-07-29 23:55:49 -07002669 return usedNetworkType;
Wink Savillef6b76692011-02-24 17:58:51 -08002670 }
Jeff Sharkey921ebf22011-05-19 17:12:49 -07002671
2672 private static <T> T checkNotNull(T value, String message) {
2673 if (value == null) {
2674 throw new NullPointerException(message);
2675 }
2676 return value;
2677 }
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002678
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002679 /**
2680 * Protect a socket from VPN routing rules. This method is used by
Chia-chi Yehbded3eb2011-07-04 03:23:12 -07002681 * VpnBuilder and not available in ConnectivityManager. Permissions
2682 * are checked in Vpn class.
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002683 * @hide
2684 */
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002685 @Override
Chia-chi Yeh9e4ff6e2011-07-14 16:19:19 -07002686 public boolean protectVpn(ParcelFileDescriptor socket) {
2687 try {
2688 int type = mActiveDefaultNetwork;
2689 if (ConnectivityManager.isNetworkTypeValid(type)) {
2690 mVpn.protect(socket, mNetTrackers[type].getLinkProperties().getInterfaceName());
2691 return true;
2692 }
2693 } catch (Exception e) {
2694 // ignore
2695 } finally {
2696 try {
2697 socket.close();
2698 } catch (Exception e) {
2699 // ignore
2700 }
2701 }
2702 return false;
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002703 }
2704
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002705 /**
2706 * Prepare for a VPN application. This method is used by VpnDialogs
Chia-chi Yehbded3eb2011-07-04 03:23:12 -07002707 * and not available in ConnectivityManager. Permissions are checked
2708 * in Vpn class.
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002709 * @hide
2710 */
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002711 @Override
Chia-chi Yeh3e2e1da2011-07-03 16:52:38 -07002712 public boolean prepareVpn(String oldPackage, String newPackage) {
2713 return mVpn.prepare(oldPackage, newPackage);
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002714 }
2715
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002716 /**
2717 * Configure a TUN interface and return its file descriptor. Parameters
2718 * are encoded and opaque to this class. This method is used by VpnBuilder
Chia-chi Yehbded3eb2011-07-04 03:23:12 -07002719 * and not available in ConnectivityManager. Permissions are checked in
2720 * Vpn class.
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002721 * @hide
2722 */
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002723 @Override
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002724 public ParcelFileDescriptor establishVpn(VpnConfig config) {
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002725 return mVpn.establish(config);
2726 }
2727
Chia-chi Yeh7e026b32011-07-02 17:15:00 -07002728 /**
Chia-chi Yehbded3eb2011-07-04 03:23:12 -07002729 * Start legacy VPN and return an intent to VpnDialogs. This method is
2730 * used by VpnSettings and not available in ConnectivityManager.
2731 * Permissions are checked in Vpn class.
Chia-chi Yeh7e026b32011-07-02 17:15:00 -07002732 * @hide
2733 */
2734 @Override
Chia-chi Yehbded3eb2011-07-04 03:23:12 -07002735 public void startLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) {
2736 mVpn.startLegacyVpn(config, racoon, mtpd);
2737 }
2738
2739 /**
2740 * Return the information of the ongoing legacy VPN. This method is used
2741 * by VpnSettings and not available in ConnectivityManager. Permissions
2742 * are checked in Vpn class.
2743 * @hide
2744 */
2745 @Override
2746 public LegacyVpnInfo getLegacyVpnInfo() {
2747 return mVpn.getLegacyVpnInfo();
Chia-chi Yeh7e026b32011-07-02 17:15:00 -07002748 }
2749
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002750 /**
2751 * Callback for VPN subsystem. Currently VPN is not adapted to the service
2752 * through NetworkStateTracker since it works differently. For example, it
2753 * needs to override DNS servers but never takes the default routes. It
2754 * relies on another data network, and it could keep existing connections
2755 * alive after reconnecting, switching between networks, or even resuming
2756 * from deep sleep. Calls from applications should be done synchronously
2757 * to avoid race conditions. As these are all hidden APIs, refactoring can
2758 * be done whenever a better abstraction is developed.
2759 */
2760 public class VpnCallback {
2761
2762 private VpnCallback() {
2763 }
2764
Chia-chi Yehcc844502011-07-14 18:01:57 -07002765 public void override(List<String> dnsServers, List<String> searchDomains) {
2766 if (dnsServers == null) {
2767 restore();
2768 return;
2769 }
2770
2771 // Convert DNS servers into addresses.
2772 List<InetAddress> addresses = new ArrayList<InetAddress>();
2773 for (String address : dnsServers) {
2774 // Double check the addresses and remove invalid ones.
2775 try {
2776 addresses.add(InetAddress.parseNumericAddress(address));
2777 } catch (Exception e) {
2778 // ignore
2779 }
2780 }
2781 if (addresses.isEmpty()) {
2782 restore();
2783 return;
2784 }
2785
2786 // Concatenate search domains into a string.
2787 StringBuilder buffer = new StringBuilder();
2788 if (searchDomains != null) {
2789 for (String domain : searchDomains) {
2790 buffer.append(domain).append(' ');
2791 }
2792 }
2793 String domains = buffer.toString().trim();
2794
2795 // Apply DNS changes.
2796 boolean changed = false;
2797 synchronized (mDnsLock) {
Robert Greenwaltfce71862011-07-25 16:06:25 -07002798 changed = updateDns("VPN", "VPN", addresses, domains);
Chia-chi Yehcc844502011-07-14 18:01:57 -07002799 mDnsOverridden = true;
2800 }
2801 if (changed) {
2802 bumpDns();
2803 }
2804
2805 // TODO: temporarily remove http proxy?
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002806 }
2807
Chia-chi Yehcc844502011-07-14 18:01:57 -07002808 public void restore() {
2809 synchronized (mDnsLock) {
2810 if (!mDnsOverridden) {
2811 return;
2812 }
2813 mDnsOverridden = false;
2814 }
2815 mHandler.sendEmptyMessage(EVENT_RESTORE_DNS);
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002816 }
2817 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002818}