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

Commit 44aae217 authored by Remi NGUYEN VAN's avatar Remi NGUYEN VAN Committed by Android (Google) Code Review
Browse files

Merge changes from topic "libframework_jni_ndk" into sc-dev

* changes:
  Build connectivity JNI against libc++_static
  Remove dependency on libnetd_client
parents 16798ed4 b2abc42c
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -112,10 +112,8 @@ cc_defaults {
        "-Wthread-safety",
    ],
    shared_libs: [
        "libbase",
        "liblog",
        "libnativehelper",
        "libnetd_client",
    ],
    header_libs: [
        "dnsproxyd_protocol_headers",
@@ -139,10 +137,11 @@ cc_library_shared {
    name: "libframework-connectivity-jni",
    defaults: ["libframework-connectivity-defaults"],
    srcs: [
        "jni/android_net_NetworkUtils.cpp",
        "jni/onload.cpp",
    ],
    shared_libs: ["libandroid"],
    static_libs: ["libconnectivityframeworkutils"],
    stl: "libc++_static",
    apex_available: [
        "//apex_available:platform",
        "com.android.tethering",
+21 −22
Original line number Diff line number Diff line
@@ -37,7 +37,6 @@
#include <utils/Log.h>
#include <utils/misc.h>

#include "NetdClient.h"
#include "jni.h"

extern "C" {
@@ -113,14 +112,14 @@ static jlong android_net_utils_getBoundNetworkHandleForProcess(JNIEnv *env, jobj
}

static jboolean android_net_utils_bindProcessToNetworkForHostResolution(JNIEnv *env, jobject thiz,
        jint netId)
        jint netId, jlong netHandle)
{
    return (jboolean) !setNetworkForResolv(netId);
    return (jboolean) !android_setprocdns(netHandle);
}

static jint android_net_utils_bindSocketToNetwork(JNIEnv *env, jobject thiz, jobject javaFd,
                                                  jint netId) {
    return setNetworkForSocket(netId, AFileDescriptor_getFD(env, javaFd));
static jint android_net_utils_bindSocketToNetworkHandle(JNIEnv *env, jobject thiz, jobject javaFd,
                                                  jlong netHandle) {
    return android_setsocknetwork(netHandle, AFileDescriptor_getFD(env, javaFd));
}

static bool checkLenAndCopy(JNIEnv* env, const jbyteArray& addr, int len, void* dst)
@@ -132,7 +131,7 @@ static bool checkLenAndCopy(JNIEnv* env, const jbyteArray& addr, int len, void*
    return true;
}

static jobject android_net_utils_resNetworkQuery(JNIEnv *env, jobject thiz, jint netId,
static jobject android_net_utils_resNetworkQuery(JNIEnv *env, jobject thiz, jlong netHandle,
        jstring dname, jint ns_class, jint ns_type, jint flags) {
    const jsize javaCharsCount = env->GetStringLength(dname);
    const jsize byteCountUTF8 = env->GetStringUTFLength(dname);
@@ -142,7 +141,8 @@ static jobject android_net_utils_resNetworkQuery(JNIEnv *env, jobject thiz, jint
    std::vector<char> queryname(byteCountUTF8 + 1, 0);

    env->GetStringUTFRegion(dname, 0, javaCharsCount, queryname.data());
    int fd = resNetworkQuery(netId, queryname.data(), ns_class, ns_type, flags);

    int fd = android_res_nquery(netHandle, queryname.data(), ns_class, ns_type, flags);

    if (fd < 0) {
        jniThrowErrnoException(env, "resNetworkQuery", -fd);
@@ -152,12 +152,12 @@ static jobject android_net_utils_resNetworkQuery(JNIEnv *env, jobject thiz, jint
    return jniCreateFileDescriptor(env, fd);
}

static jobject android_net_utils_resNetworkSend(JNIEnv *env, jobject thiz, jint netId,
static jobject android_net_utils_resNetworkSend(JNIEnv *env, jobject thiz, jlong netHandle,
        jbyteArray msg, jint msgLen, jint flags) {
    uint8_t data[MAXCMDSIZE];

    checkLenAndCopy(env, msg, msgLen, data);
    int fd = resNetworkSend(netId, data, msgLen, flags);
    int fd = android_res_nsend(netHandle, data, msgLen, flags);

    if (fd < 0) {
        jniThrowErrnoException(env, "resNetworkSend", -fd);
@@ -172,7 +172,7 @@ static jobject android_net_utils_resNetworkResult(JNIEnv *env, jobject thiz, job
    int rcode;
    std::vector<uint8_t> buf(MAXPACKETSIZE, 0);

    int res = resNetworkResult(fd, &rcode, buf.data(), MAXPACKETSIZE);
    int res = android_res_nresult(fd, &rcode, buf.data(), MAXPACKETSIZE);
    jniSetFileDescriptorOfFD(env, javaFd, -1);
    if (res < 0) {
        jniThrowErrnoException(env, "resNetworkResult", -res);
@@ -196,23 +196,22 @@ static jobject android_net_utils_resNetworkResult(JNIEnv *env, jobject thiz, job

static void android_net_utils_resNetworkCancel(JNIEnv *env, jobject thiz, jobject javaFd) {
    int fd = AFileDescriptor_getFD(env, javaFd);
    resNetworkCancel(fd);
    android_res_cancel(fd);
    jniSetFileDescriptorOfFD(env, javaFd, -1);
}

static jobject android_net_utils_getDnsNetwork(JNIEnv *env, jobject thiz) {
    unsigned dnsNetId = 0;
    if (int res = getNetworkForDns(&dnsNetId) < 0) {
        jniThrowErrnoException(env, "getDnsNetId", -res);
    net_handle_t dnsNetHandle = NETWORK_UNSPECIFIED;
    if (int res = android_getprocdns(&dnsNetHandle) < 0) {
        jniThrowErrnoException(env, "getDnsNetwork", -res);
        return nullptr;
    }
    bool privateDnsBypass = dnsNetId & NETID_USE_LOCAL_NAMESERVERS;

    static jclass class_Network = MakeGlobalRefOrDie(
            env, FindClassOrDie(env, "android/net/Network"));
    static jmethodID ctor = env->GetMethodID(class_Network, "<init>", "(IZ)V");
    return env->NewObject(
            class_Network, ctor, dnsNetId & ~NETID_USE_LOCAL_NAMESERVERS, privateDnsBypass);
    static jmethodID method = env->GetStaticMethodID(class_Network, "fromNetworkHandle",
            "(J)Landroid/net/Network;");
    return env->CallStaticObjectMethod(class_Network, method, static_cast<jlong>(dnsNetHandle));
}

static jobject android_net_utils_getTcpRepairWindow(JNIEnv *env, jobject thiz, jobject javaFd) {
@@ -261,12 +260,12 @@ static const JNINativeMethod gNetworkUtilMethods[] = {
    { "bindProcessToNetworkHandle", "(J)Z", (void*) android_net_utils_bindProcessToNetworkHandle },
    { "getBoundNetworkHandleForProcess", "()J", (void*) android_net_utils_getBoundNetworkHandleForProcess },
    { "bindProcessToNetworkForHostResolution", "(I)Z", (void*) android_net_utils_bindProcessToNetworkForHostResolution },
    { "bindSocketToNetwork", "(Ljava/io/FileDescriptor;I)I", (void*) android_net_utils_bindSocketToNetwork },
    { "bindSocketToNetworkHandle", "(Ljava/io/FileDescriptor;J)I", (void*) android_net_utils_bindSocketToNetworkHandle },
    { "attachDropAllBPFFilter", "(Ljava/io/FileDescriptor;)V", (void*) android_net_utils_attachDropAllBPFFilter },
    { "detachBPFFilter", "(Ljava/io/FileDescriptor;)V", (void*) android_net_utils_detachBPFFilter },
    { "getTcpRepairWindow", "(Ljava/io/FileDescriptor;)Landroid/net/TcpRepairWindow;", (void*) android_net_utils_getTcpRepairWindow },
    { "resNetworkSend", "(I[BII)Ljava/io/FileDescriptor;", (void*) android_net_utils_resNetworkSend },
    { "resNetworkQuery", "(ILjava/lang/String;III)Ljava/io/FileDescriptor;", (void*) android_net_utils_resNetworkQuery },
    { "resNetworkSend", "(J[BII)Ljava/io/FileDescriptor;", (void*) android_net_utils_resNetworkSend },
    { "resNetworkQuery", "(JLjava/lang/String;III)Ljava/io/FileDescriptor;", (void*) android_net_utils_resNetworkQuery },
    { "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 },
+7 −5
Original line number Diff line number Diff line
@@ -92,6 +92,7 @@ public class Network implements Parcelable {
    // value in the native/android/net.c NDK implementation.
    private static final long HANDLE_MAGIC = 0xcafed00dL;
    private static final int HANDLE_MAGIC_SIZE = 32;
    private static final int USE_LOCAL_NAMESERVERS_FLAG = 0x80000000;

    // A boolean to control how getAllByName()/getByName() behaves in the face
    // of Private DNS.
@@ -189,7 +190,7 @@ public class Network implements Parcelable {
     */
    public int getNetIdForResolv() {
        return mPrivateDnsBypass
                ? (int) (0x80000000L | (long) netId)  // Non-portable DNS resolution flag.
                ? (USE_LOCAL_NAMESERVERS_FLAG | netId)  // Non-portable DNS resolution flag.
                : netId;
    }

@@ -452,12 +453,13 @@ public class Network implements Parcelable {
            throw new IllegalArgumentException(
                    "Network.fromNetworkHandle refusing to instantiate NETID_UNSET Network.");
        }
        if ((networkHandle & ((1L << HANDLE_MAGIC_SIZE) - 1)) != HANDLE_MAGIC
                || networkHandle < 0) {
        if ((networkHandle & ((1L << HANDLE_MAGIC_SIZE) - 1)) != HANDLE_MAGIC) {
            throw new IllegalArgumentException(
                    "Value passed to fromNetworkHandle() is not a network handle.");
        }
        return new Network((int) (networkHandle >> HANDLE_MAGIC_SIZE));
        final int netIdForResolv = (int) (networkHandle >>> HANDLE_MAGIC_SIZE);
        return new Network((netIdForResolv & ~USE_LOCAL_NAMESERVERS_FLAG),
                ((netIdForResolv & USE_LOCAL_NAMESERVERS_FLAG) != 0) /* privateDnsBypass */);
    }

    /**
@@ -485,7 +487,7 @@ public class Network implements Parcelable {
        if (netId == 0) {
            return 0L;  // make this zero condition obvious for debugging
        }
        return (((long) netId) << HANDLE_MAGIC_SIZE) | HANDLE_MAGIC;
        return (((long) getNetIdForResolv()) << HANDLE_MAGIC_SIZE) | HANDLE_MAGIC;
    }

    // implement the Parcelable interface
+20 −5
Original line number Diff line number Diff line
@@ -92,12 +92,16 @@ public class NetworkUtils {
    @Deprecated
    public native static boolean bindProcessToNetworkForHostResolution(int netId);

    private static native int bindSocketToNetworkHandle(FileDescriptor fd, long netHandle);

    /**
     * Explicitly binds {@code fd} to the network designated by {@code netId}.  This
     * overrides any binding via {@link #bindProcessToNetwork}.
     * @return 0 on success or negative errno on failure.
     */
    public static native int bindSocketToNetwork(FileDescriptor fd, int netId);
    public static int bindSocketToNetwork(FileDescriptor fd, int netId) {
        return bindSocketToNetworkHandle(fd, new Network(netId).getNetworkHandle());
    }

    /**
     * Determine if {@code uid} can access network designated by {@code netId}.
@@ -108,14 +112,22 @@ public class NetworkUtils {
        return false;
    }

    private static native FileDescriptor resNetworkSend(
            long netHandle, byte[] msg, int msglen, int flags) throws ErrnoException;

    /**
     * DNS resolver series jni method.
     * Issue the query {@code msg} on the network designated by {@code netId}.
     * {@code flags} is an additional config to control actual querying behavior.
     * @return a file descriptor to watch for read events
     */
    public static native FileDescriptor resNetworkSend(
            int netId, byte[] msg, int msglen, int flags) throws ErrnoException;
    public static FileDescriptor resNetworkSend(
            int netId, byte[] msg, int msglen, int flags) throws ErrnoException {
        return resNetworkSend(new Network(netId).getNetworkHandle(), msg, msglen, flags);
    }

    private static native FileDescriptor resNetworkQuery(
            long netHandle, String dname, int nsClass, int nsType, int flags) throws ErrnoException;

    /**
     * DNS resolver series jni method.
@@ -124,8 +136,11 @@ public class NetworkUtils {
     * {@code flags} is an additional config to control actual querying behavior.
     * @return a file descriptor to watch for read events
     */
    public static native FileDescriptor resNetworkQuery(
            int netId, String dname, int nsClass, int nsType, int flags) throws ErrnoException;
    public static FileDescriptor resNetworkQuery(
            int netId, String dname, int nsClass, int nsType, int flags) throws ErrnoException {
        return resNetworkQuery(new Network(netId).getNetworkHandle(), dname, nsClass, nsType,
                flags);
    }

    /**
     * DNS resolver series jni method.
+1 −0
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ cc_library_shared {
        "jni/com_android_server_TestNetworkService.cpp",
        "jni/onload.cpp",
    ],
    stl: "libc++_static",
    shared_libs: [
        "libbase",
        "liblog",
Loading