android_net_LocalSocketImpl.cpp revision 092eb8d43baffd7723bc7847c180a32fb5269481
1f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski/*
2f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski * Copyright (C) 2006 The Android Open Source Project
3f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski *
4f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski * Licensed under the Apache License, Version 2.0 (the "License");
5bdbde55592792efe350acd6a46733f439f6a3f3dAurimas Liutikas * you may not use this file except in compliance with the License.
62682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru * You may obtain a copy of the License at
7f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski *
8f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski *      http://www.apache.org/licenses/LICENSE-2.0
9f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski *
102682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru * Unless required by applicable law or agreed to in writing, software
11f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski * distributed under the License is distributed on an "AS IS" BASIS,
12f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bdbde55592792efe350acd6a46733f439f6a3f3dAurimas Liutikas * See the License for the specific language governing permissions and
14f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski * limitations under the License.
15f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski */
16f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
17f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski#define LOG_TAG "LocalSocketImpl"
1848c95c4370533bf27e537cbca1e64d060a016c5fJon Larimer
19f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski#include "JNIHelp.h"
20f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski#include "jni.h"
21f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski#include "utils/Log.h"
22f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski#include "utils/misc.h"
23f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
24f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski#include <stdio.h>
25f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski#include <string.h>
26f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski#include <sys/types.h>
2748c95c4370533bf27e537cbca1e64d060a016c5fJon Larimer#include <sys/socket.h>
28f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski#include <sys/un.h>
29f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski#include <arpa/inet.h>
30f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski#include <netinet/in.h>
31f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski#include <stdlib.h>
32f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski#include <errno.h>
33f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski#include <unistd.h>
34f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski#include <sys/ioctl.h>
35f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
36f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski#include <cutils/sockets.h>
37f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski#include <netinet/tcp.h>
38f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski#include <ScopedUtfChars.h>
39f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
40f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinskinamespace android {
41f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
42f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinskistatic jfieldID field_inboundFileDescriptors;
43f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinskistatic jfieldID field_outboundFileDescriptors;
44f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinskistatic jclass class_Credentials;
45f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinskistatic jclass class_FileDescriptor;
46f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinskistatic jmethodID method_CredentialsInit;
47f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
48f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski/*
49f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski * private native FileDescriptor
50f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski * create_native(boolean stream)
51f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski *               throws IOException;
52f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski */
53f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinskistatic jobject
54f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinskisocket_create (JNIEnv *env, jobject object, jboolean stream)
55f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski{
56f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    int ret;
57f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
58f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    ret = socket(PF_LOCAL, stream ? SOCK_STREAM : SOCK_DGRAM, 0);
59f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
60f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    if (ret < 0) {
61f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        jniThrowIOException(env, errno);
62f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        return NULL;
6348c95c4370533bf27e537cbca1e64d060a016c5fJon Larimer    }
64f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
65f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    return jniCreateFileDescriptor(env,ret);
66f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski}
67f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
68f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski/* private native void connectLocal(FileDescriptor fd,
69f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski * String name, int namespace) throws IOException
70f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski */
71f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinskistatic void
72f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinskisocket_connect_local(JNIEnv *env, jobject object,
73f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski                        jobject fileDescriptor, jstring name, jint namespaceId)
74f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski{
75f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    int ret;
76f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    int fd;
77f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
7848c95c4370533bf27e537cbca1e64d060a016c5fJon Larimer    fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
79f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
80f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    if (env->ExceptionOccurred() != NULL) {
81f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        return;
82f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    }
83f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
84f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    ScopedUtfChars nameUtf8(env, name);
85f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
86f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    ret = socket_local_client_connect(
87f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski                fd,
88f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski                nameUtf8.c_str(),
89f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski                namespaceId,
90f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski                SOCK_STREAM);
91f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
92f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    if (ret < 0) {
93f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        jniThrowIOException(env, errno);
94f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        return;
95f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    }
96f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski}
97f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
982682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru#define DEFAULT_BACKLOG 4
99abaa0695c5361b36a7a2cdbe87c77bf60be20af7Robin Lee
100abaa0695c5361b36a7a2cdbe87c77bf60be20af7Robin Lee/* private native void bindLocal(FileDescriptor fd, String name, namespace)
1012682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru * throws IOException;
102abaa0695c5361b36a7a2cdbe87c77bf60be20af7Robin Lee */
103abaa0695c5361b36a7a2cdbe87c77bf60be20af7Robin Lee
104abaa0695c5361b36a7a2cdbe87c77bf60be20af7Robin Leestatic void
105abaa0695c5361b36a7a2cdbe87c77bf60be20af7Robin Leesocket_bind_local (JNIEnv *env, jobject object, jobject fileDescriptor,
106abaa0695c5361b36a7a2cdbe87c77bf60be20af7Robin Lee                jstring name, jint namespaceId)
107abaa0695c5361b36a7a2cdbe87c77bf60be20af7Robin Lee{
108abaa0695c5361b36a7a2cdbe87c77bf60be20af7Robin Lee    int ret;
109abaa0695c5361b36a7a2cdbe87c77bf60be20af7Robin Lee    int fd;
110abaa0695c5361b36a7a2cdbe87c77bf60be20af7Robin Lee
111abaa0695c5361b36a7a2cdbe87c77bf60be20af7Robin Lee    if (name == NULL) {
112abaa0695c5361b36a7a2cdbe87c77bf60be20af7Robin Lee        jniThrowNullPointerException(env, NULL);
113abaa0695c5361b36a7a2cdbe87c77bf60be20af7Robin Lee        return;
114abaa0695c5361b36a7a2cdbe87c77bf60be20af7Robin Lee    }
1152682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru
1162682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru    fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
1172682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru
1182682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru    if (env->ExceptionOccurred() != NULL) {
1192682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru        return;
1202682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru    }
1212682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru
1222682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru    ScopedUtfChars nameUtf8(env, name);
1232682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru
1242682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru    ret = socket_local_server_bind(fd, nameUtf8.c_str(), namespaceId);
1252682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru
1262682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru    if (ret < 0) {
1272682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru        jniThrowIOException(env, errno);
1282682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru        return;
1292682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru    }
1302682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru}
1312682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru
1322682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotaru/* private native void listen_native(int fd, int backlog) throws IOException; */
1332682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotarustatic void
1342682fa70678ec79131329a6071d70c3d84c23d41Alexandru-Andrei Rotarusocket_listen (JNIEnv *env, jobject object, jobject fileDescriptor, int backlog)
135f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski{
136f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    int ret;
137f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    int fd;
138f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
13948c95c4370533bf27e537cbca1e64d060a016c5fJon Larimer    fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
140f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
141f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    if (env->ExceptionOccurred() != NULL) {
142f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        return;
143f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    }
144f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
145f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    ret = listen(fd, backlog);
146f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
147f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    if (ret < 0) {
148f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        jniThrowIOException(env, errno);
149f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        return;
150f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    }
151f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski}
152f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
153f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski/*    private native FileDescriptor
154f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski**    accept (FileDescriptor fd, LocalSocketImpl s)
155f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski**                                   throws IOException;
156f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski*/
157f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinskistatic jobject
158f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinskisocket_accept (JNIEnv *env, jobject object, jobject fileDescriptor, jobject s)
159f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski{
160f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    union {
161f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        struct sockaddr address;
162f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        struct sockaddr_un un_address;
163f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    } sa;
164f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
165f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    int ret;
166f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    int retFD;
167f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    int fd;
168f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    socklen_t addrlen;
169f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
170f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    if (s == NULL) {
171f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        jniThrowNullPointerException(env, NULL);
172f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        return NULL;
173f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    }
174f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
175f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
176f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
177f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    if (env->ExceptionOccurred() != NULL) {
178f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        return NULL;
179f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    }
180f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
181f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    do {
182f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        addrlen = sizeof(sa);
183f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        ret = accept(fd, &(sa.address), &addrlen);
184f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    } while (ret < 0 && errno == EINTR);
185f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
186f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    if (ret < 0) {
187f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        jniThrowIOException(env, errno);
188f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        return NULL;
189f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    }
190f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
191f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    retFD = ret;
192f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
193f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    return jniCreateFileDescriptor(env, retFD);
194f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski}
195f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
196f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski/* private native void shutdown(FileDescriptor fd, boolean shutdownInput) */
197f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
198f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinskistatic void
199f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinskisocket_shutdown (JNIEnv *env, jobject object, jobject fileDescriptor,
200f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski                    jboolean shutdownInput)
201f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski{
202f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    int ret;
203f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    int fd;
204f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
205f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
206f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
207f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    if (env->ExceptionOccurred() != NULL) {
208f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        return;
209f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    }
210f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
211f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    ret = shutdown(fd, shutdownInput ? SHUT_RD : SHUT_WR);
212f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
213f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    if (ret < 0) {
214f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        jniThrowIOException(env, errno);
215f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        return;
216f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    }
217f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski}
218f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
219f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinskistatic bool
220f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinskijava_opt_to_real(int optID, int* opt, int* level)
221f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski{
222f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    switch (optID)
223f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    {
224f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        case 4098:
225f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            *opt = SO_RCVBUF;
226f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            *level = SOL_SOCKET;
227f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            return true;
228f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        case 4097:
229f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            *opt = SO_SNDBUF;
230f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            *level = SOL_SOCKET;
231f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            return true;
232f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        case 4102:
233f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            *opt = SO_SNDTIMEO;
234f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            *level = SOL_SOCKET;
235f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            return true;
236f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        case 128:
237f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            *opt = SO_LINGER;
238f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            *level = SOL_SOCKET;
239f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            return true;
240f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        case 1:
241f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            *opt = TCP_NODELAY;
242f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            *level = IPPROTO_TCP;
243f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            return true;
244f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        case 4:
245f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            *opt = SO_REUSEADDR;
246f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            *level = SOL_SOCKET;
247f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            return true;
248f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
249f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    }
250f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    return false;
251f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski}
252f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
253f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinskistatic jint
254f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinskisocket_getOption(JNIEnv *env, jobject object, jobject fileDescriptor, int optID)
255f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski{
256f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    int ret, value;
257f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    int opt, level;
258f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    int fd;
259f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
260f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    socklen_t size = sizeof(int);
261f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
262f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    if (!java_opt_to_real(optID, &opt, &level)) {
263f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        jniThrowIOException(env, -1);
264f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        return 0;
265f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    }
266f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
267f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
268f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
269f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    if (env->ExceptionOccurred() != NULL) {
270f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        return 0;
271f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    }
272f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
273f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    switch (opt)
274f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    {
275f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        case SO_LINGER:
276f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        {
277f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            struct linger lingr;
278f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            size = sizeof(lingr);
279f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            ret = getsockopt(fd, level, opt, &lingr, &size);
280f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            if (!lingr.l_onoff) {
281f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski                value = -1;
282f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            } else {
283f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski                value = lingr.l_linger;
284f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            }
285f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            break;
286f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        }
287f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        default:
288f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            ret = getsockopt(fd, level, opt, &value, &size);
289f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski            break;
290f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    }
291f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
292f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
293f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    if (ret != 0) {
294f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        jniThrowIOException(env, errno);
295f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        return 0;
296f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    }
297f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
298f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    return value;
299f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski}
300f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski
301f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinskistatic void socket_setOption(
302f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        JNIEnv *env, jobject object, jobject fileDescriptor, int optID,
303f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski        jint boolValue, jint intValue) {
304f741c3727383008b131cd3877cbdb3857e07fc9bAdam Lesinski    int ret;
305    int optname;
306    int level;
307    int fd;
308
309    if (!java_opt_to_real(optID, &optname, &level)) {
310        jniThrowIOException(env, -1);
311        return;
312    }
313
314    fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
315
316    if (env->ExceptionOccurred() != NULL) {
317        return;
318    }
319
320    switch (optname) {
321        case SO_LINGER: {
322            /*
323             * SO_LINGER is special because it needs to use a special
324             * "linger" struct as well as use the incoming boolean
325             * argument specially.
326             */
327            struct linger lingr;
328            lingr.l_onoff = boolValue ? 1 : 0; // Force it to be 0 or 1.
329            lingr.l_linger = intValue;
330            ret = setsockopt(fd, level, optname, &lingr, sizeof(lingr));
331            break;
332        }
333        case SO_SNDTIMEO: {
334            /*
335             * SO_TIMEOUT from the core library gets converted to
336             * SO_SNDTIMEO, but the option is supposed to set both
337             * send and receive timeouts. Note: The incoming timeout
338             * value is in milliseconds.
339             */
340            struct timeval timeout;
341            timeout.tv_sec = intValue / 1000;
342            timeout.tv_usec = (intValue % 1000) * 1000;
343
344            ret = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,
345                    (void *)&timeout, sizeof(timeout));
346
347            if (ret == 0) {
348                ret = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO,
349                        (void *)&timeout, sizeof(timeout));
350            }
351
352            break;
353        }
354        default: {
355            /*
356             * In all other cases, the translated option level and
357             * optname may be used directly for a call to setsockopt().
358             */
359            ret = setsockopt(fd, level, optname, &intValue, sizeof(intValue));
360            break;
361        }
362    }
363
364    if (ret != 0) {
365        jniThrowIOException(env, errno);
366        return;
367    }
368}
369
370static jint socket_available (JNIEnv *env, jobject object,
371        jobject fileDescriptor)
372{
373    int fd;
374
375    fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
376
377    if (env->ExceptionOccurred() != NULL) {
378        return (jint)-1;
379    }
380
381#if 1
382    int avail;
383    int ret = ioctl(fd, FIONREAD, &avail);
384
385    // If this were a non-socket fd, there would be other cases to worry
386    // about...
387
388    if (ret < 0) {
389        jniThrowIOException(env, errno);
390        return (jint) 0;
391    }
392
393    return (jint)avail;
394#else
395// there appears to be a bionic bug that prevents this version from working.
396
397    ssize_t ret;
398    struct msghdr msg;
399
400    memset(&msg, 0, sizeof(msg));
401
402    do {
403        ret = recvmsg(fd, &msg, MSG_PEEK | MSG_DONTWAIT | MSG_NOSIGNAL);
404    } while (ret < 0 && errno == EINTR);
405
406
407    // MSG_PEEK returns 0 on EOF and EWOULDBLOCK on none available
408    if (ret < 0 && errno == EWOULDBLOCK) {
409        return 0;
410    } if (ret < 0) {
411        jniThrowIOException(env, errno);
412        return -1;
413    }
414
415    return (jint)ret;
416#endif
417}
418
419static void socket_close (JNIEnv *env, jobject object, jobject fileDescriptor)
420{
421    int fd;
422    int err;
423
424    if (fileDescriptor == NULL) {
425        jniThrowNullPointerException(env, NULL);
426        return;
427    }
428
429    fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
430
431    if (env->ExceptionOccurred() != NULL) {
432        return;
433    }
434
435    do {
436        err = close(fd);
437    } while (err < 0 && errno == EINTR);
438
439    if (err < 0) {
440        jniThrowIOException(env, errno);
441        return;
442    }
443}
444
445/**
446 * Processes ancillary data, handling only
447 * SCM_RIGHTS. Creates appropriate objects and sets appropriate
448 * fields in the LocalSocketImpl object. Returns 0 on success
449 * or -1 if an exception was thrown.
450 */
451static int socket_process_cmsg(JNIEnv *env, jobject thisJ, struct msghdr * pMsg)
452{
453    struct cmsghdr *cmsgptr;
454
455    for (cmsgptr = CMSG_FIRSTHDR(pMsg);
456            cmsgptr != NULL; cmsgptr = CMSG_NXTHDR(pMsg, cmsgptr)) {
457
458        if (cmsgptr->cmsg_level != SOL_SOCKET) {
459            continue;
460        }
461
462        if (cmsgptr->cmsg_type == SCM_RIGHTS) {
463            int *pDescriptors = (int *)CMSG_DATA(cmsgptr);
464            jobjectArray fdArray;
465            int count
466                = ((cmsgptr->cmsg_len - CMSG_LEN(0)) / sizeof(int));
467
468            if (count < 0) {
469                jniThrowException(env, "java/io/IOException",
470                    "invalid cmsg length");
471                return -1;
472            }
473
474            fdArray = env->NewObjectArray(count, class_FileDescriptor, NULL);
475
476            if (fdArray == NULL) {
477                return -1;
478            }
479
480            for (int i = 0; i < count; i++) {
481                jobject fdObject
482                        = jniCreateFileDescriptor(env, pDescriptors[i]);
483
484                if (env->ExceptionOccurred() != NULL) {
485                    return -1;
486                }
487
488                env->SetObjectArrayElement(fdArray, i, fdObject);
489
490                if (env->ExceptionOccurred() != NULL) {
491                    return -1;
492                }
493            }
494
495            env->SetObjectField(thisJ, field_inboundFileDescriptors, fdArray);
496
497            if (env->ExceptionOccurred() != NULL) {
498                return -1;
499            }
500        }
501    }
502
503    return 0;
504}
505
506/**
507 * Reads data from a socket into buf, processing any ancillary data
508 * and adding it to thisJ.
509 *
510 * Returns the length of normal data read, or -1 if an exception has
511 * been thrown in this function.
512 */
513static ssize_t socket_read_all(JNIEnv *env, jobject thisJ, int fd,
514        void *buffer, size_t len)
515{
516    ssize_t ret;
517    ssize_t bytesread = 0;
518    struct msghdr msg;
519    struct iovec iv;
520    unsigned char *buf = (unsigned char *)buffer;
521    // Enough buffer for a pile of fd's. We throw an exception if
522    // this buffer is too small.
523    struct cmsghdr cmsgbuf[2*sizeof(cmsghdr) + 0x100];
524
525    memset(&msg, 0, sizeof(msg));
526    memset(&iv, 0, sizeof(iv));
527
528    iv.iov_base = buf;
529    iv.iov_len = len;
530
531    msg.msg_iov = &iv;
532    msg.msg_iovlen = 1;
533    msg.msg_control = cmsgbuf;
534    msg.msg_controllen = sizeof(cmsgbuf);
535
536    do {
537        ret = recvmsg(fd, &msg, MSG_NOSIGNAL);
538    } while (ret < 0 && errno == EINTR);
539
540    if (ret < 0 && errno == EPIPE) {
541        // Treat this as an end of stream
542        return 0;
543    }
544
545    if (ret < 0) {
546        jniThrowIOException(env, errno);
547        return -1;
548    }
549
550    if ((msg.msg_flags & (MSG_CTRUNC | MSG_OOB | MSG_ERRQUEUE)) != 0) {
551        // To us, any of the above flags are a fatal error
552
553        jniThrowException(env, "java/io/IOException",
554                "Unexpected error or truncation during recvmsg()");
555
556        return -1;
557    }
558
559    if (ret >= 0) {
560        socket_process_cmsg(env, thisJ, &msg);
561    }
562
563    return ret;
564}
565
566/**
567 * Writes all the data in the specified buffer to the specified socket.
568 *
569 * Returns 0 on success or -1 if an exception was thrown.
570 */
571static int socket_write_all(JNIEnv *env, jobject object, int fd,
572        void *buf, size_t len)
573{
574    ssize_t ret;
575    struct msghdr msg;
576    unsigned char *buffer = (unsigned char *)buf;
577    memset(&msg, 0, sizeof(msg));
578
579    jobjectArray outboundFds
580            = (jobjectArray)env->GetObjectField(
581                object, field_outboundFileDescriptors);
582
583    if (env->ExceptionOccurred() != NULL) {
584        return -1;
585    }
586
587    struct cmsghdr *cmsg;
588    int countFds = outboundFds == NULL ? 0 : env->GetArrayLength(outboundFds);
589    int fds[countFds];
590    char msgbuf[CMSG_SPACE(countFds)];
591
592    // Add any pending outbound file descriptors to the message
593    if (outboundFds != NULL) {
594
595        if (env->ExceptionOccurred() != NULL) {
596            return -1;
597        }
598
599        for (int i = 0; i < countFds; i++) {
600            jobject fdObject = env->GetObjectArrayElement(outboundFds, i);
601            if (env->ExceptionOccurred() != NULL) {
602                return -1;
603            }
604
605            fds[i] = jniGetFDFromFileDescriptor(env, fdObject);
606            if (env->ExceptionOccurred() != NULL) {
607                return -1;
608            }
609        }
610
611        // See "man cmsg" really
612        msg.msg_control = msgbuf;
613        msg.msg_controllen = sizeof msgbuf;
614        cmsg = CMSG_FIRSTHDR(&msg);
615        cmsg->cmsg_level = SOL_SOCKET;
616        cmsg->cmsg_type = SCM_RIGHTS;
617        cmsg->cmsg_len = CMSG_LEN(sizeof fds);
618        memcpy(CMSG_DATA(cmsg), fds, sizeof fds);
619    }
620
621    // We only write our msg_control during the first write
622    while (len > 0) {
623        struct iovec iv;
624        memset(&iv, 0, sizeof(iv));
625
626        iv.iov_base = buffer;
627        iv.iov_len = len;
628
629        msg.msg_iov = &iv;
630        msg.msg_iovlen = 1;
631
632        do {
633            ret = sendmsg(fd, &msg, MSG_NOSIGNAL);
634        } while (ret < 0 && errno == EINTR);
635
636        if (ret < 0) {
637            jniThrowIOException(env, errno);
638            return -1;
639        }
640
641        buffer += ret;
642        len -= ret;
643
644        // Wipes out any msg_control too
645        memset(&msg, 0, sizeof(msg));
646    }
647
648    return 0;
649}
650
651static jint socket_read (JNIEnv *env, jobject object, jobject fileDescriptor)
652{
653    int fd;
654    int err;
655
656    if (fileDescriptor == NULL) {
657        jniThrowNullPointerException(env, NULL);
658        return (jint)-1;
659    }
660
661    fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
662
663    if (env->ExceptionOccurred() != NULL) {
664        return (jint)0;
665    }
666
667    unsigned char buf;
668
669    err = socket_read_all(env, object, fd, &buf, 1);
670
671    if (err < 0) {
672        jniThrowIOException(env, errno);
673        return (jint)0;
674    }
675
676    if (err == 0) {
677        // end of file
678        return (jint)-1;
679    }
680
681    return (jint)buf;
682}
683
684static jint socket_readba (JNIEnv *env, jobject object,
685        jbyteArray buffer, jint off, jint len, jobject fileDescriptor)
686{
687    int fd;
688    jbyte* byteBuffer;
689    int ret;
690
691    if (fileDescriptor == NULL || buffer == NULL) {
692        jniThrowNullPointerException(env, NULL);
693        return (jint)-1;
694    }
695
696    if (off < 0 || len < 0 || (off + len) > env->GetArrayLength(buffer)) {
697        jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
698        return (jint)-1;
699    }
700
701    if (len == 0) {
702        // because socket_read_all returns 0 on EOF
703        return 0;
704    }
705
706    fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
707
708    if (env->ExceptionOccurred() != NULL) {
709        return (jint)-1;
710    }
711
712    byteBuffer = env->GetByteArrayElements(buffer, NULL);
713
714    if (NULL == byteBuffer) {
715        // an exception will have been thrown
716        return (jint)-1;
717    }
718
719    ret = socket_read_all(env, object,
720            fd, byteBuffer + off, len);
721
722    // A return of -1 above means an exception is pending
723
724    env->ReleaseByteArrayElements(buffer, byteBuffer, 0);
725
726    return (jint) ((ret == 0) ? -1 : ret);
727}
728
729static void socket_write (JNIEnv *env, jobject object,
730        jint b, jobject fileDescriptor)
731{
732    int fd;
733    int err;
734
735    if (fileDescriptor == NULL) {
736        jniThrowNullPointerException(env, NULL);
737        return;
738    }
739
740    fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
741
742    if (env->ExceptionOccurred() != NULL) {
743        return;
744    }
745
746    err = socket_write_all(env, object, fd, &b, 1);
747
748    // A return of -1 above means an exception is pending
749}
750
751static void socket_writeba (JNIEnv *env, jobject object,
752        jbyteArray buffer, jint off, jint len, jobject fileDescriptor)
753{
754    int fd;
755    int err;
756    jbyte* byteBuffer;
757
758    if (fileDescriptor == NULL || buffer == NULL) {
759        jniThrowNullPointerException(env, NULL);
760        return;
761    }
762
763    if (off < 0 || len < 0 || (off + len) > env->GetArrayLength(buffer)) {
764        jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
765        return;
766    }
767
768    fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
769
770    if (env->ExceptionOccurred() != NULL) {
771        return;
772    }
773
774    byteBuffer = env->GetByteArrayElements(buffer,NULL);
775
776    if (NULL == byteBuffer) {
777        // an exception will have been thrown
778        return;
779    }
780
781    err = socket_write_all(env, object, fd,
782            byteBuffer + off, len);
783
784    // A return of -1 above means an exception is pending
785
786    env->ReleaseByteArrayElements(buffer, byteBuffer, JNI_ABORT);
787}
788
789static jobject socket_get_peer_credentials(JNIEnv *env,
790        jobject object, jobject fileDescriptor)
791{
792    int err;
793    int fd;
794
795    if (fileDescriptor == NULL) {
796        jniThrowNullPointerException(env, NULL);
797        return NULL;
798    }
799
800    fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
801
802    if (env->ExceptionOccurred() != NULL) {
803        return NULL;
804    }
805
806    struct ucred creds;
807
808    memset(&creds, 0, sizeof(creds));
809    socklen_t szCreds = sizeof(creds);
810
811    err = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &creds, &szCreds);
812
813    if (err < 0) {
814        jniThrowIOException(env, errno);
815        return NULL;
816    }
817
818    if (szCreds == 0) {
819        return NULL;
820    }
821
822    return env->NewObject(class_Credentials, method_CredentialsInit,
823            creds.pid, creds.uid, creds.gid);
824}
825
826#if 0
827//TODO change this to return an instance of LocalSocketAddress
828static jobject socket_getSockName(JNIEnv *env,
829        jobject object, jobject fileDescriptor)
830{
831    int err;
832    int fd;
833
834    if (fileDescriptor == NULL) {
835        jniThrowNullPointerException(env, NULL);
836        return NULL;
837    }
838
839    fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
840
841    if (env->ExceptionOccurred() != NULL) {
842        return NULL;
843    }
844
845    union {
846        struct sockaddr address;
847        struct sockaddr_un un_address;
848    } sa;
849
850    memset(&sa, 0, sizeof(sa));
851
852    socklen_t namelen = sizeof(sa);
853    err = getsockname(fd, &(sa.address), &namelen);
854
855    if (err < 0) {
856        jniThrowIOException(env, errno);
857        return NULL;
858    }
859
860    if (sa.address.sa_family != AF_UNIX) {
861        // We think we're an impl only for AF_UNIX, so this should never happen.
862
863        jniThrowIOException(env, EINVAL);
864        return NULL;
865    }
866
867    if (sa.un_address.sun_path[0] == '\0') {
868    } else {
869    }
870
871
872
873
874}
875#endif
876
877/*
878 * JNI registration.
879 */
880static JNINativeMethod gMethods[] = {
881     /* name, signature, funcPtr */
882    {"getOption_native", "(Ljava/io/FileDescriptor;I)I", (void*)socket_getOption},
883    {"setOption_native", "(Ljava/io/FileDescriptor;III)V", (void*)socket_setOption},
884    {"create_native", "(Z)Ljava/io/FileDescriptor;", (void*)socket_create},
885    {"connectLocal", "(Ljava/io/FileDescriptor;Ljava/lang/String;I)V",
886                                                (void*)socket_connect_local},
887    {"bindLocal", "(Ljava/io/FileDescriptor;Ljava/lang/String;I)V", (void*)socket_bind_local},
888    {"listen_native", "(Ljava/io/FileDescriptor;I)V", (void*)socket_listen},
889    {"accept", "(Ljava/io/FileDescriptor;Landroid/net/LocalSocketImpl;)Ljava/io/FileDescriptor;", (void*)socket_accept},
890    {"shutdown", "(Ljava/io/FileDescriptor;Z)V", (void*)socket_shutdown},
891    {"available_native", "(Ljava/io/FileDescriptor;)I", (void*) socket_available},
892    {"close_native", "(Ljava/io/FileDescriptor;)V", (void*) socket_close},
893    {"read_native", "(Ljava/io/FileDescriptor;)I", (void*) socket_read},
894    {"readba_native", "([BIILjava/io/FileDescriptor;)I", (void*) socket_readba},
895    {"writeba_native", "([BIILjava/io/FileDescriptor;)V", (void*) socket_writeba},
896    {"write_native", "(ILjava/io/FileDescriptor;)V", (void*) socket_write},
897    {"getPeerCredentials_native",
898            "(Ljava/io/FileDescriptor;)Landroid/net/Credentials;",
899            (void*) socket_get_peer_credentials}
900    //,{"getSockName_native", "(Ljava/io/FileDescriptor;)Ljava/lang/String;",
901    //        (void *) socket_getSockName}
902
903};
904
905int register_android_net_LocalSocketImpl(JNIEnv *env)
906{
907    jclass clazz;
908
909    clazz = env->FindClass("android/net/LocalSocketImpl");
910
911    if (clazz == NULL) {
912        goto error;
913    }
914
915    field_inboundFileDescriptors = env->GetFieldID(clazz,
916            "inboundFileDescriptors", "[Ljava/io/FileDescriptor;");
917
918    if (field_inboundFileDescriptors == NULL) {
919        goto error;
920    }
921
922    field_outboundFileDescriptors = env->GetFieldID(clazz,
923            "outboundFileDescriptors", "[Ljava/io/FileDescriptor;");
924
925    if (field_outboundFileDescriptors == NULL) {
926        goto error;
927    }
928
929    class_Credentials = env->FindClass("android/net/Credentials");
930
931    if (class_Credentials == NULL) {
932        goto error;
933    }
934
935    class_Credentials = (jclass)env->NewGlobalRef(class_Credentials);
936
937    class_FileDescriptor = env->FindClass("java/io/FileDescriptor");
938
939    if (class_FileDescriptor == NULL) {
940        goto error;
941    }
942
943    class_FileDescriptor = (jclass)env->NewGlobalRef(class_FileDescriptor);
944
945    method_CredentialsInit
946            = env->GetMethodID(class_Credentials, "<init>", "(III)V");
947
948    if (method_CredentialsInit == NULL) {
949        goto error;
950    }
951
952    return jniRegisterNativeMethods(env,
953        "android/net/LocalSocketImpl", gMethods, NELEM(gMethods));
954
955error:
956    ALOGE("Error registering android.net.LocalSocketImpl");
957    return -1;
958}
959
960};
961