Project

General

Profile

Issue #1792 ยป 0001-Fix-RepWifi-connection-not-correctly-reported-to-use.patch

Wolfgang Wiedmeyer, 08/13/2017 08:02 PM

View differences:

services/core/java/com/android/server/ConnectivityService.java
145 145
import java.io.PrintWriter;
146 146
import java.net.Inet4Address;
147 147
import java.net.InetAddress;
148
import java.net.NetworkInterface;
148 149
import java.net.UnknownHostException;
149 150
import java.util.ArrayDeque;
150 151
import java.util.ArrayList;
151 152
import java.util.Arrays;
152 153
import java.util.Collection;
154
import java.util.Enumeration;
153 155
import java.util.HashMap;
154 156
import java.util.HashSet;
155 157
import java.util.SortedSet;
......
1017 1019
        final int uid = Binder.getCallingUid();
1018 1020
        NetworkState state = getUnfilteredActiveNetworkState(uid);
1019 1021
        NetworkInfo ni = getFilteredNetworkInfo(state.networkInfo, state.linkProperties, uid);
1022

  
1023
        // TODO RepWifi extension. Remove when a low-level, long-lived solution is found.
1024
        if (ni == null) {
1025
            ni = getNetworkInfoByInterfaceName(IFACE_NAME_WIFI);
1026
        }
1027
        if (ni == null) {
1028
            ni = getNetworkInfoByInterfaceName(IFACE_NAME_TETHER);
1029
        }
1030

  
1020 1031
        maybeLogBlockedNetworkInfo(ni, uid);
1021 1032
        return ni;
1022 1033
    }
1034

  
1035
    // TODO RepWifi extension. Remove when a low-level, long-lived solution is found.
1036
    private static final String IFACE_NAME_WIFI = "wlan0";
1037
    private static final String IFACE_NAME_TETHER = "rndis0";
1038
    private NetworkInfo getNetworkInfoByInterfaceName(String ifname) {
1039
        try {
1040

  
1041
            int type = 0;
1042
            String typeName = null;
1043

  
1044
            if (ifname == IFACE_NAME_WIFI) {
1045
                type = ConnectivityManager.TYPE_WIFI;
1046
                typeName = "WIFI";
1047

  
1048
            } else if (ifname == IFACE_NAME_TETHER) {
1049
                type = ConnectivityManager.TYPE_ETHERNET;
1050
                typeName = "ETHERNET";
1051

  
1052
            } else {
1053
                return null;
1054
            }
1055

  
1056
            NetworkInterface nifRep = NetworkInterface.getByName(ifname);
1057
            if (nifRep == null) {
1058
                return null;
1059
            }
1060

  
1061
            Enumeration<InetAddress> ads = nifRep.getInetAddresses();
1062
            if (ads == null) {
1063
                return null;
1064
            }
1065

  
1066
            while (ads.hasMoreElements()) {
1067

  
1068
                InetAddress a = ads.nextElement();
1069

  
1070
                if (a.getHostAddress() != null && a.isSiteLocalAddress()) {
1071
                    // if the interface has a valid IP address in the range of a
1072
                    // local network we consider it is connected to an AP.
1073
                    NetworkInfo ninfo = new NetworkInfo(type, 0, typeName, "");
1074
                    ninfo.setDetailedState(DetailedState.CONNECTED, null, null);
1075
                    ninfo.setIsAvailable(true);
1076
                    return ninfo;
1077
                }
1078

  
1079
            }
1080

  
1081
            // if we reached here, the interface has no valid address
1082
            return null;
1083

  
1084
        } catch (Exception e) {
1085
            // Exceptions are suppressed to avoid crashing the surrounding system service.
1086
            // This is only a best-effort hack.
1087
            return null;
1088
        }
1089

  
1090
    }
1023 1091

  
1024 1092
    @Override
1025 1093
    public Network getActiveNetwork() {
    (1-1/1)