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;
|
||||
|
||||
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.os.Build;
|
||||
import android.system.ErrnoException;
|
||||
import android.system.Os;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
|
||||
@@ -155,14 +150,6 @@ public class NetworkUtils {
|
||||
*/
|
||||
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}.
|
||||
*
|
||||
@@ -452,60 +439,4 @@ public class NetworkUtils {
|
||||
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");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -28,7 +28,6 @@
|
||||
#include <netinet/udp.h>
|
||||
|
||||
#include <DnsProxydProtocol.h> // NETID_USE_LOCAL_NAMESERVERS
|
||||
#include <android_runtime/AndroidRuntime.h>
|
||||
#include <cutils/properties.h>
|
||||
#include <nativehelper/JNIPlatformHelp.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);
|
||||
}
|
||||
|
||||
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) {
|
||||
if (javaFd == 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 },
|
||||
{ "resNetworkCancel", "(Ljava/io/FileDescriptor;)V", (void*) android_net_utils_resNetworkCancel },
|
||||
{ "getDnsNetwork", "()Landroid/net/Network;", (void*) android_net_utils_getDnsNetwork },
|
||||
{ "setAllowNetworkingForProcess", "(Z)V", (void *)android_net_utils_setAllowNetworkingForProcess },
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
|
||||
Reference in New Issue
Block a user