NetworkUtilities.cpp revision 51236bf3f42f234359ccbf1d09b41f7e09396889
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
28bool byteArrayToSocketAddress(JNIEnv* env, jclass, jbyteArray byteArray, int port, sockaddr_storage* ss) {
29    if (byteArray == NULL) {
30        jniThrowNullPointerException(env, NULL);
31        return false;
32    }
33
34    // Convert the IP address bytes to the proper IP address type.
35    size_t addressLength = env->GetArrayLength(byteArray);
36    memset(ss, 0, sizeof(*ss));
37    if (addressLength == 4) {
38        // IPv4 address.
39        sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ss);
40        sin->sin_family = AF_INET;
41        sin->sin_port = htons(port);
42        jbyte* dst = reinterpret_cast<jbyte*>(&sin->sin_addr.s_addr);
43        env->GetByteArrayRegion(byteArray, 0, 4, dst);
44    } else if (addressLength == 16) {
45        // IPv6 address.
46        sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(ss);
47        sin6->sin6_family = AF_INET6;
48        sin6->sin6_port = htons(port);
49        jbyte* dst = reinterpret_cast<jbyte*>(&sin6->sin6_addr.s6_addr);
50        env->GetByteArrayRegion(byteArray, 0, 16, dst);
51    } else {
52        // We can't throw SocketException. We aren't meant to see bad addresses, so seeing one
53        // really does imply an internal error.
54        // TODO: fix the code (native and Java) so we don't paint ourselves into this corner.
55        jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
56                "byteArrayToSocketAddress bad array length (%i)", addressLength);
57        return false;
58    }
59    return true;
60}
61
62jbyteArray socketAddressToByteArray(JNIEnv* env, const sockaddr_storage* ss) {
63    const void* rawAddress;
64    size_t addressLength;
65    if (ss->ss_family == AF_INET) {
66        const sockaddr_in* sin = reinterpret_cast<const sockaddr_in*>(ss);
67        rawAddress = &sin->sin_addr.s_addr;
68        addressLength = 4;
69    } else if (ss->ss_family == AF_INET6) {
70        const sockaddr_in6* sin6 = reinterpret_cast<const sockaddr_in6*>(ss);
71        rawAddress = &sin6->sin6_addr.s6_addr;
72        addressLength = 16;
73    } else {
74        // We can't throw SocketException. We aren't meant to see bad addresses, so seeing one
75        // really does imply an internal error.
76        // TODO: fix the code (native and Java) so we don't paint ourselves into this corner.
77        jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
78                "socketAddressToByteArray bad ss_family (%i)", ss->ss_family);
79        return NULL;
80    }
81
82    jbyteArray byteArray = env->NewByteArray(addressLength);
83    if (byteArray == NULL) {
84        return NULL;
85    }
86    env->SetByteArrayRegion(byteArray, 0, addressLength, reinterpret_cast<const jbyte*>(rawAddress));
87    return byteArray;
88}
89
90jobject byteArrayToInetAddress(JNIEnv* env, jbyteArray byteArray) {
91    if (byteArray == NULL) {
92        return NULL;
93    }
94    jmethodID getByAddressMethod = env->GetStaticMethodID(JniConstants::inetAddressClass,
95            "getByAddress", "([B)Ljava/net/InetAddress;");
96    if (getByAddressMethod == NULL) {
97        return NULL;
98    }
99    return env->CallStaticObjectMethod(JniConstants::inetAddressClass, getByAddressMethod, byteArray);
100}
101
102jobject socketAddressToInetAddress(JNIEnv* env, const sockaddr_storage* ss) {
103    jbyteArray byteArray = socketAddressToByteArray(env, ss);
104    return byteArrayToInetAddress(env, byteArray);
105}
106
107bool inetAddressToSocketAddress(JNIEnv* env, jobject inetAddress, int port, sockaddr_storage* ss) {
108    // Get the byte array that stores the IP address bytes in the InetAddress.
109    if (inetAddress == NULL) {
110        jniThrowNullPointerException(env, NULL);
111        return false;
112    }
113    static jfieldID fid = env->GetFieldID(JniConstants::inetAddressClass, "ipaddress", "[B");
114    jbyteArray addressBytes = reinterpret_cast<jbyteArray>(env->GetObjectField(inetAddress, fid));
115    return byteArrayToSocketAddress(env, NULL, addressBytes, port, ss);
116}
117
118bool setBlocking(int fd, bool blocking) {
119    int flags = fcntl(fd, F_GETFL);
120    if (flags == -1) {
121        return false;
122    }
123
124    if (!blocking) {
125        flags |= O_NONBLOCK;
126    } else {
127        flags &= ~O_NONBLOCK;
128    }
129
130    int rc = fcntl(fd, F_SETFL, flags);
131    return (rc != -1);
132}
133