1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #define LOG_TAG "NetworkUtilities" 18 19 #include "NetworkUtilities.h" 20 #include "JNIHelp.h" 21 #include "JniConstants.h" 22 #include "ScopedLocalRef.h" 23 24 #include <arpa/inet.h> 25 #include <fcntl.h> 26 #include <stdio.h> 27 #include <string.h> 28 #include <sys/socket.h> 29 30 jobject sockaddrToInetAddress(JNIEnv* env, const sockaddr_storage* ss, jint* port) { 31 // Convert IPv4-mapped IPv6 addresses to IPv4 addresses. 32 // The RI states "Java will never return an IPv4-mapped address". 33 sockaddr_storage tmp; 34 memset(&tmp, 0, sizeof(tmp)); 35 const sockaddr_in6* sin6 = reinterpret_cast<const sockaddr_in6*>(ss); 36 if (ss->ss_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 37 // Copy the IPv6 address into the temporary sockaddr_storage. 38 memcpy(&tmp, ss, sizeof(tmp)); 39 // Unmap it into an IPv4 address. 40 sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(&tmp); 41 sin->sin_family = AF_INET; 42 sin->sin_port = sin6->sin6_port; 43 memcpy(&sin->sin_addr.s_addr, &sin6->sin6_addr.s6_addr[12], 4); 44 // Fall through into the regular conversion using the unmapped address. 45 ss = &tmp; 46 } 47 48 const void* rawAddress; 49 size_t addressLength; 50 int sin_port; 51 int scope_id = 0; 52 if (ss->ss_family == AF_INET) { 53 const sockaddr_in* sin = reinterpret_cast<const sockaddr_in*>(ss); 54 rawAddress = &sin->sin_addr.s_addr; 55 addressLength = 4; 56 sin_port = ntohs(sin->sin_port); 57 } else if (ss->ss_family == AF_INET6) { 58 const sockaddr_in6* sin6 = reinterpret_cast<const sockaddr_in6*>(ss); 59 rawAddress = &sin6->sin6_addr.s6_addr; 60 addressLength = 16; 61 sin_port = ntohs(sin6->sin6_port); 62 scope_id = sin6->sin6_scope_id; 63 } else { 64 // We can't throw SocketException. We aren't meant to see bad addresses, so seeing one 65 // really does imply an internal error. 66 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", 67 "sockaddrToInetAddress bad ss_family: %i", ss->ss_family); 68 return NULL; 69 } 70 if (port != NULL) { 71 *port = sin_port; 72 } 73 74 ScopedLocalRef<jbyteArray> byteArray(env, env->NewByteArray(addressLength)); 75 if (byteArray.get() == NULL) { 76 return NULL; 77 } 78 env->SetByteArrayRegion(byteArray.get(), 0, addressLength, 79 reinterpret_cast<const jbyte*>(rawAddress)); 80 81 static jmethodID getByAddressMethod = env->GetStaticMethodID(JniConstants::inetAddressClass, 82 "getByAddress", "(Ljava/lang/String;[BI)Ljava/net/InetAddress;"); 83 if (getByAddressMethod == NULL) { 84 return NULL; 85 } 86 return env->CallStaticObjectMethod(JniConstants::inetAddressClass, getByAddressMethod, 87 NULL, byteArray.get(), scope_id); 88 } 89 90 static bool inetAddressToSockaddr(JNIEnv* env, jobject inetAddress, int port, sockaddr_storage* ss, bool map) { 91 memset(ss, 0, sizeof(*ss)); 92 93 if (inetAddress == NULL) { 94 jniThrowNullPointerException(env, NULL); 95 return false; 96 } 97 98 // Get the address family. 99 static jfieldID familyFid = env->GetFieldID(JniConstants::inetAddressClass, "family", "I"); 100 ss->ss_family = env->GetIntField(inetAddress, familyFid); 101 if (ss->ss_family == AF_UNSPEC) { 102 return true; // Job done! 103 } 104 105 // Check this is an address family we support. 106 if (ss->ss_family != AF_INET && ss->ss_family != AF_INET6) { 107 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", 108 "inetAddressToSockaddr bad family: %i", ss->ss_family); 109 return false; 110 } 111 112 // Get the byte array that stores the IP address bytes in the InetAddress. 113 static jfieldID bytesFid = env->GetFieldID(JniConstants::inetAddressClass, "ipaddress", "[B"); 114 ScopedLocalRef<jbyteArray> addressBytes(env, reinterpret_cast<jbyteArray>(env->GetObjectField(inetAddress, bytesFid))); 115 if (addressBytes.get() == NULL) { 116 jniThrowNullPointerException(env, NULL); 117 return false; 118 } 119 120 // We use AF_INET6 sockets, so we want an IPv6 address (which may be a IPv4-mapped address). 121 sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(ss); 122 sin6->sin6_port = htons(port); 123 if (ss->ss_family == AF_INET6) { 124 // IPv6 address. Copy the bytes... 125 jbyte* dst = reinterpret_cast<jbyte*>(&sin6->sin6_addr.s6_addr); 126 env->GetByteArrayRegion(addressBytes.get(), 0, 16, dst); 127 // ...and set the scope id... 128 static jfieldID scopeFid = env->GetFieldID(JniConstants::inet6AddressClass, "scope_id", "I"); 129 sin6->sin6_scope_id = env->GetIntField(inetAddress, scopeFid); 130 return true; 131 } 132 133 // Deal with Inet4Address instances. 134 if (map) { 135 // We should represent this Inet4Address as an IPv4-mapped IPv6 sockaddr_in6. 136 // Change the family... 137 sin6->sin6_family = AF_INET6; 138 // Copy the bytes... 139 jbyte* dst = reinterpret_cast<jbyte*>(&sin6->sin6_addr.s6_addr[12]); 140 env->GetByteArrayRegion(addressBytes.get(), 0, 4, dst); 141 // INADDR_ANY and in6addr_any are both all-zeros... 142 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 143 // ...but all other IPv4-mapped addresses are ::ffff:a.b.c.d, so insert the ffff... 144 memset(&(sin6->sin6_addr.s6_addr[10]), 0xff, 2); 145 } 146 } else { 147 // We should represent this Inet4Address as an IPv4 sockaddr_in. 148 sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ss); 149 sin->sin_port = htons(port); 150 jbyte* dst = reinterpret_cast<jbyte*>(&sin->sin_addr.s_addr); 151 env->GetByteArrayRegion(addressBytes.get(), 0, 4, dst); 152 } 153 return true; 154 } 155 156 bool inetAddressToSockaddrVerbatim(JNIEnv* env, jobject inetAddress, int port, sockaddr_storage* ss) { 157 return inetAddressToSockaddr(env, inetAddress, port, ss, false); 158 } 159 160 bool inetAddressToSockaddr(JNIEnv* env, jobject inetAddress, int port, sockaddr_storage* ss) { 161 return inetAddressToSockaddr(env, inetAddress, port, ss, true); 162 } 163 164 bool setBlocking(int fd, bool blocking) { 165 int flags = fcntl(fd, F_GETFL); 166 if (flags == -1) { 167 return false; 168 } 169 170 if (!blocking) { 171 flags |= O_NONBLOCK; 172 } else { 173 flags &= ~O_NONBLOCK; 174 } 175 176 int rc = fcntl(fd, F_SETFL, flags); 177 return (rc != -1); 178 } 179