Split NetworkUtils and NetworkUtilsInternal
NetworkUtils is planned to move to a dedicated JAR for connectivity
classes, while NetworkUtilsInternal would stay in the
frameworks-minus-apex JAR, in the com.android.internal.net package.
Bug: 171540887
Test: m, boots, wifi working
atest FrameworksNetTests
Change-Id: I3d38d72ad23a4bf84af823c7baeb6fed25c0665f
This commit is contained in:
@@ -16,14 +16,9 @@
|
|||||||
|
|
||||||
package android.net;
|
package android.net;
|
||||||
|
|
||||||
import static android.system.OsConstants.AF_INET;
|
|
||||||
import static android.system.OsConstants.AF_INET6;
|
|
||||||
|
|
||||||
import android.annotation.NonNull;
|
|
||||||
import android.compat.annotation.UnsupportedAppUsage;
|
import android.compat.annotation.UnsupportedAppUsage;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.system.ErrnoException;
|
import android.system.ErrnoException;
|
||||||
import android.system.Os;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.util.Pair;
|
import android.util.Pair;
|
||||||
|
|
||||||
@@ -155,14 +150,6 @@ public class NetworkUtils {
|
|||||||
*/
|
*/
|
||||||
public static native Network getDnsNetwork() throws ErrnoException;
|
public static native Network getDnsNetwork() throws ErrnoException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Allow/Disallow creating AF_INET/AF_INET6 sockets and DNS lookups for current process.
|
|
||||||
*
|
|
||||||
* @param allowNetworking whether to allow or disallow creating AF_INET/AF_INET6 sockets
|
|
||||||
* and DNS lookups.
|
|
||||||
*/
|
|
||||||
public static native void setAllowNetworkingForProcess(boolean allowNetworking);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the tcp repair window associated with the {@code fd}.
|
* Get the tcp repair window associated with the {@code fd}.
|
||||||
*
|
*
|
||||||
@@ -452,60 +439,4 @@ public class NetworkUtils {
|
|||||||
return routedIPCount;
|
return routedIPCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final int[] ADDRESS_FAMILIES = new int[] {AF_INET, AF_INET6};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if the hostname is weakly validated.
|
|
||||||
* @param hostname Name of host to validate.
|
|
||||||
* @return True if it's a valid-ish hostname.
|
|
||||||
*
|
|
||||||
* @hide
|
|
||||||
*/
|
|
||||||
public static boolean isWeaklyValidatedHostname(@NonNull String hostname) {
|
|
||||||
// TODO(b/34953048): Use a validation method that permits more accurate,
|
|
||||||
// but still inexpensive, checking of likely valid DNS hostnames.
|
|
||||||
final String weakHostnameRegex = "^[a-zA-Z0-9_.-]+$";
|
|
||||||
if (!hostname.matches(weakHostnameRegex)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int address_family : ADDRESS_FAMILIES) {
|
|
||||||
if (Os.inet_pton(address_family, hostname) != null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Safely multiple a value by a rational.
|
|
||||||
* <p>
|
|
||||||
* Internally it uses integer-based math whenever possible, but switches
|
|
||||||
* over to double-based math if values would overflow.
|
|
||||||
* @hide
|
|
||||||
*/
|
|
||||||
public static long multiplySafeByRational(long value, long num, long den) {
|
|
||||||
if (den == 0) {
|
|
||||||
throw new ArithmeticException("Invalid Denominator");
|
|
||||||
}
|
|
||||||
long x = value;
|
|
||||||
long y = num;
|
|
||||||
|
|
||||||
// Logic shamelessly borrowed from Math.multiplyExact()
|
|
||||||
long r = x * y;
|
|
||||||
long ax = Math.abs(x);
|
|
||||||
long ay = Math.abs(y);
|
|
||||||
if (((ax | ay) >>> 31 != 0)) {
|
|
||||||
// Some bits greater than 2^31 that might cause overflow
|
|
||||||
// Check the result using the divide operator
|
|
||||||
// and check for the special case of Long.MIN_VALUE * -1
|
|
||||||
if (((y != 0) && (r / y != x)) ||
|
|
||||||
(x == Long.MIN_VALUE && y == -1)) {
|
|
||||||
// Use double math to avoid overflowing
|
|
||||||
return (long) (((double) num / den) * value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return r / den;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2008, The Android Open Source Project
|
* Copyright 2020, The Android Open Source Project
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -28,7 +28,6 @@
|
|||||||
#include <netinet/udp.h>
|
#include <netinet/udp.h>
|
||||||
|
|
||||||
#include <DnsProxydProtocol.h> // NETID_USE_LOCAL_NAMESERVERS
|
#include <DnsProxydProtocol.h> // NETID_USE_LOCAL_NAMESERVERS
|
||||||
#include <android_runtime/AndroidRuntime.h>
|
|
||||||
#include <cutils/properties.h>
|
#include <cutils/properties.h>
|
||||||
#include <nativehelper/JNIPlatformHelp.h>
|
#include <nativehelper/JNIPlatformHelp.h>
|
||||||
#include <nativehelper/ScopedLocalRef.h>
|
#include <nativehelper/ScopedLocalRef.h>
|
||||||
@@ -226,11 +225,6 @@ static jobject android_net_utils_getDnsNetwork(JNIEnv *env, jobject thiz) {
|
|||||||
class_Network, ctor, dnsNetId & ~NETID_USE_LOCAL_NAMESERVERS, privateDnsBypass);
|
class_Network, ctor, dnsNetId & ~NETID_USE_LOCAL_NAMESERVERS, privateDnsBypass);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void android_net_utils_setAllowNetworkingForProcess(JNIEnv *env, jobject thiz,
|
|
||||||
jboolean hasConnectivity) {
|
|
||||||
setAllowNetworkingForProcess(hasConnectivity == JNI_TRUE);
|
|
||||||
}
|
|
||||||
|
|
||||||
static jobject android_net_utils_getTcpRepairWindow(JNIEnv *env, jobject thiz, jobject javaFd) {
|
static jobject android_net_utils_getTcpRepairWindow(JNIEnv *env, jobject thiz, jobject javaFd) {
|
||||||
if (javaFd == NULL) {
|
if (javaFd == NULL) {
|
||||||
jniThrowNullPointerException(env, NULL);
|
jniThrowNullPointerException(env, NULL);
|
||||||
@@ -288,7 +282,6 @@ static const JNINativeMethod gNetworkUtilMethods[] = {
|
|||||||
{ "resNetworkResult", "(Ljava/io/FileDescriptor;)Landroid/net/DnsResolver$DnsResponse;", (void*) android_net_utils_resNetworkResult },
|
{ "resNetworkResult", "(Ljava/io/FileDescriptor;)Landroid/net/DnsResolver$DnsResponse;", (void*) android_net_utils_resNetworkResult },
|
||||||
{ "resNetworkCancel", "(Ljava/io/FileDescriptor;)V", (void*) android_net_utils_resNetworkCancel },
|
{ "resNetworkCancel", "(Ljava/io/FileDescriptor;)V", (void*) android_net_utils_resNetworkCancel },
|
||||||
{ "getDnsNetwork", "()Landroid/net/Network;", (void*) android_net_utils_getDnsNetwork },
|
{ "getDnsNetwork", "()Landroid/net/Network;", (void*) android_net_utils_getDnsNetwork },
|
||||||
{ "setAllowNetworkingForProcess", "(Z)V", (void *)android_net_utils_setAllowNetworkingForProcess },
|
|
||||||
};
|
};
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
|
|||||||
@@ -16,24 +16,10 @@
|
|||||||
|
|
||||||
package android.net;
|
package android.net;
|
||||||
|
|
||||||
import static android.system.OsConstants.AF_INET;
|
|
||||||
import static android.system.OsConstants.AF_INET6;
|
|
||||||
import static android.system.OsConstants.AF_UNIX;
|
|
||||||
import static android.system.OsConstants.EPERM;
|
|
||||||
import static android.system.OsConstants.SOCK_DGRAM;
|
|
||||||
import static android.system.OsConstants.SOCK_STREAM;
|
|
||||||
|
|
||||||
import static junit.framework.Assert.assertEquals;
|
import static junit.framework.Assert.assertEquals;
|
||||||
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
import android.system.ErrnoException;
|
|
||||||
import android.system.Os;
|
|
||||||
|
|
||||||
import androidx.test.runner.AndroidJUnit4;
|
import androidx.test.runner.AndroidJUnit4;
|
||||||
|
|
||||||
import libcore.io.IoUtils;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
@@ -139,50 +125,4 @@ public class NetworkUtilsTest {
|
|||||||
assertEquals(BigInteger.valueOf(7l - 4 + 4 + 16 + 65536),
|
assertEquals(BigInteger.valueOf(7l - 4 + 4 + 16 + 65536),
|
||||||
NetworkUtils.routedIPv6AddressCount(set));
|
NetworkUtils.routedIPv6AddressCount(set));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void expectSocketSuccess(String msg, int domain, int type) {
|
|
||||||
try {
|
|
||||||
IoUtils.closeQuietly(Os.socket(domain, type, 0));
|
|
||||||
} catch (ErrnoException e) {
|
|
||||||
fail(msg + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void expectSocketPemissionError(String msg, int domain, int type) {
|
|
||||||
try {
|
|
||||||
IoUtils.closeQuietly(Os.socket(domain, type, 0));
|
|
||||||
fail(msg);
|
|
||||||
} catch (ErrnoException e) {
|
|
||||||
assertEquals(msg, e.errno, EPERM);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void expectHasNetworking() {
|
|
||||||
expectSocketSuccess("Creating a UNIX socket should not have thrown ErrnoException",
|
|
||||||
AF_UNIX, SOCK_STREAM);
|
|
||||||
expectSocketSuccess("Creating a AF_INET socket shouldn't have thrown ErrnoException",
|
|
||||||
AF_INET, SOCK_DGRAM);
|
|
||||||
expectSocketSuccess("Creating a AF_INET6 socket shouldn't have thrown ErrnoException",
|
|
||||||
AF_INET6, SOCK_DGRAM);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void expectNoNetworking() {
|
|
||||||
expectSocketSuccess("Creating a UNIX socket should not have thrown ErrnoException",
|
|
||||||
AF_UNIX, SOCK_STREAM);
|
|
||||||
expectSocketPemissionError(
|
|
||||||
"Creating a AF_INET socket should have thrown ErrnoException(EPERM)",
|
|
||||||
AF_INET, SOCK_DGRAM);
|
|
||||||
expectSocketPemissionError(
|
|
||||||
"Creating a AF_INET6 socket should have thrown ErrnoException(EPERM)",
|
|
||||||
AF_INET6, SOCK_DGRAM);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testSetAllowNetworkingForProcess() {
|
|
||||||
expectHasNetworking();
|
|
||||||
NetworkUtils.setAllowNetworkingForProcess(false);
|
|
||||||
expectNoNetworking();
|
|
||||||
NetworkUtils.setAllowNetworkingForProcess(true);
|
|
||||||
expectHasNetworking();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,89 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2015 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.android.internal.net;
|
||||||
|
|
||||||
|
import static android.system.OsConstants.AF_INET;
|
||||||
|
import static android.system.OsConstants.AF_INET6;
|
||||||
|
import static android.system.OsConstants.AF_UNIX;
|
||||||
|
import static android.system.OsConstants.EPERM;
|
||||||
|
import static android.system.OsConstants.SOCK_DGRAM;
|
||||||
|
import static android.system.OsConstants.SOCK_STREAM;
|
||||||
|
|
||||||
|
import static junit.framework.Assert.assertEquals;
|
||||||
|
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import android.system.ErrnoException;
|
||||||
|
import android.system.Os;
|
||||||
|
|
||||||
|
import androidx.test.runner.AndroidJUnit4;
|
||||||
|
|
||||||
|
import libcore.io.IoUtils;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4.class)
|
||||||
|
@androidx.test.filters.SmallTest
|
||||||
|
public class NetworkUtilsInternalTest {
|
||||||
|
|
||||||
|
private static void expectSocketSuccess(String msg, int domain, int type) {
|
||||||
|
try {
|
||||||
|
IoUtils.closeQuietly(Os.socket(domain, type, 0));
|
||||||
|
} catch (ErrnoException e) {
|
||||||
|
fail(msg + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void expectSocketPemissionError(String msg, int domain, int type) {
|
||||||
|
try {
|
||||||
|
IoUtils.closeQuietly(Os.socket(domain, type, 0));
|
||||||
|
fail(msg);
|
||||||
|
} catch (ErrnoException e) {
|
||||||
|
assertEquals(msg, e.errno, EPERM);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void expectHasNetworking() {
|
||||||
|
expectSocketSuccess("Creating a UNIX socket should not have thrown ErrnoException",
|
||||||
|
AF_UNIX, SOCK_STREAM);
|
||||||
|
expectSocketSuccess("Creating a AF_INET socket shouldn't have thrown ErrnoException",
|
||||||
|
AF_INET, SOCK_DGRAM);
|
||||||
|
expectSocketSuccess("Creating a AF_INET6 socket shouldn't have thrown ErrnoException",
|
||||||
|
AF_INET6, SOCK_DGRAM);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void expectNoNetworking() {
|
||||||
|
expectSocketSuccess("Creating a UNIX socket should not have thrown ErrnoException",
|
||||||
|
AF_UNIX, SOCK_STREAM);
|
||||||
|
expectSocketPemissionError(
|
||||||
|
"Creating a AF_INET socket should have thrown ErrnoException(EPERM)",
|
||||||
|
AF_INET, SOCK_DGRAM);
|
||||||
|
expectSocketPemissionError(
|
||||||
|
"Creating a AF_INET6 socket should have thrown ErrnoException(EPERM)",
|
||||||
|
AF_INET6, SOCK_DGRAM);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetAllowNetworkingForProcess() {
|
||||||
|
expectHasNetworking();
|
||||||
|
NetworkUtilsInternal.setAllowNetworkingForProcess(false);
|
||||||
|
expectNoNetworking();
|
||||||
|
NetworkUtilsInternal.setAllowNetworkingForProcess(true);
|
||||||
|
expectHasNetworking();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,11 +23,11 @@ import static android.net.NetworkStats.TAG_NONE;
|
|||||||
import static android.net.NetworkStats.UID_ALL;
|
import static android.net.NetworkStats.UID_ALL;
|
||||||
import static android.net.NetworkStatsHistory.FIELD_ALL;
|
import static android.net.NetworkStatsHistory.FIELD_ALL;
|
||||||
import static android.net.NetworkTemplate.buildTemplateMobileAll;
|
import static android.net.NetworkTemplate.buildTemplateMobileAll;
|
||||||
import static android.net.NetworkUtils.multiplySafeByRational;
|
|
||||||
import static android.os.Process.myUid;
|
import static android.os.Process.myUid;
|
||||||
import static android.text.format.DateUtils.HOUR_IN_MILLIS;
|
import static android.text.format.DateUtils.HOUR_IN_MILLIS;
|
||||||
import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
|
import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
|
||||||
|
|
||||||
|
import static com.android.internal.net.NetworkUtilsInternal.multiplySafeByRational;
|
||||||
import static com.android.testutils.MiscAsserts.assertThrows;
|
import static com.android.testutils.MiscAsserts.assertThrows;
|
||||||
|
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
|||||||
Reference in New Issue
Block a user