San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import android.app.PendingIntent; |
| 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
| 24 | import android.content.res.Resources; |
| 25 | import android.content.pm.PackageManager; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame^] | 26 | import android.net.NetworkStats; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 27 | import android.net.Uri; |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 28 | import android.net.InterfaceConfiguration; |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 29 | import android.net.INetworkManagementEventObserver; |
Robert Greenwalt | ed12640 | 2011-01-28 15:34:55 -0800 | [diff] [blame] | 30 | import android.net.LinkAddress; |
| 31 | import android.net.NetworkUtils; |
Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 32 | import android.net.wifi.WifiConfiguration; |
| 33 | import android.net.wifi.WifiConfiguration.KeyMgmt; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 34 | import android.os.INetworkManagementService; |
| 35 | import android.os.Handler; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame^] | 36 | import android.os.RemoteException; |
| 37 | import android.os.SystemClock; |
Marco Nelissen | 62dbb22 | 2010-02-18 10:56:30 -0800 | [diff] [blame] | 38 | import android.os.SystemProperties; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 39 | import android.text.TextUtils; |
Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 40 | import android.util.Log; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 41 | import android.util.Slog; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 42 | import java.util.ArrayList; |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 43 | import java.util.NoSuchElementException; |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 44 | import java.util.StringTokenizer; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 45 | import android.provider.Settings; |
| 46 | import android.content.ContentResolver; |
| 47 | import android.database.ContentObserver; |
| 48 | |
| 49 | import java.io.File; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame^] | 50 | import java.io.FileInputStream; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 51 | import java.io.FileReader; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame^] | 52 | import java.io.IOException; |
| 53 | import java.io.InputStream; |
| 54 | import java.io.InputStreamReader; |
| 55 | import java.io.RandomAccessFile; |
| 56 | import java.io.Reader; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 57 | import java.lang.IllegalStateException; |
| 58 | |
| 59 | import java.net.InetAddress; |
| 60 | import java.net.UnknownHostException; |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 61 | import java.util.concurrent.CountDownLatch; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 62 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame^] | 63 | import libcore.io.IoUtils; |
| 64 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 65 | /** |
| 66 | * @hide |
| 67 | */ |
| 68 | class NetworkManagementService extends INetworkManagementService.Stub { |
| 69 | |
| 70 | private static final String TAG = "NetworkManagmentService"; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 71 | private static final boolean DBG = false; |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 72 | private static final String NETD_TAG = "NetdConnector"; |
| 73 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 74 | class NetdResponseCode { |
| 75 | public static final int InterfaceListResult = 110; |
| 76 | public static final int TetherInterfaceListResult = 111; |
| 77 | public static final int TetherDnsFwdTgtListResult = 112; |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 78 | public static final int TtyListResult = 113; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 79 | |
| 80 | public static final int TetherStatusResult = 210; |
| 81 | public static final int IpFwdStatusResult = 211; |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 82 | public static final int InterfaceGetCfgResult = 213; |
Robert Greenwalt | e325392 | 2010-02-18 09:23:25 -0800 | [diff] [blame] | 83 | public static final int SoftapStatusResult = 214; |
| 84 | public static final int UsbRNDISStatusResult = 215; |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 85 | public static final int InterfaceRxCounterResult = 216; |
| 86 | public static final int InterfaceTxCounterResult = 217; |
| 87 | public static final int InterfaceRxThrottleResult = 218; |
| 88 | public static final int InterfaceTxThrottleResult = 219; |
Robert Greenwalt | e325392 | 2010-02-18 09:23:25 -0800 | [diff] [blame] | 89 | |
| 90 | public static final int InterfaceChange = 600; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Binder context for this service |
| 95 | */ |
| 96 | private Context mContext; |
| 97 | |
| 98 | /** |
| 99 | * connector object for communicating with netd |
| 100 | */ |
| 101 | private NativeDaemonConnector mConnector; |
| 102 | |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 103 | private Thread mThread; |
| 104 | private final CountDownLatch mConnectedSignal = new CountDownLatch(1); |
| 105 | |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 106 | private ArrayList<INetworkManagementEventObserver> mObservers; |
| 107 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 108 | /** |
| 109 | * Constructs a new NetworkManagementService instance |
| 110 | * |
| 111 | * @param context Binder context for this service |
| 112 | */ |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 113 | private NetworkManagementService(Context context) { |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 114 | mContext = context; |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 115 | mObservers = new ArrayList<INetworkManagementEventObserver>(); |
| 116 | |
Marco Nelissen | 62dbb22 | 2010-02-18 10:56:30 -0800 | [diff] [blame] | 117 | if ("simulator".equals(SystemProperties.get("ro.product.device"))) { |
| 118 | return; |
| 119 | } |
| 120 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 121 | mConnector = new NativeDaemonConnector( |
Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 122 | new NetdCallbackReceiver(), "netd", 10, NETD_TAG); |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 123 | mThread = new Thread(mConnector, NETD_TAG); |
| 124 | } |
| 125 | |
| 126 | public static NetworkManagementService create(Context context) throws InterruptedException { |
| 127 | NetworkManagementService service = new NetworkManagementService(context); |
| 128 | if (DBG) Slog.d(TAG, "Creating NetworkManagementService"); |
| 129 | service.mThread.start(); |
| 130 | if (DBG) Slog.d(TAG, "Awaiting socket connection"); |
| 131 | service.mConnectedSignal.await(); |
| 132 | if (DBG) Slog.d(TAG, "Connected"); |
| 133 | return service; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 134 | } |
| 135 | |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 136 | public void registerObserver(INetworkManagementEventObserver obs) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 137 | Slog.d(TAG, "Registering observer"); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 138 | mObservers.add(obs); |
| 139 | } |
| 140 | |
| 141 | public void unregisterObserver(INetworkManagementEventObserver obs) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 142 | Slog.d(TAG, "Unregistering observer"); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 143 | mObservers.remove(mObservers.indexOf(obs)); |
| 144 | } |
| 145 | |
| 146 | /** |
Wink Saville | 1a7e671 | 2011-01-09 12:16:38 -0800 | [diff] [blame] | 147 | * Notify our observers of an interface link status change |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 148 | */ |
Wink Saville | 1a7e671 | 2011-01-09 12:16:38 -0800 | [diff] [blame] | 149 | private void notifyInterfaceLinkStatusChanged(String iface, boolean link) { |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 150 | for (INetworkManagementEventObserver obs : mObservers) { |
| 151 | try { |
Wink Saville | 1a7e671 | 2011-01-09 12:16:38 -0800 | [diff] [blame] | 152 | obs.interfaceLinkStatusChanged(iface, link); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 153 | } catch (Exception ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 154 | Slog.w(TAG, "Observer notifier failed", ex); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Notify our observers of an interface addition. |
| 161 | */ |
| 162 | private void notifyInterfaceAdded(String iface) { |
| 163 | for (INetworkManagementEventObserver obs : mObservers) { |
| 164 | try { |
| 165 | obs.interfaceAdded(iface); |
| 166 | } catch (Exception ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 167 | Slog.w(TAG, "Observer notifier failed", ex); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Notify our observers of an interface removal. |
| 174 | */ |
| 175 | private void notifyInterfaceRemoved(String iface) { |
| 176 | for (INetworkManagementEventObserver obs : mObservers) { |
| 177 | try { |
| 178 | obs.interfaceRemoved(iface); |
| 179 | } catch (Exception ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 180 | Slog.w(TAG, "Observer notifier failed", ex); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 185 | /** |
| 186 | * Let us know the daemon is connected |
| 187 | */ |
| 188 | protected void onConnected() { |
| 189 | if (DBG) Slog.d(TAG, "onConnected"); |
| 190 | mConnectedSignal.countDown(); |
| 191 | } |
| 192 | |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 193 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 194 | // |
| 195 | // Netd Callback handling |
| 196 | // |
| 197 | |
| 198 | class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks { |
| 199 | public void onDaemonConnected() { |
Robert Greenwalt | e5c3afb | 2010-09-22 14:32:35 -0700 | [diff] [blame] | 200 | NetworkManagementService.this.onConnected(); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 201 | new Thread() { |
| 202 | public void run() { |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 203 | } |
| 204 | }.start(); |
| 205 | } |
| 206 | public boolean onEvent(int code, String raw, String[] cooked) { |
Robert Greenwalt | e325392 | 2010-02-18 09:23:25 -0800 | [diff] [blame] | 207 | if (code == NetdResponseCode.InterfaceChange) { |
| 208 | /* |
| 209 | * a network interface change occured |
| 210 | * Format: "NNN Iface added <name>" |
| 211 | * "NNN Iface removed <name>" |
| 212 | * "NNN Iface changed <name> <up/down>" |
| 213 | */ |
| 214 | if (cooked.length < 4 || !cooked[1].equals("Iface")) { |
| 215 | throw new IllegalStateException( |
| 216 | String.format("Invalid event from daemon (%s)", raw)); |
| 217 | } |
| 218 | if (cooked[2].equals("added")) { |
| 219 | notifyInterfaceAdded(cooked[3]); |
| 220 | return true; |
| 221 | } else if (cooked[2].equals("removed")) { |
| 222 | notifyInterfaceRemoved(cooked[3]); |
| 223 | return true; |
| 224 | } else if (cooked[2].equals("changed") && cooked.length == 5) { |
Wink Saville | 1a7e671 | 2011-01-09 12:16:38 -0800 | [diff] [blame] | 225 | notifyInterfaceLinkStatusChanged(cooked[3], cooked[4].equals("up")); |
Robert Greenwalt | e325392 | 2010-02-18 09:23:25 -0800 | [diff] [blame] | 226 | return true; |
| 227 | } |
| 228 | throw new IllegalStateException( |
| 229 | String.format("Invalid event from daemon (%s)", raw)); |
| 230 | } |
| 231 | return false; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 235 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 236 | // |
| 237 | // INetworkManagementService members |
| 238 | // |
| 239 | |
| 240 | public String[] listInterfaces() throws IllegalStateException { |
| 241 | mContext.enforceCallingOrSelfPermission( |
| 242 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
| 243 | |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 244 | try { |
| 245 | return mConnector.doListCommand("interface list", NetdResponseCode.InterfaceListResult); |
| 246 | } catch (NativeDaemonConnectorException e) { |
| 247 | throw new IllegalStateException( |
| 248 | "Cannot communicate with native daemon to list interfaces"); |
| 249 | } |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | public InterfaceConfiguration getInterfaceConfig(String iface) throws IllegalStateException { |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 253 | String rsp; |
| 254 | try { |
| 255 | rsp = mConnector.doCommand("interface getcfg " + iface).get(0); |
| 256 | } catch (NativeDaemonConnectorException e) { |
| 257 | throw new IllegalStateException( |
| 258 | "Cannot communicate with native daemon to get interface config"); |
| 259 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 260 | Slog.d(TAG, String.format("rsp <%s>", rsp)); |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 261 | |
Robert Greenwalt | 2d2afd1 | 2011-02-01 15:30:46 -0800 | [diff] [blame] | 262 | // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz [flag1 flag2 flag3] |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 263 | StringTokenizer st = new StringTokenizer(rsp); |
| 264 | |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 265 | InterfaceConfiguration cfg; |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 266 | try { |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 267 | try { |
| 268 | int code = Integer.parseInt(st.nextToken(" ")); |
| 269 | if (code != NetdResponseCode.InterfaceGetCfgResult) { |
| 270 | throw new IllegalStateException( |
| 271 | String.format("Expected code %d, but got %d", |
| 272 | NetdResponseCode.InterfaceGetCfgResult, code)); |
| 273 | } |
| 274 | } catch (NumberFormatException nfe) { |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 275 | throw new IllegalStateException( |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 276 | String.format("Invalid response from daemon (%s)", rsp)); |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 277 | } |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 278 | |
| 279 | cfg = new InterfaceConfiguration(); |
| 280 | cfg.hwAddr = st.nextToken(" "); |
Robert Greenwalt | ed12640 | 2011-01-28 15:34:55 -0800 | [diff] [blame] | 281 | InetAddress addr = null; |
Robert Greenwalt | 2d2afd1 | 2011-02-01 15:30:46 -0800 | [diff] [blame] | 282 | int prefixLength = 0; |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 283 | try { |
Robert Greenwalt | e590373 | 2011-02-22 16:00:42 -0800 | [diff] [blame] | 284 | addr = NetworkUtils.numericToInetAddress(st.nextToken(" ")); |
| 285 | } catch (IllegalArgumentException iae) { |
| 286 | Slog.e(TAG, "Failed to parse ipaddr", iae); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | try { |
Robert Greenwalt | 2d2afd1 | 2011-02-01 15:30:46 -0800 | [diff] [blame] | 290 | prefixLength = Integer.parseInt(st.nextToken(" ")); |
| 291 | } catch (NumberFormatException nfe) { |
| 292 | Slog.e(TAG, "Failed to parse prefixLength", nfe); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 293 | } |
Robert Greenwalt | 04808c2 | 2010-12-13 17:01:41 -0800 | [diff] [blame] | 294 | |
Robert Greenwalt | 2d2afd1 | 2011-02-01 15:30:46 -0800 | [diff] [blame] | 295 | cfg.addr = new LinkAddress(addr, prefixLength); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 296 | cfg.interfaceFlags = st.nextToken("]").trim() +"]"; |
| 297 | } catch (NoSuchElementException nsee) { |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 298 | throw new IllegalStateException( |
| 299 | String.format("Invalid response from daemon (%s)", rsp)); |
| 300 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 301 | Slog.d(TAG, String.format("flags <%s>", cfg.interfaceFlags)); |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 302 | return cfg; |
| 303 | } |
| 304 | |
| 305 | public void setInterfaceConfig( |
| 306 | String iface, InterfaceConfiguration cfg) throws IllegalStateException { |
Robert Greenwalt | ed12640 | 2011-01-28 15:34:55 -0800 | [diff] [blame] | 307 | LinkAddress linkAddr = cfg.addr; |
Robert Greenwalt | 2d2afd1 | 2011-02-01 15:30:46 -0800 | [diff] [blame] | 308 | if (linkAddr == null || linkAddr.getAddress() == null) { |
| 309 | throw new IllegalStateException("Null LinkAddress given"); |
Robert Greenwalt | ed12640 | 2011-01-28 15:34:55 -0800 | [diff] [blame] | 310 | } |
Robert Greenwalt | 2d2afd1 | 2011-02-01 15:30:46 -0800 | [diff] [blame] | 311 | String cmd = String.format("interface setcfg %s %s %d %s", iface, |
| 312 | linkAddr.getAddress().getHostAddress(), |
| 313 | linkAddr.getNetworkPrefixLength(), |
| 314 | cfg.interfaceFlags); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 315 | try { |
| 316 | mConnector.doCommand(cmd); |
| 317 | } catch (NativeDaemonConnectorException e) { |
| 318 | throw new IllegalStateException( |
Robert Greenwalt | 81d5ad5 | 2010-12-16 11:27:38 -0800 | [diff] [blame] | 319 | "Unable to communicate with native daemon to interface setcfg - " + e); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 320 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | public void shutdown() { |
| 324 | if (mContext.checkCallingOrSelfPermission( |
| 325 | android.Manifest.permission.SHUTDOWN) |
| 326 | != PackageManager.PERMISSION_GRANTED) { |
| 327 | throw new SecurityException("Requires SHUTDOWN permission"); |
| 328 | } |
| 329 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 330 | Slog.d(TAG, "Shutting down"); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | public boolean getIpForwardingEnabled() throws IllegalStateException{ |
| 334 | mContext.enforceCallingOrSelfPermission( |
| 335 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
| 336 | |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 337 | ArrayList<String> rsp; |
| 338 | try { |
| 339 | rsp = mConnector.doCommand("ipfwd status"); |
| 340 | } catch (NativeDaemonConnectorException e) { |
| 341 | throw new IllegalStateException( |
| 342 | "Unable to communicate with native daemon to ipfwd status"); |
| 343 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 344 | |
| 345 | for (String line : rsp) { |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 346 | String[] tok = line.split(" "); |
| 347 | if (tok.length < 3) { |
| 348 | Slog.e(TAG, "Malformed response from native daemon: " + line); |
| 349 | return false; |
| 350 | } |
| 351 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 352 | int code = Integer.parseInt(tok[0]); |
| 353 | if (code == NetdResponseCode.IpFwdStatusResult) { |
| 354 | // 211 Forwarding <enabled/disabled> |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 355 | return "enabled".equals(tok[2]); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 356 | } else { |
| 357 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| 358 | } |
| 359 | } |
| 360 | throw new IllegalStateException("Got an empty response"); |
| 361 | } |
| 362 | |
| 363 | public void setIpForwardingEnabled(boolean enable) throws IllegalStateException { |
| 364 | mContext.enforceCallingOrSelfPermission( |
| 365 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 366 | mConnector.doCommand(String.format("ipfwd %sable", (enable ? "en" : "dis"))); |
| 367 | } |
| 368 | |
Robert Greenwalt | bfb7bfa | 2010-03-24 16:03:21 -0700 | [diff] [blame] | 369 | public void startTethering(String[] dhcpRange) |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 370 | throws IllegalStateException { |
| 371 | mContext.enforceCallingOrSelfPermission( |
| 372 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Robert Greenwalt | bfb7bfa | 2010-03-24 16:03:21 -0700 | [diff] [blame] | 373 | // cmd is "tether start first_start first_stop second_start second_stop ..." |
| 374 | // an odd number of addrs will fail |
| 375 | String cmd = "tether start"; |
| 376 | for (String d : dhcpRange) { |
| 377 | cmd += " " + d; |
| 378 | } |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 379 | |
| 380 | try { |
| 381 | mConnector.doCommand(cmd); |
| 382 | } catch (NativeDaemonConnectorException e) { |
| 383 | throw new IllegalStateException("Unable to communicate to native daemon"); |
| 384 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | public void stopTethering() throws IllegalStateException { |
| 388 | mContext.enforceCallingOrSelfPermission( |
| 389 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 390 | try { |
| 391 | mConnector.doCommand("tether stop"); |
| 392 | } catch (NativeDaemonConnectorException e) { |
| 393 | throw new IllegalStateException("Unable to communicate to native daemon to stop tether"); |
| 394 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | public boolean isTetheringStarted() throws IllegalStateException { |
| 398 | mContext.enforceCallingOrSelfPermission( |
| 399 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
| 400 | |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 401 | ArrayList<String> rsp; |
| 402 | try { |
| 403 | rsp = mConnector.doCommand("tether status"); |
| 404 | } catch (NativeDaemonConnectorException e) { |
| 405 | throw new IllegalStateException( |
| 406 | "Unable to communicate to native daemon to get tether status"); |
| 407 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 408 | |
| 409 | for (String line : rsp) { |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 410 | String[] tok = line.split(" "); |
| 411 | if (tok.length < 3) { |
| 412 | throw new IllegalStateException("Malformed response for tether status: " + line); |
| 413 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 414 | int code = Integer.parseInt(tok[0]); |
| 415 | if (code == NetdResponseCode.TetherStatusResult) { |
| 416 | // XXX: Tethering services <started/stopped> <TBD>... |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 417 | return "started".equals(tok[2]); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 418 | } else { |
| 419 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| 420 | } |
| 421 | } |
| 422 | throw new IllegalStateException("Got an empty response"); |
| 423 | } |
| 424 | |
| 425 | public void tetherInterface(String iface) throws IllegalStateException { |
| 426 | mContext.enforceCallingOrSelfPermission( |
| 427 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 428 | try { |
| 429 | mConnector.doCommand("tether interface add " + iface); |
| 430 | } catch (NativeDaemonConnectorException e) { |
| 431 | throw new IllegalStateException( |
| 432 | "Unable to communicate to native daemon for adding tether interface"); |
| 433 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | public void untetherInterface(String iface) { |
| 437 | mContext.enforceCallingOrSelfPermission( |
| 438 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 439 | try { |
| 440 | mConnector.doCommand("tether interface remove " + iface); |
| 441 | } catch (NativeDaemonConnectorException e) { |
| 442 | throw new IllegalStateException( |
| 443 | "Unable to communicate to native daemon for removing tether interface"); |
| 444 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | public String[] listTetheredInterfaces() throws IllegalStateException { |
| 448 | mContext.enforceCallingOrSelfPermission( |
| 449 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 450 | try { |
| 451 | return mConnector.doListCommand( |
| 452 | "tether interface list", NetdResponseCode.TetherInterfaceListResult); |
| 453 | } catch (NativeDaemonConnectorException e) { |
| 454 | throw new IllegalStateException( |
| 455 | "Unable to communicate to native daemon for listing tether interfaces"); |
| 456 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | public void setDnsForwarders(String[] dns) throws IllegalStateException { |
| 460 | mContext.enforceCallingOrSelfPermission( |
| 461 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 462 | try { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 463 | String cmd = "tether dns set"; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 464 | for (String s : dns) { |
Robert Greenwalt | e590373 | 2011-02-22 16:00:42 -0800 | [diff] [blame] | 465 | cmd += " " + NetworkUtils.numericToInetAddress(s).getHostAddress(); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 466 | } |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 467 | try { |
| 468 | mConnector.doCommand(cmd); |
| 469 | } catch (NativeDaemonConnectorException e) { |
| 470 | throw new IllegalStateException( |
| 471 | "Unable to communicate to native daemon for setting tether dns"); |
| 472 | } |
Robert Greenwalt | e590373 | 2011-02-22 16:00:42 -0800 | [diff] [blame] | 473 | } catch (IllegalArgumentException e) { |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 474 | throw new IllegalStateException("Error resolving dns name", e); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | public String[] getDnsForwarders() throws IllegalStateException { |
| 479 | mContext.enforceCallingOrSelfPermission( |
| 480 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 481 | try { |
| 482 | return mConnector.doListCommand( |
| 483 | "tether dns list", NetdResponseCode.TetherDnsFwdTgtListResult); |
| 484 | } catch (NativeDaemonConnectorException e) { |
| 485 | throw new IllegalStateException( |
| 486 | "Unable to communicate to native daemon for listing tether dns"); |
| 487 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | public void enableNat(String internalInterface, String externalInterface) |
| 491 | throws IllegalStateException { |
| 492 | mContext.enforceCallingOrSelfPermission( |
| 493 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 494 | try { |
| 495 | mConnector.doCommand( |
| 496 | String.format("nat enable %s %s", internalInterface, externalInterface)); |
| 497 | } catch (NativeDaemonConnectorException e) { |
| 498 | throw new IllegalStateException( |
| 499 | "Unable to communicate to native daemon for enabling NAT interface"); |
| 500 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | public void disableNat(String internalInterface, String externalInterface) |
| 504 | throws IllegalStateException { |
| 505 | mContext.enforceCallingOrSelfPermission( |
| 506 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 507 | try { |
| 508 | mConnector.doCommand( |
| 509 | String.format("nat disable %s %s", internalInterface, externalInterface)); |
| 510 | } catch (NativeDaemonConnectorException e) { |
| 511 | throw new IllegalStateException( |
| 512 | "Unable to communicate to native daemon for disabling NAT interface"); |
| 513 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 514 | } |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 515 | |
| 516 | public String[] listTtys() throws IllegalStateException { |
| 517 | mContext.enforceCallingOrSelfPermission( |
| 518 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 519 | try { |
| 520 | return mConnector.doListCommand("list_ttys", NetdResponseCode.TtyListResult); |
| 521 | } catch (NativeDaemonConnectorException e) { |
| 522 | throw new IllegalStateException( |
| 523 | "Unable to communicate to native daemon for listing TTYs"); |
| 524 | } |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 525 | } |
| 526 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 527 | public void attachPppd(String tty, String localAddr, String remoteAddr, String dns1Addr, |
| 528 | String dns2Addr) throws IllegalStateException { |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 529 | try { |
| 530 | mContext.enforceCallingOrSelfPermission( |
| 531 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 532 | mConnector.doCommand(String.format("pppd attach %s %s %s %s %s", tty, |
Robert Greenwalt | e590373 | 2011-02-22 16:00:42 -0800 | [diff] [blame] | 533 | NetworkUtils.numericToInetAddress(localAddr).getHostAddress(), |
| 534 | NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(), |
| 535 | NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(), |
| 536 | NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress())); |
| 537 | } catch (IllegalArgumentException e) { |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 538 | throw new IllegalStateException("Error resolving addr", e); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 539 | } catch (NativeDaemonConnectorException e) { |
| 540 | throw new IllegalStateException("Error communicating to native daemon to attach pppd", e); |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
| 544 | public void detachPppd(String tty) throws IllegalStateException { |
| 545 | mContext.enforceCallingOrSelfPermission( |
| 546 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 547 | try { |
| 548 | mConnector.doCommand(String.format("pppd detach %s", tty)); |
| 549 | } catch (NativeDaemonConnectorException e) { |
| 550 | throw new IllegalStateException("Error communicating to native daemon to detach pppd", e); |
| 551 | } |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 552 | } |
Robert Greenwalt | ce1200d | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 553 | |
| 554 | public void startUsbRNDIS() throws IllegalStateException { |
| 555 | mContext.enforceCallingOrSelfPermission( |
| 556 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 557 | try { |
| 558 | mConnector.doCommand("usb startrndis"); |
| 559 | } catch (NativeDaemonConnectorException e) { |
| 560 | throw new IllegalStateException( |
| 561 | "Error communicating to native daemon for starting RNDIS", e); |
| 562 | } |
Robert Greenwalt | ce1200d | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | public void stopUsbRNDIS() throws IllegalStateException { |
| 566 | mContext.enforceCallingOrSelfPermission( |
| 567 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 568 | try { |
| 569 | mConnector.doCommand("usb stoprndis"); |
| 570 | } catch (NativeDaemonConnectorException e) { |
| 571 | throw new IllegalStateException("Error communicating to native daemon", e); |
| 572 | } |
Robert Greenwalt | ce1200d | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | public boolean isUsbRNDISStarted() throws IllegalStateException { |
| 576 | mContext.enforceCallingOrSelfPermission( |
| 577 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 578 | ArrayList<String> rsp; |
| 579 | try { |
| 580 | rsp = mConnector.doCommand("usb rndisstatus"); |
| 581 | } catch (NativeDaemonConnectorException e) { |
| 582 | throw new IllegalStateException( |
| 583 | "Error communicating to native daemon to check RNDIS status", e); |
| 584 | } |
Robert Greenwalt | ce1200d | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 585 | |
| 586 | for (String line : rsp) { |
| 587 | String []tok = line.split(" "); |
| 588 | int code = Integer.parseInt(tok[0]); |
| 589 | if (code == NetdResponseCode.UsbRNDISStatusResult) { |
Mike Lockwood | 51cb9d5 | 2010-03-03 15:12:00 -0500 | [diff] [blame] | 590 | if (tok[3].equals("started")) |
Robert Greenwalt | ce1200d | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 591 | return true; |
| 592 | return false; |
| 593 | } else { |
| 594 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| 595 | } |
| 596 | } |
| 597 | throw new IllegalStateException("Got an empty response"); |
| 598 | } |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 599 | |
Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 600 | public void startAccessPoint(WifiConfiguration wifiConfig, String wlanIface, String softapIface) |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 601 | throws IllegalStateException { |
| 602 | mContext.enforceCallingOrSelfPermission( |
| 603 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 604 | mContext.enforceCallingOrSelfPermission( |
Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 605 | android.Manifest.permission.CHANGE_WIFI_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 606 | try { |
| 607 | mConnector.doCommand(String.format("softap stop " + wlanIface)); |
| 608 | mConnector.doCommand(String.format("softap fwreload " + wlanIface + " AP")); |
| 609 | mConnector.doCommand(String.format("softap start " + wlanIface)); |
| 610 | if (wifiConfig == null) { |
| 611 | mConnector.doCommand(String.format("softap set " + wlanIface + " " + softapIface)); |
| 612 | } else { |
| 613 | /** |
| 614 | * softap set arg1 arg2 arg3 [arg4 arg5 arg6 arg7 arg8] |
| 615 | * argv1 - wlan interface |
| 616 | * argv2 - softap interface |
| 617 | * argv3 - SSID |
| 618 | * argv4 - Security |
| 619 | * argv5 - Key |
| 620 | * argv6 - Channel |
| 621 | * argv7 - Preamble |
| 622 | * argv8 - Max SCB |
| 623 | */ |
Irfan Sheriff | ec8d23a | 2011-02-16 17:00:33 -0800 | [diff] [blame] | 624 | String str = String.format("softap set " + wlanIface + " " + softapIface + |
| 625 | " %s %s %s", convertQuotedString(wifiConfig.SSID), |
| 626 | getSecurityType(wifiConfig), |
| 627 | convertQuotedString(wifiConfig.preSharedKey)); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 628 | mConnector.doCommand(str); |
| 629 | } |
| 630 | mConnector.doCommand(String.format("softap startap")); |
| 631 | } catch (NativeDaemonConnectorException e) { |
| 632 | throw new IllegalStateException("Error communicating to native daemon to start softap", e); |
Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 633 | } |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Irfan Sheriff | a6e559e | 2010-05-24 14:55:42 -0700 | [diff] [blame] | 636 | private String convertQuotedString(String s) { |
Irfan Sheriff | 7baec0f | 2010-05-26 17:16:47 -0700 | [diff] [blame] | 637 | if (s == null) { |
| 638 | return s; |
| 639 | } |
| 640 | /* Replace \ with \\, then " with \" and add quotes at end */ |
| 641 | return '"' + s.replaceAll("\\\\","\\\\\\\\").replaceAll("\"","\\\\\"") + '"'; |
Irfan Sheriff | a6e559e | 2010-05-24 14:55:42 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Irfan Sheriff | ec8d23a | 2011-02-16 17:00:33 -0800 | [diff] [blame] | 644 | private String getSecurityType(WifiConfiguration wifiConfig) { |
| 645 | switch (wifiConfig.getAuthType()) { |
| 646 | case KeyMgmt.WPA_PSK: |
| 647 | return "wpa-psk"; |
| 648 | case KeyMgmt.WPA2_PSK: |
| 649 | return "wpa2-psk"; |
| 650 | default: |
| 651 | return "open"; |
| 652 | } |
| 653 | } |
| 654 | |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 655 | public void stopAccessPoint() throws IllegalStateException { |
| 656 | mContext.enforceCallingOrSelfPermission( |
| 657 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 658 | mContext.enforceCallingOrSelfPermission( |
Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 659 | android.Manifest.permission.CHANGE_WIFI_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 660 | try { |
| 661 | mConnector.doCommand("softap stopap"); |
| 662 | } catch (NativeDaemonConnectorException e) { |
| 663 | throw new IllegalStateException("Error communicating to native daemon to stop soft AP", |
| 664 | e); |
| 665 | } |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 666 | } |
| 667 | |
Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 668 | public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface, String softapIface) |
| 669 | throws IllegalStateException { |
| 670 | mContext.enforceCallingOrSelfPermission( |
| 671 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 672 | mContext.enforceCallingOrSelfPermission( |
| 673 | android.Manifest.permission.CHANGE_WIFI_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 674 | try { |
| 675 | if (wifiConfig == null) { |
| 676 | mConnector.doCommand(String.format("softap set " + wlanIface + " " + softapIface)); |
| 677 | } else { |
| 678 | String str = String.format("softap set " + wlanIface + " " + softapIface |
| 679 | + " %s %s %s", convertQuotedString(wifiConfig.SSID), |
Irfan Sheriff | ec8d23a | 2011-02-16 17:00:33 -0800 | [diff] [blame] | 680 | getSecurityType(wifiConfig), |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 681 | convertQuotedString(wifiConfig.preSharedKey)); |
| 682 | mConnector.doCommand(str); |
| 683 | } |
| 684 | } catch (NativeDaemonConnectorException e) { |
| 685 | throw new IllegalStateException("Error communicating to native daemon to set soft AP", |
| 686 | e); |
Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 687 | } |
| 688 | } |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 689 | |
| 690 | private long getInterfaceCounter(String iface, boolean rx) { |
| 691 | mContext.enforceCallingOrSelfPermission( |
| 692 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
| 693 | try { |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 694 | String rsp; |
| 695 | try { |
| 696 | rsp = mConnector.doCommand( |
| 697 | String.format("interface read%scounter %s", (rx ? "rx" : "tx"), iface)).get(0); |
| 698 | } catch (NativeDaemonConnectorException e1) { |
| 699 | Slog.e(TAG, "Error communicating with native daemon", e1); |
| 700 | return -1; |
| 701 | } |
| 702 | |
| 703 | String[] tok = rsp.split(" "); |
| 704 | if (tok.length < 2) { |
| 705 | Slog.e(TAG, String.format("Malformed response for reading %s interface", |
| 706 | (rx ? "rx" : "tx"))); |
| 707 | return -1; |
| 708 | } |
| 709 | |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 710 | int code; |
| 711 | try { |
| 712 | code = Integer.parseInt(tok[0]); |
| 713 | } catch (NumberFormatException nfe) { |
| 714 | Slog.e(TAG, String.format("Error parsing code %s", tok[0])); |
| 715 | return -1; |
| 716 | } |
| 717 | if ((rx && code != NetdResponseCode.InterfaceRxCounterResult) || ( |
| 718 | !rx && code != NetdResponseCode.InterfaceTxCounterResult)) { |
| 719 | Slog.e(TAG, String.format("Unexpected response code %d", code)); |
| 720 | return -1; |
| 721 | } |
| 722 | return Long.parseLong(tok[1]); |
| 723 | } catch (Exception e) { |
| 724 | Slog.e(TAG, String.format( |
| 725 | "Failed to read interface %s counters", (rx ? "rx" : "tx")), e); |
| 726 | } |
| 727 | return -1; |
| 728 | } |
| 729 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame^] | 730 | /** {@inheritDoc} */ |
| 731 | public NetworkStats getNetworkStatsSummary() { |
| 732 | mContext.enforceCallingOrSelfPermission( |
| 733 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
| 734 | |
| 735 | final String[] ifaces = listInterfaces(); |
| 736 | final NetworkStats.Builder stats = new NetworkStats.Builder( |
| 737 | SystemClock.elapsedRealtime(), ifaces.length); |
| 738 | |
| 739 | for (String iface : ifaces) { |
| 740 | final long rx = getInterfaceCounter(iface, true); |
| 741 | final long tx = getInterfaceCounter(iface, false); |
| 742 | stats.addEntry(iface, NetworkStats.UID_ALL, rx, tx); |
| 743 | } |
| 744 | |
| 745 | return stats.build(); |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame^] | 748 | /** {@inheritDoc} */ |
| 749 | public NetworkStats getNetworkStatsDetail() { |
| 750 | mContext.enforceCallingOrSelfPermission( |
| 751 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
| 752 | |
| 753 | final File procPath = new File("/proc/uid_stat"); |
| 754 | final String[] knownUids = procPath.list(); |
| 755 | final NetworkStats.Builder stats = new NetworkStats.Builder( |
| 756 | SystemClock.elapsedRealtime(), knownUids.length); |
| 757 | |
| 758 | // TODO: kernel module will provide interface-level stats in future |
| 759 | // TODO: migrate these stats to come across netd in bulk, instead of all |
| 760 | // these individual file reads. |
| 761 | for (String uid : knownUids) { |
| 762 | final File uidPath = new File(procPath, uid); |
| 763 | final int rx = readSingleIntFromFile(new File(uidPath, "tcp_rcv")); |
| 764 | final int tx = readSingleIntFromFile(new File(uidPath, "tcp_snd")); |
| 765 | |
| 766 | final int uidInt = Integer.parseInt(uid); |
| 767 | stats.addEntry(NetworkStats.IFACE_ALL, uidInt, rx, tx); |
| 768 | } |
| 769 | |
| 770 | return stats.build(); |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 771 | } |
| 772 | |
San Mehat | f0db6e1 | 2010-04-07 15:22:10 -0700 | [diff] [blame] | 773 | public void setInterfaceThrottle(String iface, int rxKbps, int txKbps) { |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 774 | mContext.enforceCallingOrSelfPermission( |
| 775 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 776 | try { |
| 777 | mConnector.doCommand(String.format( |
| 778 | "interface setthrottle %s %d %d", iface, rxKbps, txKbps)); |
| 779 | } catch (NativeDaemonConnectorException e) { |
| 780 | Slog.e(TAG, "Error communicating with native daemon to set throttle", e); |
| 781 | } |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | private int getInterfaceThrottle(String iface, boolean rx) { |
| 785 | mContext.enforceCallingOrSelfPermission( |
| 786 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
| 787 | try { |
Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 788 | String rsp; |
| 789 | try { |
| 790 | rsp = mConnector.doCommand( |
| 791 | String.format("interface getthrottle %s %s", iface, |
| 792 | (rx ? "rx" : "tx"))).get(0); |
| 793 | } catch (NativeDaemonConnectorException e) { |
| 794 | Slog.e(TAG, "Error communicating with native daemon to getthrottle", e); |
| 795 | return -1; |
| 796 | } |
| 797 | |
| 798 | String[] tok = rsp.split(" "); |
| 799 | if (tok.length < 2) { |
| 800 | Slog.e(TAG, "Malformed response to getthrottle command"); |
| 801 | return -1; |
| 802 | } |
| 803 | |
San Mehat | 91cac64 | 2010-03-31 14:31:36 -0700 | [diff] [blame] | 804 | int code; |
| 805 | try { |
| 806 | code = Integer.parseInt(tok[0]); |
| 807 | } catch (NumberFormatException nfe) { |
| 808 | Slog.e(TAG, String.format("Error parsing code %s", tok[0])); |
| 809 | return -1; |
| 810 | } |
| 811 | if ((rx && code != NetdResponseCode.InterfaceRxThrottleResult) || ( |
| 812 | !rx && code != NetdResponseCode.InterfaceTxThrottleResult)) { |
| 813 | Slog.e(TAG, String.format("Unexpected response code %d", code)); |
| 814 | return -1; |
| 815 | } |
| 816 | return Integer.parseInt(tok[1]); |
| 817 | } catch (Exception e) { |
| 818 | Slog.e(TAG, String.format( |
| 819 | "Failed to read interface %s throttle value", (rx ? "rx" : "tx")), e); |
| 820 | } |
| 821 | return -1; |
| 822 | } |
| 823 | |
| 824 | public int getInterfaceRxThrottle(String iface) { |
| 825 | return getInterfaceThrottle(iface, true); |
| 826 | } |
| 827 | |
| 828 | public int getInterfaceTxThrottle(String iface) { |
| 829 | return getInterfaceThrottle(iface, false); |
| 830 | } |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame^] | 831 | |
| 832 | /** |
| 833 | * Utility method to read a single plain-text {@link Integer} from the given |
| 834 | * {@link File}, usually from a {@code /proc/} filesystem. |
| 835 | */ |
| 836 | private static int readSingleIntFromFile(File file) { |
| 837 | RandomAccessFile f = null; |
| 838 | try { |
| 839 | f = new RandomAccessFile(file, "r"); |
| 840 | byte[] buffer = new byte[(int) f.length()]; |
| 841 | f.readFully(buffer); |
| 842 | return Integer.parseInt(new String(buffer).trim()); |
| 843 | } catch (NumberFormatException e) { |
| 844 | return -1; |
| 845 | } catch (IOException e) { |
| 846 | return -1; |
| 847 | } finally { |
| 848 | IoUtils.closeQuietly(f); |
| 849 | } |
| 850 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 851 | } |