1 /*
2 * Copyright 2020, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <android/file_descriptor_jni.h>
18
19 #include "NetdClient.h"
20 #include "core_jni_helpers.h"
21 #include "jni.h"
22
23 namespace android {
android_net_utils_setAllowNetworkingForProcess(JNIEnv * env,jclass clazz,jboolean hasConnectivity)24 static void android_net_utils_setAllowNetworkingForProcess(JNIEnv *env, jclass clazz,
25 jboolean hasConnectivity) {
26 setAllowNetworkingForProcess(hasConnectivity == JNI_TRUE);
27 }
28
android_net_utils_protectFromVpn(JNIEnv * env,jclass clazz,jint socket)29 static jboolean android_net_utils_protectFromVpn(JNIEnv *env, jclass clazz, jint socket) {
30 return (jboolean)!protectFromVpn(socket);
31 }
32
android_net_utils_protectFromVpnWithFd(JNIEnv * env,jclass clazz,jobject javaFd)33 static jboolean android_net_utils_protectFromVpnWithFd(JNIEnv *env, jclass clazz, jobject javaFd) {
34 return android_net_utils_protectFromVpn(env, clazz, AFileDescriptor_getFd(env, javaFd));
35 }
36
37 static const JNINativeMethod gNetworkUtilMethods[] = {
38 {"setAllowNetworkingForProcess", "(Z)V",
39 (void *)android_net_utils_setAllowNetworkingForProcess},
40 {"protectFromVpn", "(I)Z", (void *)android_net_utils_protectFromVpn},
41 {"protectFromVpn", "(Ljava/io/FileDescriptor;)Z",
42 (void *)android_net_utils_protectFromVpnWithFd},
43 };
44
register_com_android_internal_net_NetworkUtilsInternal(JNIEnv * env)45 int register_com_android_internal_net_NetworkUtilsInternal(JNIEnv *env) {
46 return RegisterMethodsOrDie(env, "com/android/internal/net/NetworkUtilsInternal",
47 gNetworkUtilMethods, NELEM(gNetworkUtilMethods));
48 }
49
50 } // namespace android
51