blob: 58295af50135a9e54e1775fb1515cb161ed36785 [file] [log] [blame]
JP Abgrall98a4f7e2011-09-02 15:36:33 -07001/*
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 Moreland2279b252017-07-19 09:50:45 -070020#include <nativehelper/JNIHelp.h>
JP Abgrall98a4f7e2011-09-02 15:36:33 -070021
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
31namespace android {
32
33static jint QTagUid_tagSocketFd(JNIEnv* env, jclass,
34 jobject fileDescriptor,
35 jint tagNum, jint uid) {
36 int userFd = jniGetFDFromFileDescriptor(env, fileDescriptor);
37
Mathieu Chartier98671c32014-08-20 10:04:08 -070038 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +000039 ALOGE("Can't get FileDescriptor num");
JP Abgrall98a4f7e2011-09-02 15:36:33 -070040 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 Bhatf5df7002014-03-25 20:51:35 +000050static jint QTagUid_untagSocketFd(JNIEnv* env, jclass,
51 jobject fileDescriptor) {
JP Abgrall98a4f7e2011-09-02 15:36:33 -070052 int userFd = jniGetFDFromFileDescriptor(env, fileDescriptor);
53
Mathieu Chartier98671c32014-08-20 10:04:08 -070054 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +000055 ALOGE("Can't get FileDescriptor num");
JP Abgrall98a4f7e2011-09-02 15:36:33 -070056 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
66static 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
76static 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 Micay76f6a862015-09-19 17:31:01 -040086static const JNINativeMethod gQTagUidMethods[] = {
JP Abgrall98a4f7e2011-09-02 15:36:33 -070087 { "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
93int register_android_server_NetworkManagementSocketTagger(JNIEnv* env) {
94 return jniRegisterNativeMethods(env, "com/android/server/NetworkManagementSocketTagger", gQTagUidMethods, NELEM(gQTagUidMethods));
95}
96
97};