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