Node.java 16 KB

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