2016-09-21 14:09:46 -07:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
|
|
|
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2017-01-13 11:36:48 -08:00
|
|
|
|
2015-09-24 16:21:36 -07:00
|
|
|
#include "ZT_jniutils.h"
|
2023-01-30 17:11:39 -05:00
|
|
|
|
|
|
|
#include "ZT_jnicache.h"
|
2020-10-21 14:18:04 -07:00
|
|
|
|
2015-04-23 22:48:56 -07:00
|
|
|
#include <string>
|
2023-02-01 08:05:24 -05:00
|
|
|
#include <cassert>
|
2015-04-23 21:36:33 -07:00
|
|
|
|
2020-10-21 14:18:04 -07:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2023-02-02 16:13:02 -05:00
|
|
|
#define LOG_TAG "Utils"
|
|
|
|
|
2015-09-24 16:21:36 -07:00
|
|
|
jobject createResultObject(JNIEnv *env, ZT_ResultCode code)
|
2015-04-23 21:39:07 -07:00
|
|
|
{
|
|
|
|
jobject resultObject = NULL;
|
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
jfieldID field;
|
2015-04-23 21:39:07 -07:00
|
|
|
switch(code)
|
|
|
|
{
|
2015-09-24 16:21:36 -07:00
|
|
|
case ZT_RESULT_OK:
|
2020-05-18 10:31:17 -07:00
|
|
|
case ZT_RESULT_OK_IGNORED:
|
2015-09-24 16:21:36 -07:00
|
|
|
LOGV("ZT_RESULT_OK");
|
2023-01-30 17:11:39 -05:00
|
|
|
field = ResultCode_RESULT_OK_field;
|
2015-04-23 21:39:07 -07:00
|
|
|
break;
|
2015-09-24 16:21:36 -07:00
|
|
|
case ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY:
|
|
|
|
LOGV("ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY");
|
2023-01-30 17:11:39 -05:00
|
|
|
field = ResultCode_RESULT_FATAL_ERROR_OUT_OF_MEMORY_field;
|
2015-04-23 21:39:07 -07:00
|
|
|
break;
|
2015-09-24 16:21:36 -07:00
|
|
|
case ZT_RESULT_FATAL_ERROR_DATA_STORE_FAILED:
|
2023-01-31 11:31:52 -05:00
|
|
|
LOGV("ZT_RESULT_FATAL_ERROR_DATA_STORE_FAILED");
|
2023-01-30 17:11:39 -05:00
|
|
|
field = ResultCode_RESULT_FATAL_ERROR_DATA_STORE_FAILED_field;
|
2015-04-23 21:39:07 -07:00
|
|
|
break;
|
2015-09-24 16:21:36 -07:00
|
|
|
case ZT_RESULT_ERROR_NETWORK_NOT_FOUND:
|
2020-05-18 10:31:17 -07:00
|
|
|
LOGV("ZT_RESULT_ERROR_NETWORK_NOT_FOUND");
|
2023-01-30 17:11:39 -05:00
|
|
|
field = ResultCode_RESULT_ERROR_NETWORK_NOT_FOUND_field;
|
2015-04-23 21:39:07 -07:00
|
|
|
break;
|
2020-05-18 10:31:17 -07:00
|
|
|
case ZT_RESULT_ERROR_UNSUPPORTED_OPERATION:
|
|
|
|
LOGV("ZT_RESULT_ERROR_UNSUPPORTED_OPERATION");
|
2023-01-30 17:11:39 -05:00
|
|
|
field = ResultCode_RESULT_ERROR_UNSUPPORTED_OPERATION_field;
|
2020-05-18 10:31:17 -07:00
|
|
|
break;
|
|
|
|
case ZT_RESULT_ERROR_BAD_PARAMETER:
|
|
|
|
LOGV("ZT_RESULT_ERROR_BAD_PARAMETER");
|
2023-01-30 17:11:39 -05:00
|
|
|
field = ResultCode_RESULT_ERROR_BAD_PARAMETER_field;
|
2020-05-18 10:31:17 -07:00
|
|
|
break;
|
2015-09-24 16:21:36 -07:00
|
|
|
case ZT_RESULT_FATAL_ERROR_INTERNAL:
|
2015-04-23 21:39:07 -07:00
|
|
|
default:
|
2020-05-18 10:31:17 -07:00
|
|
|
LOGV("ZT_RESULT_FATAL_ERROR_INTERNAL");
|
2023-01-30 17:11:39 -05:00
|
|
|
field = ResultCode_RESULT_FATAL_ERROR_INTERNAL_field;
|
2015-04-23 21:39:07 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
resultObject = env->GetStaticObjectField(ResultCode_class, field);
|
2017-01-13 11:36:48 -08:00
|
|
|
if(env->ExceptionCheck() || resultObject == NULL)
|
2015-05-28 20:36:54 -07:00
|
|
|
{
|
|
|
|
LOGE("Error on GetStaticObjectField");
|
|
|
|
}
|
2015-04-23 21:39:07 -07:00
|
|
|
return resultObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-24 16:21:36 -07:00
|
|
|
jobject createVirtualNetworkStatus(JNIEnv *env, ZT_VirtualNetworkStatus status)
|
2015-04-23 21:39:07 -07:00
|
|
|
{
|
|
|
|
jobject statusObject = NULL;
|
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
jfieldID field;
|
2015-04-23 21:39:07 -07:00
|
|
|
switch(status)
|
|
|
|
{
|
2015-09-24 16:21:36 -07:00
|
|
|
case ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION:
|
2023-01-30 17:11:39 -05:00
|
|
|
field = VirtualNetworkStatus_NETWORK_STATUS_REQUESTING_CONFIGURATION_field;
|
2015-04-23 21:39:07 -07:00
|
|
|
break;
|
2015-09-24 16:21:36 -07:00
|
|
|
case ZT_NETWORK_STATUS_OK:
|
2023-01-30 17:11:39 -05:00
|
|
|
field = VirtualNetworkStatus_NETWORK_STATUS_OK_field;
|
2015-04-23 21:39:07 -07:00
|
|
|
break;
|
2022-03-04 14:28:25 -08:00
|
|
|
case ZT_NETWORK_STATUS_AUTHENTICATION_REQUIRED:
|
2023-01-30 17:11:39 -05:00
|
|
|
field = VirtualNetworkStatus_NETWORK_STATUS_AUTHENTICATION_REQUIRED_field;
|
2022-03-04 14:28:25 -08:00
|
|
|
break;
|
2015-09-24 16:21:36 -07:00
|
|
|
case ZT_NETWORK_STATUS_ACCESS_DENIED:
|
2023-01-30 17:11:39 -05:00
|
|
|
field = VirtualNetworkStatus_NETWORK_STATUS_ACCESS_DENIED_field;
|
2015-04-23 21:39:07 -07:00
|
|
|
break;
|
2015-09-24 16:21:36 -07:00
|
|
|
case ZT_NETWORK_STATUS_NOT_FOUND:
|
2023-01-30 17:11:39 -05:00
|
|
|
field = VirtualNetworkStatus_NETWORK_STATUS_NOT_FOUND_field;
|
2015-04-23 21:39:07 -07:00
|
|
|
break;
|
2015-09-24 16:21:36 -07:00
|
|
|
case ZT_NETWORK_STATUS_PORT_ERROR:
|
2023-01-30 17:11:39 -05:00
|
|
|
field = VirtualNetworkStatus_NETWORK_STATUS_PORT_ERROR_field;
|
2015-04-23 21:39:07 -07:00
|
|
|
break;
|
2015-09-24 16:21:36 -07:00
|
|
|
case ZT_NETWORK_STATUS_CLIENT_TOO_OLD:
|
2023-01-30 17:11:39 -05:00
|
|
|
field = VirtualNetworkStatus_NETWORK_STATUS_CLIENT_TOO_OLD_field;
|
2015-04-23 21:39:07 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
statusObject = env->GetStaticObjectField(VirtualNetworkStatus_class, field);
|
2015-04-23 21:39:07 -07:00
|
|
|
|
|
|
|
return statusObject;
|
|
|
|
}
|
|
|
|
|
2015-09-24 16:21:36 -07:00
|
|
|
jobject createEvent(JNIEnv *env, ZT_Event event)
|
2015-04-23 21:39:07 -07:00
|
|
|
{
|
2023-01-31 12:04:21 -05:00
|
|
|
jobject eventObject = env->CallStaticObjectMethod(Event_class, Event_fromInt_method, event);
|
|
|
|
if (env->ExceptionCheck() || eventObject == NULL) {
|
|
|
|
LOGE("Error creating Event object");
|
|
|
|
return NULL;
|
2015-04-23 21:39:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return eventObject;
|
|
|
|
}
|
|
|
|
|
2015-09-24 16:21:36 -07:00
|
|
|
jobject createPeerRole(JNIEnv *env, ZT_PeerRole role)
|
2015-04-24 17:58:59 -07:00
|
|
|
{
|
|
|
|
jobject peerRoleObject = NULL;
|
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
jfieldID field;
|
2015-04-24 17:58:59 -07:00
|
|
|
switch(role)
|
|
|
|
{
|
2015-09-24 16:21:36 -07:00
|
|
|
case ZT_PEER_ROLE_LEAF:
|
2023-01-30 17:11:39 -05:00
|
|
|
field = PeerRole_PEER_ROLE_LEAF_field;
|
2015-04-24 17:58:59 -07:00
|
|
|
break;
|
2017-02-13 10:51:36 -08:00
|
|
|
case ZT_PEER_ROLE_MOON:
|
2023-01-30 17:11:39 -05:00
|
|
|
field = PeerRole_PEER_ROLE_MOON_field;
|
2015-04-24 17:58:59 -07:00
|
|
|
break;
|
2017-02-13 10:51:36 -08:00
|
|
|
case ZT_PEER_ROLE_PLANET:
|
2023-01-30 17:11:39 -05:00
|
|
|
field = PeerRole_PEER_ROLE_PLANET_field;
|
2015-04-24 17:58:59 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
peerRoleObject = env->GetStaticObjectField(PeerRole_class, field);
|
2015-04-24 17:58:59 -07:00
|
|
|
|
|
|
|
return peerRoleObject;
|
|
|
|
}
|
|
|
|
|
2015-09-24 16:21:36 -07:00
|
|
|
jobject createVirtualNetworkType(JNIEnv *env, ZT_VirtualNetworkType type)
|
2015-04-23 21:39:07 -07:00
|
|
|
{
|
|
|
|
jobject vntypeObject = NULL;
|
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
jfieldID field;
|
2015-04-23 21:39:07 -07:00
|
|
|
switch(type)
|
|
|
|
{
|
2015-09-24 16:21:36 -07:00
|
|
|
case ZT_NETWORK_TYPE_PRIVATE:
|
2023-01-30 17:11:39 -05:00
|
|
|
field = VirtualNetworkType_NETWORK_TYPE_PRIVATE_field;
|
2015-04-23 21:39:07 -07:00
|
|
|
break;
|
2015-09-24 16:21:36 -07:00
|
|
|
case ZT_NETWORK_TYPE_PUBLIC:
|
2023-01-30 17:11:39 -05:00
|
|
|
field = VirtualNetworkType_NETWORK_TYPE_PUBLIC_field;
|
2015-04-23 21:39:07 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
vntypeObject = env->GetStaticObjectField(VirtualNetworkType_class, field);
|
2015-04-23 21:39:07 -07:00
|
|
|
return vntypeObject;
|
|
|
|
}
|
2015-04-23 22:48:56 -07:00
|
|
|
|
2015-09-24 16:21:36 -07:00
|
|
|
jobject createVirtualNetworkConfigOperation(JNIEnv *env, ZT_VirtualNetworkConfigOperation op)
|
2015-04-24 18:39:17 -07:00
|
|
|
{
|
|
|
|
jobject vnetConfigOpObject = NULL;
|
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
jfieldID field;
|
2015-04-24 18:39:17 -07:00
|
|
|
switch(op)
|
|
|
|
{
|
2015-09-24 16:21:36 -07:00
|
|
|
case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP:
|
2023-01-30 17:11:39 -05:00
|
|
|
field = VirtualNetworkConfigOperation_VIRTUAL_NETWORK_CONFIG_OPERATION_UP_field;
|
2015-04-24 18:39:17 -07:00
|
|
|
break;
|
2015-09-24 16:21:36 -07:00
|
|
|
case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE:
|
2023-01-30 17:11:39 -05:00
|
|
|
field = VirtualNetworkConfigOperation_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE_field;
|
2015-04-24 18:39:17 -07:00
|
|
|
break;
|
2015-09-24 16:21:36 -07:00
|
|
|
case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN:
|
2023-01-30 17:11:39 -05:00
|
|
|
field = VirtualNetworkConfigOperation_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN_field;
|
2015-04-24 18:39:17 -07:00
|
|
|
break;
|
2015-09-24 16:21:36 -07:00
|
|
|
case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY:
|
2023-01-30 17:11:39 -05:00
|
|
|
field = VirtualNetworkConfigOperation_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY_field;
|
2015-04-24 18:39:17 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
vnetConfigOpObject = env->GetStaticObjectField(VirtualNetworkConfigOperation_class, field);
|
2015-04-24 18:39:17 -07:00
|
|
|
return vnetConfigOpObject;
|
|
|
|
}
|
|
|
|
|
2015-04-23 22:48:56 -07:00
|
|
|
jobject newInetAddress(JNIEnv *env, const sockaddr_storage &addr)
|
2015-04-23 21:36:33 -07:00
|
|
|
{
|
2015-05-27 20:42:54 -07:00
|
|
|
LOGV("newInetAddress");
|
2015-04-23 22:48:56 -07:00
|
|
|
|
|
|
|
jobject inetAddressObj = NULL;
|
|
|
|
switch(addr.ss_family)
|
|
|
|
{
|
|
|
|
case AF_INET6:
|
|
|
|
{
|
|
|
|
sockaddr_in6 *ipv6 = (sockaddr_in6*)&addr;
|
|
|
|
jbyteArray buff = env->NewByteArray(16);
|
|
|
|
if(buff == NULL)
|
|
|
|
{
|
2015-05-27 20:42:54 -07:00
|
|
|
LOGE("Error creating IPV6 byte array");
|
2015-04-23 22:48:56 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
env->SetByteArrayRegion(buff, 0, 16, (jbyte*)ipv6->sin6_addr.s6_addr);
|
|
|
|
inetAddressObj = env->CallStaticObjectMethod(
|
2023-01-30 17:11:39 -05:00
|
|
|
InetAddress_class, InetAddress_getByAddress_method, buff);
|
2015-04-23 22:48:56 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AF_INET:
|
|
|
|
{
|
|
|
|
sockaddr_in *ipv4 = (sockaddr_in*)&addr;
|
|
|
|
jbyteArray buff = env->NewByteArray(4);
|
|
|
|
if(buff == NULL)
|
|
|
|
{
|
2015-05-27 20:42:54 -07:00
|
|
|
LOGE("Error creating IPV4 byte array");
|
2015-04-23 22:48:56 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
env->SetByteArrayRegion(buff, 0, 4, (jbyte*)&ipv4->sin_addr);
|
|
|
|
inetAddressObj = env->CallStaticObjectMethod(
|
2023-01-30 17:11:39 -05:00
|
|
|
InetAddress_class, InetAddress_getByAddress_method, buff);
|
2015-04-23 22:48:56 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-05-27 20:42:54 -07:00
|
|
|
if(env->ExceptionCheck() || inetAddressObj == NULL) {
|
|
|
|
LOGE("Error creating InetAddress object");
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-04-23 22:48:56 -07:00
|
|
|
|
|
|
|
return inetAddressObj;
|
2015-04-23 21:36:33 -07:00
|
|
|
}
|
|
|
|
|
2015-04-27 17:48:37 -07:00
|
|
|
jobject newInetSocketAddress(JNIEnv *env, const sockaddr_storage &addr)
|
|
|
|
{
|
2015-05-27 20:42:54 -07:00
|
|
|
LOGV("newInetSocketAddress Called");
|
2015-04-27 17:48:37 -07:00
|
|
|
|
2016-09-19 13:40:53 -07:00
|
|
|
jobject inetAddressObject = NULL;
|
2017-01-13 11:36:48 -08:00
|
|
|
|
2016-09-19 13:40:53 -07:00
|
|
|
if(addr.ss_family != 0)
|
|
|
|
{
|
|
|
|
inetAddressObject = newInetAddress(env, addr);
|
2015-04-27 17:48:37 -07:00
|
|
|
|
2016-09-19 13:40:53 -07:00
|
|
|
if(env->ExceptionCheck() || inetAddressObject == NULL)
|
|
|
|
{
|
|
|
|
LOGE("Error creating new inet address");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2015-04-27 17:48:37 -07:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int port = 0;
|
|
|
|
switch(addr.ss_family)
|
|
|
|
{
|
|
|
|
case AF_INET6:
|
|
|
|
{
|
2015-06-09 19:38:05 -07:00
|
|
|
LOGV("IPV6 Address");
|
2015-04-27 17:48:37 -07:00
|
|
|
sockaddr_in6 *ipv6 = (sockaddr_in6*)&addr;
|
2015-04-30 22:07:14 -07:00
|
|
|
port = ntohs(ipv6->sin6_port);
|
2015-06-09 19:38:05 -07:00
|
|
|
LOGV("Port %d", port);
|
2015-04-27 17:48:37 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AF_INET:
|
|
|
|
{
|
2015-06-09 19:38:05 -07:00
|
|
|
LOGV("IPV4 Address");
|
2015-04-27 17:48:37 -07:00
|
|
|
sockaddr_in *ipv4 = (sockaddr_in*)&addr;
|
2015-04-30 22:07:14 -07:00
|
|
|
port = ntohs(ipv4->sin_port);
|
2015-06-09 19:38:05 -07:00
|
|
|
LOGV("Port: %d", port);
|
2015-04-27 17:48:37 -07:00
|
|
|
}
|
|
|
|
break;
|
2015-06-01 20:03:28 -07:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2016-09-19 13:40:53 -07:00
|
|
|
}
|
2015-04-27 17:48:37 -07:00
|
|
|
|
2015-06-01 20:03:28 -07:00
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
jobject inetSocketAddressObject = env->NewObject(InetSocketAddress_class, InetSocketAddress_ctor, inetAddressObject, port);
|
2015-05-27 20:42:54 -07:00
|
|
|
if(env->ExceptionCheck() || inetSocketAddressObject == NULL) {
|
|
|
|
LOGE("Error creating InetSocketAddress object");
|
|
|
|
}
|
2015-04-27 17:48:37 -07:00
|
|
|
return inetSocketAddressObject;
|
|
|
|
}
|
|
|
|
|
2015-09-24 16:21:36 -07:00
|
|
|
jobject newPeerPhysicalPath(JNIEnv *env, const ZT_PeerPhysicalPath &ppp)
|
2015-04-24 17:58:59 -07:00
|
|
|
{
|
2015-05-27 20:42:54 -07:00
|
|
|
LOGV("newPeerPhysicalPath Called");
|
2015-04-24 17:58:59 -07:00
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
jobject pppObject = env->NewObject(PeerPhysicalPath_class, PeerPhysicalPath_ctor);
|
2015-05-27 20:42:54 -07:00
|
|
|
if(env->ExceptionCheck() || pppObject == NULL)
|
2015-04-24 17:58:59 -07:00
|
|
|
{
|
2015-05-27 20:42:54 -07:00
|
|
|
LOGE("Error creating PPP object");
|
2015-04-24 17:58:59 -07:00
|
|
|
return NULL; // out of memory
|
|
|
|
}
|
|
|
|
|
2015-05-27 20:42:54 -07:00
|
|
|
jobject addressObject = newInetSocketAddress(env, ppp.address);
|
|
|
|
if(env->ExceptionCheck() || addressObject == NULL) {
|
|
|
|
LOGE("Error creating InetSocketAddress object");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
env->SetObjectField(pppObject, PeerPhysicalPath_address_field, addressObject);
|
|
|
|
env->SetLongField(pppObject, PeerPhysicalPath_lastSend_field, ppp.lastSend);
|
|
|
|
env->SetLongField(pppObject, PeerPhysicalPath_lastReceive_field, ppp.lastReceive);
|
|
|
|
env->SetBooleanField(pppObject, PeerPhysicalPath_preferred_field, ppp.preferred);
|
2015-04-24 17:58:59 -07:00
|
|
|
|
2015-05-27 20:42:54 -07:00
|
|
|
if(env->ExceptionCheck()) {
|
|
|
|
LOGE("Exception assigning fields to PeerPhysicalPath object");
|
|
|
|
}
|
2015-04-24 17:58:59 -07:00
|
|
|
|
|
|
|
return pppObject;
|
|
|
|
}
|
|
|
|
|
2017-01-13 11:36:48 -08:00
|
|
|
jobject newPeer(JNIEnv *env, const ZT_Peer &peer)
|
2015-04-24 17:58:59 -07:00
|
|
|
{
|
2015-05-27 20:42:54 -07:00
|
|
|
LOGV("newPeer called");
|
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
jobject peerObject = env->NewObject(Peer_class, Peer_ctor);
|
2015-05-27 20:42:54 -07:00
|
|
|
if(env->ExceptionCheck() || peerObject == NULL)
|
2015-04-24 17:58:59 -07:00
|
|
|
{
|
2015-05-28 20:36:54 -07:00
|
|
|
LOGE("Error creating Peer object");
|
2015-04-24 17:58:59 -07:00
|
|
|
return NULL; // out of memory
|
|
|
|
}
|
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
env->SetLongField(peerObject, Peer_address_field, (jlong)peer.address);
|
|
|
|
env->SetIntField(peerObject, Peer_versionMajor_field, peer.versionMajor);
|
|
|
|
env->SetIntField(peerObject, Peer_versionMinor_field, peer.versionMinor);
|
|
|
|
env->SetIntField(peerObject, Peer_versionRev_field, peer.versionRev);
|
|
|
|
env->SetIntField(peerObject, Peer_latency_field, peer.latency);
|
|
|
|
env->SetObjectField(peerObject, Peer_role_field, createPeerRole(env, peer.role));
|
2015-05-28 20:36:54 -07:00
|
|
|
|
|
|
|
jobjectArray arrayObject = env->NewObjectArray(
|
2023-01-30 17:11:39 -05:00
|
|
|
peer.pathCount, PeerPhysicalPath_class, NULL);
|
2017-01-13 11:36:48 -08:00
|
|
|
if(env->ExceptionCheck() || arrayObject == NULL)
|
2015-05-28 20:36:54 -07:00
|
|
|
{
|
|
|
|
LOGE("Error creating PeerPhysicalPath[] array");
|
2015-05-27 20:42:54 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-04-24 17:58:59 -07:00
|
|
|
for(unsigned int i = 0; i < peer.pathCount; ++i)
|
|
|
|
{
|
|
|
|
jobject path = newPeerPhysicalPath(env, peer.paths[i]);
|
2015-05-28 20:36:54 -07:00
|
|
|
|
|
|
|
env->SetObjectArrayElement(arrayObject, i, path);
|
|
|
|
if(env->ExceptionCheck()) {
|
|
|
|
LOGE("exception assigning PeerPhysicalPath to array");
|
2015-05-27 20:42:54 -07:00
|
|
|
break;
|
|
|
|
}
|
2023-02-01 17:44:51 -05:00
|
|
|
|
|
|
|
env->DeleteLocalRef(path);
|
2015-04-24 17:58:59 -07:00
|
|
|
}
|
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
env->SetObjectField(peerObject, Peer_paths_field, arrayObject);
|
2015-04-24 17:58:59 -07:00
|
|
|
|
|
|
|
return peerObject;
|
|
|
|
}
|
|
|
|
|
2015-09-24 16:21:36 -07:00
|
|
|
jobject newNetworkConfig(JNIEnv *env, const ZT_VirtualNetworkConfig &vnetConfig)
|
2015-04-24 18:20:10 -07:00
|
|
|
{
|
2023-01-30 17:11:39 -05:00
|
|
|
jobject vnetConfigObj = env->NewObject(VirtualNetworkConfig_class, VirtualNetworkConfig_ctor);
|
2015-05-27 20:42:54 -07:00
|
|
|
if(env->ExceptionCheck() || vnetConfigObj == NULL)
|
2015-04-24 18:20:10 -07:00
|
|
|
{
|
2015-04-29 19:29:35 -07:00
|
|
|
LOGE("Error creating new VirtualNetworkConfig object");
|
2015-04-24 18:20:10 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
env->SetLongField(vnetConfigObj, VirtualNetworkConfig_nwid_field, vnetConfig.nwid);
|
|
|
|
env->SetLongField(vnetConfigObj, VirtualNetworkConfig_mac_field, vnetConfig.mac);
|
2015-04-24 18:20:10 -07:00
|
|
|
jstring nameStr = env->NewStringUTF(vnetConfig.name);
|
2015-05-27 20:42:54 -07:00
|
|
|
if(env->ExceptionCheck() || nameStr == NULL)
|
2015-04-24 18:20:10 -07:00
|
|
|
{
|
|
|
|
return NULL; // out of memory
|
|
|
|
}
|
2023-01-30 17:11:39 -05:00
|
|
|
env->SetObjectField(vnetConfigObj, VirtualNetworkConfig_name_field, nameStr);
|
2015-04-24 18:20:10 -07:00
|
|
|
|
|
|
|
jobject statusObject = createVirtualNetworkStatus(env, vnetConfig.status);
|
2015-05-27 20:42:54 -07:00
|
|
|
if(env->ExceptionCheck() || statusObject == NULL)
|
2015-04-24 18:20:10 -07:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2023-01-30 17:11:39 -05:00
|
|
|
env->SetObjectField(vnetConfigObj, VirtualNetworkConfig_status_field, statusObject);
|
2015-04-24 18:20:10 -07:00
|
|
|
|
|
|
|
jobject typeObject = createVirtualNetworkType(env, vnetConfig.type);
|
2015-05-27 20:42:54 -07:00
|
|
|
if(env->ExceptionCheck() || typeObject == NULL)
|
2015-04-24 18:20:10 -07:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2023-01-30 17:11:39 -05:00
|
|
|
env->SetObjectField(vnetConfigObj, VirtualNetworkConfig_type_field, typeObject);
|
2015-04-24 18:20:10 -07:00
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
env->SetIntField(vnetConfigObj, VirtualNetworkConfig_mtu_field, (int)vnetConfig.mtu);
|
|
|
|
env->SetBooleanField(vnetConfigObj, VirtualNetworkConfig_dhcp_field, vnetConfig.dhcp);
|
|
|
|
env->SetBooleanField(vnetConfigObj, VirtualNetworkConfig_bridge_field, vnetConfig.bridge);
|
|
|
|
env->SetBooleanField(vnetConfigObj, VirtualNetworkConfig_broadcastEnabled_field, vnetConfig.broadcastEnabled);
|
|
|
|
env->SetIntField(vnetConfigObj, VirtualNetworkConfig_portError_field, vnetConfig.portError);
|
2015-05-28 20:36:54 -07:00
|
|
|
|
|
|
|
jobjectArray assignedAddrArrayObj = env->NewObjectArray(
|
2023-01-30 17:11:39 -05:00
|
|
|
vnetConfig.assignedAddressCount, InetSocketAddress_class, NULL);
|
2015-05-28 20:36:54 -07:00
|
|
|
if(env->ExceptionCheck() || assignedAddrArrayObj == NULL)
|
|
|
|
{
|
|
|
|
LOGE("Error creating InetSocketAddress[] array");
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-04-24 18:20:10 -07:00
|
|
|
|
|
|
|
for(unsigned int i = 0; i < vnetConfig.assignedAddressCount; ++i)
|
|
|
|
{
|
2015-06-01 20:03:28 -07:00
|
|
|
jobject inetAddrObj = newInetSocketAddress(env, vnetConfig.assignedAddresses[i]);
|
2015-05-28 20:36:54 -07:00
|
|
|
env->SetObjectArrayElement(assignedAddrArrayObj, i, inetAddrObj);
|
|
|
|
if(env->ExceptionCheck())
|
|
|
|
{
|
|
|
|
LOGE("Error assigning InetSocketAddress to array");
|
|
|
|
return NULL;
|
|
|
|
}
|
2023-02-01 17:44:51 -05:00
|
|
|
|
|
|
|
env->DeleteLocalRef(inetAddrObj);
|
2015-04-24 18:20:10 -07:00
|
|
|
}
|
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
env->SetObjectField(vnetConfigObj, VirtualNetworkConfig_assignedAddresses_field, assignedAddrArrayObj);
|
2016-09-05 13:52:29 -07:00
|
|
|
|
|
|
|
jobjectArray routesArrayObj = env->NewObjectArray(
|
2023-01-30 17:11:39 -05:00
|
|
|
vnetConfig.routeCount, VirtualNetworkRoute_class, NULL);
|
2016-09-05 13:52:29 -07:00
|
|
|
if(env->ExceptionCheck() || routesArrayObj == NULL)
|
|
|
|
{
|
|
|
|
LOGE("Error creating VirtualNetworkRoute[] array");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < vnetConfig.routeCount; ++i)
|
|
|
|
{
|
|
|
|
jobject routeObj = newVirtualNetworkRoute(env, vnetConfig.routes[i]);
|
|
|
|
env->SetObjectArrayElement(routesArrayObj, i, routeObj);
|
|
|
|
if(env->ExceptionCheck())
|
|
|
|
{
|
|
|
|
LOGE("Error assigning VirtualNetworkRoute to array");
|
|
|
|
return NULL;
|
|
|
|
}
|
2023-02-01 17:44:51 -05:00
|
|
|
|
|
|
|
env->DeleteLocalRef(routeObj);
|
2016-09-05 13:52:29 -07:00
|
|
|
}
|
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
env->SetObjectField(vnetConfigObj, VirtualNetworkConfig_routes_field, routesArrayObj);
|
2016-09-05 13:52:29 -07:00
|
|
|
|
2020-10-21 14:18:04 -07:00
|
|
|
jobject dnsObj = newVirtualNetworkDNS(env, vnetConfig.dns);
|
|
|
|
if (dnsObj != NULL) {
|
2023-01-30 17:11:39 -05:00
|
|
|
env->SetObjectField(vnetConfigObj, VirtualNetworkConfig_dns_field, dnsObj);
|
2020-10-21 14:18:04 -07:00
|
|
|
}
|
2015-04-24 18:20:10 -07:00
|
|
|
return vnetConfigObj;
|
|
|
|
}
|
|
|
|
|
2016-07-07 20:07:07 -07:00
|
|
|
jobject newVersion(JNIEnv *env, int major, int minor, int rev)
|
2015-04-24 20:13:21 -07:00
|
|
|
{
|
2023-01-30 17:11:39 -05:00
|
|
|
// create a com.zerotier.sdk.Version object
|
|
|
|
jobject versionObj = env->NewObject(Version_class, Version_ctor);
|
2015-05-27 20:42:54 -07:00
|
|
|
if(env->ExceptionCheck() || versionObj == NULL)
|
2015-04-24 20:13:21 -07:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
env->SetIntField(versionObj, Version_major_field, (jint)major);
|
|
|
|
env->SetIntField(versionObj, Version_minor_field, (jint)minor);
|
|
|
|
env->SetIntField(versionObj, Version_revision_field, (jint)rev);
|
2015-04-29 19:29:35 -07:00
|
|
|
|
|
|
|
return versionObj;
|
2015-04-24 20:13:21 -07:00
|
|
|
}
|
2015-04-24 18:39:17 -07:00
|
|
|
|
2016-09-05 13:52:29 -07:00
|
|
|
jobject newVirtualNetworkRoute(JNIEnv *env, const ZT_VirtualNetworkRoute &route)
|
|
|
|
{
|
2023-01-30 17:11:39 -05:00
|
|
|
jobject routeObj = env->NewObject(VirtualNetworkRoute_class, VirtualNetworkRoute_ctor);
|
2016-09-05 13:52:29 -07:00
|
|
|
if(env->ExceptionCheck() || routeObj == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
jobject targetObj = newInetSocketAddress(env, route.target);
|
|
|
|
jobject viaObj = newInetSocketAddress(env, route.via);
|
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
env->SetObjectField(routeObj, VirtualNetworkRoute_target_field, targetObj);
|
|
|
|
env->SetObjectField(routeObj, VirtualNetworkRoute_via_field, viaObj);
|
|
|
|
env->SetIntField(routeObj, VirtualNetworkRoute_flags_field, (jint)route.flags);
|
|
|
|
env->SetIntField(routeObj, VirtualNetworkRoute_metric_field, (jint)route.metric);
|
2016-09-05 13:52:29 -07:00
|
|
|
|
|
|
|
return routeObj;
|
|
|
|
}
|
|
|
|
|
2020-10-21 14:18:04 -07:00
|
|
|
jobject newVirtualNetworkDNS(JNIEnv *env, const ZT_VirtualNetworkDNS &dns)
|
|
|
|
{
|
2023-01-30 17:11:39 -05:00
|
|
|
jobject dnsObj = env->NewObject(VirtualNetworkDNS_class, VirtualNetworkDNS_ctor);
|
2020-10-21 14:18:04 -07:00
|
|
|
if(env->ExceptionCheck() || dnsObj == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(dns.domain) > 0) {
|
2023-01-30 17:11:39 -05:00
|
|
|
|
2020-10-21 14:18:04 -07:00
|
|
|
jstring domain = env->NewStringUTF(dns.domain);
|
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
jobject addrArray = env->NewObject(ArrayList_class, ArrayList_ctor, 0);
|
2020-10-21 14:18:04 -07:00
|
|
|
|
|
|
|
struct sockaddr_storage nullAddr;
|
|
|
|
memset(&nullAddr, 0, sizeof(struct sockaddr_storage));
|
|
|
|
for(int i = 0; i < ZT_MAX_DNS_SERVERS; ++i) {
|
|
|
|
struct sockaddr_storage tmp = dns.server_addr[i];
|
|
|
|
|
|
|
|
if (memcmp(&tmp, &nullAddr, sizeof(struct sockaddr_storage)) != 0) {
|
|
|
|
jobject addr = newInetSocketAddress(env, tmp);
|
2023-01-30 17:11:39 -05:00
|
|
|
env->CallBooleanMethod(addrArray, ArrayList_add_method, addr);
|
2020-10-21 14:18:04 -07:00
|
|
|
env->DeleteLocalRef(addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 17:11:39 -05:00
|
|
|
env->SetObjectField(dnsObj, VirtualNetworkDNS_domain_field, domain);
|
|
|
|
env->SetObjectField(dnsObj, VirtualNetworkDNS_servers_field, addrArray);
|
2020-10-21 14:18:04 -07:00
|
|
|
|
|
|
|
return dnsObj;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|