diff options
-rw-r--r-- | services/core/java/com/android/server/ConnectivityService.java | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index 1489fd8713a..058af4a9a51 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -145,11 +145,13 @@ import java.io.IOException; import java.io.PrintWriter; import java.net.Inet4Address; import java.net.InetAddress; +import java.net.NetworkInterface; import java.net.UnknownHostException; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; import java.util.SortedSet; @@ -1017,10 +1019,76 @@ public class ConnectivityService extends IConnectivityManager.Stub final int uid = Binder.getCallingUid(); NetworkState state = getUnfilteredActiveNetworkState(uid); NetworkInfo ni = getFilteredNetworkInfo(state.networkInfo, state.linkProperties, uid); + + // TODO RepWifi extension. Remove when a low-level, long-lived solution is found. + if (ni == null) { + ni = getNetworkInfoByInterfaceName(IFACE_NAME_WIFI); + } + if (ni == null) { + ni = getNetworkInfoByInterfaceName(IFACE_NAME_TETHER); + } + maybeLogBlockedNetworkInfo(ni, uid); return ni; } + // TODO RepWifi extension. Remove when a low-level, long-lived solution is found. + private static final String IFACE_NAME_WIFI = "wlan0"; + private static final String IFACE_NAME_TETHER = "rndis0"; + private NetworkInfo getNetworkInfoByInterfaceName(String ifname) { + try { + + int type = 0; + String typeName = null; + + if (ifname == IFACE_NAME_WIFI) { + type = ConnectivityManager.TYPE_WIFI; + typeName = "WIFI"; + + } else if (ifname == IFACE_NAME_TETHER) { + type = ConnectivityManager.TYPE_ETHERNET; + typeName = "ETHERNET"; + + } else { + return null; + } + + NetworkInterface nifRep = NetworkInterface.getByName(ifname); + if (nifRep == null) { + return null; + } + + Enumeration<InetAddress> ads = nifRep.getInetAddresses(); + if (ads == null) { + return null; + } + + while (ads.hasMoreElements()) { + + InetAddress a = ads.nextElement(); + + if (a.getHostAddress() != null && a.isSiteLocalAddress()) { + // if the interface has a valid IP address in the range of a + // local network we consider it is connected to an AP. + NetworkInfo ninfo = new NetworkInfo(type, 0, typeName, ""); + ninfo.setDetailedState(DetailedState.CONNECTED, null, null); + ninfo.setIsAvailable(true); + return ninfo; + } + + } + + // if we reached here, the interface has no valid address + return null; + + } catch (Exception e) { + // Exceptions are suppressed to avoid crashing the surrounding system service. + // This is only a best-effort hack. + return null; + } + + } + @Override public Network getActiveNetwork() { enforceAccessPermission(); |