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 if (ss.ss_family == AF_UNIX) {
65         const sockaddr_un& sun = reinterpret_cast<const sockaddr_un&>(ss);
66         rawAddress = &sun.sun_path;
67         addressLength = strlen(sun.sun_path);
68     } else {
69         // We can't throw SocketException. We aren't meant to see bad addresses, so seeing one
70         // really does imply an internal error.
71         jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
72                              "sockaddrToInetAddress unsupported ss_family: %i", ss.ss_family);
73         return NULL;
74     }
75     if (port != NULL) {
76         *port = sin_port;
77     }
78 
79     ScopedLocalRef<jbyteArray> byteArray(env, env->NewByteArray(addressLength));
80     if (byteArray.get() == NULL) {
81         return NULL;
82     }
83     env->SetByteArrayRegion(byteArray.get(), 0, addressLength,
84             reinterpret_cast<const jbyte*>(rawAddress));
85 
86     if (ss.ss_family == AF_UNIX) {
87         // Note that we get here for AF_UNIX sockets on accept(2). The unix(7) man page claims
88         // that the peer's sun_path will contain the path, but in practice it doesn't, and the
89         // peer length is returned as 2 (meaning only the sun_family field was set).
90         static jmethodID ctor = env->GetMethodID(JniConstants::inetUnixAddressClass, "<init>", "([B)V");
91         return env->NewObject(JniConstants::inetUnixAddressClass, ctor, byteArray.get());
92     }
93 
94     static jmethodID getByAddressMethod = env->GetStaticMethodID(JniConstants::inetAddressClass,
95             "getByAddress", "(Ljava/lang/String;[BI)Ljava/net/InetAddress;");
96     if (getByAddressMethod == NULL) {
97         return NULL;
98     }
99     return env->CallStaticObjectMethod(JniConstants::inetAddressClass, getByAddressMethod,
100             NULL, byteArray.get(), scope_id);
101 }
102 
inetAddressToSockaddr(JNIEnv * env,jobject inetAddress,int port,sockaddr_storage & ss,socklen_t & sa_len,bool map)103 static bool inetAddressToSockaddr(JNIEnv* env, jobject inetAddress, int port, sockaddr_storage& ss, socklen_t& sa_len, bool map) {
104     memset(&ss, 0, sizeof(ss));
105     sa_len = 0;
106 
107     if (inetAddress == NULL) {
108         jniThrowNullPointerException(env, NULL);
109         return false;
110     }
111 
112     // Get the address family.
113     static jfieldID familyFid = env->GetFieldID(JniConstants::inetAddressClass, "family", "I");
114     ss.ss_family = env->GetIntField(inetAddress, familyFid);
115     if (ss.ss_family == AF_UNSPEC) {
116         sa_len = sizeof(ss.ss_family);
117         return true; // Job done!
118     }
119 
120     // Check this is an address family we support.
121     if (ss.ss_family != AF_INET && ss.ss_family != AF_INET6 && ss.ss_family != AF_UNIX) {
122         jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
123                 "inetAddressToSockaddr bad family: %i", ss.ss_family);
124         return false;
125     }
126 
127     // Get the byte array that stores the IP address bytes in the InetAddress.
128     static jfieldID bytesFid = env->GetFieldID(JniConstants::inetAddressClass, "ipaddress", "[B");
129     ScopedLocalRef<jbyteArray> addressBytes(env, reinterpret_cast<jbyteArray>(env->GetObjectField(inetAddress, bytesFid)));
130     if (addressBytes.get() == NULL) {
131         jniThrowNullPointerException(env, NULL);
132         return false;
133     }
134 
135     // Handle the AF_UNIX special case.
136     if (ss.ss_family == AF_UNIX) {
137         sockaddr_un& sun = reinterpret_cast<sockaddr_un&>(ss);
138 
139         size_t path_length = env->GetArrayLength(addressBytes.get());
140         if (path_length >= sizeof(sun.sun_path)) {
141             jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
142                                  "inetAddressToSockaddr path too long for AF_UNIX: %i", path_length);
143             return false;
144         }
145 
146         // Copy the bytes...
147         jbyte* dst = reinterpret_cast<jbyte*>(&sun.sun_path);
148         memset(dst, 0, sizeof(sun.sun_path));
149         env->GetByteArrayRegion(addressBytes.get(), 0, path_length, dst);
150         sa_len = sizeof(sun.sun_path);
151         return true;
152     }
153 
154     // TODO: bionic's getnameinfo(3) seems to want its length parameter to be exactly
155     // sizeof(sockaddr_in) for an IPv4 address and sizeof (sockaddr_in6) for an
156     // IPv6 address. Fix getnameinfo so it accepts sizeof(sockaddr_storage), and
157     // then unconditionally set sa_len to sizeof(sockaddr_storage) instead of having
158     // to deal with this case by case.
159 
160     // We use AF_INET6 sockets, so we want an IPv6 address (which may be a IPv4-mapped address).
161     sockaddr_in6& sin6 = reinterpret_cast<sockaddr_in6&>(ss);
162     sin6.sin6_port = htons(port);
163     if (ss.ss_family == AF_INET6) {
164         // IPv6 address. Copy the bytes...
165         jbyte* dst = reinterpret_cast<jbyte*>(&sin6.sin6_addr.s6_addr);
166         env->GetByteArrayRegion(addressBytes.get(), 0, 16, dst);
167         // ...and set the scope id...
168         static jfieldID scopeFid = env->GetFieldID(JniConstants::inet6AddressClass, "scope_id", "I");
169         sin6.sin6_scope_id = env->GetIntField(inetAddress, scopeFid);
170         sa_len = sizeof(sockaddr_in6);
171         return true;
172     }
173 
174     // Deal with Inet4Address instances.
175     if (map) {
176         // We should represent this Inet4Address as an IPv4-mapped IPv6 sockaddr_in6.
177         // Change the family...
178         sin6.sin6_family = AF_INET6;
179         // Copy the bytes...
180         jbyte* dst = reinterpret_cast<jbyte*>(&sin6.sin6_addr.s6_addr[12]);
181         env->GetByteArrayRegion(addressBytes.get(), 0, 4, dst);
182         // INADDR_ANY and in6addr_any are both all-zeros...
183         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr)) {
184             // ...but all other IPv4-mapped addresses are ::ffff:a.b.c.d, so insert the ffff...
185             memset(&(sin6.sin6_addr.s6_addr[10]), 0xff, 2);
186         }
187         sa_len = sizeof(sockaddr_in6);
188     } else {
189         // We should represent this Inet4Address as an IPv4 sockaddr_in.
190         sockaddr_in& sin = reinterpret_cast<sockaddr_in&>(ss);
191         sin.sin_port = htons(port);
192         jbyte* dst = reinterpret_cast<jbyte*>(&sin.sin_addr.s_addr);
193         env->GetByteArrayRegion(addressBytes.get(), 0, 4, dst);
194         sa_len = sizeof(sockaddr_in);
195     }
196     return true;
197 }
198 
inetAddressToSockaddrVerbatim(JNIEnv * env,jobject inetAddress,int port,sockaddr_storage & ss,socklen_t & sa_len)199 bool inetAddressToSockaddrVerbatim(JNIEnv* env, jobject inetAddress, int port, sockaddr_storage& ss, socklen_t& sa_len) {
200     return inetAddressToSockaddr(env, inetAddress, port, ss, sa_len, false);
201 }
202 
inetAddressToSockaddr(JNIEnv * env,jobject inetAddress,int port,sockaddr_storage & ss,socklen_t & sa_len)203 bool inetAddressToSockaddr(JNIEnv* env, jobject inetAddress, int port, sockaddr_storage& ss, socklen_t& sa_len) {
204     return inetAddressToSockaddr(env, inetAddress, port, ss, sa_len, true);
205 }
206 
setBlocking(int fd,bool blocking)207 bool setBlocking(int fd, bool blocking) {
208     int flags = fcntl(fd, F_GETFL);
209     if (flags == -1) {
210         return false;
211     }
212 
213     if (!blocking) {
214         flags |= O_NONBLOCK;
215     } else {
216         flags &= ~O_NONBLOCK;
217     }
218 
219     int rc = fcntl(fd, F_SETFL, flags);
220     return (rc != -1);
221 }
222