NetworkUtilities.cpp revision a9f5c16a864ff63ba63f810410f8a27c086d5d52
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 <stdio.h>
25#include <string.h>
26
27// Used by functions that shouldn't throw SocketException. (These functions
28// aren't meant to see bad addresses, so seeing one really does imply an
29// internal error.)
30// TODO: fix the code (native and Java) so we don't paint ourselves into this corner.
31static void jniThrowBadAddressFamily(JNIEnv* env) {
32    jniThrowException(env, "java/lang/IllegalArgumentException", "Bad address family");
33}
34
35bool byteArrayToSocketAddress(JNIEnv* env, jclass, jbyteArray byteArray, int port, sockaddr_storage* ss) {
36    if (byteArray == NULL) {
37        jniThrowNullPointerException(env, NULL);
38        return false;
39    }
40
41    // Convert the IP address bytes to the proper IP address type.
42    size_t addressLength = env->GetArrayLength(byteArray);
43    memset(ss, 0, sizeof(*ss));
44    if (addressLength == 4) {
45        // IPv4 address.
46        sockaddr_in *sin = reinterpret_cast<sockaddr_in*>(ss);
47        sin->sin_family = AF_INET;
48        sin->sin_port = htons(port);
49        jbyte* dst = reinterpret_cast<jbyte*>(&sin->sin_addr.s_addr);
50        env->GetByteArrayRegion(byteArray, 0, 4, dst);
51    } else if (addressLength == 16) {
52        // IPv6 address.
53        sockaddr_in6 *sin6 = reinterpret_cast<sockaddr_in6*>(ss);
54        sin6->sin6_family = AF_INET6;
55        sin6->sin6_port = htons(port);
56        jbyte* dst = reinterpret_cast<jbyte*>(&sin6->sin6_addr.s6_addr);
57        env->GetByteArrayRegion(byteArray, 0, 16, dst);
58    } else {
59        jniThrowBadAddressFamily(env);
60        return false;
61    }
62    return true;
63}
64
65jbyteArray socketAddressToByteArray(JNIEnv* env, sockaddr_storage* ss) {
66    void *rawAddress;
67    size_t addressLength;
68    if (ss->ss_family == AF_INET) {
69        struct sockaddr_in *sin = reinterpret_cast<sockaddr_in*>(ss);
70        rawAddress = &sin->sin_addr.s_addr;
71        addressLength = 4;
72    } else if (ss->ss_family == AF_INET6) {
73        struct sockaddr_in6 *sin6 = reinterpret_cast<sockaddr_in6*>(ss);
74        rawAddress = &sin6->sin6_addr.s6_addr;
75        addressLength = 16;
76    } else {
77        jniThrowBadAddressFamily(env);
78        return NULL;
79    }
80
81    jbyteArray byteArray = env->NewByteArray(addressLength);
82    if (byteArray == NULL) {
83        return NULL;
84    }
85    env->SetByteArrayRegion(byteArray, 0, addressLength, reinterpret_cast<jbyte*>(rawAddress));
86    return byteArray;
87}
88
89jobject byteArrayToInetAddress(JNIEnv* env, jbyteArray byteArray) {
90    if (byteArray == NULL) {
91        return NULL;
92    }
93    jmethodID getByAddressMethod = env->GetStaticMethodID(JniConstants::inetAddressClass,
94            "getByAddress", "([B)Ljava/net/InetAddress;");
95    if (getByAddressMethod == NULL) {
96        return NULL;
97    }
98    return env->CallStaticObjectMethod(JniConstants::inetAddressClass, getByAddressMethod, byteArray);
99}
100
101jobject socketAddressToInetAddress(JNIEnv* env, sockaddr_storage* ss) {
102    jbyteArray byteArray = socketAddressToByteArray(env, ss);
103    return byteArrayToInetAddress(env, byteArray);
104}
105