NetworkUtilities.cpp revision da15009528cc8300a6251f1d0931ac8657c9fc31
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
23#include <arpa/inet.h>
24#include <fcntl.h>
25#include <stdio.h>
26#include <string.h>
27#include <sys/socket.h>
28
29jobject socketAddressToInetAddress(JNIEnv* env, const sockaddr_storage* ss) {
30    // Convert IPv4-mapped IPv6 addresses to IPv4 addresses.
31    // The RI states "Java will never return an IPv4-mapped address".
32    sockaddr_storage tmp;
33    memset(&tmp, 0, sizeof(tmp));
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        memcpy(&tmp, ss, sizeof(tmp));
38        // Unmap it into an IPv4 address.
39        sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(&tmp);
40        sin->sin_family = AF_INET;
41        sin->sin_port = sin6->sin6_port;
42        memcpy(&sin->sin_addr.s_addr, &sin6->sin6_addr.s6_addr[12], 4);
43        // Fall through into the regular conversion using the unmapped address.
44        ss = &tmp;
45    }
46
47    const void* rawAddress;
48    size_t addressLength;
49    if (ss->ss_family == AF_INET) {
50        const sockaddr_in* sin = reinterpret_cast<const sockaddr_in*>(ss);
51        rawAddress = &sin->sin_addr.s_addr;
52        addressLength = 4;
53    } else if (ss->ss_family == AF_INET6) {
54        const sockaddr_in6* sin6 = reinterpret_cast<const sockaddr_in6*>(ss);
55        rawAddress = &sin6->sin6_addr.s6_addr;
56        addressLength = 16;
57    } else {
58        // We can't throw SocketException. We aren't meant to see bad addresses, so seeing one
59        // really does imply an internal error.
60        jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
61                "socketAddressToInetAddress bad ss_family: %i", ss->ss_family);
62        return NULL;
63    }
64
65    jbyteArray byteArray = env->NewByteArray(addressLength);
66    if (byteArray == NULL) {
67        return NULL;
68    }
69    env->SetByteArrayRegion(byteArray, 0, addressLength, reinterpret_cast<const jbyte*>(rawAddress));
70
71    static jmethodID getByAddressMethod = env->GetStaticMethodID(JniConstants::inetAddressClass,
72            "getByAddress", "([B)Ljava/net/InetAddress;");
73    if (getByAddressMethod == NULL) {
74        return NULL;
75    }
76    return env->CallStaticObjectMethod(JniConstants::inetAddressClass, getByAddressMethod, byteArray);
77}
78
79static bool inetAddressToSocketAddress(JNIEnv* env, jobject inetAddress, int port, sockaddr_storage* ss, bool map) {
80    memset(ss, 0, sizeof(*ss));
81
82    // Get the byte array that stores the IP address bytes in the InetAddress.
83    if (inetAddress == NULL) {
84        jniThrowNullPointerException(env, NULL);
85        return false;
86    }
87    static jfieldID fid = env->GetFieldID(JniConstants::inetAddressClass, "ipaddress", "[B");
88    jbyteArray addressBytes = reinterpret_cast<jbyteArray>(env->GetObjectField(inetAddress, fid));
89    if (addressBytes == NULL) {
90        jniThrowNullPointerException(env, NULL);
91        return false;
92    }
93
94    // We use AF_INET6 sockets, so we want an IPv6 address (which may be a IPv4-mapped address).
95    sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(ss);
96    sin6->sin6_family = AF_INET6;
97    sin6->sin6_port = htons(port);
98
99    // Convert the IP address bytes to the appropriate kind of sockaddr.
100    size_t addressLength = env->GetArrayLength(addressBytes);
101    if (addressLength == 4) {
102        if (map) {
103            // We should represent this IPv4 address as an IPv4-mapped IPv6 sockaddr_in6.
104            // Copy the bytes...
105            jbyte* dst = reinterpret_cast<jbyte*>(&sin6->sin6_addr.s6_addr[12]);
106            env->GetByteArrayRegion(addressBytes, 0, 4, dst);
107            // INADDR_ANY and in6addr_any are both all-zeros...
108            if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
109                // ...but all other IPv4-mapped addresses are ::ffff:a.b.c.d, so insert the ffff...
110                memset(&(sin6->sin6_addr.s6_addr[10]), 0xff, 2);
111            }
112        } else {
113            // We should represent this IPv4 address as an IPv4 sockaddr_in.
114            sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ss);
115            sin->sin_family = AF_INET;
116            sin->sin_port = htons(port);
117            jbyte* dst = reinterpret_cast<jbyte*>(&sin->sin_addr.s_addr);
118            env->GetByteArrayRegion(addressBytes, 0, 4, dst);
119        }
120        return true;
121    } else if (addressLength == 16) {
122        // IPv6 address. Copy the bytes...
123        jbyte* dst = reinterpret_cast<jbyte*>(&sin6->sin6_addr.s6_addr);
124        env->GetByteArrayRegion(addressBytes, 0, 16, dst);
125        // ...and set the scope id...
126        static jfieldID fid = env->GetFieldID(JniConstants::inet6AddressClass, "scope_id", "I");
127        sin6->sin6_scope_id = env->GetIntField(inetAddress, fid);
128        return true;
129    }
130
131    // We can't throw SocketException. We aren't meant to see bad addresses, so seeing one
132    // really does imply an internal error.
133    jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
134            "inetAddressToSocketAddress bad array length: %i", addressLength);
135    return false;
136}
137
138bool inetAddressToSockaddr_getnameinfo(JNIEnv* env, jobject inetAddress, int port, sockaddr_storage* ss) {
139    return inetAddressToSocketAddress(env, inetAddress, port, ss, false);
140}
141
142bool inetAddressToSockaddr(JNIEnv* env, jobject inetAddress, int port, sockaddr_storage* ss) {
143    return inetAddressToSocketAddress(env, inetAddress, port, ss, true);
144}
145
146bool setBlocking(int fd, bool blocking) {
147    int flags = fcntl(fd, F_GETFL);
148    if (flags == -1) {
149        return false;
150    }
151
152    if (!blocking) {
153        flags |= O_NONBLOCK;
154    } else {
155        flags &= ~O_NONBLOCK;
156    }
157
158    int rc = fcntl(fd, F_SETFL, flags);
159    return (rc != -1);
160}
161