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

Commit 914dca36 authored by Remi NGUYEN VAN's avatar Remi NGUYEN VAN
Browse files

Move SocketUtils out of the connectivity module

SocketUtils contains system APIs for modules to interact for sockets,
wrapping internal APIs. It should be part of the platform to keep access
to the internal APIs.

This involves splitting NetworkUtils.protectVpn to NetworkUtilsInternal,
since SocketUtils and VpnService are the only users of that method.

The @UnsupportedAppUsage NetworkUtils.protectVpn has low usage
count, and is already available through VpnService.protect.

Bug: 181512874
Test: boots, VPN working
Change-Id: I7028d334975f7536c06afac7a22200c33db707ac
parent 3d31f1ea
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -6449,6 +6449,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 {
  public class VcnManager {
+2 −1
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserHandle;

import com.android.internal.net.NetworkUtilsInternal;
import com.android.internal.net.VpnConfig;

import java.net.DatagramSocket;
@@ -254,7 +255,7 @@ public class VpnService extends Service {
     * @return {@code true} on success.
     */
    public boolean protect(int socket) {
        return NetworkUtils.protectFromVpn(socket);
        return NetworkUtilsInternal.protectFromVpn(socket);
    }

    /**
+3 −2
Original line number Diff line number Diff line
@@ -22,12 +22,13 @@ import static android.system.OsConstants.SO_BINDTODEVICE;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.net.NetworkUtils;
import android.system.ErrnoException;
import android.system.NetlinkSocketAddress;
import android.system.Os;
import android.system.PacketSocketAddress;

import com.android.internal.net.NetworkUtilsInternal;

import libcore.io.IoBridge;

import java.io.FileDescriptor;
@@ -51,7 +52,7 @@ public final class SocketUtils {
        // of struct ifreq is a NULL-terminated interface name.
        // TODO: add a setsockoptString()
        Os.setsockoptIfreq(socket, SOL_SOCKET, SO_BINDTODEVICE, iface);
        NetworkUtils.protectFromVpn(socket);
        NetworkUtilsInternal.protectFromVpn(socket);
    }

    /**
+16 −0
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@ import static android.system.OsConstants.AF_INET6;
import android.annotation.NonNull;
import android.system.Os;

import java.io.FileDescriptor;

/** @hide */
public class NetworkUtilsInternal {

@@ -35,6 +37,20 @@ public class NetworkUtilsInternal {
     */
    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.
     * @param hostname Name of host to validate.
+1 −1
Original line number Diff line number Diff line
@@ -149,7 +149,7 @@ cc_library_shared {
                "android_os_VintfRuntimeInfo.cpp",
                "android_os_incremental_IncrementalManager.cpp",
                "android_net_LocalSocketImpl.cpp",
                "android_net_NetUtils.cpp",
                "android_net_NetworkUtils.cpp",
                "android_service_DataLoaderService.cpp",
                "android_util_AssetManager.cpp",
                "android_util_Binder.cpp",
Loading