Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright (C) 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 #include <jni.h>
     18 #include <stdio.h>
     19 #include <cutils/log.h>
     20 #include <asm/types.h>
     21 #include <sys/socket.h>
     22 #include <linux/netlink.h>
     23 #include <errno.h>
     24 #include <string.h>
     25 #include "JNIHelp.h"
     26 
     27 #include "android_net_cts_NetlinkSocket.h"
     28 
     29 static void android_net_cts_NetlinkSocket_create(JNIEnv* env, jclass,
     30     jobject fileDescriptor)
     31 {
     32     int sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
     33     if (sock == -1) {
     34         LOGE("Can't create socket %s", strerror(errno));
     35         jclass SocketException = env->FindClass("java/net/SocketException");
     36         env->ThrowNew(SocketException, "Can't create socket");
     37         return;
     38     }
     39     jniSetFileDescriptorOfFD(env, fileDescriptor, sock);
     40 }
     41 
     42 static int android_net_cts_NetlinkSocket_sendmsg(JNIEnv *e, jclass,
     43     jobject fileDescriptor, jint pid, jbyteArray packet)
     44 {
     45     void *bytes = (void *)e->GetByteArrayElements(packet, NULL);
     46     uint32_t length = (uint32_t)e->GetArrayLength(packet);
     47     struct sockaddr_nl snl;
     48     struct iovec iov = {bytes, length};
     49     struct msghdr msg = {&snl, sizeof(snl), &iov, 1, NULL, 0, 0};
     50 
     51     memset(&snl, 0, sizeof(snl));
     52     snl.nl_family = AF_NETLINK;
     53     snl.nl_pid = pid;
     54 
     55     int sock = jniGetFDFromFileDescriptor(e, fileDescriptor);
     56     int retval = sendmsg(sock, &msg, 0);
     57     e->ReleaseByteArrayElements(packet, (jbyte*)bytes, 0);
     58     return retval;
     59 }
     60 
     61 
     62 static JNINativeMethod gMethods[] = {
     63     {  "sendmsg", "(Ljava/io/FileDescriptor;I[B)I", (void *) android_net_cts_NetlinkSocket_sendmsg },
     64     {  "create_native", "(Ljava/io/FileDescriptor;)V", (void *) android_net_cts_NetlinkSocket_create },
     65 };
     66 
     67 int register_android_net_cts_NetlinkSocket(JNIEnv* env)
     68 {
     69     jclass clazz = env->FindClass("android/net/cts/NetlinkSocket");
     70 
     71     return env->RegisterNatives(clazz, gMethods,
     72             sizeof(gMethods) / sizeof(JNINativeMethod));
     73 }
     74