blob: b285a01331106572846b90bbdfa37d1679cfbfda [file] [log] [blame]
Elliott Hughes753dcd82010-06-01 18:07:56 -07001/*
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
Elliott Hughesabf945f2010-06-03 19:55:20 -070017#define LOG_TAG "NetworkUtilities"
18
Elliott Hughes753dcd82010-06-01 18:07:56 -070019#include "NetworkUtilities.h"
20#include "JNIHelp.h"
Elliott Hughesa9f5c162010-06-16 16:32:18 -070021#include "JniConstants.h"
Elliott Hughese9f12042011-05-11 11:48:46 -070022#include "ScopedLocalRef.h"
Elliott Hughes753dcd82010-06-01 18:07:56 -070023
24#include <arpa/inet.h>
Elliott Hughes3b0a5b92010-07-22 15:06:54 -070025#include <fcntl.h>
Elliott Hughes753dcd82010-06-01 18:07:56 -070026#include <stdio.h>
27#include <string.h>
Elliott Hughes4f11ebe2011-04-19 15:38:10 -070028#include <sys/socket.h>
Elliott Hughes482a3fc2013-03-20 16:10:12 -070029#include <sys/un.h>
Elliott Hughes753dcd82010-06-01 18:07:56 -070030
Elliott Hughes482a3fc2013-03-20 16:10:12 -070031jobject sockaddrToInetAddress(JNIEnv* env, const sockaddr_storage& ss, jint* port) {
Elliott Hughes4664da52011-05-06 17:09:43 -070032 // Convert IPv4-mapped IPv6 addresses to IPv4 addresses.
Elliott Hughes1c039d72011-05-04 16:04:04 -070033 // The RI states "Java will never return an IPv4-mapped address".
Elliott Hughes482a3fc2013-03-20 16:10:12 -070034 const sockaddr_in6& sin6 = reinterpret_cast<const sockaddr_in6&>(ss);
35 if (ss.ss_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
Elliott Hughes1c039d72011-05-04 16:04:04 -070036 // Copy the IPv6 address into the temporary sockaddr_storage.
Elliott Hughes482a3fc2013-03-20 16:10:12 -070037 sockaddr_storage tmp;
38 memset(&tmp, 0, sizeof(tmp));
39 memcpy(&tmp, &ss, sizeof(sockaddr_in6));
Elliott Hughes1c039d72011-05-04 16:04:04 -070040 // Unmap it into an IPv4 address.
Elliott Hughes482a3fc2013-03-20 16:10:12 -070041 sockaddr_in& sin = reinterpret_cast<sockaddr_in&>(tmp);
42 sin.sin_family = AF_INET;
43 sin.sin_port = sin6.sin6_port;
44 memcpy(&sin.sin_addr.s_addr, &sin6.sin6_addr.s6_addr[12], 4);
45 // Do the regular conversion using the unmapped address.
46 return sockaddrToInetAddress(env, tmp, port);
Elliott Hughes1c039d72011-05-04 16:04:04 -070047 }
48
Elliott Hughes0a9d1ee2011-03-30 15:35:16 -070049 const void* rawAddress;
Elliott Hughes753dcd82010-06-01 18:07:56 -070050 size_t addressLength;
Elliott Hughes482a3fc2013-03-20 16:10:12 -070051 int sin_port = 0;
Elliott Hughes4728a012011-05-19 11:30:51 -070052 int scope_id = 0;
Elliott Hughes482a3fc2013-03-20 16:10:12 -070053 if (ss.ss_family == AF_INET) {
54 const sockaddr_in& sin = reinterpret_cast<const sockaddr_in&>(ss);
55 rawAddress = &sin.sin_addr.s_addr;
Elliott Hughes753dcd82010-06-01 18:07:56 -070056 addressLength = 4;
Elliott Hughes482a3fc2013-03-20 16:10:12 -070057 sin_port = ntohs(sin.sin_port);
58 } else if (ss.ss_family == AF_INET6) {
59 const sockaddr_in6& sin6 = reinterpret_cast<const sockaddr_in6&>(ss);
60 rawAddress = &sin6.sin6_addr.s6_addr;
Elliott Hughes753dcd82010-06-01 18:07:56 -070061 addressLength = 16;
Elliott Hughes482a3fc2013-03-20 16:10:12 -070062 sin_port = ntohs(sin6.sin6_port);
63 scope_id = sin6.sin6_scope_id;
Elliott Hughes753dcd82010-06-01 18:07:56 -070064 } else {
Elliott Hughes91ad12a2010-07-26 14:13:55 -070065 // We can't throw SocketException. We aren't meant to see bad addresses, so seeing one
66 // really does imply an internal error.
Elliott Hughes51236bf2011-04-08 17:21:47 -070067 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
Elliott Hughes482a3fc2013-03-20 16:10:12 -070068 "sockaddrToInetAddress unsupported ss_family: %i", ss.ss_family);
Elliott Hughes753dcd82010-06-01 18:07:56 -070069 return NULL;
70 }
Elliott Hughes23ec0912011-05-18 18:21:17 -070071 if (port != NULL) {
Elliott Hughes4728a012011-05-19 11:30:51 -070072 *port = sin_port;
Elliott Hughes23ec0912011-05-18 18:21:17 -070073 }
Elliott Hughes753dcd82010-06-01 18:07:56 -070074
Elliott Hughesd8cec782011-06-20 18:03:37 -070075 ScopedLocalRef<jbyteArray> byteArray(env, env->NewByteArray(addressLength));
76 if (byteArray.get() == NULL) {
Elliott Hughes753dcd82010-06-01 18:07:56 -070077 return NULL;
78 }
Elliott Hughesd8cec782011-06-20 18:03:37 -070079 env->SetByteArrayRegion(byteArray.get(), 0, addressLength,
80 reinterpret_cast<const jbyte*>(rawAddress));
Elliott Hughes753dcd82010-06-01 18:07:56 -070081
Elliott Hughes49ff8b82011-05-06 14:23:01 -070082 static jmethodID getByAddressMethod = env->GetStaticMethodID(JniConstants::inetAddressClass,
Elliott Hughes4728a012011-05-19 11:30:51 -070083 "getByAddress", "(Ljava/lang/String;[BI)Ljava/net/InetAddress;");
Elliott Hughes753dcd82010-06-01 18:07:56 -070084 if (getByAddressMethod == NULL) {
85 return NULL;
86 }
Elliott Hughes4728a012011-05-19 11:30:51 -070087 return env->CallStaticObjectMethod(JniConstants::inetAddressClass, getByAddressMethod,
Elliott Hughesd8cec782011-06-20 18:03:37 -070088 NULL, byteArray.get(), scope_id);
Elliott Hughes753dcd82010-06-01 18:07:56 -070089}
90
Elliott Hughes482a3fc2013-03-20 16:10:12 -070091static bool inetAddressToSockaddr(JNIEnv* env, jobject inetAddress, int port, sockaddr_storage& ss, socklen_t& sa_len, bool map) {
92 memset(&ss, 0, sizeof(ss));
93 sa_len = 0;
Elliott Hughesc06e5e22011-05-04 19:00:47 -070094
Elliott Hughes83982392011-03-31 16:45:18 -070095 if (inetAddress == NULL) {
96 jniThrowNullPointerException(env, NULL);
97 return false;
98 }
Elliott Hughes32b0fa42011-05-10 16:07:38 -070099
Piotr Jastrzebskif7ab2bc2015-05-06 14:00:00 +0100100 // Get holder.
101 static jfieldID holderFid = env->GetFieldID(JniConstants::inetAddressClass, "holder", "Ljava/net/InetAddress$InetAddressHolder;");
102 ScopedLocalRef<jobject> holder(env, env->GetObjectField(inetAddress, holderFid));
Elliott Hughes32b0fa42011-05-10 16:07:38 -0700103 // Get the address family.
Piotr Jastrzebskif7ab2bc2015-05-06 14:00:00 +0100104 static jfieldID familyFid = env->GetFieldID(JniConstants::inetAddressHolderClass, "family", "I");
105 ss.ss_family = env->GetIntField(holder.get(), familyFid);
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700106 if (ss.ss_family == AF_UNSPEC) {
Elliott Hughesd52f91a2013-03-21 23:55:03 -0700107 sa_len = sizeof(ss.ss_family);
Elliott Hughes32b0fa42011-05-10 16:07:38 -0700108 return true; // Job done!
109 }
110
111 // Check this is an address family we support.
Neil Fuller0ab1a262015-07-07 17:03:25 +0100112 if (ss.ss_family != AF_INET && ss.ss_family != AF_INET6) {
Elliott Hughes32b0fa42011-05-10 16:07:38 -0700113 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700114 "inetAddressToSockaddr bad family: %i", ss.ss_family);
Elliott Hughes32b0fa42011-05-10 16:07:38 -0700115 return false;
116 }
117
118 // Get the byte array that stores the IP address bytes in the InetAddress.
Przemyslaw Szczepaniak68b18922016-02-08 14:40:37 +0000119 static jmethodID bytesMid = env->GetMethodID(JniConstants::inetAddressClass, "getAddress", "()[B");
Piotr Jastrzebskif7ab2bc2015-05-06 14:00:00 +0100120 ScopedLocalRef<jbyteArray> addressBytes(env, reinterpret_cast<jbyteArray>(env->CallObjectMethod(inetAddress, bytesMid)));
Przemyslaw Szczepaniak68b18922016-02-08 14:40:37 +0000121 if (env->ExceptionCheck()) {
122 return false;
123 }
Elliott Hughese9f12042011-05-11 11:48:46 -0700124 if (addressBytes.get() == NULL) {
Elliott Hughesc06e5e22011-05-04 19:00:47 -0700125 jniThrowNullPointerException(env, NULL);
126 return false;
127 }
128
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700129 // TODO: bionic's getnameinfo(3) seems to want its length parameter to be exactly
130 // sizeof(sockaddr_in) for an IPv4 address and sizeof (sockaddr_in6) for an
131 // IPv6 address. Fix getnameinfo so it accepts sizeof(sockaddr_storage), and
132 // then unconditionally set sa_len to sizeof(sockaddr_storage) instead of having
133 // to deal with this case by case.
134
Elliott Hughes4664da52011-05-06 17:09:43 -0700135 // We use AF_INET6 sockets, so we want an IPv6 address (which may be a IPv4-mapped address).
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700136 sockaddr_in6& sin6 = reinterpret_cast<sockaddr_in6&>(ss);
137 sin6.sin6_port = htons(port);
138 if (ss.ss_family == AF_INET6) {
Elliott Hughes4664da52011-05-06 17:09:43 -0700139 // IPv6 address. Copy the bytes...
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700140 jbyte* dst = reinterpret_cast<jbyte*>(&sin6.sin6_addr.s6_addr);
Elliott Hughese9f12042011-05-11 11:48:46 -0700141 env->GetByteArrayRegion(addressBytes.get(), 0, 16, dst);
Elliott Hughes4664da52011-05-06 17:09:43 -0700142 // ...and set the scope id...
Elliott Hughes32b0fa42011-05-10 16:07:38 -0700143 static jfieldID scopeFid = env->GetFieldID(JniConstants::inet6AddressClass, "scope_id", "I");
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700144 sin6.sin6_scope_id = env->GetIntField(inetAddress, scopeFid);
145 sa_len = sizeof(sockaddr_in6);
Elliott Hughesc06e5e22011-05-04 19:00:47 -0700146 return true;
147 }
148
Elliott Hughes32b0fa42011-05-10 16:07:38 -0700149 // Deal with Inet4Address instances.
150 if (map) {
151 // We should represent this Inet4Address as an IPv4-mapped IPv6 sockaddr_in6.
152 // Change the family...
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700153 sin6.sin6_family = AF_INET6;
Elliott Hughes32b0fa42011-05-10 16:07:38 -0700154 // Copy the bytes...
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700155 jbyte* dst = reinterpret_cast<jbyte*>(&sin6.sin6_addr.s6_addr[12]);
Elliott Hughese9f12042011-05-11 11:48:46 -0700156 env->GetByteArrayRegion(addressBytes.get(), 0, 4, dst);
Elliott Hughes32b0fa42011-05-10 16:07:38 -0700157 // INADDR_ANY and in6addr_any are both all-zeros...
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700158 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr)) {
Elliott Hughes32b0fa42011-05-10 16:07:38 -0700159 // ...but all other IPv4-mapped addresses are ::ffff:a.b.c.d, so insert the ffff...
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700160 memset(&(sin6.sin6_addr.s6_addr[10]), 0xff, 2);
Elliott Hughes32b0fa42011-05-10 16:07:38 -0700161 }
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700162 sa_len = sizeof(sockaddr_in6);
Elliott Hughes32b0fa42011-05-10 16:07:38 -0700163 } else {
164 // We should represent this Inet4Address as an IPv4 sockaddr_in.
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700165 sockaddr_in& sin = reinterpret_cast<sockaddr_in&>(ss);
166 sin.sin_port = htons(port);
167 jbyte* dst = reinterpret_cast<jbyte*>(&sin.sin_addr.s_addr);
Elliott Hughese9f12042011-05-11 11:48:46 -0700168 env->GetByteArrayRegion(addressBytes.get(), 0, 4, dst);
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700169 sa_len = sizeof(sockaddr_in);
Elliott Hughes32b0fa42011-05-10 16:07:38 -0700170 }
171 return true;
Elliott Hughes83982392011-03-31 16:45:18 -0700172}
173
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700174bool inetAddressToSockaddrVerbatim(JNIEnv* env, jobject inetAddress, int port, sockaddr_storage& ss, socklen_t& sa_len) {
175 return inetAddressToSockaddr(env, inetAddress, port, ss, sa_len, false);
Elliott Hughes4664da52011-05-06 17:09:43 -0700176}
177
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700178bool inetAddressToSockaddr(JNIEnv* env, jobject inetAddress, int port, sockaddr_storage& ss, socklen_t& sa_len) {
179 return inetAddressToSockaddr(env, inetAddress, port, ss, sa_len, true);
Elliott Hughes4664da52011-05-06 17:09:43 -0700180}
181
Elliott Hughes440ba562010-08-04 10:44:16 -0700182bool setBlocking(int fd, bool blocking) {
Elliott Hughes3b0a5b92010-07-22 15:06:54 -0700183 int flags = fcntl(fd, F_GETFL);
184 if (flags == -1) {
185 return false;
186 }
187
Elliott Hughes440ba562010-08-04 10:44:16 -0700188 if (!blocking) {
Elliott Hughes3b0a5b92010-07-22 15:06:54 -0700189 flags |= O_NONBLOCK;
190 } else {
191 flags &= ~O_NONBLOCK;
192 }
193
194 int rc = fcntl(fd, F_SETFL, flags);
195 return (rc != -1);
196}