Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 259fdbb0 authored by Mark Chien's avatar Mark Chien Committed by Gerrit Code Review
Browse files

Merge "[Tether05] Migrate UpstreamNetworkMonitor into module"

parents 47db7adc d020e557
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -72,6 +72,7 @@ filegroup {
    srcs: [
        "src/com/android/server/connectivity/tethering/EntitlementManager.java",
        "src/com/android/server/connectivity/tethering/TetheringConfiguration.java",
        "src/com/android/server/connectivity/tethering/UpstreamNetworkMonitor.java",
    ],
}

@@ -84,5 +85,6 @@ filegroup {
        "src/android/net/ip/IpServer.java",
        "src/android/net/ip/RouterAdvertisementDaemon.java",
        "src/android/net/util/InterfaceSet.java",
        "src/android/net/util/PrefixUtils.java",
    ],
}
+1 −2
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@
    <application
        android:label="Tethering"
        android:defaultToDeviceProtectedStorage="true"
        android:directBootAware="true"
        android:usesCleartextTraffic="true">
        android:directBootAware="true">
    </application>
</manifest>
+5 −0
Original line number Diff line number Diff line
@@ -42,16 +42,19 @@ public class PrefixUtils {

    public static final IpPrefix DEFAULT_WIFI_P2P_PREFIX = pfx("192.168.49.0/24");

    /** Get non forwardable prefixes. */
    public static Set<IpPrefix> getNonForwardablePrefixes() {
        final HashSet<IpPrefix> prefixes = new HashSet<>();
        addNonForwardablePrefixes(prefixes);
        return prefixes;
    }

    /** Add non forwardable prefixes. */
    public static void addNonForwardablePrefixes(Set<IpPrefix> prefixes) {
        Collections.addAll(prefixes, MIN_NON_FORWARDABLE_PREFIXES);
    }

    /** Get local prefixes from |lp|. */
    public static Set<IpPrefix> localPrefixesFrom(LinkProperties lp) {
        final HashSet<IpPrefix> localPrefixes = new HashSet<>();
        if (lp == null) return localPrefixes;
@@ -66,10 +69,12 @@ public class PrefixUtils {
        return localPrefixes;
    }

    /** Convert LinkAddress |addr| to IpPrefix. */
    public static IpPrefix asIpPrefix(LinkAddress addr) {
        return new IpPrefix(addr.getAddress(), addr.getPrefixLength());
    }

    /** Convert InetAddress |ip| to IpPrefix. */
    public static IpPrefix ipAddressAsPrefix(InetAddress ip) {
        final int bitLength = (ip instanceof Inet4Address)
                ? NetworkConstants.IPV4_ADDR_BITS
+30 −9
Original line number Diff line number Diff line
@@ -146,6 +146,7 @@ public class UpstreamNetworkMonitor {
        }
    }

    /** Listen all networks. */
    public void startObserveAllNetworks() {
        stop();

@@ -155,6 +156,13 @@ public class UpstreamNetworkMonitor {
        cm().registerNetworkCallback(listenAllRequest, mListenAllCallback, mHandler);
    }

    /**
     * Stop tracking candidate tethering upstreams and release mobile network request.
     * Note: this function is used when tethering is stopped because tethering do not need to
     * choose upstream anymore. But it would not stop default network tracking because
     * EntitlementManager may need to know default network to decide whether to request entitlement
     * check even tethering is not active yet.
     */
    public void stop() {
        releaseMobileNetworkRequest();

@@ -165,6 +173,7 @@ public class UpstreamNetworkMonitor {
        mNetworkMap.clear();
    }

    /** Setup or teardown DUN connection according to |dunRequired|. */
    public void updateMobileRequiresDun(boolean dunRequired) {
        final boolean valueChanged = (mDunRequired != dunRequired);
        mDunRequired = dunRequired;
@@ -174,10 +183,12 @@ public class UpstreamNetworkMonitor {
        }
    }

    /** Whether mobile network is requested. */
    public boolean mobileNetworkRequested() {
        return (mMobileNetworkCallback != null);
    }

    /** Request mobile network if mobile upstream is permitted. */
    public void registerMobileNetworkRequest() {
        if (!isCellularUpstreamPermitted()) {
            mLog.i("registerMobileNetworkRequest() is not permitted");
@@ -209,6 +220,7 @@ public class UpstreamNetworkMonitor {
        cm().requestNetwork(mobileUpstreamRequest, mMobileNetworkCallback, 0, legacyType, mHandler);
    }

    /** Release mobile network request. */
    public void releaseMobileNetworkRequest() {
        if (mMobileNetworkCallback == null) return;

@@ -221,6 +233,9 @@ public class UpstreamNetworkMonitor {
    // becomes available and useful we (a) file a request to keep it up as
    // necessary and (b) change all upstream tracking state accordingly (by
    // passing LinkProperties up to Tethering).
    /**
     * Select the first available network from |perferredTypes|.
     */
    public NetworkState selectPreferredUpstreamType(Iterable<Integer> preferredTypes) {
        final TypeStatePair typeStatePair = findFirstAvailableUpstreamByType(
                mNetworkMap.values(), preferredTypes, isCellularUpstreamPermitted());
@@ -254,7 +269,11 @@ public class UpstreamNetworkMonitor {
        return typeStatePair.ns;
    }

    // Returns null if no current upstream available.
    /**
     * Get current preferred upstream network. If default network is cellular and DUN is required,
     * preferred upstream would be DUN otherwise preferred upstream is the same as default network.
     * Returns null if no current upstream is available.
     */
    public NetworkState getCurrentPreferredUpstream() {
        final NetworkState dfltState = (mDefaultInternetNetwork != null)
                ? mNetworkMap.get(mDefaultInternetNetwork)
@@ -270,10 +289,12 @@ public class UpstreamNetworkMonitor {
        return findFirstDunNetwork(mNetworkMap.values());
    }

    /** Tell UpstreamNetworkMonitor which network is the current upstream of tethering. */
    public void setCurrentUpstream(Network upstream) {
        mTetheringUpstreamNetwork = upstream;
    }

    /** Return local prefixes. */
    public Set<IpPrefix> getLocalPrefixes() {
        return (Set<IpPrefix>) mLocalPrefixes.clone();
    }
@@ -501,8 +522,8 @@ public class UpstreamNetworkMonitor {
            try {
                nc = ConnectivityManager.networkCapabilitiesForType(type);
            } catch (IllegalArgumentException iae) {
                Log.e(TAG, "No NetworkCapabilities mapping for legacy type: " +
                       ConnectivityManager.getNetworkTypeName(type));
                Log.e(TAG, "No NetworkCapabilities mapping for legacy type: "
                        + ConnectivityManager.getNetworkTypeName(type));
                continue;
            }
            if (!isCellularUpstreamPermitted && isCellular(nc)) {
@@ -547,18 +568,18 @@ public class UpstreamNetworkMonitor {
    }

    private static boolean isCellular(NetworkCapabilities nc) {
        return (nc != null) && nc.hasTransport(TRANSPORT_CELLULAR) &&
               nc.hasCapability(NET_CAPABILITY_NOT_VPN);
        return (nc != null) && nc.hasTransport(TRANSPORT_CELLULAR)
               && nc.hasCapability(NET_CAPABILITY_NOT_VPN);
    }

    private static boolean hasCapability(NetworkState ns, int netCap) {
        return (ns != null) && (ns.networkCapabilities != null) &&
               ns.networkCapabilities.hasCapability(netCap);
        return (ns != null) && (ns.networkCapabilities != null)
               && ns.networkCapabilities.hasCapability(netCap);
    }

    private static boolean isNetworkUsableAndNotCellular(NetworkState ns) {
        return (ns != null) && (ns.networkCapabilities != null) && (ns.linkProperties != null) &&
               !isCellular(ns.networkCapabilities);
        return (ns != null) && (ns.networkCapabilities != null) && (ns.linkProperties != null)
               && !isCellular(ns.networkCapabilities);
    }

    private static NetworkState findFirstDunNetwork(Iterable<NetworkState> netStates) {
+1 −0
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ filegroup {
    srcs: [
        "src/com/android/server/connectivity/tethering/EntitlementManagerTest.java",
        "src/com/android/server/connectivity/tethering/TetheringConfigurationTest.java",
        "src/com/android/server/connectivity/tethering/UpstreamNetworkMonitorTest.java",
        "src/android/net/dhcp/DhcpServingParamsParcelExtTest.java",
        "src/android/net/ip/IpServerTest.java",
        "src/android/net/util/InterfaceSetTest.java",
Loading