Home | History | Annotate | Download | only in jni
      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 
     20 #include <nativehelper/JNIHelp.h>
     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 
     38   if (env->ExceptionCheck()) {
     39     ALOGE("Can't get FileDescriptor num");
     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 
     50 static jint QTagUid_untagSocketFd(JNIEnv* env, jclass,
     51                                   jobject fileDescriptor) {
     52   int userFd = jniGetFDFromFileDescriptor(env, fileDescriptor);
     53 
     54   if (env->ExceptionCheck()) {
     55     ALOGE("Can't get FileDescriptor num");
     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 
     86 static const JNINativeMethod gQTagUidMethods[] = {
     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 };
     98