Merge "Move SocketUtils out of the connectivity module" am: 4eb9b18e02 am: b3bc93fbcf
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1592273 MUST ONLY BE SUBMITTED BY AUTOMERGER Change-Id: Icb9911ad2933d76acbfc8b23463639ffc82bb58b
This commit is contained in:
@@ -6399,6 +6399,19 @@ package android.net.sip {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
package android.net.util {
|
||||||
|
|
||||||
|
public final class SocketUtils {
|
||||||
|
method public static void bindSocketToInterface(@NonNull java.io.FileDescriptor, @NonNull String) throws android.system.ErrnoException;
|
||||||
|
method public static void closeSocket(@Nullable java.io.FileDescriptor) throws java.io.IOException;
|
||||||
|
method @NonNull public static java.net.SocketAddress makeNetlinkSocketAddress(int, int);
|
||||||
|
method @NonNull public static java.net.SocketAddress makePacketSocketAddress(int, int);
|
||||||
|
method @Deprecated @NonNull public static java.net.SocketAddress makePacketSocketAddress(int, @NonNull byte[]);
|
||||||
|
method @NonNull public static java.net.SocketAddress makePacketSocketAddress(int, int, @NonNull byte[]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
package android.net.vcn {
|
package android.net.vcn {
|
||||||
|
|
||||||
public class VcnManager {
|
public class VcnManager {
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ import android.os.RemoteException;
|
|||||||
import android.os.ServiceManager;
|
import android.os.ServiceManager;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
|
|
||||||
|
import com.android.internal.net.NetworkUtilsInternal;
|
||||||
import com.android.internal.net.VpnConfig;
|
import com.android.internal.net.VpnConfig;
|
||||||
|
|
||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
@@ -254,7 +255,7 @@ public class VpnService extends Service {
|
|||||||
* @return {@code true} on success.
|
* @return {@code true} on success.
|
||||||
*/
|
*/
|
||||||
public boolean protect(int socket) {
|
public boolean protect(int socket) {
|
||||||
return NetworkUtils.protectFromVpn(socket);
|
return NetworkUtilsInternal.protectFromVpn(socket);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -22,12 +22,13 @@ import static android.system.OsConstants.SO_BINDTODEVICE;
|
|||||||
import android.annotation.NonNull;
|
import android.annotation.NonNull;
|
||||||
import android.annotation.Nullable;
|
import android.annotation.Nullable;
|
||||||
import android.annotation.SystemApi;
|
import android.annotation.SystemApi;
|
||||||
import android.net.NetworkUtils;
|
|
||||||
import android.system.ErrnoException;
|
import android.system.ErrnoException;
|
||||||
import android.system.NetlinkSocketAddress;
|
import android.system.NetlinkSocketAddress;
|
||||||
import android.system.Os;
|
import android.system.Os;
|
||||||
import android.system.PacketSocketAddress;
|
import android.system.PacketSocketAddress;
|
||||||
|
|
||||||
|
import com.android.internal.net.NetworkUtilsInternal;
|
||||||
|
|
||||||
import libcore.io.IoBridge;
|
import libcore.io.IoBridge;
|
||||||
|
|
||||||
import java.io.FileDescriptor;
|
import java.io.FileDescriptor;
|
||||||
@@ -51,7 +52,7 @@ public final class SocketUtils {
|
|||||||
// of struct ifreq is a NULL-terminated interface name.
|
// of struct ifreq is a NULL-terminated interface name.
|
||||||
// TODO: add a setsockoptString()
|
// TODO: add a setsockoptString()
|
||||||
Os.setsockoptIfreq(socket, SOL_SOCKET, SO_BINDTODEVICE, iface);
|
Os.setsockoptIfreq(socket, SOL_SOCKET, SO_BINDTODEVICE, iface);
|
||||||
NetworkUtils.protectFromVpn(socket);
|
NetworkUtilsInternal.protectFromVpn(socket);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -22,6 +22,8 @@ import static android.system.OsConstants.AF_INET6;
|
|||||||
import android.annotation.NonNull;
|
import android.annotation.NonNull;
|
||||||
import android.system.Os;
|
import android.system.Os;
|
||||||
|
|
||||||
|
import java.io.FileDescriptor;
|
||||||
|
|
||||||
/** @hide */
|
/** @hide */
|
||||||
public class NetworkUtilsInternal {
|
public class NetworkUtilsInternal {
|
||||||
|
|
||||||
@@ -35,6 +37,20 @@ public class NetworkUtilsInternal {
|
|||||||
*/
|
*/
|
||||||
public static native void setAllowNetworkingForProcess(boolean allowNetworking);
|
public static native void setAllowNetworkingForProcess(boolean allowNetworking);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Protect {@code fd} from VPN connections. After protecting, data sent through
|
||||||
|
* this socket will go directly to the underlying network, so its traffic will not be
|
||||||
|
* forwarded through the VPN.
|
||||||
|
*/
|
||||||
|
public static native boolean protectFromVpn(FileDescriptor fd);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Protect {@code socketfd} from VPN connections. After protecting, data sent through
|
||||||
|
* this socket will go directly to the underlying network, so its traffic will not be
|
||||||
|
* forwarded through the VPN.
|
||||||
|
*/
|
||||||
|
public static native boolean protectFromVpn(int socketfd);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the hostname is weakly validated.
|
* Returns true if the hostname is weakly validated.
|
||||||
* @param hostname Name of host to validate.
|
* @param hostname Name of host to validate.
|
||||||
|
|||||||
@@ -149,7 +149,7 @@ cc_library_shared {
|
|||||||
"android_os_VintfRuntimeInfo.cpp",
|
"android_os_VintfRuntimeInfo.cpp",
|
||||||
"android_os_incremental_IncrementalManager.cpp",
|
"android_os_incremental_IncrementalManager.cpp",
|
||||||
"android_net_LocalSocketImpl.cpp",
|
"android_net_LocalSocketImpl.cpp",
|
||||||
"android_net_NetUtils.cpp",
|
"android_net_NetworkUtils.cpp",
|
||||||
"android_service_DataLoaderService.cpp",
|
"android_service_DataLoaderService.cpp",
|
||||||
"android_util_AssetManager.cpp",
|
"android_util_AssetManager.cpp",
|
||||||
"android_util_Binder.cpp",
|
"android_util_Binder.cpp",
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define LOG_TAG "NetUtils"
|
#define LOG_TAG "NetworkUtils"
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -123,15 +123,6 @@ static jint android_net_utils_bindSocketToNetwork(JNIEnv *env, jobject thiz, job
|
|||||||
return setNetworkForSocket(netId, AFileDescriptor_getFD(env, javaFd));
|
return setNetworkForSocket(netId, AFileDescriptor_getFD(env, javaFd));
|
||||||
}
|
}
|
||||||
|
|
||||||
static jboolean android_net_utils_protectFromVpn(JNIEnv *env, jobject thiz, jint socket)
|
|
||||||
{
|
|
||||||
return (jboolean) !protectFromVpn(socket);
|
|
||||||
}
|
|
||||||
|
|
||||||
static jboolean android_net_utils_protectFromVpnWithFd(JNIEnv *env, jobject thiz, jobject javaFd) {
|
|
||||||
return android_net_utils_protectFromVpn(env, thiz, AFileDescriptor_getFD(env, javaFd));
|
|
||||||
}
|
|
||||||
|
|
||||||
static jboolean android_net_utils_queryUserAccess(JNIEnv *env, jobject thiz, jint uid, jint netId)
|
static jboolean android_net_utils_queryUserAccess(JNIEnv *env, jobject thiz, jint uid, jint netId)
|
||||||
{
|
{
|
||||||
return (jboolean) !queryUserAccess(uid, netId);
|
return (jboolean) !queryUserAccess(uid, netId);
|
||||||
@@ -276,8 +267,6 @@ static const JNINativeMethod gNetworkUtilMethods[] = {
|
|||||||
{ "getBoundNetworkForProcess", "()I", (void*) android_net_utils_getBoundNetworkForProcess },
|
{ "getBoundNetworkForProcess", "()I", (void*) android_net_utils_getBoundNetworkForProcess },
|
||||||
{ "bindProcessToNetworkForHostResolution", "(I)Z", (void*) android_net_utils_bindProcessToNetworkForHostResolution },
|
{ "bindProcessToNetworkForHostResolution", "(I)Z", (void*) android_net_utils_bindProcessToNetworkForHostResolution },
|
||||||
{ "bindSocketToNetwork", "(Ljava/io/FileDescriptor;I)I", (void*) android_net_utils_bindSocketToNetwork },
|
{ "bindSocketToNetwork", "(Ljava/io/FileDescriptor;I)I", (void*) android_net_utils_bindSocketToNetwork },
|
||||||
{ "protectFromVpn", "(I)Z", (void*) android_net_utils_protectFromVpn },
|
|
||||||
{ "protectFromVpn", "(Ljava/io/FileDescriptor;)Z", (void*) android_net_utils_protectFromVpnWithFd },
|
|
||||||
{ "queryUserAccess", "(II)Z", (void*)android_net_utils_queryUserAccess },
|
{ "queryUserAccess", "(II)Z", (void*)android_net_utils_queryUserAccess },
|
||||||
{ "attachDropAllBPFFilter", "(Ljava/io/FileDescriptor;)V", (void*) android_net_utils_attachDropAllBPFFilter },
|
{ "attachDropAllBPFFilter", "(Ljava/io/FileDescriptor;)V", (void*) android_net_utils_attachDropAllBPFFilter },
|
||||||
{ "detachBPFFilter", "(Ljava/io/FileDescriptor;)V", (void*) android_net_utils_detachBPFFilter },
|
{ "detachBPFFilter", "(Ljava/io/FileDescriptor;)V", (void*) android_net_utils_detachBPFFilter },
|
||||||
@@ -14,6 +14,8 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <android/file_descriptor_jni.h>
|
||||||
|
|
||||||
#include "NetdClient.h"
|
#include "NetdClient.h"
|
||||||
#include "core_jni_helpers.h"
|
#include "core_jni_helpers.h"
|
||||||
#include "jni.h"
|
#include "jni.h"
|
||||||
@@ -24,9 +26,20 @@ static void android_net_utils_setAllowNetworkingForProcess(JNIEnv *env, jobject
|
|||||||
setAllowNetworkingForProcess(hasConnectivity == JNI_TRUE);
|
setAllowNetworkingForProcess(hasConnectivity == JNI_TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static jboolean android_net_utils_protectFromVpn(JNIEnv *env, jobject thiz, jint socket) {
|
||||||
|
return (jboolean)!protectFromVpn(socket);
|
||||||
|
}
|
||||||
|
|
||||||
|
static jboolean android_net_utils_protectFromVpnWithFd(JNIEnv *env, jobject thiz, jobject javaFd) {
|
||||||
|
return android_net_utils_protectFromVpn(env, thiz, AFileDescriptor_getFD(env, javaFd));
|
||||||
|
}
|
||||||
|
|
||||||
static const JNINativeMethod gNetworkUtilMethods[] = {
|
static const JNINativeMethod gNetworkUtilMethods[] = {
|
||||||
{"setAllowNetworkingForProcess", "(Z)V",
|
{"setAllowNetworkingForProcess", "(Z)V",
|
||||||
(void *)android_net_utils_setAllowNetworkingForProcess},
|
(void *)android_net_utils_setAllowNetworkingForProcess},
|
||||||
|
{"protectFromVpn", "(I)Z", (void *)android_net_utils_protectFromVpn},
|
||||||
|
{"protectFromVpn", "(Ljava/io/FileDescriptor;)Z",
|
||||||
|
(void *)android_net_utils_protectFromVpnWithFd},
|
||||||
};
|
};
|
||||||
|
|
||||||
int register_com_android_internal_net_NetworkUtilsInternal(JNIEnv *env) {
|
int register_com_android_internal_net_NetworkUtilsInternal(JNIEnv *env) {
|
||||||
|
|||||||
@@ -442,16 +442,3 @@ package android.net.apf {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
package android.net.util {
|
|
||||||
|
|
||||||
public final class SocketUtils {
|
|
||||||
method public static void bindSocketToInterface(@NonNull java.io.FileDescriptor, @NonNull String) throws android.system.ErrnoException;
|
|
||||||
method public static void closeSocket(@Nullable java.io.FileDescriptor) throws java.io.IOException;
|
|
||||||
method @NonNull public static java.net.SocketAddress makeNetlinkSocketAddress(int, int);
|
|
||||||
method @NonNull public static java.net.SocketAddress makePacketSocketAddress(int, int);
|
|
||||||
method @Deprecated @NonNull public static java.net.SocketAddress makePacketSocketAddress(int, @NonNull byte[]);
|
|
||||||
method @NonNull public static java.net.SocketAddress makePacketSocketAddress(int, int, @NonNull byte[]);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -86,22 +86,6 @@ public class NetworkUtils {
|
|||||||
*/
|
*/
|
||||||
public static native int bindSocketToNetwork(FileDescriptor fd, int netId);
|
public static native int bindSocketToNetwork(FileDescriptor fd, int netId);
|
||||||
|
|
||||||
/**
|
|
||||||
* Protect {@code fd} from VPN connections. After protecting, data sent through
|
|
||||||
* this socket will go directly to the underlying network, so its traffic will not be
|
|
||||||
* forwarded through the VPN.
|
|
||||||
*/
|
|
||||||
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553,
|
|
||||||
publicAlternatives = "Use {@link android.net.VpnService#protect} instead.")
|
|
||||||
public static native boolean protectFromVpn(FileDescriptor fd);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Protect {@code socketfd} from VPN connections. After protecting, data sent through
|
|
||||||
* this socket will go directly to the underlying network, so its traffic will not be
|
|
||||||
* forwarded through the VPN.
|
|
||||||
*/
|
|
||||||
public native static boolean protectFromVpn(int socketfd);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if {@code uid} can access network designated by {@code netId}.
|
* Determine if {@code uid} can access network designated by {@code netId}.
|
||||||
* @return {@code true} if {@code uid} can access network, {@code false} otherwise.
|
* @return {@code true} if {@code uid} can access network, {@code false} otherwise.
|
||||||
|
|||||||
Reference in New Issue
Block a user