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

Commit 04f24b9f authored by Remi NGUYEN VAN's avatar Remi NGUYEN VAN Committed by Gerrit Code Review
Browse files

Merge "Cleanup networkstack-client"

parents 945c4de8 c0c75416
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -68,7 +68,7 @@ java_defaults {
    name: "NetworkStackShimsDefaults",
    name: "NetworkStackShimsDefaults",
    libs: [
    libs: [
        "androidx.annotation_annotation",
        "androidx.annotation_annotation",
        "networkstack-client",
        "networkstack-aidl-latest",
    ],
    ],
    static_libs : [
    static_libs : [
        "modules-utils-build_system"
        "modules-utils-build_system"
+22 −5
Original line number Original line Diff line number Diff line
@@ -145,12 +145,27 @@ aidl_interface {
}
}


java_library {
java_library {
    name: "networkstack-client",
    name: "networkstack-aidl-latest",
    sdk_version: "system_current",
    sdk_version: "system_current",
    min_sdk_version: "29",
    static_libs: [
        "ipmemorystore-aidl-interfaces-V10-java",
        "networkstack-aidl-interfaces-V12-java",
    ],
    visibility: ["//packages/modules/NetworkStack:__subpackages__"],
    apex_available: [
        "//apex_available:platform",
        "com.android.tethering",
        "com.android.wifi",
    ],
}

java_library {
    name: "networkstack-client",
    sdk_version: "module_current",
    // this is part of updatable modules(NetworkStack) which runs on Q and above
    // this is part of updatable modules(NetworkStack) which runs on Q and above
    min_sdk_version: "29",
    min_sdk_version: "29",
    srcs: [
    srcs: [
        ":framework-annotations",
        "src/android/net/ip/**/*.java",
        "src/android/net/ip/**/*.java",
        "src/android/net/IpMemoryStore.java",
        "src/android/net/IpMemoryStore.java",
        "src/android/net/IpMemoryStoreClient.java",
        "src/android/net/IpMemoryStoreClient.java",
@@ -162,11 +177,13 @@ java_library {
        "src/android/net/util/**/*.java",
        "src/android/net/util/**/*.java",
    ],
    ],
    libs: [
    libs: [
        "net-utils-framework-common", // XXX for IpUtils.java only
        // Since this library is sdk_version: "module_current", "framework-connectivity" is just
        // the module_current API stubs of framework-connectivity
        "framework-connectivity",
        "framework-annotations-lib",
    ],
    ],
    static_libs: [
    static_libs: [
        "ipmemorystore-aidl-interfaces-V10-java",
        "networkstack-aidl-latest",
        "networkstack-aidl-interfaces-V12-java",
    ],
    ],
    visibility: [
    visibility: [
        "//frameworks/base/packages/Connectivity/service",
        "//frameworks/base/packages/Connectivity/service",
+0 −75
Original line number Original line Diff line number Diff line
@@ -16,24 +16,17 @@


package android.net.util;
package android.net.util;


import static android.net.SocketKeepalive.ERROR_INVALID_IP_ADDRESS;

import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.net.InvalidPacketException;
import android.net.KeepalivePacketData;
import android.net.KeepalivePacketData;
import android.net.NattKeepalivePacketData;
import android.net.NattKeepalivePacketData;
import android.net.NattKeepalivePacketDataParcelable;
import android.net.NattKeepalivePacketDataParcelable;
import android.net.TcpKeepalivePacketData;
import android.net.TcpKeepalivePacketData;
import android.net.TcpKeepalivePacketDataParcelable;
import android.net.TcpKeepalivePacketDataParcelable;
import android.os.Build;
import android.os.Build;
import android.system.OsConstants;
import android.util.Log;
import android.util.Log;


import com.android.net.module.util.IpUtils;

import java.net.InetAddress;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.ByteOrder;


@@ -47,7 +40,6 @@ import java.nio.ByteOrder;
public final class KeepalivePacketDataUtil {
public final class KeepalivePacketDataUtil {
    private static final int IPV4_HEADER_LENGTH = 20;
    private static final int IPV4_HEADER_LENGTH = 20;
    private static final int IPV6_HEADER_LENGTH = 40;
    private static final int IPV6_HEADER_LENGTH = 40;
    private static final int TCP_HEADER_LENGTH = 20;


    private static final String TAG = KeepalivePacketDataUtil.class.getSimpleName();
    private static final String TAG = KeepalivePacketDataUtil.class.getSimpleName();


@@ -89,73 +81,6 @@ public final class KeepalivePacketDataUtil {
        return parcel;
        return parcel;
    }
    }


    /**
     * Factory method to create tcp keepalive packet structure.
     * @hide
     */
    public static TcpKeepalivePacketData fromStableParcelable(
            TcpKeepalivePacketDataParcelable tcpDetails) throws InvalidPacketException {
        final byte[] packet;
        try {
            if ((tcpDetails.srcAddress != null) && (tcpDetails.dstAddress != null)
                    && (tcpDetails.srcAddress.length == 4 /* V4 IP length */)
                    && (tcpDetails.dstAddress.length == 4 /* V4 IP length */)) {
                packet = buildV4Packet(tcpDetails);
            } else {
                // TODO: support ipv6
                throw new InvalidPacketException(ERROR_INVALID_IP_ADDRESS);
            }
            return new TcpKeepalivePacketData(
                    InetAddress.getByAddress(tcpDetails.srcAddress),
                    tcpDetails.srcPort,
                    InetAddress.getByAddress(tcpDetails.dstAddress),
                    tcpDetails.dstPort,
                    packet,
                    tcpDetails.seq, tcpDetails.ack, tcpDetails.rcvWnd, tcpDetails.rcvWndScale,
                    tcpDetails.tos, tcpDetails.ttl);
        } catch (UnknownHostException e) {
            throw new InvalidPacketException(ERROR_INVALID_IP_ADDRESS);
        }

    }

    /**
     * Build ipv4 tcp keepalive packet, not including the link-layer header.
     */
    // TODO : if this code is ever moved to the network stack, factorize constants with the ones
    // over there.
    private static byte[] buildV4Packet(TcpKeepalivePacketDataParcelable tcpDetails) {
        final int length = IPV4_HEADER_LENGTH + TCP_HEADER_LENGTH;
        ByteBuffer buf = ByteBuffer.allocate(length);
        buf.order(ByteOrder.BIG_ENDIAN);
        buf.put((byte) 0x45);                       // IP version and IHL
        buf.put((byte) tcpDetails.tos);             // TOS
        buf.putShort((short) length);
        buf.putInt(0x00004000);                     // ID, flags=DF, offset
        buf.put((byte) tcpDetails.ttl);             // TTL
        buf.put((byte) OsConstants.IPPROTO_TCP);
        final int ipChecksumOffset = buf.position();
        buf.putShort((short) 0);                    // IP checksum
        buf.put(tcpDetails.srcAddress);
        buf.put(tcpDetails.dstAddress);
        buf.putShort((short) tcpDetails.srcPort);
        buf.putShort((short) tcpDetails.dstPort);
        buf.putInt(tcpDetails.seq);                 // Sequence Number
        buf.putInt(tcpDetails.ack);                 // ACK
        buf.putShort((short) 0x5010);               // TCP length=5, flags=ACK
        buf.putShort((short) (tcpDetails.rcvWnd >> tcpDetails.rcvWndScale));   // Window size
        final int tcpChecksumOffset = buf.position();
        buf.putShort((short) 0);                    // TCP checksum
        // URG is not set therefore the urgent pointer is zero.
        buf.putShort((short) 0);                    // Urgent pointer

        buf.putShort(ipChecksumOffset, com.android.net.module.util.IpUtils.ipChecksum(buf, 0));
        buf.putShort(tcpChecksumOffset, IpUtils.tcpChecksum(
                buf, 0, IPV4_HEADER_LENGTH, TCP_HEADER_LENGTH));

        return buf.array();
    }

    // TODO: add buildV6Packet.
    // TODO: add buildV6Packet.


    /**
    /**