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

Commit b7bda39d authored by Remi NGUYEN VAN's avatar Remi NGUYEN VAN
Browse files

Fix SocketUtils API

 - connect, sendTo, bind were added as public API, so do not need to be
   maintained as SystemApi in SocketUtils. These APIs were added in Q
   timeframe.
 - Add @Nullable/@NonNull annotations.

Bug: 126699304
Bug: 126477266
Test: booted, WiFi works
Change-Id: I3f086ac8952cfd6a4d8f861d2e1c7aa1ca8be999
parent 7c447498
Loading
Loading
Loading
Loading
+8 −11
Original line number Diff line number Diff line
@@ -3516,19 +3516,16 @@ package android.net.metrics {
package android.net.util {
  public class SocketUtils {
    method public static void addArpEntry(java.net.Inet4Address, android.net.MacAddress, String, java.io.FileDescriptor) throws java.io.IOException;
    method public static void attachControlPacketFilter(java.io.FileDescriptor, int) throws java.net.SocketException;
    method public static void attachDhcpFilter(java.io.FileDescriptor) throws java.net.SocketException;
    method public static void attachRaFilter(java.io.FileDescriptor, int) throws java.net.SocketException;
    method public static void bindSocket(java.io.FileDescriptor, java.net.SocketAddress) throws android.system.ErrnoException, java.net.SocketException;
    method public static void bindSocketToInterface(java.io.FileDescriptor, String) throws android.system.ErrnoException;
    method public static void closeSocket(java.io.FileDescriptor) throws java.io.IOException;
    method public static void connectSocket(java.io.FileDescriptor, java.net.SocketAddress) throws android.system.ErrnoException, java.net.SocketException;
    method public static void addArpEntry(@NonNull java.net.Inet4Address, @NonNull android.net.MacAddress, @NonNull String, @NonNull java.io.FileDescriptor) throws java.io.IOException;
    method public static void attachControlPacketFilter(@NonNull java.io.FileDescriptor, int) throws java.net.SocketException;
    method public static void attachDhcpFilter(@NonNull java.io.FileDescriptor) throws java.net.SocketException;
    method public static void attachRaFilter(@NonNull java.io.FileDescriptor, int) throws java.net.SocketException;
    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 public static java.net.SocketAddress makeNetlinkSocketAddress(int, int);
    method public static java.net.SocketAddress makePacketSocketAddress(short, int);
    method public static java.net.SocketAddress makePacketSocketAddress(int, byte[]);
    method public static void sendTo(java.io.FileDescriptor, byte[], int, int, int, java.net.SocketAddress) throws android.system.ErrnoException, java.net.SocketException;
    method public static void setSocketTimeValueOption(java.io.FileDescriptor, int, int, long) throws android.system.ErrnoException;
    method public static java.net.SocketAddress makePacketSocketAddress(int, @NonNull byte[]);
    method public static void setSocketTimeValueOption(@NonNull java.io.FileDescriptor, int, int, long) throws android.system.ErrnoException;
  }
}
+8 −11
Original line number Diff line number Diff line
@@ -917,19 +917,16 @@ package android.net.metrics {
package android.net.util {

  public class SocketUtils {
    method public static void addArpEntry(java.net.Inet4Address, android.net.MacAddress, String, java.io.FileDescriptor) throws java.io.IOException;
    method public static void attachControlPacketFilter(java.io.FileDescriptor, int) throws java.net.SocketException;
    method public static void attachDhcpFilter(java.io.FileDescriptor) throws java.net.SocketException;
    method public static void attachRaFilter(java.io.FileDescriptor, int) throws java.net.SocketException;
    method public static void bindSocket(java.io.FileDescriptor, java.net.SocketAddress) throws android.system.ErrnoException, java.net.SocketException;
    method public static void bindSocketToInterface(java.io.FileDescriptor, String) throws android.system.ErrnoException;
    method public static void closeSocket(java.io.FileDescriptor) throws java.io.IOException;
    method public static void connectSocket(java.io.FileDescriptor, java.net.SocketAddress) throws android.system.ErrnoException, java.net.SocketException;
    method public static void addArpEntry(@NonNull java.net.Inet4Address, @NonNull android.net.MacAddress, @NonNull String, @NonNull java.io.FileDescriptor) throws java.io.IOException;
    method public static void attachControlPacketFilter(@NonNull java.io.FileDescriptor, int) throws java.net.SocketException;
    method public static void attachDhcpFilter(@NonNull java.io.FileDescriptor) throws java.net.SocketException;
    method public static void attachRaFilter(@NonNull java.io.FileDescriptor, int) throws java.net.SocketException;
    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 public static java.net.SocketAddress makeNetlinkSocketAddress(int, int);
    method public static java.net.SocketAddress makePacketSocketAddress(short, int);
    method public static java.net.SocketAddress makePacketSocketAddress(int, byte[]);
    method public static void sendTo(java.io.FileDescriptor, byte[], int, int, int, java.net.SocketAddress) throws android.system.ErrnoException, java.net.SocketException;
    method public static void setSocketTimeValueOption(java.io.FileDescriptor, int, int, long) throws android.system.ErrnoException;
    method public static java.net.SocketAddress makePacketSocketAddress(int, @NonNull byte[]);
    method public static void setSocketTimeValueOption(@NonNull java.io.FileDescriptor, int, int, long) throws android.system.ErrnoException;
  }

}
+12 −33
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ package android.net.util;
import static android.system.OsConstants.SOL_SOCKET;
import static android.system.OsConstants.SO_BINDTODEVICE;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.annotation.TestApi;
import android.net.MacAddress;
@@ -49,7 +51,7 @@ public class SocketUtils {
     *
     * <p>Data sent through the socket will go directly to the underlying network, ignoring VPNs.
     */
    public static void bindSocketToInterface(FileDescriptor socket, String iface)
    public static void bindSocketToInterface(@NonNull FileDescriptor socket, @NonNull String iface)
            throws ErrnoException {
        // SO_BINDTODEVICE actually takes a string. This works because the first member
        // of struct ifreq is a NULL-terminated interface name.
@@ -75,7 +77,7 @@ public class SocketUtils {
    /**
     * Make a socket address that packet socket can send packets to.
     */
    public static SocketAddress makePacketSocketAddress(int ifIndex, byte[] hwAddr) {
    public static SocketAddress makePacketSocketAddress(int ifIndex, @NonNull byte[] hwAddr) {
        return new PacketSocketAddress(ifIndex, hwAddr);
    }

@@ -83,45 +85,21 @@ public class SocketUtils {
     * Set an option on a socket that takes a time value argument.
     */
    public static void setSocketTimeValueOption(
            FileDescriptor fd, int level, int option, long millis) throws ErrnoException {
            @NonNull FileDescriptor fd, int level, int option, long millis) throws ErrnoException {
        Os.setsockoptTimeval(fd, level, option, StructTimeval.fromMillis(millis));
    }

    /**
     * Bind a socket to the specified address.
     */
    public static void bindSocket(FileDescriptor fd, SocketAddress addr)
            throws ErrnoException, SocketException {
        Os.bind(fd, addr);
    }

    /**
     * Connect a socket to the specified address.
     */
    public static void connectSocket(FileDescriptor fd, SocketAddress addr)
            throws ErrnoException, SocketException {
        Os.connect(fd, addr);
    }

    /**
     * Send a message on a socket, using the specified SocketAddress.
     */
    public static void sendTo(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount,
            int flags, SocketAddress addr) throws ErrnoException, SocketException {
        Os.sendto(fd, bytes, byteOffset, byteCount, flags, addr);
    }

    /**
     * @see IoBridge#closeAndSignalBlockedThreads(FileDescriptor)
     */
    public static void closeSocket(FileDescriptor fd) throws IOException {
    public static void closeSocket(@Nullable FileDescriptor fd) throws IOException {
        IoBridge.closeAndSignalBlockedThreads(fd);
    }

    /**
     * Attaches a socket filter that accepts DHCP packets to the given socket.
     */
    public static void attachDhcpFilter(FileDescriptor fd) throws SocketException {
    public static void attachDhcpFilter(@NonNull FileDescriptor fd) throws SocketException {
        NetworkUtils.attachDhcpFilter(fd);
    }

@@ -130,7 +108,8 @@ public class SocketUtils {
     * @param fd the socket's {@link FileDescriptor}.
     * @param packetType the hardware address type, one of ARPHRD_*.
     */
    public static void attachRaFilter(FileDescriptor fd, int packetType) throws SocketException {
    public static void attachRaFilter(@NonNull FileDescriptor fd, int packetType)
            throws SocketException {
        NetworkUtils.attachRaFilter(fd, packetType);
    }

@@ -142,7 +121,7 @@ public class SocketUtils {
     * @param fd the socket's {@link FileDescriptor}.
     * @param packetType the hardware address type, one of ARPHRD_*.
     */
    public static void attachControlPacketFilter(FileDescriptor fd, int packetType)
    public static void attachControlPacketFilter(@NonNull FileDescriptor fd, int packetType)
            throws SocketException {
        NetworkUtils.attachControlPacketFilter(fd, packetType);
    }
@@ -150,8 +129,8 @@ public class SocketUtils {
    /**
     * Add an entry into the ARP cache.
     */
    public static void addArpEntry(Inet4Address ipv4Addr, MacAddress ethAddr, String ifname,
            FileDescriptor fd) throws IOException {
    public static void addArpEntry(@NonNull Inet4Address ipv4Addr, @NonNull MacAddress ethAddr,
            @NonNull String ifname, @NonNull FileDescriptor fd) throws IOException {
        NetworkUtils.addArpEntry(ipv4Addr, ethAddr, ifname, fd);
    }

+1 −1
Original line number Diff line number Diff line
@@ -475,7 +475,7 @@ public class ApfFilter {
            socket = Os.socket(AF_PACKET, SOCK_RAW, ETH_P_IPV6);
            SocketAddress addr = makePacketSocketAddress(
                    (short) ETH_P_IPV6, mInterfaceParams.index);
            SocketUtils.bindSocket(socket, addr);
            Os.bind(socket, addr);
            SocketUtils.attachRaFilter(socket, mApfCapabilities.apfPacketFormat);
        } catch(SocketException|ErrnoException e) {
            Log.e(TAG, "Error starting filter", e);
+2 −3
Original line number Diff line number Diff line
@@ -317,7 +317,7 @@ public class DhcpClient extends StateMachine {
        try {
            mPacketSock = Os.socket(AF_PACKET, SOCK_RAW, ETH_P_IP);
            SocketAddress addr = makePacketSocketAddress((short) ETH_P_IP, mIface.index);
            SocketUtils.bindSocket(mPacketSock, addr);
            Os.bind(mPacketSock, addr);
            SocketUtils.attachDhcpFilter(mPacketSock);
        } catch(SocketException|ErrnoException e) {
            Log.e(TAG, "Error creating packet socket", e);
@@ -412,8 +412,7 @@ public class DhcpClient extends StateMachine {
        try {
            if (encap == DhcpPacket.ENCAP_L2) {
                if (DBG) Log.d(TAG, "Broadcasting " + description);
                SocketUtils.sendTo(
                        mPacketSock, buf.array(), 0, buf.limit(), 0, mInterfaceBroadcastAddr);
                Os.sendto(mPacketSock, buf.array(), 0, buf.limit(), 0, mInterfaceBroadcastAddr);
            } else if (encap == DhcpPacket.ENCAP_BOOTP && to.equals(INADDR_BROADCAST)) {
                if (DBG) Log.d(TAG, "Broadcasting " + description);
                // We only send L3-encapped broadcasts in DhcpRebindingState,
Loading