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; |
| 26 | import android.net.Uri; |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 27 | import android.net.InterfaceConfiguration; |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 28 | import android.net.INetworkManagementEventObserver; |
Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 29 | import android.net.wifi.WifiConfiguration; |
| 30 | import android.net.wifi.WifiConfiguration.KeyMgmt; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 31 | import android.os.INetworkManagementService; |
| 32 | import android.os.Handler; |
Marco Nelissen | 62dbb22 | 2010-02-18 10:56:30 -0800 | [diff] [blame] | 33 | import android.os.SystemProperties; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 34 | import android.text.TextUtils; |
Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 35 | import android.util.Log; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 36 | import android.util.Slog; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 37 | import java.util.ArrayList; |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 38 | import java.util.StringTokenizer; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 39 | import android.provider.Settings; |
| 40 | import android.content.ContentResolver; |
| 41 | import android.database.ContentObserver; |
| 42 | |
| 43 | import java.io.File; |
| 44 | import java.io.FileReader; |
| 45 | import java.lang.IllegalStateException; |
| 46 | |
| 47 | import java.net.InetAddress; |
| 48 | import java.net.UnknownHostException; |
| 49 | |
| 50 | /** |
| 51 | * @hide |
| 52 | */ |
| 53 | class NetworkManagementService extends INetworkManagementService.Stub { |
| 54 | |
| 55 | private static final String TAG = "NetworkManagmentService"; |
| 56 | |
| 57 | class NetdResponseCode { |
| 58 | public static final int InterfaceListResult = 110; |
| 59 | public static final int TetherInterfaceListResult = 111; |
| 60 | public static final int TetherDnsFwdTgtListResult = 112; |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 61 | public static final int TtyListResult = 113; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 62 | |
| 63 | public static final int TetherStatusResult = 210; |
| 64 | public static final int IpFwdStatusResult = 211; |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 65 | public static final int InterfaceGetCfgResult = 213; |
Robert Greenwalt | e325392 | 2010-02-18 09:23:25 -0800 | [diff] [blame] | 66 | public static final int SoftapStatusResult = 214; |
| 67 | public static final int UsbRNDISStatusResult = 215; |
| 68 | |
| 69 | public static final int InterfaceChange = 600; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Binder context for this service |
| 74 | */ |
| 75 | private Context mContext; |
| 76 | |
| 77 | /** |
| 78 | * connector object for communicating with netd |
| 79 | */ |
| 80 | private NativeDaemonConnector mConnector; |
| 81 | |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 82 | private ArrayList<INetworkManagementEventObserver> mObservers; |
| 83 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 84 | /** |
| 85 | * Constructs a new NetworkManagementService instance |
| 86 | * |
| 87 | * @param context Binder context for this service |
| 88 | */ |
San Mehat | d1df8ac | 2010-01-26 06:17:26 -0800 | [diff] [blame] | 89 | public NetworkManagementService(Context context) { |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 90 | mContext = context; |
| 91 | |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 92 | mObservers = new ArrayList<INetworkManagementEventObserver>(); |
| 93 | |
Marco Nelissen | 62dbb22 | 2010-02-18 10:56:30 -0800 | [diff] [blame] | 94 | if ("simulator".equals(SystemProperties.get("ro.product.device"))) { |
| 95 | return; |
| 96 | } |
| 97 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 98 | mConnector = new NativeDaemonConnector( |
| 99 | new NetdCallbackReceiver(), "netd", 10, "NetdConnector"); |
| 100 | Thread thread = new Thread(mConnector, NativeDaemonConnector.class.getName()); |
| 101 | thread.start(); |
| 102 | } |
| 103 | |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 104 | public void registerObserver(INetworkManagementEventObserver obs) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 105 | Slog.d(TAG, "Registering observer"); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 106 | mObservers.add(obs); |
| 107 | } |
| 108 | |
| 109 | public void unregisterObserver(INetworkManagementEventObserver obs) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 110 | Slog.d(TAG, "Unregistering observer"); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 111 | mObservers.remove(mObservers.indexOf(obs)); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Notify our observers of an interface link status change |
| 116 | */ |
| 117 | private void notifyInterfaceLinkStatusChanged(String iface, boolean link) { |
| 118 | for (INetworkManagementEventObserver obs : mObservers) { |
| 119 | try { |
| 120 | obs.interfaceLinkStatusChanged(iface, link); |
| 121 | } catch (Exception ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 122 | Slog.w(TAG, "Observer notifier failed", ex); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Notify our observers of an interface addition. |
| 129 | */ |
| 130 | private void notifyInterfaceAdded(String iface) { |
| 131 | for (INetworkManagementEventObserver obs : mObservers) { |
| 132 | try { |
| 133 | obs.interfaceAdded(iface); |
| 134 | } catch (Exception ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 135 | Slog.w(TAG, "Observer notifier failed", ex); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Notify our observers of an interface removal. |
| 142 | */ |
| 143 | private void notifyInterfaceRemoved(String iface) { |
| 144 | for (INetworkManagementEventObserver obs : mObservers) { |
| 145 | try { |
| 146 | obs.interfaceRemoved(iface); |
| 147 | } catch (Exception ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 148 | Slog.w(TAG, "Observer notifier failed", ex); |
San Mehat | 4d02d00 | 2010-01-22 16:07:46 -0800 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 154 | // |
| 155 | // Netd Callback handling |
| 156 | // |
| 157 | |
| 158 | class NetdCallbackReceiver implements INativeDaemonConnectorCallbacks { |
| 159 | public void onDaemonConnected() { |
| 160 | new Thread() { |
| 161 | public void run() { |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 162 | } |
| 163 | }.start(); |
| 164 | } |
| 165 | public boolean onEvent(int code, String raw, String[] cooked) { |
Robert Greenwalt | e325392 | 2010-02-18 09:23:25 -0800 | [diff] [blame] | 166 | if (code == NetdResponseCode.InterfaceChange) { |
| 167 | /* |
| 168 | * a network interface change occured |
| 169 | * Format: "NNN Iface added <name>" |
| 170 | * "NNN Iface removed <name>" |
| 171 | * "NNN Iface changed <name> <up/down>" |
| 172 | */ |
| 173 | if (cooked.length < 4 || !cooked[1].equals("Iface")) { |
| 174 | throw new IllegalStateException( |
| 175 | String.format("Invalid event from daemon (%s)", raw)); |
| 176 | } |
| 177 | if (cooked[2].equals("added")) { |
| 178 | notifyInterfaceAdded(cooked[3]); |
| 179 | return true; |
| 180 | } else if (cooked[2].equals("removed")) { |
| 181 | notifyInterfaceRemoved(cooked[3]); |
| 182 | return true; |
| 183 | } else if (cooked[2].equals("changed") && cooked.length == 5) { |
| 184 | notifyInterfaceLinkStatusChanged(cooked[3], cooked[4].equals("up")); |
| 185 | return true; |
| 186 | } |
| 187 | throw new IllegalStateException( |
| 188 | String.format("Invalid event from daemon (%s)", raw)); |
| 189 | } |
| 190 | return false; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 194 | private static int stringToIpAddr(String addrString) throws UnknownHostException { |
| 195 | try { |
| 196 | String[] parts = addrString.split("\\."); |
| 197 | if (parts.length != 4) { |
| 198 | throw new UnknownHostException(addrString); |
| 199 | } |
| 200 | |
| 201 | int a = Integer.parseInt(parts[0]) ; |
| 202 | int b = Integer.parseInt(parts[1]) << 8; |
| 203 | int c = Integer.parseInt(parts[2]) << 16; |
| 204 | int d = Integer.parseInt(parts[3]) << 24; |
| 205 | |
| 206 | return a | b | c | d; |
| 207 | } catch (NumberFormatException ex) { |
| 208 | throw new UnknownHostException(addrString); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | public static String intToIpString(int i) { |
| 213 | return ((i >> 24 ) & 0xFF) + "." + ((i >> 16 ) & 0xFF) + "." + ((i >> 8 ) & 0xFF) + "." + |
| 214 | (i & 0xFF); |
| 215 | } |
| 216 | |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 217 | // |
| 218 | // INetworkManagementService members |
| 219 | // |
| 220 | |
| 221 | public String[] listInterfaces() throws IllegalStateException { |
| 222 | mContext.enforceCallingOrSelfPermission( |
| 223 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
| 224 | |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 225 | return mConnector.doListCommand("interface list", NetdResponseCode.InterfaceListResult); |
| 226 | } |
| 227 | |
| 228 | public InterfaceConfiguration getInterfaceConfig(String iface) throws IllegalStateException { |
| 229 | String rsp = mConnector.doCommand("interface getcfg " + iface).get(0); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 230 | Slog.d(TAG, String.format("rsp <%s>", rsp)); |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 231 | |
| 232 | // Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz.zzz.zzz.zzz [flag1 flag2 flag3] |
| 233 | StringTokenizer st = new StringTokenizer(rsp); |
| 234 | |
| 235 | try { |
| 236 | int code = Integer.parseInt(st.nextToken(" ")); |
| 237 | if (code != NetdResponseCode.InterfaceGetCfgResult) { |
| 238 | throw new IllegalStateException( |
| 239 | String.format("Expected code %d, but got %d", |
| 240 | NetdResponseCode.InterfaceGetCfgResult, code)); |
| 241 | } |
| 242 | } catch (NumberFormatException nfe) { |
| 243 | throw new IllegalStateException( |
| 244 | String.format("Invalid response from daemon (%s)", rsp)); |
| 245 | } |
| 246 | |
| 247 | InterfaceConfiguration cfg = new InterfaceConfiguration(); |
| 248 | cfg.hwAddr = st.nextToken(" "); |
| 249 | try { |
| 250 | cfg.ipAddr = stringToIpAddr(st.nextToken(" ")); |
| 251 | } catch (UnknownHostException uhe) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 252 | Slog.e(TAG, "Failed to parse ipaddr", uhe); |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 253 | cfg.ipAddr = 0; |
| 254 | } |
| 255 | |
| 256 | try { |
| 257 | cfg.netmask = stringToIpAddr(st.nextToken(" ")); |
| 258 | } catch (UnknownHostException uhe) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 259 | Slog.e(TAG, "Failed to parse netmask", uhe); |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 260 | cfg.netmask = 0; |
| 261 | } |
Robert Greenwalt | 65ae29b | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 262 | cfg.interfaceFlags = st.nextToken("]").trim() +"]"; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 263 | Slog.d(TAG, String.format("flags <%s>", cfg.interfaceFlags)); |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 264 | return cfg; |
| 265 | } |
| 266 | |
| 267 | public void setInterfaceConfig( |
| 268 | String iface, InterfaceConfiguration cfg) throws IllegalStateException { |
Robert Greenwalt | 65ae29b | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 269 | String cmd = String.format("interface setcfg %s %s %s %s", iface, |
San Mehat | ed4fc8a | 2010-01-22 12:28:36 -0800 | [diff] [blame] | 270 | intToIpString(cfg.ipAddr), intToIpString(cfg.netmask), cfg.interfaceFlags); |
| 271 | mConnector.doCommand(cmd); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | public void shutdown() { |
| 275 | if (mContext.checkCallingOrSelfPermission( |
| 276 | android.Manifest.permission.SHUTDOWN) |
| 277 | != PackageManager.PERMISSION_GRANTED) { |
| 278 | throw new SecurityException("Requires SHUTDOWN permission"); |
| 279 | } |
| 280 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 281 | Slog.d(TAG, "Shutting down"); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | public boolean getIpForwardingEnabled() throws IllegalStateException{ |
| 285 | mContext.enforceCallingOrSelfPermission( |
| 286 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
| 287 | |
| 288 | ArrayList<String> rsp = mConnector.doCommand("ipfwd status"); |
| 289 | |
| 290 | for (String line : rsp) { |
| 291 | String []tok = line.split(" "); |
| 292 | int code = Integer.parseInt(tok[0]); |
| 293 | if (code == NetdResponseCode.IpFwdStatusResult) { |
| 294 | // 211 Forwarding <enabled/disabled> |
| 295 | if (tok.length !=2) { |
| 296 | throw new IllegalStateException( |
| 297 | String.format("Malformatted list entry '%s'", line)); |
| 298 | } |
| 299 | if (tok[2].equals("enabled")) |
| 300 | return true; |
| 301 | return false; |
| 302 | } else { |
| 303 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| 304 | } |
| 305 | } |
| 306 | throw new IllegalStateException("Got an empty response"); |
| 307 | } |
| 308 | |
| 309 | public void setIpForwardingEnabled(boolean enable) throws IllegalStateException { |
| 310 | mContext.enforceCallingOrSelfPermission( |
| 311 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 312 | mConnector.doCommand(String.format("ipfwd %sable", (enable ? "en" : "dis"))); |
| 313 | } |
| 314 | |
Robert Greenwalt | bfb7bfa | 2010-03-24 16:03:21 -0700 | [diff] [blame^] | 315 | public void startTethering(String[] dhcpRange) |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 316 | throws IllegalStateException { |
| 317 | mContext.enforceCallingOrSelfPermission( |
| 318 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Robert Greenwalt | bfb7bfa | 2010-03-24 16:03:21 -0700 | [diff] [blame^] | 319 | // cmd is "tether start first_start first_stop second_start second_stop ..." |
| 320 | // an odd number of addrs will fail |
| 321 | String cmd = "tether start"; |
| 322 | for (String d : dhcpRange) { |
| 323 | cmd += " " + d; |
| 324 | } |
| 325 | mConnector.doCommand(cmd); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | public void stopTethering() throws IllegalStateException { |
| 329 | mContext.enforceCallingOrSelfPermission( |
| 330 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 331 | mConnector.doCommand("tether stop"); |
| 332 | } |
| 333 | |
| 334 | public boolean isTetheringStarted() throws IllegalStateException { |
| 335 | mContext.enforceCallingOrSelfPermission( |
| 336 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
| 337 | |
| 338 | ArrayList<String> rsp = mConnector.doCommand("tether status"); |
| 339 | |
| 340 | for (String line : rsp) { |
| 341 | String []tok = line.split(" "); |
| 342 | int code = Integer.parseInt(tok[0]); |
| 343 | if (code == NetdResponseCode.TetherStatusResult) { |
| 344 | // XXX: Tethering services <started/stopped> <TBD>... |
| 345 | if (tok[2].equals("started")) |
| 346 | return true; |
| 347 | return false; |
| 348 | } else { |
| 349 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| 350 | } |
| 351 | } |
| 352 | throw new IllegalStateException("Got an empty response"); |
| 353 | } |
| 354 | |
| 355 | public void tetherInterface(String iface) throws IllegalStateException { |
| 356 | mContext.enforceCallingOrSelfPermission( |
| 357 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 358 | mConnector.doCommand("tether interface add " + iface); |
| 359 | } |
| 360 | |
| 361 | public void untetherInterface(String iface) { |
| 362 | mContext.enforceCallingOrSelfPermission( |
| 363 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 364 | mConnector.doCommand("tether interface remove " + iface); |
| 365 | } |
| 366 | |
| 367 | public String[] listTetheredInterfaces() throws IllegalStateException { |
| 368 | mContext.enforceCallingOrSelfPermission( |
| 369 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 370 | return mConnector.doListCommand( |
| 371 | "tether interface list", NetdResponseCode.TetherInterfaceListResult); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | public void setDnsForwarders(String[] dns) throws IllegalStateException { |
| 375 | mContext.enforceCallingOrSelfPermission( |
| 376 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 377 | try { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 378 | String cmd = "tether dns set"; |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 379 | for (String s : dns) { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 380 | cmd += " " + InetAddress.getByName(s).getHostAddress(); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 381 | } |
| 382 | mConnector.doCommand(cmd); |
| 383 | } catch (UnknownHostException e) { |
| 384 | throw new IllegalStateException("Error resolving dns name", e); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | public String[] getDnsForwarders() throws IllegalStateException { |
| 389 | mContext.enforceCallingOrSelfPermission( |
| 390 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 391 | return mConnector.doListCommand( |
| 392 | "tether dns list", NetdResponseCode.TetherDnsFwdTgtListResult); |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | public void enableNat(String internalInterface, String externalInterface) |
| 396 | throws IllegalStateException { |
| 397 | mContext.enforceCallingOrSelfPermission( |
| 398 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 399 | mConnector.doCommand( |
| 400 | String.format("nat enable %s %s", internalInterface, externalInterface)); |
| 401 | } |
| 402 | |
| 403 | public void disableNat(String internalInterface, String externalInterface) |
| 404 | throws IllegalStateException { |
| 405 | mContext.enforceCallingOrSelfPermission( |
| 406 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 407 | mConnector.doCommand( |
| 408 | String.format("nat disable %s %s", internalInterface, externalInterface)); |
| 409 | } |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 410 | |
| 411 | public String[] listTtys() throws IllegalStateException { |
| 412 | mContext.enforceCallingOrSelfPermission( |
| 413 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
| 414 | return mConnector.doListCommand("list_ttys", NetdResponseCode.TtyListResult); |
| 415 | } |
| 416 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 417 | public void attachPppd(String tty, String localAddr, String remoteAddr, String dns1Addr, |
| 418 | String dns2Addr) throws IllegalStateException { |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 419 | try { |
| 420 | mContext.enforceCallingOrSelfPermission( |
| 421 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 422 | mConnector.doCommand(String.format("pppd attach %s %s %s %s %s", tty, |
| 423 | InetAddress.getByName(localAddr).getHostAddress(), |
| 424 | InetAddress.getByName(remoteAddr).getHostAddress(), |
| 425 | InetAddress.getByName(dns1Addr).getHostAddress(), |
| 426 | InetAddress.getByName(dns2Addr).getHostAddress())); |
San Mehat | 72759df | 2010-01-19 13:50:37 -0800 | [diff] [blame] | 427 | } catch (UnknownHostException e) { |
| 428 | throw new IllegalStateException("Error resolving addr", e); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | public void detachPppd(String tty) throws IllegalStateException { |
| 433 | mContext.enforceCallingOrSelfPermission( |
| 434 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 435 | mConnector.doCommand(String.format("pppd detach %s", tty)); |
| 436 | } |
Robert Greenwalt | ce1200d | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 437 | |
| 438 | public void startUsbRNDIS() throws IllegalStateException { |
| 439 | mContext.enforceCallingOrSelfPermission( |
| 440 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 441 | mConnector.doCommand("usb startrndis"); |
| 442 | } |
| 443 | |
| 444 | public void stopUsbRNDIS() throws IllegalStateException { |
| 445 | mContext.enforceCallingOrSelfPermission( |
| 446 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 447 | mConnector.doCommand("usb stoprndis"); |
| 448 | } |
| 449 | |
| 450 | public boolean isUsbRNDISStarted() throws IllegalStateException { |
| 451 | mContext.enforceCallingOrSelfPermission( |
| 452 | android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService"); |
| 453 | ArrayList<String> rsp = mConnector.doCommand("usb rndisstatus"); |
| 454 | |
| 455 | for (String line : rsp) { |
| 456 | String []tok = line.split(" "); |
| 457 | int code = Integer.parseInt(tok[0]); |
| 458 | if (code == NetdResponseCode.UsbRNDISStatusResult) { |
Mike Lockwood | 51cb9d5 | 2010-03-03 15:12:00 -0500 | [diff] [blame] | 459 | if (tok[3].equals("started")) |
Robert Greenwalt | ce1200d | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 460 | return true; |
| 461 | return false; |
| 462 | } else { |
| 463 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| 464 | } |
| 465 | } |
| 466 | throw new IllegalStateException("Got an empty response"); |
| 467 | } |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 468 | |
Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 469 | public void startAccessPoint(WifiConfiguration wifiConfig, String wlanIface, String softapIface) |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 470 | throws IllegalStateException { |
| 471 | mContext.enforceCallingOrSelfPermission( |
| 472 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 473 | mContext.enforceCallingOrSelfPermission( |
Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 474 | android.Manifest.permission.CHANGE_WIFI_STATE, "NetworkManagementService"); |
| 475 | mConnector.doCommand(String.format("softap stop " + wlanIface)); |
| 476 | mConnector.doCommand(String.format("softap fwreload " + wlanIface + " AP")); |
| 477 | mConnector.doCommand(String.format("softap start " + wlanIface)); |
Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 478 | if (wifiConfig == null) { |
Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 479 | mConnector.doCommand(String.format("softap set " + wlanIface + " " + softapIface)); |
Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 480 | } else { |
| 481 | /** |
| 482 | * softap set arg1 arg2 arg3 [arg4 arg5 arg6 arg7 arg8] |
| 483 | * argv1 - wlan interface |
| 484 | * argv2 - softap interface |
| 485 | * argv3 - SSID |
| 486 | * argv4 - Security |
| 487 | * argv5 - Key |
| 488 | * argv6 - Channel |
| 489 | * argv7 - Preamble |
| 490 | * argv8 - Max SCB |
Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 491 | */ |
Irfan Sheriff | 723f109 | 2010-03-19 15:55:58 -0700 | [diff] [blame] | 492 | String str = String.format("softap set " + wlanIface + " " + softapIface + |
| 493 | " \"%s\" %s %s", wifiConfig.SSID, |
Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 494 | wifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_PSK) ? |
| 495 | "wpa2-psk" : "open", |
| 496 | wifiConfig.preSharedKey); |
| 497 | mConnector.doCommand(str); |
| 498 | } |
| 499 | mConnector.doCommand(String.format("softap startap")); |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | public void stopAccessPoint() throws IllegalStateException { |
| 503 | mContext.enforceCallingOrSelfPermission( |
| 504 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 505 | mContext.enforceCallingOrSelfPermission( |
Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 506 | android.Manifest.permission.CHANGE_WIFI_STATE, "NetworkManagementService"); |
Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 507 | mConnector.doCommand("softap stopap"); |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 508 | } |
| 509 | |
Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 510 | public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface, String softapIface) |
| 511 | throws IllegalStateException { |
| 512 | mContext.enforceCallingOrSelfPermission( |
| 513 | android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); |
| 514 | mContext.enforceCallingOrSelfPermission( |
| 515 | android.Manifest.permission.CHANGE_WIFI_STATE, "NetworkManagementService"); |
| 516 | if (wifiConfig == null) { |
| 517 | mConnector.doCommand(String.format("softap set " + wlanIface + " " + softapIface)); |
| 518 | } else { |
| 519 | String str = String.format("softap set " + wlanIface + " " + softapIface + |
Irfan Sheriff | 723f109 | 2010-03-19 15:55:58 -0700 | [diff] [blame] | 520 | " \"%s\" %s %s", wifiConfig.SSID, |
Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 521 | wifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_PSK) ? |
| 522 | "wpa2-psk" : "open", |
| 523 | wifiConfig.preSharedKey); |
| 524 | mConnector.doCommand(str); |
| 525 | } |
| 526 | } |
San Mehat | 873f214 | 2010-01-14 10:25:07 -0800 | [diff] [blame] | 527 | } |