JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011, 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 | #define LOG_TAG "NMST_QTagUidNative" |
| 18 | #include <utils/Log.h> |
| 19 | |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 20 | #include <nativehelper/JNIHelp.h> |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 21 | |
| 22 | #include "jni.h" |
| 23 | #include <utils/misc.h> |
| 24 | #include <cutils/qtaguid.h> |
| 25 | |
| 26 | #include <errno.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/socket.h> |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | static jint QTagUid_tagSocketFd(JNIEnv* env, jclass, |
| 34 | jobject fileDescriptor, |
| 35 | jint tagNum, jint uid) { |
| 36 | int userFd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
| 37 | |
Mathieu Chartier | 98671c3 | 2014-08-20 10:04:08 -0700 | [diff] [blame] | 38 | if (env->ExceptionCheck()) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 39 | ALOGE("Can't get FileDescriptor num"); |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 40 | return (jint)-1; |
| 41 | } |
| 42 | |
| 43 | int res = qtaguid_tagSocket(userFd, tagNum, uid); |
| 44 | if (res < 0) { |
| 45 | return (jint)-errno; |
| 46 | } |
| 47 | return (jint)res; |
| 48 | } |
| 49 | |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 50 | static jint QTagUid_untagSocketFd(JNIEnv* env, jclass, |
| 51 | jobject fileDescriptor) { |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 52 | int userFd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
| 53 | |
Mathieu Chartier | 98671c3 | 2014-08-20 10:04:08 -0700 | [diff] [blame] | 54 | if (env->ExceptionCheck()) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 55 | ALOGE("Can't get FileDescriptor num"); |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 56 | return (jint)-1; |
| 57 | } |
| 58 | |
| 59 | int res = qtaguid_untagSocket(userFd); |
| 60 | if (res < 0) { |
| 61 | return (jint)-errno; |
| 62 | } |
| 63 | return (jint)res; |
| 64 | } |
| 65 | |
| 66 | static jint QTagUid_setCounterSet(JNIEnv* env, jclass, |
| 67 | jint setNum, jint uid) { |
| 68 | |
| 69 | int res = qtaguid_setCounterSet(setNum, uid); |
| 70 | if (res < 0) { |
| 71 | return (jint)-errno; |
| 72 | } |
| 73 | return (jint)res; |
| 74 | } |
| 75 | |
| 76 | static jint QTagUid_deleteTagData(JNIEnv* env, jclass, |
| 77 | jint tagNum, jint uid) { |
| 78 | |
| 79 | int res = qtaguid_deleteTagData(tagNum, uid); |
| 80 | if (res < 0) { |
| 81 | return (jint)-errno; |
| 82 | } |
| 83 | return (jint)res; |
| 84 | } |
| 85 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 86 | static const JNINativeMethod gQTagUidMethods[] = { |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 87 | { "native_tagSocketFd", "(Ljava/io/FileDescriptor;II)I", (void*)QTagUid_tagSocketFd}, |
| 88 | { "native_untagSocketFd", "(Ljava/io/FileDescriptor;)I", (void*)QTagUid_untagSocketFd}, |
| 89 | { "native_setCounterSet", "(II)I", (void*)QTagUid_setCounterSet}, |
| 90 | { "native_deleteTagData", "(II)I", (void*)QTagUid_deleteTagData}, |
| 91 | }; |
| 92 | |
| 93 | int register_android_server_NetworkManagementSocketTagger(JNIEnv* env) { |
| 94 | return jniRegisterNativeMethods(env, "com/android/server/NetworkManagementSocketTagger", gQTagUidMethods, NELEM(gQTagUidMethods)); |
| 95 | } |
| 96 | |
| 97 | }; |