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 <nativehelper/JNIHelp.h>
21 #include <nativehelper/JniConstants.h>
22 #include <nativehelper/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 #include <sys/un.h>
30
sockaddrToInetAddress(JNIEnv * env,const sockaddr_storage & ss,jint * port)31 jobject sockaddrToInetAddress(JNIEnv* env, const sockaddr_storage& ss, jint* port) {
32 // Convert IPv4-mapped IPv6 addresses to IPv4 addresses.
33 // The RI states "Java will never return an IPv4-mapped address".
34 const sockaddr_in6& sin6 = reinterpret_cast<const sockaddr_in6&>(ss);
35 if (ss.ss_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
36 // Copy the IPv6 address into the temporary sockaddr_storage.
37 sockaddr_storage tmp;
38 memset(&tmp, 0, sizeof(tmp));
39 memcpy(&tmp, &ss, sizeof(sockaddr_in6));
40 // Unmap it into an IPv4 address.
41 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);
47 }
48
49 const void* rawAddress;
50 size_t addressLength;
51 int sin_port = 0;
52 int scope_id = 0;
53 if (ss.ss_family == AF_INET) {
54 const sockaddr_in& sin = reinterpret_cast<const sockaddr_in&>(ss);
55 rawAddress = &sin.sin_addr.s_addr;
56 addressLength = 4;
57 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;
61 addressLength = 16;
62 sin_port = ntohs(sin6.sin6_port);
63 scope_id = sin6.sin6_scope_id;
64 } else {
65 // We can't throw SocketException. We aren't meant to see bad addresses, so seeing one
66 // really does imply an internal error.
67 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
68 "sockaddrToInetAddress unsupported ss_family: %i", ss.ss_family);
69 return NULL;
70 }
71 if (port != NULL) {
72 *port = sin_port;
73 }
74
75 ScopedLocalRef<jbyteArray> byteArray(env, env->NewByteArray(addressLength));
76 if (byteArray.get() == NULL) {
77 return NULL;
78 }
79 env->SetByteArrayRegion(byteArray.get(), 0, addressLength,
80 reinterpret_cast<const jbyte*>(rawAddress));
81
82 static jmethodID getByAddressMethod = env->GetStaticMethodID(JniConstants::inetAddressClass,
83 "getByAddress", "(Ljava/lang/String;[BI)Ljava/net/InetAddress;");
84 if (getByAddressMethod == NULL) {
85 return NULL;
86 }
87 return env->CallStaticObjectMethod(JniConstants::inetAddressClass, getByAddressMethod,
88 NULL, byteArray.get(), scope_id);
89 }
90
inetAddressToSockaddr(JNIEnv * env,jobject inetAddress,int port,sockaddr_storage & ss,socklen_t & sa_len,bool map)91 static 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;
94
95 if (inetAddress == NULL) {
96 jniThrowNullPointerException(env, NULL);
97 return false;
98 }
99
100 // Get holder.
101 static jfieldID holderFid = env->GetFieldID(JniConstants::inetAddressClass, "holder", "Ljava/net/InetAddress$InetAddressHolder;");
102 if (holderFid == NULL) {
103 return false;
104 }
105 ScopedLocalRef<jobject> holder(env, env->GetObjectField(inetAddress, holderFid));
106 // Get the address family.
107 static jfieldID familyFid = env->GetFieldID(JniConstants::inetAddressHolderClass, "family", "I");
108 if (familyFid == NULL) {
109 return false;
110 }
111 ss.ss_family = env->GetIntField(holder.get(), familyFid);
112 if (ss.ss_family == AF_UNSPEC) {
113 sa_len = sizeof(ss.ss_family);
114 return true; // Job done!
115 }
116
117 // Check this is an address family we support.
118 if (ss.ss_family != AF_INET && ss.ss_family != AF_INET6) {
119 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
120 "inetAddressToSockaddr bad family: %i", ss.ss_family);
121 return false;
122 }
123
124 // Get the byte array that stores the IP address bytes in the InetAddress.
125 static jmethodID bytesMid = env->GetMethodID(JniConstants::inetAddressClass, "getAddress", "()[B");
126 if (bytesMid == NULL) {
127 return false;
128 }
129 ScopedLocalRef<jbyteArray> addressBytes(env, reinterpret_cast<jbyteArray>(env->CallObjectMethod(inetAddress, bytesMid)));
130 if (env->ExceptionCheck()) {
131 return false;
132 }
133 if (addressBytes.get() == NULL) {
134 jniThrowNullPointerException(env, NULL);
135 return false;
136 }
137
138 // TODO: bionic's getnameinfo(3) seems to want its length parameter to be exactly
139 // sizeof(sockaddr_in) for an IPv4 address and sizeof (sockaddr_in6) for an
140 // IPv6 address. Fix getnameinfo so it accepts sizeof(sockaddr_storage), and
141 // then unconditionally set sa_len to sizeof(sockaddr_storage) instead of having
142 // to deal with this case by case.
143
144 // We use AF_INET6 sockets, so we want an IPv6 address (which may be a IPv4-mapped address).
145 sockaddr_in6& sin6 = reinterpret_cast<sockaddr_in6&>(ss);
146 sin6.sin6_port = htons(port);
147 if (ss.ss_family == AF_INET6) {
148 // IPv6 address. Copy the bytes...
149 jbyte* dst = reinterpret_cast<jbyte*>(&sin6.sin6_addr.s6_addr);
150 env->GetByteArrayRegion(addressBytes.get(), 0, 16, dst);
151 // ...and set the scope id...
152 static jfieldID holder6Fid = env->GetFieldID(JniConstants::inet6AddressClass,
153 "holder6",
154 "Ljava/net/Inet6Address$Inet6AddressHolder;");
155 if (holder6Fid == NULL) {
156 return false;
157 }
158 ScopedLocalRef<jobject> holder6(env, env->GetObjectField(inetAddress, holder6Fid));
159 static jfieldID scopeFid = env->GetFieldID(JniConstants::inet6AddressHolderClass, "scope_id", "I");
160 sin6.sin6_scope_id = env->GetIntField(holder6.get(), scopeFid);
161 sa_len = sizeof(sockaddr_in6);
162 return true;
163 }
164
165 // Deal with Inet4Address instances.
166 if (map) {
167 // We should represent this Inet4Address as an IPv4-mapped IPv6 sockaddr_in6.
168 // Change the family...
169 sin6.sin6_family = AF_INET6;
170 // Copy the bytes...
171 jbyte* dst = reinterpret_cast<jbyte*>(&sin6.sin6_addr.s6_addr[12]);
172 env->GetByteArrayRegion(addressBytes.get(), 0, 4, dst);
173 // INADDR_ANY and in6addr_any are both all-zeros...
174 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr)) {
175 // ...but all other IPv4-mapped addresses are ::ffff:a.b.c.d, so insert the ffff...
176 memset(&(sin6.sin6_addr.s6_addr[10]), 0xff, 2);
177 }
178 sa_len = sizeof(sockaddr_in6);
179 } else {
180 // We should represent this Inet4Address as an IPv4 sockaddr_in.
181 sockaddr_in& sin = reinterpret_cast<sockaddr_in&>(ss);
182 sin.sin_port = htons(port);
183 jbyte* dst = reinterpret_cast<jbyte*>(&sin.sin_addr.s_addr);
184 env->GetByteArrayRegion(addressBytes.get(), 0, 4, dst);
185 sa_len = sizeof(sockaddr_in);
186 }
187 return true;
188 }
189
inetAddressToSockaddrVerbatim(JNIEnv * env,jobject inetAddress,int port,sockaddr_storage & ss,socklen_t & sa_len)190 bool inetAddressToSockaddrVerbatim(JNIEnv* env, jobject inetAddress, int port, sockaddr_storage& ss, socklen_t& sa_len) {
191 return inetAddressToSockaddr(env, inetAddress, port, ss, sa_len, false);
192 }
193
inetAddressToSockaddr(JNIEnv * env,jobject inetAddress,int port,sockaddr_storage & ss,socklen_t & sa_len)194 bool inetAddressToSockaddr(JNIEnv* env, jobject inetAddress, int port, sockaddr_storage& ss, socklen_t& sa_len) {
195 return inetAddressToSockaddr(env, inetAddress, port, ss, sa_len, true);
196 }
197
setBlocking(int fd,bool blocking)198 bool setBlocking(int fd, bool blocking) {
199 int flags = fcntl(fd, F_GETFL);
200 if (flags == -1) {
201 return false;
202 }
203
204 if (!blocking) {
205 flags |= O_NONBLOCK;
206 } else {
207 flags &= ~O_NONBLOCK;
208 }
209
210 int rc = fcntl(fd, F_SETFL, flags);
211 return (rc != -1);
212 }
213