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

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

Merge "Move NetworkUtils JNI out of core/jni" into sc-dev

parents 11bc52ed cdadd64b
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -149,7 +149,6 @@ 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_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",
@@ -222,6 +221,7 @@ cc_library_shared {


            static_libs: [
            static_libs: [
                "libasync_safe",
                "libasync_safe",
                "libconnectivityframeworkutils",
                "libbinderthreadstateutils",
                "libbinderthreadstateutils",
                "libdmabufinfo",
                "libdmabufinfo",
                "libgif",
                "libgif",
+45 −8
Original line number Original line Diff line number Diff line
@@ -100,9 +100,50 @@ java_sdk_library {
    libs: [
    libs: [
        "unsupportedappusage",
        "unsupportedappusage",
    ],
    ],
    permitted_packages: [
    permitted_packages: ["android.net"],
        "android.net",
}
        "com.android.connectivity.aidl",

cc_defaults {
    name: "libframework-connectivity-defaults",
    cflags: [
        "-Wall",
        "-Werror",
        "-Wno-unused-parameter",
        "-Wthread-safety",
    ],
    shared_libs: [
        "libbase",
        "liblog",
        "libnativehelper",
        "libnetd_client",
    ],
    header_libs: [
        "dnsproxyd_protocol_headers",
    ],
}

cc_library_static {
    name: "libconnectivityframeworkutils",
    defaults: ["libframework-connectivity-defaults"],
    srcs: [
        "jni/android_net_NetworkUtils.cpp",
    ],
    apex_available: [
        "//apex_available:platform",
        "com.android.tethering",
    ],
}

cc_library_shared {
    name: "libframework-connectivity-jni",
    defaults: ["libframework-connectivity-defaults"],
    srcs: [
        "jni/onload.cpp",
    ],
    static_libs: ["libconnectivityframeworkutils"],
    apex_available: [
        "//apex_available:platform",
        "com.android.tethering",
    ],
    ],
}
}


@@ -124,7 +165,6 @@ java_library {
        "framework-tethering",
        "framework-tethering",
        "framework-wifi",
        "framework-wifi",
        "unsupportedappusage",
        "unsupportedappusage",
        "ServiceConnectivityResources",
    ],
    ],
    static_libs: [
    static_libs: [
        "framework-connectivity-protos",
        "framework-connectivity-protos",
@@ -133,8 +173,5 @@ java_library {
    jarjar_rules: "jarjar-rules.txt",
    jarjar_rules: "jarjar-rules.txt",
    apex_available: ["com.android.tethering"],
    apex_available: ["com.android.tethering"],
    installable: true,
    installable: true,
    permitted_packages: [
    permitted_packages: ["android.net"],
        "android.net",
        "com.android.connectivity.aidl",
    ],
}
}
+51 −10
Original line number Original line Diff line number Diff line
@@ -36,7 +36,6 @@
#include <utils/misc.h>
#include <utils/misc.h>


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


extern "C" {
extern "C" {
@@ -52,6 +51,48 @@ constexpr int MAXPACKETSIZE = 8 * 1024;
// FrameworkListener limits the size of commands to 4096 bytes.
// FrameworkListener limits the size of commands to 4096 bytes.
constexpr int MAXCMDSIZE = 4096;
constexpr int MAXCMDSIZE = 4096;


static inline jclass FindClassOrDie(JNIEnv* env, const char* class_name) {
    jclass clazz = env->FindClass(class_name);
    LOG_ALWAYS_FATAL_IF(clazz == NULL, "Unable to find class %s", class_name);
    return clazz;
}

static inline jmethodID GetMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name,
                                         const char* method_signature) {
    jmethodID res = env->GetMethodID(clazz, method_name, method_signature);
    LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find method %s with signature %s", method_name,
                        method_signature);
    return res;
}

template <typename T>
static inline T MakeGlobalRefOrDie(JNIEnv* env, T in) {
    jobject res = env->NewGlobalRef(in);
    LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to create global reference.");
    return static_cast<T>(res);
}

static void throwErrnoException(JNIEnv* env, const char* functionName, int error) {
    ScopedLocalRef<jstring> detailMessage(env, env->NewStringUTF(functionName));
    if (detailMessage.get() == NULL) {
        // Not really much we can do here. We're probably dead in the water,
        // but let's try to stumble on...
        env->ExceptionClear();
    }
    static jclass errnoExceptionClass =
            MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/system/ErrnoException"));

    static jmethodID errnoExceptionCtor =
            GetMethodIDOrDie(env, errnoExceptionClass,
            "<init>", "(Ljava/lang/String;I)V");

    jobject exception = env->NewObject(errnoExceptionClass,
                                       errnoExceptionCtor,
                                       detailMessage.get(),
                                       error);
    env->Throw(reinterpret_cast<jthrowable>(exception));
}

static void android_net_utils_attachDropAllBPFFilter(JNIEnv *env, jobject clazz, jobject javaFd)
static void android_net_utils_attachDropAllBPFFilter(JNIEnv *env, jobject clazz, jobject javaFd)
{
{
    struct sock_filter filter_code[] = {
    struct sock_filter filter_code[] = {
@@ -129,7 +170,7 @@ static jobject android_net_utils_resNetworkQuery(JNIEnv *env, jobject thiz, jint
    int fd = resNetworkQuery(netId, queryname.data(), ns_class, ns_type, flags);
    int fd = resNetworkQuery(netId, queryname.data(), ns_class, ns_type, flags);


    if (fd < 0) {
    if (fd < 0) {
        jniThrowErrnoException(env, "resNetworkQuery", -fd);
        throwErrnoException(env, "resNetworkQuery", -fd);
        return nullptr;
        return nullptr;
    }
    }


@@ -144,7 +185,7 @@ static jobject android_net_utils_resNetworkSend(JNIEnv *env, jobject thiz, jint
    int fd = resNetworkSend(netId, data, msgLen, flags);
    int fd = resNetworkSend(netId, data, msgLen, flags);


    if (fd < 0) {
    if (fd < 0) {
        jniThrowErrnoException(env, "resNetworkSend", -fd);
        throwErrnoException(env, "resNetworkSend", -fd);
        return nullptr;
        return nullptr;
    }
    }


@@ -159,13 +200,13 @@ static jobject android_net_utils_resNetworkResult(JNIEnv *env, jobject thiz, job
    int res = resNetworkResult(fd, &rcode, buf.data(), MAXPACKETSIZE);
    int res = resNetworkResult(fd, &rcode, buf.data(), MAXPACKETSIZE);
    jniSetFileDescriptorOfFD(env, javaFd, -1);
    jniSetFileDescriptorOfFD(env, javaFd, -1);
    if (res < 0) {
    if (res < 0) {
        jniThrowErrnoException(env, "resNetworkResult", -res);
        throwErrnoException(env, "resNetworkResult", -res);
        return nullptr;
        return nullptr;
    }
    }


    jbyteArray answer = env->NewByteArray(res);
    jbyteArray answer = env->NewByteArray(res);
    if (answer == nullptr) {
    if (answer == nullptr) {
        jniThrowErrnoException(env, "resNetworkResult", ENOMEM);
        throwErrnoException(env, "resNetworkResult", ENOMEM);
        return nullptr;
        return nullptr;
    } else {
    } else {
        env->SetByteArrayRegion(answer, 0, res,
        env->SetByteArrayRegion(answer, 0, res,
@@ -187,7 +228,7 @@ static void android_net_utils_resNetworkCancel(JNIEnv *env, jobject thiz, jobjec
static jobject android_net_utils_getDnsNetwork(JNIEnv *env, jobject thiz) {
static jobject android_net_utils_getDnsNetwork(JNIEnv *env, jobject thiz) {
    unsigned dnsNetId = 0;
    unsigned dnsNetId = 0;
    if (int res = getNetworkForDns(&dnsNetId) < 0) {
    if (int res = getNetworkForDns(&dnsNetId) < 0) {
        jniThrowErrnoException(env, "getDnsNetId", -res);
        throwErrnoException(env, "getDnsNetId", -res);
        return nullptr;
        return nullptr;
    }
    }
    bool privateDnsBypass = dnsNetId & NETID_USE_LOCAL_NAMESERVERS;
    bool privateDnsBypass = dnsNetId & NETID_USE_LOCAL_NAMESERVERS;
@@ -212,7 +253,7 @@ static jobject android_net_utils_getTcpRepairWindow(JNIEnv *env, jobject thiz, j
    // Obtain the parameters of the TCP repair window.
    // Obtain the parameters of the TCP repair window.
    int rc = getsockopt(fd, IPPROTO_TCP, TCP_REPAIR_WINDOW, &trw, &size);
    int rc = getsockopt(fd, IPPROTO_TCP, TCP_REPAIR_WINDOW, &trw, &size);
    if (rc == -1) {
    if (rc == -1) {
        jniThrowErrnoException(env, "getsockopt : TCP_REPAIR_WINDOW", errno);
        throwErrnoException(env, "getsockopt : TCP_REPAIR_WINDOW", errno);
        return NULL;
        return NULL;
    }
    }


@@ -223,7 +264,7 @@ static jobject android_net_utils_getTcpRepairWindow(JNIEnv *env, jobject thiz, j
    // should be applied to the window size.
    // should be applied to the window size.
    rc = getsockopt(fd, IPPROTO_TCP, TCP_INFO, &tcpinfo, &tcpinfo_size);
    rc = getsockopt(fd, IPPROTO_TCP, TCP_INFO, &tcpinfo, &tcpinfo_size);
    if (rc == -1) {
    if (rc == -1) {
        jniThrowErrnoException(env, "getsockopt : TCP_INFO", errno);
        throwErrnoException(env, "getsockopt : TCP_INFO", errno);
        return NULL;
        return NULL;
    }
    }


@@ -260,7 +301,7 @@ static const JNINativeMethod gNetworkUtilMethods[] = {


int register_android_net_NetworkUtils(JNIEnv* env)
int register_android_net_NetworkUtils(JNIEnv* env)
{
{
    return RegisterMethodsOrDie(env, NETUTILS_PKG_NAME, gNetworkUtilMethods,
    return jniRegisterNativeMethods(env, NETUTILS_PKG_NAME, gNetworkUtilMethods,
                                    NELEM(gNetworkUtilMethods));
                                    NELEM(gNetworkUtilMethods));
}
}


+38 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <nativehelper/JNIHelp.h>
#include <log/log.h>

namespace android {

int register_android_net_NetworkUtils(JNIEnv* env);

extern "C" jint JNI_OnLoad(JavaVM* vm, void*) {
    JNIEnv *env;
    if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
        ALOGE("GetEnv failed");
        return JNI_ERR;
    }

    if (register_android_net_NetworkUtils(env) < 0) {
        return JNI_ERR;
    }

    return JNI_VERSION_1_6;
}

};
 No newline at end of file