Node.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.zerotierone.sdk;
  28. import java.nio.ByteBuffer;
  29. import java.lang.Long;
  30. import java.net.InetAddress;
  31. import java.util.ArrayList;
  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. * -1 if the node has already been closed
  42. */
  43. private long nodeId;
  44. private final DataStoreGetListener getListener;
  45. private final DataStorePutListener putListener;
  46. private final PacketSender sender;
  47. private final EventListener eventListener;
  48. private final VirtualNetworkFrameListener frameListener;
  49. private final VirtualNetworkConfigListener configListener;
  50. public Node(long now,
  51. DataStoreGetListener getListener,
  52. DataStorePutListener putListener,
  53. PacketSender sender,
  54. EventListener eventListener,
  55. VirtualNetworkFrameListener frameListener,
  56. VirtualNetworkConfigListener configListener)
  57. {
  58. this.nodeId = now;
  59. this.getListener = getListener;
  60. this.putListener = putListener;
  61. this.sender = sender;
  62. this.eventListener = eventListener;
  63. this.frameListener = frameListener;
  64. this.configListener = configListener;
  65. ResultCode rc = node_init(now);
  66. if(rc != ResultCode.RESULT_OK)
  67. {
  68. // TODO: Throw Exception
  69. }
  70. }
  71. public void close() {
  72. if(nodeId != -1) {
  73. node_delete(nodeId);
  74. nodeId = -1;
  75. }
  76. }
  77. @Override
  78. protected void finalize() {
  79. close();
  80. }
  81. public ResultCode processVirtualNetworkFrame(
  82. long now,
  83. long nwid,
  84. long sourceMac,
  85. long destMac,
  86. int etherType,
  87. int vlanId,
  88. byte[] frameData,
  89. long[] nextBackgroundTaskDeadline) {
  90. return processVirtualNetworkFrame(
  91. nodeId, now, nwid, sourceMac, destMac, etherType, vlanId,
  92. frameData, nextBackgroundTaskDeadline);
  93. }
  94. public ResultCode processWirePacket(
  95. long now,
  96. InetAddress remoteAddress,
  97. int linkDesperation,
  98. byte[] packetData,
  99. long[] nextBackgroundTaskDeadline) {
  100. return processWirePacket(
  101. nodeId, now, remoteAddress, linkDesperation, packetData,
  102. nextBackgroundTaskDeadline);
  103. }
  104. public ResultCode processBackgroundTasks(long now, long[] nextBackgroundTaskDeadline) {
  105. return processBackgroundTasks(nodeId, now, nextBackgroundTaskDeadline);
  106. }
  107. public ResultCode join(long nwid) {
  108. return join(nodeId, nwid);
  109. }
  110. public ResultCode leave(long nwid) {
  111. return leave(nodeId, nwid);
  112. }
  113. public ResultCode multicastSubscribe(
  114. long nwid,
  115. long multicastGroup) {
  116. return multicastSubscribe(nodeId, nwid, multicastGroup, 0);
  117. }
  118. public ResultCode multicastSubscribe(
  119. long nwid,
  120. long multicastGroup,
  121. long multicastAdi) {
  122. return multicastSubscribe(nodeId, nwid, multicastGroup, multicastAdi);
  123. }
  124. public ResultCode multicastUnsubscribe(
  125. long nwid,
  126. long multicastGroup) {
  127. return multicastUnsubscribe(nodeId, nwid, multicastGroup, 0);
  128. }
  129. public ResultCode multicastUnsubscribe(
  130. long nwid,
  131. long multicastGroup,
  132. long multicastAdi) {
  133. return multicastUnsubscribe(nodeId, nwid, multicastGroup, multicastAdi);
  134. }
  135. public long address() {
  136. return address(nodeId);
  137. }
  138. public NodeStatus status() {
  139. return status(nodeId);
  140. }
  141. public ArrayList<Peer> peers() {
  142. return peers(nodeId);
  143. }
  144. public VirtualNetworkConfig networkConfig(long nwid) {
  145. return networkConfig(nodeId, nwid);
  146. }
  147. public ArrayList<VirtualNetworkConfig> networks() {
  148. return networks(nodeId);
  149. }
  150. public Version getVersion() {
  151. return version();
  152. }
  153. /**
  154. * function declarations for JNI
  155. */
  156. private native ResultCode node_init(long now);
  157. private native void node_delete(long nodeId);
  158. private native ResultCode processVirtualNetworkFrame(
  159. long nodeId,
  160. long now,
  161. long nwid,
  162. long sourceMac,
  163. long destMac,
  164. int etherType,
  165. int vlanId,
  166. byte[] frameData,
  167. long[] nextBackgroundTaskDeadline);
  168. private native ResultCode processWirePacket(
  169. long nodeId,
  170. long now,
  171. InetAddress remoteAddress,
  172. int linkDesperation,
  173. byte[] packetData,
  174. long[] nextBackgroundTaskDeadline);
  175. private native ResultCode processBackgroundTasks(
  176. long nodeId,
  177. long now,
  178. long[] nextBackgroundTaskDeadline);
  179. private native ResultCode join(long nodeId, long nwid);
  180. private native ResultCode leave(long nodeId, long nwid);
  181. private native ResultCode multicastSubscribe(
  182. long nodeId,
  183. long nwid,
  184. long multicastGroup,
  185. long multicastAdi);
  186. private native ResultCode multicastUnsubscribe(
  187. long nodeId,
  188. long nwid,
  189. long multicastGroup,
  190. long multicastAdi);
  191. private native long address(long nodeId);
  192. private native NodeStatus status(long nodeId);
  193. private native VirtualNetworkConfig networkConfig(long nodeId, long nwid);
  194. private native Version version();
  195. private native ArrayList<Peer> peers(long nodeId);
  196. private native ArrayList<VirtualNetworkConfig> networks(long nodeId);
  197. }