2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "LocalSocketImpl"
21 #include "utils/Log.h"
22 #include "utils/misc.h"
26 #include <sys/types.h>
27 #include <sys/socket.h>
29 #include <arpa/inet.h>
30 #include <netinet/in.h>
34 #include <sys/ioctl.h>
36 #include <cutils/sockets.h>
37 #include <netinet/tcp.h>
41 static jfieldID field_inboundFileDescriptors;
42 static jfieldID field_outboundFileDescriptors;
43 static jclass class_Credentials;
44 static jclass class_FileDescriptor;
45 static jmethodID method_CredentialsInit;
47 /* private native void connectLocal(FileDescriptor fd,
48 * String name, int namespace) throws IOException
51 socket_connect_local(JNIEnv *env, jobject object,
52 jobject fileDescriptor, jstring name, jint namespaceId)
58 nameUtf8 = env->GetStringUTFChars(name, NULL);
60 fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
62 if (env->ExceptionOccurred() != NULL) {
66 ret = socket_local_client_connect(
72 env->ReleaseStringUTFChars(name, nameUtf8);
75 jniThrowIOException(env, errno);
80 #define DEFAULT_BACKLOG 4
82 /* private native void bindLocal(FileDescriptor fd, String name, namespace)
87 socket_bind_local (JNIEnv *env, jobject object, jobject fileDescriptor,
88 jstring name, jint namespaceId)
96 jniThrowNullPointerException(env, NULL);
99 fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
101 if (env->ExceptionOccurred() != NULL) {
105 nameUtf8 = env->GetStringUTFChars(name, NULL);
107 ret = socket_local_server_bind(fd, nameUtf8, namespaceId);
109 env->ReleaseStringUTFChars(name, nameUtf8);
112 jniThrowIOException(env, errno);
117 /* private native void listen_native(int fd, int backlog) throws IOException; */
119 socket_listen (JNIEnv *env, jobject object, jobject fileDescriptor, int backlog)
124 fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
126 if (env->ExceptionOccurred() != NULL) {
130 ret = listen(fd, backlog);
133 jniThrowIOException(env, errno);
138 /* private native FileDescriptor
139 ** accept (FileDescriptor fd, LocalSocketImpl s)
140 ** throws IOException;
143 socket_accept (JNIEnv *env, jobject object, jobject fileDescriptor, jobject s)
146 struct sockaddr address;
147 struct sockaddr_un un_address;
156 jniThrowNullPointerException(env, NULL);
160 fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
162 if (env->ExceptionOccurred() != NULL) {
167 addrlen = sizeof(sa);
168 ret = accept(fd, &(sa.address), &addrlen);
169 } while (ret < 0 && errno == EINTR);
172 jniThrowIOException(env, errno);
178 return jniCreateFileDescriptor(env, retFD);
181 /* private native void shutdown(FileDescriptor fd, boolean shutdownInput) */
184 socket_shutdown (JNIEnv *env, jobject object, jobject fileDescriptor,
185 jboolean shutdownInput)
190 fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
192 if (env->ExceptionOccurred() != NULL) {
196 ret = shutdown(fd, shutdownInput ? SHUT_RD : SHUT_WR);
199 jniThrowIOException(env, errno);
205 java_opt_to_real(int optID, int* opt, int* level)
227 *level = IPPROTO_TCP;
239 socket_getOption(JNIEnv *env, jobject object, jobject fileDescriptor, int optID)
245 socklen_t size = sizeof(int);
247 if (!java_opt_to_real(optID, &opt, &level)) {
248 jniThrowIOException(env, -1);
252 fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
254 if (env->ExceptionOccurred() != NULL) {
263 size = sizeof(lingr);
264 ret = getsockopt(fd, level, opt, &lingr, &size);
265 if (!lingr.l_onoff) {
268 value = lingr.l_linger;
273 ret = getsockopt(fd, level, opt, &value, &size);
279 jniThrowIOException(env, errno);
286 static void socket_setOption(
287 JNIEnv *env, jobject object, jobject fileDescriptor, int optID,
288 jint boolValue, jint intValue) {
294 if (!java_opt_to_real(optID, &optname, &level)) {
295 jniThrowIOException(env, -1);
299 fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
301 if (env->ExceptionOccurred() != NULL) {
308 * SO_LINGER is special because it needs to use a special
309 * "linger" struct as well as use the incoming boolean
310 * argument specially.
313 lingr.l_onoff = boolValue ? 1 : 0; // Force it to be 0 or 1.
314 lingr.l_linger = intValue;
315 ret = setsockopt(fd, level, optname, &lingr, sizeof(lingr));
320 * SO_TIMEOUT from the core library gets converted to
321 * SO_SNDTIMEO, but the option is supposed to set both
322 * send and receive timeouts. Note: The incoming timeout
323 * value is in milliseconds.
325 struct timeval timeout;
326 timeout.tv_sec = intValue / 1000;
327 timeout.tv_usec = (intValue % 1000) * 1000;
329 ret = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,
330 (void *)&timeout, sizeof(timeout));
333 ret = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO,
334 (void *)&timeout, sizeof(timeout));
341 * In all other cases, the translated option level and
342 * optname may be used directly for a call to setsockopt().
344 ret = setsockopt(fd, level, optname, &intValue, sizeof(intValue));
350 jniThrowIOException(env, errno);
354 static jint socket_pending (JNIEnv *env, jobject object,
355 jobject fileDescriptor)
359 fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
361 if (env->ExceptionOccurred() != NULL) {
366 int ret = ioctl(fd, TIOCOUTQ, &pending);
368 // If this were a non-socket fd, there would be other cases to worry
371 //ALOGD("socket_pending, ioctl ret:%d, pending:%d", ret, pending);
373 jniThrowIOException(env, errno);
377 return (jint)pending;
379 static jint socket_available (JNIEnv *env, jobject object,
380 jobject fileDescriptor)
384 fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
386 if (env->ExceptionOccurred() != NULL) {
392 int ret = ioctl(fd, FIONREAD, &avail);
394 // If this were a non-socket fd, there would be other cases to worry
398 jniThrowIOException(env, errno);
404 // there appears to be a bionic bug that prevents this version from working.
409 memset(&msg, 0, sizeof(msg));
412 ret = recvmsg(fd, &msg, MSG_PEEK | MSG_DONTWAIT | MSG_NOSIGNAL);
413 } while (ret < 0 && errno == EINTR);
416 // MSG_PEEK returns 0 on EOF and EWOULDBLOCK on none available
417 if (ret < 0 && errno == EWOULDBLOCK) {
420 jniThrowIOException(env, errno);
429 * Processes ancillary data, handling only
430 * SCM_RIGHTS. Creates appropriate objects and sets appropriate
431 * fields in the LocalSocketImpl object. Returns 0 on success
432 * or -1 if an exception was thrown.
434 static int socket_process_cmsg(JNIEnv *env, jobject thisJ, struct msghdr * pMsg)
436 struct cmsghdr *cmsgptr;
438 for (cmsgptr = CMSG_FIRSTHDR(pMsg);
439 cmsgptr != NULL; cmsgptr = CMSG_NXTHDR(pMsg, cmsgptr)) {
441 if (cmsgptr->cmsg_level != SOL_SOCKET) {
445 if (cmsgptr->cmsg_type == SCM_RIGHTS) {
446 int *pDescriptors = (int *)CMSG_DATA(cmsgptr);
447 jobjectArray fdArray;
449 = ((cmsgptr->cmsg_len - CMSG_LEN(0)) / sizeof(int));
452 jniThrowException(env, "java/io/IOException",
453 "invalid cmsg length");
456 fdArray = env->NewObjectArray(count, class_FileDescriptor, NULL);
458 if (fdArray == NULL) {
462 for (int i = 0; i < count; i++) {
464 = jniCreateFileDescriptor(env, pDescriptors[i]);
466 if (env->ExceptionOccurred() != NULL) {
470 env->SetObjectArrayElement(fdArray, i, fdObject);
472 if (env->ExceptionOccurred() != NULL) {
477 env->SetObjectField(thisJ, field_inboundFileDescriptors, fdArray);
479 if (env->ExceptionOccurred() != NULL) {
489 * Reads data from a socket into buf, processing any ancillary data
490 * and adding it to thisJ.
492 * Returns the length of normal data read, or -1 if an exception has
493 * been thrown in this function.
495 static ssize_t socket_read_all(JNIEnv *env, jobject thisJ, int fd,
496 void *buffer, size_t len)
499 ssize_t bytesread = 0;
502 unsigned char *buf = (unsigned char *)buffer;
503 // Enough buffer for a pile of fd's. We throw an exception if
504 // this buffer is too small.
505 struct cmsghdr cmsgbuf[2*sizeof(cmsghdr) + 0x100];
507 memset(&msg, 0, sizeof(msg));
508 memset(&iv, 0, sizeof(iv));
515 msg.msg_control = cmsgbuf;
516 msg.msg_controllen = sizeof(cmsgbuf);
519 ret = recvmsg(fd, &msg, MSG_NOSIGNAL);
520 } while (ret < 0 && errno == EINTR);
522 if (ret < 0 && errno == EPIPE) {
523 // Treat this as an end of stream
528 jniThrowIOException(env, errno);
532 if ((msg.msg_flags & (MSG_CTRUNC | MSG_OOB | MSG_ERRQUEUE)) != 0) {
533 // To us, any of the above flags are a fatal error
535 jniThrowException(env, "java/io/IOException",
536 "Unexpected error or truncation during recvmsg()");
542 socket_process_cmsg(env, thisJ, &msg);
549 * Writes all the data in the specified buffer to the specified socket.
551 * Returns 0 on success or -1 if an exception was thrown.
553 static int socket_write_all(JNIEnv *env, jobject object, int fd,
554 void *buf, size_t len)
558 unsigned char *buffer = (unsigned char *)buf;
559 memset(&msg, 0, sizeof(msg));
561 jobjectArray outboundFds
562 = (jobjectArray)env->GetObjectField(
563 object, field_outboundFileDescriptors);
565 if (env->ExceptionOccurred() != NULL) {
569 struct cmsghdr *cmsg;
570 int countFds = outboundFds == NULL ? 0 : env->GetArrayLength(outboundFds);
572 char msgbuf[CMSG_SPACE(countFds)];
574 // Add any pending outbound file descriptors to the message
575 if (outboundFds != NULL) {
577 if (env->ExceptionOccurred() != NULL) {
581 for (int i = 0; i < countFds; i++) {
582 jobject fdObject = env->GetObjectArrayElement(outboundFds, i);
583 if (env->ExceptionOccurred() != NULL) {
587 fds[i] = jniGetFDFromFileDescriptor(env, fdObject);
588 if (env->ExceptionOccurred() != NULL) {
593 // See "man cmsg" really
594 msg.msg_control = msgbuf;
595 msg.msg_controllen = sizeof msgbuf;
596 cmsg = CMSG_FIRSTHDR(&msg);
597 cmsg->cmsg_level = SOL_SOCKET;
598 cmsg->cmsg_type = SCM_RIGHTS;
599 cmsg->cmsg_len = CMSG_LEN(sizeof fds);
600 memcpy(CMSG_DATA(cmsg), fds, sizeof fds);
603 // We only write our msg_control during the first write
606 memset(&iv, 0, sizeof(iv));
608 iv.iov_base = buffer;
615 ret = sendmsg(fd, &msg, MSG_NOSIGNAL);
616 } while (ret < 0 && errno == EINTR);
619 jniThrowIOException(env, errno);
626 // Wipes out any msg_control too
627 memset(&msg, 0, sizeof(msg));
633 static jint socket_read (JNIEnv *env, jobject object, jobject fileDescriptor)
638 if (fileDescriptor == NULL) {
639 jniThrowNullPointerException(env, NULL);
643 fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
645 if (env->ExceptionOccurred() != NULL) {
651 err = socket_read_all(env, object, fd, &buf, 1);
654 jniThrowIOException(env, errno);
666 static jint socket_readba (JNIEnv *env, jobject object,
667 jbyteArray buffer, jint off, jint len, jobject fileDescriptor)
673 if (fileDescriptor == NULL || buffer == NULL) {
674 jniThrowNullPointerException(env, NULL);
678 if (off < 0 || len < 0 || (off + len) > env->GetArrayLength(buffer)) {
679 jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
684 // because socket_read_all returns 0 on EOF
688 fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
690 if (env->ExceptionOccurred() != NULL) {
694 byteBuffer = env->GetByteArrayElements(buffer, NULL);
696 if (NULL == byteBuffer) {
697 // an exception will have been thrown
701 ret = socket_read_all(env, object,
702 fd, byteBuffer + off, len);
704 // A return of -1 above means an exception is pending
706 env->ReleaseByteArrayElements(buffer, byteBuffer, 0);
708 return (jint) ((ret == 0) ? -1 : ret);
711 static void socket_write (JNIEnv *env, jobject object,
712 jint b, jobject fileDescriptor)
717 if (fileDescriptor == NULL) {
718 jniThrowNullPointerException(env, NULL);
722 fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
724 if (env->ExceptionOccurred() != NULL) {
728 err = socket_write_all(env, object, fd, &b, 1);
730 // A return of -1 above means an exception is pending
733 static void socket_writeba (JNIEnv *env, jobject object,
734 jbyteArray buffer, jint off, jint len, jobject fileDescriptor)
740 if (fileDescriptor == NULL || buffer == NULL) {
741 jniThrowNullPointerException(env, NULL);
745 if (off < 0 || len < 0 || (off + len) > env->GetArrayLength(buffer)) {
746 jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
750 fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
752 if (env->ExceptionOccurred() != NULL) {
756 byteBuffer = env->GetByteArrayElements(buffer,NULL);
758 if (NULL == byteBuffer) {
759 // an exception will have been thrown
763 err = socket_write_all(env, object, fd,
764 byteBuffer + off, len);
766 // A return of -1 above means an exception is pending
768 env->ReleaseByteArrayElements(buffer, byteBuffer, JNI_ABORT);
771 static jobject socket_get_peer_credentials(JNIEnv *env,
772 jobject object, jobject fileDescriptor)
777 if (fileDescriptor == NULL) {
778 jniThrowNullPointerException(env, NULL);
782 fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
784 if (env->ExceptionOccurred() != NULL) {
790 memset(&creds, 0, sizeof(creds));
791 socklen_t szCreds = sizeof(creds);
793 err = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &creds, &szCreds);
796 jniThrowIOException(env, errno);
804 return env->NewObject(class_Credentials, method_CredentialsInit,
805 creds.pid, creds.uid, creds.gid);
809 //TODO change this to return an instance of LocalSocketAddress
810 static jobject socket_getSockName(JNIEnv *env,
811 jobject object, jobject fileDescriptor)
816 if (fileDescriptor == NULL) {
817 jniThrowNullPointerException(env, NULL);
821 fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
823 if (env->ExceptionOccurred() != NULL) {
828 struct sockaddr address;
829 struct sockaddr_un un_address;
832 memset(&sa, 0, sizeof(sa));
834 socklen_t namelen = sizeof(sa);
835 err = getsockname(fd, &(sa.address), &namelen);
838 jniThrowIOException(env, errno);
842 if (sa.address.sa_family != AF_UNIX) {
843 // We think we're an impl only for AF_UNIX, so this should never happen.
845 jniThrowIOException(env, EINVAL);
849 if (sa.un_address.sun_path[0] == '\0') {
862 static JNINativeMethod gMethods[] = {
863 /* name, signature, funcPtr */
864 {"getOption_native", "(Ljava/io/FileDescriptor;I)I", (void*)socket_getOption},
865 {"setOption_native", "(Ljava/io/FileDescriptor;III)V", (void*)socket_setOption},
866 {"connectLocal", "(Ljava/io/FileDescriptor;Ljava/lang/String;I)V",
867 (void*)socket_connect_local},
868 {"bindLocal", "(Ljava/io/FileDescriptor;Ljava/lang/String;I)V", (void*)socket_bind_local},
869 {"listen_native", "(Ljava/io/FileDescriptor;I)V", (void*)socket_listen},
870 {"accept", "(Ljava/io/FileDescriptor;Landroid/net/LocalSocketImpl;)Ljava/io/FileDescriptor;", (void*)socket_accept},
871 {"shutdown", "(Ljava/io/FileDescriptor;Z)V", (void*)socket_shutdown},
872 {"available_native", "(Ljava/io/FileDescriptor;)I", (void*) socket_available},
873 {"pending_native", "(Ljava/io/FileDescriptor;)I", (void*) socket_pending},
874 {"read_native", "(Ljava/io/FileDescriptor;)I", (void*) socket_read},
875 {"readba_native", "([BIILjava/io/FileDescriptor;)I", (void*) socket_readba},
876 {"writeba_native", "([BIILjava/io/FileDescriptor;)V", (void*) socket_writeba},
877 {"write_native", "(ILjava/io/FileDescriptor;)V", (void*) socket_write},
878 {"getPeerCredentials_native",
879 "(Ljava/io/FileDescriptor;)Landroid/net/Credentials;",
880 (void*) socket_get_peer_credentials}
881 //,{"getSockName_native", "(Ljava/io/FileDescriptor;)Ljava/lang/String;",
882 // (void *) socket_getSockName}
886 int register_android_net_LocalSocketImpl(JNIEnv *env)
890 clazz = env->FindClass("android/net/LocalSocketImpl");
896 field_inboundFileDescriptors = env->GetFieldID(clazz,
897 "inboundFileDescriptors", "[Ljava/io/FileDescriptor;");
899 if (field_inboundFileDescriptors == NULL) {
903 field_outboundFileDescriptors = env->GetFieldID(clazz,
904 "outboundFileDescriptors", "[Ljava/io/FileDescriptor;");
906 if (field_outboundFileDescriptors == NULL) {
910 class_Credentials = env->FindClass("android/net/Credentials");
912 if (class_Credentials == NULL) {
916 class_Credentials = (jclass)env->NewGlobalRef(class_Credentials);
918 class_FileDescriptor = env->FindClass("java/io/FileDescriptor");
920 if (class_FileDescriptor == NULL) {
924 class_FileDescriptor = (jclass)env->NewGlobalRef(class_FileDescriptor);
926 method_CredentialsInit
927 = env->GetMethodID(class_Credentials, "<init>", "(III)V");
929 if (method_CredentialsInit == NULL) {
933 return jniRegisterNativeMethods(env,
934 "android/net/LocalSocketImpl", gMethods, NELEM(gMethods));
937 ALOGE("Error registering android.net.LocalSocketImpl");