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 #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 ScopedLocalRef<jobject> holder(env, env->GetObjectField(inetAddress, holderFid));
103 // Get the address family.
104 static jfieldID familyFid = env->GetFieldID(JniConstants::inetAddressHolderClass, "family", "I");
105 ss.ss_family = env->GetIntField(holder.get(), familyFid);
106 if (ss.ss_family == AF_UNSPEC) {
107 sa_len = sizeof(ss.ss_family);
108 return true; // Job done!
109 }
110
111 // Check this is an address family we support.
112 if (ss.ss_family != AF_INET && ss.ss_family != AF_INET6) {
113 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
114 "inetAddressToSockaddr bad family: %i", ss.ss_family);
115 return false;
116 }
117
118 // Get the byte array that stores the IP address bytes in the InetAddress.
119 static jmethodID bytesMid = env->GetMethodID(JniConstants::inetAddressClass, "getAddress", "()[B");
120 ScopedLocalRef<jbyteArray> addressBytes(env, reinterpret_cast<jbyteArray>(env->CallObjectMethod(inetAddress, bytesMid)));
121 if (env->ExceptionCheck()) {
122 return false;
123 }
124 if (addressBytes.get() == NULL) {
125 jniThrowNullPointerException(env, NULL);
126 return false;
127 }
128
129 // 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
135 // We use AF_INET6 sockets, so we want an IPv6 address (which may be a IPv4-mapped address).
136 sockaddr_in6& sin6 = reinterpret_cast<sockaddr_in6&>(ss);
137 sin6.sin6_port = htons(port);
138 if (ss.ss_family == AF_INET6) {
139 // IPv6 address. Copy the bytes...
140 jbyte* dst = reinterpret_cast<jbyte*>(&sin6.sin6_addr.s6_addr);
141 env->GetByteArrayRegion(addressBytes.get(), 0, 16, dst);
142 // ...and set the scope id...
143 static jfieldID holder6Fid = env->GetFieldID(JniConstants::inet6AddressClass,
144 "holder6",
145 "Ljava/net/Inet6Address$Inet6AddressHolder;");
146 ScopedLocalRef<jobject> holder6(env, env->GetObjectField(inetAddress, holder6Fid));
147 static jfieldID scopeFid = env->GetFieldID(JniConstants::inet6AddressHolderClass, "scope_id", "I");
148 sin6.sin6_scope_id = env->GetIntField(holder6.get(), scopeFid);
149 sa_len = sizeof(sockaddr_in6);
150 return true;
151 }
152
153 // Deal with Inet4Address instances.
154 if (map) {
155 // We should represent this Inet4Address as an IPv4-mapped IPv6 sockaddr_in6.
156 // Change the family...
157 sin6.sin6_family = AF_INET6;
158 // Copy the bytes...
159 jbyte* dst = reinterpret_cast<jbyte*>(&sin6.sin6_addr.s6_addr[12]);
160 env->GetByteArrayRegion(addressBytes.get(), 0, 4, dst);
161 // INADDR_ANY and in6addr_any are both all-zeros...
162 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr)) {
163 // ...but all other IPv4-mapped addresses are ::ffff:a.b.c.d, so insert the ffff...
164 memset(&(sin6.sin6_addr.s6_addr[10]), 0xff, 2);
165 }
166 sa_len = sizeof(sockaddr_in6);
167 } else {
168 // We should represent this Inet4Address as an IPv4 sockaddr_in.
169 sockaddr_in& sin = reinterpret_cast<sockaddr_in&>(ss);
170 sin.sin_port = htons(port);
171 jbyte* dst = reinterpret_cast<jbyte*>(&sin.sin_addr.s_addr);
172 env->GetByteArrayRegion(addressBytes.get(), 0, 4, dst);
173 sa_len = sizeof(sockaddr_in);
174 }
175 return true;
176 }
177
inetAddressToSockaddrVerbatim(JNIEnv * env,jobject inetAddress,int port,sockaddr_storage & ss,socklen_t & sa_len)178 bool inetAddressToSockaddrVerbatim(JNIEnv* env, jobject inetAddress, int port, sockaddr_storage& ss, socklen_t& sa_len) {
179 return inetAddressToSockaddr(env, inetAddress, port, ss, sa_len, false);
180 }
181
inetAddressToSockaddr(JNIEnv * env,jobject inetAddress,int port,sockaddr_storage & ss,socklen_t & sa_len)182 bool inetAddressToSockaddr(JNIEnv* env, jobject inetAddress, int port, sockaddr_storage& ss, socklen_t& sa_len) {
183 return inetAddressToSockaddr(env, inetAddress, port, ss, sa_len, true);
184 }
185
setBlocking(int fd,bool blocking)186 bool setBlocking(int fd, bool blocking) {
187 int flags = fcntl(fd, F_GETFL);
188 if (flags == -1) {
189 return false;
190 }
191
192 if (!blocking) {
193 flags |= O_NONBLOCK;
194 } else {
195 flags &= ~O_NONBLOCK;
196 }
197
198 int rc = fcntl(fd, F_SETFL, flags);
199 return (rc != -1);
200 }
201