com_zerotierone_sdk_Node.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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)", (long long)localSocket, remoteAddress, buffer, bufferSize);
  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. jobject remoteAddressObj = newInetSocketAddress(env, *remoteAddress);
  364. const unsigned char *bytes = static_cast<const unsigned char *>(buffer);
  365. jbyteArray bufferObj = newByteArray(env, bytes, bufferSize);
  366. if (env->ExceptionCheck() || bufferObj == NULL)
  367. {
  368. return -101;
  369. }
  370. int retval = env->CallIntMethod(ref->packetSender, PacketSender_onSendPacketRequested_method, localSocket, remoteAddressObj, bufferObj);
  371. LOGV("JNI Packet Sender returned: %d", retval);
  372. return retval;
  373. }
  374. int PathCheckFunction(ZT_Node *node,
  375. void *userPtr,
  376. void *threadPtr,
  377. uint64_t address,
  378. int64_t localSocket,
  379. const struct sockaddr_storage *remoteAddress)
  380. {
  381. JniRef *ref = (JniRef*)userPtr;
  382. assert(ref->node == node);
  383. if(ref->pathChecker == NULL) {
  384. return true;
  385. }
  386. JNIEnv *env = NULL;
  387. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  388. //
  389. // was:
  390. // struct sockaddr_storage nullAddress = {0};
  391. //
  392. // but was getting this warning:
  393. // warning: suggest braces around initialization of subobject
  394. //
  395. // when building ZeroTierOne
  396. //
  397. struct sockaddr_storage nullAddress;
  398. //
  399. // It is possible to assume knowledge about internals of sockaddr_storage and construct
  400. // correct 0-initializer, but it is simpler to just treat sockaddr_storage as opaque and
  401. // use memset here to fill with 0
  402. //
  403. // This is also done in InetAddress.hpp for InetAddress
  404. //
  405. memset(&nullAddress, 0, sizeof(sockaddr_storage));
  406. jobject remoteAddressObj = NULL;
  407. if(memcmp(remoteAddress, &nullAddress, sizeof(sockaddr_storage)) != 0)
  408. {
  409. remoteAddressObj = newInetSocketAddress(env, *remoteAddress);
  410. }
  411. return env->CallBooleanMethod(ref->pathChecker, PathChecker_onPathCheck_method, address, localSocket, remoteAddressObj);
  412. }
  413. int PathLookupFunction(ZT_Node *node,
  414. void *userPtr,
  415. void *threadPtr,
  416. uint64_t address,
  417. int ss_family,
  418. struct sockaddr_storage *result)
  419. {
  420. JniRef *ref = (JniRef*)userPtr;
  421. assert(ref->node == node);
  422. if(ref->pathChecker == NULL) {
  423. return false;
  424. }
  425. JNIEnv *env = NULL;
  426. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  427. jobject sockAddressObject = env->CallObjectMethod(ref->pathChecker, PathChecker_onPathLookup_method, address, ss_family);
  428. if(sockAddressObject == NULL)
  429. {
  430. LOGE("Unable to call onPathLookup implementation");
  431. return false;
  432. }
  433. *result = fromSocketAddressObject(env, sockAddressObject);
  434. if (env->ExceptionCheck() || isSocketAddressEmpty(*result)) {
  435. return false;
  436. }
  437. return true;
  438. }
  439. typedef std::map<int64_t, JniRef*> NodeMap;
  440. NodeMap nodeMap;
  441. ZeroTier::Mutex nodeMapMutex;
  442. bool isInited(int64_t nodeId) {
  443. ZeroTier::Mutex::Lock lock(nodeMapMutex);
  444. NodeMap::iterator found = nodeMap.find(nodeId);
  445. if (found == nodeMap.end()) {
  446. //
  447. // not in map yet, or has been removed from map
  448. //
  449. return false;
  450. }
  451. JniRef *ref = found->second;
  452. assert(ref);
  453. return ref->inited;
  454. }
  455. bool JniRef::finishInitializing() {
  456. ZeroTier::Mutex::Lock lock(nodeMapMutex);
  457. NodeMap::iterator found = nodeMap.find(id);
  458. if (found != nodeMap.end()) {
  459. //
  460. // already in map
  461. //
  462. LOGE("Cannot finish initializing; node is already in map");
  463. return false;
  464. }
  465. nodeMap.insert(std::make_pair(id, this));
  466. assert(!inited);
  467. inited = true;
  468. return true;
  469. }
  470. ZT_Node* findNode(int64_t nodeId)
  471. {
  472. ZeroTier::Mutex::Lock lock(nodeMapMutex);
  473. NodeMap::iterator found = nodeMap.find(nodeId);
  474. assert(found != nodeMap.end());
  475. JniRef *ref = found->second;
  476. assert(ref);
  477. return ref->node;
  478. }
  479. JniRef *removeRef(int64_t nodeId) {
  480. ZeroTier::Mutex::Lock lock(nodeMapMutex);
  481. NodeMap::iterator found = nodeMap.find(nodeId);
  482. if (found == nodeMap.end()) {
  483. return nullptr;
  484. }
  485. JniRef *ref = found->second;
  486. assert(ref);
  487. nodeMap.erase(nodeId);
  488. return ref;
  489. }
  490. }
  491. #ifdef __cplusplus
  492. extern "C" {
  493. #endif
  494. JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
  495. {
  496. setupJNICache(vm);
  497. return JNI_VERSION_1_6;
  498. }
  499. JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
  500. {
  501. teardownJNICache(vm);
  502. }
  503. /*
  504. * Class: com_zerotier_sdk_Node
  505. * Method: node_init
  506. * 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;
  507. */
  508. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_node_1init(
  509. JNIEnv *env, jobject obj, jlong now, jobject dataStoreGetListener,
  510. jobject dataStorePutListener, jobject packetSender, jobject eventListener,
  511. jobject frameListener, jobject configListener,
  512. jobject pathChecker)
  513. {
  514. LOGV("Creating ZT_Node struct");
  515. jobject resultObject = ResultCode_RESULT_OK_enum;
  516. ZT_Node *node;
  517. JniRef *ref = new JniRef;
  518. ref->id = (int64_t)now;
  519. env->GetJavaVM(&ref->jvm);
  520. if(dataStoreGetListener == NULL)
  521. {
  522. return NULL;
  523. }
  524. ref->dataStoreGetListener = env->NewGlobalRef(dataStoreGetListener);
  525. if(dataStorePutListener == NULL)
  526. {
  527. return NULL;
  528. }
  529. ref->dataStorePutListener = env->NewGlobalRef(dataStorePutListener);
  530. if(packetSender == NULL)
  531. {
  532. return NULL;
  533. }
  534. ref->packetSender = env->NewGlobalRef(packetSender);
  535. if(frameListener == NULL)
  536. {
  537. return NULL;
  538. }
  539. ref->frameListener = env->NewGlobalRef(frameListener);
  540. if(configListener == NULL)
  541. {
  542. return NULL;
  543. }
  544. ref->configListener = env->NewGlobalRef(configListener);
  545. if(eventListener == NULL)
  546. {
  547. return NULL;
  548. }
  549. ref->eventListener = env->NewGlobalRef(eventListener);
  550. if(pathChecker != NULL)
  551. {
  552. ref->pathChecker = env->NewGlobalRef(pathChecker);
  553. }
  554. ref->callbacks->stateGetFunction = &StateGetFunction;
  555. ref->callbacks->statePutFunction = &StatePutFunction;
  556. ref->callbacks->wirePacketSendFunction = &WirePacketSendFunction;
  557. ref->callbacks->virtualNetworkFrameFunction = &VirtualNetworkFrameFunctionCallback;
  558. ref->callbacks->virtualNetworkConfigFunction = &VirtualNetworkConfigFunctionCallback;
  559. ref->callbacks->eventCallback = &EventCallback;
  560. ref->callbacks->pathCheckFunction = &PathCheckFunction;
  561. ref->callbacks->pathLookupFunction = &PathLookupFunction;
  562. ZT_ResultCode rc = ZT_Node_new(
  563. &node,
  564. ref,
  565. NULL,
  566. ref->callbacks,
  567. (int64_t)now);
  568. if(rc != ZT_RESULT_OK)
  569. {
  570. LOGE("Error creating Node: %d", rc);
  571. resultObject = createResultObject(env, rc);
  572. if (env->ExceptionCheck() || resultObject == NULL) {
  573. return NULL;
  574. }
  575. if(node)
  576. {
  577. ZT_Node_delete(node);
  578. node = NULL;
  579. }
  580. delete ref;
  581. ref = NULL;
  582. return resultObject;
  583. }
  584. //
  585. // node is now updated
  586. //
  587. ref->node = node;
  588. if (!ref->finishInitializing()) {
  589. LOGE("finishInitializing() failed");
  590. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  591. }
  592. return resultObject;
  593. }
  594. /*
  595. * Class: com_zerotier_sdk_Node
  596. * Method: node_isInited
  597. * Signature: (J)Z
  598. */
  599. JNIEXPORT jboolean JNICALL Java_com_zerotier_sdk_Node_node_1isInited
  600. (JNIEnv *env, jobject obj, jlong nodeId) {
  601. return isInited(nodeId);
  602. }
  603. /*
  604. * Class: com_zerotier_sdk_Node
  605. * Method: node_delete
  606. * Signature: (J)V
  607. */
  608. JNIEXPORT void JNICALL Java_com_zerotier_sdk_Node_node_1delete(
  609. JNIEnv *env, jobject obj, jlong id)
  610. {
  611. LOGV("Destroying ZT_Node struct");
  612. int64_t nodeId = (int64_t)id;
  613. JniRef *ref = removeRef(nodeId);
  614. if (!ref) {
  615. return;
  616. }
  617. ZT_Node_delete(ref->node);
  618. delete ref;
  619. }
  620. /*
  621. * Class: com_zerotier_sdk_Node
  622. * Method: processVirtualNetworkFrame
  623. * Signature: (JJJJJII[B[J)Lcom/zerotier/sdk/ResultCode;
  624. */
  625. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processVirtualNetworkFrame(
  626. JNIEnv *env, jobject obj,
  627. jlong id,
  628. jlong in_now,
  629. jlong in_nwid,
  630. jlong in_sourceMac,
  631. jlong in_destMac,
  632. jint in_etherType,
  633. jint in_vlanId,
  634. jbyteArray in_frameData,
  635. jlongArray out_nextBackgroundTaskDeadline)
  636. {
  637. int64_t nodeId = (int64_t) id;
  638. ZT_Node *node = findNode(nodeId);
  639. unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline);
  640. if(nbtd_len < 1)
  641. {
  642. // array for next background task length has 0 elements!
  643. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  644. }
  645. int64_t now = (int64_t)in_now;
  646. uint64_t nwid = (uint64_t)in_nwid;
  647. uint64_t sourceMac = (uint64_t)in_sourceMac;
  648. uint64_t destMac = (uint64_t)in_destMac;
  649. unsigned int etherType = (unsigned int)in_etherType;
  650. unsigned int vlanId = (unsigned int)in_vlanId;
  651. unsigned int frameLength = env->GetArrayLength(in_frameData);
  652. void *frameData = env->GetPrimitiveArrayCritical(in_frameData, NULL);
  653. void *localData = malloc(frameLength);
  654. memcpy(localData, frameData, frameLength);
  655. env->ReleasePrimitiveArrayCritical(in_frameData, frameData, 0);
  656. int64_t nextBackgroundTaskDeadline = 0;
  657. ZT_ResultCode rc = ZT_Node_processVirtualNetworkFrame(
  658. node,
  659. NULL,
  660. now,
  661. nwid,
  662. sourceMac,
  663. destMac,
  664. etherType,
  665. vlanId,
  666. (const void*)localData,
  667. frameLength,
  668. &nextBackgroundTaskDeadline);
  669. free(localData);
  670. jlong *outDeadline = (jlong*)env->GetPrimitiveArrayCritical(out_nextBackgroundTaskDeadline, NULL);
  671. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  672. env->ReleasePrimitiveArrayCritical(out_nextBackgroundTaskDeadline, outDeadline, 0);
  673. return createResultObject(env, rc);
  674. }
  675. /*
  676. * Class: com_zerotier_sdk_Node
  677. * Method: processWirePacket
  678. * Signature: (JJJLjava/net/InetSocketAddress;[B[J)Lcom/zerotier/sdk/ResultCode;
  679. */
  680. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket(
  681. JNIEnv *env, jobject obj,
  682. jlong id,
  683. jlong in_now,
  684. jlong in_localSocket,
  685. jobject in_remoteAddress,
  686. jbyteArray in_packetData,
  687. jlongArray out_nextBackgroundTaskDeadline)
  688. {
  689. int64_t nodeId = (int64_t) id;
  690. ZT_Node *node = findNode(nodeId);
  691. unsigned int nbtd_len = (unsigned int)env->GetArrayLength(out_nextBackgroundTaskDeadline);
  692. if(nbtd_len < 1)
  693. {
  694. LOGE("nbtd_len < 1");
  695. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  696. }
  697. int64_t now = (int64_t)in_now;
  698. sockaddr_storage remoteAddress = fromSocketAddressObject(env, in_remoteAddress);
  699. if (env->ExceptionCheck() || isSocketAddressEmpty(remoteAddress)) {
  700. return NULL;
  701. }
  702. unsigned int packetLength = (unsigned int)env->GetArrayLength(in_packetData);
  703. if(packetLength == 0)
  704. {
  705. LOGE("Empty packet?!?");
  706. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  707. }
  708. void *packetData = env->GetPrimitiveArrayCritical(in_packetData, NULL);
  709. void *localData = malloc(packetLength);
  710. memcpy(localData, packetData, packetLength);
  711. env->ReleasePrimitiveArrayCritical(in_packetData, packetData, 0);
  712. int64_t nextBackgroundTaskDeadline = 0;
  713. ZT_ResultCode rc = ZT_Node_processWirePacket(
  714. node,
  715. NULL,
  716. now,
  717. in_localSocket,
  718. &remoteAddress,
  719. localData,
  720. packetLength,
  721. &nextBackgroundTaskDeadline);
  722. if(rc != ZT_RESULT_OK)
  723. {
  724. LOGE("ZT_Node_processWirePacket returned: %d", rc);
  725. }
  726. free(localData);
  727. jlong *outDeadline = (jlong*)env->GetPrimitiveArrayCritical(out_nextBackgroundTaskDeadline, NULL);
  728. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  729. env->ReleasePrimitiveArrayCritical(out_nextBackgroundTaskDeadline, outDeadline, 0);
  730. return createResultObject(env, rc);
  731. }
  732. /*
  733. * Class: com_zerotier_sdk_Node
  734. * Method: processBackgroundTasks
  735. * Signature: (JJ[J)Lcom/zerotier/sdk/ResultCode;
  736. */
  737. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processBackgroundTasks(
  738. JNIEnv *env, jobject obj,
  739. jlong id,
  740. jlong in_now,
  741. jlongArray out_nextBackgroundTaskDeadline)
  742. {
  743. int64_t nodeId = (int64_t) id;
  744. ZT_Node *node = findNode(nodeId);
  745. unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline);
  746. if(nbtd_len < 1)
  747. {
  748. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  749. }
  750. int64_t now = (int64_t)in_now;
  751. int64_t nextBackgroundTaskDeadline = 0;
  752. ZT_ResultCode rc = ZT_Node_processBackgroundTasks(node, NULL, now, &nextBackgroundTaskDeadline);
  753. jlong *outDeadline = (jlong*)env->GetPrimitiveArrayCritical(out_nextBackgroundTaskDeadline, NULL);
  754. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  755. env->ReleasePrimitiveArrayCritical(out_nextBackgroundTaskDeadline, outDeadline, 0);
  756. return createResultObject(env, rc);
  757. }
  758. /*
  759. * Class: com_zerotier_sdk_Node
  760. * Method: join
  761. * Signature: (JJ)Lcom/zerotier/sdk/ResultCode;
  762. */
  763. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_join(
  764. JNIEnv *env, jobject obj, jlong id, jlong in_nwid)
  765. {
  766. int64_t nodeId = (int64_t) id;
  767. ZT_Node *node = findNode(nodeId);
  768. uint64_t nwid = (uint64_t)in_nwid;
  769. ZT_ResultCode rc = ZT_Node_join(node, nwid, NULL, NULL);
  770. return createResultObject(env, rc);
  771. }
  772. /*
  773. * Class: com_zerotier_sdk_Node
  774. * Method: leave
  775. * Signature: (JJ)Lcom/zerotier/sdk/ResultCode;
  776. */
  777. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_leave(
  778. JNIEnv *env, jobject obj, jlong id, jlong in_nwid)
  779. {
  780. int64_t nodeId = (int64_t) id;
  781. ZT_Node *node = findNode(nodeId);
  782. uint64_t nwid = (uint64_t)in_nwid;
  783. ZT_ResultCode rc = ZT_Node_leave(node, nwid, NULL, NULL);
  784. return createResultObject(env, rc);
  785. }
  786. /*
  787. * Class: com_zerotier_sdk_Node
  788. * Method: multicastSubscribe
  789. * Signature: (JJJJ)Lcom/zerotier/sdk/ResultCode;
  790. */
  791. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastSubscribe(
  792. JNIEnv *env, jobject obj,
  793. jlong id,
  794. jlong in_nwid,
  795. jlong in_multicastGroup,
  796. jlong in_multicastAdi)
  797. {
  798. int64_t nodeId = (int64_t) id;
  799. ZT_Node *node = findNode(nodeId);
  800. uint64_t nwid = (uint64_t)in_nwid;
  801. uint64_t multicastGroup = (uint64_t)in_multicastGroup;
  802. unsigned long multicastAdi = (unsigned long)in_multicastAdi;
  803. ZT_ResultCode rc = ZT_Node_multicastSubscribe(
  804. node, NULL, nwid, multicastGroup, multicastAdi);
  805. return createResultObject(env, rc);
  806. }
  807. /*
  808. * Class: com_zerotier_sdk_Node
  809. * Method: multicastUnsubscribe
  810. * Signature: (JJJJ)Lcom/zerotier/sdk/ResultCode;
  811. */
  812. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastUnsubscribe(
  813. JNIEnv *env, jobject obj,
  814. jlong id,
  815. jlong in_nwid,
  816. jlong in_multicastGroup,
  817. jlong in_multicastAdi)
  818. {
  819. int64_t nodeId = (int64_t) id;
  820. ZT_Node *node = findNode(nodeId);
  821. uint64_t nwid = (uint64_t)in_nwid;
  822. uint64_t multicastGroup = (uint64_t)in_multicastGroup;
  823. unsigned long multicastAdi = (unsigned long)in_multicastAdi;
  824. ZT_ResultCode rc = ZT_Node_multicastUnsubscribe(
  825. node, nwid, multicastGroup, multicastAdi);
  826. return createResultObject(env, rc);
  827. }
  828. /*
  829. * Class: com_zerotier_sdk_Node
  830. * Method: orbit
  831. * Signature: (JJJ)Lcom/zerotier/sdk/ResultCode;
  832. */
  833. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_orbit(
  834. JNIEnv *env, jobject obj,
  835. jlong id,
  836. jlong in_moonWorldId,
  837. jlong in_moonSeed)
  838. {
  839. int64_t nodeId = (int64_t)id;
  840. ZT_Node *node = findNode(nodeId);
  841. uint64_t moonWorldId = (uint64_t)in_moonWorldId;
  842. uint64_t moonSeed = (uint64_t)in_moonSeed;
  843. ZT_ResultCode rc = ZT_Node_orbit(node, NULL, moonWorldId, moonSeed);
  844. return createResultObject(env, rc);
  845. }
  846. /*
  847. * Class: com_zerotier_sdk_Node
  848. * Method: deorbit
  849. * Signature: (JJ)L/com/zerotier/sdk/ResultCode;
  850. */
  851. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_deorbit(
  852. JNIEnv *env, jobject obj,
  853. jlong id,
  854. jlong in_moonWorldId)
  855. {
  856. int64_t nodeId = (int64_t)id;
  857. ZT_Node *node = findNode(nodeId);
  858. uint64_t moonWorldId = (uint64_t)in_moonWorldId;
  859. ZT_ResultCode rc = ZT_Node_deorbit(node, NULL, moonWorldId);
  860. return createResultObject(env, rc);
  861. }
  862. /*
  863. * Class: com_zerotier_sdk_Node
  864. * Method: address
  865. * Signature: (J)J
  866. */
  867. JNIEXPORT jlong JNICALL Java_com_zerotier_sdk_Node_address(
  868. JNIEnv *env , jobject obj, jlong id)
  869. {
  870. int64_t nodeId = (int64_t) id;
  871. ZT_Node *node = findNode(nodeId);
  872. uint64_t address = ZT_Node_address(node);
  873. return (jlong)address;
  874. }
  875. /*
  876. * Class: com_zerotier_sdk_Node
  877. * Method: status
  878. * Signature: (J)Lcom/zerotier/sdk/NodeStatus;
  879. */
  880. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_status
  881. (JNIEnv *env, jobject obj, jlong id)
  882. {
  883. int64_t nodeId = (int64_t) id;
  884. ZT_Node *node = findNode(nodeId);
  885. ZT_NodeStatus nodeStatus;
  886. ZT_Node_status(node, &nodeStatus);
  887. return newNodeStatus(env, nodeStatus);
  888. }
  889. /*
  890. * Class: com_zerotier_sdk_Node
  891. * Method: networkConfig
  892. * Signature: (JJ)Lcom/zerotier/sdk/VirtualNetworkConfig;
  893. */
  894. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_networkConfig(
  895. JNIEnv *env, jobject obj, jlong id, jlong nwid)
  896. {
  897. int64_t nodeId = (int64_t) id;
  898. ZT_Node *node = findNode(nodeId);
  899. ZT_VirtualNetworkConfig *vnetConfig = ZT_Node_networkConfig(node, nwid);
  900. jobject vnetConfigObject = newNetworkConfig(env, *vnetConfig);
  901. ZT_Node_freeQueryResult(node, vnetConfig);
  902. return vnetConfigObject;
  903. }
  904. /*
  905. * Class: com_zerotier_sdk_Node
  906. * Method: version
  907. * Signature: ()Lcom/zerotier/sdk/Version;
  908. */
  909. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_version(
  910. JNIEnv *env, jobject obj)
  911. {
  912. int major = 0;
  913. int minor = 0;
  914. int revision = 0;
  915. ZT_version(&major, &minor, &revision);
  916. return newVersion(env, major, minor, revision);
  917. }
  918. /*
  919. * Class: com_zerotier_sdk_Node
  920. * Method: peers
  921. * Signature: (J)[Lcom/zerotier/sdk/Peer;
  922. */
  923. JNIEXPORT jobjectArray JNICALL Java_com_zerotier_sdk_Node_peers(
  924. JNIEnv *env, jobject obj, jlong id)
  925. {
  926. int64_t nodeId = (int64_t) id;
  927. ZT_Node *node = findNode(nodeId);
  928. ZT_PeerList *peerList = ZT_Node_peers(node);
  929. if(peerList == NULL)
  930. {
  931. LOGE("ZT_Node_peers returned NULL");
  932. return NULL;
  933. }
  934. jobjectArray peerArrayObj = newPeerArray(env, peerList->peers, peerList->peerCount);
  935. ZT_Node_freeQueryResult(node, peerList);
  936. peerList = NULL;
  937. return peerArrayObj;
  938. }
  939. /*
  940. * Class: com_zerotier_sdk_Node
  941. * Method: networks
  942. * Signature: (J)[Lcom/zerotier/sdk/VirtualNetworkConfig;
  943. */
  944. JNIEXPORT jobjectArray JNICALL Java_com_zerotier_sdk_Node_networks(
  945. JNIEnv *env, jobject obj, jlong id)
  946. {
  947. int64_t nodeId = (int64_t) id;
  948. ZT_Node *node = findNode(nodeId);
  949. ZT_VirtualNetworkList *networkList = ZT_Node_networks(node);
  950. if(networkList == NULL)
  951. {
  952. return NULL;
  953. }
  954. jobjectArray networkListObject = newVirtualNetworkConfigArray(env, networkList->networks, networkList->networkCount);
  955. ZT_Node_freeQueryResult(node, networkList);
  956. return networkListObject;
  957. }
  958. #ifdef __cplusplus
  959. } // extern "C"
  960. #endif