mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-04-15 06:46:36 +00:00
Switch to InetSockAddress instead of InetAddress so we can send the port # to java as well
This commit is contained in:
parent
73d68c0c98
commit
d1ed269537
@ -301,6 +301,9 @@ bool appendItemToArrayList(JNIEnv *env, jobject array, jobject object)
|
||||
|
||||
jobject newInetAddress(JNIEnv *env, const sockaddr_storage &addr)
|
||||
{
|
||||
static jclass inetAddressClass = NULL;
|
||||
static jmethodID inetAddress_getByAddress = NULL;
|
||||
|
||||
if(inetAddressClass == NULL)
|
||||
{
|
||||
inetAddressClass = env->FindClass("java/net/InetAddress");
|
||||
@ -356,6 +359,58 @@ jobject newInetAddress(JNIEnv *env, const sockaddr_storage &addr)
|
||||
return inetAddressObj;
|
||||
}
|
||||
|
||||
jobject newInetSocketAddress(JNIEnv *env, const sockaddr_storage &addr)
|
||||
{
|
||||
static jclass inetSocketAddressClass = NULL;
|
||||
static jmethodID inetSocketAddress_constructor = NULL;
|
||||
|
||||
if(inetSocketAddressClass == NULL)
|
||||
{
|
||||
inetSocketAddressClass == env->FindClass("java/net/InetSocketAddress");
|
||||
if(inetSocketAddressClass == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
jobject inetAddressObject = newInetAddress(env, addr);
|
||||
|
||||
if(inetAddressObject == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(inetSocketAddress_constructor == NULL)
|
||||
{
|
||||
inetSocketAddress_constructor = env->GetMethodID(
|
||||
inetSocketAddressClass, "<init>", "(Ljava/net/InetAddress;I)V");
|
||||
if(inetSocketAddress_constructor == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int port = 0;
|
||||
switch(addr.ss_family)
|
||||
{
|
||||
case AF_INET6:
|
||||
{
|
||||
sockaddr_in6 *ipv6 = (sockaddr_in6*)&addr;
|
||||
port = ipv6->sin6_port;
|
||||
}
|
||||
break;
|
||||
case AF_INET:
|
||||
{
|
||||
sockaddr_in *ipv4 = (sockaddr_in*)&addr;
|
||||
port = ipv4->sin_port;
|
||||
}
|
||||
break;
|
||||
};
|
||||
|
||||
jobject inetSocketAddressObject = env->NewObject(inetSocketAddressClass, inetSocketAddress_constructor, inetAddressObject, port);
|
||||
return inetSocketAddressObject;
|
||||
}
|
||||
|
||||
jobject newMulticastGroup(JNIEnv *env, const ZT1_MulticastGroup &mc)
|
||||
{
|
||||
static jclass multicastGroupClass = NULL;
|
||||
|
@ -30,6 +30,7 @@ jobject createVirtualNetworkConfigOperation(JNIEnv *env, ZT1_VirtualNetworkConfi
|
||||
jobject newArrayList(JNIEnv *env);
|
||||
bool appendItemToArrayList(JNIEnv *env, jobject array, jobject object);
|
||||
|
||||
jobject newInetSocketAddress(JNIEnv *env, const sockaddr_storage &addr);
|
||||
jobject newInetAddress(JNIEnv *env, const sockaddr_storage &addr);
|
||||
|
||||
jobject newMulticastGroup(JNIEnv *env, const ZT1_MulticastGroup &mc);
|
||||
|
@ -245,7 +245,7 @@ namespace {
|
||||
if(ref->onOutOfDateMethod == NULL)
|
||||
{
|
||||
ref->onNetworkErrorMethod = env->GetMethodID(ref->eventListenerClass,
|
||||
"onNetworkError", "(Lcom/zerotierone/sdk/Event;Ljava/net/InetAddress;)V");
|
||||
"onNetworkError", "(Lcom/zerotierone/sdk/Event;Ljava/net/InetSocketAddress;)V");
|
||||
if(ref->onNetworkErrorMethod == NULL)
|
||||
{
|
||||
LOGE("Couldn't find onNetworkError method");
|
||||
@ -301,7 +301,7 @@ namespace {
|
||||
if(data != NULL)
|
||||
{
|
||||
sockaddr_storage *addr = (sockaddr_storage*)data;
|
||||
jobject addressObj = newInetAddress(env, *addr);
|
||||
jobject addressObj = newInetSocketAddress(env, *addr);
|
||||
env->CallVoidMethod(ref->eventListener, ref->onNetworkErrorMethod, addressObj);
|
||||
}
|
||||
}
|
||||
@ -463,7 +463,7 @@ namespace {
|
||||
if(ref->packetSenderCallbackMethod == NULL)
|
||||
{
|
||||
ref->packetSenderCallbackMethod = env->GetMethodID(ref->packetSenderClass,
|
||||
"onSendPacketRequested", "(Ljava/net/InetAddress;I[B)I");
|
||||
"onSendPacketRequested", "(Ljava/net/InetSocketAddress;I[B)I");
|
||||
if(ref->packetSenderCallbackMethod == NULL)
|
||||
{
|
||||
LOGE("Couldn't find onSendPacketRequested method");
|
||||
@ -471,7 +471,7 @@ namespace {
|
||||
}
|
||||
}
|
||||
|
||||
jobject addressObj = newInetAddress(env, *address);
|
||||
jobject addressObj = newInetSocketAddress(env, *address);
|
||||
jbyteArray bufferObj = env->NewByteArray(bufferSize);
|
||||
env->SetByteArrayRegion(bufferObj, 0, bufferSize, (jbyte*)buffer);
|
||||
return env->CallIntMethod(ref->packetSender, ref->packetSenderCallbackMethod, addressObj, linkDesparation, bufferObj);
|
||||
@ -717,7 +717,7 @@ JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_processVirtualNetworkFra
|
||||
/*
|
||||
* Class: com_zerotierone_sdk_Node
|
||||
* Method: processWirePacket
|
||||
* Signature: (JJLjava/net/InetAddress;I[B[J)Lcom/zerotierone/sdk/ResultCode;
|
||||
* Signature: (JJLjava/net/InetSocketAddress;I[B[J)Lcom/zerotierone/sdk/ResultCode;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_processWirePacket(
|
||||
JNIEnv *env, jobject obj,
|
||||
@ -745,7 +745,7 @@ JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_processWirePacket(
|
||||
uint64_t now = (uint64_t)in_now;
|
||||
unsigned int linkDesparation = (unsigned int)in_linkDesparation;
|
||||
|
||||
// get the java.net.InetAddress class and getAddress() method
|
||||
// get the java.net.InetSocketAddress class and getAddress() method
|
||||
jclass inetAddressClass = env->FindClass("java/net/InetAddress");
|
||||
if(inetAddressClass == NULL)
|
||||
{
|
||||
@ -761,8 +761,24 @@ JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_processWirePacket(
|
||||
return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
|
||||
}
|
||||
|
||||
jclass InetSocketAddressClass = env->FindClass("java/net/InetSocketAddress");
|
||||
if(InetSocketAddressClass == NULL)
|
||||
{
|
||||
return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
|
||||
}
|
||||
|
||||
jmethodID inetSockGetAddressMethod = env->GetMethodID(
|
||||
InetSocketAddressClass, "getAddress", "()Ljava/net/InetAddress;");
|
||||
|
||||
jobject addrObject = env->CallObjectMethod(in_remoteAddress, inetSockGetAddressMethod);
|
||||
|
||||
if(addrObject == NULL)
|
||||
{
|
||||
return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
|
||||
}
|
||||
|
||||
// Call InetAddress.getAddress()
|
||||
jbyteArray addressArray = (jbyteArray)env->CallObjectMethod(in_remoteAddress, getAddressMethod);
|
||||
jbyteArray addressArray = (jbyteArray)env->CallObjectMethod(addrObject, getAddressMethod);
|
||||
if(addressArray == NULL)
|
||||
{
|
||||
// unable to call getAddress()
|
||||
|
@ -34,7 +34,7 @@ JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_processVirtualNetworkFra
|
||||
/*
|
||||
* Class: com_zerotierone_sdk_Node
|
||||
* Method: processWirePacket
|
||||
* Signature: (JJLjava/net/InetAddress;I[B[J)Lcom/zerotierone/sdk/ResultCode;
|
||||
* Signature: (JJLjava/net/InetSockAddress;I[B[J)Lcom/zerotierone/sdk/ResultCode;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_processWirePacket
|
||||
(JNIEnv *, jobject, jlong, jlong, jobject, jint, jbyteArray, jlongArray);
|
||||
|
@ -94,14 +94,14 @@ public enum Event {
|
||||
/**
|
||||
* A packet failed authentication
|
||||
*
|
||||
* <p>Meta-data: {@link InetAddress} containing origin address of packet</p>
|
||||
* <p>Meta-data: {@link InetSocketAddress} containing origin address of packet</p>
|
||||
*/
|
||||
EVENT_AUTHENTICATION_FAILURE,
|
||||
|
||||
/**
|
||||
* A received packet was not valid
|
||||
*
|
||||
* <p>Meta-data: {@link InetAddress} containing origin address of packet</p>
|
||||
* <p>Meta-data: {@link InetSocketAddress} containing origin address of packet</p>
|
||||
*/
|
||||
EVENT_INVALID_PACKET,
|
||||
|
||||
|
@ -27,7 +27,7 @@
|
||||
|
||||
package com.zerotierone.sdk;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.lang.String;
|
||||
|
||||
/**
|
||||
@ -45,9 +45,9 @@ public interface EventListener {
|
||||
* Callback for network error events: {@link Event.EVENT_AUTHENTICATION_FAILUER}, {link Event.EVENT_INVALID_PACKET}
|
||||
*
|
||||
* @param event {@link Event} enum
|
||||
* @param source {@link InetAddress} containing the origin address of the packet
|
||||
* @param source {@link InetSocketAddress} containing the origin address of the packet
|
||||
*/
|
||||
public void onNetworkError(Event event, InetAddress source);
|
||||
public void onNetworkError(Event event, InetSocketAddress source);
|
||||
|
||||
/**
|
||||
* Callback when the node detects that it's out of date.
|
||||
|
@ -29,7 +29,7 @@ package com.zerotierone.sdk;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.lang.Long;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
@ -152,7 +152,7 @@ public class Node {
|
||||
*/
|
||||
public ResultCode processWirePacket(
|
||||
long now,
|
||||
InetAddress remoteAddress,
|
||||
InetSocketAddress remoteAddress,
|
||||
int linkDesperation,
|
||||
byte[] packetData,
|
||||
long[] nextBackgroundTaskDeadline) {
|
||||
@ -377,7 +377,7 @@ public class Node {
|
||||
private native ResultCode processWirePacket(
|
||||
long nodeId,
|
||||
long now,
|
||||
InetAddress remoteAddress,
|
||||
InetSocketAddress remoteAddress,
|
||||
int linkDesperation,
|
||||
byte[] packetData,
|
||||
long[] nextBackgroundTaskDeadline);
|
||||
|
@ -26,7 +26,7 @@
|
||||
*/
|
||||
package com.zerotierone.sdk;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
|
||||
public interface PacketSender {
|
||||
@ -37,13 +37,13 @@ public interface PacketSender {
|
||||
* on failure. Note that success does not (of course) guarantee packet
|
||||
* delivery. It only means that the packet appears to have been sent.</p>
|
||||
*
|
||||
* @param addr {@link InetAddress} to send to
|
||||
* @param addr {@link InetSocketAddress} to send to
|
||||
* @param linkDesperation
|
||||
* @param packetData data to send
|
||||
* @return 0 on success, any error code on failure.
|
||||
*/
|
||||
public int onSendPacketRequested(
|
||||
InetAddress addr,
|
||||
InetSocketAddress addr,
|
||||
int linkDesperation,
|
||||
byte[] packetData);
|
||||
}
|
||||
|
@ -27,13 +27,13 @@
|
||||
|
||||
package com.zerotierone.sdk;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
/**
|
||||
* Physical network path to a peer
|
||||
*/
|
||||
public final class PeerPhysicalPath {
|
||||
private InetAddress address;
|
||||
private InetSocketAddress address;
|
||||
private long lastSend;
|
||||
private long lastReceive;
|
||||
private boolean fixed;
|
||||
@ -45,7 +45,7 @@ public final class PeerPhysicalPath {
|
||||
/**
|
||||
* Address of endpoint
|
||||
*/
|
||||
public final InetAddress address() {
|
||||
public final InetSocketAddress address() {
|
||||
return address;
|
||||
}
|
||||
|
||||
|
@ -29,7 +29,7 @@ package com.zerotierone.sdk;
|
||||
|
||||
import java.lang.String;
|
||||
import java.util.ArrayList;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
public final class VirtualNetworkConfig {
|
||||
public static final int MAX_MULTICAST_SUBSCRIPTIONS = 4096;
|
||||
@ -48,7 +48,7 @@ public final class VirtualNetworkConfig {
|
||||
private boolean enabled;
|
||||
private long netconfRevision;
|
||||
private ArrayList<MulticastGroup> multicastSubscriptions;
|
||||
private ArrayList<InetAddress> assignedAddresses;
|
||||
private ArrayList<InetSocketAddress> assignedAddresses;
|
||||
|
||||
private VirtualNetworkConfig() {
|
||||
|
||||
@ -155,7 +155,7 @@ public final class VirtualNetworkConfig {
|
||||
}
|
||||
|
||||
/**
|
||||
* ZeroTier-assigned addresses (in {@link java.net.InetAddress} objects)
|
||||
* ZeroTier-assigned addresses (in {@link java.net.InetSocketAddress} objects)
|
||||
*
|
||||
* For IP, the port number of the sockaddr_XX structure contains the number
|
||||
* of bits in the address netmask. Only the IP address and port are used.
|
||||
@ -164,7 +164,7 @@ public final class VirtualNetworkConfig {
|
||||
* This is only used for ZeroTier-managed address assignments sent by the
|
||||
* virtual network's configuration master.
|
||||
*/
|
||||
public final ArrayList<InetAddress> assignedAddresses() {
|
||||
public final ArrayList<InetSocketAddress> assignedAddresses() {
|
||||
return assignedAddresses;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user