Node.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. package com.zerotier.sdk;
  28. import java.net.InetSocketAddress;
  29. import java.util.ArrayList;
  30. import java.io.IOException;
  31. /**
  32. * A ZeroTier One node
  33. */
  34. public class Node {
  35. static {
  36. try {
  37. System.loadLibrary("ZeroTierOneJNI");
  38. } catch (UnsatisfiedLinkError e) {
  39. try {
  40. if(System.getProperty("os.name").startsWith("Windows")) {
  41. System.out.println("Arch: " + System.getProperty("sun.arch.data.model"));
  42. if(System.getProperty("sun.arch.data.model").equals("64")) {
  43. NativeUtils.loadLibraryFromJar("/lib/ZeroTierOneJNI_win64.dll");
  44. } else {
  45. NativeUtils.loadLibraryFromJar("/lib/ZeroTierOneJNI_win32.dll");
  46. }
  47. } else if(System.getProperty("os.name").startsWith("Mac")) {
  48. NativeUtils.loadLibraryFromJar("/lib/libZeroTierOneJNI.jnilib");
  49. } else {
  50. // TODO: Linux
  51. }
  52. } catch (IOException ioe) {
  53. ioe.printStackTrace();
  54. }
  55. }
  56. }
  57. private static final String TAG = "NODE";
  58. /**
  59. * Node ID for JNI purposes.
  60. * Currently set to the now value passed in at the constructor
  61. *
  62. * -1 if the node has already been closed
  63. */
  64. private long nodeId;
  65. private final DataStoreGetListener getListener;
  66. private final DataStorePutListener putListener;
  67. private final PacketSender sender;
  68. private final EventListener eventListener;
  69. private final VirtualNetworkFrameListener frameListener;
  70. private final VirtualNetworkConfigListener configListener;
  71. private final PathChecker pathChecker;
  72. /**
  73. * Create a new ZeroTier One node
  74. *
  75. * <p>Note that this can take a few seconds the first time it's called, as it
  76. * will generate an identity.</p>
  77. *
  78. * @param now Current clock in milliseconds
  79. * @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.
  80. * @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.
  81. * @param sender
  82. * @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.
  83. * @param frameListener
  84. * @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.
  85. * @param pathChecker User written instance of the {@link PathChecker} interface. Not required and can be null.
  86. */
  87. public Node(long now,
  88. DataStoreGetListener getListener,
  89. DataStorePutListener putListener,
  90. PacketSender sender,
  91. EventListener eventListener,
  92. VirtualNetworkFrameListener frameListener,
  93. VirtualNetworkConfigListener configListener,
  94. PathChecker pathChecker) throws NodeException
  95. {
  96. this.nodeId = now;
  97. this.getListener = getListener;
  98. this.putListener = putListener;
  99. this.sender = sender;
  100. this.eventListener = eventListener;
  101. this.frameListener = frameListener;
  102. this.configListener = configListener;
  103. this.pathChecker = pathChecker;
  104. ResultCode rc = node_init(now);
  105. if(rc != ResultCode.RESULT_OK)
  106. {
  107. // TODO: Throw Exception
  108. throw new NodeException(rc.toString());
  109. }
  110. }
  111. /**
  112. * Close this Node.
  113. *
  114. * <p>The Node object can no longer be used once this method is called.</p>
  115. */
  116. public void close() {
  117. if(nodeId != -1) {
  118. node_delete(nodeId);
  119. nodeId = -1;
  120. }
  121. }
  122. @Override
  123. protected void finalize() {
  124. close();
  125. }
  126. /**
  127. * Process a frame from a virtual network port
  128. *
  129. * @param now Current clock in milliseconds
  130. * @param nwid ZeroTier 64-bit virtual network ID
  131. * @param sourceMac Source MAC address (least significant 48 bits)
  132. * @param destMac Destination MAC address (least significant 48 bits)
  133. * @param etherType 16-bit Ethernet frame type
  134. * @param vlanId 10-bit VLAN ID or 0 if none
  135. * @param frameData Frame payload data
  136. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  137. * @return OK (0) or error code if a fatal error condition has occurred
  138. */
  139. public ResultCode processVirtualNetworkFrame(
  140. long now,
  141. long nwid,
  142. long sourceMac,
  143. long destMac,
  144. int etherType,
  145. int vlanId,
  146. byte[] frameData,
  147. long[] nextBackgroundTaskDeadline) {
  148. return processVirtualNetworkFrame(
  149. nodeId, now, nwid, sourceMac, destMac, etherType, vlanId,
  150. frameData, nextBackgroundTaskDeadline);
  151. }
  152. /**
  153. * Process a packet received from the physical wire
  154. *
  155. * @param now Current clock in milliseconds
  156. * @param remoteAddress Origin of packet
  157. * @param packetData Packet data
  158. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  159. * @return OK (0) or error code if a fatal error condition has occurred
  160. */
  161. public ResultCode processWirePacket(
  162. long now,
  163. long localSocket,
  164. InetSocketAddress remoteAddress,
  165. byte[] packetData,
  166. long[] nextBackgroundTaskDeadline) {
  167. return processWirePacket(
  168. nodeId, now, localSocket, remoteAddress, packetData,
  169. nextBackgroundTaskDeadline);
  170. }
  171. /**
  172. * Perform periodic background operations
  173. *
  174. * @param now Current clock in milliseconds
  175. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  176. * @return OK (0) or error code if a fatal error condition has occurred
  177. */
  178. public ResultCode processBackgroundTasks(long now, long[] nextBackgroundTaskDeadline) {
  179. return processBackgroundTasks(nodeId, now, nextBackgroundTaskDeadline);
  180. }
  181. /**
  182. * Join a network
  183. *
  184. * <p>This may generate calls to the port config callback before it returns,
  185. * or these may be deferred if a netconf is not available yet.</p>
  186. *
  187. * <p>If we are already a member of the network, nothing is done and OK is
  188. * returned.</p>
  189. *
  190. * @param nwid 64-bit ZeroTier network ID
  191. * @return OK (0) or error code if a fatal error condition has occurred
  192. */
  193. public ResultCode join(long nwid) {
  194. return join(nodeId, nwid);
  195. }
  196. /**
  197. * Leave a network
  198. *
  199. * <p>If a port has been configured for this network this will generate a call
  200. * to the port config callback with a NULL second parameter to indicate that
  201. * the port is now deleted.</p>
  202. *
  203. * @param nwid 64-bit network ID
  204. * @return OK (0) or error code if a fatal error condition has occurred
  205. */
  206. public ResultCode leave(long nwid) {
  207. return leave(nodeId, nwid);
  208. }
  209. /**
  210. * Subscribe to an Ethernet multicast group
  211. *
  212. * <p>For IPv4 ARP, the implementation must subscribe to 0xffffffffffff (the
  213. * broadcast address) but with an ADI equal to each IPv4 address in host
  214. * byte order. This converts ARP from a non-scalable broadcast protocol to
  215. * a scalable multicast protocol with perfect address specificity.</p>
  216. *
  217. * <p>If this is not done, ARP will not work reliably.</p>
  218. *
  219. * <p>Multiple calls to subscribe to the same multicast address will have no
  220. * effect. It is perfectly safe to do this.</p>
  221. *
  222. * <p>This does not generate an update call to the {@link VirtualNetworkConfigListener#onNetworkConfigurationUpdated} method.</p>
  223. *
  224. * @param nwid 64-bit network ID
  225. * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
  226. * @return OK (0) or error code if a fatal error condition has occurred
  227. */
  228. public ResultCode multicastSubscribe(
  229. long nwid,
  230. long multicastGroup) {
  231. return multicastSubscribe(nodeId, nwid, multicastGroup, 0);
  232. }
  233. /**
  234. * Subscribe to an Ethernet multicast group
  235. *
  236. * <p>ADI stands for additional distinguishing information. This defaults to zero
  237. * and is rarely used. Right now its only use is to enable IPv4 ARP to scale,
  238. * and this must be done.</p>
  239. *
  240. * <p>For IPv4 ARP, the implementation must subscribe to 0xffffffffffff (the
  241. * broadcast address) but with an ADI equal to each IPv4 address in host
  242. * byte order. This converts ARP from a non-scalable broadcast protocol to
  243. * a scalable multicast protocol with perfect address specificity.</p>
  244. *
  245. * <p>If this is not done, ARP will not work reliably.</p>
  246. *
  247. * <p>Multiple calls to subscribe to the same multicast address will have no
  248. * effect. It is perfectly safe to do this.</p>
  249. *
  250. * <p>This does not generate an update call to the {@link VirtualNetworkConfigListener#onNetworkConfigurationUpdated} method.</p>
  251. *
  252. * @param nwid 64-bit network ID
  253. * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
  254. * @param multicastAdi Multicast ADI (least significant 32 bits only, default: 0)
  255. * @return OK (0) or error code if a fatal error condition has occurred
  256. */
  257. public ResultCode multicastSubscribe(
  258. long nwid,
  259. long multicastGroup,
  260. long multicastAdi) {
  261. return multicastSubscribe(nodeId, nwid, multicastGroup, multicastAdi);
  262. }
  263. /**
  264. * Unsubscribe from an Ethernet multicast group (or all groups)
  265. *
  266. * <p>If multicastGroup is zero (0), this will unsubscribe from all groups. If
  267. * you are not subscribed to a group this has no effect.</p>
  268. *
  269. * <p>This does not generate an update call to the {@link VirtualNetworkConfigListener#onNetworkConfigurationUpdated} method.</p>
  270. *
  271. * @param nwid 64-bit network ID
  272. * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
  273. * @return OK (0) or error code if a fatal error condition has occurred
  274. */
  275. public ResultCode multicastUnsubscribe(
  276. long nwid,
  277. long multicastGroup) {
  278. return multicastUnsubscribe(nodeId, nwid, multicastGroup, 0);
  279. }
  280. /**
  281. * Unsubscribe from an Ethernet multicast group (or all groups)
  282. *
  283. * <p>If multicastGroup is zero (0), this will unsubscribe from all groups. If
  284. * you are not subscribed to a group this has no effect.</p>
  285. *
  286. * <p>This does not generate an update call to the {@link VirtualNetworkConfigListener#onNetworkConfigurationUpdated} method.</p>
  287. *
  288. * <p>ADI stands for additional distinguishing information. This defaults to zero
  289. * and is rarely used. Right now its only use is to enable IPv4 ARP to scale,
  290. * and this must be done.</p>
  291. *
  292. * @param nwid 64-bit network ID
  293. * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
  294. * @param multicastAdi Multicast ADI (least significant 32 bits only, default: 0)
  295. * @return OK (0) or error code if a fatal error condition has occurred
  296. */
  297. public ResultCode multicastUnsubscribe(
  298. long nwid,
  299. long multicastGroup,
  300. long multicastAdi) {
  301. return multicastUnsubscribe(nodeId, nwid, multicastGroup, multicastAdi);
  302. }
  303. /**
  304. * Add or update a moon
  305. *
  306. * Moons are persisted in the data store in moons.d/, so this can persist
  307. * across invocations if the contents of moon.d are scanned and orbit is
  308. * called for each on startup.
  309. *
  310. * @param moonWorldId Moon's world ID
  311. * @param moonSeed If non-zero, the ZeroTier address of any member of the moon to query for moon definition
  312. * @return Error if moon was invalid or failed to be added
  313. */
  314. public ResultCode orbit(
  315. long moonWorldId,
  316. long moonSeed) {
  317. return orbit(nodeId, moonWorldId, moonSeed);
  318. }
  319. /**
  320. * Remove a moon (does nothing if not present)
  321. *
  322. * @param moonWorldId World ID of moon to remove
  323. * @return Error if anything bad happened
  324. */
  325. public ResultCode deorbit(
  326. long moonWorldId) {
  327. return deorbit(nodeId, moonWorldId);
  328. }
  329. /**
  330. * Get this node's 40-bit ZeroTier address
  331. *
  332. * @return ZeroTier address (least significant 40 bits of 64-bit int)
  333. */
  334. public long address() {
  335. return address(nodeId);
  336. }
  337. /**
  338. * Get the status of this node
  339. *
  340. * @return @{link NodeStatus} struct with the current node status.
  341. */
  342. public NodeStatus status() {
  343. return status(nodeId);
  344. }
  345. /**
  346. * Get a list of known peer nodes
  347. *
  348. * @return List of known peers or NULL on failure
  349. */
  350. public Peer[] peers() {
  351. return peers(nodeId);
  352. }
  353. /**
  354. * Get the status of a virtual network
  355. *
  356. * @param nwid 64-bit network ID
  357. * @return {@link VirtualNetworkConfig} or NULL if we are not a member of this network
  358. */
  359. public VirtualNetworkConfig networkConfig(long nwid) {
  360. return networkConfig(nodeId, nwid);
  361. }
  362. /**
  363. * Enumerate and get status of all networks
  364. *
  365. * @return List of networks or NULL on failure
  366. */
  367. public VirtualNetworkConfig[] networks() {
  368. return networks(nodeId);
  369. }
  370. /**
  371. * Get ZeroTier One version
  372. *
  373. * @return {@link Version} object with ZeroTierOne version information.
  374. */
  375. public Version getVersion() {
  376. return version();
  377. }
  378. //
  379. // function declarations for JNI
  380. //
  381. private native ResultCode node_init(long now);
  382. private native void node_delete(long nodeId);
  383. private native ResultCode processVirtualNetworkFrame(
  384. long nodeId,
  385. long now,
  386. long nwid,
  387. long sourceMac,
  388. long destMac,
  389. int etherType,
  390. int vlanId,
  391. byte[] frameData,
  392. long[] nextBackgroundTaskDeadline);
  393. private native ResultCode processWirePacket(
  394. long nodeId,
  395. long now,
  396. long localSocket,
  397. InetSocketAddress remoteAddress,
  398. byte[] packetData,
  399. long[] nextBackgroundTaskDeadline);
  400. private native ResultCode processBackgroundTasks(
  401. long nodeId,
  402. long now,
  403. long[] nextBackgroundTaskDeadline);
  404. private native ResultCode join(long nodeId, long nwid);
  405. private native ResultCode leave(long nodeId, long nwid);
  406. private native ResultCode multicastSubscribe(
  407. long nodeId,
  408. long nwid,
  409. long multicastGroup,
  410. long multicastAdi);
  411. private native ResultCode multicastUnsubscribe(
  412. long nodeId,
  413. long nwid,
  414. long multicastGroup,
  415. long multicastAdi);
  416. private native ResultCode orbit(
  417. long nodeId,
  418. long moonWorldId,
  419. long moonSeed);
  420. private native ResultCode deorbit(
  421. long nodeId,
  422. long moonWorldId);
  423. private native long address(long nodeId);
  424. private native NodeStatus status(long nodeId);
  425. private native VirtualNetworkConfig networkConfig(long nodeId, long nwid);
  426. private native Version version();
  427. private native Peer[] peers(long nodeId);
  428. private native VirtualNetworkConfig[] networks(long nodeId);
  429. }