comments and logging

This commit is contained in:
Brenton Bostick 2023-02-02 14:52:39 -05:00
parent 914b4fae7b
commit fbd834716f
7 changed files with 48 additions and 22 deletions

View file

@ -98,8 +98,6 @@ jobject createVirtualNetworkConfigOperation(JNIEnv *env, ZT_VirtualNetworkConfig
jobject newInetAddress(JNIEnv *env, const sockaddr_storage &addr) jobject newInetAddress(JNIEnv *env, const sockaddr_storage &addr)
{ {
LOGV("newInetAddress");
jobject inetAddressObj = NULL; jobject inetAddressObj = NULL;
switch(addr.ss_family) switch(addr.ss_family)
{ {
@ -152,18 +150,14 @@ int addressPort(const sockaddr_storage addr) {
{ {
case AF_INET6: case AF_INET6:
{ {
LOGV("IPV6 Address");
sockaddr_in6 *ipv6 = (sockaddr_in6*)&addr; sockaddr_in6 *ipv6 = (sockaddr_in6*)&addr;
port = ntohs(ipv6->sin6_port); port = ntohs(ipv6->sin6_port);
LOGV("Port %d", port);
} }
break; break;
case AF_INET: case AF_INET:
{ {
LOGV("IPV4 Address");
sockaddr_in *ipv4 = (sockaddr_in*)&addr; sockaddr_in *ipv4 = (sockaddr_in*)&addr;
port = ntohs(ipv4->sin_port); port = ntohs(ipv4->sin_port);
LOGV("Port: %d", port);
} }
break; break;
default: default:
@ -182,8 +176,6 @@ int addressPort(const sockaddr_storage addr) {
// //
jobject newInetSocketAddress(JNIEnv *env, const sockaddr_storage &addr) jobject newInetSocketAddress(JNIEnv *env, const sockaddr_storage &addr)
{ {
LOGV("newInetSocketAddress Called");
if(isSocketAddressEmpty(addr)) if(isSocketAddressEmpty(addr))
{ {
return NULL; return NULL;
@ -208,8 +200,6 @@ jobject newInetSocketAddress(JNIEnv *env, const sockaddr_storage &addr)
jobject newPeerPhysicalPath(JNIEnv *env, const ZT_PeerPhysicalPath &ppp) jobject newPeerPhysicalPath(JNIEnv *env, const ZT_PeerPhysicalPath &ppp)
{ {
LOGV("newPeerPhysicalPath Called");
// //
// may be NULL // may be NULL
// //
@ -236,8 +226,6 @@ jobject newPeerPhysicalPath(JNIEnv *env, const ZT_PeerPhysicalPath &ppp)
jobject newPeer(JNIEnv *env, const ZT_Peer &peer) jobject newPeer(JNIEnv *env, const ZT_Peer &peer)
{ {
LOGV("newPeer called");
jobject peerRoleObj = createPeerRole(env, peer.role); jobject peerRoleObj = createPeerRole(env, peer.role);
if(env->ExceptionCheck() || peerRoleObj == NULL) if(env->ExceptionCheck() || peerRoleObj == NULL)
{ {

View file

@ -112,12 +112,17 @@ namespace {
}; };
/*
* This must return 0 on success. It can return any OS-dependent error code
* on failure, and this results in the network being placed into the
* PORT_ERROR state.
*/
int VirtualNetworkConfigFunctionCallback( int VirtualNetworkConfigFunctionCallback(
ZT_Node *node, ZT_Node *node,
void *userData, void *userData,
void *threadData, void *threadData,
uint64_t nwid, uint64_t nwid,
void **, void **nuptr,
enum ZT_VirtualNetworkConfigOperation operation, enum ZT_VirtualNetworkConfigOperation operation,
const ZT_VirtualNetworkConfig *config) const ZT_VirtualNetworkConfig *config)
{ {
@ -158,7 +163,7 @@ namespace {
void *userData, void *userData,
void *threadData, void *threadData,
uint64_t nwid, uint64_t nwid,
void**, void** nuptr,
uint64_t sourceMac, uint64_t sourceMac,
uint64_t destMac, uint64_t destMac,
unsigned int etherType, unsigned int etherType,
@ -268,6 +273,8 @@ namespace {
const uint64_t id[2], const uint64_t id[2],
const void *buffer, const void *buffer,
int bufferLength) { int bufferLength) {
LOGV("StatePutFunction");
char p[4096] = {0}; char p[4096] = {0};
bool secure = false; bool secure = false;
switch (type) { switch (type) {
@ -327,6 +334,11 @@ namespace {
} }
} }
/**
* This function should return the number of bytes actually stored to the
* buffer or -1 if the state object was not found or the buffer was too
* small to store it.
*/
int StateGetFunction( int StateGetFunction(
ZT_Node *node, ZT_Node *node,
void *userData, void *userData,
@ -335,6 +347,8 @@ namespace {
const uint64_t id[2], const uint64_t id[2],
void *buffer, void *buffer,
unsigned int bufferLength) { unsigned int bufferLength) {
LOGV("StateGetFunction");
char p[4096] = {0}; char p[4096] = {0};
switch (type) { switch (type) {
case ZT_STATE_OBJECT_IDENTITY_PUBLIC: case ZT_STATE_OBJECT_IDENTITY_PUBLIC:
@ -405,6 +419,11 @@ namespace {
return retval; return retval;
} }
/**
* The function must return zero on success and may return any error code
* on failure. Note that success does not (of course) guarantee packet
* delivery. It only means that the packet appears to have been sent.
*/
int WirePacketSendFunction(ZT_Node *node, int WirePacketSendFunction(ZT_Node *node,
void *userData, void *userData,
void *threadData, void *threadData,
@ -414,7 +433,7 @@ namespace {
unsigned int bufferSize, unsigned int bufferSize,
unsigned int ttl) unsigned int ttl)
{ {
LOGV("WirePacketSendFunction(%" PRId64 ", %p, %p, %d, %u)", localSocket, remoteAddress, buffer, bufferSize, ttl); LOGV("WirePacketSendFunction(%" PRId64 ", %p, %p, %u, %u)", localSocket, remoteAddress, buffer, bufferSize, ttl);
JniRef *ref = (JniRef*)userData; JniRef *ref = (JniRef*)userData;
assert(ref->node == node); assert(ref->node == node);
@ -446,6 +465,9 @@ namespace {
return retval; return retval;
} }
/**
* This function must return nonzero (true) if the path should be used.
*/
int PathCheckFunction(ZT_Node *node, int PathCheckFunction(ZT_Node *node,
void *userPtr, void *userPtr,
void *threadPtr, void *threadPtr,
@ -453,6 +475,8 @@ namespace {
int64_t localSocket, int64_t localSocket,
const struct sockaddr_storage *remoteAddress) const struct sockaddr_storage *remoteAddress)
{ {
LOGV("PathCheckFunction");
JniRef *ref = (JniRef*)userPtr; JniRef *ref = (JniRef*)userPtr;
assert(ref->node == node); assert(ref->node == node);
@ -474,6 +498,9 @@ namespace {
return env->CallBooleanMethod(ref->pathChecker, PathChecker_onPathCheck_method, address, localSocket, remoteAddressObj); return env->CallBooleanMethod(ref->pathChecker, PathChecker_onPathCheck_method, address, localSocket, remoteAddressObj);
} }
/**
* It must return a nonzero (true) value if the result buffer has been filled with an address.
*/
int PathLookupFunction(ZT_Node *node, int PathLookupFunction(ZT_Node *node,
void *userPtr, void *userPtr,
void *threadPtr, void *threadPtr,
@ -481,6 +508,8 @@ namespace {
int ss_family, int ss_family,
struct sockaddr_storage *result) struct sockaddr_storage *result)
{ {
LOGV("PathLookupFunction");
JniRef *ref = (JniRef*)userPtr; JniRef *ref = (JniRef*)userPtr;
assert(ref->node == node); assert(ref->node == node);
@ -491,6 +520,9 @@ namespace {
JNIEnv *env; JNIEnv *env;
GETENV(env, ref->jvm); GETENV(env, ref->jvm);
//
// may be NULL
//
jobject sockAddressObject = env->CallObjectMethod(ref->pathChecker, PathChecker_onPathLookup_method, address, ss_family); jobject sockAddressObject = env->CallObjectMethod(ref->pathChecker, PathChecker_onPathLookup_method, address, ss_family);
if(sockAddressObject == NULL) if(sockAddressObject == NULL)
{ {
@ -761,6 +793,9 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processVirtualNetworkFrame(
unsigned int frameLength = env->GetArrayLength(in_frameData); unsigned int frameLength = env->GetArrayLength(in_frameData);
void *frameData = env->GetPrimitiveArrayCritical(in_frameData, NULL); void *frameData = env->GetPrimitiveArrayCritical(in_frameData, NULL);
//
// need local copy of frameData because arbitrary code may run in ZT_Node_processVirtualNetworkFrame and no other JNI work may happen between GetPrimitiveArrayCritical / ReleasePrimitiveArrayCritical
//
void *localData = malloc(frameLength); void *localData = malloc(frameLength);
memcpy(localData, frameData, frameLength); memcpy(localData, frameData, frameLength);
env->ReleasePrimitiveArrayCritical(in_frameData, frameData, 0); env->ReleasePrimitiveArrayCritical(in_frameData, frameData, 0);
@ -827,6 +862,9 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket(
return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum; return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
} }
void *packetData = env->GetPrimitiveArrayCritical(in_packetData, NULL); void *packetData = env->GetPrimitiveArrayCritical(in_packetData, NULL);
//
// need local copy of packetData because arbitrary code may run in ZT_Node_processWirePacket and no other JNI work may happen between GetPrimitiveArrayCritical / ReleasePrimitiveArrayCritical
//
void *localData = malloc(packetLength); void *localData = malloc(packetLength);
memcpy(localData, packetData, packetLength); memcpy(localData, packetData, packetLength);
env->ReleasePrimitiveArrayCritical(in_packetData, packetData, 0); env->ReleasePrimitiveArrayCritical(in_packetData, packetData, 0);

View file

@ -166,6 +166,7 @@ public class Node {
* Process a packet received from the physical wire * Process a packet received from the physical wire
* *
* @param now Current clock in milliseconds * @param now Current clock in milliseconds
* @param localSocket Local socket or -1
* @param remoteAddress Origin of packet * @param remoteAddress Origin of packet
* @param packetData Packet data * @param packetData Packet data
* @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks() * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()

View file

@ -29,6 +29,7 @@ public interface PathChecker {
* @param ztAddress ZeroTier address or 0 for none/any * @param ztAddress ZeroTier address or 0 for none/any
* @param localSocket Local interface socket. -1 if unspecified * @param localSocket Local interface socket. -1 if unspecified
* @param remoteAddress remote address * @param remoteAddress remote address
* @return true if the path should be used
*/ */
boolean onPathCheck(long ztAddress, long localSocket, InetSocketAddress remoteAddress); boolean onPathCheck(long ztAddress, long localSocket, InetSocketAddress remoteAddress);

View file

@ -373,7 +373,7 @@ public class VirtualNetworkConfig implements Comparable<VirtualNetworkConfig> {
} }
/** /**
* ZeroTier-assigned addresses (in {@link java.net.InetSocketAddress} objects) * ZeroTier-assigned addresses (in {@link InetSocketAddress} objects)
* *
* For IP, the port number of the sockaddr_XX structure contains the number * 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. * of bits in the address netmask. Only the IP address and port are used.
@ -387,9 +387,7 @@ public class VirtualNetworkConfig implements Comparable<VirtualNetworkConfig> {
} }
/** /**
* ZeroTier-assigned routes (in {@link com.zerotier.sdk.VirtualNetworkRoute} objects) * ZeroTier-assigned routes (in {@link VirtualNetworkRoute} objects)
*
* @return
*/ */
public VirtualNetworkRoute[] getRoutes() { public VirtualNetworkRoute[] getRoutes() {
return routes; return routes;

View file

@ -40,7 +40,7 @@ public interface VirtualNetworkConfigListener {
* This in turn should be used by the underlying implementation to create * This in turn should be used by the underlying implementation to create
* and configure tap devices at the OS (or virtual network stack) layer.</P> * and configure tap devices at the OS (or virtual network stack) layer.</P>
* *
* This should not call {@link Node#multicastSubscribe} or other network-modifying * This should not call {@link Node#multicastSubscribe(long, long)} or other network-modifying
* methods, as this could cause a deadlock in multithreaded or interrupt * methods, as this could cause a deadlock in multithreaded or interrupt
* driven environments. * driven environments.
* *

View file

@ -35,8 +35,8 @@ public interface VirtualNetworkFrameListener {
* @param nwid ZeroTier One network ID * @param nwid ZeroTier One network ID
* @param srcMac source MAC address * @param srcMac source MAC address
* @param destMac destination MAC address * @param destMac destination MAC address
* @param ethertype * @param etherType EtherType
* @param vlanId * @param vlanId VLAN ID
* @param frameData data to send * @param frameData data to send
*/ */
public void onVirtualNetworkFrame( public void onVirtualNetworkFrame(