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
byteArrayToSocketAddress(JNIEnv * env,jclass,jbyteArray byteArray,int port,sockaddr_storage * ss)28 bool 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 char buf[64];
56 snprintf(buf, sizeof(buf), "byteArrayToSocketAddress bad array length (%i)", addressLength);
57 jniThrowException(env, "java/lang/IllegalArgumentException", buf);
58 return false;
59 }
60 return true;
61 }
62
socketAddressToByteArray(JNIEnv * env,sockaddr_storage * ss)63 jbyteArray socketAddressToByteArray(JNIEnv* env, sockaddr_storage* ss) {
64 void* rawAddress;
65 size_t addressLength;
66 if (ss->ss_family == AF_INET) {
67 sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ss);
68 rawAddress = &sin->sin_addr.s_addr;
69 addressLength = 4;
70 } else if (ss->ss_family == AF_INET6) {
71 sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(ss);
72 rawAddress = &sin6->sin6_addr.s6_addr;
73 addressLength = 16;
74 } else {
75 // We can't throw SocketException. We aren't meant to see bad addresses, so seeing one
76 // really does imply an internal error.
77 // TODO: fix the code (native and Java) so we don't paint ourselves into this corner.
78 char buf[64];
79 snprintf(buf, sizeof(buf), "socketAddressToByteArray bad ss_family (%i)", ss->ss_family);
80 jniThrowException(env, "java/lang/IllegalArgumentException", buf);
81 return NULL;
82 }
83
84 jbyteArray byteArray = env->NewByteArray(addressLength);
85 if (byteArray == NULL) {
86 return NULL;
87 }
88 env->SetByteArrayRegion(byteArray, 0, addressLength, reinterpret_cast<jbyte*>(rawAddress));
89 return byteArray;
90 }
91
byteArrayToInetAddress(JNIEnv * env,jbyteArray byteArray)92 jobject byteArrayToInetAddress(JNIEnv* env, jbyteArray byteArray) {
93 if (byteArray == NULL) {
94 return NULL;
95 }
96 jmethodID getByAddressMethod = env->GetStaticMethodID(JniConstants::inetAddressClass,
97 "getByAddress", "([B)Ljava/net/InetAddress;");
98 if (getByAddressMethod == NULL) {
99 return NULL;
100 }
101 return env->CallStaticObjectMethod(JniConstants::inetAddressClass, getByAddressMethod, byteArray);
102 }
103
socketAddressToInetAddress(JNIEnv * env,sockaddr_storage * ss)104 jobject socketAddressToInetAddress(JNIEnv* env, sockaddr_storage* ss) {
105 jbyteArray byteArray = socketAddressToByteArray(env, ss);
106 return byteArrayToInetAddress(env, byteArray);
107 }
108
setBlocking(int fd,bool blocking)109 bool setBlocking(int fd, bool blocking) {
110 int flags = fcntl(fd, F_GETFL);
111 if (flags == -1) {
112 return false;
113 }
114
115 if (!blocking) {
116 flags |= O_NONBLOCK;
117 } else {
118 flags &= ~O_NONBLOCK;
119 }
120
121 int rc = fcntl(fd, F_SETFL, flags);
122 return (rc != -1);
123 }
124