The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
Lorenzo Colitti | e859690 | 2009-09-18 15:25:06 -0700 | [diff] [blame] | 17 | // BEGIN android-changed |
| 18 | // |
| 19 | // This file has been substantially reworked in order to provide more IPv6 |
| 20 | // support and to move functionality from Java to native code where it made |
| 21 | // sense (e.g. when converting between IP addresses, socket structures, and |
| 22 | // strings, for which there exist fast and robust native implementations). |
| 23 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 24 | #define LOG_TAG "OSNetworkSystem" |
| 25 | |
| 26 | #include "JNIHelp.h" |
Elliott Hughes | 845ce3c | 2009-11-13 17:07:00 -0800 | [diff] [blame] | 27 | #include "LocalArray.h" |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 28 | #include "jni.h" |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 29 | |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 30 | #include <arpa/inet.h> |
| 31 | #include <assert.h> |
| 32 | #include <errno.h> |
| 33 | #include <netdb.h> |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 34 | #include <netinet/in.h> |
| 35 | #include <netinet/tcp.h> |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 36 | #include <stdio.h> |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 37 | #include <stdlib.h> |
Dan Bornstein | 31ba233 | 2009-10-26 16:30:39 -0700 | [diff] [blame] | 38 | #include <string.h> |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 39 | #include <sys/ioctl.h> |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 40 | #include <sys/socket.h> |
| 41 | #include <sys/time.h> |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 42 | #include <sys/un.h> |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 43 | #include <unistd.h> |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 44 | |
Lorenzo Colitti | e35dd52 | 2009-06-04 16:32:29 -0700 | [diff] [blame] | 45 | // Temporary hack to build on systems that don't have up-to-date libc headers. |
| 46 | #ifndef IPV6_TCLASS |
Dan Bornstein | 033eddd | 2009-10-29 10:15:42 -0700 | [diff] [blame] | 47 | #ifdef __linux__ |
| 48 | #define IPV6_TCLASS 67 // Linux |
| 49 | #else |
| 50 | #define IPV6_TCLASS -1 // BSD(-like); TODO: Something better than this! |
| 51 | #endif |
Lorenzo Colitti | e35dd52 | 2009-06-04 16:32:29 -0700 | [diff] [blame] | 52 | #endif |
| 53 | |
Dan Bornstein | fef8875 | 2009-11-04 15:34:14 -0800 | [diff] [blame] | 54 | /* |
Dan Bornstein | fef8875 | 2009-11-04 15:34:14 -0800 | [diff] [blame] | 55 | * TODO: The multicast code is highly platform-dependent, and for now |
| 56 | * we just punt on anything but Linux. |
| 57 | */ |
| 58 | #ifdef __linux__ |
| 59 | #define ENABLE_MULTICAST |
| 60 | #endif |
| 61 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 62 | #define JAVASOCKOPT_TCP_NODELAY 1 |
| 63 | #define JAVASOCKOPT_IP_TOS 3 |
| 64 | #define JAVASOCKOPT_SO_REUSEADDR 4 |
| 65 | #define JAVASOCKOPT_SO_KEEPALIVE 8 |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 66 | #define JAVASOCKOPT_IP_MULTICAST_IF 16 |
Elliott Hughes | 4401c75 | 2010-05-04 09:47:02 -0700 | [diff] [blame] | 67 | #define JAVASOCKOPT_MULTICAST_TTL 17 |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 68 | #define JAVASOCKOPT_IP_MULTICAST_LOOP 18 |
| 69 | #define JAVASOCKOPT_MCAST_ADD_MEMBERSHIP 19 |
| 70 | #define JAVASOCKOPT_MCAST_DROP_MEMBERSHIP 20 |
| 71 | #define JAVASOCKOPT_IP_MULTICAST_IF2 31 |
| 72 | #define JAVASOCKOPT_SO_BROADCAST 32 |
| 73 | #define JAVASOCKOPT_SO_LINGER 128 |
| 74 | #define JAVASOCKOPT_REUSEADDR_AND_REUSEPORT 10001 |
| 75 | #define JAVASOCKOPT_SO_SNDBUF 4097 |
| 76 | #define JAVASOCKOPT_SO_RCVBUF 4098 |
| 77 | #define JAVASOCKOPT_SO_RCVTIMEOUT 4102 |
| 78 | #define JAVASOCKOPT_SO_OOBINLINE 4099 |
| 79 | |
| 80 | /* constants for calling multi-call functions */ |
| 81 | #define SOCKET_STEP_START 10 |
| 82 | #define SOCKET_STEP_CHECK 20 |
| 83 | #define SOCKET_STEP_DONE 30 |
| 84 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 85 | #define SOCKET_CONNECT_STEP_START 0 |
| 86 | #define SOCKET_CONNECT_STEP_CHECK 1 |
| 87 | |
| 88 | #define SOCKET_OP_NONE 0 |
| 89 | #define SOCKET_OP_READ 1 |
| 90 | #define SOCKET_OP_WRITE 2 |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 91 | |
| 92 | #define SOCKET_NOFLAGS 0 |
| 93 | |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 94 | static struct CachedFields { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 95 | jfieldID fd_descriptor; |
| 96 | jclass iaddr_class; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 97 | jmethodID iaddr_getbyaddress; |
Lorenzo Colitti | 02936f3 | 2009-09-02 13:52:54 -0700 | [diff] [blame] | 98 | jclass i4addr_class; |
| 99 | jmethodID i4addr_class_init; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 100 | jfieldID iaddr_ipaddress; |
| 101 | jclass genericipmreq_class; |
| 102 | jclass integer_class; |
| 103 | jmethodID integer_class_init; |
| 104 | jfieldID integer_class_value; |
| 105 | jclass boolean_class; |
| 106 | jmethodID boolean_class_init; |
| 107 | jfieldID boolean_class_value; |
| 108 | jclass byte_class; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 109 | jfieldID byte_class_value; |
Lorenzo Colitti | 1790dd3 | 2009-09-08 18:30:56 -0700 | [diff] [blame] | 110 | jclass socketimpl_class; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 111 | jfieldID socketimpl_address; |
| 112 | jfieldID socketimpl_port; |
| 113 | jclass dpack_class; |
| 114 | jfieldID dpack_address; |
| 115 | jfieldID dpack_port; |
| 116 | jfieldID dpack_length; |
| 117 | } gCachedFields; |
| 118 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 119 | /* needed for connecting with timeout */ |
Elliott Hughes | 8752768 | 2009-10-06 14:08:53 -0700 | [diff] [blame] | 120 | struct selectFDSet { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 121 | int nfds; |
| 122 | int sock; |
| 123 | fd_set writeSet; |
| 124 | fd_set readSet; |
| 125 | fd_set exceptionSet; |
Elliott Hughes | 8752768 | 2009-10-06 14:08:53 -0700 | [diff] [blame] | 126 | }; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 127 | |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 128 | // TODO(enh): move to JNIHelp.h |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 129 | static void jniThrowExceptionWithErrno(JNIEnv* env, const char* exceptionClassName, int error) { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 130 | char buf[BUFSIZ]; |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 131 | jniThrowException(env, exceptionClassName, jniStrError(error, buf, sizeof(buf))); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Elliott Hughes | ba563a4 | 2009-11-05 13:50:06 -0800 | [diff] [blame] | 134 | static void jniThrowBindException(JNIEnv* env, int error) { |
| 135 | jniThrowExceptionWithErrno(env, "java/net/BindException", error); |
| 136 | } |
| 137 | |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 138 | static void jniThrowConnectException(JNIEnv* env, int error) { |
| 139 | jniThrowExceptionWithErrno(env, "java/net/ConnectException", error); |
| 140 | } |
| 141 | |
| 142 | static void jniThrowSecurityException(JNIEnv* env, int error) { |
| 143 | jniThrowExceptionWithErrno(env, "java/lang/SecurityException", error); |
| 144 | } |
| 145 | |
Elliott Hughes | ba563a4 | 2009-11-05 13:50:06 -0800 | [diff] [blame] | 146 | static void jniThrowSocketException(JNIEnv* env, int error) { |
| 147 | jniThrowExceptionWithErrno(env, "java/net/SocketException", error); |
| 148 | } |
| 149 | |
| 150 | static void jniThrowSocketTimeoutException(JNIEnv* env, int error) { |
| 151 | jniThrowExceptionWithErrno(env, "java/net/SocketTimeoutException", error); |
| 152 | } |
| 153 | |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 154 | // Used by functions that shouldn't throw SocketException. (These functions |
| 155 | // aren't meant to see bad addresses, so seeing one really does imply an |
| 156 | // internal error.) |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 157 | // TODO: fix the code (native and Java) so we don't paint ourselves into this corner. |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 158 | static void jniThrowBadAddressFamily(JNIEnv* env) { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 159 | jniThrowException(env, "java/lang/IllegalArgumentException", "Bad address family"); |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 162 | static bool jniGetFd(JNIEnv* env, jobject fileDescriptor, int& fd) { |
| 163 | fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
| 164 | if (fd == -1) { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 165 | jniThrowSocketException(env, EBADF); |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 166 | return false; |
| 167 | } |
| 168 | return true; |
| 169 | } |
| 170 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 171 | /** |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 172 | * Converts a native address structure to a Java byte array. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 173 | */ |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 174 | static jbyteArray socketAddressToByteArray(JNIEnv *env, struct sockaddr_storage *address) { |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 175 | void *rawAddress; |
| 176 | size_t addressLength; |
| 177 | if (address->ss_family == AF_INET) { |
| 178 | struct sockaddr_in *sin = (struct sockaddr_in *) address; |
| 179 | rawAddress = &sin->sin_addr.s_addr; |
| 180 | addressLength = 4; |
| 181 | } else if (address->ss_family == AF_INET6) { |
| 182 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) address; |
| 183 | rawAddress = &sin6->sin6_addr.s6_addr; |
| 184 | addressLength = 16; |
| 185 | } else { |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 186 | jniThrowBadAddressFamily(env); |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 187 | return NULL; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 190 | jbyteArray byteArray = env->NewByteArray(addressLength); |
| 191 | if (byteArray == NULL) { |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 192 | return NULL; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 193 | } |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 194 | env->SetByteArrayRegion(byteArray, 0, addressLength, (jbyte *) rawAddress); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 195 | |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 196 | return byteArray; |
| 197 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 198 | |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 199 | /** |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 200 | * Returns the port number in a sockaddr_storage structure. |
| 201 | * |
| 202 | * @param address the sockaddr_storage structure to get the port from |
| 203 | * |
| 204 | * @return the port number, or -1 if the address family is unknown. |
| 205 | */ |
| 206 | static int getSocketAddressPort(struct sockaddr_storage *address) { |
| 207 | switch (address->ss_family) { |
| 208 | case AF_INET: |
| 209 | return ntohs(((struct sockaddr_in *) address)->sin_port); |
| 210 | case AF_INET6: |
| 211 | return ntohs(((struct sockaddr_in6 *) address)->sin6_port); |
| 212 | default: |
| 213 | return -1; |
| 214 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /** |
Elliott Hughes | f5eafc5 | 2009-11-05 10:00:02 -0800 | [diff] [blame] | 218 | * Obtain the socket address family from an existing socket. |
| 219 | * |
| 220 | * @param socket the file descriptor of the socket to examine |
| 221 | * @return an integer, the address family of the socket |
Lorenzo Colitti | e859690 | 2009-09-18 15:25:06 -0700 | [diff] [blame] | 222 | */ |
Elliott Hughes | f5eafc5 | 2009-11-05 10:00:02 -0800 | [diff] [blame] | 223 | static int getSocketAddressFamily(int socket) { |
| 224 | sockaddr_storage ss; |
| 225 | socklen_t namelen = sizeof(ss); |
| 226 | int ret = getsockname(socket, (sockaddr*) &ss, &namelen); |
| 227 | if (ret != 0) { |
| 228 | return AF_UNSPEC; |
| 229 | } else { |
| 230 | return ss.ss_family; |
Lorenzo Colitti | e859690 | 2009-09-18 15:25:06 -0700 | [diff] [blame] | 231 | } |
Lorenzo Colitti | e859690 | 2009-09-18 15:25:06 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 234 | static jobject byteArrayToInetAddress(JNIEnv* env, jbyteArray byteArray) { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 235 | if (byteArray == NULL) { |
| 236 | return NULL; |
| 237 | } |
| 238 | return env->CallStaticObjectMethod(gCachedFields.iaddr_class, |
| 239 | gCachedFields.iaddr_getbyaddress, byteArray); |
| 240 | } |
| 241 | |
Lorenzo Colitti | e859690 | 2009-09-18 15:25:06 -0700 | [diff] [blame] | 242 | /** |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 243 | * Converts a native address structure to an InetAddress object. |
| 244 | * Throws a NullPointerException or an IOException in case of |
Lorenzo Colitti | 511ab05 | 2009-09-19 11:06:31 -0700 | [diff] [blame] | 245 | * error. |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 246 | * |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 247 | * @param sockAddress the sockaddr_storage structure to convert |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 248 | * |
| 249 | * @return a jobject representing an InetAddress |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 250 | */ |
Elliott Hughes | 5be11d7 | 2009-10-02 17:20:00 -0700 | [diff] [blame] | 251 | jobject socketAddressToInetAddress(JNIEnv* env, sockaddr_storage* sockAddress) { |
| 252 | jbyteArray byteArray = socketAddressToByteArray(env, sockAddress); |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 253 | return byteArrayToInetAddress(env, byteArray); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Elliott Hughes | 722a27e | 2010-03-05 20:37:25 -0800 | [diff] [blame] | 256 | // Handles translating between IPv4 and IPv6 addresses so -- where possible -- |
| 257 | // we can use either class of address with either an IPv4 or IPv6 socket. |
| 258 | class CompatibleSocketAddress { |
| 259 | public: |
| 260 | // Constructs an address corresponding to 'ss' that's compatible with 'fd'. |
| 261 | CompatibleSocketAddress(int fd, const sockaddr_storage& ss, bool mapUnspecified) { |
| 262 | const int desiredFamily = getSocketAddressFamily(fd); |
| 263 | if (ss.ss_family == AF_INET6) { |
| 264 | if (desiredFamily == AF_INET6) { |
| 265 | // Nothing to do. |
| 266 | mCompatibleAddress = reinterpret_cast<const sockaddr*>(&ss); |
| 267 | } else { |
| 268 | sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(&mTmp); |
| 269 | const sockaddr_in6* sin6 = reinterpret_cast<const sockaddr_in6*>(&ss); |
| 270 | memset(sin, 0, sizeof(*sin)); |
| 271 | sin->sin_family = AF_INET; |
| 272 | sin->sin_port = sin6->sin6_port; |
| 273 | if (IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr)) { |
| 274 | // We have an IPv6-mapped IPv4 address, but need plain old IPv4. |
| 275 | // Unmap the mapped address in ss into an IPv6 address in mTmp. |
| 276 | memcpy(&sin->sin_addr.s_addr, &sin6->sin6_addr.s6_addr[12], 4); |
| 277 | mCompatibleAddress = reinterpret_cast<const sockaddr*>(&mTmp); |
| 278 | } else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr)) { |
| 279 | // Translate the IPv6 loopback address to the IPv4 one. |
| 280 | sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 281 | mCompatibleAddress = reinterpret_cast<const sockaddr*>(&mTmp); |
| 282 | } else { |
| 283 | // We can't help you. We return what you gave us, and assume you'll |
| 284 | // get a sensible error when you use the address. |
| 285 | mCompatibleAddress = reinterpret_cast<const sockaddr*>(&ss); |
| 286 | } |
| 287 | } |
| 288 | } else /* ss.ss_family == AF_INET */ { |
| 289 | if (desiredFamily == AF_INET) { |
| 290 | // Nothing to do. |
| 291 | mCompatibleAddress = reinterpret_cast<const sockaddr*>(&ss); |
| 292 | } else { |
| 293 | // We have IPv4 and need IPv6. |
| 294 | // Map the IPv4 address in ss into an IPv6 address in mTmp. |
| 295 | const sockaddr_in* sin = reinterpret_cast<const sockaddr_in*>(&ss); |
| 296 | sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(&mTmp); |
| 297 | memset(sin6, 0, sizeof(*sin6)); |
| 298 | sin6->sin6_family = AF_INET6; |
| 299 | sin6->sin6_port = sin->sin_port; |
| 300 | // TODO: mapUnspecified was introduced because kernels < 2.6.31 don't allow |
| 301 | // you to bind to ::ffff:0.0.0.0. When we move to something >= 2.6.31, we |
| 302 | // should make the code behave as if mapUnspecified were always true, and |
| 303 | // remove the parameter. |
| 304 | if (sin->sin_addr.s_addr != 0 || mapUnspecified) { |
| 305 | memset(&(sin6->sin6_addr.s6_addr[10]), 0xff, 2); |
| 306 | } |
| 307 | memcpy(&sin6->sin6_addr.s6_addr[12], &sin->sin_addr.s_addr, 4); |
| 308 | mCompatibleAddress = reinterpret_cast<const sockaddr*>(&mTmp); |
| 309 | } |
| 310 | } |
Elliott Hughes | f5eafc5 | 2009-11-05 10:00:02 -0800 | [diff] [blame] | 311 | } |
Elliott Hughes | 722a27e | 2010-03-05 20:37:25 -0800 | [diff] [blame] | 312 | // Returns a pointer to an address compatible with the socket. |
| 313 | const sockaddr* get() const { |
| 314 | return mCompatibleAddress; |
Elliott Hughes | f5eafc5 | 2009-11-05 10:00:02 -0800 | [diff] [blame] | 315 | } |
Elliott Hughes | 722a27e | 2010-03-05 20:37:25 -0800 | [diff] [blame] | 316 | private: |
| 317 | const sockaddr* mCompatibleAddress; |
| 318 | sockaddr_storage mTmp; |
| 319 | }; |
Lorenzo Colitti | e859690 | 2009-09-18 15:25:06 -0700 | [diff] [blame] | 320 | |
| 321 | /** |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 322 | * Converts an InetAddress object and port number to a native address structure. |
| 323 | * Throws a NullPointerException or a SocketException in case of |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 324 | * error. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 325 | */ |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 326 | static bool byteArrayToSocketAddress(JNIEnv *env, |
Lorenzo Colitti | 511ab05 | 2009-09-19 11:06:31 -0700 | [diff] [blame] | 327 | jbyteArray addressBytes, int port, sockaddr_storage *sockaddress) { |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 328 | if (addressBytes == NULL) { |
Elliott Hughes | da4f31d | 2010-01-28 13:43:39 -0800 | [diff] [blame] | 329 | jniThrowNullPointerException(env, NULL); |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 330 | return false; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 331 | } |
Lorenzo Colitti | e859690 | 2009-09-18 15:25:06 -0700 | [diff] [blame] | 332 | |
| 333 | // Convert the IP address bytes to the proper IP address type. |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 334 | size_t addressLength = env->GetArrayLength(addressBytes); |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 335 | memset(sockaddress, 0, sizeof(*sockaddress)); |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 336 | if (addressLength == 4) { |
| 337 | // IPv4 address. |
Lorenzo Colitti | 511ab05 | 2009-09-19 11:06:31 -0700 | [diff] [blame] | 338 | sockaddr_in *sin = reinterpret_cast<sockaddr_in*>(sockaddress); |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 339 | sin->sin_family = AF_INET; |
| 340 | sin->sin_port = htons(port); |
Elliott Hughes | 44550df | 2009-09-08 19:44:54 -0700 | [diff] [blame] | 341 | jbyte* dst = reinterpret_cast<jbyte*>(&sin->sin_addr.s_addr); |
| 342 | env->GetByteArrayRegion(addressBytes, 0, 4, dst); |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 343 | } else if (addressLength == 16) { |
| 344 | // IPv6 address. |
Lorenzo Colitti | 511ab05 | 2009-09-19 11:06:31 -0700 | [diff] [blame] | 345 | sockaddr_in6 *sin6 = reinterpret_cast<sockaddr_in6*>(sockaddress); |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 346 | sin6->sin6_family = AF_INET6; |
| 347 | sin6->sin6_port = htons(port); |
Elliott Hughes | 44550df | 2009-09-08 19:44:54 -0700 | [diff] [blame] | 348 | jbyte* dst = reinterpret_cast<jbyte*>(&sin6->sin6_addr.s6_addr); |
| 349 | env->GetByteArrayRegion(addressBytes, 0, 16, dst); |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 350 | } else { |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 351 | jniThrowBadAddressFamily(env); |
| 352 | return false; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 353 | } |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 354 | return true; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /** |
Lorenzo Colitti | 511ab05 | 2009-09-19 11:06:31 -0700 | [diff] [blame] | 358 | * Converts an InetAddress object and port number to a native address structure. |
Lorenzo Colitti | 511ab05 | 2009-09-19 11:06:31 -0700 | [diff] [blame] | 359 | */ |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 360 | static bool inetAddressToSocketAddress(JNIEnv *env, jobject inetaddress, |
Lorenzo Colitti | 511ab05 | 2009-09-19 11:06:31 -0700 | [diff] [blame] | 361 | int port, sockaddr_storage *sockaddress) { |
| 362 | // Get the byte array that stores the IP address bytes in the InetAddress. |
| 363 | if (inetaddress == NULL) { |
Elliott Hughes | da4f31d | 2010-01-28 13:43:39 -0800 | [diff] [blame] | 364 | jniThrowNullPointerException(env, NULL); |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 365 | return false; |
Lorenzo Colitti | 511ab05 | 2009-09-19 11:06:31 -0700 | [diff] [blame] | 366 | } |
| 367 | jbyteArray addressBytes = |
| 368 | reinterpret_cast<jbyteArray>(env->GetObjectField(inetaddress, |
| 369 | gCachedFields.iaddr_ipaddress)); |
| 370 | |
| 371 | return byteArrayToSocketAddress(env, addressBytes, port, sockaddress); |
| 372 | } |
| 373 | |
| 374 | /** |
Lorenzo Colitti | 511ab05 | 2009-09-19 11:06:31 -0700 | [diff] [blame] | 375 | * Convert a Java byte array representing an IP address to a Java string. |
| 376 | * |
| 377 | * @param addressByteArray the byte array to convert. |
| 378 | * |
| 379 | * @return a string with the textual representation of the address. |
Lorenzo Colitti | 511ab05 | 2009-09-19 11:06:31 -0700 | [diff] [blame] | 380 | */ |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 381 | static jstring osNetworkSystem_byteArrayToIpString(JNIEnv* env, jobject, |
Lorenzo Colitti | 511ab05 | 2009-09-19 11:06:31 -0700 | [diff] [blame] | 382 | jbyteArray byteArray) { |
Lorenzo Colitti | 511ab05 | 2009-09-19 11:06:31 -0700 | [diff] [blame] | 383 | if (byteArray == NULL) { |
Elliott Hughes | da4f31d | 2010-01-28 13:43:39 -0800 | [diff] [blame] | 384 | jniThrowNullPointerException(env, NULL); |
Lorenzo Colitti | 511ab05 | 2009-09-19 11:06:31 -0700 | [diff] [blame] | 385 | return NULL; |
| 386 | } |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 387 | sockaddr_storage ss; |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 388 | if (!byteArrayToSocketAddress(env, byteArray, 0, &ss)) { |
Lorenzo Colitti | 511ab05 | 2009-09-19 11:06:31 -0700 | [diff] [blame] | 389 | return NULL; |
| 390 | } |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 391 | // TODO: getnameinfo seems to want its length parameter to be exactly |
| 392 | // sizeof(sockaddr_in) for an IPv4 address and sizeof (sockaddr_in6) for an |
| 393 | // IPv6 address. Fix getnameinfo so it accepts sizeof(sockaddr_storage), and |
| 394 | // then remove this hack. |
| 395 | int sa_size; |
| 396 | if (ss.ss_family == AF_INET) { |
| 397 | sa_size = sizeof(sockaddr_in); |
| 398 | } else if (ss.ss_family == AF_INET6) { |
| 399 | sa_size = sizeof(sockaddr_in6); |
| 400 | } else { |
| 401 | jniThrowBadAddressFamily(env); |
| 402 | return NULL; |
| 403 | } |
Lorenzo Colitti | 511ab05 | 2009-09-19 11:06:31 -0700 | [diff] [blame] | 404 | char ipString[INET6_ADDRSTRLEN]; |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 405 | int rc = getnameinfo(reinterpret_cast<sockaddr*>(&ss), sa_size, |
| 406 | ipString, sizeof(ipString), NULL, 0, NI_NUMERICHOST); |
| 407 | if (rc != 0) { |
| 408 | jniThrowException(env, "java/net/UnknownHostException", gai_strerror(rc)); |
Lorenzo Colitti | 511ab05 | 2009-09-19 11:06:31 -0700 | [diff] [blame] | 409 | return NULL; |
| 410 | } |
| 411 | return env->NewStringUTF(ipString); |
| 412 | } |
| 413 | |
| 414 | /** |
Lorenzo Colitti | e859690 | 2009-09-18 15:25:06 -0700 | [diff] [blame] | 415 | * Convert a Java string representing an IP address to a Java byte array. |
| 416 | * The formats accepted are: |
| 417 | * - IPv4: |
| 418 | * - 1.2.3.4 |
| 419 | * - 1.2.4 |
| 420 | * - 1.4 |
| 421 | * - 4 |
| 422 | * - IPv6 |
| 423 | * - Compressed form (2001:db8::1) |
| 424 | * - Uncompressed form (2001:db8:0:0:0:0:0:1) |
| 425 | * - IPv4-compatible (::192.0.2.0) |
| 426 | * - With an embedded IPv4 address (2001:db8::192.0.2.0). |
| 427 | * IPv6 addresses may appear in square brackets. |
| 428 | * |
| 429 | * @param addressByteArray the byte array to convert. |
| 430 | * |
| 431 | * @return a string with the textual representation of the address. |
| 432 | * |
| 433 | * @throws UnknownHostException the IP address was invalid. |
| 434 | */ |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 435 | static jbyteArray osNetworkSystem_ipStringToByteArray(JNIEnv* env, jobject, |
Lorenzo Colitti | e859690 | 2009-09-18 15:25:06 -0700 | [diff] [blame] | 436 | jstring javaString) { |
| 437 | if (javaString == NULL) { |
Elliott Hughes | da4f31d | 2010-01-28 13:43:39 -0800 | [diff] [blame] | 438 | jniThrowNullPointerException(env, NULL); |
Lorenzo Colitti | 6dbbfbd | 2009-10-02 12:45:21 -0700 | [diff] [blame] | 439 | return NULL; |
Lorenzo Colitti | e859690 | 2009-09-18 15:25:06 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Elliott Hughes | 845ce3c | 2009-11-13 17:07:00 -0800 | [diff] [blame] | 442 | // Convert the String to UTF bytes. |
Elliott Hughes | c00b8a4 | 2009-11-11 21:55:42 -0800 | [diff] [blame] | 443 | size_t byteCount = env->GetStringUTFLength(javaString); |
Elliott Hughes | 845ce3c | 2009-11-13 17:07:00 -0800 | [diff] [blame] | 444 | LocalArray<INET6_ADDRSTRLEN> bytes(byteCount + 1); |
| 445 | char* ipString = &bytes[0]; |
Elliott Hughes | c00b8a4 | 2009-11-11 21:55:42 -0800 | [diff] [blame] | 446 | env->GetStringUTFRegion(javaString, 0, env->GetStringLength(javaString), ipString); |
Lorenzo Colitti | e859690 | 2009-09-18 15:25:06 -0700 | [diff] [blame] | 447 | |
| 448 | // Accept IPv6 addresses (only) in square brackets for compatibility. |
Elliott Hughes | c00b8a4 | 2009-11-11 21:55:42 -0800 | [diff] [blame] | 449 | if (ipString[0] == '[' && ipString[byteCount - 1] == ']' && |
Carl Shapiro | 22ff07e | 2010-03-16 15:18:53 -0700 | [diff] [blame] | 450 | strchr(ipString, ':') != NULL) { |
Elliott Hughes | c00b8a4 | 2009-11-11 21:55:42 -0800 | [diff] [blame] | 451 | memmove(ipString, ipString + 1, byteCount - 2); |
| 452 | ipString[byteCount - 2] = '\0'; |
Lorenzo Colitti | e859690 | 2009-09-18 15:25:06 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | jbyteArray result = NULL; |
Lorenzo Colitti | 6dbbfbd | 2009-10-02 12:45:21 -0700 | [diff] [blame] | 456 | addrinfo hints; |
Lorenzo Colitti | e859690 | 2009-09-18 15:25:06 -0700 | [diff] [blame] | 457 | memset(&hints, 0, sizeof(hints)); |
| 458 | hints.ai_flags = AI_NUMERICHOST; |
Lorenzo Colitti | 6dbbfbd | 2009-10-02 12:45:21 -0700 | [diff] [blame] | 459 | |
Elliott Hughes | f5eafc5 | 2009-11-05 10:00:02 -0800 | [diff] [blame] | 460 | sockaddr_storage ss; |
| 461 | memset(&ss, 0, sizeof(ss)); |
| 462 | |
| 463 | addrinfo* res = NULL; |
Lorenzo Colitti | e859690 | 2009-09-18 15:25:06 -0700 | [diff] [blame] | 464 | int ret = getaddrinfo(ipString, NULL, &hints, &res); |
| 465 | if (ret == 0 && res) { |
Elliott Hughes | f5eafc5 | 2009-11-05 10:00:02 -0800 | [diff] [blame] | 466 | // Convert IPv4-mapped addresses to IPv4 addresses. |
| 467 | // The RI states "Java will never return an IPv4-mapped address". |
| 468 | sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(res->ai_addr); |
| 469 | if (res->ai_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { |
| 470 | sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(&ss); |
| 471 | sin->sin_family = AF_INET; |
| 472 | sin->sin_port = sin6->sin6_port; |
| 473 | memcpy(&sin->sin_addr.s_addr, &sin6->sin6_addr.s6_addr[12], 4); |
| 474 | result = socketAddressToByteArray(env, &ss); |
Lorenzo Colitti | e859690 | 2009-09-18 15:25:06 -0700 | [diff] [blame] | 475 | } else { |
Elliott Hughes | f5eafc5 | 2009-11-05 10:00:02 -0800 | [diff] [blame] | 476 | result = socketAddressToByteArray(env, reinterpret_cast<sockaddr_storage*>(res->ai_addr)); |
Lorenzo Colitti | e859690 | 2009-09-18 15:25:06 -0700 | [diff] [blame] | 477 | } |
| 478 | } else { |
| 479 | // For backwards compatibility, deal with address formats that |
| 480 | // getaddrinfo does not support. For example, 1.2.3, 1.3, and even 3 are |
| 481 | // valid IPv4 addresses according to the Java API. If getaddrinfo fails, |
| 482 | // try to use inet_aton. |
Elliott Hughes | f5eafc5 | 2009-11-05 10:00:02 -0800 | [diff] [blame] | 483 | sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(&ss); |
| 484 | if (inet_aton(ipString, &sin->sin_addr)) { |
| 485 | sin->sin_family = AF_INET; |
| 486 | sin->sin_port = 0; |
| 487 | result = socketAddressToByteArray(env, &ss); |
Lorenzo Colitti | e859690 | 2009-09-18 15:25:06 -0700 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
| 491 | if (res) { |
| 492 | freeaddrinfo(res); |
| 493 | } |
| 494 | |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 495 | if (!result) { |
Lorenzo Colitti | e859690 | 2009-09-18 15:25:06 -0700 | [diff] [blame] | 496 | env->ExceptionClear(); |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 497 | jniThrowException(env, "java/net/UnknownHostException", gai_strerror(ret)); |
Lorenzo Colitti | e859690 | 2009-09-18 15:25:06 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | return result; |
| 501 | } |
| 502 | |
| 503 | /** |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 504 | * Answer a new java.lang.Boolean object. |
| 505 | * |
| 506 | * @param env pointer to the JNI library |
| 507 | * @param anInt the Boolean constructor argument |
| 508 | * |
| 509 | * @return the new Boolean |
| 510 | */ |
Elliott Hughes | c83e55b | 2009-10-06 10:18:34 -0700 | [diff] [blame] | 511 | static jobject newJavaLangBoolean(JNIEnv * env, jint anInt) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 512 | jclass tempClass; |
| 513 | jmethodID tempMethod; |
| 514 | |
| 515 | tempClass = gCachedFields.boolean_class; |
| 516 | tempMethod = gCachedFields.boolean_class_init; |
| 517 | return env->NewObject(tempClass, tempMethod, (jboolean) (anInt != 0)); |
| 518 | } |
| 519 | |
| 520 | /** |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 521 | * Answer a new java.lang.Integer object. |
| 522 | * |
| 523 | * @param env pointer to the JNI library |
| 524 | * @param anInt the Integer constructor argument |
| 525 | * |
| 526 | * @return the new Integer |
| 527 | */ |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 528 | static jobject newJavaLangInteger(JNIEnv* env, jint anInt) { |
| 529 | return env->NewObject(gCachedFields.integer_class, gCachedFields.integer_class_init, anInt); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 530 | } |
| 531 | |
Elliott Hughes | 42d8ce7 | 2009-09-17 18:32:07 -0700 | [diff] [blame] | 532 | // Converts a number of milliseconds to a timeval. |
| 533 | static timeval toTimeval(long ms) { |
| 534 | timeval tv; |
| 535 | tv.tv_sec = ms / 1000; |
| 536 | tv.tv_usec = (ms - tv.tv_sec*1000) * 1000; |
| 537 | return tv; |
| 538 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 539 | |
Elliott Hughes | 42d8ce7 | 2009-09-17 18:32:07 -0700 | [diff] [blame] | 540 | // Converts a timeval to a number of milliseconds. |
| 541 | static long toMs(const timeval& tv) { |
| 542 | return tv.tv_sec * 1000 + tv.tv_usec / 1000; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Query OS for timestamp. |
| 547 | * Retrieve the current value of system clock and convert to milliseconds. |
| 548 | * |
| 549 | * @param[in] portLibrary The port library. |
| 550 | * |
| 551 | * @return 0 on failure, time value in milliseconds on success. |
| 552 | * @deprecated Use @ref time_hires_clock and @ref time_hires_delta |
| 553 | * |
| 554 | * technically, this should return I_64 since both timeval.tv_sec and |
| 555 | * timeval.tv_usec are long |
| 556 | */ |
| 557 | |
| 558 | static int time_msec_clock() { |
Elliott Hughes | 42d8ce7 | 2009-09-17 18:32:07 -0700 | [diff] [blame] | 559 | timeval tp; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 560 | struct timezone tzp; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 561 | gettimeofday(&tp, &tzp); |
Elliott Hughes | 42d8ce7 | 2009-09-17 18:32:07 -0700 | [diff] [blame] | 562 | return toMs(tp); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 563 | } |
| 564 | |
Elliott Hughes | ba563a4 | 2009-11-05 13:50:06 -0800 | [diff] [blame] | 565 | static int selectWait(int fd, int uSecTime) { |
| 566 | timeval tv; |
| 567 | timeval* tvp; |
| 568 | if (uSecTime >= 0) { |
| 569 | /* Use a timeout if uSecTime >= 0 */ |
| 570 | memset(&tv, 0, sizeof(tv)); |
| 571 | tv.tv_usec = uSecTime; |
| 572 | tvp = &tv; |
| 573 | } else { |
| 574 | /* Infinite timeout if uSecTime < 0 */ |
| 575 | tvp = NULL; |
| 576 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 577 | |
Elliott Hughes | ba563a4 | 2009-11-05 13:50:06 -0800 | [diff] [blame] | 578 | fd_set readFds; |
| 579 | FD_ZERO(&readFds); |
| 580 | FD_SET(fd, &readFds); |
| 581 | int result = select(fd + 1, &readFds, NULL, NULL, tvp); |
| 582 | if (result == -1) { |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 583 | return -errno; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 584 | } else if (result == 0) { |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 585 | return -ETIMEDOUT; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 586 | } |
| 587 | return result; |
| 588 | } |
| 589 | |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 590 | // Returns 0 on success, not obviously meaningful negative values on error. |
Elliott Hughes | ba563a4 | 2009-11-05 13:50:06 -0800 | [diff] [blame] | 591 | static int pollSelectWait(JNIEnv *env, jobject fileDescriptor, int timeout) { |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 592 | /* now try reading the socket for the timeout. |
| 593 | * if timeout is 0 try forever until the sockets gets ready or until an |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 594 | * exception occurs. |
| 595 | */ |
| 596 | int pollTimeoutUSec = 100000, pollMsec = 100; |
| 597 | int finishTime = 0; |
| 598 | int timeLeft = timeout; |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 599 | bool hasTimeout = timeout > 0; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 600 | int result = 0; |
| 601 | int handle; |
| 602 | |
| 603 | if (hasTimeout) { |
| 604 | finishTime = time_msec_clock() + timeout; |
| 605 | } |
| 606 | |
| 607 | int poll = 1; |
| 608 | |
| 609 | while (poll) { /* begin polling loop */ |
| 610 | |
| 611 | /* |
| 612 | * Fetch the handle every time in case the socket is closed. |
| 613 | */ |
| 614 | handle = jniGetFDFromFileDescriptor(env, fileDescriptor); |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 615 | if (handle == -1) { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 616 | jniThrowSocketException(env, EINTR); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 617 | return -1; |
| 618 | } |
| 619 | |
| 620 | if (hasTimeout) { |
| 621 | |
| 622 | if (timeLeft - 10 < pollMsec) { |
| 623 | pollTimeoutUSec = timeLeft <= 0 ? 0 : (timeLeft * 1000); |
| 624 | } |
| 625 | |
Elliott Hughes | ba563a4 | 2009-11-05 13:50:06 -0800 | [diff] [blame] | 626 | result = selectWait(handle, pollTimeoutUSec); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 627 | |
| 628 | /* |
| 629 | * because we are polling at a time smaller than timeout |
| 630 | * (presumably) lets treat an interrupt and timeout the same - go |
| 631 | * see if we're done timewise, and then just try again if not. |
| 632 | */ |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 633 | if (result == -ETIMEDOUT || result == -EINTR) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 634 | timeLeft = finishTime - time_msec_clock(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 635 | if (timeLeft <= 0) { |
| 636 | /* |
| 637 | * Always throw the "timeout" message because that is |
| 638 | * effectively what has happened, even if we happen to |
| 639 | * have been interrupted. |
| 640 | */ |
Elliott Hughes | ba563a4 | 2009-11-05 13:50:06 -0800 | [diff] [blame] | 641 | jniThrowSocketTimeoutException(env, ETIMEDOUT); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 642 | } else { |
| 643 | continue; // try again |
| 644 | } |
| 645 | |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 646 | } else if (result < 0) { |
| 647 | jniThrowSocketException(env, -result); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 648 | } |
| 649 | poll = 0; |
| 650 | |
| 651 | } else { /* polling with no timeout (why would you do this?)*/ |
| 652 | |
Elliott Hughes | ba563a4 | 2009-11-05 13:50:06 -0800 | [diff] [blame] | 653 | result = selectWait(handle, pollTimeoutUSec); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 654 | |
| 655 | /* |
| 656 | * if interrupted (or a timeout) just retry |
| 657 | */ |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 658 | if (result == -ETIMEDOUT || result == -EINTR) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 659 | continue; // try again |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 660 | } else if (result < 0) { |
| 661 | jniThrowSocketException(env, -result); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 662 | } |
| 663 | poll = 0; |
| 664 | } |
| 665 | } /* end polling loop */ |
| 666 | |
| 667 | return result; |
| 668 | } |
| 669 | |
| 670 | /** |
Lorenzo Colitti | dcfdc98 | 2009-06-15 11:02:24 -0700 | [diff] [blame] | 671 | * Wrapper for connect() that converts IPv4 addresses to IPv4-mapped IPv6 |
| 672 | * addresses if necessary. |
| 673 | * |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 674 | * @param socket the file descriptor of the socket to connect |
Lorenzo Colitti | dcfdc98 | 2009-06-15 11:02:24 -0700 | [diff] [blame] | 675 | * @param socketAddress the address to connect to |
| 676 | */ |
Elliott Hughes | f5eafc5 | 2009-11-05 10:00:02 -0800 | [diff] [blame] | 677 | static int doConnect(int fd, const sockaddr_storage* socketAddress) { |
Elliott Hughes | 722a27e | 2010-03-05 20:37:25 -0800 | [diff] [blame] | 678 | const CompatibleSocketAddress compatibleAddress(fd, *socketAddress, true); |
| 679 | return TEMP_FAILURE_RETRY(connect(fd, compatibleAddress.get(), sizeof(sockaddr_storage))); |
Lorenzo Colitti | dcfdc98 | 2009-06-15 11:02:24 -0700 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /** |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 683 | * Establish a connection to a peer with a timeout. This function is called |
| 684 | * repeatedly in order to carry out the connect and to allow other tasks to |
| 685 | * proceed on certain platforms. The caller must first call with |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 686 | * step = SOCKET_STEP_START, if the result is -EINPROGRESS it will then |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 687 | * call it with step = CHECK until either another error or 0 is returned to |
| 688 | * indicate the connect is complete. Each time the function should sleep for no |
| 689 | * more than timeout milliseconds. If the connect succeeds or an error occurs, |
| 690 | * the caller must always end the process by calling the function with |
| 691 | * step = SOCKET_STEP_DONE |
| 692 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 693 | * @param[in] timeout the timeout in milliseconds. If timeout is negative, |
| 694 | * perform a block operation. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 695 | * |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 696 | * @return 0, if no errors occurred, otherwise -errno. TODO: use +errno. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 697 | */ |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 698 | static int sockConnectWithTimeout(int fd, const sockaddr_storage& addr, int timeout, unsigned int step, selectFDSet* context) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 699 | int errorVal; |
| 700 | socklen_t errorValLen = sizeof(int); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 701 | |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 702 | if (step == SOCKET_STEP_START) { |
| 703 | context->sock = fd; |
| 704 | context->nfds = fd + 1; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 705 | |
Elliott Hughes | e9bb15a | 2009-09-30 13:26:11 -0700 | [diff] [blame] | 706 | /* set the socket to non-blocking */ |
| 707 | int block = JNI_TRUE; |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 708 | if (ioctl(fd, FIONBIO, &block) == -1) { |
| 709 | LOGE("ioctl(fd, FIONBIO, true) failed: %s %i", strerror(errno), errno); |
| 710 | return -errno; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 711 | } |
| 712 | |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 713 | if (doConnect(fd, &addr) == -1) { |
| 714 | return -errno; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | /* we connected right off the bat so just return */ |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 718 | return 0; |
| 719 | } else if (step == SOCKET_STEP_CHECK) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 720 | /* now check if we have connected yet */ |
| 721 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 722 | /* |
| 723 | * set the timeout value to be used. Because on some unix platforms we |
| 724 | * don't get notified when a socket is closed we only sleep for 100ms |
| 725 | * at a time |
Elliott Hughes | 42d8ce7 | 2009-09-17 18:32:07 -0700 | [diff] [blame] | 726 | * |
| 727 | * TODO: is this relevant for Android? |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 728 | */ |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 729 | if (timeout > 100) { |
Elliott Hughes | 42d8ce7 | 2009-09-17 18:32:07 -0700 | [diff] [blame] | 730 | timeout = 100; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 731 | } |
Elliott Hughes | 42d8ce7 | 2009-09-17 18:32:07 -0700 | [diff] [blame] | 732 | timeval passedTimeout(toTimeval(timeout)); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 733 | |
| 734 | /* initialize the FD sets for the select */ |
| 735 | FD_ZERO(&(context->exceptionSet)); |
| 736 | FD_ZERO(&(context->writeSet)); |
| 737 | FD_ZERO(&(context->readSet)); |
| 738 | FD_SET(context->sock, &(context->writeSet)); |
| 739 | FD_SET(context->sock, &(context->readSet)); |
| 740 | FD_SET(context->sock, &(context->exceptionSet)); |
| 741 | |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 742 | int rc = TEMP_FAILURE_RETRY(select(context->nfds, |
| 743 | &(context->readSet), &(context->writeSet), &(context->exceptionSet), |
| 744 | timeout >= 0 ? &passedTimeout : NULL)); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 745 | |
| 746 | /* if there is at least one descriptor ready to be checked */ |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 747 | if (rc > 0) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 748 | /* if the descriptor is in the write set we connected or failed */ |
| 749 | if (FD_ISSET(context->sock, &(context->writeSet))) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 750 | if (!FD_ISSET(context->sock, &(context->readSet))) { |
| 751 | /* ok we have connected ok */ |
| 752 | return 0; |
| 753 | } else { |
| 754 | /* ok we have more work to do to figure it out */ |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 755 | if (getsockopt(context->sock, SOL_SOCKET, SO_ERROR, &errorVal, &errorValLen) >= 0) { |
| 756 | return errorVal ? -errorVal : 0; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 757 | } else { |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 758 | return -errno; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | /* if the descriptor is in the exception set the connect failed */ |
| 764 | if (FD_ISSET(context->sock, &(context->exceptionSet))) { |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 765 | if (getsockopt(context->sock, SOL_SOCKET, SO_ERROR, &errorVal, &errorValLen) >= 0) { |
| 766 | return errorVal ? -errorVal : 0; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 767 | } |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 768 | return -errno; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 769 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 770 | } else if (rc < 0) { |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 771 | /* some other error occurred */ |
| 772 | return -errno; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | /* |
| 776 | * if we get here the timeout expired or the connect had not yet |
| 777 | * completed just indicate that the connect is not yet complete |
| 778 | */ |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 779 | return -EINPROGRESS; |
| 780 | } else if (step == SOCKET_STEP_DONE) { |
| 781 | /* we are done the connect or an error occurred so clean up */ |
| 782 | if (fd != -1) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 783 | int block = JNI_FALSE; |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 784 | ioctl(fd, FIONBIO, &block); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 785 | } |
| 786 | return 0; |
| 787 | } |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 788 | return -EFAULT; |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 789 | } |
| 790 | |
Dan Bornstein | fef8875 | 2009-11-04 15:34:14 -0800 | [diff] [blame] | 791 | #ifdef ENABLE_MULTICAST |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 792 | /* |
| 793 | * Find the interface index that was set for this socket by the IP_MULTICAST_IF |
| 794 | * or IPV6_MULTICAST_IF socket option. |
| 795 | * |
| 796 | * @param socket the socket to examine |
| 797 | * |
| 798 | * @return the interface index, or -1 on failure |
| 799 | * |
| 800 | * @note on internal failure, the errno variable will be set appropriately |
| 801 | */ |
| 802 | static int interfaceIndexFromMulticastSocket(int socket) { |
| 803 | int family = getSocketAddressFamily(socket); |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 804 | if (family == AF_INET) { |
| 805 | // IP_MULTICAST_IF returns a pointer to a struct ip_mreqn. |
| 806 | struct ip_mreqn tempRequest; |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 807 | socklen_t requestLength = sizeof(tempRequest); |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 808 | int rc = getsockopt(socket, IPPROTO_IP, IP_MULTICAST_IF, &tempRequest, &requestLength); |
| 809 | return (rc == -1) ? -1 : tempRequest.imr_ifindex; |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 810 | } else if (family == AF_INET6) { |
| 811 | // IPV6_MULTICAST_IF returns a pointer to an integer. |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 812 | int interfaceIndex; |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 813 | socklen_t requestLength = sizeof(interfaceIndex); |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 814 | int rc = getsockopt(socket, IPPROTO_IPV6, IPV6_MULTICAST_IF, &interfaceIndex, &requestLength); |
| 815 | return (rc == -1) ? -1 : interfaceIndex; |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 816 | } else { |
| 817 | errno = EAFNOSUPPORT; |
| 818 | return -1; |
| 819 | } |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 820 | } |
| 821 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 822 | /** |
| 823 | * Join/Leave the nominated multicast group on the specified socket. |
| 824 | * Implemented by setting the multicast 'add membership'/'drop membership' |
| 825 | * option at the HY_IPPROTO_IP level on the socket. |
| 826 | * |
| 827 | * Implementation note for multicast sockets in general: |
| 828 | * |
| 829 | * - This code is untested, because at the time of this writing multicast can't |
| 830 | * be properly tested on Android due to GSM routing restrictions. So it might |
| 831 | * or might not work. |
| 832 | * |
| 833 | * - The REUSEPORT socket option that Harmony employs is not supported on Linux |
| 834 | * and thus also not supported on Android. It's is not needed for multicast |
| 835 | * to work anyway (REUSEADDR should suffice). |
| 836 | * |
| 837 | * @param env pointer to the JNI library. |
| 838 | * @param socketP pointer to the hysocket to join/leave on. |
| 839 | * @param optVal pointer to the InetAddress, the multicast group to join/drop. |
| 840 | * |
| 841 | * @exception SocketException if an error occurs during the call |
| 842 | */ |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 843 | static void mcastAddDropMembership(JNIEnv *env, int handle, jobject optVal, int setSockOptVal) { |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 844 | struct sockaddr_storage sockaddrP; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 845 | int result; |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 846 | // By default, let the system decide which interface to use. |
| 847 | int interfaceIndex = 0; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 848 | |
| 849 | /* |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 850 | * Check whether we are getting an InetAddress or an Generic IPMreq. For now |
| 851 | * we support both so that we will not break the tests. If an InetAddress |
| 852 | * is passed in, only support IPv4 as obtaining an interface from an |
| 853 | * InetAddress is complex and should be done by the Java caller. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 854 | */ |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 855 | if (env->IsInstanceOf(optVal, gCachedFields.iaddr_class)) { |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 856 | /* |
| 857 | * optVal is an InetAddress. Construct a multicast request structure |
| 858 | * from this address. Support IPv4 only. |
| 859 | */ |
| 860 | struct ip_mreqn multicastRequest; |
| 861 | socklen_t length = sizeof(multicastRequest); |
| 862 | memset(&multicastRequest, 0, length); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 863 | |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 864 | interfaceIndex = interfaceIndexFromMulticastSocket(handle); |
| 865 | multicastRequest.imr_ifindex = interfaceIndex; |
| 866 | if (interfaceIndex == -1) { |
| 867 | jniThrowSocketException(env, errno); |
| 868 | return; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 869 | } |
| 870 | |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 871 | // Convert the inetAddress to an IPv4 address structure. |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 872 | if (!inetAddressToSocketAddress(env, optVal, 0, &sockaddrP)) { |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 873 | return; |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 874 | } |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 875 | if (sockaddrP.ss_family != AF_INET) { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 876 | jniThrowSocketException(env, EAFNOSUPPORT); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 877 | return; |
| 878 | } |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 879 | struct sockaddr_in *sin = (struct sockaddr_in *) &sockaddrP; |
| 880 | multicastRequest.imr_multiaddr = sin->sin_addr; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 881 | |
Dan Bornstein | 033eddd | 2009-10-29 10:15:42 -0700 | [diff] [blame] | 882 | result = setsockopt(handle, IPPROTO_IP, setSockOptVal, |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 883 | &multicastRequest, length); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 884 | if (0 != result) { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 885 | jniThrowSocketException(env, errno); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 886 | return; |
| 887 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 888 | } else { |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 889 | /* |
| 890 | * optVal is a GenericIPMreq object. Extract the relevant fields from |
| 891 | * it and construct a multicast request structure from these. Support |
| 892 | * both IPv4 and IPv6. |
| 893 | */ |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 894 | |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 895 | // Get the multicast address to join or leave. |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 896 | jclass cls = env->GetObjectClass(optVal); |
| 897 | jfieldID multiaddrID = env->GetFieldID(cls, "multiaddr", "Ljava/net/InetAddress;"); |
| 898 | jobject multiaddr = env->GetObjectField(optVal, multiaddrID); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 899 | |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 900 | // Get the interface index to use. |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 901 | jfieldID interfaceIdxID = env->GetFieldID(cls, "interfaceIdx", "I"); |
| 902 | interfaceIndex = env->GetIntField(optVal, interfaceIdxID); |
Elliott Hughes | b5fc5ec | 2009-11-19 18:32:43 -0800 | [diff] [blame] | 903 | LOGI("mcastAddDropMembership interfaceIndex=%i", interfaceIndex); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 904 | |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 905 | if (!inetAddressToSocketAddress(env, multiaddr, 0, &sockaddrP)) { |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 906 | return; |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 907 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 908 | |
Lorenzo Colitti | e325918 | 2009-09-30 15:49:22 -0700 | [diff] [blame] | 909 | int family = getSocketAddressFamily(handle); |
| 910 | |
| 911 | // Handle IPv4 multicast on an IPv6 socket. |
| 912 | if (family == AF_INET6 && sockaddrP.ss_family == AF_INET) { |
| 913 | family = AF_INET; |
| 914 | } |
| 915 | |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 916 | struct ip_mreqn ipv4Request; |
| 917 | struct ipv6_mreq ipv6Request; |
| 918 | void *multicastRequest; |
| 919 | socklen_t requestLength; |
| 920 | int level; |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 921 | switch (family) { |
| 922 | case AF_INET: |
| 923 | requestLength = sizeof(ipv4Request); |
| 924 | memset(&ipv4Request, 0, requestLength); |
| 925 | ipv4Request.imr_multiaddr = |
| 926 | ((struct sockaddr_in *) &sockaddrP)->sin_addr; |
| 927 | ipv4Request.imr_ifindex = interfaceIndex; |
| 928 | multicastRequest = &ipv4Request; |
Dan Bornstein | 033eddd | 2009-10-29 10:15:42 -0700 | [diff] [blame] | 929 | level = IPPROTO_IP; |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 930 | break; |
| 931 | case AF_INET6: |
Lorenzo Colitti | e325918 | 2009-09-30 15:49:22 -0700 | [diff] [blame] | 932 | // setSockOptVal is passed in by the caller and may be IPv4-only |
| 933 | if (setSockOptVal == IP_ADD_MEMBERSHIP) { |
| 934 | setSockOptVal = IPV6_ADD_MEMBERSHIP; |
| 935 | } |
| 936 | if (setSockOptVal == IP_DROP_MEMBERSHIP) { |
Elliott Hughes | 18f8a70 | 2010-04-26 13:45:16 -0700 | [diff] [blame] | 937 | setSockOptVal = IPV6_DROP_MEMBERSHIP; |
Lorenzo Colitti | e325918 | 2009-09-30 15:49:22 -0700 | [diff] [blame] | 938 | } |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 939 | requestLength = sizeof(ipv6Request); |
| 940 | memset(&ipv6Request, 0, requestLength); |
| 941 | ipv6Request.ipv6mr_multiaddr = |
| 942 | ((struct sockaddr_in6 *) &sockaddrP)->sin6_addr; |
Lorenzo Colitti | c0ff779 | 2009-06-11 19:35:46 -0700 | [diff] [blame] | 943 | ipv6Request.ipv6mr_interface = interfaceIndex; |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 944 | multicastRequest = &ipv6Request; |
Dan Bornstein | 033eddd | 2009-10-29 10:15:42 -0700 | [diff] [blame] | 945 | level = IPPROTO_IPV6; |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 946 | break; |
| 947 | default: |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 948 | jniThrowSocketException(env, EAFNOSUPPORT); |
| 949 | return; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | /* join/drop the multicast address */ |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 953 | result = setsockopt(handle, level, setSockOptVal, multicastRequest, |
| 954 | requestLength); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 955 | if (0 != result) { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 956 | jniThrowSocketException(env, errno); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 957 | return; |
| 958 | } |
| 959 | } |
| 960 | } |
Dan Bornstein | fef8875 | 2009-11-04 15:34:14 -0800 | [diff] [blame] | 961 | #endif // def ENABLE_MULTICAST |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 962 | |
Elliott Hughes | 7861ea9 | 2009-11-12 12:23:05 -0800 | [diff] [blame] | 963 | static bool initCachedFields(JNIEnv* env) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 964 | memset(&gCachedFields, 0, sizeof(gCachedFields)); |
Lorenzo Colitti | 1790dd3 | 2009-09-08 18:30:56 -0700 | [diff] [blame] | 965 | struct CachedFields *c = &gCachedFields; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 966 | |
Lorenzo Colitti | 1790dd3 | 2009-09-08 18:30:56 -0700 | [diff] [blame] | 967 | struct classInfo { |
| 968 | jclass *clazz; |
| 969 | const char *name; |
| 970 | } classes[] = { |
| 971 | {&c->iaddr_class, "java/net/InetAddress"}, |
| 972 | {&c->i4addr_class, "java/net/Inet4Address"}, |
| 973 | {&c->genericipmreq_class, "org/apache/harmony/luni/net/GenericIPMreq"}, |
| 974 | {&c->integer_class, "java/lang/Integer"}, |
| 975 | {&c->boolean_class, "java/lang/Boolean"}, |
| 976 | {&c->byte_class, "java/lang/Byte"}, |
Lorenzo Colitti | 1790dd3 | 2009-09-08 18:30:56 -0700 | [diff] [blame] | 977 | {&c->socketimpl_class, "java/net/SocketImpl"}, |
| 978 | {&c->dpack_class, "java/net/DatagramPacket"} |
| 979 | }; |
| 980 | for (unsigned i = 0; i < sizeof(classes) / sizeof(classes[0]); i++) { |
| 981 | classInfo c = classes[i]; |
| 982 | jclass tempClass = env->FindClass(c.name); |
Elliott Hughes | 7861ea9 | 2009-11-12 12:23:05 -0800 | [diff] [blame] | 983 | if (tempClass == NULL) return false; |
Lorenzo Colitti | 1790dd3 | 2009-09-08 18:30:56 -0700 | [diff] [blame] | 984 | *c.clazz = (jclass) env->NewGlobalRef(tempClass); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 985 | } |
| 986 | |
Lorenzo Colitti | 1790dd3 | 2009-09-08 18:30:56 -0700 | [diff] [blame] | 987 | struct methodInfo { |
| 988 | jmethodID *method; |
| 989 | jclass clazz; |
| 990 | const char *name; |
| 991 | const char *signature; |
| 992 | bool isStatic; |
| 993 | } methods[] = { |
| 994 | {&c->i4addr_class_init, c->i4addr_class, "<init>", "([B)V", false}, |
| 995 | {&c->integer_class_init, c->integer_class, "<init>", "(I)V", false}, |
| 996 | {&c->boolean_class_init, c->boolean_class, "<init>", "(Z)V", false}, |
Lorenzo Colitti | 1790dd3 | 2009-09-08 18:30:56 -0700 | [diff] [blame] | 997 | {&c->iaddr_getbyaddress, c->iaddr_class, "getByAddress", |
| 998 | "([B)Ljava/net/InetAddress;", true} |
| 999 | }; |
| 1000 | for (unsigned i = 0; i < sizeof(methods) / sizeof(methods[0]); i++) { |
| 1001 | methodInfo m = methods[i]; |
| 1002 | if (m.isStatic) { |
| 1003 | *m.method = env->GetStaticMethodID(m.clazz, m.name, m.signature); |
| 1004 | } else { |
| 1005 | *m.method = env->GetMethodID(m.clazz, m.name, m.signature); |
| 1006 | } |
Elliott Hughes | 7861ea9 | 2009-11-12 12:23:05 -0800 | [diff] [blame] | 1007 | if (*m.method == NULL) return false; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1008 | } |
| 1009 | |
Lorenzo Colitti | 1790dd3 | 2009-09-08 18:30:56 -0700 | [diff] [blame] | 1010 | struct fieldInfo { |
| 1011 | jfieldID *field; |
| 1012 | jclass clazz; |
| 1013 | const char *name; |
| 1014 | const char *type; |
| 1015 | } fields[] = { |
| 1016 | {&c->iaddr_ipaddress, c->iaddr_class, "ipaddress", "[B"}, |
| 1017 | {&c->integer_class_value, c->integer_class, "value", "I"}, |
| 1018 | {&c->boolean_class_value, c->boolean_class, "value", "Z"}, |
| 1019 | {&c->byte_class_value, c->byte_class, "value", "B"}, |
| 1020 | {&c->socketimpl_port, c->socketimpl_class, "port", "I"}, |
| 1021 | {&c->socketimpl_address, c->socketimpl_class, "address", |
| 1022 | "Ljava/net/InetAddress;"}, |
| 1023 | {&c->dpack_address, c->dpack_class, "address", |
| 1024 | "Ljava/net/InetAddress;"}, |
| 1025 | {&c->dpack_port, c->dpack_class, "port", "I"}, |
| 1026 | {&c->dpack_length, c->dpack_class, "length", "I"} |
| 1027 | }; |
| 1028 | for (unsigned i = 0; i < sizeof(fields) / sizeof(fields[0]); i++) { |
| 1029 | fieldInfo f = fields[i]; |
| 1030 | *f.field = env->GetFieldID(f.clazz, f.name, f.type); |
Elliott Hughes | 7861ea9 | 2009-11-12 12:23:05 -0800 | [diff] [blame] | 1031 | if (*f.field == NULL) return false; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1032 | } |
Elliott Hughes | 7861ea9 | 2009-11-12 12:23:05 -0800 | [diff] [blame] | 1033 | return true; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1034 | } |
| 1035 | |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1036 | /** |
| 1037 | * Helper function to create a socket of the specified type and bind it to a |
| 1038 | * Java file descriptor. |
| 1039 | * |
| 1040 | * @param fileDescriptor the file descriptor to bind the socket to |
| 1041 | * @param type the socket type to create, e.g., SOCK_STREAM |
Lorenzo Colitti | 2d97a2b | 2009-09-04 13:35:10 -0700 | [diff] [blame] | 1042 | * @throws SocketException an error occurred when creating the socket |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1043 | * |
Lorenzo Colitti | 2d97a2b | 2009-09-04 13:35:10 -0700 | [diff] [blame] | 1044 | * @return the socket file descriptor. On failure, an exception is thrown and |
| 1045 | * a negative value is returned. |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1046 | * |
| 1047 | */ |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1048 | static int createSocketFileDescriptor(JNIEnv* env, jobject fileDescriptor, int type) { |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1049 | if (fileDescriptor == NULL) { |
Elliott Hughes | da4f31d | 2010-01-28 13:43:39 -0800 | [diff] [blame] | 1050 | jniThrowNullPointerException(env, NULL); |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1051 | errno = EBADF; |
| 1052 | return -1; |
| 1053 | } |
| 1054 | |
Elliott Hughes | 722a27e | 2010-03-05 20:37:25 -0800 | [diff] [blame] | 1055 | // Try IPv6 but fall back to IPv4... |
| 1056 | int sock = socket(PF_INET6, type, 0); |
| 1057 | if (sock == -1 && errno == EAFNOSUPPORT) { |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1058 | sock = socket(PF_INET, type, 0); |
| 1059 | } |
Elliott Hughes | 722a27e | 2010-03-05 20:37:25 -0800 | [diff] [blame] | 1060 | if (sock == -1) { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 1061 | jniThrowSocketException(env, errno); |
Lorenzo Colitti | 66e35e8 | 2009-09-04 11:59:12 -0700 | [diff] [blame] | 1062 | return sock; |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1063 | } |
| 1064 | jniSetFileDescriptorOfFD(env, fileDescriptor, sock); |
| 1065 | return sock; |
| 1066 | } |
| 1067 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1068 | static void osNetworkSystem_createStreamSocket(JNIEnv* env, jobject, jobject fileDescriptor, jboolean) { |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1069 | createSocketFileDescriptor(env, fileDescriptor, SOCK_STREAM); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1070 | } |
| 1071 | |
Elliott Hughes | 4401c75 | 2010-05-04 09:47:02 -0700 | [diff] [blame] | 1072 | static void osNetworkSystem_createDatagramSocket(JNIEnv* env, jobject, jobject fileDescriptor, jboolean) { |
| 1073 | int fd = createSocketFileDescriptor(env, fileDescriptor, SOCK_DGRAM); |
| 1074 | #ifdef __linux__ |
| 1075 | // The RFC (http://tools.ietf.org/rfc/rfc3493.txt) says that IPV6_MULTICAST_HOPS defaults to 1. |
| 1076 | // The Linux kernel (at least up to 2.6.32) accidentally defaults to 64 (which would be correct |
| 1077 | // for the *unicast* hop limit). See http://www.spinics.net/lists/netdev/msg129022.html. |
| 1078 | // When that's fixed, we can remove this code. Until then, we manually set the hop limit on |
| 1079 | // IPv6 datagram sockets. (IPv4 is already correct.) |
| 1080 | if (fd != -1 && getSocketAddressFamily(fd) == AF_INET6) { |
| 1081 | int ttl = 1; |
| 1082 | setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(int)); |
| 1083 | } |
| 1084 | #endif |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1085 | } |
| 1086 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1087 | static jint osNetworkSystem_readDirect(JNIEnv* env, jobject, |
Elliott Hughes | 7088292 | 2009-10-21 17:09:13 -0700 | [diff] [blame] | 1088 | jobject fileDescriptor, jint address, jint count, jint timeout) { |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1089 | int fd; |
| 1090 | if (!jniGetFd(env, fileDescriptor, fd)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1091 | return 0; |
| 1092 | } |
| 1093 | |
Elliott Hughes | 7088292 | 2009-10-21 17:09:13 -0700 | [diff] [blame] | 1094 | if (timeout != 0) { |
Elliott Hughes | ba563a4 | 2009-11-05 13:50:06 -0800 | [diff] [blame] | 1095 | int result = selectWait(fd, timeout * 1000); |
Elliott Hughes | 7088292 | 2009-10-21 17:09:13 -0700 | [diff] [blame] | 1096 | if (result < 0) { |
| 1097 | return 0; |
| 1098 | } |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1099 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1100 | |
Elliott Hughes | 7088292 | 2009-10-21 17:09:13 -0700 | [diff] [blame] | 1101 | jbyte* dst = reinterpret_cast<jbyte*>(static_cast<uintptr_t>(address)); |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1102 | ssize_t bytesReceived = TEMP_FAILURE_RETRY(recv(fd, dst, count, SOCKET_NOFLAGS)); |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1103 | if (bytesReceived == 0) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1104 | return -1; |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1105 | } else if (bytesReceived == -1) { |
Elliott Hughes | d9179c4 | 2009-10-26 16:38:13 -0700 | [diff] [blame] | 1106 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
Elliott Hughes | 7088292 | 2009-10-21 17:09:13 -0700 | [diff] [blame] | 1107 | // We were asked to read a non-blocking socket with no data |
Elliott Hughes | d9179c4 | 2009-10-26 16:38:13 -0700 | [diff] [blame] | 1108 | // available, so report "no bytes read". |
Elliott Hughes | 7088292 | 2009-10-21 17:09:13 -0700 | [diff] [blame] | 1109 | return 0; |
| 1110 | } else { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 1111 | jniThrowSocketException(env, errno); |
Elliott Hughes | 7088292 | 2009-10-21 17:09:13 -0700 | [diff] [blame] | 1112 | return 0; |
| 1113 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1114 | } |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1115 | return bytesReceived; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1116 | } |
| 1117 | |
Brian Carlstrom | 44e0e56 | 2010-05-06 23:44:16 -0700 | [diff] [blame^] | 1118 | static jint osNetworkSystem_readSocketImpl(JNIEnv* env, jclass, |
Elliott Hughes | 7088292 | 2009-10-21 17:09:13 -0700 | [diff] [blame] | 1119 | jobject fileDescriptor, jbyteArray byteArray, jint offset, jint count, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1120 | jint timeout) { |
| 1121 | // LOGD("ENTER readSocketImpl"); |
| 1122 | |
Elliott Hughes | 7088292 | 2009-10-21 17:09:13 -0700 | [diff] [blame] | 1123 | jbyte* bytes = env->GetByteArrayElements(byteArray, NULL); |
| 1124 | if (bytes == NULL) { |
| 1125 | return -1; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1126 | } |
Elliott Hughes | 7088292 | 2009-10-21 17:09:13 -0700 | [diff] [blame] | 1127 | jint address = |
| 1128 | static_cast<jint>(reinterpret_cast<uintptr_t>(bytes + offset)); |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1129 | int result = osNetworkSystem_readDirect(env, NULL, |
Elliott Hughes | 7088292 | 2009-10-21 17:09:13 -0700 | [diff] [blame] | 1130 | fileDescriptor, address, count, timeout); |
| 1131 | env->ReleaseByteArrayElements(byteArray, bytes, 0); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1132 | return result; |
| 1133 | } |
| 1134 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1135 | static jint osNetworkSystem_writeDirect(JNIEnv* env, jobject, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1136 | jobject fileDescriptor, jint address, jint offset, jint count) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1137 | if (count <= 0) { |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
Elliott Hughes | d9179c4 | 2009-10-26 16:38:13 -0700 | [diff] [blame] | 1141 | int fd; |
| 1142 | if (!jniGetFd(env, fileDescriptor, fd)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1143 | return 0; |
| 1144 | } |
| 1145 | |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1146 | jbyte* message = reinterpret_cast<jbyte*>(static_cast<uintptr_t>(address + offset)); |
Elliott Hughes | d9179c4 | 2009-10-26 16:38:13 -0700 | [diff] [blame] | 1147 | int bytesSent = send(fd, message, count, SOCKET_NOFLAGS); |
| 1148 | if (bytesSent == -1) { |
| 1149 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
| 1150 | // We were asked to write to a non-blocking socket, but were told |
| 1151 | // it would block, so report "no bytes written". |
| 1152 | return 0; |
| 1153 | } else { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 1154 | jniThrowSocketException(env, errno); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1155 | return 0; |
| 1156 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1157 | } |
Elliott Hughes | d9179c4 | 2009-10-26 16:38:13 -0700 | [diff] [blame] | 1158 | return bytesSent; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1159 | } |
| 1160 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1161 | static jint osNetworkSystem_write(JNIEnv* env, jobject, |
Elliott Hughes | d9179c4 | 2009-10-26 16:38:13 -0700 | [diff] [blame] | 1162 | jobject fileDescriptor, jbyteArray byteArray, jint offset, jint count) { |
Elliott Hughes | d9179c4 | 2009-10-26 16:38:13 -0700 | [diff] [blame] | 1163 | jbyte* bytes = env->GetByteArrayElements(byteArray, NULL); |
| 1164 | if (bytes == NULL) { |
| 1165 | return -1; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1166 | } |
Elliott Hughes | d9179c4 | 2009-10-26 16:38:13 -0700 | [diff] [blame] | 1167 | jint address = static_cast<jint>(reinterpret_cast<uintptr_t>(bytes)); |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1168 | int result = osNetworkSystem_writeDirect(env, NULL, |
Elliott Hughes | d9179c4 | 2009-10-26 16:38:13 -0700 | [diff] [blame] | 1169 | fileDescriptor, address, offset, count); |
| 1170 | env->ReleaseByteArrayElements(byteArray, bytes, 0); |
The Android Open Source Project | 5d287a9 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1171 | return result; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1172 | } |
| 1173 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1174 | static void osNetworkSystem_setNonBlocking(JNIEnv* env, jobject, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1175 | jobject fileDescriptor, jboolean nonblocking) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1176 | int handle; |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1177 | if (!jniGetFd(env, fileDescriptor, handle)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1178 | return; |
| 1179 | } |
| 1180 | |
| 1181 | int block = nonblocking; |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1182 | int rc = ioctl(handle, FIONBIO, &block); |
| 1183 | if (rc == -1) { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 1184 | jniThrowSocketException(env, errno); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1188 | static jboolean osNetworkSystem_connectWithTimeout(JNIEnv* env, |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1189 | jobject, jobject fileDescriptor, jint timeout, jint trafficClass, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1190 | jobject inetAddr, jint port, jint step, jbyteArray passContext) { |
Elliott Hughes | 8752768 | 2009-10-06 14:08:53 -0700 | [diff] [blame] | 1191 | sockaddr_storage address; |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 1192 | if (!inetAddressToSocketAddress(env, inetAddr, port, &address)) { |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1193 | return JNI_FALSE; |
Elliott Hughes | 8752768 | 2009-10-06 14:08:53 -0700 | [diff] [blame] | 1194 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1195 | |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1196 | int handle; |
| 1197 | if (!jniGetFd(env, fileDescriptor, handle)) { |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1198 | return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1199 | } |
| 1200 | |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1201 | jbyte* contextBytes = env->GetByteArrayElements(passContext, NULL); |
| 1202 | selectFDSet* context = reinterpret_cast<selectFDSet*>(contextBytes); |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 1203 | int result = 0; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1204 | switch (step) { |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 1205 | case SOCKET_CONNECT_STEP_START: |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1206 | result = sockConnectWithTimeout(handle, address, 0, SOCKET_STEP_START, context); |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 1207 | break; |
| 1208 | case SOCKET_CONNECT_STEP_CHECK: |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1209 | result = sockConnectWithTimeout(handle, address, timeout, SOCKET_STEP_CHECK, context); |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 1210 | break; |
| 1211 | default: |
| 1212 | assert(false); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1213 | } |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1214 | env->ReleaseByteArrayElements(passContext, contextBytes, 0); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1215 | |
Elliott Hughes | 8752768 | 2009-10-06 14:08:53 -0700 | [diff] [blame] | 1216 | if (result == 0) { |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1217 | // Connected! |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1218 | sockConnectWithTimeout(handle, address, 0, SOCKET_STEP_DONE, NULL); |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1219 | return JNI_TRUE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1220 | } |
| 1221 | |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1222 | if (result == -EINPROGRESS) { |
| 1223 | // Not yet connected, but not yet denied either... Try again later. |
| 1224 | return JNI_FALSE; |
| 1225 | } |
| 1226 | |
| 1227 | // Denied! |
| 1228 | sockConnectWithTimeout(handle, address, 0, SOCKET_STEP_DONE, NULL); |
| 1229 | if (result == -EACCES) { |
| 1230 | jniThrowSecurityException(env, -result); |
| 1231 | } else { |
| 1232 | jniThrowConnectException(env, -result); |
| 1233 | } |
| 1234 | return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1235 | } |
| 1236 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1237 | static void osNetworkSystem_connectStreamWithTimeoutSocket(JNIEnv* env, |
| 1238 | jobject, jobject fileDescriptor, jint remotePort, jint timeout, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1239 | jint trafficClass, jobject inetAddr) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1240 | int result = 0; |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1241 | struct sockaddr_storage address; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1242 | int remainingTimeout = timeout; |
| 1243 | int passedTimeout = 0; |
| 1244 | int finishTime = 0; |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1245 | bool hasTimeout = timeout > 0; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1246 | |
| 1247 | /* if a timeout was specified calculate the finish time value */ |
| 1248 | if (hasTimeout) { |
| 1249 | finishTime = time_msec_clock() + (int) timeout; |
| 1250 | } |
| 1251 | |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1252 | int handle; |
| 1253 | if (!jniGetFd(env, fileDescriptor, handle)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1254 | return; |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1255 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1256 | |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 1257 | if (!inetAddressToSocketAddress(env, inetAddr, remotePort, &address)) { |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1258 | return; |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 1259 | } |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1260 | |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1261 | /* |
| 1262 | * we will be looping checking for when we are connected so allocate |
| 1263 | * the descriptor sets that we will use |
| 1264 | */ |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1265 | selectFDSet context; |
| 1266 | result = sockConnectWithTimeout(handle, address, 0, SOCKET_STEP_START, &context); |
| 1267 | if (result == 0) { |
| 1268 | /* ok we connected right away so we are done */ |
| 1269 | sockConnectWithTimeout(handle, address, 0, SOCKET_STEP_DONE, &context); |
| 1270 | return; |
| 1271 | } else if (result != -EINPROGRESS) { |
| 1272 | sockConnectWithTimeout(handle, address, 0, SOCKET_STEP_DONE, &context); |
| 1273 | /* we got an error other than NOTCONNECTED so we cannot continue */ |
| 1274 | if (result == -EACCES) { |
| 1275 | jniThrowSecurityException(env, -result); |
| 1276 | } else { |
| 1277 | jniThrowSocketException(env, -result); |
| 1278 | } |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1279 | return; |
| 1280 | } |
| 1281 | |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1282 | while (result == -EINPROGRESS) { |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1283 | passedTimeout = remainingTimeout; |
| 1284 | |
| 1285 | /* |
| 1286 | * ok now try and connect. Depending on the platform this may sleep |
| 1287 | * for up to passedTimeout milliseconds |
| 1288 | */ |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1289 | result = sockConnectWithTimeout(handle, address, passedTimeout, SOCKET_STEP_CHECK, &context); |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1290 | |
| 1291 | /* |
| 1292 | * now check if the socket is still connected. |
| 1293 | * Do it here as some platforms seem to think they |
| 1294 | * are connected if the socket is closed on them. |
| 1295 | */ |
| 1296 | handle = jniGetFDFromFileDescriptor(env, fileDescriptor); |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1297 | if (handle == -1) { |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1298 | sockConnectWithTimeout(handle, address, 0, SOCKET_STEP_DONE, &context); |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 1299 | jniThrowSocketException(env, EBADF); |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1300 | return; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | /* |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1304 | * check if we are now connected, |
| 1305 | * if so we can finish the process and return |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1306 | */ |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1307 | if (result == 0) { |
| 1308 | sockConnectWithTimeout(handle, address, 0, SOCKET_STEP_DONE, &context); |
| 1309 | return; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1310 | } |
| 1311 | |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1312 | /* |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1313 | * if the error is -EINPROGRESS then we have not yet |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1314 | * connected and we may not be done yet |
| 1315 | */ |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1316 | if (result == -EINPROGRESS) { |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1317 | /* check if the timeout has expired */ |
| 1318 | if (hasTimeout) { |
| 1319 | remainingTimeout = finishTime - time_msec_clock(); |
| 1320 | if (remainingTimeout <= 0) { |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1321 | sockConnectWithTimeout(handle, address, 0, SOCKET_STEP_DONE, &context); |
| 1322 | jniThrowSocketTimeoutException(env, ETIMEDOUT); |
| 1323 | return; |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1324 | } |
| 1325 | } else { |
| 1326 | remainingTimeout = 100; |
| 1327 | } |
| 1328 | } else { |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1329 | sockConnectWithTimeout(handle, address, remainingTimeout, SOCKET_STEP_DONE, &context); |
| 1330 | if (result == -ECONNRESET || result == -ECONNREFUSED || result == -EADDRNOTAVAIL || |
| 1331 | result == -EADDRINUSE || result == -ENETUNREACH) { |
| 1332 | jniThrowConnectException(env, -result); |
| 1333 | } else if (result == -EACCES) { |
| 1334 | jniThrowSecurityException(env, -result); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1335 | } else { |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1336 | jniThrowSocketException(env, -result); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1337 | } |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1338 | return; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1339 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1340 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1341 | } |
| 1342 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1343 | static void osNetworkSystem_bind(JNIEnv* env, jobject, jobject fileDescriptor, |
| 1344 | jobject inetAddress, jint port) { |
Elliott Hughes | f5eafc5 | 2009-11-05 10:00:02 -0800 | [diff] [blame] | 1345 | sockaddr_storage socketAddress; |
| 1346 | if (!inetAddressToSocketAddress(env, inetAddress, port, &socketAddress)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1347 | return; |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 1348 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1349 | |
Elliott Hughes | f5eafc5 | 2009-11-05 10:00:02 -0800 | [diff] [blame] | 1350 | int fd; |
| 1351 | if (!jniGetFd(env, fileDescriptor, fd)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1352 | return; |
| 1353 | } |
| 1354 | |
Elliott Hughes | 722a27e | 2010-03-05 20:37:25 -0800 | [diff] [blame] | 1355 | const CompatibleSocketAddress compatibleAddress(fd, socketAddress, false); |
| 1356 | int rc = TEMP_FAILURE_RETRY(bind(fd, compatibleAddress.get(), sizeof(sockaddr_storage))); |
Elliott Hughes | f5eafc5 | 2009-11-05 10:00:02 -0800 | [diff] [blame] | 1357 | if (rc == -1) { |
Elliott Hughes | ba563a4 | 2009-11-05 13:50:06 -0800 | [diff] [blame] | 1358 | jniThrowBindException(env, errno); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1359 | } |
| 1360 | } |
| 1361 | |
Elliott Hughes | 97144c8 | 2010-03-30 14:21:19 -0700 | [diff] [blame] | 1362 | static void osNetworkSystem_listen(JNIEnv* env, jobject, jobject fileDescriptor, jint backlog) { |
| 1363 | int fd; |
| 1364 | if (!jniGetFd(env, fileDescriptor, fd)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1365 | return; |
| 1366 | } |
| 1367 | |
Elliott Hughes | 97144c8 | 2010-03-30 14:21:19 -0700 | [diff] [blame] | 1368 | int rc = listen(fd, backlog); |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1369 | if (rc == -1) { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 1370 | jniThrowSocketException(env, errno); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1371 | } |
| 1372 | } |
| 1373 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1374 | static void osNetworkSystem_accept(JNIEnv* env, jobject, |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1375 | jobject serverFileDescriptor, |
| 1376 | jobject newSocket, jobject clientFileDescriptor, jint timeout) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1377 | // LOGD("ENTER acceptSocketImpl"); |
| 1378 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1379 | if (newSocket == NULL) { |
Elliott Hughes | da4f31d | 2010-01-28 13:43:39 -0800 | [diff] [blame] | 1380 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1381 | return; |
| 1382 | } |
| 1383 | |
Elliott Hughes | ba563a4 | 2009-11-05 13:50:06 -0800 | [diff] [blame] | 1384 | int rc = pollSelectWait(env, serverFileDescriptor, timeout); |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1385 | if (rc < 0) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1386 | return; |
| 1387 | } |
| 1388 | |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1389 | int serverFd; |
| 1390 | if (!jniGetFd(env, serverFileDescriptor, serverFd)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1391 | return; |
| 1392 | } |
| 1393 | |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1394 | sockaddr_storage sa; |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1395 | socklen_t addrlen = sizeof(sa); |
| 1396 | int clientFd = TEMP_FAILURE_RETRY(accept(serverFd, |
| 1397 | reinterpret_cast<sockaddr*>(&sa), &addrlen)); |
| 1398 | if (clientFd == -1) { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 1399 | jniThrowSocketException(env, errno); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1400 | return; |
| 1401 | } |
| 1402 | |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1403 | /* |
| 1404 | * For network sockets, put the peer address and port in instance variables. |
| 1405 | * We don't bother to do this for UNIX domain sockets, since most peers are |
| 1406 | * anonymous anyway. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1407 | */ |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1408 | if (sa.ss_family == AF_INET || sa.ss_family == AF_INET6) { |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1409 | jobject inetAddress = socketAddressToInetAddress(env, &sa); |
| 1410 | if (inetAddress == NULL) { |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1411 | close(clientFd); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1412 | return; |
| 1413 | } |
| 1414 | |
| 1415 | env->SetObjectField(newSocket, |
| 1416 | gCachedFields.socketimpl_address, inetAddress); |
| 1417 | |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1418 | int port = getSocketAddressPort(&sa); |
| 1419 | env->SetIntField(newSocket, gCachedFields.socketimpl_port, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1420 | } |
| 1421 | |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1422 | jniSetFileDescriptorOfFD(env, clientFileDescriptor, clientFd); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1423 | } |
| 1424 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1425 | static jboolean osNetworkSystem_supportsUrgentData(JNIEnv* env, |
| 1426 | jobject, jobject fileDescriptor) { |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1427 | // TODO(enh): do we really need to exclude the invalid file descriptor case? |
| 1428 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
| 1429 | return (fd == -1) ? JNI_FALSE : JNI_TRUE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1430 | } |
| 1431 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1432 | static void osNetworkSystem_sendUrgentData(JNIEnv* env, jobject, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1433 | jobject fileDescriptor, jbyte value) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1434 | int handle; |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1435 | if (!jniGetFd(env, fileDescriptor, handle)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1436 | return; |
| 1437 | } |
| 1438 | |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1439 | int rc = send(handle, &value, 1, MSG_OOB); |
| 1440 | if (rc == -1) { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 1441 | jniThrowSocketException(env, errno); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1442 | } |
| 1443 | } |
| 1444 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1445 | static void osNetworkSystem_connectDatagram(JNIEnv* env, jobject, |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1446 | jobject fileDescriptor, jint port, jint trafficClass, jobject inetAddress) { |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1447 | sockaddr_storage sockAddr; |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 1448 | if (!inetAddressToSocketAddress(env, inetAddress, port, &sockAddr)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1449 | return; |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 1450 | } |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1451 | |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1452 | int fd; |
| 1453 | if (!jniGetFd(env, fileDescriptor, fd)) { |
| 1454 | return; |
| 1455 | } |
| 1456 | |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 1457 | if (doConnect(fd, &sockAddr) == -1) { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 1458 | jniThrowSocketException(env, errno); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1459 | } |
| 1460 | } |
| 1461 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1462 | static void osNetworkSystem_disconnectDatagram(JNIEnv* env, jobject, |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1463 | jobject fileDescriptor) { |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1464 | int fd; |
| 1465 | if (!jniGetFd(env, fileDescriptor, fd)) { |
| 1466 | return; |
| 1467 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1468 | |
Elliott Hughes | 722a27e | 2010-03-05 20:37:25 -0800 | [diff] [blame] | 1469 | // To disconnect a datagram socket, we connect to a bogus address with |
| 1470 | // the family AF_UNSPEC. |
| 1471 | sockaddr_storage ss; |
| 1472 | memset(&ss, 0, sizeof(ss)); |
| 1473 | ss.ss_family = AF_UNSPEC; |
| 1474 | const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss); |
| 1475 | int rc = TEMP_FAILURE_RETRY(connect(fd, sa, sizeof(ss))); |
| 1476 | if (rc == -1) { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 1477 | jniThrowSocketException(env, errno); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1478 | } |
| 1479 | } |
| 1480 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1481 | static void osNetworkSystem_setInetAddress(JNIEnv* env, jobject, |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1482 | jobject sender, jbyteArray address) { |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1483 | env->SetObjectField(sender, gCachedFields.iaddr_ipaddress, address); |
| 1484 | } |
| 1485 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1486 | static jint osNetworkSystem_peekDatagram(JNIEnv* env, jobject, |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1487 | jobject fileDescriptor, jobject sender, jint receiveTimeout) { |
Elliott Hughes | ba563a4 | 2009-11-05 13:50:06 -0800 | [diff] [blame] | 1488 | int result = pollSelectWait(env, fileDescriptor, receiveTimeout); |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1489 | if (result < 0) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1490 | return 0; |
| 1491 | } |
| 1492 | |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1493 | int fd; |
| 1494 | if (!jniGetFd(env, fileDescriptor, fd)) { |
| 1495 | return 0; |
| 1496 | } |
| 1497 | |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1498 | sockaddr_storage sockAddr; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1499 | socklen_t sockAddrLen = sizeof(sockAddr); |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1500 | ssize_t length = TEMP_FAILURE_RETRY(recvfrom(fd, NULL, 0, MSG_PEEK, |
| 1501 | reinterpret_cast<sockaddr*>(&sockAddr), &sockAddrLen)); |
| 1502 | if (length == -1) { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 1503 | jniThrowSocketException(env, errno); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1504 | return 0; |
| 1505 | } |
| 1506 | |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1507 | // We update the byte[] in the 'sender' InetAddress, and return the port. |
| 1508 | // This awful API is public in the RI, so there's no point returning |
| 1509 | // InetSocketAddress here instead. |
| 1510 | jbyteArray senderAddressArray = socketAddressToByteArray(env, &sockAddr); |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 1511 | if (sender == NULL) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1512 | return -1; |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 1513 | } |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1514 | osNetworkSystem_setInetAddress(env, NULL, sender, senderAddressArray); |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1515 | return getSocketAddressPort(&sockAddr); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1516 | } |
| 1517 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1518 | static jint osNetworkSystem_receiveDatagramDirect(JNIEnv* env, jobject, |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1519 | jobject fileDescriptor, jobject packet, jint address, jint offset, |
| 1520 | jint length, jint receiveTimeout, jboolean peek) { |
Elliott Hughes | ba563a4 | 2009-11-05 13:50:06 -0800 | [diff] [blame] | 1521 | int result = pollSelectWait(env, fileDescriptor, receiveTimeout); |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1522 | if (result < 0) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1523 | return 0; |
| 1524 | } |
| 1525 | |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1526 | int fd; |
| 1527 | if (!jniGetFd(env, fileDescriptor, fd)) { |
| 1528 | return 0; |
| 1529 | } |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1530 | |
| 1531 | char* buf = |
| 1532 | reinterpret_cast<char*>(static_cast<uintptr_t>(address + offset)); |
| 1533 | const int mode = peek ? MSG_PEEK : 0; |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1534 | sockaddr_storage sockAddr; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1535 | socklen_t sockAddrLen = sizeof(sockAddr); |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1536 | ssize_t actualLength = TEMP_FAILURE_RETRY(recvfrom(fd, buf, length, mode, |
| 1537 | reinterpret_cast<sockaddr*>(&sockAddr), &sockAddrLen)); |
| 1538 | if (actualLength == -1) { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 1539 | jniThrowSocketException(env, errno); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1540 | return 0; |
| 1541 | } |
| 1542 | |
| 1543 | if (packet != NULL) { |
Lorenzo Colitti | e859690 | 2009-09-18 15:25:06 -0700 | [diff] [blame] | 1544 | jbyteArray addr = socketAddressToByteArray(env, &sockAddr); |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 1545 | if (addr == NULL) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1546 | return 0; |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 1547 | } |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1548 | int port = getSocketAddressPort(&sockAddr); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1549 | jobject sender = env->CallStaticObjectMethod( |
| 1550 | gCachedFields.iaddr_class, gCachedFields.iaddr_getbyaddress, |
| 1551 | addr); |
| 1552 | env->SetObjectField(packet, gCachedFields.dpack_address, sender); |
| 1553 | env->SetIntField(packet, gCachedFields.dpack_port, port); |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1554 | env->SetIntField(packet, gCachedFields.dpack_length, |
| 1555 | (jint) actualLength); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1556 | } |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1557 | return (jint) actualLength; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1558 | } |
| 1559 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1560 | static jint osNetworkSystem_receiveDatagram(JNIEnv* env, jobject, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1561 | jobject fd, jobject packet, jbyteArray data, jint offset, jint length, |
| 1562 | jint receiveTimeout, jboolean peek) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1563 | int localLength = (length < 65536) ? length : 65536; |
| 1564 | jbyte *bytes = (jbyte*) malloc(localLength); |
| 1565 | if (bytes == NULL) { |
| 1566 | jniThrowException(env, "java/lang/OutOfMemoryError", |
| 1567 | "couldn't allocate enough memory for receiveDatagram"); |
| 1568 | return 0; |
| 1569 | } |
| 1570 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1571 | int actualLength = osNetworkSystem_receiveDatagramDirect(env, NULL, fd, |
The Android Open Source Project | 5d287a9 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1572 | packet, (jint)bytes, 0, localLength, receiveTimeout, peek); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1573 | |
| 1574 | if (actualLength > 0) { |
| 1575 | env->SetByteArrayRegion(data, offset, actualLength, bytes); |
| 1576 | } |
| 1577 | free(bytes); |
| 1578 | |
| 1579 | return actualLength; |
| 1580 | } |
| 1581 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1582 | static jint osNetworkSystem_recvConnectedDatagramDirect(JNIEnv* env, |
| 1583 | jobject, jobject fileDescriptor, jobject packet, |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1584 | jint address, jint offset, jint length, |
| 1585 | jint receiveTimeout, jboolean peek) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1586 | |
Elliott Hughes | ba563a4 | 2009-11-05 13:50:06 -0800 | [diff] [blame] | 1587 | int result = pollSelectWait(env, fileDescriptor, receiveTimeout); |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1588 | if (result < 0) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1589 | return 0; |
| 1590 | } |
| 1591 | |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1592 | int fd; |
| 1593 | if (!jniGetFd(env, fileDescriptor, fd)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1594 | return 0; |
| 1595 | } |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1596 | |
| 1597 | char* buf = reinterpret_cast<char*>(static_cast<uintptr_t>(address + offset)); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1598 | int mode = peek ? MSG_PEEK : 0; |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1599 | int actualLength = recvfrom(fd, buf, length, mode, NULL, NULL); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1600 | if (actualLength < 0) { |
| 1601 | jniThrowException(env, "java/net/PortUnreachableException", ""); |
| 1602 | return 0; |
| 1603 | } |
| 1604 | |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1605 | if (packet != NULL) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1606 | env->SetIntField(packet, gCachedFields.dpack_length, actualLength); |
| 1607 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1608 | return actualLength; |
| 1609 | } |
| 1610 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1611 | static jint osNetworkSystem_recvConnectedDatagram(JNIEnv* env, jobject, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1612 | jobject fd, jobject packet, jbyteArray data, jint offset, jint length, |
| 1613 | jint receiveTimeout, jboolean peek) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1614 | int localLength = (length < 65536) ? length : 65536; |
| 1615 | jbyte *bytes = (jbyte*) malloc(localLength); |
| 1616 | if (bytes == NULL) { |
| 1617 | jniThrowException(env, "java/lang/OutOfMemoryError", |
| 1618 | "couldn't allocate enough memory for recvConnectedDatagram"); |
| 1619 | return 0; |
| 1620 | } |
| 1621 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1622 | int actualLength = osNetworkSystem_recvConnectedDatagramDirect(env, |
| 1623 | NULL, fd, packet, (jint)bytes, 0, localLength, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1624 | receiveTimeout, peek); |
| 1625 | |
| 1626 | if (actualLength > 0) { |
| 1627 | env->SetByteArrayRegion(data, offset, actualLength, bytes); |
| 1628 | } |
| 1629 | free(bytes); |
| 1630 | |
| 1631 | return actualLength; |
| 1632 | } |
| 1633 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1634 | static jint osNetworkSystem_sendDatagramDirect(JNIEnv* env, jobject, |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1635 | jobject fileDescriptor, jint address, jint offset, jint length, |
| 1636 | jint port, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1637 | jboolean bindToDevice, jint trafficClass, jobject inetAddress) { |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1638 | int fd; |
| 1639 | if (!jniGetFd(env, fileDescriptor, fd)) { |
| 1640 | return -1; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1641 | } |
| 1642 | |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1643 | sockaddr_storage receiver; |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 1644 | if (!inetAddressToSocketAddress(env, inetAddress, port, &receiver)) { |
| 1645 | return -1; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1646 | } |
| 1647 | |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1648 | char* buf = |
| 1649 | reinterpret_cast<char*>(static_cast<uintptr_t>(address + offset)); |
| 1650 | ssize_t bytesSent = TEMP_FAILURE_RETRY(sendto(fd, buf, length, |
| 1651 | SOCKET_NOFLAGS, |
| 1652 | reinterpret_cast<sockaddr*>(&receiver), sizeof(receiver))); |
| 1653 | if (bytesSent == -1) { |
| 1654 | if (errno == ECONNRESET || errno == ECONNREFUSED) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1655 | return 0; |
| 1656 | } else { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 1657 | jniThrowSocketException(env, errno); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1658 | } |
| 1659 | } |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1660 | return bytesSent; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1661 | } |
| 1662 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1663 | static jint osNetworkSystem_sendDatagram(JNIEnv* env, jobject, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1664 | jobject fd, jbyteArray data, jint offset, jint length, jint port, |
| 1665 | jboolean bindToDevice, jint trafficClass, jobject inetAddress) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1666 | jbyte *bytes = env->GetByteArrayElements(data, NULL); |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1667 | int actualLength = osNetworkSystem_sendDatagramDirect(env, NULL, fd, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1668 | (jint)bytes, offset, length, port, bindToDevice, trafficClass, |
| 1669 | inetAddress); |
| 1670 | env->ReleaseByteArrayElements(data, bytes, JNI_ABORT); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1671 | return actualLength; |
| 1672 | } |
| 1673 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1674 | static jint osNetworkSystem_sendConnectedDatagramDirect(JNIEnv* env, |
| 1675 | jobject, jobject fileDescriptor, |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1676 | jint address, jint offset, jint length, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1677 | jboolean bindToDevice) { |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1678 | int fd; |
| 1679 | if (!jniGetFd(env, fileDescriptor, fd)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1680 | return 0; |
| 1681 | } |
| 1682 | |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1683 | char* buf = |
| 1684 | reinterpret_cast<char*>(static_cast<uintptr_t>(address + offset)); |
| 1685 | ssize_t bytesSent = TEMP_FAILURE_RETRY(send(fd, buf, length, 0)); |
| 1686 | if (bytesSent == -1) { |
| 1687 | if (errno == ECONNRESET || errno == ECONNREFUSED) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1688 | return 0; |
| 1689 | } else { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 1690 | jniThrowSocketException(env, errno); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1691 | } |
| 1692 | } |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1693 | return bytesSent; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1694 | } |
| 1695 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1696 | static jint osNetworkSystem_sendConnectedDatagram(JNIEnv* env, jobject, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1697 | jobject fd, jbyteArray data, jint offset, jint length, |
| 1698 | jboolean bindToDevice) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1699 | jbyte *bytes = env->GetByteArrayElements(data, NULL); |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1700 | int actualLength = osNetworkSystem_sendConnectedDatagramDirect(env, |
| 1701 | NULL, fd, (jint)bytes, offset, length, bindToDevice); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1702 | env->ReleaseByteArrayElements(data, bytes, JNI_ABORT); |
| 1703 | |
| 1704 | return actualLength; |
| 1705 | } |
| 1706 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1707 | static void osNetworkSystem_createServerStreamSocket(JNIEnv* env, jobject, |
| 1708 | jobject fileDescriptor, jboolean) { |
| 1709 | int fd = createSocketFileDescriptor(env, fileDescriptor, SOCK_STREAM); |
| 1710 | if (fd != -1) { |
| 1711 | // TODO: we could actually do this in Java. (and check for errors!) |
| 1712 | int value = 1; |
| 1713 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(int)); |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 1714 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1715 | } |
| 1716 | |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1717 | static void doShutdown(JNIEnv* env, jobject fileDescriptor, int how) { |
| 1718 | int fd; |
| 1719 | if (!jniGetFd(env, fileDescriptor, fd)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1720 | return; |
| 1721 | } |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1722 | int rc = shutdown(fd, how); |
| 1723 | if (rc == -1) { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 1724 | jniThrowSocketException(env, errno); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1725 | } |
| 1726 | } |
| 1727 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1728 | static void osNetworkSystem_shutdownInput(JNIEnv* env, jobject, jobject fd) { |
| 1729 | doShutdown(env, fd, SHUT_RD); |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1730 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1731 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1732 | static void osNetworkSystem_shutdownOutput(JNIEnv* env, jobject, jobject fd) { |
| 1733 | doShutdown(env, fd, SHUT_WR); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1734 | } |
| 1735 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1736 | static jint osNetworkSystem_sendDatagram2(JNIEnv* env, jobject, |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1737 | jobject fileDescriptor, jbyteArray data, jint offset, jint length, |
| 1738 | jint port, jobject inetAddress) { |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1739 | sockaddr_storage sockAddr; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1740 | if (inetAddress != NULL) { |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1741 | if (!inetAddressToSocketAddress(env, inetAddress, port, &sockAddr)) { |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 1742 | return -1; |
| 1743 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1744 | } |
| 1745 | |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1746 | int fd; |
| 1747 | if (!jniGetFd(env, fileDescriptor, fd)) { |
| 1748 | return 0; |
| 1749 | } |
| 1750 | |
Elliott Hughes | fc354fe | 2009-10-16 17:25:34 -0700 | [diff] [blame] | 1751 | jbyte* message = (jbyte*) malloc(length * sizeof(jbyte)); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1752 | if (message == NULL) { |
| 1753 | jniThrowException(env, "java/lang/OutOfMemoryError", |
| 1754 | "couldn't allocate enough memory for readSocket"); |
| 1755 | return 0; |
| 1756 | } |
| 1757 | |
| 1758 | env->GetByteArrayRegion(data, offset, length, message); |
| 1759 | |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1760 | int totalBytesSent = 0; |
| 1761 | while (totalBytesSent < length) { |
| 1762 | ssize_t bytesSent = TEMP_FAILURE_RETRY(sendto(fd, |
| 1763 | message + totalBytesSent, length - totalBytesSent, |
| 1764 | SOCKET_NOFLAGS, |
| 1765 | reinterpret_cast<sockaddr*>(&sockAddr), sizeof(sockAddr))); |
| 1766 | if (bytesSent == -1) { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 1767 | jniThrowSocketException(env, errno); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1768 | free(message); |
| 1769 | return 0; |
| 1770 | } |
| 1771 | |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1772 | totalBytesSent += bytesSent; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | free(message); |
Elliott Hughes | b150dcd | 2009-10-20 22:58:23 -0700 | [diff] [blame] | 1776 | return totalBytesSent; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1777 | } |
| 1778 | |
Elliott Hughes | de816cc | 2010-03-31 18:34:53 -0700 | [diff] [blame] | 1779 | static bool isValidFd(int fd) { |
| 1780 | return fd >= 0 && fd < FD_SETSIZE; |
| 1781 | } |
| 1782 | |
Elliott Hughes | 42d8ce7 | 2009-09-17 18:32:07 -0700 | [diff] [blame] | 1783 | static bool initFdSet(JNIEnv* env, jobjectArray fdArray, jint count, fd_set* fdSet, int* maxFd) { |
| 1784 | for (int i = 0; i < count; ++i) { |
| 1785 | jobject fileDescriptor = env->GetObjectArrayElement(fdArray, i); |
| 1786 | if (fileDescriptor == NULL) { |
| 1787 | return false; |
| 1788 | } |
| 1789 | |
| 1790 | const int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
Elliott Hughes | de816cc | 2010-03-31 18:34:53 -0700 | [diff] [blame] | 1791 | if (!isValidFd(fd)) { |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1792 | LOGE("selectImpl: ignoring invalid fd %i", fd); |
Elliott Hughes | 42d8ce7 | 2009-09-17 18:32:07 -0700 | [diff] [blame] | 1793 | continue; |
| 1794 | } |
| 1795 | |
| 1796 | FD_SET(fd, fdSet); |
| 1797 | |
| 1798 | if (fd > *maxFd) { |
| 1799 | *maxFd = fd; |
| 1800 | } |
| 1801 | } |
| 1802 | return true; |
| 1803 | } |
| 1804 | |
Elliott Hughes | f5eafc5 | 2009-11-05 10:00:02 -0800 | [diff] [blame] | 1805 | /* |
| 1806 | * Note: fdSet has to be non-const because although on Linux FD_ISSET() is sane |
| 1807 | * and takes a const fd_set*, it takes fd_set* on Mac OS. POSIX is not on our |
| 1808 | * side here: |
| 1809 | * http://www.opengroup.org/onlinepubs/000095399/functions/select.html |
| 1810 | */ |
Dan Bornstein | fef8875 | 2009-11-04 15:34:14 -0800 | [diff] [blame] | 1811 | static bool translateFdSet(JNIEnv* env, jobjectArray fdArray, jint count, fd_set& fdSet, jint* flagArray, size_t offset, jint op) { |
Elliott Hughes | 42d8ce7 | 2009-09-17 18:32:07 -0700 | [diff] [blame] | 1812 | for (int i = 0; i < count; ++i) { |
| 1813 | jobject fileDescriptor = env->GetObjectArrayElement(fdArray, i); |
| 1814 | if (fileDescriptor == NULL) { |
| 1815 | return false; |
| 1816 | } |
| 1817 | |
| 1818 | const int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
Elliott Hughes | de816cc | 2010-03-31 18:34:53 -0700 | [diff] [blame] | 1819 | if (isValidFd(fd) && FD_ISSET(fd, &fdSet)) { |
Elliott Hughes | 42d8ce7 | 2009-09-17 18:32:07 -0700 | [diff] [blame] | 1820 | flagArray[i + offset] = op; |
| 1821 | } else { |
| 1822 | flagArray[i + offset] = SOCKET_OP_NONE; |
| 1823 | } |
| 1824 | } |
| 1825 | return true; |
| 1826 | } |
| 1827 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1828 | static jboolean osNetworkSystem_selectImpl(JNIEnv* env, jclass, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1829 | jobjectArray readFDArray, jobjectArray writeFDArray, jint countReadC, |
Elliott Hughes | 42d8ce7 | 2009-09-17 18:32:07 -0700 | [diff] [blame] | 1830 | jint countWriteC, jintArray outFlags, jlong timeoutMs) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1831 | // LOGD("ENTER selectImpl"); |
Elliott Hughes | 42d8ce7 | 2009-09-17 18:32:07 -0700 | [diff] [blame] | 1832 | |
| 1833 | // Initialize the fd_sets. |
| 1834 | int maxFd = -1; |
| 1835 | fd_set readFds; |
| 1836 | fd_set writeFds; |
| 1837 | FD_ZERO(&readFds); |
| 1838 | FD_ZERO(&writeFds); |
| 1839 | bool initialized = initFdSet(env, readFDArray, countReadC, &readFds, &maxFd) && |
| 1840 | initFdSet(env, writeFDArray, countWriteC, &writeFds, &maxFd); |
| 1841 | if (!initialized) { |
| 1842 | return -1; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1843 | } |
Elliott Hughes | 42d8ce7 | 2009-09-17 18:32:07 -0700 | [diff] [blame] | 1844 | |
| 1845 | // Initialize the timeout, if any. |
| 1846 | timeval tv; |
| 1847 | timeval* tvp = NULL; |
| 1848 | if (timeoutMs >= 0) { |
| 1849 | tv = toTimeval(timeoutMs); |
| 1850 | tvp = &tv; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1851 | } |
Elliott Hughes | 42d8ce7 | 2009-09-17 18:32:07 -0700 | [diff] [blame] | 1852 | |
| 1853 | // Perform the select. |
Elliott Hughes | ba563a4 | 2009-11-05 13:50:06 -0800 | [diff] [blame] | 1854 | int result = select(maxFd + 1, &readFds, &writeFds, NULL, tvp); |
| 1855 | if (result == 0) { |
| 1856 | // Timeout. |
| 1857 | return JNI_FALSE; |
| 1858 | } else if (result == -1) { |
| 1859 | // Error. |
| 1860 | if (errno == EINTR) { |
| 1861 | return JNI_FALSE; |
| 1862 | } else { |
| 1863 | jniThrowSocketException(env, errno); |
| 1864 | return JNI_FALSE; |
| 1865 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1866 | } |
Elliott Hughes | 42d8ce7 | 2009-09-17 18:32:07 -0700 | [diff] [blame] | 1867 | |
| 1868 | // Translate the result into the int[] we're supposed to fill in. |
| 1869 | jint* flagArray = env->GetIntArrayElements(outFlags, NULL); |
| 1870 | if (flagArray == NULL) { |
Elliott Hughes | ba563a4 | 2009-11-05 13:50:06 -0800 | [diff] [blame] | 1871 | return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1872 | } |
Elliott Hughes | 42d8ce7 | 2009-09-17 18:32:07 -0700 | [diff] [blame] | 1873 | bool okay = translateFdSet(env, readFDArray, countReadC, readFds, flagArray, 0, SOCKET_OP_READ) && |
| 1874 | translateFdSet(env, writeFDArray, countWriteC, writeFds, flagArray, countReadC, SOCKET_OP_WRITE); |
| 1875 | env->ReleaseIntArrayElements(outFlags, flagArray, 0); |
Elliott Hughes | ba563a4 | 2009-11-05 13:50:06 -0800 | [diff] [blame] | 1876 | return okay; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1877 | } |
| 1878 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1879 | static jobject osNetworkSystem_getSocketLocalAddress(JNIEnv* env, |
| 1880 | jobject, jobject fileDescriptor) { |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1881 | int fd; |
| 1882 | if (!jniGetFd(env, fileDescriptor, fd)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1883 | return NULL; |
| 1884 | } |
| 1885 | |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1886 | sockaddr_storage addr; |
| 1887 | socklen_t addrLen = sizeof(addr); |
| 1888 | memset(&addr, 0, addrLen); |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1889 | int rc = getsockname(fd, (sockaddr*) &addr, &addrLen); |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 1890 | if (rc == -1) { |
| 1891 | // TODO: the public API doesn't allow failure, so this whole method |
| 1892 | // represents a broken design. In practice, though, getsockname can't |
| 1893 | // fail unless we give it invalid arguments. |
| 1894 | LOGE("getsockname failed: %s (errno=%i)", strerror(errno), errno); |
| 1895 | return NULL; |
| 1896 | } |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 1897 | return socketAddressToInetAddress(env, &addr); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1898 | } |
| 1899 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 1900 | static jint osNetworkSystem_getSocketLocalPort(JNIEnv* env, jobject, |
Elliott Hughes | 4c5cbf2 | 2010-01-19 15:29:02 -0800 | [diff] [blame] | 1901 | jobject fileDescriptor) { |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1902 | int fd; |
| 1903 | if (!jniGetFd(env, fileDescriptor, fd)) { |
| 1904 | return 0; |
| 1905 | } |
| 1906 | |
| 1907 | sockaddr_storage addr; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1908 | socklen_t addrLen = sizeof(addr); |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 1909 | memset(&addr, 0, addrLen); |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 1910 | int rc = getsockname(fd, (sockaddr*) &addr, &addrLen); |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 1911 | if (rc == -1) { |
| 1912 | // TODO: the public API doesn't allow failure, so this whole method |
| 1913 | // represents a broken design. In practice, though, getsockname can't |
| 1914 | // fail unless we give it invalid arguments. |
| 1915 | LOGE("getsockname failed: %s (errno=%i)", strerror(errno), errno); |
| 1916 | return 0; |
| 1917 | } |
| 1918 | return getSocketAddressPort(&addr); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1919 | } |
| 1920 | |
Elliott Hughes | 4401c75 | 2010-05-04 09:47:02 -0700 | [diff] [blame] | 1921 | template <typename T> |
| 1922 | static bool getSocketOption(JNIEnv* env, int fd, int level, int option, T* value) { |
| 1923 | socklen_t size = sizeof(*value); |
| 1924 | int rc = getsockopt(fd, level, option, value, &size); |
| 1925 | if (rc == -1) { |
| 1926 | LOGE("getSocketOption(fd=%i, level=%i, option=%i) failed: %s (errno=%i)", |
| 1927 | fd, level, option, strerror(errno), errno); |
| 1928 | jniThrowSocketException(env, errno); |
| 1929 | return false; |
| 1930 | } |
| 1931 | return true; |
| 1932 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1933 | |
Elliott Hughes | 4401c75 | 2010-05-04 09:47:02 -0700 | [diff] [blame] | 1934 | static jobject getSocketOption_Boolean(JNIEnv* env, int fd, int level, int option) { |
| 1935 | int value; |
| 1936 | return getSocketOption(env, fd, level, option, &value) ? newJavaLangBoolean(env, value) : NULL; |
| 1937 | } |
| 1938 | |
| 1939 | static jobject getSocketOption_Integer(JNIEnv* env, int fd, int level, int option) { |
| 1940 | int value; |
| 1941 | return getSocketOption(env, fd, level, option, &value) ? newJavaLangInteger(env, value) : NULL; |
| 1942 | } |
| 1943 | |
| 1944 | static jobject osNetworkSystem_getSocketOption(JNIEnv* env, jobject, jobject fileDescriptor, jint option) { |
| 1945 | int fd; |
| 1946 | if (!jniGetFd(env, fileDescriptor, fd)) { |
| 1947 | return NULL; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1948 | } |
| 1949 | |
Elliott Hughes | 4401c75 | 2010-05-04 09:47:02 -0700 | [diff] [blame] | 1950 | int family = getSocketAddressFamily(fd); |
| 1951 | if (family != AF_INET && family != AF_INET6) { |
| 1952 | jniThrowSocketException(env, EAFNOSUPPORT); |
| 1953 | return NULL; |
| 1954 | } |
Dan Bornstein | fef8875 | 2009-11-04 15:34:14 -0800 | [diff] [blame] | 1955 | |
Elliott Hughes | 4401c75 | 2010-05-04 09:47:02 -0700 | [diff] [blame] | 1956 | switch (option) { |
| 1957 | case JAVASOCKOPT_TCP_NODELAY: |
| 1958 | return getSocketOption_Boolean(env, fd, IPPROTO_TCP, TCP_NODELAY); |
| 1959 | case JAVASOCKOPT_SO_SNDBUF: |
| 1960 | return getSocketOption_Integer(env, fd, SOL_SOCKET, SO_SNDBUF); |
| 1961 | case JAVASOCKOPT_SO_RCVBUF: |
| 1962 | return getSocketOption_Integer(env, fd, SOL_SOCKET, SO_RCVBUF); |
| 1963 | case JAVASOCKOPT_SO_BROADCAST: |
| 1964 | return getSocketOption_Boolean(env, fd, SOL_SOCKET, SO_BROADCAST); |
| 1965 | case JAVASOCKOPT_SO_REUSEADDR: |
| 1966 | return getSocketOption_Boolean(env, fd, SOL_SOCKET, SO_REUSEADDR); |
| 1967 | case JAVASOCKOPT_SO_KEEPALIVE: |
| 1968 | return getSocketOption_Boolean(env, fd, SOL_SOCKET, SO_KEEPALIVE); |
| 1969 | case JAVASOCKOPT_SO_OOBINLINE: |
| 1970 | return getSocketOption_Boolean(env, fd, SOL_SOCKET, SO_OOBINLINE); |
| 1971 | case JAVASOCKOPT_IP_TOS: |
| 1972 | if (family == AF_INET) { |
| 1973 | return getSocketOption_Boolean(env, fd, IPPROTO_IP, IP_TOS); |
| 1974 | } else { |
| 1975 | return getSocketOption_Boolean(env, fd, IPPROTO_IPV6, IPV6_TCLASS); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1976 | } |
Elliott Hughes | 4401c75 | 2010-05-04 09:47:02 -0700 | [diff] [blame] | 1977 | case JAVASOCKOPT_SO_LINGER: |
| 1978 | { |
| 1979 | linger lingr; |
| 1980 | bool ok = getSocketOption(env, fd, SOL_SOCKET, SO_LINGER, &lingr); |
| 1981 | return ok ? newJavaLangInteger(env, !lingr.l_onoff ? -1 : lingr.l_linger) : NULL; |
Dan Bornstein | fef8875 | 2009-11-04 15:34:14 -0800 | [diff] [blame] | 1982 | } |
Elliott Hughes | 4401c75 | 2010-05-04 09:47:02 -0700 | [diff] [blame] | 1983 | case JAVASOCKOPT_SO_RCVTIMEOUT: |
| 1984 | { |
| 1985 | timeval timeout; |
| 1986 | bool ok = getSocketOption(env, fd, SOL_SOCKET, SO_RCVTIMEO, &timeout); |
| 1987 | return ok ? newJavaLangInteger(env, toMs(timeout)) : NULL; |
Dan Bornstein | fef8875 | 2009-11-04 15:34:14 -0800 | [diff] [blame] | 1988 | } |
Dan Bornstein | fef8875 | 2009-11-04 15:34:14 -0800 | [diff] [blame] | 1989 | #ifdef ENABLE_MULTICAST |
Elliott Hughes | 4401c75 | 2010-05-04 09:47:02 -0700 | [diff] [blame] | 1990 | case JAVASOCKOPT_IP_MULTICAST_IF: |
| 1991 | { |
| 1992 | struct sockaddr_storage sockVal; |
| 1993 | if (!getSocketOption(env, fd, IPPROTO_IP, IP_MULTICAST_IF, &sockVal)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1994 | return NULL; |
| 1995 | } |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 1996 | if (sockVal.ss_family != AF_INET) { |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 1997 | LOGE("sockVal.ss_family != AF_INET (%i)", sockVal.ss_family); |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 1998 | // Java expects an AF_INET INADDR_ANY, but Linux just returns AF_UNSPEC. |
| 1999 | jbyteArray inAddrAny = env->NewByteArray(4); // { 0, 0, 0, 0 } |
| 2000 | return byteArrayToInetAddress(env, inAddrAny); |
| 2001 | } |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 2002 | return socketAddressToInetAddress(env, &sockVal); |
| 2003 | } |
Elliott Hughes | 4401c75 | 2010-05-04 09:47:02 -0700 | [diff] [blame] | 2004 | case JAVASOCKOPT_IP_MULTICAST_IF2: |
| 2005 | if (family == AF_INET) { |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 2006 | struct ip_mreqn multicastRequest; |
Elliott Hughes | 4401c75 | 2010-05-04 09:47:02 -0700 | [diff] [blame] | 2007 | bool ok = getSocketOption(env, fd, IPPROTO_IP, IP_MULTICAST_IF, &multicastRequest); |
| 2008 | return ok ? newJavaLangInteger(env, multicastRequest.imr_ifindex) : NULL; |
| 2009 | } else { |
| 2010 | return getSocketOption_Integer(env, fd, IPPROTO_IPV6, IPV6_MULTICAST_IF); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2011 | } |
Elliott Hughes | 4401c75 | 2010-05-04 09:47:02 -0700 | [diff] [blame] | 2012 | case JAVASOCKOPT_IP_MULTICAST_LOOP: |
| 2013 | if (family == AF_INET) { |
| 2014 | // Although IPv6 was cleaned up to use int, IPv4 multicast loopback uses a byte. |
| 2015 | u_char loopback; |
| 2016 | bool ok = getSocketOption(env, fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loopback); |
| 2017 | return ok ? newJavaLangBoolean(env, loopback) : NULL; |
| 2018 | } else { |
| 2019 | return getSocketOption_Boolean(env, fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP); |
| 2020 | } |
| 2021 | case JAVASOCKOPT_MULTICAST_TTL: |
| 2022 | if (family == AF_INET) { |
| 2023 | // Although IPv6 was cleaned up to use int, and IPv4 non-multicast TTL uses int, |
| 2024 | // IPv4 multicast TTL uses a byte. |
| 2025 | u_char ttl; |
| 2026 | bool ok = getSocketOption(env, fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl); |
| 2027 | return ok ? newJavaLangInteger(env, ttl) : NULL; |
| 2028 | } else { |
| 2029 | return getSocketOption_Integer(env, fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2030 | } |
Dan Bornstein | fef8875 | 2009-11-04 15:34:14 -0800 | [diff] [blame] | 2031 | #else |
Elliott Hughes | 4401c75 | 2010-05-04 09:47:02 -0700 | [diff] [blame] | 2032 | case JAVASOCKOPT_MULTICAST_TTL: |
| 2033 | case JAVASOCKOPT_IP_MULTICAST_IF: |
| 2034 | case JAVASOCKOPT_IP_MULTICAST_IF2: |
| 2035 | case JAVASOCKOPT_IP_MULTICAST_LOOP: |
| 2036 | jniThrowException(env, "java/lang/UnsupportedOperationException", NULL); |
| 2037 | return NULL; |
Dan Bornstein | fef8875 | 2009-11-04 15:34:14 -0800 | [diff] [blame] | 2038 | #endif // def ENABLE_MULTICAST |
Elliott Hughes | 4401c75 | 2010-05-04 09:47:02 -0700 | [diff] [blame] | 2039 | default: |
| 2040 | jniThrowSocketException(env, ENOPROTOOPT); |
| 2041 | return NULL; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2042 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2043 | } |
| 2044 | |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 2045 | template <typename T> |
| 2046 | static void setSocketOption(JNIEnv* env, int fd, int level, int option, T* value) { |
| 2047 | int rc = setsockopt(fd, level, option, value, sizeof(*value)); |
| 2048 | if (rc == -1) { |
| 2049 | LOGE("setSocketOption(fd=%i, level=%i, option=%i) failed: %s (errno=%i)", |
Elliott Hughes | 4401c75 | 2010-05-04 09:47:02 -0700 | [diff] [blame] | 2050 | fd, level, option, strerror(errno), errno); |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 2051 | jniThrowSocketException(env, errno); |
| 2052 | } |
| 2053 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2054 | |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 2055 | static void osNetworkSystem_setSocketOption(JNIEnv* env, jobject, jobject fileDescriptor, jint option, jobject optVal) { |
| 2056 | int fd; |
| 2057 | if (!jniGetFd(env, fileDescriptor, fd)) { |
| 2058 | return; |
| 2059 | } |
| 2060 | |
| 2061 | int intVal; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2062 | if (env->IsInstanceOf(optVal, gCachedFields.integer_class)) { |
| 2063 | intVal = (int) env->GetIntField(optVal, gCachedFields.integer_class_value); |
| 2064 | } else if (env->IsInstanceOf(optVal, gCachedFields.boolean_class)) { |
| 2065 | intVal = (int) env->GetBooleanField(optVal, gCachedFields.boolean_class_value); |
| 2066 | } else if (env->IsInstanceOf(optVal, gCachedFields.byte_class)) { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 2067 | intVal = (int) env->GetByteField(optVal, gCachedFields.byte_class_value); |
Elliott Hughes | 4401c75 | 2010-05-04 09:47:02 -0700 | [diff] [blame] | 2068 | } else if (env->IsInstanceOf(optVal, gCachedFields.genericipmreq_class) || env->IsInstanceOf(optVal, gCachedFields.iaddr_class)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2069 | // we'll use optVal directly |
| 2070 | } else { |
Elliott Hughes | 4401c75 | 2010-05-04 09:47:02 -0700 | [diff] [blame] | 2071 | jniThrowSocketException(env, EINVAL); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2072 | return; |
| 2073 | } |
| 2074 | |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 2075 | int family = getSocketAddressFamily(fd); |
| 2076 | if (family != AF_INET && family != AF_INET6) { |
| 2077 | jniThrowSocketException(env, EAFNOSUPPORT); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2078 | return; |
| 2079 | } |
| 2080 | |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 2081 | switch (option) { |
| 2082 | case JAVASOCKOPT_SO_LINGER: |
| 2083 | { |
Elliott Hughes | 4401c75 | 2010-05-04 09:47:02 -0700 | [diff] [blame] | 2084 | linger lingr; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2085 | lingr.l_onoff = intVal > 0 ? 1 : 0; |
| 2086 | lingr.l_linger = intVal; |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 2087 | setSocketOption(env, fd, SOL_SOCKET, SO_LINGER, &lingr); |
| 2088 | return; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2089 | } |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 2090 | case JAVASOCKOPT_SO_SNDBUF: |
| 2091 | setSocketOption(env, fd, SOL_SOCKET, SO_SNDBUF, &intVal); |
| 2092 | return; |
| 2093 | case JAVASOCKOPT_SO_RCVBUF: |
| 2094 | setSocketOption(env, fd, SOL_SOCKET, SO_RCVBUF, &intVal); |
| 2095 | return; |
| 2096 | case JAVASOCKOPT_SO_BROADCAST: |
| 2097 | setSocketOption(env, fd, SOL_SOCKET, SO_BROADCAST, &intVal); |
| 2098 | return; |
| 2099 | case JAVASOCKOPT_SO_REUSEADDR: |
| 2100 | setSocketOption(env, fd, SOL_SOCKET, SO_REUSEADDR, &intVal); |
| 2101 | return; |
| 2102 | case JAVASOCKOPT_SO_KEEPALIVE: |
| 2103 | setSocketOption(env, fd, SOL_SOCKET, SO_KEEPALIVE, &intVal); |
| 2104 | return; |
| 2105 | case JAVASOCKOPT_SO_OOBINLINE: |
| 2106 | setSocketOption(env, fd, SOL_SOCKET, SO_OOBINLINE, &intVal); |
| 2107 | return; |
| 2108 | case JAVASOCKOPT_REUSEADDR_AND_REUSEPORT: |
| 2109 | // SO_REUSEPORT doesn't need to get set on this System |
| 2110 | setSocketOption(env, fd, SOL_SOCKET, SO_REUSEADDR, &intVal); |
| 2111 | return; |
| 2112 | case JAVASOCKOPT_SO_RCVTIMEOUT: |
| 2113 | { |
Dan Bornstein | fef8875 | 2009-11-04 15:34:14 -0800 | [diff] [blame] | 2114 | timeval timeout(toTimeval(intVal)); |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 2115 | setSocketOption(env, fd, SOL_SOCKET, SO_RCVTIMEO, &timeout); |
| 2116 | return; |
Dan Bornstein | fef8875 | 2009-11-04 15:34:14 -0800 | [diff] [blame] | 2117 | } |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 2118 | case JAVASOCKOPT_IP_TOS: |
| 2119 | if (family == AF_INET) { |
| 2120 | setSocketOption(env, fd, IPPROTO_IP, IP_TOS, &intVal); |
| 2121 | } else { |
| 2122 | setSocketOption(env, fd, IPPROTO_IPV6, IPV6_TCLASS, &intVal); |
| 2123 | } |
| 2124 | return; |
| 2125 | case JAVASOCKOPT_TCP_NODELAY: |
| 2126 | setSocketOption(env, fd, IPPROTO_TCP, TCP_NODELAY, &intVal); |
| 2127 | return; |
Dan Bornstein | fef8875 | 2009-11-04 15:34:14 -0800 | [diff] [blame] | 2128 | #ifdef ENABLE_MULTICAST |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 2129 | case JAVASOCKOPT_MCAST_ADD_MEMBERSHIP: |
| 2130 | mcastAddDropMembership(env, fd, optVal, IP_ADD_MEMBERSHIP); |
| 2131 | return; |
| 2132 | case JAVASOCKOPT_MCAST_DROP_MEMBERSHIP: |
| 2133 | mcastAddDropMembership(env, fd, optVal, IP_DROP_MEMBERSHIP); |
| 2134 | return; |
| 2135 | case JAVASOCKOPT_IP_MULTICAST_IF: |
| 2136 | { |
| 2137 | struct sockaddr_storage sockVal; |
| 2138 | if (!env->IsInstanceOf(optVal, gCachedFields.iaddr_class) || |
| 2139 | !inetAddressToSocketAddress(env, optVal, 0, &sockVal)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2140 | return; |
| 2141 | } |
Lorenzo Colitti | beeef8a | 2009-09-30 17:51:26 -0700 | [diff] [blame] | 2142 | // This call is IPv4 only. The socket may be IPv6, but the address |
| 2143 | // that identifies the interface to join must be an IPv4 address. |
| 2144 | if (sockVal.ss_family != AF_INET) { |
Elliott Hughes | a9a57f2 | 2009-10-30 17:54:32 -0700 | [diff] [blame] | 2145 | jniThrowSocketException(env, EAFNOSUPPORT); |
Lorenzo Colitti | 65663e1 | 2009-05-26 15:51:50 -0700 | [diff] [blame] | 2146 | return; |
| 2147 | } |
Mike Wakerly | 7076abb | 2009-03-24 18:37:27 -0700 | [diff] [blame] | 2148 | struct ip_mreqn mcast_req; |
| 2149 | memset(&mcast_req, 0, sizeof(mcast_req)); |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 2150 | mcast_req.imr_address = reinterpret_cast<sockaddr_in*>(&sockVal)->sin_addr; |
| 2151 | setSocketOption(env, fd, IPPROTO_IP, IP_MULTICAST_IF, &mcast_req); |
Dan Bornstein | fef8875 | 2009-11-04 15:34:14 -0800 | [diff] [blame] | 2152 | return; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2153 | } |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 2154 | case JAVASOCKOPT_IP_MULTICAST_IF2: |
| 2155 | if (family == AF_INET) { |
| 2156 | // IP_MULTICAST_IF expects a pointer to a struct ip_mreqn. |
| 2157 | struct ip_mreqn multicastRequest; |
| 2158 | memset(&multicastRequest, 0, sizeof(multicastRequest)); |
| 2159 | multicastRequest.imr_ifindex = intVal; |
| 2160 | setSocketOption(env, fd, IPPROTO_IP, IP_MULTICAST_IF, &multicastRequest); |
| 2161 | } else { |
| 2162 | // IPV6_MULTICAST_IF expects a pointer to an integer. |
| 2163 | setSocketOption(env, fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &intVal); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2164 | } |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 2165 | return; |
Elliott Hughes | 4401c75 | 2010-05-04 09:47:02 -0700 | [diff] [blame] | 2166 | case JAVASOCKOPT_MULTICAST_TTL: |
| 2167 | if (family == AF_INET) { |
| 2168 | // Although IPv6 was cleaned up to use int, and IPv4 non-multicast TTL uses int, |
| 2169 | // IPv4 multicast TTL uses a byte. |
| 2170 | u_char ttl = intVal; |
| 2171 | setSocketOption(env, fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl); |
| 2172 | } else { |
| 2173 | setSocketOption(env, fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &intVal); |
| 2174 | } |
| 2175 | return; |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 2176 | case JAVASOCKOPT_IP_MULTICAST_LOOP: |
| 2177 | if (family == AF_INET) { |
Elliott Hughes | 4401c75 | 2010-05-04 09:47:02 -0700 | [diff] [blame] | 2178 | // Although IPv6 was cleaned up to use int, IPv4 multicast loopback uses a byte. |
| 2179 | u_char loopback = intVal; |
| 2180 | setSocketOption(env, fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loopback); |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 2181 | } else { |
| 2182 | setSocketOption(env, fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &intVal); |
| 2183 | } |
| 2184 | return; |
| 2185 | #else |
Elliott Hughes | 4401c75 | 2010-05-04 09:47:02 -0700 | [diff] [blame] | 2186 | case JAVASOCKOPT_MULTICAST_TTL: |
Elliott Hughes | 4d8e933 | 2010-05-03 14:31:03 -0700 | [diff] [blame] | 2187 | case JAVASOCKOPT_MCAST_ADD_MEMBERSHIP: |
| 2188 | case JAVASOCKOPT_MCAST_DROP_MEMBERSHIP: |
| 2189 | case JAVASOCKOPT_IP_MULTICAST_IF: |
| 2190 | case JAVASOCKOPT_IP_MULTICAST_IF2: |
| 2191 | case JAVASOCKOPT_IP_MULTICAST_LOOP: |
| 2192 | jniThrowException(env, "java/lang/UnsupportedOperationException", NULL); |
| 2193 | return; |
| 2194 | #endif // def ENABLE_MULTICAST |
| 2195 | default: |
| 2196 | jniThrowSocketException(env, ENOPROTOOPT); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2197 | } |
| 2198 | } |
| 2199 | |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 2200 | static void osNetworkSystem_socketClose(JNIEnv* env, jobject, jobject fileDescriptor) { |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 2201 | int fd; |
| 2202 | if (!jniGetFd(env, fileDescriptor, fd)) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2203 | return; |
| 2204 | } |
| 2205 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2206 | jniSetFileDescriptorOfFD(env, fileDescriptor, -1); |
| 2207 | |
Elliott Hughes | 24912e0 | 2009-10-19 16:36:13 -0700 | [diff] [blame] | 2208 | close(fd); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2209 | } |
| 2210 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2211 | static JNINativeMethod gMethods[] = { |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 2212 | { "accept", "(Ljava/io/FileDescriptor;Ljava/net/SocketImpl;Ljava/io/FileDescriptor;I)V",(void*) osNetworkSystem_accept }, |
| 2213 | { "bind", "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)V", (void*) osNetworkSystem_bind }, |
| 2214 | { "byteArrayToIpString", "([B)Ljava/lang/String;", (void*) osNetworkSystem_byteArrayToIpString }, |
| 2215 | { "connectDatagram", "(Ljava/io/FileDescriptor;IILjava/net/InetAddress;)V", (void*) osNetworkSystem_connectDatagram }, |
| 2216 | { "connectStreamWithTimeoutSocket", "(Ljava/io/FileDescriptor;IIILjava/net/InetAddress;)V", (void*) osNetworkSystem_connectStreamWithTimeoutSocket }, |
Elliott Hughes | b375c0c | 2010-05-04 18:06:21 -0700 | [diff] [blame] | 2217 | { "connectWithTimeout", "(Ljava/io/FileDescriptor;IILjava/net/InetAddress;II[B)Z", (void*) osNetworkSystem_connectWithTimeout }, |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 2218 | { "createDatagramSocket", "(Ljava/io/FileDescriptor;Z)V", (void*) osNetworkSystem_createDatagramSocket }, |
| 2219 | { "createServerStreamSocket", "(Ljava/io/FileDescriptor;Z)V", (void*) osNetworkSystem_createServerStreamSocket }, |
| 2220 | { "createStreamSocket", "(Ljava/io/FileDescriptor;Z)V", (void*) osNetworkSystem_createStreamSocket }, |
| 2221 | { "disconnectDatagram", "(Ljava/io/FileDescriptor;)V", (void*) osNetworkSystem_disconnectDatagram }, |
| 2222 | { "getSocketLocalAddress", "(Ljava/io/FileDescriptor;)Ljava/net/InetAddress;", (void*) osNetworkSystem_getSocketLocalAddress }, |
| 2223 | { "getSocketLocalPort", "(Ljava/io/FileDescriptor;)I", (void*) osNetworkSystem_getSocketLocalPort }, |
| 2224 | { "getSocketOption", "(Ljava/io/FileDescriptor;I)Ljava/lang/Object;", (void*) osNetworkSystem_getSocketOption }, |
| 2225 | { "ipStringToByteArray", "(Ljava/lang/String;)[B", (void*) osNetworkSystem_ipStringToByteArray }, |
Elliott Hughes | 97144c8 | 2010-03-30 14:21:19 -0700 | [diff] [blame] | 2226 | { "listen", "(Ljava/io/FileDescriptor;I)V", (void*) osNetworkSystem_listen }, |
Elliott Hughes | d9d2630 | 2010-03-15 16:40:21 -0700 | [diff] [blame] | 2227 | { "peekDatagram", "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)I", (void*) osNetworkSystem_peekDatagram }, |
| 2228 | { "readDirect", "(Ljava/io/FileDescriptor;III)I", (void*) osNetworkSystem_readDirect }, |
| 2229 | { "readSocketImpl", "(Ljava/io/FileDescriptor;[BIII)I", (void*) osNetworkSystem_readSocketImpl }, |
| 2230 | { "receiveDatagramDirect", "(Ljava/io/FileDescriptor;Ljava/net/DatagramPacket;IIIIZ)I", (void*) osNetworkSystem_receiveDatagramDirect }, |
| 2231 | { "receiveDatagram", "(Ljava/io/FileDescriptor;Ljava/net/DatagramPacket;[BIIIZ)I", (void*) osNetworkSystem_receiveDatagram }, |
| 2232 | { "recvConnectedDatagramDirect", "(Ljava/io/FileDescriptor;Ljava/net/DatagramPacket;IIIIZ)I", (void*) osNetworkSystem_recvConnectedDatagramDirect }, |
| 2233 | { "recvConnectedDatagram", "(Ljava/io/FileDescriptor;Ljava/net/DatagramPacket;[BIIIZ)I", (void*) osNetworkSystem_recvConnectedDatagram }, |
| 2234 | { "selectImpl", "([Ljava/io/FileDescriptor;[Ljava/io/FileDescriptor;II[IJ)Z", (void*) osNetworkSystem_selectImpl }, |
| 2235 | { "sendConnectedDatagramDirect", "(Ljava/io/FileDescriptor;IIIZ)I", (void*) osNetworkSystem_sendConnectedDatagramDirect }, |
| 2236 | { "sendConnectedDatagram", "(Ljava/io/FileDescriptor;[BIIZ)I", (void*) osNetworkSystem_sendConnectedDatagram }, |
| 2237 | { "sendDatagramDirect", "(Ljava/io/FileDescriptor;IIIIZILjava/net/InetAddress;)I", (void*) osNetworkSystem_sendDatagramDirect }, |
| 2238 | { "sendDatagram", "(Ljava/io/FileDescriptor;[BIIIZILjava/net/InetAddress;)I", (void*) osNetworkSystem_sendDatagram }, |
| 2239 | { "sendDatagram2", "(Ljava/io/FileDescriptor;[BIIILjava/net/InetAddress;)I", (void*) osNetworkSystem_sendDatagram2 }, |
| 2240 | { "sendUrgentData", "(Ljava/io/FileDescriptor;B)V", (void*) osNetworkSystem_sendUrgentData }, |
| 2241 | { "setInetAddress", "(Ljava/net/InetAddress;[B)V", (void*) osNetworkSystem_setInetAddress }, |
| 2242 | { "setNonBlocking", "(Ljava/io/FileDescriptor;Z)V", (void*) osNetworkSystem_setNonBlocking }, |
| 2243 | { "setSocketOption", "(Ljava/io/FileDescriptor;ILjava/lang/Object;)V", (void*) osNetworkSystem_setSocketOption }, |
| 2244 | { "shutdownInput", "(Ljava/io/FileDescriptor;)V", (void*) osNetworkSystem_shutdownInput }, |
| 2245 | { "shutdownOutput", "(Ljava/io/FileDescriptor;)V", (void*) osNetworkSystem_shutdownOutput }, |
| 2246 | { "socketClose", "(Ljava/io/FileDescriptor;)V", (void*) osNetworkSystem_socketClose }, |
| 2247 | { "supportsUrgentData", "(Ljava/io/FileDescriptor;)Z", (void*) osNetworkSystem_supportsUrgentData }, |
| 2248 | { "writeDirect", "(Ljava/io/FileDescriptor;III)I", (void*) osNetworkSystem_writeDirect }, |
| 2249 | { "write", "(Ljava/io/FileDescriptor;[BII)I", (void*) osNetworkSystem_write }, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2250 | }; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2251 | int register_org_apache_harmony_luni_platform_OSNetworkSystem(JNIEnv* env) { |
Elliott Hughes | 7861ea9 | 2009-11-12 12:23:05 -0800 | [diff] [blame] | 2252 | return initCachedFields(env) && jniRegisterNativeMethods(env, |
Elliott Hughes | c08f9fb | 2010-04-16 17:44:12 -0700 | [diff] [blame] | 2253 | "org/apache/harmony/luni/platform/OSNetworkSystem", gMethods, NELEM(gMethods)); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2254 | } |
Lorenzo Colitti | e859690 | 2009-09-18 15:25:06 -0700 | [diff] [blame] | 2255 | // END android-changed |