ZeroTierOne/java/src/com/zerotier/sdk/Node.java
Adam Ierymenko 1c5897895b
1.10.4 merge into main (#1893)
* add note about forceTcpRelay

* Create a sample systemd unit for tcp proxy

* set gitattributes for rust & cargo so hashes dont conflict on Windows

* Revert "set gitattributes for rust & cargo so hashes dont conflict on Windows"

This reverts commit 032dc5c108.

* Turn off autocrlf for rust source

Doesn't appear to play nice well when it comes to git and vendored cargo package hashes

* Fix #1883 (#1886)

Still unknown as to why, but the call to `nc->GetProperties()` can fail
when setting a friendly name on the Windows virtual ethernet adapter.
Ensure that `ncp` is not null before continuing and accessing the device
GUID.

* Don't vendor packages for zeroidc (#1885)

* Added docker environment way to join networks (#1871)

* add StringUtils

* fix headers
use recommended headers and remove unused headers

* move extern "C"
only JNI functions need to be exported

* cleanup

* fix ANDROID-50: RESULT_ERROR_BAD_PARAMETER typo

* fix typo in log message

* fix typos in JNI method signatures

* fix typo

* fix ANDROID-51: fieldName is uninitialized

* fix ANDROID-35: memory leak

* fix missing DeleteLocalRef in loops

* update to use unique error codes

* add GETENV macro

* add LOG_TAG defines

* ANDROID-48: add ZT_jnicache.cpp

* ANDROID-48: use ZT_jnicache.cpp and remove ZT_jnilookup.cpp and ZT_jniarray.cpp

* add Event.fromInt

* add PeerRole.fromInt

* add ResultCode.fromInt

* fix ANDROID-36: issues with ResultCode

* add VirtualNetworkConfigOperation.fromInt

* fix ANDROID-40: VirtualNetworkConfigOperation out-of-sync with ZT_VirtualNetworkConfigOperation enum

* add VirtualNetworkStatus.fromInt

* fix ANDROID-37: VirtualNetworkStatus out-of-sync with ZT_VirtualNetworkStatus enum

* add VirtualNetworkType.fromInt

* make NodeStatus a plain data class

* fix ANDROID-52: synchronization bug with nodeMap

* Node init work: separate Node construction and init

* add Node.toString

* make PeerPhysicalPath a plain data class

* remove unused PeerPhysicalPath.fixed

* add array functions

* make Peer a plain data class

* make Version a plain data class

* fix ANDROID-42: copy/paste error

* fix ANDROID-49: VirtualNetworkConfig.equals is wrong

* reimplement VirtualNetworkConfig.equals

* reimplement VirtualNetworkConfig.compareTo

* add VirtualNetworkConfig.hashCode

* make VirtualNetworkConfig a plain data class

* remove unused VirtualNetworkConfig.enabled

* reimplement VirtualNetworkDNS.equals

* add VirtualNetworkDNS.hashCode

* make VirtualNetworkDNS a plain data class

* reimplement VirtualNetworkRoute.equals

* reimplement VirtualNetworkRoute.compareTo

* reimplement VirtualNetworkRoute.toString

* add VirtualNetworkRoute.hashCode

* make VirtualNetworkRoute a plain data class

* add isSocketAddressEmpty

* add addressPort

* add fromSocketAddressObject

* invert logic in a couple of places and return early

* newInetAddress and newInetSocketAddress work
allow newInetSocketAddress to return NULL if given empty address

* fix ANDROID-38: stack corruption in onSendPacketRequested

* use GETENV macro

* JniRef work
JniRef does not use callbacks struct, so remove
fix NewGlobalRef / DeleteGlobalRef mismatch

* use PRId64 macros

* switch statement work

* comments and logging

* Modifier 'public' is redundant for interface members

* NodeException can be made a checked Exception

* 'NodeException' does not define a 'serialVersionUID' field

* 'finalize()' should not be overridden
this is fine to do because ZeroTierOneService calls close() when it is done

* error handling, error reporting, asserts, logging

* simplify loadLibrary

* rename Node.networks -> Node.networkConfigs

* Windows file permissions fix (#1887)

* Allow macOS interfaces to use multiple IP addresses (#1879)

Co-authored-by: Sean OMeara <someara@users.noreply.github.com>
Co-authored-by: Grant Limberg <glimberg@users.noreply.github.com>

* Fix condition where full HELLOs might not be sent when necessary (#1877)

Co-authored-by: Grant Limberg <glimberg@users.noreply.github.com>

* 1.10.4 version bumps

* Add security policy to repo (#1889)

* [+] add e2k64 arch (#1890)

* temp fix for ANDROID-56: crash inside newNetworkConfig from too many args

* 1.10.4 release notes

---------

Co-authored-by: travis laduke <travisladuke@gmail.com>
Co-authored-by: Grant Limberg <grant.limberg@zerotier.com>
Co-authored-by: Grant Limberg <glimberg@users.noreply.github.com>
Co-authored-by: Leonardo Amaral <leleobhz@users.noreply.github.com>
Co-authored-by: Brenton Bostick <bostick@gmail.com>
Co-authored-by: Sean OMeara <someara@users.noreply.github.com>
Co-authored-by: Joseph Henry <joseph-henry@users.noreply.github.com>
Co-authored-by: Roman Peshkichev <roman.peshkichev@gmail.com>
2023-03-07 16:50:34 -05:00

460 lines
16 KiB
Java

/*
* ZeroTier One - Network Virtualization Everywhere
* Copyright (C) 2011-2015 ZeroTier, Inc.
*
* 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/>.
*
* --
*
* ZeroTier may be used and distributed under the terms of the GPLv3, which
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
*
* If you would like to embed ZeroTier into a commercial application or
* redistribute it in a modified binary form, please contact ZeroTier Networks
* LLC. Start here: http://www.zerotier.com/
*/
package com.zerotier.sdk;
import java.net.InetSocketAddress;
/**
* A ZeroTier One node
*/
public class Node {
static {
System.loadLibrary("ZeroTierOneJNI");
}
private static final String TAG = "NODE";
/**
* Node ID for JNI purposes.
* Currently set to the now value passed in at the constructor
*/
private final long nodeId;
/**
* Create a new ZeroTier One node
*
* @param now Current clock in milliseconds
*/
public Node(long now) {
this.nodeId = now;
}
/**
* Init a new ZeroTier One node
*
* <p>Note that this can take a few seconds the first time it's called, as it
* will generate an identity.</p>
*
* @param getListener User written instance of the {@link DataStoreGetListener} interface called to get objects from persistent storage. This instance must be unique per Node object.
* @param putListener User written instance of the {@link DataStorePutListener} interface called to put objects in persistent storage. This instance must be unique per Node object.
* @param sender User written instance of the {@link PacketSender} interface to send ZeroTier packets out over the wire.
* @param eventListener User written instance of the {@link EventListener} interface to receive status updates and non-fatal error notices. This instance must be unique per Node object.
* @param frameListener User written instance of the {@link VirtualNetworkFrameListener} interface to send a frame out to a virtual network port.
* @param configListener User written instance of the {@link VirtualNetworkConfigListener} interface to be called when virtual LANs are created, deleted, or their config parameters change. This instance must be unique per Node object.
* @param pathChecker User written instance of the {@link PathChecker} interface. Not required and can be null.
*/
public ResultCode init(
DataStoreGetListener getListener,
DataStorePutListener putListener,
PacketSender sender,
EventListener eventListener,
VirtualNetworkFrameListener frameListener,
VirtualNetworkConfigListener configListener,
PathChecker pathChecker) throws NodeException {
ResultCode rc = node_init(
nodeId,
getListener,
putListener,
sender,
eventListener,
frameListener,
configListener,
pathChecker);
if(rc != ResultCode.RESULT_OK) {
throw new NodeException(rc.toString());
}
return rc;
}
public boolean isInited() {
return node_isInited(nodeId);
}
/**
* Close this Node.
*
* <p>The Node object can no longer be used once this method is called.</p>
*/
public void close() {
node_delete(nodeId);
}
@Override
public String toString() {
return "Node(" + nodeId + ")";
}
/**
* Process a frame from a virtual network port
*
* @param now Current clock in milliseconds
* @param nwid ZeroTier 64-bit virtual network ID
* @param sourceMac Source MAC address (least significant 48 bits)
* @param destMac Destination MAC address (least significant 48 bits)
* @param etherType 16-bit Ethernet frame type
* @param vlanId 10-bit VLAN ID or 0 if none
* @param frameData Frame payload data
* @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
* @return OK (0) or error code if a fatal error condition has occurred
*/
public ResultCode processVirtualNetworkFrame(
long now,
long nwid,
long sourceMac,
long destMac,
int etherType,
int vlanId,
byte[] frameData,
long[] nextBackgroundTaskDeadline) {
return processVirtualNetworkFrame(
nodeId, now, nwid, sourceMac, destMac, etherType, vlanId,
frameData, nextBackgroundTaskDeadline);
}
/**
* Process a packet received from the physical wire
*
* @param now Current clock in milliseconds
* @param localSocket Local socket or -1
* @param remoteAddress Origin of packet
* @param packetData Packet data
* @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
* @return OK (0) or error code if a fatal error condition has occurred
*/
public ResultCode processWirePacket(
long now,
long localSocket,
InetSocketAddress remoteAddress,
byte[] packetData,
long[] nextBackgroundTaskDeadline) {
return processWirePacket(
nodeId, now, localSocket, remoteAddress, packetData,
nextBackgroundTaskDeadline);
}
/**
* Perform periodic background operations
*
* @param now Current clock in milliseconds
* @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
* @return OK (0) or error code if a fatal error condition has occurred
*/
public ResultCode processBackgroundTasks(long now, long[] nextBackgroundTaskDeadline) {
return processBackgroundTasks(nodeId, now, nextBackgroundTaskDeadline);
}
/**
* Join a network
*
* <p>This may generate calls to the port config callback before it returns,
* or these may be deferred if a netconf is not available yet.</p>
*
* <p>If we are already a member of the network, nothing is done and OK is
* returned.</p>
*
* @param nwid 64-bit ZeroTier network ID
* @return OK (0) or error code if a fatal error condition has occurred
*/
public ResultCode join(long nwid) {
return join(nodeId, nwid);
}
/**
* Leave a network
*
* <p>If a port has been configured for this network this will generate a call
* to the port config callback with a NULL second parameter to indicate that
* the port is now deleted.</p>
*
* @param nwid 64-bit network ID
* @return OK (0) or error code if a fatal error condition has occurred
*/
public ResultCode leave(long nwid) {
return leave(nodeId, nwid);
}
/**
* Subscribe to an Ethernet multicast group
*
* <p>For IPv4 ARP, the implementation must subscribe to 0xffffffffffff (the
* broadcast address) but with an ADI equal to each IPv4 address in host
* byte order. This converts ARP from a non-scalable broadcast protocol to
* a scalable multicast protocol with perfect address specificity.</p>
*
* <p>If this is not done, ARP will not work reliably.</p>
*
* <p>Multiple calls to subscribe to the same multicast address will have no
* effect. It is perfectly safe to do this.</p>
*
* <p>This does not generate an update call to the {@link VirtualNetworkConfigListener#onNetworkConfigurationUpdated} method.</p>
*
* @param nwid 64-bit network ID
* @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
* @return OK (0) or error code if a fatal error condition has occurred
*/
public ResultCode multicastSubscribe(
long nwid,
long multicastGroup) {
return multicastSubscribe(nodeId, nwid, multicastGroup, 0);
}
/**
* Subscribe to an Ethernet multicast group
*
* <p>ADI stands for additional distinguishing information. This defaults to zero
* and is rarely used. Right now its only use is to enable IPv4 ARP to scale,
* and this must be done.</p>
*
* <p>For IPv4 ARP, the implementation must subscribe to 0xffffffffffff (the
* broadcast address) but with an ADI equal to each IPv4 address in host
* byte order. This converts ARP from a non-scalable broadcast protocol to
* a scalable multicast protocol with perfect address specificity.</p>
*
* <p>If this is not done, ARP will not work reliably.</p>
*
* <p>Multiple calls to subscribe to the same multicast address will have no
* effect. It is perfectly safe to do this.</p>
*
* <p>This does not generate an update call to the {@link VirtualNetworkConfigListener#onNetworkConfigurationUpdated} method.</p>
*
* @param nwid 64-bit network ID
* @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
* @param multicastAdi Multicast ADI (least significant 32 bits only, default: 0)
* @return OK (0) or error code if a fatal error condition has occurred
*/
public ResultCode multicastSubscribe(
long nwid,
long multicastGroup,
long multicastAdi) {
return multicastSubscribe(nodeId, nwid, multicastGroup, multicastAdi);
}
/**
* Unsubscribe from an Ethernet multicast group (or all groups)
*
* <p>If multicastGroup is zero (0), this will unsubscribe from all groups. If
* you are not subscribed to a group this has no effect.</p>
*
* <p>This does not generate an update call to the {@link VirtualNetworkConfigListener#onNetworkConfigurationUpdated} method.</p>
*
* @param nwid 64-bit network ID
* @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
* @return OK (0) or error code if a fatal error condition has occurred
*/
public ResultCode multicastUnsubscribe(
long nwid,
long multicastGroup) {
return multicastUnsubscribe(nodeId, nwid, multicastGroup, 0);
}
/**
* Unsubscribe from an Ethernet multicast group (or all groups)
*
* <p>If multicastGroup is zero (0), this will unsubscribe from all groups. If
* you are not subscribed to a group this has no effect.</p>
*
* <p>This does not generate an update call to the {@link VirtualNetworkConfigListener#onNetworkConfigurationUpdated} method.</p>
*
* <p>ADI stands for additional distinguishing information. This defaults to zero
* and is rarely used. Right now its only use is to enable IPv4 ARP to scale,
* and this must be done.</p>
*
* @param nwid 64-bit network ID
* @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
* @param multicastAdi Multicast ADI (least significant 32 bits only, default: 0)
* @return OK (0) or error code if a fatal error condition has occurred
*/
public ResultCode multicastUnsubscribe(
long nwid,
long multicastGroup,
long multicastAdi) {
return multicastUnsubscribe(nodeId, nwid, multicastGroup, multicastAdi);
}
/**
* Add or update a moon
*
* Moons are persisted in the data store in moons.d/, so this can persist
* across invocations if the contents of moon.d are scanned and orbit is
* called for each on startup.
*
* @param moonWorldId Moon's world ID
* @param moonSeed If non-zero, the ZeroTier address of any member of the moon to query for moon definition
* @return Error if moon was invalid or failed to be added
*/
public ResultCode orbit(
long moonWorldId,
long moonSeed) {
return orbit(nodeId, moonWorldId, moonSeed);
}
/**
* Remove a moon (does nothing if not present)
*
* @param moonWorldId World ID of moon to remove
* @return Error if anything bad happened
*/
public ResultCode deorbit(
long moonWorldId) {
return deorbit(nodeId, moonWorldId);
}
/**
* Get this node's 40-bit ZeroTier address
*
* @return ZeroTier address (least significant 40 bits of 64-bit int)
*/
public long address() {
return address(nodeId);
}
/**
* Get the status of this node
*
* @return @{link NodeStatus} struct with the current node status.
*/
public NodeStatus status() {
return status(nodeId);
}
/**
* Get a list of known peer nodes
*
* @return List of known peers or NULL on failure
*/
public Peer[] peers() {
return peers(nodeId);
}
/**
* Get the status of a virtual network
*
* @param nwid 64-bit network ID
* @return {@link VirtualNetworkConfig} or NULL if we are not a member of this network
*/
public VirtualNetworkConfig networkConfig(long nwid) {
return networkConfig(nodeId, nwid);
}
/**
* Enumerate and get status of all networks
*
* @return List of networks or NULL on failure
*/
public VirtualNetworkConfig[] networkConfigs() {
return networkConfigs(nodeId);
}
/**
* Get ZeroTier One version
*
* @return {@link Version} object with ZeroTierOne version information.
*/
public Version getVersion() {
return version();
}
//
// function declarations for JNI
//
private native ResultCode node_init(
long nodeId,
DataStoreGetListener dataStoreGetListener,
DataStorePutListener dataStorePutListener,
PacketSender packetSender,
EventListener eventListener,
VirtualNetworkFrameListener virtualNetworkFrameListener,
VirtualNetworkConfigListener virtualNetworkConfigListener,
PathChecker pathChecker);
private native boolean node_isInited(long nodeId);
private native void node_delete(long nodeId);
private native ResultCode processVirtualNetworkFrame(
long nodeId,
long now,
long nwid,
long sourceMac,
long destMac,
int etherType,
int vlanId,
byte[] frameData,
long[] nextBackgroundTaskDeadline);
private native ResultCode processWirePacket(
long nodeId,
long now,
long localSocket,
InetSocketAddress remoteAddress,
byte[] packetData,
long[] nextBackgroundTaskDeadline);
private native ResultCode processBackgroundTasks(
long nodeId,
long now,
long[] nextBackgroundTaskDeadline);
private native ResultCode join(long nodeId, long nwid);
private native ResultCode leave(long nodeId, long nwid);
private native ResultCode multicastSubscribe(
long nodeId,
long nwid,
long multicastGroup,
long multicastAdi);
private native ResultCode multicastUnsubscribe(
long nodeId,
long nwid,
long multicastGroup,
long multicastAdi);
private native ResultCode orbit(
long nodeId,
long moonWorldId,
long moonSeed);
private native ResultCode deorbit(
long nodeId,
long moonWorldId);
private native long address(long nodeId);
private native NodeStatus status(long nodeId);
private native VirtualNetworkConfig networkConfig(long nodeId, long nwid);
private native Version version();
private native Peer[] peers(long nodeId);
private native VirtualNetworkConfig[] networkConfigs(long nodeId);
}