Merge "Move NetworkUtils JNI out of core/jni"

This commit is contained in:
Remi NGUYEN VAN
2021-05-12 13:12:09 +00:00
committed by Gerrit Code Review
3 changed files with 134 additions and 18 deletions

View File

@@ -100,9 +100,50 @@ java_sdk_library {
libs: [
"unsupportedappusage",
],
permitted_packages: [
"android.net",
"com.android.connectivity.aidl",
permitted_packages: ["android.net"],
}
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-wifi",
"unsupportedappusage",
"ServiceConnectivityResources",
],
static_libs: [
"framework-connectivity-protos",
@@ -133,8 +173,5 @@ java_library {
jarjar_rules: "jarjar-rules.txt",
apex_available: ["com.android.tethering"],
installable: true,
permitted_packages: [
"android.net",
"com.android.connectivity.aidl",
],
permitted_packages: ["android.net"],
}

View File

@@ -36,7 +36,6 @@
#include <utils/misc.h>
#include "NetdClient.h"
#include "core_jni_helpers.h"
#include "jni.h"
extern "C" {
@@ -52,6 +51,48 @@ constexpr int MAXPACKETSIZE = 8 * 1024;
// FrameworkListener limits the size of commands to 4096 bytes.
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)
{
struct sock_filter filter_code[] = {
@@ -124,7 +165,7 @@ static jobject android_net_utils_resNetworkQuery(JNIEnv *env, jobject thiz, jint
int fd = resNetworkQuery(netId, queryname.data(), ns_class, ns_type, flags);
if (fd < 0) {
jniThrowErrnoException(env, "resNetworkQuery", -fd);
throwErrnoException(env, "resNetworkQuery", -fd);
return nullptr;
}
@@ -139,7 +180,7 @@ static jobject android_net_utils_resNetworkSend(JNIEnv *env, jobject thiz, jint
int fd = resNetworkSend(netId, data, msgLen, flags);
if (fd < 0) {
jniThrowErrnoException(env, "resNetworkSend", -fd);
throwErrnoException(env, "resNetworkSend", -fd);
return nullptr;
}
@@ -154,13 +195,13 @@ static jobject android_net_utils_resNetworkResult(JNIEnv *env, jobject thiz, job
int res = resNetworkResult(fd, &rcode, buf.data(), MAXPACKETSIZE);
jniSetFileDescriptorOfFD(env, javaFd, -1);
if (res < 0) {
jniThrowErrnoException(env, "resNetworkResult", -res);
throwErrnoException(env, "resNetworkResult", -res);
return nullptr;
}
jbyteArray answer = env->NewByteArray(res);
if (answer == nullptr) {
jniThrowErrnoException(env, "resNetworkResult", ENOMEM);
throwErrnoException(env, "resNetworkResult", ENOMEM);
return nullptr;
} else {
env->SetByteArrayRegion(answer, 0, res,
@@ -182,7 +223,7 @@ static void android_net_utils_resNetworkCancel(JNIEnv *env, jobject thiz, jobjec
static jobject android_net_utils_getDnsNetwork(JNIEnv *env, jobject thiz) {
unsigned dnsNetId = 0;
if (int res = getNetworkForDns(&dnsNetId) < 0) {
jniThrowErrnoException(env, "getDnsNetId", -res);
throwErrnoException(env, "getDnsNetId", -res);
return nullptr;
}
bool privateDnsBypass = dnsNetId & NETID_USE_LOCAL_NAMESERVERS;
@@ -207,7 +248,7 @@ static jobject android_net_utils_getTcpRepairWindow(JNIEnv *env, jobject thiz, j
// Obtain the parameters of the TCP repair window.
int rc = getsockopt(fd, IPPROTO_TCP, TCP_REPAIR_WINDOW, &trw, &size);
if (rc == -1) {
jniThrowErrnoException(env, "getsockopt : TCP_REPAIR_WINDOW", errno);
throwErrnoException(env, "getsockopt : TCP_REPAIR_WINDOW", errno);
return NULL;
}
@@ -218,7 +259,7 @@ static jobject android_net_utils_getTcpRepairWindow(JNIEnv *env, jobject thiz, j
// should be applied to the window size.
rc = getsockopt(fd, IPPROTO_TCP, TCP_INFO, &tcpinfo, &tcpinfo_size);
if (rc == -1) {
jniThrowErrnoException(env, "getsockopt : TCP_INFO", errno);
throwErrnoException(env, "getsockopt : TCP_INFO", errno);
return NULL;
}
@@ -254,8 +295,8 @@ static const JNINativeMethod gNetworkUtilMethods[] = {
int register_android_net_NetworkUtils(JNIEnv* env)
{
return RegisterMethodsOrDie(env, NETUTILS_PKG_NAME, gNetworkUtilMethods,
NELEM(gNetworkUtilMethods));
return jniRegisterNativeMethods(env, NETUTILS_PKG_NAME, gNetworkUtilMethods,
NELEM(gNetworkUtilMethods));
}
}; // namespace android

38
framework/jni/onload.cpp Normal file
View File

@@ -0,0 +1,38 @@
/*
* 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;
}
};