com_zerotierone_sdk_Node.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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. #include "com_zerotierone_sdk_Node.h"
  28. #include "ZT_jnicache.h"
  29. #include "ZT_jniutils.h"
  30. #include <ZeroTierOne.h>
  31. #include "Mutex.hpp"
  32. #include <map>
  33. #include <string>
  34. #include <cassert>
  35. #include <cstring>
  36. #define LOG_TAG "Node"
  37. namespace {
  38. struct JniRef
  39. {
  40. JniRef()
  41. : jvm(NULL)
  42. , node(NULL)
  43. , dataStoreGetListener(NULL)
  44. , dataStorePutListener(NULL)
  45. , packetSender(NULL)
  46. , eventListener(NULL)
  47. , frameListener(NULL)
  48. , configListener(NULL)
  49. , pathChecker(NULL)
  50. , callbacks(NULL)
  51. , inited()
  52. {
  53. callbacks = (ZT_Node_Callbacks*)malloc(sizeof(ZT_Node_Callbacks));
  54. memset(callbacks, 0, sizeof(ZT_Node_Callbacks));
  55. }
  56. ~JniRef()
  57. {
  58. JNIEnv *env = NULL;
  59. jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  60. env->DeleteGlobalRef(dataStoreGetListener);
  61. env->DeleteGlobalRef(dataStorePutListener);
  62. env->DeleteGlobalRef(packetSender);
  63. env->DeleteGlobalRef(eventListener);
  64. env->DeleteGlobalRef(frameListener);
  65. env->DeleteGlobalRef(configListener);
  66. env->DeleteGlobalRef(pathChecker);
  67. free(callbacks);
  68. callbacks = NULL;
  69. }
  70. int64_t id;
  71. JavaVM *jvm;
  72. ZT_Node *node;
  73. jobject dataStoreGetListener;
  74. jobject dataStorePutListener;
  75. jobject packetSender;
  76. jobject eventListener;
  77. jobject frameListener;
  78. jobject configListener;
  79. jobject pathChecker;
  80. ZT_Node_Callbacks *callbacks;
  81. bool inited;
  82. bool finishInitializing();
  83. };
  84. int VirtualNetworkConfigFunctionCallback(
  85. ZT_Node *node,
  86. void *userData,
  87. void *threadData,
  88. uint64_t nwid,
  89. void **,
  90. enum ZT_VirtualNetworkConfigOperation operation,
  91. const ZT_VirtualNetworkConfig *config)
  92. {
  93. LOGV("VirtualNetworkConfigFunctionCallback");
  94. JniRef *ref = (JniRef*)userData;
  95. JNIEnv *env = NULL;
  96. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  97. if (ref->configListener == NULL) {
  98. LOGE("configListener is NULL");
  99. return -100;
  100. }
  101. jobject operationObject = createVirtualNetworkConfigOperation(env, operation);
  102. if(env->ExceptionCheck() || operationObject == NULL)
  103. {
  104. return -101;
  105. }
  106. if (config == NULL) {
  107. LOGE("Config is NULL");
  108. return -102;
  109. }
  110. jobject networkConfigObject = newNetworkConfig(env, *config);
  111. if(env->ExceptionCheck() || networkConfigObject == NULL)
  112. {
  113. return -103;
  114. }
  115. return env->CallIntMethod(
  116. ref->configListener,
  117. VirtualNetworkConfigListener_onNetworkConfigurationUpdated_method,
  118. (jlong)nwid, operationObject, networkConfigObject);
  119. }
  120. void VirtualNetworkFrameFunctionCallback(ZT_Node *node,
  121. void *userData,
  122. void *threadData,
  123. uint64_t nwid,
  124. void**,
  125. uint64_t sourceMac,
  126. uint64_t destMac,
  127. unsigned int etherType,
  128. unsigned int vlanid,
  129. const void *frameData,
  130. unsigned int frameLength)
  131. {
  132. LOGV("VirtualNetworkFrameFunctionCallback");
  133. #ifndef NDEBUG
  134. unsigned char* local = (unsigned char*)frameData;
  135. LOGV("Type Bytes: 0x%02x%02x", local[12], local[13]);
  136. #endif
  137. JniRef *ref = (JniRef*)userData;
  138. assert(ref->node == node);
  139. JNIEnv *env = NULL;
  140. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  141. if (ref->frameListener == NULL) {
  142. LOGE("frameListener is NULL");
  143. return;
  144. }
  145. const unsigned char *bytes = static_cast<const unsigned char*>(frameData);
  146. jbyteArray dataArray = newByteArray(env, bytes, frameLength);
  147. if(env->ExceptionCheck() || dataArray == NULL)
  148. {
  149. return;
  150. }
  151. env->CallVoidMethod(ref->frameListener, VirtualNetworkFrameListener_onVirtualNetworkFrame_method, (jlong)nwid, (jlong)sourceMac, (jlong)destMac, (jlong)etherType, (jlong)vlanid, dataArray);
  152. }
  153. void EventCallback(ZT_Node *node,
  154. void *userData,
  155. void *threadData,
  156. enum ZT_Event event,
  157. const void *data) {
  158. LOGV("EventCallback");
  159. JniRef *ref = (JniRef *) userData;
  160. if (ref->node != node && event != ZT_EVENT_UP) {
  161. LOGE("Nodes not equal. ref->node %p, node %p. Event: %d", ref->node, node, event);
  162. return;
  163. }
  164. JNIEnv *env = NULL;
  165. ref->jvm->GetEnv((void **) &env, JNI_VERSION_1_6);
  166. if (ref->eventListener == NULL) {
  167. LOGE("eventListener is NULL");
  168. return;
  169. }
  170. jobject eventObject = createEvent(env, event);
  171. if (env->ExceptionCheck() || eventObject == NULL) {
  172. return;
  173. }
  174. switch (event) {
  175. case ZT_EVENT_UP: {
  176. LOGD("Event Up");
  177. env->CallVoidMethod(ref->eventListener, EventListener_onEvent_method, eventObject);
  178. break;
  179. }
  180. case ZT_EVENT_OFFLINE: {
  181. LOGD("Event Offline");
  182. env->CallVoidMethod(ref->eventListener, EventListener_onEvent_method, eventObject);
  183. break;
  184. }
  185. case ZT_EVENT_ONLINE: {
  186. LOGD("Event Online");
  187. env->CallVoidMethod(ref->eventListener, EventListener_onEvent_method, eventObject);
  188. break;
  189. }
  190. case ZT_EVENT_DOWN: {
  191. LOGD("Event Down");
  192. env->CallVoidMethod(ref->eventListener, EventListener_onEvent_method, eventObject);
  193. break;
  194. }
  195. case ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION: {
  196. LOGV("Identity Collision");
  197. // call onEvent()
  198. env->CallVoidMethod(ref->eventListener, EventListener_onEvent_method, eventObject);
  199. }
  200. break;
  201. case ZT_EVENT_TRACE: {
  202. LOGV("Trace Event");
  203. // call onTrace()
  204. if (data == NULL) {
  205. break;
  206. }
  207. const char *message = (const char *) data;
  208. jstring messageStr = env->NewStringUTF(message);
  209. env->CallVoidMethod(ref->eventListener, EventListener_onTrace_method, messageStr);
  210. }
  211. break;
  212. case ZT_EVENT_USER_MESSAGE:
  213. case ZT_EVENT_REMOTE_TRACE:
  214. default:
  215. break;
  216. }
  217. }
  218. void StatePutFunction(
  219. ZT_Node *node,
  220. void *userData,
  221. void *threadData,
  222. enum ZT_StateObjectType type,
  223. const uint64_t id[2],
  224. const void *buffer,
  225. int bufferLength) {
  226. char p[4096] = {0};
  227. bool secure = false;
  228. switch (type) {
  229. case ZT_STATE_OBJECT_IDENTITY_PUBLIC:
  230. snprintf(p, sizeof(p), "identity.public");
  231. break;
  232. case ZT_STATE_OBJECT_IDENTITY_SECRET:
  233. snprintf(p, sizeof(p), "identity.secret");
  234. secure = true;
  235. break;
  236. case ZT_STATE_OBJECT_PLANET:
  237. snprintf(p, sizeof(p), "planet");
  238. break;
  239. case ZT_STATE_OBJECT_MOON:
  240. snprintf(p, sizeof(p), "moons.d/%.16llx.moon", (unsigned long long)id[0]);
  241. break;
  242. case ZT_STATE_OBJECT_NETWORK_CONFIG:
  243. snprintf(p, sizeof(p), "networks.d/%.16llx.conf", (unsigned long long)id[0]);
  244. break;
  245. case ZT_STATE_OBJECT_PEER:
  246. snprintf(p, sizeof(p), "peers.d/%.10llx", (unsigned long long)id[0]);
  247. break;
  248. default:
  249. return;
  250. }
  251. if (strlen(p) < 1) {
  252. return;
  253. }
  254. JniRef *ref = (JniRef*)userData;
  255. JNIEnv *env = NULL;
  256. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  257. if (ref->dataStorePutListener == NULL) {
  258. LOGE("dataStorePutListener is NULL");
  259. return;
  260. }
  261. jstring nameStr = env->NewStringUTF(p);
  262. if (bufferLength >= 0) {
  263. LOGD("JNI: Write file: %s", p);
  264. const unsigned char *bytes = static_cast<const unsigned char *>(buffer);
  265. jbyteArray bufferObj = newByteArray(env, bytes, bufferLength);
  266. if(env->ExceptionCheck() || bufferObj == NULL)
  267. {
  268. return;
  269. }
  270. env->CallIntMethod(ref->dataStorePutListener,
  271. DataStorePutListener_onDataStorePut_method,
  272. nameStr, bufferObj, secure);
  273. } else {
  274. LOGD("JNI: Delete file: %s", p);
  275. env->CallIntMethod(ref->dataStorePutListener, DataStorePutListener_onDelete_method, nameStr);
  276. }
  277. }
  278. int StateGetFunction(
  279. ZT_Node *node,
  280. void *userData,
  281. void *threadData,
  282. ZT_StateObjectType type,
  283. const uint64_t id[2],
  284. void *buffer,
  285. unsigned int bufferLength) {
  286. char p[4096] = {0};
  287. switch (type) {
  288. case ZT_STATE_OBJECT_IDENTITY_PUBLIC:
  289. snprintf(p, sizeof(p), "identity.public");
  290. break;
  291. case ZT_STATE_OBJECT_IDENTITY_SECRET:
  292. snprintf(p, sizeof(p), "identity.secret");
  293. break;
  294. case ZT_STATE_OBJECT_PLANET:
  295. snprintf(p, sizeof(p), "planet");
  296. break;
  297. case ZT_STATE_OBJECT_MOON:
  298. snprintf(p, sizeof(p), "moons.d/%.16llx.moon", (unsigned long long)id[0]);
  299. break;
  300. case ZT_STATE_OBJECT_NETWORK_CONFIG:
  301. snprintf(p, sizeof(p), "networks.d/%.16llx.conf", (unsigned long long)id[0]);
  302. break;
  303. case ZT_STATE_OBJECT_PEER:
  304. snprintf(p, sizeof(p), "peers.d/%.10llx", (unsigned long long)id[0]);
  305. break;
  306. default:
  307. return -100;
  308. }
  309. if (strlen(p) < 1) {
  310. return -101;
  311. }
  312. JniRef *ref = (JniRef*)userData;
  313. JNIEnv *env = NULL;
  314. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  315. if (ref->dataStoreGetListener == NULL) {
  316. LOGE("dataStoreGetListener is NULL");
  317. return -102;
  318. }
  319. jstring nameStr = env->NewStringUTF(p);
  320. if(nameStr == NULL)
  321. {
  322. LOGE("Error creating name string object");
  323. return -103; // out of memory
  324. }
  325. jbyteArray bufferObj = newByteArray(env, bufferLength);
  326. if(env->ExceptionCheck() || bufferObj == NULL)
  327. {
  328. return -104;
  329. }
  330. LOGV("Calling onDataStoreGet(%s, %p)", p, buffer);
  331. int retval = (int)env->CallLongMethod(
  332. ref->dataStoreGetListener,
  333. DataStoreGetListener_onDataStoreGet_method,
  334. nameStr,
  335. bufferObj);
  336. LOGV("onDataStoreGet returned %d", retval);
  337. if(retval > 0)
  338. {
  339. void *data = env->GetPrimitiveArrayCritical(bufferObj, NULL);
  340. memcpy(buffer, data, retval);
  341. env->ReleasePrimitiveArrayCritical(bufferObj, data, 0);
  342. }
  343. return retval;
  344. }
  345. int WirePacketSendFunction(ZT_Node *node,
  346. void *userData,
  347. void *threadData,
  348. int64_t localSocket,
  349. const struct sockaddr_storage *remoteAddress,
  350. const void *buffer,
  351. unsigned int bufferSize,
  352. unsigned int ttl)
  353. {
  354. LOGV("WirePacketSendFunction(%lld, %p, %p, %d, %u)", (long long)localSocket, remoteAddress, buffer, bufferSize, ttl);
  355. JniRef *ref = (JniRef*)userData;
  356. assert(ref->node == node);
  357. JNIEnv *env = NULL;
  358. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  359. if (ref->packetSender == NULL) {
  360. LOGE("packetSender is NULL");
  361. return -100;
  362. }
  363. //
  364. // may be NULL
  365. //
  366. jobject remoteAddressObj = newInetSocketAddress(env, *remoteAddress);
  367. if (env->ExceptionCheck()) {
  368. return -101;
  369. }
  370. const unsigned char *bytes = static_cast<const unsigned char *>(buffer);
  371. jbyteArray bufferObj = newByteArray(env, bytes, bufferSize);
  372. if (env->ExceptionCheck() || bufferObj == NULL)
  373. {
  374. return -102;
  375. }
  376. int retval = env->CallIntMethod(ref->packetSender, PacketSender_onSendPacketRequested_method, localSocket, remoteAddressObj, bufferObj, 0);
  377. LOGV("JNI Packet Sender returned: %d", retval);
  378. return retval;
  379. }
  380. int PathCheckFunction(ZT_Node *node,
  381. void *userPtr,
  382. void *threadPtr,
  383. uint64_t address,
  384. int64_t localSocket,
  385. const struct sockaddr_storage *remoteAddress)
  386. {
  387. JniRef *ref = (JniRef*)userPtr;
  388. assert(ref->node == node);
  389. if(ref->pathChecker == NULL) {
  390. return true;
  391. }
  392. JNIEnv *env = NULL;
  393. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  394. //
  395. // may be NULL
  396. //
  397. jobject remoteAddressObj = newInetSocketAddress(env, *remoteAddress);
  398. if (env->ExceptionCheck()) {
  399. return true;
  400. }
  401. return env->CallBooleanMethod(ref->pathChecker, PathChecker_onPathCheck_method, address, localSocket, remoteAddressObj);
  402. }
  403. int PathLookupFunction(ZT_Node *node,
  404. void *userPtr,
  405. void *threadPtr,
  406. uint64_t address,
  407. int ss_family,
  408. struct sockaddr_storage *result)
  409. {
  410. JniRef *ref = (JniRef*)userPtr;
  411. assert(ref->node == node);
  412. if(ref->pathChecker == NULL) {
  413. return false;
  414. }
  415. JNIEnv *env = NULL;
  416. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  417. jobject sockAddressObject = env->CallObjectMethod(ref->pathChecker, PathChecker_onPathLookup_method, address, ss_family);
  418. if(sockAddressObject == NULL)
  419. {
  420. LOGE("Unable to call onPathLookup implementation");
  421. return false;
  422. }
  423. *result = fromSocketAddressObject(env, sockAddressObject);
  424. if (env->ExceptionCheck() || isSocketAddressEmpty(*result)) {
  425. return false;
  426. }
  427. return true;
  428. }
  429. typedef std::map<int64_t, JniRef*> NodeMap;
  430. NodeMap nodeMap;
  431. ZeroTier::Mutex nodeMapMutex;
  432. bool isInited(int64_t nodeId) {
  433. ZeroTier::Mutex::Lock lock(nodeMapMutex);
  434. NodeMap::iterator found = nodeMap.find(nodeId);
  435. if (found == nodeMap.end()) {
  436. //
  437. // not in map yet, or has been removed from map
  438. //
  439. return false;
  440. }
  441. JniRef *ref = found->second;
  442. assert(ref);
  443. return ref->inited;
  444. }
  445. bool JniRef::finishInitializing() {
  446. ZeroTier::Mutex::Lock lock(nodeMapMutex);
  447. NodeMap::iterator found = nodeMap.find(id);
  448. if (found != nodeMap.end()) {
  449. //
  450. // already in map
  451. //
  452. LOGE("Cannot finish initializing; node is already in map");
  453. return false;
  454. }
  455. nodeMap.insert(std::make_pair(id, this));
  456. assert(!inited);
  457. inited = true;
  458. return true;
  459. }
  460. ZT_Node* findNode(int64_t nodeId)
  461. {
  462. ZeroTier::Mutex::Lock lock(nodeMapMutex);
  463. NodeMap::iterator found = nodeMap.find(nodeId);
  464. assert(found != nodeMap.end());
  465. JniRef *ref = found->second;
  466. assert(ref);
  467. return ref->node;
  468. }
  469. JniRef *removeRef(int64_t nodeId) {
  470. ZeroTier::Mutex::Lock lock(nodeMapMutex);
  471. NodeMap::iterator found = nodeMap.find(nodeId);
  472. if (found == nodeMap.end()) {
  473. return nullptr;
  474. }
  475. JniRef *ref = found->second;
  476. assert(ref);
  477. nodeMap.erase(nodeId);
  478. return ref;
  479. }
  480. }
  481. #ifdef __cplusplus
  482. extern "C" {
  483. #endif
  484. JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
  485. {
  486. setupJNICache(vm);
  487. return JNI_VERSION_1_6;
  488. }
  489. JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
  490. {
  491. teardownJNICache(vm);
  492. }
  493. /*
  494. * Class: com_zerotier_sdk_Node
  495. * Method: node_init
  496. * Signature: (JLcom/zerotier/sdk/DataStoreGetListener;Lcom/zerotier/sdk/DataStorePutListener;Lcom/zerotier/sdk/PacketSender;Lcom/zerotier/sdk/EventListener;Lcom/zerotier/sdk/VirtualNetworkFrameListener;Lcom/zerotier/sdk/VirtualNetworkConfigListener;Lcom/zerotier/sdk/PathChecker;)Lcom/zerotier/sdk/ResultCode;
  497. */
  498. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_node_1init(
  499. JNIEnv *env, jobject obj, jlong now, jobject dataStoreGetListener,
  500. jobject dataStorePutListener, jobject packetSender, jobject eventListener,
  501. jobject frameListener, jobject configListener,
  502. jobject pathChecker)
  503. {
  504. LOGV("Creating ZT_Node struct");
  505. jobject resultObject = ResultCode_RESULT_OK_enum;
  506. ZT_Node *node;
  507. JniRef *ref = new JniRef;
  508. ref->id = (int64_t)now;
  509. env->GetJavaVM(&ref->jvm);
  510. if(dataStoreGetListener == NULL)
  511. {
  512. return NULL;
  513. }
  514. ref->dataStoreGetListener = env->NewGlobalRef(dataStoreGetListener);
  515. if(dataStorePutListener == NULL)
  516. {
  517. return NULL;
  518. }
  519. ref->dataStorePutListener = env->NewGlobalRef(dataStorePutListener);
  520. if(packetSender == NULL)
  521. {
  522. return NULL;
  523. }
  524. ref->packetSender = env->NewGlobalRef(packetSender);
  525. if(frameListener == NULL)
  526. {
  527. return NULL;
  528. }
  529. ref->frameListener = env->NewGlobalRef(frameListener);
  530. if(configListener == NULL)
  531. {
  532. return NULL;
  533. }
  534. ref->configListener = env->NewGlobalRef(configListener);
  535. if(eventListener == NULL)
  536. {
  537. return NULL;
  538. }
  539. ref->eventListener = env->NewGlobalRef(eventListener);
  540. if(pathChecker != NULL)
  541. {
  542. ref->pathChecker = env->NewGlobalRef(pathChecker);
  543. }
  544. ref->callbacks->stateGetFunction = &StateGetFunction;
  545. ref->callbacks->statePutFunction = &StatePutFunction;
  546. ref->callbacks->wirePacketSendFunction = &WirePacketSendFunction;
  547. ref->callbacks->virtualNetworkFrameFunction = &VirtualNetworkFrameFunctionCallback;
  548. ref->callbacks->virtualNetworkConfigFunction = &VirtualNetworkConfigFunctionCallback;
  549. ref->callbacks->eventCallback = &EventCallback;
  550. ref->callbacks->pathCheckFunction = &PathCheckFunction;
  551. ref->callbacks->pathLookupFunction = &PathLookupFunction;
  552. ZT_ResultCode rc = ZT_Node_new(
  553. &node,
  554. ref,
  555. NULL,
  556. ref->callbacks,
  557. (int64_t)now);
  558. if(rc != ZT_RESULT_OK)
  559. {
  560. LOGE("Error creating Node: %d", rc);
  561. resultObject = createResultObject(env, rc);
  562. if (env->ExceptionCheck() || resultObject == NULL) {
  563. return NULL;
  564. }
  565. if(node)
  566. {
  567. ZT_Node_delete(node);
  568. node = NULL;
  569. }
  570. delete ref;
  571. ref = NULL;
  572. return resultObject;
  573. }
  574. //
  575. // node is now updated
  576. //
  577. ref->node = node;
  578. if (!ref->finishInitializing()) {
  579. LOGE("finishInitializing() failed");
  580. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  581. }
  582. return resultObject;
  583. }
  584. /*
  585. * Class: com_zerotier_sdk_Node
  586. * Method: node_isInited
  587. * Signature: (J)Z
  588. */
  589. JNIEXPORT jboolean JNICALL Java_com_zerotier_sdk_Node_node_1isInited
  590. (JNIEnv *env, jobject obj, jlong nodeId) {
  591. return isInited(nodeId);
  592. }
  593. /*
  594. * Class: com_zerotier_sdk_Node
  595. * Method: node_delete
  596. * Signature: (J)V
  597. */
  598. JNIEXPORT void JNICALL Java_com_zerotier_sdk_Node_node_1delete(
  599. JNIEnv *env, jobject obj, jlong id)
  600. {
  601. LOGV("Destroying ZT_Node struct");
  602. int64_t nodeId = (int64_t)id;
  603. JniRef *ref = removeRef(nodeId);
  604. if (!ref) {
  605. return;
  606. }
  607. ZT_Node_delete(ref->node);
  608. delete ref;
  609. }
  610. /*
  611. * Class: com_zerotier_sdk_Node
  612. * Method: processVirtualNetworkFrame
  613. * Signature: (JJJJJII[B[J)Lcom/zerotier/sdk/ResultCode;
  614. */
  615. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processVirtualNetworkFrame(
  616. JNIEnv *env, jobject obj,
  617. jlong id,
  618. jlong in_now,
  619. jlong in_nwid,
  620. jlong in_sourceMac,
  621. jlong in_destMac,
  622. jint in_etherType,
  623. jint in_vlanId,
  624. jbyteArray in_frameData,
  625. jlongArray out_nextBackgroundTaskDeadline)
  626. {
  627. int64_t nodeId = (int64_t) id;
  628. ZT_Node *node = findNode(nodeId);
  629. unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline);
  630. if(nbtd_len < 1)
  631. {
  632. // array for next background task length has 0 elements!
  633. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  634. }
  635. int64_t now = (int64_t)in_now;
  636. uint64_t nwid = (uint64_t)in_nwid;
  637. uint64_t sourceMac = (uint64_t)in_sourceMac;
  638. uint64_t destMac = (uint64_t)in_destMac;
  639. unsigned int etherType = (unsigned int)in_etherType;
  640. unsigned int vlanId = (unsigned int)in_vlanId;
  641. unsigned int frameLength = env->GetArrayLength(in_frameData);
  642. void *frameData = env->GetPrimitiveArrayCritical(in_frameData, NULL);
  643. void *localData = malloc(frameLength);
  644. memcpy(localData, frameData, frameLength);
  645. env->ReleasePrimitiveArrayCritical(in_frameData, frameData, 0);
  646. int64_t nextBackgroundTaskDeadline = 0;
  647. ZT_ResultCode rc = ZT_Node_processVirtualNetworkFrame(
  648. node,
  649. NULL,
  650. now,
  651. nwid,
  652. sourceMac,
  653. destMac,
  654. etherType,
  655. vlanId,
  656. (const void*)localData,
  657. frameLength,
  658. &nextBackgroundTaskDeadline);
  659. free(localData);
  660. jlong *outDeadline = (jlong*)env->GetPrimitiveArrayCritical(out_nextBackgroundTaskDeadline, NULL);
  661. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  662. env->ReleasePrimitiveArrayCritical(out_nextBackgroundTaskDeadline, outDeadline, 0);
  663. return createResultObject(env, rc);
  664. }
  665. /*
  666. * Class: com_zerotier_sdk_Node
  667. * Method: processWirePacket
  668. * Signature: (JJJLjava/net/InetSocketAddress;[B[J)Lcom/zerotier/sdk/ResultCode;
  669. */
  670. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket(
  671. JNIEnv *env, jobject obj,
  672. jlong id,
  673. jlong in_now,
  674. jlong in_localSocket,
  675. jobject in_remoteAddress,
  676. jbyteArray in_packetData,
  677. jlongArray out_nextBackgroundTaskDeadline)
  678. {
  679. int64_t nodeId = (int64_t) id;
  680. ZT_Node *node = findNode(nodeId);
  681. unsigned int nbtd_len = (unsigned int)env->GetArrayLength(out_nextBackgroundTaskDeadline);
  682. if(nbtd_len < 1)
  683. {
  684. LOGE("nbtd_len < 1");
  685. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  686. }
  687. int64_t now = (int64_t)in_now;
  688. sockaddr_storage remoteAddress = fromSocketAddressObject(env, in_remoteAddress);
  689. if (env->ExceptionCheck() || isSocketAddressEmpty(remoteAddress)) {
  690. return NULL;
  691. }
  692. unsigned int packetLength = (unsigned int)env->GetArrayLength(in_packetData);
  693. if(packetLength == 0)
  694. {
  695. LOGE("Empty packet?!?");
  696. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  697. }
  698. void *packetData = env->GetPrimitiveArrayCritical(in_packetData, NULL);
  699. void *localData = malloc(packetLength);
  700. memcpy(localData, packetData, packetLength);
  701. env->ReleasePrimitiveArrayCritical(in_packetData, packetData, 0);
  702. int64_t nextBackgroundTaskDeadline = 0;
  703. ZT_ResultCode rc = ZT_Node_processWirePacket(
  704. node,
  705. NULL,
  706. now,
  707. in_localSocket,
  708. &remoteAddress,
  709. localData,
  710. packetLength,
  711. &nextBackgroundTaskDeadline);
  712. if(rc != ZT_RESULT_OK)
  713. {
  714. LOGE("ZT_Node_processWirePacket returned: %d", rc);
  715. }
  716. free(localData);
  717. jlong *outDeadline = (jlong*)env->GetPrimitiveArrayCritical(out_nextBackgroundTaskDeadline, NULL);
  718. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  719. env->ReleasePrimitiveArrayCritical(out_nextBackgroundTaskDeadline, outDeadline, 0);
  720. return createResultObject(env, rc);
  721. }
  722. /*
  723. * Class: com_zerotier_sdk_Node
  724. * Method: processBackgroundTasks
  725. * Signature: (JJ[J)Lcom/zerotier/sdk/ResultCode;
  726. */
  727. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processBackgroundTasks(
  728. JNIEnv *env, jobject obj,
  729. jlong id,
  730. jlong in_now,
  731. jlongArray out_nextBackgroundTaskDeadline)
  732. {
  733. int64_t nodeId = (int64_t) id;
  734. ZT_Node *node = findNode(nodeId);
  735. unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline);
  736. if(nbtd_len < 1)
  737. {
  738. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  739. }
  740. int64_t now = (int64_t)in_now;
  741. int64_t nextBackgroundTaskDeadline = 0;
  742. ZT_ResultCode rc = ZT_Node_processBackgroundTasks(node, NULL, now, &nextBackgroundTaskDeadline);
  743. jlong *outDeadline = (jlong*)env->GetPrimitiveArrayCritical(out_nextBackgroundTaskDeadline, NULL);
  744. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  745. env->ReleasePrimitiveArrayCritical(out_nextBackgroundTaskDeadline, outDeadline, 0);
  746. return createResultObject(env, rc);
  747. }
  748. /*
  749. * Class: com_zerotier_sdk_Node
  750. * Method: join
  751. * Signature: (JJ)Lcom/zerotier/sdk/ResultCode;
  752. */
  753. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_join(
  754. JNIEnv *env, jobject obj, jlong id, jlong in_nwid)
  755. {
  756. int64_t nodeId = (int64_t) id;
  757. ZT_Node *node = findNode(nodeId);
  758. uint64_t nwid = (uint64_t)in_nwid;
  759. ZT_ResultCode rc = ZT_Node_join(node, nwid, NULL, NULL);
  760. return createResultObject(env, rc);
  761. }
  762. /*
  763. * Class: com_zerotier_sdk_Node
  764. * Method: leave
  765. * Signature: (JJ)Lcom/zerotier/sdk/ResultCode;
  766. */
  767. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_leave(
  768. JNIEnv *env, jobject obj, jlong id, jlong in_nwid)
  769. {
  770. int64_t nodeId = (int64_t) id;
  771. ZT_Node *node = findNode(nodeId);
  772. uint64_t nwid = (uint64_t)in_nwid;
  773. ZT_ResultCode rc = ZT_Node_leave(node, nwid, NULL, NULL);
  774. return createResultObject(env, rc);
  775. }
  776. /*
  777. * Class: com_zerotier_sdk_Node
  778. * Method: multicastSubscribe
  779. * Signature: (JJJJ)Lcom/zerotier/sdk/ResultCode;
  780. */
  781. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastSubscribe(
  782. JNIEnv *env, jobject obj,
  783. jlong id,
  784. jlong in_nwid,
  785. jlong in_multicastGroup,
  786. jlong in_multicastAdi)
  787. {
  788. int64_t nodeId = (int64_t) id;
  789. ZT_Node *node = findNode(nodeId);
  790. uint64_t nwid = (uint64_t)in_nwid;
  791. uint64_t multicastGroup = (uint64_t)in_multicastGroup;
  792. unsigned long multicastAdi = (unsigned long)in_multicastAdi;
  793. ZT_ResultCode rc = ZT_Node_multicastSubscribe(
  794. node, NULL, nwid, multicastGroup, multicastAdi);
  795. return createResultObject(env, rc);
  796. }
  797. /*
  798. * Class: com_zerotier_sdk_Node
  799. * Method: multicastUnsubscribe
  800. * Signature: (JJJJ)Lcom/zerotier/sdk/ResultCode;
  801. */
  802. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastUnsubscribe(
  803. JNIEnv *env, jobject obj,
  804. jlong id,
  805. jlong in_nwid,
  806. jlong in_multicastGroup,
  807. jlong in_multicastAdi)
  808. {
  809. int64_t nodeId = (int64_t) id;
  810. ZT_Node *node = findNode(nodeId);
  811. uint64_t nwid = (uint64_t)in_nwid;
  812. uint64_t multicastGroup = (uint64_t)in_multicastGroup;
  813. unsigned long multicastAdi = (unsigned long)in_multicastAdi;
  814. ZT_ResultCode rc = ZT_Node_multicastUnsubscribe(
  815. node, nwid, multicastGroup, multicastAdi);
  816. return createResultObject(env, rc);
  817. }
  818. /*
  819. * Class: com_zerotier_sdk_Node
  820. * Method: orbit
  821. * Signature: (JJJ)Lcom/zerotier/sdk/ResultCode;
  822. */
  823. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_orbit(
  824. JNIEnv *env, jobject obj,
  825. jlong id,
  826. jlong in_moonWorldId,
  827. jlong in_moonSeed)
  828. {
  829. int64_t nodeId = (int64_t)id;
  830. ZT_Node *node = findNode(nodeId);
  831. uint64_t moonWorldId = (uint64_t)in_moonWorldId;
  832. uint64_t moonSeed = (uint64_t)in_moonSeed;
  833. ZT_ResultCode rc = ZT_Node_orbit(node, NULL, moonWorldId, moonSeed);
  834. return createResultObject(env, rc);
  835. }
  836. /*
  837. * Class: com_zerotier_sdk_Node
  838. * Method: deorbit
  839. * Signature: (JJ)L/com/zerotier/sdk/ResultCode;
  840. */
  841. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_deorbit(
  842. JNIEnv *env, jobject obj,
  843. jlong id,
  844. jlong in_moonWorldId)
  845. {
  846. int64_t nodeId = (int64_t)id;
  847. ZT_Node *node = findNode(nodeId);
  848. uint64_t moonWorldId = (uint64_t)in_moonWorldId;
  849. ZT_ResultCode rc = ZT_Node_deorbit(node, NULL, moonWorldId);
  850. return createResultObject(env, rc);
  851. }
  852. /*
  853. * Class: com_zerotier_sdk_Node
  854. * Method: address
  855. * Signature: (J)J
  856. */
  857. JNIEXPORT jlong JNICALL Java_com_zerotier_sdk_Node_address(
  858. JNIEnv *env , jobject obj, jlong id)
  859. {
  860. int64_t nodeId = (int64_t) id;
  861. ZT_Node *node = findNode(nodeId);
  862. uint64_t address = ZT_Node_address(node);
  863. return (jlong)address;
  864. }
  865. /*
  866. * Class: com_zerotier_sdk_Node
  867. * Method: status
  868. * Signature: (J)Lcom/zerotier/sdk/NodeStatus;
  869. */
  870. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_status
  871. (JNIEnv *env, jobject obj, jlong id)
  872. {
  873. int64_t nodeId = (int64_t) id;
  874. ZT_Node *node = findNode(nodeId);
  875. ZT_NodeStatus nodeStatus;
  876. ZT_Node_status(node, &nodeStatus);
  877. return newNodeStatus(env, nodeStatus);
  878. }
  879. /*
  880. * Class: com_zerotier_sdk_Node
  881. * Method: networkConfig
  882. * Signature: (JJ)Lcom/zerotier/sdk/VirtualNetworkConfig;
  883. */
  884. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_networkConfig(
  885. JNIEnv *env, jobject obj, jlong id, jlong nwid)
  886. {
  887. int64_t nodeId = (int64_t) id;
  888. ZT_Node *node = findNode(nodeId);
  889. ZT_VirtualNetworkConfig *vnetConfig = ZT_Node_networkConfig(node, nwid);
  890. jobject vnetConfigObject = newNetworkConfig(env, *vnetConfig);
  891. ZT_Node_freeQueryResult(node, vnetConfig);
  892. return vnetConfigObject;
  893. }
  894. /*
  895. * Class: com_zerotier_sdk_Node
  896. * Method: version
  897. * Signature: ()Lcom/zerotier/sdk/Version;
  898. */
  899. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_version(
  900. JNIEnv *env, jobject obj)
  901. {
  902. int major = 0;
  903. int minor = 0;
  904. int revision = 0;
  905. ZT_version(&major, &minor, &revision);
  906. return newVersion(env, major, minor, revision);
  907. }
  908. /*
  909. * Class: com_zerotier_sdk_Node
  910. * Method: peers
  911. * Signature: (J)[Lcom/zerotier/sdk/Peer;
  912. */
  913. JNIEXPORT jobjectArray JNICALL Java_com_zerotier_sdk_Node_peers(
  914. JNIEnv *env, jobject obj, jlong id)
  915. {
  916. int64_t nodeId = (int64_t) id;
  917. ZT_Node *node = findNode(nodeId);
  918. ZT_PeerList *peerList = ZT_Node_peers(node);
  919. if(peerList == NULL)
  920. {
  921. LOGE("ZT_Node_peers returned NULL");
  922. return NULL;
  923. }
  924. jobjectArray peerArrayObj = newPeerArray(env, peerList->peers, peerList->peerCount);
  925. ZT_Node_freeQueryResult(node, peerList);
  926. peerList = NULL;
  927. return peerArrayObj;
  928. }
  929. /*
  930. * Class: com_zerotier_sdk_Node
  931. * Method: networks
  932. * Signature: (J)[Lcom/zerotier/sdk/VirtualNetworkConfig;
  933. */
  934. JNIEXPORT jobjectArray JNICALL Java_com_zerotier_sdk_Node_networks(
  935. JNIEnv *env, jobject obj, jlong id)
  936. {
  937. int64_t nodeId = (int64_t) id;
  938. ZT_Node *node = findNode(nodeId);
  939. ZT_VirtualNetworkList *networkList = ZT_Node_networks(node);
  940. if(networkList == NULL)
  941. {
  942. return NULL;
  943. }
  944. jobjectArray networkListObject = newVirtualNetworkConfigArray(env, networkList->networks, networkList->networkCount);
  945. ZT_Node_freeQueryResult(node, networkList);
  946. return networkListObject;
  947. }
  948. #ifdef __cplusplus
  949. } // extern "C"
  950. #endif