blob: 22acfbf03d13957de68dfc0dbfdb0aba0b22f54c [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;
Haoyu Baib5da5752012-06-20 14:29:57 -070020import static android.Manifest.permission.RECEIVE_DATA_ACTIVITY_CHANGE;
Jeff Sharkey971cd162011-08-29 16:02:57 -070021import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
22import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070023import static android.net.ConnectivityManager.isNetworkTypeValid;
24import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL;
Jeff Sharkeya47d7a12011-06-16 15:07:48 -070025import static android.net.NetworkPolicyManager.RULE_REJECT_METERED;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070026
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080027import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080028import android.content.ContentResolver;
29import android.content.Context;
tk.mun093f55c2011-10-13 22:51:57 +090030import android.content.ContextWrapper;
The Android Open Source Project28527d22009-03-03 19:31:44 -080031import android.content.Intent;
32import android.content.pm.PackageManager;
tk.mun093f55c2011-10-13 22:51:57 +090033import android.content.res.Resources;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070034import android.database.ContentObserver;
The Android Open Source Project28527d22009-03-03 19:31:44 -080035import android.net.ConnectivityManager;
Robert Greenwalteb123ac2010-12-06 13:56:24 -080036import android.net.DummyDataStateTracker;
Benoit Goby211b5692010-12-22 14:29:40 -080037import android.net.EthernetDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080038import android.net.IConnectivityManager;
Haoyu Baib5da5752012-06-20 14:29:57 -070039import android.net.INetworkManagementEventObserver;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070040import android.net.INetworkPolicyListener;
41import android.net.INetworkPolicyManager;
Jeff Sharkeyb6188a12011-09-22 14:59:51 -070042import android.net.INetworkStatsService;
Wink Savilledc5d1ba2011-07-14 12:23:28 -070043import android.net.LinkAddress;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080044import android.net.LinkProperties;
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -070045import android.net.LinkProperties.CompareResult;
The Android Open Source Project28527d22009-03-03 19:31:44 -080046import android.net.MobileDataStateTracker;
Robert Greenwalt34848c02011-03-25 13:09:25 -070047import android.net.NetworkConfig;
The Android Open Source Project28527d22009-03-03 19:31:44 -080048import android.net.NetworkInfo;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070049import android.net.NetworkInfo.DetailedState;
Jeff Sharkey66fa9682011-08-02 17:22:34 -070050import android.net.NetworkQuotaInfo;
Jeff Sharkey21062e72011-05-28 20:56:34 -070051import android.net.NetworkState;
The Android Open Source Project28527d22009-03-03 19:31:44 -080052import android.net.NetworkStateTracker;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070053import android.net.NetworkUtils;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070054import android.net.Proxy;
55import android.net.ProxyProperties;
Robert Greenwalt5a901292011-04-28 14:28:50 -070056import android.net.RouteInfo;
The Android Open Source Project28527d22009-03-03 19:31:44 -080057import android.net.wifi.WifiStateTracker;
tk.mun093f55c2011-10-13 22:51:57 +090058import android.net.wimax.WimaxManagerConstants;
The Android Open Source Project28527d22009-03-03 19:31:44 -080059import android.os.Binder;
Mike Lockwood0d5916c2011-05-28 13:24:04 -040060import android.os.FileUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080061import android.os.Handler;
Wink Saville775aad62010-09-02 19:23:52 -070062import android.os.HandlerThread;
Robert Greenwalt2034b912009-08-12 16:08:25 -070063import android.os.IBinder;
Chia-chi Yeh4df51322011-05-11 16:35:13 -070064import android.os.INetworkManagementService;
The Android Open Source Project28527d22009-03-03 19:31:44 -080065import android.os.Looper;
66import android.os.Message;
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -070067import android.os.ParcelFileDescriptor;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070068import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070069import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080070import android.os.ServiceManager;
Robert Greenwalt1e6991e2012-05-22 16:07:46 -070071import android.os.SystemClock;
The Android Open Source Project28527d22009-03-03 19:31:44 -080072import android.os.SystemProperties;
73import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070074import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080075import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080076import android.util.Slog;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070077import android.util.SparseIntArray;
The Android Open Source Project28527d22009-03-03 19:31:44 -080078
Chia-chi Yehbded3eb2011-07-04 03:23:12 -070079import com.android.internal.net.LegacyVpnInfo;
Chia-chi Yeh75cacd52011-06-15 17:07:27 -070080import com.android.internal.net.VpnConfig;
Robert Greenwalt2034b912009-08-12 16:08:25 -070081import com.android.internal.telephony.Phone;
Wink Saville64e3f782012-07-10 12:37:54 -070082import com.android.internal.telephony.PhoneConstants;
Jeff Sharkeyaac2c502011-10-04 16:54:49 -070083import com.android.server.am.BatteryStatsService;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080084import com.android.server.connectivity.Tethering;
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -070085import com.android.server.connectivity.Vpn;
Jeff Sharkey21062e72011-05-28 20:56:34 -070086import com.google.android.collect.Lists;
Jeff Sharkeya47d7a12011-06-16 15:07:48 -070087import com.google.android.collect.Sets;
tk.mun093f55c2011-10-13 22:51:57 +090088import dalvik.system.DexClassLoader;
The Android Open Source Project28527d22009-03-03 19:31:44 -080089import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070090import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080091import java.io.PrintWriter;
tk.mun093f55c2011-10-13 22:51:57 +090092import java.lang.reflect.Constructor;
93import java.lang.reflect.Method;
94import java.lang.reflect.Modifier;
95import java.lang.reflect.InvocationTargetException;
Wink Savilledc5d1ba2011-07-14 12:23:28 -070096import java.net.Inet4Address;
Wink Saville051a6642011-07-13 13:44:13 -070097import java.net.Inet6Address;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070098import java.net.InetAddress;
99import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700100import java.util.ArrayList;
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700101import java.util.Arrays;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700102import java.util.Collection;
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700103import java.util.GregorianCalendar;
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700104import java.util.HashSet;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700105import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800106
107/**
108 * @hide
109 */
110public class ConnectivityService extends IConnectivityManager.Stub {
111
Robert Greenwalt063dc7d2010-10-05 19:12:26 -0700112 private static final boolean DBG = true;
Wink Savillea7d56572011-09-21 11:05:43 -0700113 private static final boolean VDBG = false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800114 private static final String TAG = "ConnectivityService";
115
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700116 private static final boolean LOGD_RULES = false;
117
Robert Greenwalt2034b912009-08-12 16:08:25 -0700118 // how long to wait before switching back to a radio's default network
119 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
120 // system property that can override the above value
121 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
122 "android.telephony.apn-restore";
123
Robert Greenwaltbd492212011-05-06 17:10:53 -0700124 // used in recursive route setting to add gateways for the host for which
125 // a host route was requested.
126 private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10;
127
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800128 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800129 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800130
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -0700131 private Vpn mVpn;
132
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700133 /** Lock around {@link #mUidRules} and {@link #mMeteredIfaces}. */
134 private Object mRulesLock = new Object();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700135 /** Currently active network rules by UID. */
136 private SparseIntArray mUidRules = new SparseIntArray();
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700137 /** Set of ifaces that are costly. */
138 private HashSet<String> mMeteredIfaces = Sets.newHashSet();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700139
The Android Open Source Project28527d22009-03-03 19:31:44 -0800140 /**
141 * Sometimes we want to refer to the individual network state
142 * trackers separately, and sometimes we just want to treat them
143 * abstractly.
144 */
145 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -0700146
147 /**
Wink Saville051a6642011-07-13 13:44:13 -0700148 * The link properties that define the current links
149 */
150 private LinkProperties mCurrentLinkProperties[];
151
152 /**
Robert Greenwalt2034b912009-08-12 16:08:25 -0700153 * A per Net list of the PID's that requested access to the net
154 * used both as a refcount and for per-PID DNS selection
155 */
156 private List mNetRequestersPids[];
157
Robert Greenwalt2034b912009-08-12 16:08:25 -0700158 // priority order of the nettrackers
159 // (excluding dynamically set mNetworkPreference)
160 // TODO - move mNetworkTypePreference into this
161 private int[] mPriorityList;
162
The Android Open Source Project28527d22009-03-03 19:31:44 -0800163 private Context mContext;
164 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700165 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -0700166 // 0 is full bad, 100 is full good
167 private int mDefaultInetCondition = 0;
168 private int mDefaultInetConditionPublished = 0;
169 private boolean mInetConditionChangeInFlight = false;
170 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800171
Chia-chi Yehcc844502011-07-14 18:01:57 -0700172 private Object mDnsLock = new Object();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800173 private int mNumDnsEntries;
Chia-chi Yehcc844502011-07-14 18:01:57 -0700174 private boolean mDnsOverridden = false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800175
176 private boolean mTestMode;
Joe Onorato56023ad2010-09-01 21:18:22 -0700177 private static ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800178
Robert Greenwalt355205c2011-05-10 15:05:02 -0700179 private INetworkManagementService mNetd;
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700180 private INetworkPolicyManager mPolicyManager;
Robert Greenwalt355205c2011-05-10 15:05:02 -0700181
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700182 private static final int ENABLED = 1;
183 private static final int DISABLED = 0;
184
Robert Greenwalt49c75d32011-11-02 14:37:19 -0700185 private static final boolean ADD = true;
186 private static final boolean REMOVE = false;
187
188 private static final boolean TO_DEFAULT_TABLE = true;
189 private static final boolean TO_SECONDARY_TABLE = false;
190
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700191 /**
192 * used internally as a delayed event to make us switch back to the
193 * default network
194 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700195 private static final int EVENT_RESTORE_DEFAULT_NETWORK = 1;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700196
197 /**
198 * used internally to change our mobile data enabled flag
199 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700200 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED = 2;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700201
202 /**
203 * used internally to change our network preference setting
204 * arg1 = networkType to prefer
205 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700206 private static final int EVENT_SET_NETWORK_PREFERENCE = 3;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700207
208 /**
209 * used internally to synchronize inet condition reports
210 * arg1 = networkType
211 * arg2 = condition (0 bad, 100 good)
212 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700213 private static final int EVENT_INET_CONDITION_CHANGE = 4;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700214
215 /**
216 * used internally to mark the end of inet condition hold periods
217 * arg1 = networkType
218 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700219 private static final int EVENT_INET_CONDITION_HOLD_END = 5;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700220
221 /**
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700222 * used internally to set enable/disable cellular data
223 * arg1 = ENBALED or DISABLED
224 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700225 private static final int EVENT_SET_MOBILE_DATA = 7;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700226
Robert Greenwaltccb36f92010-09-24 14:32:21 -0700227 /**
228 * used internally to clear a wakelock when transitioning
229 * from one net to another
230 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700231 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK = 8;
Robert Greenwaltccb36f92010-09-24 14:32:21 -0700232
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700233 /**
234 * used internally to reload global proxy settings
235 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700236 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY = 9;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700237
Robert Greenwalt34848c02011-03-25 13:09:25 -0700238 /**
239 * used internally to set external dependency met/unmet
240 * arg1 = ENABLED (met) or DISABLED (unmet)
241 * arg2 = NetworkType
242 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700243 private static final int EVENT_SET_DEPENDENCY_MET = 10;
Robert Greenwalt34848c02011-03-25 13:09:25 -0700244
Chia-chi Yehcc844502011-07-14 18:01:57 -0700245 /**
246 * used internally to restore DNS properties back to the
247 * default network
248 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700249 private static final int EVENT_RESTORE_DNS = 11;
Chia-chi Yehcc844502011-07-14 18:01:57 -0700250
Wink Saville7e4333c2011-08-05 11:40:22 -0700251 /**
Wink Saville4f0de1e2011-08-04 15:01:58 -0700252 * used internally to send a sticky broadcast delayed.
253 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700254 private static final int EVENT_SEND_STICKY_BROADCAST_INTENT = 12;
Wink Saville4f0de1e2011-08-04 15:01:58 -0700255
Jeff Sharkey805662d2011-08-19 02:24:24 -0700256 /**
257 * Used internally to
258 * {@link NetworkStateTracker#setPolicyDataEnable(boolean)}.
259 */
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700260 private static final int EVENT_SET_POLICY_DATA_ENABLE = 13;
Jeff Sharkey805662d2011-08-19 02:24:24 -0700261
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700262 /** Handler used for internal events. */
263 private InternalHandler mHandler;
264 /** Handler used for incoming {@link NetworkStateTracker} events. */
265 private NetworkStateTrackerHandler mTrackerHandler;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700266
267 // list of DeathRecipients used to make sure features are turned off when
268 // a process dies
Kazuhiro Ondocae18f12011-07-19 11:23:37 -0500269 private List<FeatureUser> mFeatureUsers;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700270
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400271 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800272 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400273
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700274 private PowerManager.WakeLock mNetTransitionWakeLock;
275 private String mNetTransitionWakeLockCausedBy = "";
276 private int mNetTransitionWakeLockSerialNumber;
277 private int mNetTransitionWakeLockTimeout;
278
Robert Greenwalt94daa182010-09-01 11:34:05 -0700279 private InetAddress mDefaultDns;
280
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -0700281 // this collection is used to refcount the added routes - if there are none left
282 // it's time to remove the route from the route table
283 private Collection<RouteInfo> mAddedRoutes = new ArrayList<RouteInfo>();
284
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700285 // used in DBG mode to track inet condition reports
286 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
287 private ArrayList mInetLog;
288
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700289 // track the current default http proxy - tell the world if we get a new one (real change)
290 private ProxyProperties mDefaultProxy = null;
Chia-chi Yeh763a11c2011-10-03 15:34:04 -0700291 private Object mDefaultProxyLock = new Object();
292 private boolean mDefaultProxyDisabled = false;
293
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700294 // track the global proxy.
295 private ProxyProperties mGlobalProxy = null;
296 private final Object mGlobalProxyLock = new Object();
297
298 private SettingsObserver mSettingsObserver;
299
Robert Greenwalt34848c02011-03-25 13:09:25 -0700300 NetworkConfig[] mNetConfigs;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700301 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700302
Robert Greenwalt12c44552009-12-07 11:33:18 -0800303 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700304 public int mSimultaneity;
305 public int mType;
306 public RadioAttributes(String init) {
307 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700308 mType = Integer.parseInt(fragments[0]);
309 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700310 }
311 }
312 RadioAttributes[] mRadioAttributes;
313
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700314 // the set of network types that can only be enabled by system/sig apps
315 List mProtectedNetworks;
316
Jeff Sharkeyb6188a12011-09-22 14:59:51 -0700317 public ConnectivityService(Context context, INetworkManagementService netd,
318 INetworkStatsService statsService, INetworkPolicyManager policyManager) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800319 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800320
Wink Saville775aad62010-09-02 19:23:52 -0700321 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
322 handlerThread.start();
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700323 mHandler = new InternalHandler(handlerThread.getLooper());
324 mTrackerHandler = new NetworkStateTrackerHandler(handlerThread.getLooper());
Wink Saville775aad62010-09-02 19:23:52 -0700325
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800326 // setup our unique device name
Robert Greenwalt82cde132010-12-06 09:30:17 -0800327 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
328 String id = Settings.Secure.getString(context.getContentResolver(),
329 Settings.Secure.ANDROID_ID);
330 if (id != null && id.length() > 0) {
Irfan Sheriff4aa0b2e2011-09-20 15:17:07 -0700331 String name = new String("android-").concat(id);
Robert Greenwalt82cde132010-12-06 09:30:17 -0800332 SystemProperties.set("net.hostname", name);
333 }
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800334 }
335
Robert Greenwalt94daa182010-09-01 11:34:05 -0700336 // read our default dns server ip
337 String dns = Settings.Secure.getString(context.getContentResolver(),
338 Settings.Secure.DEFAULT_DNS_SERVER);
339 if (dns == null || dns.length() == 0) {
340 dns = context.getResources().getString(
341 com.android.internal.R.string.config_default_dns_server);
342 }
343 try {
Robert Greenwalt35e34d12011-02-22 16:00:42 -0800344 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
345 } catch (IllegalArgumentException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800346 loge("Error setting defaultDns using " + dns);
Robert Greenwalt94daa182010-09-01 11:34:05 -0700347 }
348
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700349 mContext = checkNotNull(context, "missing Context");
350 mNetd = checkNotNull(netd, "missing INetworkManagementService");
351 mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager");
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700352
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700353 try {
354 mPolicyManager.registerListener(mPolicyListener);
355 } catch (RemoteException e) {
356 // ouch, no rules updates means some processes may never get network
Robert Greenwalt78f28112011-08-02 17:18:41 -0700357 loge("unable to register INetworkPolicyListener" + e.toString());
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700358 }
359
360 final PowerManager powerManager = (PowerManager) context.getSystemService(
361 Context.POWER_SERVICE);
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700362 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
363 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
364 com.android.internal.R.integer.config_networkTransitionTimeout);
365
Robert Greenwalt2034b912009-08-12 16:08:25 -0700366 mNetTrackers = new NetworkStateTracker[
367 ConnectivityManager.MAX_NETWORK_TYPE+1];
Wink Saville051a6642011-07-13 13:44:13 -0700368 mCurrentLinkProperties = new LinkProperties[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt0659da32009-07-16 17:21:39 -0700369
The Android Open Source Project28527d22009-03-03 19:31:44 -0800370 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700371
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700372 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwalt34848c02011-03-25 13:09:25 -0700373 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700374
Robert Greenwalt2034b912009-08-12 16:08:25 -0700375 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700376 String[] raStrings = context.getResources().getStringArray(
377 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700378 for (String raString : raStrings) {
379 RadioAttributes r = new RadioAttributes(raString);
380 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800381 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700382 continue;
383 }
384 if (mRadioAttributes[r.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800385 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700386 r.mType);
387 continue;
388 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700389 mRadioAttributes[r.mType] = r;
390 }
391
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700392 String[] naStrings = context.getResources().getStringArray(
393 com.android.internal.R.array.networkAttributes);
394 for (String naString : naStrings) {
395 try {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700396 NetworkConfig n = new NetworkConfig(naString);
Wink Savillef2a62832011-04-07 14:23:45 -0700397 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800398 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700399 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700400 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700401 }
Wink Savillef2a62832011-04-07 14:23:45 -0700402 if (mNetConfigs[n.type] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800403 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700404 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700405 continue;
406 }
Wink Savillef2a62832011-04-07 14:23:45 -0700407 if (mRadioAttributes[n.radio] == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800408 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Savillef2a62832011-04-07 14:23:45 -0700409 "radio " + n.radio + " in network type " + n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700410 continue;
411 }
Wink Savillef2a62832011-04-07 14:23:45 -0700412 mNetConfigs[n.type] = n;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700413 mNetworksDefined++;
414 } catch(Exception e) {
415 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700416 }
417 }
418
Robert Greenwalt6cac0742011-06-21 17:26:14 -0700419 mProtectedNetworks = new ArrayList<Integer>();
420 int[] protectedNetworks = context.getResources().getIntArray(
421 com.android.internal.R.array.config_protectedNetworks);
422 for (int p : protectedNetworks) {
423 if ((mNetConfigs[p] != null) && (mProtectedNetworks.contains(p) == false)) {
424 mProtectedNetworks.add(p);
425 } else {
426 if (DBG) loge("Ignoring protectedNetwork " + p);
427 }
428 }
429
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700430 // high priority first
431 mPriorityList = new int[mNetworksDefined];
432 {
433 int insertionPoint = mNetworksDefined-1;
434 int currentLowest = 0;
435 int nextLowest = 0;
436 while (insertionPoint > -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700437 for (NetworkConfig na : mNetConfigs) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700438 if (na == null) continue;
Wink Savillef2a62832011-04-07 14:23:45 -0700439 if (na.priority < currentLowest) continue;
440 if (na.priority > currentLowest) {
441 if (na.priority < nextLowest || nextLowest == 0) {
442 nextLowest = na.priority;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700443 }
444 continue;
445 }
Wink Savillef2a62832011-04-07 14:23:45 -0700446 mPriorityList[insertionPoint--] = na.type;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700447 }
448 currentLowest = nextLowest;
449 nextLowest = 0;
450 }
451 }
452
453 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
454 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700455 mNetRequestersPids[i] = new ArrayList();
456 }
457
Kazuhiro Ondocae18f12011-07-19 11:23:37 -0500458 mFeatureUsers = new ArrayList<FeatureUser>();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700459
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700460 mNumDnsEntries = 0;
461
462 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
463 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800464 /*
465 * Create the network state trackers for Wi-Fi and mobile
466 * data. Maybe this could be done with a factory class,
467 * but it's not clear that it's worth it, given that
468 * the number of different network types is not going
469 * to change very often.
470 */
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700471 for (int netType : mPriorityList) {
Wink Savillef2a62832011-04-07 14:23:45 -0700472 switch (mNetConfigs[netType].radio) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700473 case ConnectivityManager.TYPE_WIFI:
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700474 mNetTrackers[netType] = new WifiStateTracker(
475 netType, mNetConfigs[netType].name);
476 mNetTrackers[netType].startMonitoring(context, mTrackerHandler);
477 break;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700478 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700479 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700480 mNetConfigs[netType].name);
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700481 mNetTrackers[netType].startMonitoring(context, mTrackerHandler);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700482 break;
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800483 case ConnectivityManager.TYPE_DUMMY:
484 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700485 mNetConfigs[netType].name);
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700486 mNetTrackers[netType].startMonitoring(context, mTrackerHandler);
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800487 break;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800488 case ConnectivityManager.TYPE_BLUETOOTH:
489 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700490 mNetTrackers[netType].startMonitoring(context, mTrackerHandler);
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800491 break;
tk.mun093f55c2011-10-13 22:51:57 +0900492 case ConnectivityManager.TYPE_WIMAX:
493 mNetTrackers[netType] = makeWimaxStateTracker();
Robert Greenwalte72c4ac2011-11-08 10:12:25 -0800494 if (mNetTrackers[netType]!= null) {
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700495 mNetTrackers[netType].startMonitoring(context, mTrackerHandler);
tk.mun093f55c2011-10-13 22:51:57 +0900496 }
497 break;
Benoit Goby211b5692010-12-22 14:29:40 -0800498 case ConnectivityManager.TYPE_ETHERNET:
499 mNetTrackers[netType] = EthernetDataTracker.getInstance();
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700500 mNetTrackers[netType].startMonitoring(context, mTrackerHandler);
Benoit Goby211b5692010-12-22 14:29:40 -0800501 break;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700502 default:
Wink Savillee70c6f52010-12-03 12:01:38 -0800503 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700504 mNetConfigs[netType].radio);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700505 continue;
506 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -0700507 mCurrentLinkProperties[netType] = null;
Robert Greenwaltcafd8582011-11-10 16:55:20 -0800508 if (mNetTrackers[netType] != null && mNetConfigs[netType].isDefault()) {
509 mNetTrackers[netType].reconnect();
510 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700511 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800512
Chia-chi Yeh4df51322011-05-11 16:35:13 -0700513 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
514 INetworkManagementService nmService = INetworkManagementService.Stub.asInterface(b);
515
Robert Greenwalt49c75d32011-11-02 14:37:19 -0700516 mTethering = new Tethering(mContext, nmService, statsService, this, mHandler.getLooper());
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700517 mTetheringConfigValid = ((mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700518 mTethering.getTetherableWifiRegexs().length != 0 ||
519 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwalt33cdcdf2011-06-02 17:30:47 -0700520 mTethering.getUpstreamIfaceTypes().length != 0);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800521
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -0700522 mVpn = new Vpn(mContext, new VpnCallback());
523
Chia-chi Yehf3204aa2011-05-23 15:08:29 -0700524 try {
525 nmService.registerObserver(mTethering);
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -0700526 nmService.registerObserver(mVpn);
Chia-chi Yehf3204aa2011-05-23 15:08:29 -0700527 } catch (RemoteException e) {
528 loge("Error registering observer :" + e);
529 }
530
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700531 if (DBG) {
532 mInetLog = new ArrayList();
533 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700534
535 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
536 mSettingsObserver.observe(mContext);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800537
Haoyu Baib5da5752012-06-20 14:29:57 -0700538 INetworkManagementEventObserver netdObserver = new NetdObserver();
539 try {
540 mNetd.registerObserver(netdObserver);
541 } catch (RemoteException e) {
542 loge("Error registering observer :" + e);
543 }
544
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800545 loadGlobalProxy();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800546 }
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700547
548 private NetworkStateTracker makeWimaxStateTracker() {
549 // Initialize Wimax
tk.mun093f55c2011-10-13 22:51:57 +0900550 DexClassLoader wimaxClassLoader;
551 Class wimaxStateTrackerClass = null;
552 Class wimaxServiceClass = null;
553 Class wimaxManagerClass;
554 String wimaxJarLocation;
555 String wimaxLibLocation;
556 String wimaxManagerClassName;
557 String wimaxServiceClassName;
558 String wimaxStateTrackerClassName;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800559
tk.mun093f55c2011-10-13 22:51:57 +0900560 NetworkStateTracker wimaxStateTracker = null;
561
562 boolean isWimaxEnabled = mContext.getResources().getBoolean(
563 com.android.internal.R.bool.config_wimaxEnabled);
564
565 if (isWimaxEnabled) {
566 try {
567 wimaxJarLocation = mContext.getResources().getString(
568 com.android.internal.R.string.config_wimaxServiceJarLocation);
569 wimaxLibLocation = mContext.getResources().getString(
570 com.android.internal.R.string.config_wimaxNativeLibLocation);
571 wimaxManagerClassName = mContext.getResources().getString(
572 com.android.internal.R.string.config_wimaxManagerClassname);
573 wimaxServiceClassName = mContext.getResources().getString(
574 com.android.internal.R.string.config_wimaxServiceClassname);
575 wimaxStateTrackerClassName = mContext.getResources().getString(
576 com.android.internal.R.string.config_wimaxStateTrackerClassname);
577
578 log("wimaxJarLocation: " + wimaxJarLocation);
579 wimaxClassLoader = new DexClassLoader(wimaxJarLocation,
580 new ContextWrapper(mContext).getCacheDir().getAbsolutePath(),
581 wimaxLibLocation, ClassLoader.getSystemClassLoader());
582
583 try {
584 wimaxManagerClass = wimaxClassLoader.loadClass(wimaxManagerClassName);
585 wimaxStateTrackerClass = wimaxClassLoader.loadClass(wimaxStateTrackerClassName);
586 wimaxServiceClass = wimaxClassLoader.loadClass(wimaxServiceClassName);
587 } catch (ClassNotFoundException ex) {
588 loge("Exception finding Wimax classes: " + ex.toString());
589 return null;
590 }
591 } catch(Resources.NotFoundException ex) {
592 loge("Wimax Resources does not exist!!! ");
593 return null;
594 }
595
596 try {
597 log("Starting Wimax Service... ");
598
599 Constructor wmxStTrkrConst = wimaxStateTrackerClass.getConstructor
600 (new Class[] {Context.class, Handler.class});
601 wimaxStateTracker = (NetworkStateTracker)wmxStTrkrConst.newInstance(mContext,
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700602 mTrackerHandler);
tk.mun093f55c2011-10-13 22:51:57 +0900603
604 Constructor wmxSrvConst = wimaxServiceClass.getDeclaredConstructor
605 (new Class[] {Context.class, wimaxStateTrackerClass});
606 wmxSrvConst.setAccessible(true);
607 IBinder svcInvoker = (IBinder)wmxSrvConst.newInstance(mContext, wimaxStateTracker);
608 wmxSrvConst.setAccessible(false);
609
610 ServiceManager.addService(WimaxManagerConstants.WIMAX_SERVICE, svcInvoker);
611
612 } catch(Exception ex) {
613 loge("Exception creating Wimax classes: " + ex.toString());
614 return null;
615 }
616 } else {
617 loge("Wimax is not enabled or not added to the network attributes!!! ");
618 return null;
619 }
620
621 return wimaxStateTracker;
622 }
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700623
The Android Open Source Project28527d22009-03-03 19:31:44 -0800624 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700625 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800626 * @param preference the new preference
627 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700628 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800629 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700630
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -0700631 mHandler.sendMessage(
632 mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800633 }
634
635 public int getNetworkPreference() {
636 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700637 int preference;
638 synchronized(this) {
639 preference = mNetworkPreference;
640 }
641 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800642 }
643
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700644 private void handleSetNetworkPreference(int preference) {
645 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt34848c02011-03-25 13:09:25 -0700646 mNetConfigs[preference] != null &&
647 mNetConfigs[preference].isDefault()) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700648 if (mNetworkPreference != preference) {
649 final ContentResolver cr = mContext.getContentResolver();
650 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
651 synchronized(this) {
652 mNetworkPreference = preference;
653 }
654 enforcePreference();
655 }
656 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800657 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700658
Wink Saville4f0de1e2011-08-04 15:01:58 -0700659 private int getConnectivityChangeDelay() {
660 final ContentResolver cr = mContext.getContentResolver();
661
662 /** Check system properties for the default value then use secure settings value, if any. */
663 int defaultDelay = SystemProperties.getInt(
664 "conn." + Settings.Secure.CONNECTIVITY_CHANGE_DELAY,
665 Settings.Secure.CONNECTIVITY_CHANGE_DELAY_DEFAULT);
666 return Settings.Secure.getInt(cr, Settings.Secure.CONNECTIVITY_CHANGE_DELAY,
667 defaultDelay);
668 }
669
The Android Open Source Project28527d22009-03-03 19:31:44 -0800670 private int getPersistedNetworkPreference() {
671 final ContentResolver cr = mContext.getContentResolver();
672
673 final int networkPrefSetting = Settings.Secure
674 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
675 if (networkPrefSetting != -1) {
676 return networkPrefSetting;
677 }
678
679 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
680 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700681
The Android Open Source Project28527d22009-03-03 19:31:44 -0800682 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700683 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800684 * In this method, we only tear down a non-preferred network. Establishing
685 * a connection to the preferred network is taken care of when we handle
686 * the disconnect event from the non-preferred network
687 * (see {@link #handleDisconnect(NetworkInfo)}).
688 */
689 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700690 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800691 return;
692
Robert Greenwalt2034b912009-08-12 16:08:25 -0700693 if (!mNetTrackers[mNetworkPreference].isAvailable())
694 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800695
Robert Greenwalt2034b912009-08-12 16:08:25 -0700696 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700697 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700698 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700699 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800700 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700701 " in enforcePreference");
702 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700703 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800704 }
705 }
706 }
707
708 private boolean teardown(NetworkStateTracker netTracker) {
709 if (netTracker.teardown()) {
710 netTracker.setTeardownRequested(true);
711 return true;
712 } else {
713 return false;
714 }
715 }
716
717 /**
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700718 * Check if UID should be blocked from using the network represented by the
719 * given {@link NetworkStateTracker}.
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700720 */
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700721 private boolean isNetworkBlocked(NetworkStateTracker tracker, int uid) {
722 final String iface = tracker.getLinkProperties().getInterfaceName();
Jeff Sharkey21062e72011-05-28 20:56:34 -0700723
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700724 final boolean networkCostly;
725 final int uidRules;
726 synchronized (mRulesLock) {
727 networkCostly = mMeteredIfaces.contains(iface);
728 uidRules = mUidRules.get(uid, RULE_ALLOW_ALL);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700729 }
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700730
731 if (networkCostly && (uidRules & RULE_REJECT_METERED) != 0) {
732 return true;
733 }
734
735 // no restrictive rules; network is visible
736 return false;
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700737 }
738
739 /**
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700740 * Return a filtered {@link NetworkInfo}, potentially marked
741 * {@link DetailedState#BLOCKED} based on
742 * {@link #isNetworkBlocked(NetworkStateTracker, int)}.
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700743 */
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700744 private NetworkInfo getFilteredNetworkInfo(NetworkStateTracker tracker, int uid) {
745 NetworkInfo info = tracker.getNetworkInfo();
746 if (isNetworkBlocked(tracker, uid)) {
Jeff Sharkey21062e72011-05-28 20:56:34 -0700747 // network is blocked; clone and override state
748 info = new NetworkInfo(info);
749 info.setDetailedState(DetailedState.BLOCKED, null, null);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700750 }
Jeff Sharkey21062e72011-05-28 20:56:34 -0700751 return info;
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700752 }
753
754 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -0800755 * Return NetworkInfo for the active (i.e., connected) network interface.
756 * It is assumed that at most one network is active at a time. If more
757 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700758 * @return the info for the active network, or {@code null} if none is
759 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800760 */
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700761 @Override
The Android Open Source Project28527d22009-03-03 19:31:44 -0800762 public NetworkInfo getActiveNetworkInfo() {
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700763 enforceAccessPermission();
764 final int uid = Binder.getCallingUid();
765 return getNetworkInfo(mActiveDefaultNetwork, uid);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800766 }
767
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700768 @Override
769 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
770 enforceConnectivityInternalPermission();
771 return getNetworkInfo(mActiveDefaultNetwork, uid);
772 }
773
774 @Override
The Android Open Source Project28527d22009-03-03 19:31:44 -0800775 public NetworkInfo getNetworkInfo(int networkType) {
776 enforceAccessPermission();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700777 final int uid = Binder.getCallingUid();
778 return getNetworkInfo(networkType, uid);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800779 }
780
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700781 private NetworkInfo getNetworkInfo(int networkType, int uid) {
782 NetworkInfo info = null;
783 if (isNetworkTypeValid(networkType)) {
784 final NetworkStateTracker tracker = mNetTrackers[networkType];
785 if (tracker != null) {
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700786 info = getFilteredNetworkInfo(tracker, uid);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700787 }
788 }
789 return info;
790 }
791
792 @Override
The Android Open Source Project28527d22009-03-03 19:31:44 -0800793 public NetworkInfo[] getAllNetworkInfo() {
794 enforceAccessPermission();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700795 final int uid = Binder.getCallingUid();
Jeff Sharkey21062e72011-05-28 20:56:34 -0700796 final ArrayList<NetworkInfo> result = Lists.newArrayList();
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700797 synchronized (mRulesLock) {
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700798 for (NetworkStateTracker tracker : mNetTrackers) {
799 if (tracker != null) {
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700800 result.add(getFilteredNetworkInfo(tracker, uid));
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700801 }
802 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800803 }
Jeff Sharkey21062e72011-05-28 20:56:34 -0700804 return result.toArray(new NetworkInfo[result.size()]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800805 }
806
Robert Greenwalt0114f6e2011-08-31 11:46:42 -0700807 @Override
808 public boolean isNetworkSupported(int networkType) {
809 enforceAccessPermission();
810 return (isNetworkTypeValid(networkType) && (mNetTrackers[networkType] != null));
811 }
812
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700813 /**
814 * Return LinkProperties for the active (i.e., connected) default
815 * network interface. It is assumed that at most one default network
816 * is active at a time. If more than one is active, it is indeterminate
817 * which will be returned.
818 * @return the ip properties for the active network, or {@code null} if
819 * none is active
820 */
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700821 @Override
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700822 public LinkProperties getActiveLinkProperties() {
Robert Greenwalte1544bb2011-05-20 12:23:41 -0700823 return getLinkProperties(mActiveDefaultNetwork);
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700824 }
825
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700826 @Override
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700827 public LinkProperties getLinkProperties(int networkType) {
828 enforceAccessPermission();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700829 if (isNetworkTypeValid(networkType)) {
830 final NetworkStateTracker tracker = mNetTrackers[networkType];
831 if (tracker != null) {
832 return tracker.getLinkProperties();
833 }
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700834 }
835 return null;
836 }
837
Jeff Sharkey21062e72011-05-28 20:56:34 -0700838 @Override
839 public NetworkState[] getAllNetworkState() {
840 enforceAccessPermission();
841 final int uid = Binder.getCallingUid();
842 final ArrayList<NetworkState> result = Lists.newArrayList();
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700843 synchronized (mRulesLock) {
Jeff Sharkey21062e72011-05-28 20:56:34 -0700844 for (NetworkStateTracker tracker : mNetTrackers) {
845 if (tracker != null) {
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700846 final NetworkInfo info = getFilteredNetworkInfo(tracker, uid);
Jeff Sharkey21062e72011-05-28 20:56:34 -0700847 result.add(new NetworkState(
848 info, tracker.getLinkProperties(), tracker.getLinkCapabilities()));
849 }
850 }
851 }
852 return result.toArray(new NetworkState[result.size()]);
853 }
854
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700855 private NetworkState getNetworkStateUnchecked(int networkType) {
856 if (isNetworkTypeValid(networkType)) {
857 final NetworkStateTracker tracker = mNetTrackers[networkType];
858 if (tracker != null) {
859 return new NetworkState(tracker.getNetworkInfo(), tracker.getLinkProperties(),
860 tracker.getLinkCapabilities());
861 }
862 }
863 return null;
864 }
865
866 @Override
867 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
868 enforceAccessPermission();
Jeff Sharkey3022ee12012-04-17 12:23:40 -0700869
870 final long token = Binder.clearCallingIdentity();
871 try {
872 final NetworkState state = getNetworkStateUnchecked(mActiveDefaultNetwork);
873 if (state != null) {
874 try {
875 return mPolicyManager.getNetworkQuotaInfo(state);
876 } catch (RemoteException e) {
877 }
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700878 }
Jeff Sharkey3022ee12012-04-17 12:23:40 -0700879 return null;
880 } finally {
881 Binder.restoreCallingIdentity(token);
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700882 }
Jeff Sharkey66fa9682011-08-02 17:22:34 -0700883 }
884
Jeff Sharkeyd00b1302012-04-12 18:34:54 -0700885 @Override
886 public boolean isActiveNetworkMetered() {
887 enforceAccessPermission();
Jeff Sharkey3022ee12012-04-17 12:23:40 -0700888 final long token = Binder.clearCallingIdentity();
889 try {
Jeff Sharkeyedf85d42012-04-30 15:47:05 -0700890 return isNetworkMeteredUnchecked(mActiveDefaultNetwork);
Jeff Sharkey3022ee12012-04-17 12:23:40 -0700891 } finally {
892 Binder.restoreCallingIdentity(token);
Jeff Sharkeyd00b1302012-04-12 18:34:54 -0700893 }
Jeff Sharkeyd00b1302012-04-12 18:34:54 -0700894 }
895
Jeff Sharkeyedf85d42012-04-30 15:47:05 -0700896 private boolean isNetworkMeteredUnchecked(int networkType) {
897 final NetworkState state = getNetworkStateUnchecked(networkType);
898 if (state != null) {
899 try {
900 return mPolicyManager.isNetworkMetered(state);
901 } catch (RemoteException e) {
902 }
903 }
904 return false;
905 }
906
The Android Open Source Project28527d22009-03-03 19:31:44 -0800907 public boolean setRadios(boolean turnOn) {
908 boolean result = true;
909 enforceChangePermission();
910 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700911 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800912 }
913 return result;
914 }
915
916 public boolean setRadio(int netType, boolean turnOn) {
917 enforceChangePermission();
918 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
919 return false;
920 }
921 NetworkStateTracker tracker = mNetTrackers[netType];
922 return tracker != null && tracker.setRadio(turnOn);
923 }
924
Haoyu Baib5da5752012-06-20 14:29:57 -0700925 private class NetdObserver extends INetworkManagementEventObserver.Stub {
926 public void interfaceClassDataActivityChanged(String label, boolean active) {
927 int deviceType = Integer.parseInt(label);
928 sendDataActivityBroadcast(deviceType, active);
929 }
930
931 public void interfaceStatusChanged(String iface, boolean up) {}
932 public void interfaceLinkStateChanged(String iface, boolean up) {}
933 public void interfaceAdded(String iface) {}
934 public void interfaceRemoved(String iface) {}
935 public void limitReached(String limitName, String iface) {}
936 }
937
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700938 /**
939 * Used to notice when the calling process dies so we can self-expire
940 *
941 * Also used to know if the process has cleaned up after itself when
942 * our auto-expire timer goes off. The timer has a link to an object.
943 *
944 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700945 private class FeatureUser implements IBinder.DeathRecipient {
946 int mNetworkType;
947 String mFeature;
948 IBinder mBinder;
949 int mPid;
950 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800951 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700952
953 FeatureUser(int type, String feature, IBinder binder) {
954 super();
955 mNetworkType = type;
956 mFeature = feature;
957 mBinder = binder;
958 mPid = getCallingPid();
959 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800960 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700961
Robert Greenwalt2034b912009-08-12 16:08:25 -0700962 try {
963 mBinder.linkToDeath(this, 0);
964 } catch (RemoteException e) {
965 binderDied();
966 }
967 }
968
969 void unlinkDeathRecipient() {
970 mBinder.unlinkToDeath(this, 0);
971 }
972
973 public void binderDied() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800974 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800975 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
976 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700977 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700978 }
979
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700980 public void expire() {
Robert Greenwalt78f28112011-08-02 17:18:41 -0700981 if (VDBG) {
982 log("ConnectivityService FeatureUser expire(" +
983 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
984 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
985 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700986 stopUsingNetworkFeature(this, false);
987 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800988
Kazuhiro Ondocae18f12011-07-19 11:23:37 -0500989 public boolean isSameUser(FeatureUser u) {
990 if (u == null) return false;
991
992 return isSameUser(u.mPid, u.mUid, u.mNetworkType, u.mFeature);
993 }
994
995 public boolean isSameUser(int pid, int uid, int networkType, String feature) {
996 if ((mPid == pid) && (mUid == uid) && (mNetworkType == networkType) &&
997 TextUtils.equals(mFeature, feature)) {
998 return true;
999 }
1000 return false;
1001 }
1002
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001003 public String toString() {
1004 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
1005 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
1006 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001007 }
1008
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001009 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -07001010 public int startUsingNetworkFeature(int networkType, String feature,
1011 IBinder binder) {
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001012 long startTime = 0;
1013 if (DBG) {
1014 startTime = SystemClock.elapsedRealtime();
1015 }
Wink Savillea7d56572011-09-21 11:05:43 -07001016 if (VDBG) {
Jeff Sharkeyedf85d42012-04-30 15:47:05 -07001017 log("startUsingNetworkFeature for net " + networkType + ": " + feature + ", uid="
1018 + Binder.getCallingUid());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001019 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001020 enforceChangePermission();
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001021 try {
1022 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
1023 mNetConfigs[networkType] == null) {
Wink Saville64e3f782012-07-10 12:37:54 -07001024 return PhoneConstants.APN_REQUEST_FAILED;
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001025 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001026
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001027 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001028
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001029 // TODO - move this into individual networktrackers
1030 int usedNetworkType = convertFeatureToNetworkType(networkType, feature);
Robert Greenwalt6cac0742011-06-21 17:26:14 -07001031
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001032 if (mProtectedNetworks.contains(usedNetworkType)) {
1033 enforceConnectivityInternalPermission();
1034 }
Robert Greenwalt6cac0742011-06-21 17:26:14 -07001035
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001036 // if UID is restricted, don't allow them to bring up metered APNs
1037 final boolean networkMetered = isNetworkMeteredUnchecked(usedNetworkType);
1038 final int uidRules;
1039 synchronized (mRulesLock) {
1040 uidRules = mUidRules.get(Binder.getCallingUid(), RULE_ALLOW_ALL);
1041 }
1042 if (networkMetered && (uidRules & RULE_REJECT_METERED) != 0) {
Wink Saville64e3f782012-07-10 12:37:54 -07001043 return PhoneConstants.APN_REQUEST_FAILED;
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001044 }
Jeff Sharkeyedf85d42012-04-30 15:47:05 -07001045
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001046 NetworkStateTracker network = mNetTrackers[usedNetworkType];
1047 if (network != null) {
1048 Integer currentPid = new Integer(getCallingPid());
1049 if (usedNetworkType != networkType) {
1050 NetworkInfo ni = network.getNetworkInfo();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001051
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001052 if (ni.isAvailable() == false) {
1053 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
1054 if (DBG) log("special network not available ni=" + ni.getTypeName());
Wink Saville64e3f782012-07-10 12:37:54 -07001055 return PhoneConstants.APN_TYPE_NOT_AVAILABLE;
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001056 } else {
1057 // else make the attempt anyway - probably giving REQUEST_STARTED below
1058 if (DBG) {
1059 log("special network not available, but try anyway ni=" +
1060 ni.getTypeName());
Kazuhiro Ondocae18f12011-07-19 11:23:37 -05001061 }
1062 }
1063 }
1064
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001065 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001066
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001067 synchronized(this) {
1068 boolean addToList = true;
1069 if (restoreTimer < 0) {
1070 // In case there is no timer is specified for the feature,
1071 // make sure we don't add duplicate entry with the same request.
1072 for (FeatureUser u : mFeatureUsers) {
1073 if (u.isSameUser(f)) {
1074 // Duplicate user is found. Do not add.
1075 addToList = false;
1076 break;
1077 }
1078 }
Robert Greenwalt7fa3bdb2011-12-13 15:26:02 -08001079 }
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001080
1081 if (addToList) mFeatureUsers.add(f);
1082 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
1083 // this gets used for per-pid dns when connected
1084 mNetRequestersPids[usedNetworkType].add(currentPid);
1085 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001086 }
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001087
1088 if (restoreTimer >= 0) {
1089 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1090 EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
1091 }
1092
1093 if ((ni.isConnectedOrConnecting() == true) &&
1094 !network.isTeardownRequested()) {
1095 if (ni.isConnected() == true) {
1096 final long token = Binder.clearCallingIdentity();
1097 try {
1098 // add the pid-specific dns
1099 handleDnsConfigurationChange(usedNetworkType);
1100 if (VDBG) log("special network already active");
1101 } finally {
1102 Binder.restoreCallingIdentity(token);
1103 }
Wink Saville64e3f782012-07-10 12:37:54 -07001104 return PhoneConstants.APN_ALREADY_ACTIVE;
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001105 }
1106 if (VDBG) log("special network already connecting");
Wink Saville64e3f782012-07-10 12:37:54 -07001107 return PhoneConstants.APN_REQUEST_STARTED;
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001108 }
1109
1110 // check if the radio in play can make another contact
1111 // assume if cannot for now
1112
1113 if (DBG) {
1114 log("startUsingNetworkFeature reconnecting to " + networkType + ": " +
1115 feature);
1116 }
1117 network.reconnect();
Wink Saville64e3f782012-07-10 12:37:54 -07001118 return PhoneConstants.APN_REQUEST_STARTED;
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001119 } else {
1120 // need to remember this unsupported request so we respond appropriately on stop
1121 synchronized(this) {
1122 mFeatureUsers.add(f);
1123 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
1124 // this gets used for per-pid dns when connected
1125 mNetRequestersPids[usedNetworkType].add(currentPid);
1126 }
Robert Greenwalt5364d752010-12-15 13:26:33 -08001127 }
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001128 return -1;
Robert Greenwalt5364d752010-12-15 13:26:33 -08001129 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001130 }
Wink Saville64e3f782012-07-10 12:37:54 -07001131 return PhoneConstants.APN_TYPE_NOT_AVAILABLE;
Robert Greenwalt1e6991e2012-05-22 16:07:46 -07001132 } finally {
1133 if (DBG) {
1134 final long execTime = SystemClock.elapsedRealtime() - startTime;
1135 if (execTime > 250) {
1136 loge("startUsingNetworkFeature took too long: " + execTime + "ms");
1137 } else {
1138 if (VDBG) log("startUsingNetworkFeature took " + execTime + "ms");
1139 }
1140 }
1141 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001142 }
1143
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001144 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -08001145 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -07001146 enforceChangePermission();
1147
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001148 int pid = getCallingPid();
1149 int uid = getCallingUid();
1150
1151 FeatureUser u = null;
1152 boolean found = false;
1153
1154 synchronized(this) {
Kazuhiro Ondocae18f12011-07-19 11:23:37 -05001155 for (FeatureUser x : mFeatureUsers) {
1156 if (x.isSameUser(pid, uid, networkType, feature)) {
1157 u = x;
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001158 found = true;
1159 break;
1160 }
1161 }
1162 }
1163 if (found && u != null) {
1164 // stop regardless of how many other time this proc had called start
1165 return stopUsingNetworkFeature(u, true);
1166 } else {
1167 // none found!
Wink Savillea7d56572011-09-21 11:05:43 -07001168 if (VDBG) log("stopUsingNetworkFeature - not a live request, ignoring");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001169 return 1;
1170 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001171 }
1172
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001173 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
1174 int networkType = u.mNetworkType;
1175 String feature = u.mFeature;
1176 int pid = u.mPid;
1177 int uid = u.mUid;
1178
1179 NetworkStateTracker tracker = null;
1180 boolean callTeardown = false; // used to carry our decision outside of sync block
1181
Wink Savillea7d56572011-09-21 11:05:43 -07001182 if (VDBG) {
1183 log("stopUsingNetworkFeature: net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001184 }
Robert Greenwalt28f43012009-10-06 17:52:40 -07001185
The Android Open Source Project28527d22009-03-03 19:31:44 -08001186 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
Wink Savillea7d56572011-09-21 11:05:43 -07001187 if (DBG) {
1188 log("stopUsingNetworkFeature: net " + networkType + ": " + feature +
1189 ", net is invalid");
1190 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001191 return -1;
1192 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001193
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001194 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
1195 // sync block
1196 synchronized(this) {
1197 // check if this process still has an outstanding start request
1198 if (!mFeatureUsers.contains(u)) {
Wink Savillea7d56572011-09-21 11:05:43 -07001199 if (VDBG) {
1200 log("stopUsingNetworkFeature: this process has no outstanding requests" +
1201 ", ignoring");
1202 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001203 return 1;
1204 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001205 u.unlinkDeathRecipient();
1206 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
1207 // If we care about duplicate requests, check for that here.
1208 //
1209 // This is done to support the extension of a request - the app
1210 // can request we start the network feature again and renew the
1211 // auto-shutoff delay. Normal "stop" calls from the app though
1212 // do not pay attention to duplicate requests - in effect the
1213 // API does not refcount and a single stop will counter multiple starts.
1214 if (ignoreDups == false) {
Kazuhiro Ondocae18f12011-07-19 11:23:37 -05001215 for (FeatureUser x : mFeatureUsers) {
1216 if (x.isSameUser(u)) {
Wink Savillea7d56572011-09-21 11:05:43 -07001217 if (VDBG) log("stopUsingNetworkFeature: dup is found, ignoring");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001218 return 1;
1219 }
1220 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001221 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001222
repo syncf5de5572011-07-29 23:55:49 -07001223 // TODO - move to individual network trackers
1224 int usedNetworkType = convertFeatureToNetworkType(networkType, feature);
1225
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001226 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001227 if (tracker == null) {
Wink Savillea7d56572011-09-21 11:05:43 -07001228 if (DBG) {
1229 log("stopUsingNetworkFeature: net " + networkType + ": " + feature +
1230 " no known tracker for used net type " + usedNetworkType);
1231 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001232 return -1;
1233 }
1234 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001235 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001236 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -08001237 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001238 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillea7d56572011-09-21 11:05:43 -07001239 if (VDBG) {
1240 log("stopUsingNetworkFeature: net " + networkType + ": " + feature +
1241 " others still using it");
1242 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001243 return 1;
1244 }
1245 callTeardown = true;
Robert Greenwalt9f3be4c2011-01-10 11:58:31 -08001246 } else {
Wink Savillea7d56572011-09-21 11:05:43 -07001247 if (DBG) {
1248 log("stopUsingNetworkFeature: net " + networkType + ": " + feature +
1249 " not a known feature - dropping");
1250 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001251 }
1252 }
Wink Savillea7d56572011-09-21 11:05:43 -07001253
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001254 if (callTeardown) {
Wink Savillea7d56572011-09-21 11:05:43 -07001255 if (DBG) {
1256 log("stopUsingNetworkFeature: teardown net " + networkType + ": " + feature);
1257 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001258 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001259 return 1;
1260 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -07001261 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001262 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001263 }
1264
1265 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001266 * @deprecated use requestRouteToHostAddress instead
1267 *
The Android Open Source Project28527d22009-03-03 19:31:44 -08001268 * Ensure that a network route exists to deliver traffic to the specified
1269 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001270 * @param networkType the type of the network over which traffic to the
1271 * specified host is to be routed
1272 * @param hostAddress the IP address of the host to which the route is
1273 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -08001274 * @return {@code true} on success, {@code false} on failure
1275 */
1276 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001277 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
1278
1279 if (inetAddress == null) {
1280 return false;
1281 }
1282
1283 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
1284 }
1285
1286 /**
1287 * Ensure that a network route exists to deliver traffic to the specified
1288 * host via the specified network interface.
1289 * @param networkType the type of the network over which traffic to the
1290 * specified host is to be routed
1291 * @param hostAddress the IP address of the host to which the route is
1292 * desired
1293 * @return {@code true} on success, {@code false} on failure
1294 */
1295 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001296 enforceChangePermission();
Robert Greenwalt6cac0742011-06-21 17:26:14 -07001297 if (mProtectedNetworks.contains(networkType)) {
1298 enforceConnectivityInternalPermission();
1299 }
1300
The Android Open Source Project28527d22009-03-03 19:31:44 -08001301 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
Robert Greenwalt7fa3bdb2011-12-13 15:26:02 -08001302 if (DBG) log("requestRouteToHostAddress on invalid network: " + networkType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001303 return false;
1304 }
1305 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -07001306
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001307 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
1308 tracker.isTeardownRequested()) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001309 if (VDBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001310 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001311 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -07001312 }
1313 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001314 }
Robert Greenwalt7fa3bdb2011-12-13 15:26:02 -08001315 final long token = Binder.clearCallingIdentity();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001316 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001317 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001318 LinkProperties lp = tracker.getLinkProperties();
Robert Greenwalt98107422011-07-22 11:55:33 -07001319 return addRouteToAddress(lp, addr);
Robert Greenwalt7fa3bdb2011-12-13 15:26:02 -08001320 } catch (UnknownHostException e) {
1321 if (DBG) log("requestRouteToHostAddress got " + e.toString());
1322 } finally {
1323 Binder.restoreCallingIdentity(token);
1324 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001325 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001326 }
1327
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001328 private boolean addRoute(LinkProperties p, RouteInfo r, boolean toDefaultTable) {
1329 return modifyRoute(p.getInterfaceName(), p, r, 0, ADD, toDefaultTable);
Robert Greenwaltbd492212011-05-06 17:10:53 -07001330 }
1331
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001332 private boolean removeRoute(LinkProperties p, RouteInfo r, boolean toDefaultTable) {
1333 return modifyRoute(p.getInterfaceName(), p, r, 0, REMOVE, toDefaultTable);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001334 }
1335
Robert Greenwalt98107422011-07-22 11:55:33 -07001336 private boolean addRouteToAddress(LinkProperties lp, InetAddress addr) {
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001337 return modifyRouteToAddress(lp, addr, ADD, TO_DEFAULT_TABLE);
Robert Greenwalt98107422011-07-22 11:55:33 -07001338 }
1339
1340 private boolean removeRouteToAddress(LinkProperties lp, InetAddress addr) {
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001341 return modifyRouteToAddress(lp, addr, REMOVE, TO_DEFAULT_TABLE);
Robert Greenwalt98107422011-07-22 11:55:33 -07001342 }
1343
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001344 private boolean modifyRouteToAddress(LinkProperties lp, InetAddress addr, boolean doAdd,
1345 boolean toDefaultTable) {
Robert Greenwalt98107422011-07-22 11:55:33 -07001346 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), addr);
1347 if (bestRoute == null) {
1348 bestRoute = RouteInfo.makeHostRoute(addr);
1349 } else {
1350 if (bestRoute.getGateway().equals(addr)) {
1351 // if there is no better route, add the implied hostroute for our gateway
1352 bestRoute = RouteInfo.makeHostRoute(addr);
1353 } else {
1354 // if we will connect to this through another route, add a direct route
1355 // to it's gateway
1356 bestRoute = RouteInfo.makeHostRoute(addr, bestRoute.getGateway());
1357 }
1358 }
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001359 return modifyRoute(lp.getInterfaceName(), lp, bestRoute, 0, doAdd, toDefaultTable);
Robert Greenwalt98107422011-07-22 11:55:33 -07001360 }
1361
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001362 private boolean modifyRoute(String ifaceName, LinkProperties lp, RouteInfo r, int cycleCount,
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001363 boolean doAdd, boolean toDefaultTable) {
Robert Greenwalt7fa3bdb2011-12-13 15:26:02 -08001364 if ((ifaceName == null) || (lp == null) || (r == null)) {
1365 if (DBG) log("modifyRoute got unexpected null: " + ifaceName + ", " + lp + ", " + r);
1366 return false;
1367 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001368
1369 if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001370 loge("Error modifying route - too much recursion");
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001371 return false;
1372 }
1373
1374 if (r.isHostRoute() == false) {
1375 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), r.getGateway());
1376 if (bestRoute != null) {
Robert Greenwalt57c83bb2011-07-15 09:45:08 -07001377 if (bestRoute.getGateway().equals(r.getGateway())) {
1378 // if there is no better route, add the implied hostroute for our gateway
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001379 bestRoute = RouteInfo.makeHostRoute(r.getGateway());
Robert Greenwalt57c83bb2011-07-15 09:45:08 -07001380 } else {
1381 // if we will connect to our gateway through another route, add a direct
1382 // route to it's gateway
1383 bestRoute = RouteInfo.makeHostRoute(r.getGateway(), bestRoute.getGateway());
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001384 }
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001385 modifyRoute(ifaceName, lp, bestRoute, cycleCount+1, doAdd, toDefaultTable);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001386 }
1387 }
1388 if (doAdd) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001389 if (VDBG) log("Adding " + r + " for interface " + ifaceName);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001390 try {
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001391 if (toDefaultTable) {
1392 mAddedRoutes.add(r); // only track default table - only one apps can effect
1393 mNetd.addRoute(ifaceName, r);
1394 } else {
1395 mNetd.addSecondaryRoute(ifaceName, r);
1396 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001397 } catch (Exception e) {
1398 // never crash - catch them all
Robert Greenwalt7fa3bdb2011-12-13 15:26:02 -08001399 if (DBG) loge("Exception trying to add a route: " + e);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001400 return false;
1401 }
1402 } else {
1403 // if we remove this one and there are no more like it, then refcount==0 and
1404 // we can remove it from the table
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001405 if (toDefaultTable) {
1406 mAddedRoutes.remove(r);
1407 if (mAddedRoutes.contains(r) == false) {
1408 if (VDBG) log("Removing " + r + " for interface " + ifaceName);
1409 try {
1410 mNetd.removeRoute(ifaceName, r);
1411 } catch (Exception e) {
1412 // never crash - catch them all
Robert Greenwalt436db352012-04-23 18:00:37 -07001413 if (VDBG) loge("Exception trying to remove a route: " + e);
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001414 return false;
1415 }
1416 } else {
1417 if (VDBG) log("not removing " + r + " as it's still in use");
1418 }
1419 } else {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001420 if (VDBG) log("Removing " + r + " for interface " + ifaceName);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001421 try {
Robert Greenwalt49c75d32011-11-02 14:37:19 -07001422 mNetd.removeSecondaryRoute(ifaceName, r);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001423 } catch (Exception e) {
1424 // never crash - catch them all
Robert Greenwalt436db352012-04-23 18:00:37 -07001425 if (VDBG) loge("Exception trying to remove a route: " + e);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001426 return false;
1427 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001428 }
1429 }
1430 return true;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001431 }
1432
1433 /**
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001434 * @see ConnectivityManager#getMobileDataEnabled()
1435 */
1436 public boolean getMobileDataEnabled() {
Wink Savilleb9024c62010-12-07 10:31:02 -08001437 // TODO: This detail should probably be in DataConnectionTracker's
1438 // which is where we store the value and maybe make this
1439 // asynchronous.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001440 enforceAccessPermission();
1441 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1442 Settings.Secure.MOBILE_DATA, 1) == 1;
Robert Greenwalt78f28112011-08-02 17:18:41 -07001443 if (VDBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001444 return retVal;
1445 }
1446
Robert Greenwalt34848c02011-03-25 13:09:25 -07001447 public void setDataDependency(int networkType, boolean met) {
Robert Greenwalt6cac0742011-06-21 17:26:14 -07001448 enforceConnectivityInternalPermission();
1449
Robert Greenwalt34848c02011-03-25 13:09:25 -07001450 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1451 (met ? ENABLED : DISABLED), networkType));
1452 }
1453
1454 private void handleSetDependencyMet(int networkType, boolean met) {
1455 if (mNetTrackers[networkType] != null) {
1456 if (DBG) {
1457 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1458 }
1459 mNetTrackers[networkType].setDependencyMet(met);
1460 }
1461 }
1462
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001463 private INetworkPolicyListener mPolicyListener = new INetworkPolicyListener.Stub() {
1464 @Override
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001465 public void onUidRulesChanged(int uid, int uidRules) {
Jeff Sharkeyb1b6ccd2012-02-07 12:05:43 -08001466 // caller is NPMS, since we only register with them
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001467 if (LOGD_RULES) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001468 log("onUidRulesChanged(uid=" + uid + ", uidRules=" + uidRules + ")");
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001469 }
1470
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001471 synchronized (mRulesLock) {
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001472 // skip update when we've already applied rules
1473 final int oldRules = mUidRules.get(uid, RULE_ALLOW_ALL);
1474 if (oldRules == uidRules) return;
1475
1476 mUidRules.put(uid, uidRules);
1477 }
1478
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001479 // TODO: notify UID when it has requested targeted updates
1480 }
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001481
1482 @Override
1483 public void onMeteredIfacesChanged(String[] meteredIfaces) {
Jeff Sharkeyb1b6ccd2012-02-07 12:05:43 -08001484 // caller is NPMS, since we only register with them
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001485 if (LOGD_RULES) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001486 log("onMeteredIfacesChanged(ifaces=" + Arrays.toString(meteredIfaces) + ")");
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001487 }
1488
1489 synchronized (mRulesLock) {
1490 mMeteredIfaces.clear();
1491 for (String iface : meteredIfaces) {
1492 mMeteredIfaces.add(iface);
1493 }
1494 }
1495 }
Jeff Sharkeyb1b6ccd2012-02-07 12:05:43 -08001496
1497 @Override
1498 public void onRestrictBackgroundChanged(boolean restrictBackground) {
1499 // caller is NPMS, since we only register with them
1500 if (LOGD_RULES) {
1501 log("onRestrictBackgroundChanged(restrictBackground=" + restrictBackground + ")");
1502 }
1503
1504 // kick off connectivity change broadcast for active network, since
1505 // global background policy change is radical.
1506 final int networkType = mActiveDefaultNetwork;
1507 if (isNetworkTypeValid(networkType)) {
1508 final NetworkStateTracker tracker = mNetTrackers[networkType];
1509 if (tracker != null) {
1510 final NetworkInfo info = tracker.getNetworkInfo();
1511 if (info != null && info.isConnected()) {
1512 sendConnectedBroadcast(info);
1513 }
1514 }
1515 }
1516 }
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001517 };
1518
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001519 /**
1520 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1521 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001522 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001523 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -08001524 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001525
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001526 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwalt34848c02011-03-25 13:09:25 -07001527 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001528 }
1529
1530 private void handleSetMobileData(boolean enabled) {
Wink Savilleb9024c62010-12-07 10:31:02 -08001531 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07001532 if (VDBG) {
1533 log(mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001534 }
Jeff Sharkey805662d2011-08-19 02:24:24 -07001535 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setUserDataEnable(enabled);
1536 }
tk.mun5eee1042012-01-06 10:43:52 +09001537 if (mNetTrackers[ConnectivityManager.TYPE_WIMAX] != null) {
1538 if (VDBG) {
1539 log(mNetTrackers[ConnectivityManager.TYPE_WIMAX].toString() + enabled);
1540 }
1541 mNetTrackers[ConnectivityManager.TYPE_WIMAX].setUserDataEnable(enabled);
1542 }
Jeff Sharkey805662d2011-08-19 02:24:24 -07001543 }
1544
1545 @Override
1546 public void setPolicyDataEnable(int networkType, boolean enabled) {
1547 // only someone like NPMS should only be calling us
1548 mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
1549
1550 mHandler.sendMessage(mHandler.obtainMessage(
1551 EVENT_SET_POLICY_DATA_ENABLE, networkType, (enabled ? ENABLED : DISABLED)));
1552 }
1553
1554 private void handleSetPolicyDataEnable(int networkType, boolean enabled) {
1555 if (isNetworkTypeValid(networkType)) {
1556 final NetworkStateTracker tracker = mNetTrackers[networkType];
1557 if (tracker != null) {
1558 tracker.setPolicyDataEnable(enabled);
1559 }
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001560 }
1561 }
1562
The Android Open Source Project28527d22009-03-03 19:31:44 -08001563 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001564 mContext.enforceCallingOrSelfPermission(
1565 android.Manifest.permission.ACCESS_NETWORK_STATE,
1566 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001567 }
1568
1569 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001570 mContext.enforceCallingOrSelfPermission(
1571 android.Manifest.permission.CHANGE_NETWORK_STATE,
1572 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001573 }
1574
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001575 // TODO Make this a special check when it goes public
1576 private void enforceTetherChangePermission() {
1577 mContext.enforceCallingOrSelfPermission(
1578 android.Manifest.permission.CHANGE_NETWORK_STATE,
1579 "ConnectivityService");
1580 }
1581
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001582 private void enforceTetherAccessPermission() {
1583 mContext.enforceCallingOrSelfPermission(
1584 android.Manifest.permission.ACCESS_NETWORK_STATE,
1585 "ConnectivityService");
1586 }
1587
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001588 private void enforceConnectivityInternalPermission() {
1589 mContext.enforceCallingOrSelfPermission(
1590 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1591 "ConnectivityService");
1592 }
1593
The Android Open Source Project28527d22009-03-03 19:31:44 -08001594 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001595 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1596 * network, we ignore it. If it is for the active network, we send out a
1597 * broadcast. But first, we check whether it might be possible to connect
1598 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001599 * @param info the {@code NetworkInfo} for the network
1600 */
1601 private void handleDisconnect(NetworkInfo info) {
1602
Robert Greenwalt2034b912009-08-12 16:08:25 -07001603 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001604
Robert Greenwalt2034b912009-08-12 16:08:25 -07001605 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001606 /*
1607 * If the disconnected network is not the active one, then don't report
1608 * this as a loss of connectivity. What probably happened is that we're
1609 * getting the disconnect for a network that we explicitly disabled
1610 * in accordance with network preference policies.
1611 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001612 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001613 List pids = mNetRequestersPids[prevNetType];
1614 for (int i = 0; i<pids.size(); i++) {
1615 Integer pid = (Integer)pids.get(i);
1616 // will remove them because the net's no longer connected
1617 // need to do this now as only now do we know the pids and
1618 // can properly null things that are no longer referenced.
1619 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001620 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001621 }
1622
The Android Open Source Project28527d22009-03-03 19:31:44 -08001623 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1624 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
Jeff Sharkey47905d12012-08-06 11:41:50 -07001625 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001626 if (info.isFailover()) {
1627 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1628 info.setFailover(false);
1629 }
1630 if (info.getReason() != null) {
1631 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1632 }
1633 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001634 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1635 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001636 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001637
Robert Greenwalt34848c02011-03-25 13:09:25 -07001638 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001639 tryFailover(prevNetType);
1640 if (mActiveDefaultNetwork != -1) {
1641 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001642 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1643 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001644 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001645 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1646 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001647 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001648 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001649
1650 // Reset interface if no other connections are using the same interface
1651 boolean doReset = true;
1652 LinkProperties linkProperties = mNetTrackers[prevNetType].getLinkProperties();
1653 if (linkProperties != null) {
1654 String oldIface = linkProperties.getInterfaceName();
1655 if (TextUtils.isEmpty(oldIface) == false) {
1656 for (NetworkStateTracker networkStateTracker : mNetTrackers) {
1657 if (networkStateTracker == null) continue;
1658 NetworkInfo networkInfo = networkStateTracker.getNetworkInfo();
1659 if (networkInfo.isConnected() && networkInfo.getType() != prevNetType) {
1660 LinkProperties l = networkStateTracker.getLinkProperties();
1661 if (l == null) continue;
1662 if (oldIface.equals(l.getInterfaceName())) {
1663 doReset = false;
1664 break;
1665 }
1666 }
1667 }
1668 }
1669 }
1670
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001671 // do this before we broadcast the change
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001672 handleConnectivityChange(prevNetType, doReset);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001673
Jeff Sharkey971cd162011-08-29 16:02:57 -07001674 final Intent immediateIntent = new Intent(intent);
1675 immediateIntent.setAction(CONNECTIVITY_ACTION_IMMEDIATE);
1676 sendStickyBroadcast(immediateIntent);
Wink Saville4f0de1e2011-08-04 15:01:58 -07001677 sendStickyBroadcastDelayed(intent, getConnectivityChangeDelay());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001678 /*
1679 * If the failover network is already connected, then immediately send
1680 * out a followup broadcast indicating successful failover
1681 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001682 if (mActiveDefaultNetwork != -1) {
Wink Saville4f0de1e2011-08-04 15:01:58 -07001683 sendConnectedBroadcastDelayed(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(),
1684 getConnectivityChangeDelay());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001685 }
1686 }
1687
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001688 private void tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001689 /*
Robert Greenwalt92564852011-01-06 15:41:07 -08001690 * If this is a default network, check if other defaults are available.
1691 * Try to reconnect on all available and let them hash it out when
1692 * more than one connects.
Robert Greenwalt2034b912009-08-12 16:08:25 -07001693 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001694 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001695 if (mActiveDefaultNetwork == prevNetType) {
1696 mActiveDefaultNetwork = -1;
1697 }
1698
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001699 // don't signal a reconnect for anything lower or equal priority than our
1700 // current connected default
1701 // TODO - don't filter by priority now - nice optimization but risky
1702// int currentPriority = -1;
1703// if (mActiveDefaultNetwork != -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001704// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001705// }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001706 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001707 if (checkType == prevNetType) continue;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001708 if (mNetConfigs[checkType] == null) continue;
1709 if (!mNetConfigs[checkType].isDefault()) continue;
Robert Greenwaltbc026d02011-11-11 09:56:03 -08001710 if (mNetTrackers[checkType] == null) continue;
Wink Saville72a95b92011-01-26 15:43:49 -08001711
1712// Enabling the isAvailable() optimization caused mobile to not get
1713// selected if it was in the middle of error handling. Specifically
1714// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1715// would not be available and we wouldn't get connected to anything.
1716// So removing the isAvailable() optimization below for now. TODO: This
1717// optimization should work and we need to investigate why it doesn't work.
1718// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1719// complete before it is really complete.
1720// if (!mNetTrackers[checkType].isAvailable()) continue;
1721
Robert Greenwalt34848c02011-03-25 13:09:25 -07001722// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001723
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001724 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1725 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1726 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1727 checkInfo.setFailover(true);
1728 checkTracker.reconnect();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001729 }
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001730 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001731 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001732 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001733 }
1734
1735 private void sendConnectedBroadcast(NetworkInfo info) {
Jeff Sharkey971cd162011-08-29 16:02:57 -07001736 sendGeneralBroadcast(info, CONNECTIVITY_ACTION_IMMEDIATE);
1737 sendGeneralBroadcast(info, CONNECTIVITY_ACTION);
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001738 }
1739
Wink Saville4f0de1e2011-08-04 15:01:58 -07001740 private void sendConnectedBroadcastDelayed(NetworkInfo info, int delayMs) {
Jeff Sharkey971cd162011-08-29 16:02:57 -07001741 sendGeneralBroadcast(info, CONNECTIVITY_ACTION_IMMEDIATE);
1742 sendGeneralBroadcastDelayed(info, CONNECTIVITY_ACTION, delayMs);
Wink Saville4f0de1e2011-08-04 15:01:58 -07001743 }
1744
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001745 private void sendInetConditionBroadcast(NetworkInfo info) {
1746 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1747 }
1748
Wink Saville4f0de1e2011-08-04 15:01:58 -07001749 private Intent makeGeneralIntent(NetworkInfo info, String bcastType) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001750 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001751 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
Jeff Sharkey47905d12012-08-06 11:41:50 -07001752 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001753 if (info.isFailover()) {
1754 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1755 info.setFailover(false);
1756 }
1757 if (info.getReason() != null) {
1758 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1759 }
1760 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001761 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1762 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001763 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001764 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Wink Saville4f0de1e2011-08-04 15:01:58 -07001765 return intent;
1766 }
1767
1768 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1769 sendStickyBroadcast(makeGeneralIntent(info, bcastType));
1770 }
1771
1772 private void sendGeneralBroadcastDelayed(NetworkInfo info, String bcastType, int delayMs) {
1773 sendStickyBroadcastDelayed(makeGeneralIntent(info, bcastType), delayMs);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001774 }
1775
Haoyu Baib5da5752012-06-20 14:29:57 -07001776 private void sendDataActivityBroadcast(int deviceType, boolean active) {
1777 Intent intent = new Intent(ConnectivityManager.ACTION_DATA_ACTIVITY_CHANGE);
1778 intent.putExtra(ConnectivityManager.EXTRA_DEVICE_TYPE, deviceType);
1779 intent.putExtra(ConnectivityManager.EXTRA_IS_ACTIVE, active);
1780 mContext.sendOrderedBroadcast(intent, RECEIVE_DATA_ACTIVITY_CHANGE);
1781 }
1782
The Android Open Source Project28527d22009-03-03 19:31:44 -08001783 /**
1784 * Called when an attempt to fail over to another network has failed.
1785 * @param info the {@link NetworkInfo} for the failed network
1786 */
1787 private void handleConnectionFailure(NetworkInfo info) {
1788 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001789
Robert Greenwalt2034b912009-08-12 16:08:25 -07001790 String reason = info.getReason();
1791 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001792
Robert Greenwalte981bc52010-10-08 16:35:52 -07001793 String reasonText;
1794 if (reason == null) {
1795 reasonText = ".";
1796 } else {
1797 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001798 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001799 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001800
1801 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1802 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
Jeff Sharkey47905d12012-08-06 11:41:50 -07001803 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001804 if (getActiveNetworkInfo() == null) {
1805 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1806 }
1807 if (reason != null) {
1808 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1809 }
1810 if (extraInfo != null) {
1811 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1812 }
1813 if (info.isFailover()) {
1814 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1815 info.setFailover(false);
1816 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001817
Robert Greenwalt34848c02011-03-25 13:09:25 -07001818 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001819 tryFailover(info.getType());
1820 if (mActiveDefaultNetwork != -1) {
1821 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001822 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1823 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001824 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001825 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1826 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001827 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001828
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001829 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Jeff Sharkey971cd162011-08-29 16:02:57 -07001830
1831 final Intent immediateIntent = new Intent(intent);
1832 immediateIntent.setAction(CONNECTIVITY_ACTION_IMMEDIATE);
1833 sendStickyBroadcast(immediateIntent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001834 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001835 /*
1836 * If the failover network is already connected, then immediately send
1837 * out a followup broadcast indicating successful failover
1838 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001839 if (mActiveDefaultNetwork != -1) {
1840 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001841 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001842 }
1843
1844 private void sendStickyBroadcast(Intent intent) {
1845 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001846 if (!mSystemReady) {
1847 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001848 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001849 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Wink Savillea7d56572011-09-21 11:05:43 -07001850 if (VDBG) {
Jeff Sharkey971cd162011-08-29 16:02:57 -07001851 log("sendStickyBroadcast: action=" + intent.getAction());
Wink Saville4f0de1e2011-08-04 15:01:58 -07001852 }
1853
Dianne Hackborna417ff82009-12-08 19:45:14 -08001854 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001855 }
1856 }
1857
Wink Saville4f0de1e2011-08-04 15:01:58 -07001858 private void sendStickyBroadcastDelayed(Intent intent, int delayMs) {
1859 if (delayMs <= 0) {
1860 sendStickyBroadcast(intent);
1861 } else {
Wink Savillea7d56572011-09-21 11:05:43 -07001862 if (VDBG) {
Jeff Sharkey971cd162011-08-29 16:02:57 -07001863 log("sendStickyBroadcastDelayed: delayMs=" + delayMs + ", action="
1864 + intent.getAction());
1865 }
Wink Saville4f0de1e2011-08-04 15:01:58 -07001866 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1867 EVENT_SEND_STICKY_BROADCAST_INTENT, intent), delayMs);
1868 }
1869 }
1870
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001871 void systemReady() {
1872 synchronized(this) {
1873 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001874 if (mInitialBroadcast != null) {
1875 mContext.sendStickyBroadcast(mInitialBroadcast);
1876 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001877 }
1878 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001879 // load the global proxy at startup
1880 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001881 }
1882
1883 private void handleConnect(NetworkInfo info) {
Jeff Sharkeyaac2c502011-10-04 16:54:49 -07001884 final int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001885
1886 // snapshot isFailover, because sendConnectedBroadcast() resets it
1887 boolean isFailover = info.isFailover();
Jeff Sharkeyaac2c502011-10-04 16:54:49 -07001888 final NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001889
Robert Greenwalt2034b912009-08-12 16:08:25 -07001890 // if this is a default net and other default is running
1891 // kill the one not preferred
Robert Greenwalt34848c02011-03-25 13:09:25 -07001892 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001893 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1894 if ((type != mNetworkPreference &&
Wink Savillef2a62832011-04-07 14:23:45 -07001895 mNetConfigs[mActiveDefaultNetwork].priority >
1896 mNetConfigs[type].priority) ||
Robert Greenwalt2034b912009-08-12 16:08:25 -07001897 mNetworkPreference == mActiveDefaultNetwork) {
1898 // don't accept this one
Robert Greenwalt78f28112011-08-02 17:18:41 -07001899 if (VDBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001900 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001901 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001902 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001903 teardown(thisNet);
1904 return;
1905 } else {
1906 // tear down the other
1907 NetworkStateTracker otherNet =
1908 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001909 if (DBG) {
1910 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001911 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001912 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001913 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001914 loge("Network declined teardown request");
Robert Greenwalt99910172011-03-29 11:36:28 -07001915 teardown(thisNet);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001916 return;
1917 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001918 }
1919 }
1920 synchronized (ConnectivityService.this) {
1921 // have a new default network, release the transition wakelock in a second
1922 // if it's held. The second pause is to allow apps to reconnect over the
1923 // new network
1924 if (mNetTransitionWakeLock.isHeld()) {
1925 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001926 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001927 mNetTransitionWakeLockSerialNumber, 0),
1928 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001929 }
1930 }
1931 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001932 // this will cause us to come up initially as unconnected and switching
1933 // to connected after our normal pause unless somebody reports us as reall
1934 // disconnected
1935 mDefaultInetConditionPublished = 0;
1936 mDefaultConnectionSequence++;
1937 mInetConditionChangeInFlight = false;
1938 // Don't do this - if we never sign in stay, grey
1939 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001940 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001941 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001942 updateNetworkSettings(thisNet);
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001943 handleConnectivityChange(type, false);
Wink Saville4f0de1e2011-08-04 15:01:58 -07001944 sendConnectedBroadcastDelayed(info, getConnectivityChangeDelay());
Jeff Sharkeyaac2c502011-10-04 16:54:49 -07001945
1946 // notify battery stats service about this network
1947 final String iface = thisNet.getLinkProperties().getInterfaceName();
1948 if (iface != null) {
1949 try {
1950 BatteryStatsService.getService().noteNetworkInterfaceType(iface, type);
1951 } catch (RemoteException e) {
1952 // ignored; service lives in system_server
1953 }
1954 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001955 }
1956
The Android Open Source Project28527d22009-03-03 19:31:44 -08001957 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001958 * After a change in the connectivity state of a network. We're mainly
1959 * concerned with making sure that the list of DNS servers is set up
1960 * according to which networks are connected, and ensuring that the
1961 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001962 */
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001963 private void handleConnectivityChange(int netType, boolean doReset) {
Wink Saville051a6642011-07-13 13:44:13 -07001964 int resetMask = doReset ? NetworkUtils.RESET_ALL_ADDRESSES : 0;
1965
The Android Open Source Project28527d22009-03-03 19:31:44 -08001966 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001967 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001968 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001969 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001970 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001971
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001972 LinkProperties curLp = mCurrentLinkProperties[netType];
1973 LinkProperties newLp = null;
1974
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001975 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001976 newLp = mNetTrackers[netType].getLinkProperties();
Wink Saville051a6642011-07-13 13:44:13 -07001977 if (VDBG) {
1978 log("handleConnectivityChange: changed linkProperty[" + netType + "]:" +
1979 " doReset=" + doReset + " resetMask=" + resetMask +
1980 "\n curLp=" + curLp +
1981 "\n newLp=" + newLp);
1982 }
1983
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001984 if (curLp != null) {
1985 if (curLp.isIdenticalInterfaceName(newLp)) {
1986 CompareResult<LinkAddress> car = curLp.compareAddresses(newLp);
1987 if ((car.removed.size() != 0) || (car.added.size() != 0)) {
1988 for (LinkAddress linkAddr : car.removed) {
1989 if (linkAddr.getAddress() instanceof Inet4Address) {
1990 resetMask |= NetworkUtils.RESET_IPV4_ADDRESSES;
1991 }
1992 if (linkAddr.getAddress() instanceof Inet6Address) {
1993 resetMask |= NetworkUtils.RESET_IPV6_ADDRESSES;
1994 }
Wink Saville051a6642011-07-13 13:44:13 -07001995 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07001996 if (DBG) {
1997 log("handleConnectivityChange: addresses changed" +
1998 " linkProperty[" + netType + "]:" + " resetMask=" + resetMask +
1999 "\n car=" + car);
Wink Saville051a6642011-07-13 13:44:13 -07002000 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002001 } else {
2002 if (DBG) {
2003 log("handleConnectivityChange: address are the same reset per doReset" +
2004 " linkProperty[" + netType + "]:" +
2005 " resetMask=" + resetMask);
2006 }
Wink Saville051a6642011-07-13 13:44:13 -07002007 }
2008 } else {
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002009 resetMask = NetworkUtils.RESET_ALL_ADDRESSES;
Robert Greenwalt78f28112011-08-02 17:18:41 -07002010 if (DBG) {
2011 log("handleConnectivityChange: interface not not equivalent reset both" +
2012 " linkProperty[" + netType + "]:" +
2013 " resetMask=" + resetMask);
2014 }
Wink Saville051a6642011-07-13 13:44:13 -07002015 }
Wink Saville051a6642011-07-13 13:44:13 -07002016 }
Robert Greenwalt34848c02011-03-25 13:09:25 -07002017 if (mNetConfigs[netType].isDefault()) {
Chia-chi Yeh763a11c2011-10-03 15:34:04 -07002018 handleApplyDefaultProxy(newLp.getHttpProxy());
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002019 }
2020 } else {
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002021 if (VDBG) {
2022 log("handleConnectivityChange: changed linkProperty[" + netType + "]:" +
2023 " doReset=" + doReset + " resetMask=" + resetMask +
2024 "\n curLp=" + curLp +
2025 "\n newLp= null");
Robert Greenwalt2034b912009-08-12 16:08:25 -07002026 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002027 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002028 mCurrentLinkProperties[netType] = newLp;
Robert Greenwalt8d777252011-08-15 12:31:55 -07002029 boolean resetDns = updateRoutes(newLp, curLp, mNetConfigs[netType].isDefault());
Robert Greenwalt36ea8692011-06-15 12:22:07 -07002030
Chia-chi Yeh4a243f52011-08-15 15:19:40 -07002031 if (resetMask != 0 || resetDns) {
Robert Greenwalt36ea8692011-06-15 12:22:07 -07002032 LinkProperties linkProperties = mNetTrackers[netType].getLinkProperties();
2033 if (linkProperties != null) {
2034 String iface = linkProperties.getInterfaceName();
2035 if (TextUtils.isEmpty(iface) == false) {
Chia-chi Yeh4a243f52011-08-15 15:19:40 -07002036 if (resetMask != 0) {
Robert Greenwalt8d777252011-08-15 12:31:55 -07002037 if (DBG) log("resetConnections(" + iface + ", " + resetMask + ")");
2038 NetworkUtils.resetConnections(iface, resetMask);
Chia-chi Yeh4a243f52011-08-15 15:19:40 -07002039
2040 // Tell VPN the interface is down. It is a temporary
2041 // but effective fix to make VPN aware of the change.
2042 if ((resetMask & NetworkUtils.RESET_IPV4_ADDRESSES) != 0) {
2043 mVpn.interfaceStatusChanged(iface, false);
2044 }
Robert Greenwalt8d777252011-08-15 12:31:55 -07002045 }
2046 if (resetDns) {
Wink Savillea7d56572011-09-21 11:05:43 -07002047 if (VDBG) log("resetting DNS cache for " + iface);
Robert Greenwalt8d777252011-08-15 12:31:55 -07002048 try {
2049 mNetd.flushInterfaceDnsCache(iface);
2050 } catch (Exception e) {
2051 // never crash - catch them all
Wink Savillea7d56572011-09-21 11:05:43 -07002052 if (DBG) loge("Exception resetting dns cache: " + e);
Robert Greenwalt8d777252011-08-15 12:31:55 -07002053 }
2054 }
Robert Greenwalt36ea8692011-06-15 12:22:07 -07002055 }
2056 }
2057 }
Kazuhiro Ondo07680062011-06-22 21:10:34 -05002058
2059 // TODO: Temporary notifying upstread change to Tethering.
2060 // @see bug/4455071
2061 /** Notify TetheringService if interface name has been changed. */
2062 if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
Wink Saville64e3f782012-07-10 12:37:54 -07002063 PhoneConstants.REASON_LINK_PROPERTIES_CHANGED)) {
Kazuhiro Ondo07680062011-06-22 21:10:34 -05002064 if (isTetheringSupported()) {
2065 mTethering.handleTetherIfaceChange();
2066 }
2067 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002068 }
2069
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002070 /**
2071 * Add and remove routes using the old properties (null if not previously connected),
2072 * new properties (null if becoming disconnected). May even be double null, which
2073 * is a noop.
2074 * Uses isLinkDefault to determine if default routes should be set or conversely if
2075 * host routes should be set to the dns servers
Robert Greenwalt8d777252011-08-15 12:31:55 -07002076 * returns a boolean indicating the routes changed
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002077 */
Robert Greenwalt8d777252011-08-15 12:31:55 -07002078 private boolean updateRoutes(LinkProperties newLp, LinkProperties curLp,
2079 boolean isLinkDefault) {
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002080 Collection<RouteInfo> routesToAdd = null;
Robert Greenwalt98107422011-07-22 11:55:33 -07002081 CompareResult<InetAddress> dnsDiff = new CompareResult<InetAddress>();
2082 CompareResult<RouteInfo> routeDiff = new CompareResult<RouteInfo>();
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002083 if (curLp != null) {
2084 // check for the delta between the current set and the new
Robert Greenwalt98107422011-07-22 11:55:33 -07002085 routeDiff = curLp.compareRoutes(newLp);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002086 dnsDiff = curLp.compareDnses(newLp);
Robert Greenwalt98107422011-07-22 11:55:33 -07002087 } else if (newLp != null) {
2088 routeDiff.added = newLp.getRoutes();
2089 dnsDiff.added = newLp.getDnses();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002090 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002091
Robert Greenwalt8d777252011-08-15 12:31:55 -07002092 boolean routesChanged = (routeDiff.removed.size() != 0 || routeDiff.added.size() != 0);
2093
Robert Greenwalt98107422011-07-22 11:55:33 -07002094 for (RouteInfo r : routeDiff.removed) {
2095 if (isLinkDefault || ! r.isDefaultRoute()) {
Robert Greenwalt49c75d32011-11-02 14:37:19 -07002096 removeRoute(curLp, r, TO_DEFAULT_TABLE);
2097 }
2098 if (isLinkDefault == false) {
2099 // remove from a secondary route table
2100 removeRoute(curLp, r, TO_SECONDARY_TABLE);
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002101 }
Robert Greenwalt98107422011-07-22 11:55:33 -07002102 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002103
Robert Greenwalt98107422011-07-22 11:55:33 -07002104 for (RouteInfo r : routeDiff.added) {
2105 if (isLinkDefault || ! r.isDefaultRoute()) {
Robert Greenwalt49c75d32011-11-02 14:37:19 -07002106 addRoute(newLp, r, TO_DEFAULT_TABLE);
Robert Greenwalt08ae9bc2011-08-09 16:52:58 -07002107 } else {
Robert Greenwalt49c75d32011-11-02 14:37:19 -07002108 // add to a secondary route table
2109 addRoute(newLp, r, TO_SECONDARY_TABLE);
2110
Robert Greenwalt08ae9bc2011-08-09 16:52:58 -07002111 // many radios add a default route even when we don't want one.
2112 // remove the default route unless somebody else has asked for it
2113 String ifaceName = newLp.getInterfaceName();
2114 if (TextUtils.isEmpty(ifaceName) == false && mAddedRoutes.contains(r) == false) {
Wink Savillea7d56572011-09-21 11:05:43 -07002115 if (VDBG) log("Removing " + r + " for interface " + ifaceName);
Robert Greenwalt08ae9bc2011-08-09 16:52:58 -07002116 try {
2117 mNetd.removeRoute(ifaceName, r);
2118 } catch (Exception e) {
2119 // never crash - catch them all
Robert Greenwalt7fa3bdb2011-12-13 15:26:02 -08002120 if (DBG) loge("Exception trying to remove a route: " + e);
Robert Greenwalt08ae9bc2011-08-09 16:52:58 -07002121 }
2122 }
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002123 }
2124 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002125
Robert Greenwalte8d2a4a2011-07-14 14:28:05 -07002126 if (!isLinkDefault) {
2127 // handle DNS routes
Robert Greenwalt8d777252011-08-15 12:31:55 -07002128 if (routesChanged) {
Robert Greenwalt98107422011-07-22 11:55:33 -07002129 // routes changed - remove all old dns entries and add new
2130 if (curLp != null) {
2131 for (InetAddress oldDns : curLp.getDnses()) {
2132 removeRouteToAddress(curLp, oldDns);
2133 }
2134 }
2135 if (newLp != null) {
2136 for (InetAddress newDns : newLp.getDnses()) {
2137 addRouteToAddress(newLp, newDns);
2138 }
Robert Greenwalt03d53da2011-03-22 18:47:42 -07002139 }
Robert Greenwalt8d777252011-08-15 12:31:55 -07002140 } else {
2141 // no change in routes, check for change in dns themselves
2142 for (InetAddress oldDns : dnsDiff.removed) {
2143 removeRouteToAddress(curLp, oldDns);
2144 }
2145 for (InetAddress newDns : dnsDiff.added) {
2146 addRouteToAddress(newLp, newDns);
2147 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002148 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002149 }
Robert Greenwalt8d777252011-08-15 12:31:55 -07002150 return routesChanged;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002151 }
2152
2153
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002154 /**
2155 * Reads the network specific TCP buffer sizes from SystemProperties
2156 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
2157 * wide use
2158 */
2159 public void updateNetworkSettings(NetworkStateTracker nt) {
2160 String key = nt.getTcpBufferSizesPropName();
2161 String bufferSizes = SystemProperties.get(key);
2162
2163 if (bufferSizes.length() == 0) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07002164 if (VDBG) log(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002165
2166 // Setting to default values so we won't be stuck to previous values
2167 key = "net.tcp.buffersize.default";
2168 bufferSizes = SystemProperties.get(key);
2169 }
2170
2171 // Set values in kernel
2172 if (bufferSizes.length() != 0) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07002173 if (VDBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002174 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002175 + "] which comes from [" + key + "]");
2176 }
2177 setBufferSize(bufferSizes);
2178 }
2179 }
2180
2181 /**
2182 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
2183 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
2184 *
2185 * @param bufferSizes in the format of "readMin, readInitial, readMax,
2186 * writeMin, writeInitial, writeMax"
2187 */
2188 private void setBufferSize(String bufferSizes) {
2189 try {
2190 String[] values = bufferSizes.split(",");
2191
2192 if (values.length == 6) {
2193 final String prefix = "/sys/kernel/ipv4/tcp_";
Mike Lockwood0d5916c2011-05-28 13:24:04 -04002194 FileUtils.stringToFile(prefix + "rmem_min", values[0]);
2195 FileUtils.stringToFile(prefix + "rmem_def", values[1]);
2196 FileUtils.stringToFile(prefix + "rmem_max", values[2]);
2197 FileUtils.stringToFile(prefix + "wmem_min", values[3]);
2198 FileUtils.stringToFile(prefix + "wmem_def", values[4]);
2199 FileUtils.stringToFile(prefix + "wmem_max", values[5]);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002200 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08002201 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002202 }
2203 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002204 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07002205 }
2206 }
2207
Robert Greenwalt2034b912009-08-12 16:08:25 -07002208 /**
2209 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
2210 * on the highest priority active net which this process requested.
2211 * If there aren't any, clear it out
2212 */
2213 private void reassessPidDns(int myPid, boolean doBump)
2214 {
Robert Greenwalt78f28112011-08-02 17:18:41 -07002215 if (VDBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07002216 for(int i : mPriorityList) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07002217 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07002218 continue;
2219 }
2220 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07002221 if (nt.getNetworkInfo().isConnected() &&
2222 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07002223 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07002224 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07002225 List pids = mNetRequestersPids[i];
2226 for (int j=0; j<pids.size(); j++) {
2227 Integer pid = (Integer)pids.get(j);
2228 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07002229 Collection<InetAddress> dnses = p.getDnses();
Lorenzo Colittiaa035382011-09-28 22:31:45 -07002230 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07002231 if (doBump) {
2232 bumpDns();
2233 }
2234 return;
2235 }
2236 }
2237 }
2238 }
2239 // nothing found - delete
2240 for (int i = 1; ; i++) {
2241 String prop = "net.dns" + i + "." + myPid;
2242 if (SystemProperties.get(prop).length() == 0) {
2243 if (doBump) {
2244 bumpDns();
2245 }
2246 return;
2247 }
2248 SystemProperties.set(prop, "");
2249 }
2250 }
2251
Robert Greenwalt8ca88762010-12-17 15:20:36 -08002252 // return true if results in a change
Lorenzo Colittiaa035382011-09-28 22:31:45 -07002253 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07002254 int j = 1;
Robert Greenwalt8ca88762010-12-17 15:20:36 -08002255 boolean changed = false;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07002256 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08002257 String dnsString = dns.getHostAddress();
2258 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
2259 changed = true;
yoonsung.nam48d18332012-03-02 19:56:16 +09002260 SystemProperties.set("net.dns" + j + "." + pid, dns.getHostAddress());
Robert Greenwalt8ca88762010-12-17 15:20:36 -08002261 }
yoonsung.nam48d18332012-03-02 19:56:16 +09002262 j++;
Robert Greenwalt2034b912009-08-12 16:08:25 -07002263 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08002264 return changed;
Robert Greenwalt2034b912009-08-12 16:08:25 -07002265 }
2266
2267 private void bumpDns() {
2268 /*
2269 * Bump the property that tells the name resolver library to reread
2270 * the DNS server list from the properties.
2271 */
2272 String propVal = SystemProperties.get("net.dnschange");
2273 int n = 0;
2274 if (propVal.length() != 0) {
2275 try {
2276 n = Integer.parseInt(propVal);
2277 } catch (NumberFormatException e) {}
2278 }
2279 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07002280 /*
2281 * Tell the VMs to toss their DNS caches
2282 */
2283 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
2284 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnuttf444f502011-01-05 17:14:03 -08002285 /*
2286 * Connectivity events can happen before boot has completed ...
2287 */
2288 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt051642b2010-11-02 14:08:23 -07002289 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07002290 }
2291
Chia-chi Yehcc844502011-07-14 18:01:57 -07002292 // Caller must grab mDnsLock.
Robert Greenwaltfce71862011-07-25 16:06:25 -07002293 private boolean updateDns(String network, String iface,
Lorenzo Colittiaa035382011-09-28 22:31:45 -07002294 Collection<InetAddress> dnses, String domains) {
Chia-chi Yehcc844502011-07-14 18:01:57 -07002295 boolean changed = false;
2296 int last = 0;
2297 if (dnses.size() == 0 && mDefaultDns != null) {
2298 ++last;
2299 String value = mDefaultDns.getHostAddress();
2300 if (!value.equals(SystemProperties.get("net.dns1"))) {
2301 if (DBG) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07002302 loge("no dns provided for " + network + " - using " + value);
Chia-chi Yehcc844502011-07-14 18:01:57 -07002303 }
2304 changed = true;
2305 SystemProperties.set("net.dns1", value);
2306 }
2307 } else {
2308 for (InetAddress dns : dnses) {
2309 ++last;
2310 String key = "net.dns" + last;
2311 String value = dns.getHostAddress();
2312 if (!changed && value.equals(SystemProperties.get(key))) {
2313 continue;
2314 }
Robert Greenwalt78f28112011-08-02 17:18:41 -07002315 if (VDBG) {
Chia-chi Yehcc844502011-07-14 18:01:57 -07002316 log("adding dns " + value + " for " + network);
2317 }
2318 changed = true;
2319 SystemProperties.set(key, value);
2320 }
2321 }
2322 for (int i = last + 1; i <= mNumDnsEntries; ++i) {
2323 String key = "net.dns" + i;
Robert Greenwalt78f28112011-08-02 17:18:41 -07002324 if (VDBG) log("erasing " + key);
Chia-chi Yehcc844502011-07-14 18:01:57 -07002325 changed = true;
2326 SystemProperties.set(key, "");
2327 }
2328 mNumDnsEntries = last;
2329
Robert Greenwaltfce71862011-07-25 16:06:25 -07002330 if (changed) {
2331 try {
2332 mNetd.setDnsServersForInterface(iface, NetworkUtils.makeStrings(dnses));
2333 mNetd.setDefaultInterfaceForDns(iface);
2334 } catch (Exception e) {
Robert Greenwalt7fa3bdb2011-12-13 15:26:02 -08002335 if (DBG) loge("exception setting default dns interface: " + e);
Robert Greenwaltfce71862011-07-25 16:06:25 -07002336 }
2337 }
Chia-chi Yehcc844502011-07-14 18:01:57 -07002338 if (!domains.equals(SystemProperties.get("net.dns.search"))) {
2339 SystemProperties.set("net.dns.search", domains);
2340 changed = true;
2341 }
2342 return changed;
2343 }
2344
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002345 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07002346 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002347 NetworkStateTracker nt = mNetTrackers[netType];
2348 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07002349 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002350 if (p == null) return;
2351 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt8ca88762010-12-17 15:20:36 -08002352 boolean changed = false;
Robert Greenwalt34848c02011-03-25 13:09:25 -07002353 if (mNetConfigs[netType].isDefault()) {
Chia-chi Yehcc844502011-07-14 18:01:57 -07002354 String network = nt.getNetworkInfo().getTypeName();
2355 synchronized (mDnsLock) {
2356 if (!mDnsOverridden) {
Lorenzo Colittiaa035382011-09-28 22:31:45 -07002357 changed = updateDns(network, p.getInterfaceName(), dnses, "");
Robert Greenwalt94daa182010-09-01 11:34:05 -07002358 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002359 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002360 } else {
Robert Greenwaltfce71862011-07-25 16:06:25 -07002361 try {
Robert Greenwalt49f762e2011-07-27 10:00:36 -07002362 mNetd.setDnsServersForInterface(p.getInterfaceName(),
Robert Greenwaltfce71862011-07-25 16:06:25 -07002363 NetworkUtils.makeStrings(dnses));
2364 } catch (Exception e) {
Robert Greenwalt7fa3bdb2011-12-13 15:26:02 -08002365 if (DBG) loge("exception setting dns servers: " + e);
Robert Greenwaltfce71862011-07-25 16:06:25 -07002366 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002367 // set per-pid dns for attached secondary nets
2368 List pids = mNetRequestersPids[netType];
2369 for (int y=0; y< pids.size(); y++) {
2370 Integer pid = (Integer)pids.get(y);
Lorenzo Colittiaa035382011-09-28 22:31:45 -07002371 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08002372 }
2373 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08002374 if (changed) bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08002375 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07002376 }
2377
Robert Greenwalt20f819c2011-05-03 19:02:44 -07002378 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07002379 String restoreDefaultNetworkDelayStr = SystemProperties.get(
2380 NETWORK_RESTORE_DELAY_PROP_NAME);
2381 if(restoreDefaultNetworkDelayStr != null &&
2382 restoreDefaultNetworkDelayStr.length() != 0) {
2383 try {
2384 return Integer.valueOf(restoreDefaultNetworkDelayStr);
2385 } catch (NumberFormatException e) {
2386 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002387 }
Robert Greenwalt20f819c2011-05-03 19:02:44 -07002388 // if the system property isn't set, use the value for the apn type
2389 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
2390
2391 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
2392 (mNetConfigs[networkType] != null)) {
2393 ret = mNetConfigs[networkType].restoreTime;
2394 }
2395 return ret;
The Android Open Source Project28527d22009-03-03 19:31:44 -08002396 }
2397
2398 @Override
2399 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07002400 if (mContext.checkCallingOrSelfPermission(
2401 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08002402 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07002403 pw.println("Permission Denial: can't dump ConnectivityService " +
2404 "from from pid=" + Binder.getCallingPid() + ", uid=" +
2405 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08002406 return;
2407 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002408 pw.println();
2409 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08002410 if (nst != null) {
2411 if (nst.getNetworkInfo().isConnected()) {
2412 pw.println("Active network: " + nst.getNetworkInfo().
2413 getTypeName());
2414 }
2415 pw.println(nst.getNetworkInfo());
2416 pw.println(nst);
2417 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07002418 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002419 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08002420
2421 pw.println("Network Requester Pids:");
2422 for (int net : mPriorityList) {
2423 String pidString = net + ": ";
2424 for (Object pid : mNetRequestersPids[net]) {
2425 pidString = pidString + pid.toString() + ", ";
2426 }
2427 pw.println(pidString);
2428 }
2429 pw.println();
2430
2431 pw.println("FeatureUsers:");
2432 for (Object requester : mFeatureUsers) {
2433 pw.println(requester.toString());
2434 }
2435 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002436
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002437 synchronized (this) {
2438 pw.println("NetworkTranstionWakeLock is currently " +
2439 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
2440 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
2441 }
2442 pw.println();
2443
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002444 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07002445
2446 if (mInetLog != null) {
2447 pw.println();
2448 pw.println("Inet condition reports:");
2449 for(int i = 0; i < mInetLog.size(); i++) {
2450 pw.println(mInetLog.get(i));
2451 }
2452 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002453 }
2454
Robert Greenwalt2034b912009-08-12 16:08:25 -07002455 // must be stateless - things change under us.
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -07002456 private class NetworkStateTrackerHandler extends Handler {
2457 public NetworkStateTrackerHandler(Looper looper) {
Wink Saville775aad62010-09-02 19:23:52 -07002458 super(looper);
2459 }
2460
The Android Open Source Project28527d22009-03-03 19:31:44 -08002461 @Override
2462 public void handleMessage(Message msg) {
2463 NetworkInfo info;
2464 switch (msg.what) {
2465 case NetworkStateTracker.EVENT_STATE_CHANGED:
2466 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08002467 int type = info.getType();
2468 NetworkInfo.State state = info.getState();
Robert Greenwalt12c44552009-12-07 11:33:18 -08002469
Wink Savillea7d56572011-09-21 11:05:43 -07002470 if (VDBG || (state == NetworkInfo.State.CONNECTED) ||
2471 (state == NetworkInfo.State.DISCONNECTED)) {
2472 log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07002473 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08002474 state + "/" + info.getDetailedState());
Wink Savillea7d56572011-09-21 11:05:43 -07002475 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002476
2477 // Connectivity state changed:
Robert Greenwalt310fa082012-04-25 13:45:02 -07002478 // [31-14] Reserved for future use
2479 // [13-10] Network subtype (for mobile network, as defined
Robert Greenwalt0659da32009-07-16 17:21:39 -07002480 // by TelephonyManager)
Robert Greenwalt310fa082012-04-25 13:45:02 -07002481 // [9-4] Detailed state ordinal (as defined by
Robert Greenwalt0659da32009-07-16 17:21:39 -07002482 // NetworkInfo.DetailedState)
Robert Greenwalt310fa082012-04-25 13:45:02 -07002483 // [3-0] Network type (as defined by ConnectivityManager)
2484 int eventLogParam = (info.getType() & 0xf) |
2485 ((info.getDetailedState().ordinal() & 0x3f) << 4) |
2486 (info.getSubtype() << 10);
Doug Zongker2fc96232009-12-04 10:31:43 -08002487 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07002488 eventLogParam);
2489
2490 if (info.getDetailedState() ==
2491 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002492 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08002493 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002494 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08002495 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002496 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07002497 // the logic here is, handle SUSPENDED the same as
2498 // DISCONNECTED. The only difference being we are
2499 // broadcasting an intent with NetworkInfo that's
2500 // suspended. This allows the applications an
2501 // opportunity to handle DISCONNECTED and SUSPENDED
2502 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08002503 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08002504 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002505 handleConnect(info);
2506 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002507 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08002508 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002509 info = (NetworkInfo) msg.obj;
Kazuhiro Ondo07680062011-06-22 21:10:34 -05002510 // TODO: Temporary allowing network configuration
2511 // change not resetting sockets.
2512 // @see bug/4455071
2513 handleConnectivityChange(info.getType(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08002514 break;
Jeff Sharkeya1ef1be2012-07-23 13:19:46 -07002515 }
2516 }
2517 }
2518
2519 private class InternalHandler extends Handler {
2520 public InternalHandler(Looper looper) {
2521 super(looper);
2522 }
2523
2524 @Override
2525 public void handleMessage(Message msg) {
2526 NetworkInfo info;
2527 switch (msg.what) {
Robert Greenwaltccb36f92010-09-24 14:32:21 -07002528 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002529 String causedBy = null;
2530 synchronized (ConnectivityService.this) {
2531 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
2532 mNetTransitionWakeLock.isHeld()) {
2533 mNetTransitionWakeLock.release();
2534 causedBy = mNetTransitionWakeLockCausedBy;
2535 }
2536 }
2537 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002538 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002539 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07002540 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002541 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07002542 FeatureUser u = (FeatureUser)msg.obj;
2543 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07002544 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002545 case EVENT_INET_CONDITION_CHANGE:
2546 {
2547 int netType = msg.arg1;
2548 int condition = msg.arg2;
2549 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002550 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002551 }
2552 case EVENT_INET_CONDITION_HOLD_END:
2553 {
2554 int netType = msg.arg1;
2555 int sequence = msg.arg2;
2556 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07002557 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002558 }
2559 case EVENT_SET_NETWORK_PREFERENCE:
2560 {
2561 int preference = msg.arg1;
2562 handleSetNetworkPreference(preference);
2563 break;
2564 }
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002565 case EVENT_SET_MOBILE_DATA:
2566 {
2567 boolean enabled = (msg.arg1 == ENABLED);
2568 handleSetMobileData(enabled);
2569 break;
2570 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002571 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
2572 {
2573 handleDeprecatedGlobalHttpProxy();
Robert Greenwalt34848c02011-03-25 13:09:25 -07002574 break;
2575 }
2576 case EVENT_SET_DEPENDENCY_MET:
2577 {
2578 boolean met = (msg.arg1 == ENABLED);
2579 handleSetDependencyMet(msg.arg2, met);
2580 break;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002581 }
Chia-chi Yehcc844502011-07-14 18:01:57 -07002582 case EVENT_RESTORE_DNS:
2583 {
2584 if (mActiveDefaultNetwork != -1) {
2585 handleDnsConfigurationChange(mActiveDefaultNetwork);
2586 }
2587 break;
2588 }
Wink Saville4f0de1e2011-08-04 15:01:58 -07002589 case EVENT_SEND_STICKY_BROADCAST_INTENT:
2590 {
2591 Intent intent = (Intent)msg.obj;
Wink Saville4f0de1e2011-08-04 15:01:58 -07002592 sendStickyBroadcast(intent);
2593 break;
2594 }
Jeff Sharkey805662d2011-08-19 02:24:24 -07002595 case EVENT_SET_POLICY_DATA_ENABLE: {
2596 final int networkType = msg.arg1;
2597 final boolean enabled = msg.arg2 == ENABLED;
2598 handleSetPolicyDataEnable(networkType, enabled);
2599 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002600 }
2601 }
2602 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002603
2604 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002605 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002606 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002607
2608 if (isTetheringSupported()) {
2609 return mTethering.tether(iface);
2610 } else {
2611 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2612 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002613 }
2614
2615 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002616 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002617 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002618
2619 if (isTetheringSupported()) {
2620 return mTethering.untether(iface);
2621 } else {
2622 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2623 }
2624 }
2625
2626 // javadoc from interface
2627 public int getLastTetherError(String iface) {
2628 enforceTetherAccessPermission();
2629
2630 if (isTetheringSupported()) {
2631 return mTethering.getLastTetherError(iface);
2632 } else {
2633 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2634 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002635 }
2636
2637 // TODO - proper iface API for selection by property, inspection, etc
2638 public String[] getTetherableUsbRegexs() {
2639 enforceTetherAccessPermission();
2640 if (isTetheringSupported()) {
2641 return mTethering.getTetherableUsbRegexs();
2642 } else {
2643 return new String[0];
2644 }
2645 }
2646
2647 public String[] getTetherableWifiRegexs() {
2648 enforceTetherAccessPermission();
2649 if (isTetheringSupported()) {
2650 return mTethering.getTetherableWifiRegexs();
2651 } else {
2652 return new String[0];
2653 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002654 }
2655
Danica Chang96567052010-08-11 14:54:43 -07002656 public String[] getTetherableBluetoothRegexs() {
2657 enforceTetherAccessPermission();
2658 if (isTetheringSupported()) {
2659 return mTethering.getTetherableBluetoothRegexs();
2660 } else {
2661 return new String[0];
2662 }
2663 }
2664
Mike Lockwooded4a1742011-07-19 13:04:47 -07002665 public int setUsbTethering(boolean enable) {
2666 enforceTetherAccessPermission();
2667 if (isTetheringSupported()) {
2668 return mTethering.setUsbTethering(enable);
2669 } else {
2670 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2671 }
2672 }
2673
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002674 // TODO - move iface listing, queries, etc to new module
2675 // javadoc from interface
2676 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002677 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002678 return mTethering.getTetherableIfaces();
2679 }
2680
2681 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002682 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002683 return mTethering.getTetheredIfaces();
2684 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002685
Jeff Sharkey300f08f2011-09-16 01:52:49 -07002686 @Override
2687 public String[] getTetheredIfacePairs() {
2688 enforceTetherAccessPermission();
2689 return mTethering.getTetheredIfacePairs();
2690 }
2691
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002692 public String[] getTetheringErroredIfaces() {
2693 enforceTetherAccessPermission();
2694 return mTethering.getErroredIfaces();
2695 }
2696
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002697 // if ro.tether.denied = true we default to no tethering
2698 // gservices could set the secure setting to 1 though to enable it on a build where it
2699 // had previously been turned off.
2700 public boolean isTetheringSupported() {
2701 enforceTetherAccessPermission();
2702 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08002703 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
2704 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
2705 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002706 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002707
2708 // An API NetworkStateTrackers can call when they lose their network.
2709 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
2710 // whichever happens first. The timer is started by the first caller and not
2711 // restarted by subsequent callers.
2712 public void requestNetworkTransitionWakelock(String forWhom) {
2713 enforceConnectivityInternalPermission();
2714 synchronized (this) {
2715 if (mNetTransitionWakeLock.isHeld()) return;
2716 mNetTransitionWakeLockSerialNumber++;
2717 mNetTransitionWakeLock.acquire();
2718 mNetTransitionWakeLockCausedBy = forWhom;
2719 }
2720 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07002721 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002722 mNetTransitionWakeLockSerialNumber, 0),
2723 mNetTransitionWakeLockTimeout);
2724 return;
2725 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07002726
Robert Greenwalt986c7412010-09-08 15:24:47 -07002727 // 100 percent is full good, 0 is full bad.
2728 public void reportInetCondition(int networkType, int percentage) {
Robert Greenwalt78f28112011-08-02 17:18:41 -07002729 if (VDBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07002730 mContext.enforceCallingOrSelfPermission(
2731 android.Manifest.permission.STATUS_BAR,
2732 "ConnectivityService");
2733
Robert Greenwalt0e80be12010-09-20 14:35:25 -07002734 if (DBG) {
2735 int pid = getCallingPid();
2736 int uid = getCallingUid();
2737 String s = pid + "(" + uid + ") reports inet is " +
2738 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2739 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2740 mInetLog.add(s);
2741 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2742 mInetLog.remove(0);
2743 }
2744 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07002745 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002746 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2747 }
2748
2749 private void handleInetConditionChange(int netType, int condition) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002750 if (mActiveDefaultNetwork == -1) {
Wink Savillea7d56572011-09-21 11:05:43 -07002751 if (DBG) log("handleInetConditionChange: no active default network - ignore");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002752 return;
2753 }
2754 if (mActiveDefaultNetwork != netType) {
Wink Savillea7d56572011-09-21 11:05:43 -07002755 if (DBG) log("handleInetConditionChange: net=" + netType +
2756 " != default=" + mActiveDefaultNetwork + " - ignore");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002757 return;
2758 }
Wink Savillea7d56572011-09-21 11:05:43 -07002759 if (VDBG) {
2760 log("handleInetConditionChange: net=" +
2761 netType + ", condition=" + condition +
2762 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2763 }
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002764 mDefaultInetCondition = condition;
2765 int delay;
2766 if (mInetConditionChangeInFlight == false) {
Wink Savillea7d56572011-09-21 11:05:43 -07002767 if (VDBG) log("handleInetConditionChange: starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002768 // setup a new hold to debounce this
2769 if (mDefaultInetCondition > 50) {
2770 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2771 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2772 } else {
2773 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2774 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2775 }
2776 mInetConditionChangeInFlight = true;
2777 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2778 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2779 } else {
Wink Savillea7d56572011-09-21 11:05:43 -07002780 // we've set the new condition, when this hold ends that will get picked up
2781 if (VDBG) log("handleInetConditionChange: currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002782 }
2783 }
2784
2785 private void handleInetConditionHoldEnd(int netType, int sequence) {
Wink Savillea7d56572011-09-21 11:05:43 -07002786 if (DBG) {
2787 log("handleInetConditionHoldEnd: net=" + netType +
2788 ", condition=" + mDefaultInetCondition +
2789 ", published condition=" + mDefaultInetConditionPublished);
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002790 }
2791 mInetConditionChangeInFlight = false;
2792
2793 if (mActiveDefaultNetwork == -1) {
Wink Savillea7d56572011-09-21 11:05:43 -07002794 if (DBG) log("handleInetConditionHoldEnd: no active default network - ignoring");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002795 return;
2796 }
2797 if (mDefaultConnectionSequence != sequence) {
Wink Savillea7d56572011-09-21 11:05:43 -07002798 if (DBG) log("handleInetConditionHoldEnd: event hold for obsolete network - ignoring");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002799 return;
2800 }
Wink Saville4f0de1e2011-08-04 15:01:58 -07002801 // TODO: Figure out why this optimization sometimes causes a
2802 // change in mDefaultInetCondition to be missed and the
2803 // UI to not be updated.
2804 //if (mDefaultInetConditionPublished == mDefaultInetCondition) {
2805 // if (DBG) log("no change in condition - aborting");
2806 // return;
2807 //}
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002808 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2809 if (networkInfo.isConnected() == false) {
Wink Savillea7d56572011-09-21 11:05:43 -07002810 if (DBG) log("handleInetConditionHoldEnd: default network not connected - ignoring");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002811 return;
2812 }
2813 mDefaultInetConditionPublished = mDefaultInetCondition;
2814 sendInetConditionBroadcast(networkInfo);
2815 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002816 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002817
Chia-chi Yeh763a11c2011-10-03 15:34:04 -07002818 public ProxyProperties getProxy() {
2819 synchronized (mDefaultProxyLock) {
2820 return mDefaultProxyDisabled ? null : mDefaultProxy;
2821 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002822 }
2823
2824 public void setGlobalProxy(ProxyProperties proxyProperties) {
2825 enforceChangePermission();
2826 synchronized (mGlobalProxyLock) {
2827 if (proxyProperties == mGlobalProxy) return;
2828 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2829 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2830
2831 String host = "";
2832 int port = 0;
2833 String exclList = "";
2834 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2835 mGlobalProxy = new ProxyProperties(proxyProperties);
2836 host = mGlobalProxy.getHost();
2837 port = mGlobalProxy.getPort();
2838 exclList = mGlobalProxy.getExclusionList();
2839 } else {
2840 mGlobalProxy = null;
2841 }
2842 ContentResolver res = mContext.getContentResolver();
2843 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2844 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002845 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002846 exclList);
2847 }
2848
2849 if (mGlobalProxy == null) {
2850 proxyProperties = mDefaultProxy;
2851 }
Chia-chi Yehae166612011-09-28 16:38:18 -07002852 //sendProxyBroadcast(proxyProperties);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002853 }
2854
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002855 private void loadGlobalProxy() {
2856 ContentResolver res = mContext.getContentResolver();
2857 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2858 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2859 String exclList = Settings.Secure.getString(res,
2860 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2861 if (!TextUtils.isEmpty(host)) {
2862 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2863 synchronized (mGlobalProxyLock) {
2864 mGlobalProxy = proxyProperties;
2865 }
2866 }
2867 }
2868
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002869 public ProxyProperties getGlobalProxy() {
2870 synchronized (mGlobalProxyLock) {
2871 return mGlobalProxy;
2872 }
2873 }
2874
Chia-chi Yeh763a11c2011-10-03 15:34:04 -07002875 private void handleApplyDefaultProxy(ProxyProperties proxy) {
2876 if (proxy != null && TextUtils.isEmpty(proxy.getHost())) {
2877 proxy = null;
2878 }
2879 synchronized (mDefaultProxyLock) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002880 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2881 if (mDefaultProxy == proxy) return;
Chia-chi Yeh763a11c2011-10-03 15:34:04 -07002882 mDefaultProxy = proxy;
2883
2884 if (!mDefaultProxyDisabled) {
2885 sendProxyBroadcast(proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002886 }
2887 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002888 }
2889
2890 private void handleDeprecatedGlobalHttpProxy() {
2891 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2892 Settings.Secure.HTTP_PROXY);
2893 if (!TextUtils.isEmpty(proxy)) {
2894 String data[] = proxy.split(":");
2895 String proxyHost = data[0];
2896 int proxyPort = 8080;
2897 if (data.length > 1) {
2898 try {
2899 proxyPort = Integer.parseInt(data[1]);
2900 } catch (NumberFormatException e) {
2901 return;
2902 }
2903 }
2904 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2905 setGlobalProxy(p);
2906 }
2907 }
2908
2909 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt611291c2010-12-23 15:51:10 -08002910 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Robert Greenwalt78f28112011-08-02 17:18:41 -07002911 if (DBG) log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002912 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnutt1f2a2ac2011-01-06 11:00:19 -08002913 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2914 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002915 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002916 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002917 }
2918
2919 private static class SettingsObserver extends ContentObserver {
2920 private int mWhat;
2921 private Handler mHandler;
2922 SettingsObserver(Handler handler, int what) {
2923 super(handler);
2924 mHandler = handler;
2925 mWhat = what;
2926 }
2927
2928 void observe(Context context) {
2929 ContentResolver resolver = context.getContentResolver();
2930 resolver.registerContentObserver(Settings.Secure.getUriFor(
2931 Settings.Secure.HTTP_PROXY), false, this);
2932 }
2933
2934 @Override
2935 public void onChange(boolean selfChange) {
2936 mHandler.obtainMessage(mWhat).sendToTarget();
2937 }
2938 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002939
2940 private void log(String s) {
2941 Slog.d(TAG, s);
2942 }
2943
2944 private void loge(String s) {
2945 Slog.e(TAG, s);
2946 }
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002947
repo syncf5de5572011-07-29 23:55:49 -07002948 int convertFeatureToNetworkType(int networkType, String feature) {
2949 int usedNetworkType = networkType;
2950
2951 if(networkType == ConnectivityManager.TYPE_MOBILE) {
2952 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2953 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
2954 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2955 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2956 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2957 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2958 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
2959 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2960 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2961 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2962 usedNetworkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2963 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2964 usedNetworkType = ConnectivityManager.TYPE_MOBILE_IMS;
2965 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2966 usedNetworkType = ConnectivityManager.TYPE_MOBILE_CBS;
2967 } else {
2968 Slog.e(TAG, "Can't match any mobile netTracker!");
2969 }
2970 } else if (networkType == ConnectivityManager.TYPE_WIFI) {
2971 if (TextUtils.equals(feature, "p2p")) {
2972 usedNetworkType = ConnectivityManager.TYPE_WIFI_P2P;
2973 } else {
2974 Slog.e(TAG, "Can't match any wifi netTracker!");
2975 }
2976 } else {
2977 Slog.e(TAG, "Unexpected network type");
Wink Savillef6b76692011-02-24 17:58:51 -08002978 }
repo syncf5de5572011-07-29 23:55:49 -07002979 return usedNetworkType;
Wink Savillef6b76692011-02-24 17:58:51 -08002980 }
Jeff Sharkey921ebf22011-05-19 17:12:49 -07002981
2982 private static <T> T checkNotNull(T value, String message) {
2983 if (value == null) {
2984 throw new NullPointerException(message);
2985 }
2986 return value;
2987 }
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002988
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002989 /**
2990 * Protect a socket from VPN routing rules. This method is used by
Chia-chi Yehbded3eb2011-07-04 03:23:12 -07002991 * VpnBuilder and not available in ConnectivityManager. Permissions
2992 * are checked in Vpn class.
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002993 * @hide
2994 */
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002995 @Override
Chia-chi Yeh9e4ff6e2011-07-14 16:19:19 -07002996 public boolean protectVpn(ParcelFileDescriptor socket) {
2997 try {
2998 int type = mActiveDefaultNetwork;
2999 if (ConnectivityManager.isNetworkTypeValid(type)) {
3000 mVpn.protect(socket, mNetTrackers[type].getLinkProperties().getInterfaceName());
3001 return true;
3002 }
3003 } catch (Exception e) {
3004 // ignore
3005 } finally {
3006 try {
3007 socket.close();
3008 } catch (Exception e) {
3009 // ignore
3010 }
3011 }
3012 return false;
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07003013 }
3014
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07003015 /**
3016 * Prepare for a VPN application. This method is used by VpnDialogs
Chia-chi Yehbded3eb2011-07-04 03:23:12 -07003017 * and not available in ConnectivityManager. Permissions are checked
3018 * in Vpn class.
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07003019 * @hide
3020 */
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07003021 @Override
Chia-chi Yeh3e2e1da2011-07-03 16:52:38 -07003022 public boolean prepareVpn(String oldPackage, String newPackage) {
3023 return mVpn.prepare(oldPackage, newPackage);
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07003024 }
3025
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07003026 /**
3027 * Configure a TUN interface and return its file descriptor. Parameters
3028 * are encoded and opaque to this class. This method is used by VpnBuilder
Chia-chi Yehbded3eb2011-07-04 03:23:12 -07003029 * and not available in ConnectivityManager. Permissions are checked in
3030 * Vpn class.
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07003031 * @hide
3032 */
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07003033 @Override
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07003034 public ParcelFileDescriptor establishVpn(VpnConfig config) {
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07003035 return mVpn.establish(config);
3036 }
3037
Chia-chi Yeh7e026b32011-07-02 17:15:00 -07003038 /**
Chia-chi Yehbded3eb2011-07-04 03:23:12 -07003039 * Start legacy VPN and return an intent to VpnDialogs. This method is
3040 * used by VpnSettings and not available in ConnectivityManager.
3041 * Permissions are checked in Vpn class.
Chia-chi Yeh7e026b32011-07-02 17:15:00 -07003042 * @hide
3043 */
3044 @Override
Chia-chi Yehbded3eb2011-07-04 03:23:12 -07003045 public void startLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) {
3046 mVpn.startLegacyVpn(config, racoon, mtpd);
3047 }
3048
3049 /**
3050 * Return the information of the ongoing legacy VPN. This method is used
3051 * by VpnSettings and not available in ConnectivityManager. Permissions
3052 * are checked in Vpn class.
3053 * @hide
3054 */
3055 @Override
3056 public LegacyVpnInfo getLegacyVpnInfo() {
3057 return mVpn.getLegacyVpnInfo();
Chia-chi Yeh7e026b32011-07-02 17:15:00 -07003058 }
3059
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07003060 /**
3061 * Callback for VPN subsystem. Currently VPN is not adapted to the service
3062 * through NetworkStateTracker since it works differently. For example, it
3063 * needs to override DNS servers but never takes the default routes. It
3064 * relies on another data network, and it could keep existing connections
3065 * alive after reconnecting, switching between networks, or even resuming
3066 * from deep sleep. Calls from applications should be done synchronously
3067 * to avoid race conditions. As these are all hidden APIs, refactoring can
3068 * be done whenever a better abstraction is developed.
3069 */
3070 public class VpnCallback {
3071
3072 private VpnCallback() {
3073 }
3074
Chia-chi Yehcc844502011-07-14 18:01:57 -07003075 public void override(List<String> dnsServers, List<String> searchDomains) {
3076 if (dnsServers == null) {
3077 restore();
3078 return;
3079 }
3080
3081 // Convert DNS servers into addresses.
3082 List<InetAddress> addresses = new ArrayList<InetAddress>();
3083 for (String address : dnsServers) {
3084 // Double check the addresses and remove invalid ones.
3085 try {
3086 addresses.add(InetAddress.parseNumericAddress(address));
3087 } catch (Exception e) {
3088 // ignore
3089 }
3090 }
3091 if (addresses.isEmpty()) {
3092 restore();
3093 return;
3094 }
3095
3096 // Concatenate search domains into a string.
3097 StringBuilder buffer = new StringBuilder();
3098 if (searchDomains != null) {
3099 for (String domain : searchDomains) {
3100 buffer.append(domain).append(' ');
3101 }
3102 }
3103 String domains = buffer.toString().trim();
3104
3105 // Apply DNS changes.
3106 boolean changed = false;
3107 synchronized (mDnsLock) {
Lorenzo Colittiaa035382011-09-28 22:31:45 -07003108 changed = updateDns("VPN", "VPN", addresses, domains);
Chia-chi Yehcc844502011-07-14 18:01:57 -07003109 mDnsOverridden = true;
3110 }
3111 if (changed) {
3112 bumpDns();
3113 }
3114
Chia-chi Yeh763a11c2011-10-03 15:34:04 -07003115 // Temporarily disable the default proxy.
3116 synchronized (mDefaultProxyLock) {
3117 mDefaultProxyDisabled = true;
3118 if (mDefaultProxy != null) {
3119 sendProxyBroadcast(null);
3120 }
3121 }
3122
3123 // TODO: support proxy per network.
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07003124 }
3125
Chia-chi Yehcc844502011-07-14 18:01:57 -07003126 public void restore() {
3127 synchronized (mDnsLock) {
Chia-chi Yeh763a11c2011-10-03 15:34:04 -07003128 if (mDnsOverridden) {
3129 mDnsOverridden = false;
3130 mHandler.sendEmptyMessage(EVENT_RESTORE_DNS);
Chia-chi Yehcc844502011-07-14 18:01:57 -07003131 }
Chia-chi Yehcc844502011-07-14 18:01:57 -07003132 }
Chia-chi Yeh763a11c2011-10-03 15:34:04 -07003133 synchronized (mDefaultProxyLock) {
3134 mDefaultProxyDisabled = false;
3135 if (mDefaultProxy != null) {
3136 sendProxyBroadcast(mDefaultProxy);
3137 }
3138 }
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07003139 }
3140 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08003141}