com_zerotierone_sdk_Node.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  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. #include <cinttypes> // for PRId64
  37. #define LOG_TAG "Node"
  38. namespace {
  39. struct JniRef
  40. {
  41. JniRef(
  42. int64_t id,
  43. JavaVM *jvm,
  44. jobject dataStoreGetListenerLocalIn,
  45. jobject dataStorePutListenerLocalIn,
  46. jobject packetSenderLocalIn,
  47. jobject eventListenerLocalIn,
  48. jobject frameListenerLocalIn,
  49. jobject configListenerLocalIn,
  50. jobject pathCheckerLocalIn,
  51. ZT_Node_Config *nc)
  52. : id(id)
  53. , jvm(jvm)
  54. , node()
  55. , dataStoreGetListener()
  56. , dataStorePutListener()
  57. , packetSender()
  58. , eventListener()
  59. , frameListener()
  60. , configListener()
  61. , pathChecker()
  62. , nodeConfig(nc)
  63. , inited() {
  64. JNIEnv *env;
  65. GETENV(env, jvm);
  66. dataStoreGetListener = env->NewGlobalRef(dataStoreGetListenerLocalIn);
  67. dataStorePutListener = env->NewGlobalRef(dataStorePutListenerLocalIn);
  68. packetSender = env->NewGlobalRef(packetSenderLocalIn);
  69. eventListener = env->NewGlobalRef(eventListenerLocalIn);
  70. frameListener = env->NewGlobalRef(frameListenerLocalIn);
  71. configListener = env->NewGlobalRef(configListenerLocalIn);
  72. pathChecker = env->NewGlobalRef(pathCheckerLocalIn);
  73. };
  74. ~JniRef()
  75. {
  76. JNIEnv *env;
  77. GETENV(env, jvm);
  78. env->DeleteGlobalRef(dataStoreGetListener);
  79. env->DeleteGlobalRef(dataStorePutListener);
  80. env->DeleteGlobalRef(packetSender);
  81. env->DeleteGlobalRef(eventListener);
  82. env->DeleteGlobalRef(frameListener);
  83. env->DeleteGlobalRef(configListener);
  84. env->DeleteGlobalRef(pathChecker);
  85. delete nodeConfig;
  86. nodeConfig = NULL;
  87. }
  88. int64_t id;
  89. JavaVM *jvm;
  90. ZT_Node *node;
  91. jobject dataStoreGetListener;
  92. jobject dataStorePutListener;
  93. jobject packetSender;
  94. jobject eventListener;
  95. jobject frameListener;
  96. jobject configListener;
  97. jobject pathChecker;
  98. ZT_Node_Config *nodeConfig;
  99. bool inited;
  100. bool finishInitializing();
  101. };
  102. //
  103. // RAII construct for calling AttachCurrentThread and DetachCurrent automatically
  104. //
  105. struct ScopedJNIThreadAttacher {
  106. JavaVM *jvm;
  107. JNIEnv **env_p;
  108. jint getEnvRet;
  109. ScopedJNIThreadAttacher(JavaVM *jvmIn, JNIEnv **env_pIn, jint getEnvRetIn) :
  110. jvm(jvmIn),
  111. env_p(env_pIn),
  112. getEnvRet(getEnvRetIn) {
  113. if (getEnvRet != JNI_EDETACHED) {
  114. return;
  115. }
  116. jint attachCurrentThreadRet;
  117. if ((attachCurrentThreadRet = jvm->AttachCurrentThread(env_p, NULL)) != JNI_OK) {
  118. LOGE("Error calling AttachCurrentThread: %d", attachCurrentThreadRet);
  119. assert(false && "Error calling AttachCurrentThread");
  120. }
  121. }
  122. ~ScopedJNIThreadAttacher() {
  123. if (getEnvRet != JNI_EDETACHED) {
  124. return;
  125. }
  126. jint detachCurrentThreadRet;
  127. if ((detachCurrentThreadRet = jvm->DetachCurrentThread()) != JNI_OK) {
  128. LOGE("Error calling DetachCurrentThread: %d", detachCurrentThreadRet);
  129. assert(false && "Error calling DetachCurrentThread");
  130. }
  131. }
  132. };
  133. /*
  134. * This must return 0 on success. It can return any OS-dependent error code
  135. * on failure, and this results in the network being placed into the
  136. * PORT_ERROR state.
  137. */
  138. int VirtualNetworkConfigFunctionCallback(
  139. ZT_Node *node,
  140. void *userData,
  141. void *threadData,
  142. uint64_t nwid,
  143. void **nuptr,
  144. enum ZT_VirtualNetworkConfigOperation operation,
  145. const ZT_VirtualNetworkConfig *config)
  146. {
  147. LOGV("VirtualNetworkConfigFunctionCallback");
  148. JniRef *ref = (JniRef*)userData;
  149. assert(ref);
  150. JNIEnv *env;
  151. GETENV(env, ref->jvm);
  152. if (env->ExceptionCheck()) {
  153. LOGE("Unhandled pending exception");
  154. return -100;
  155. }
  156. if (ref->configListener == NULL) {
  157. LOGE("configListener is NULL");
  158. return -101;
  159. }
  160. jobject operationObject = createVirtualNetworkConfigOperation(env, operation);
  161. if(env->ExceptionCheck() || operationObject == NULL)
  162. {
  163. return -102;
  164. }
  165. if (config == NULL) {
  166. LOGE("Config is NULL");
  167. return -103;
  168. }
  169. jobject networkConfigObject = newNetworkConfig(env, *config);
  170. if(env->ExceptionCheck() || networkConfigObject == NULL)
  171. {
  172. return -104;
  173. }
  174. jint ret = env->CallIntMethod(
  175. ref->configListener,
  176. VirtualNetworkConfigListener_onNetworkConfigurationUpdated_method,
  177. (jlong)nwid, (jobject)operationObject, (jobject)networkConfigObject);
  178. if (env->ExceptionCheck()) {
  179. LOGE("Exception calling onNetworkConfigurationUpdated");
  180. return -105;
  181. }
  182. return ret;
  183. }
  184. void VirtualNetworkFrameFunctionCallback(ZT_Node *node,
  185. void *userData,
  186. void *threadData,
  187. uint64_t nwid,
  188. void** nuptr,
  189. uint64_t sourceMac,
  190. uint64_t destMac,
  191. unsigned int etherType,
  192. unsigned int vlanid,
  193. const void *frameData,
  194. unsigned int frameLength)
  195. {
  196. LOGV("VirtualNetworkFrameFunctionCallback");
  197. #ifndef NDEBUG
  198. if (frameLength >= 14) {
  199. unsigned char* local = (unsigned char*)frameData;
  200. LOGV("Type Bytes: 0x%02x%02x", local[12], local[13]);
  201. }
  202. #endif
  203. JniRef *ref = (JniRef*)userData;
  204. assert(ref);
  205. assert(ref->node == node);
  206. JNIEnv *env;
  207. jint getEnvRet;
  208. assert(ref->jvm);
  209. getEnvRet = ref->jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
  210. if (!(getEnvRet == JNI_OK || getEnvRet == JNI_EDETACHED)) {
  211. LOGE("Error calling GetEnv: %d", getEnvRet);
  212. assert(false && "Error calling GetEnv");
  213. }
  214. //
  215. // Thread might actually be detached.
  216. //
  217. // e.g:
  218. // https://github.com/zerotier/ZeroTierOne/blob/91e7ce87f09ac1cfdeaf6ff22c3cedcd93574c86/node/Switch.cpp#L519
  219. //
  220. // Make sure to attach if needed
  221. //
  222. ScopedJNIThreadAttacher attacher{ref->jvm, &env, getEnvRet};
  223. if (env->ExceptionCheck()) {
  224. LOGE("Unhandled pending exception");
  225. return;
  226. }
  227. if (ref->frameListener == NULL) {
  228. LOGE("frameListener is NULL");
  229. return;
  230. }
  231. const unsigned char *bytes = static_cast<const unsigned char*>(frameData);
  232. jbyteArray dataArray = newByteArray(env, bytes, frameLength);
  233. if(env->ExceptionCheck() || dataArray == NULL)
  234. {
  235. return;
  236. }
  237. env->CallVoidMethod(ref->frameListener, VirtualNetworkFrameListener_onVirtualNetworkFrame_method, (jlong)nwid, (jlong)sourceMac, (jlong)destMac, (jlong)etherType, (jlong)vlanid, (jbyteArray)dataArray);
  238. if (env->ExceptionCheck()) {
  239. LOGE("Exception calling onVirtualNetworkFrame");
  240. return;
  241. }
  242. }
  243. void EventCallback(ZT_Node *node,
  244. void *userData,
  245. void *threadData,
  246. enum ZT_Event event,
  247. const void *data) {
  248. LOGV("EventCallback");
  249. JniRef *ref = (JniRef *) userData;
  250. assert(ref);
  251. if (ref->node != node && event != ZT_EVENT_UP) {
  252. LOGE("Nodes not equal. ref->node %p, node %p. Event: %d", ref->node, node, event);
  253. return;
  254. }
  255. JNIEnv *env;
  256. GETENV(env, ref->jvm);
  257. if (env->ExceptionCheck()) {
  258. LOGE("Unhandled pending exception");
  259. return;
  260. }
  261. if (ref->eventListener == NULL) {
  262. LOGE("eventListener is NULL");
  263. return;
  264. }
  265. jobject eventObject = createEvent(env, event);
  266. if (env->ExceptionCheck() || eventObject == NULL) {
  267. return;
  268. }
  269. switch (event) {
  270. case ZT_EVENT_UP: {
  271. LOGD("Event Up");
  272. env->CallVoidMethod(ref->eventListener, EventListener_onEvent_method, (jobject)eventObject);
  273. if (env->ExceptionCheck()) {
  274. LOGE("Exception calling onEvent");
  275. return;
  276. }
  277. break;
  278. }
  279. case ZT_EVENT_OFFLINE: {
  280. LOGD("Event Offline");
  281. env->CallVoidMethod(ref->eventListener, EventListener_onEvent_method, (jobject)eventObject);
  282. if (env->ExceptionCheck()) {
  283. LOGE("Exception calling onEvent");
  284. return;
  285. }
  286. break;
  287. }
  288. case ZT_EVENT_ONLINE: {
  289. LOGD("Event Online");
  290. env->CallVoidMethod(ref->eventListener, EventListener_onEvent_method, (jobject)eventObject);
  291. if (env->ExceptionCheck()) {
  292. LOGE("Exception calling onEvent");
  293. return;
  294. }
  295. break;
  296. }
  297. case ZT_EVENT_DOWN: {
  298. LOGD("Event Down");
  299. env->CallVoidMethod(ref->eventListener, EventListener_onEvent_method, (jobject)eventObject);
  300. if (env->ExceptionCheck()) {
  301. LOGE("Exception calling onEvent");
  302. return;
  303. }
  304. break;
  305. }
  306. case ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION: {
  307. LOGV("Identity Collision");
  308. // call onEvent()
  309. env->CallVoidMethod(ref->eventListener, EventListener_onEvent_method, (jobject)eventObject);
  310. if (env->ExceptionCheck()) {
  311. LOGE("Exception calling onEvent");
  312. return;
  313. }
  314. }
  315. break;
  316. case ZT_EVENT_TRACE: {
  317. LOGV("Trace Event");
  318. // call onTrace()
  319. if (data == NULL) {
  320. break;
  321. }
  322. const char *message = (const char *) data;
  323. jstring messageStr = env->NewStringUTF(message);
  324. if (env->ExceptionCheck() || messageStr == NULL) {
  325. LOGE("Exception creating new string");
  326. return;
  327. }
  328. env->CallVoidMethod(ref->eventListener, EventListener_onTrace_method, (jstring)messageStr);
  329. if (env->ExceptionCheck()) {
  330. LOGE("Exception calling onTrace");
  331. return;
  332. }
  333. }
  334. break;
  335. case ZT_EVENT_USER_MESSAGE:
  336. case ZT_EVENT_REMOTE_TRACE:
  337. break;
  338. }
  339. }
  340. void StatePutFunction(
  341. ZT_Node *node,
  342. void *userData,
  343. void *threadData,
  344. enum ZT_StateObjectType type,
  345. const uint64_t id[2],
  346. const void *buffer,
  347. int bufferLength) {
  348. LOGV("StatePutFunction");
  349. char p[4096] = {0};
  350. bool secure = false;
  351. int res = 0;
  352. switch (type) {
  353. case ZT_STATE_OBJECT_IDENTITY_PUBLIC:
  354. res = snprintf(p, sizeof(p), "identity.public");
  355. break;
  356. case ZT_STATE_OBJECT_IDENTITY_SECRET:
  357. res = snprintf(p, sizeof(p), "identity.secret");
  358. secure = true;
  359. break;
  360. case ZT_STATE_OBJECT_PLANET:
  361. res = snprintf(p, sizeof(p), "planet");
  362. break;
  363. case ZT_STATE_OBJECT_MOON:
  364. res = snprintf(p, sizeof(p), "moons.d/%.16" PRIx64 ".moon", id[0]);
  365. break;
  366. case ZT_STATE_OBJECT_NETWORK_CONFIG:
  367. res = snprintf(p, sizeof(p), "networks.d/%.16" PRIx64 ".conf", id[0]);
  368. break;
  369. case ZT_STATE_OBJECT_PEER:
  370. res = snprintf(p, sizeof(p), "peers.d/%.10" PRIx64, id[0]);
  371. break;
  372. case ZT_STATE_OBJECT_NULL:
  373. return;
  374. }
  375. if (!(0 <= res && res < sizeof(p))) {
  376. LOGE("snprintf error: %d", res);
  377. return;
  378. }
  379. JniRef *ref = (JniRef*)userData;
  380. assert(ref);
  381. JNIEnv *env;
  382. GETENV(env, ref->jvm);
  383. if (env->ExceptionCheck()) {
  384. LOGE("Unhandled pending exception");
  385. return;
  386. }
  387. if (ref->dataStorePutListener == NULL) {
  388. LOGE("dataStorePutListener is NULL");
  389. return;
  390. }
  391. jstring nameStr = env->NewStringUTF(p);
  392. if (env->ExceptionCheck() || nameStr == NULL) {
  393. LOGE("Exception creating new string");
  394. return;
  395. }
  396. if (bufferLength >= 0) {
  397. LOGD("JNI: Write file: %s", p);
  398. const unsigned char *bytes = static_cast<const unsigned char *>(buffer);
  399. jbyteArray bufferObj = newByteArray(env, bytes, bufferLength);
  400. if(env->ExceptionCheck() || bufferObj == NULL)
  401. {
  402. return;
  403. }
  404. int retval = env->CallIntMethod(ref->dataStorePutListener,
  405. DataStorePutListener_onDataStorePut_method,
  406. (jstring)nameStr, (jbyteArray)bufferObj, (jboolean)secure);
  407. if (env->ExceptionCheck()) {
  408. LOGE("Exception calling onDataStorePut");
  409. return;
  410. }
  411. if (retval != 0) {
  412. LOGE("onDataStorePut error: %d", retval);
  413. }
  414. } else {
  415. LOGD("JNI: Delete file: %s", p);
  416. int retval = env->CallIntMethod(ref->dataStorePutListener, DataStorePutListener_onDelete_method, (jstring)nameStr);
  417. if (env->ExceptionCheck()) {
  418. LOGE("Exception calling onDelete");
  419. return;
  420. }
  421. if (retval != 0) {
  422. LOGE("onDelete error: %d", retval);
  423. }
  424. }
  425. }
  426. /**
  427. * This function should return the number of bytes actually stored to the
  428. * buffer or -1 if the state object was not found or the buffer was too
  429. * small to store it.
  430. */
  431. int StateGetFunction(
  432. ZT_Node *node,
  433. void *userData,
  434. void *threadData,
  435. ZT_StateObjectType type,
  436. const uint64_t id[2],
  437. void *buffer,
  438. unsigned int bufferLength) {
  439. LOGV("StateGetFunction");
  440. char p[4096] = {0};
  441. int res = 0;
  442. switch (type) {
  443. case ZT_STATE_OBJECT_IDENTITY_PUBLIC:
  444. res = snprintf(p, sizeof(p), "identity.public");
  445. break;
  446. case ZT_STATE_OBJECT_IDENTITY_SECRET:
  447. res = snprintf(p, sizeof(p), "identity.secret");
  448. break;
  449. case ZT_STATE_OBJECT_PLANET:
  450. res = snprintf(p, sizeof(p), "planet");
  451. break;
  452. case ZT_STATE_OBJECT_MOON:
  453. res = snprintf(p, sizeof(p), "moons.d/%.16" PRIx64 ".moon", id[0]);
  454. break;
  455. case ZT_STATE_OBJECT_NETWORK_CONFIG:
  456. res = snprintf(p, sizeof(p), "networks.d/%.16" PRIx64 ".conf", id[0]);
  457. break;
  458. case ZT_STATE_OBJECT_PEER:
  459. res = snprintf(p, sizeof(p), "peers.d/%.10" PRIx64, id[0]);
  460. break;
  461. case ZT_STATE_OBJECT_NULL:
  462. return -100;
  463. }
  464. if (!(0 <= res && res < sizeof(p))) {
  465. LOGE("snprintf error: %d", res);
  466. return -101;
  467. }
  468. JniRef *ref = (JniRef*)userData;
  469. assert(ref);
  470. JNIEnv *env;
  471. GETENV(env, ref->jvm);
  472. if (env->ExceptionCheck()) {
  473. LOGE("Unhandled pending exception");
  474. return -102;
  475. }
  476. if (ref->dataStoreGetListener == NULL) {
  477. LOGE("dataStoreGetListener is NULL");
  478. return -103;
  479. }
  480. jstring nameStr = env->NewStringUTF(p);
  481. if(env->ExceptionCheck() || nameStr == NULL)
  482. {
  483. LOGE("Error creating name string object");
  484. return -104; // out of memory
  485. }
  486. jbyteArray bufferObj = newByteArray(env, bufferLength);
  487. if(env->ExceptionCheck() || bufferObj == NULL)
  488. {
  489. return -105;
  490. }
  491. LOGV("Calling onDataStoreGet(%s, %p)", p, buffer);
  492. int retval = (int)env->CallLongMethod(
  493. ref->dataStoreGetListener,
  494. DataStoreGetListener_onDataStoreGet_method,
  495. (jstring)nameStr,
  496. (jbyteArray)bufferObj);
  497. if (env->ExceptionCheck()) {
  498. LOGE("Exception calling onDataStoreGet");
  499. return -106;
  500. }
  501. LOGV("onDataStoreGet returned %d", retval);
  502. if(retval > 0)
  503. {
  504. if (retval > bufferLength) {
  505. LOGE("retval > bufferLength. retval: %d, bufferLength: %u", retval, bufferLength);
  506. return -107;
  507. }
  508. void *data = env->GetPrimitiveArrayCritical(bufferObj, NULL);
  509. memcpy(buffer, data, retval);
  510. env->ReleasePrimitiveArrayCritical(bufferObj, data, 0);
  511. }
  512. return retval;
  513. }
  514. /**
  515. * The function must return zero on success and may return any error code
  516. * on failure. Note that success does not (of course) guarantee packet
  517. * delivery. It only means that the packet appears to have been sent.
  518. */
  519. int WirePacketSendFunction(ZT_Node *node,
  520. void *userData,
  521. void *threadData,
  522. int64_t localSocket,
  523. const struct sockaddr_storage *remoteAddress,
  524. const void *buffer,
  525. unsigned int bufferSize,
  526. unsigned int ttl)
  527. {
  528. LOGV("WirePacketSendFunction(%" PRId64 ", %p, %p, %u, %u)", localSocket, remoteAddress, buffer, bufferSize, ttl);
  529. JniRef *ref = (JniRef*)userData;
  530. assert(ref);
  531. assert(ref->node == node);
  532. JNIEnv *env;
  533. GETENV(env, ref->jvm);
  534. if (env->ExceptionCheck()) {
  535. LOGE("Unhandled pending exception");
  536. return -100;
  537. }
  538. if (ref->packetSender == NULL) {
  539. LOGE("packetSender is NULL");
  540. return -101;
  541. }
  542. //
  543. // may be NULL
  544. //
  545. jobject remoteAddressObj = newInetSocketAddress(env, *remoteAddress);
  546. if (env->ExceptionCheck()) {
  547. return -102;
  548. }
  549. const unsigned char *bytes = static_cast<const unsigned char *>(buffer);
  550. jbyteArray bufferObj = newByteArray(env, bytes, bufferSize);
  551. if (env->ExceptionCheck() || bufferObj == NULL)
  552. {
  553. return -103;
  554. }
  555. int retval = env->CallIntMethod(ref->packetSender, PacketSender_onSendPacketRequested_method, (jlong)localSocket, (jobject)remoteAddressObj, (jbyteArray)bufferObj, (jint)0);
  556. if (env->ExceptionCheck()) {
  557. LOGE("Exception calling onSendPacketRequested");
  558. return -104;
  559. }
  560. LOGV("JNI Packet Sender returned: %d", retval);
  561. return retval;
  562. }
  563. /**
  564. * This function must return nonzero (true) if the path should be used.
  565. */
  566. int PathCheckFunction(ZT_Node *node,
  567. void *userPtr,
  568. void *threadPtr,
  569. uint64_t address,
  570. int64_t localSocket,
  571. const struct sockaddr_storage *remoteAddress)
  572. {
  573. LOGV("PathCheckFunction");
  574. JniRef *ref = (JniRef*)userPtr;
  575. assert(ref);
  576. assert(ref->node == node);
  577. if(ref->pathChecker == NULL) {
  578. return true;
  579. }
  580. JNIEnv *env;
  581. GETENV(env, ref->jvm);
  582. if (env->ExceptionCheck()) {
  583. LOGE("Unhandled pending exception");
  584. return true;
  585. }
  586. //
  587. // may be NULL
  588. //
  589. jobject remoteAddressObj = newInetSocketAddress(env, *remoteAddress);
  590. if (env->ExceptionCheck()) {
  591. return true;
  592. }
  593. jboolean ret = env->CallBooleanMethod(ref->pathChecker, PathChecker_onPathCheck_method, (jlong)address, (jlong)localSocket, (jobject)remoteAddressObj);
  594. if (env->ExceptionCheck()) {
  595. LOGE("Exception calling onPathCheck");
  596. return true;
  597. }
  598. return ret;
  599. }
  600. /**
  601. * It must return a nonzero (true) value if the result buffer has been filled with an address.
  602. */
  603. int PathLookupFunction(ZT_Node *node,
  604. void *userPtr,
  605. void *threadPtr,
  606. uint64_t address,
  607. int ss_family,
  608. struct sockaddr_storage *result)
  609. {
  610. LOGV("PathLookupFunction");
  611. JniRef *ref = (JniRef*)userPtr;
  612. assert(ref);
  613. assert(ref->node == node);
  614. if(ref->pathChecker == NULL) {
  615. return false;
  616. }
  617. JNIEnv *env;
  618. GETENV(env, ref->jvm);
  619. if (env->ExceptionCheck()) {
  620. LOGE("Unhandled pending exception");
  621. return false;
  622. }
  623. //
  624. // may be NULL
  625. //
  626. jobject sockAddressObject = env->CallObjectMethod(ref->pathChecker, PathChecker_onPathLookup_method, (jlong)address, (jint)ss_family);
  627. if (env->ExceptionCheck()) {
  628. LOGE("Unable to call onPathLookup implementation");
  629. return false;
  630. }
  631. if(sockAddressObject == NULL)
  632. {
  633. LOGE("Unable to call onPathLookup implementation");
  634. return false;
  635. }
  636. *result = fromSocketAddressObject(env, sockAddressObject);
  637. if (env->ExceptionCheck() || isSocketAddressEmpty(*result)) {
  638. return false;
  639. }
  640. return true;
  641. }
  642. typedef std::map<int64_t, JniRef*> NodeMap;
  643. NodeMap nodeMap;
  644. ZeroTier::Mutex nodeMapMutex;
  645. bool isInited(int64_t nodeId) {
  646. ZeroTier::Mutex::Lock lock(nodeMapMutex);
  647. NodeMap::iterator found = nodeMap.find(nodeId);
  648. if (found == nodeMap.end()) {
  649. //
  650. // not in map yet, or has been removed from map
  651. //
  652. return false;
  653. }
  654. JniRef *ref = found->second;
  655. assert(ref);
  656. return ref->inited;
  657. }
  658. bool JniRef::finishInitializing() {
  659. ZeroTier::Mutex::Lock lock(nodeMapMutex);
  660. NodeMap::iterator found = nodeMap.find(id);
  661. if (found != nodeMap.end()) {
  662. //
  663. // already in map
  664. //
  665. LOGE("Cannot finish initializing; node is already in map");
  666. return false;
  667. }
  668. nodeMap.insert(std::make_pair(id, this));
  669. assert(!inited);
  670. inited = true;
  671. return true;
  672. }
  673. ZT_Node* findNode(int64_t nodeId)
  674. {
  675. ZeroTier::Mutex::Lock lock(nodeMapMutex);
  676. NodeMap::iterator found = nodeMap.find(nodeId);
  677. assert(found != nodeMap.end());
  678. JniRef *ref = found->second;
  679. assert(ref);
  680. return ref->node;
  681. }
  682. JniRef *removeRef(int64_t nodeId) {
  683. ZeroTier::Mutex::Lock lock(nodeMapMutex);
  684. NodeMap::iterator found = nodeMap.find(nodeId);
  685. if (found == nodeMap.end()) {
  686. return nullptr;
  687. }
  688. JniRef *ref = found->second;
  689. assert(ref);
  690. nodeMap.erase(nodeId);
  691. return ref;
  692. }
  693. }
  694. #ifdef __cplusplus
  695. extern "C" {
  696. #endif
  697. JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
  698. {
  699. setupJNICache(vm);
  700. return JNI_VERSION_1_6;
  701. }
  702. JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
  703. {
  704. teardownJNICache(vm);
  705. }
  706. /*
  707. * Class: com_zerotier_sdk_Node
  708. * Method: node_init
  709. * 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;
  710. */
  711. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_node_1init(
  712. JNIEnv *env, jobject obj, jlong now, jobject dataStoreGetListener,
  713. jobject dataStorePutListener, jobject packetSender, jobject eventListener,
  714. jobject frameListener, jobject configListener,
  715. jobject pathChecker)
  716. {
  717. LOGV("Creating ZT_Node struct");
  718. jobject resultObject = ResultCode_RESULT_OK_enum;
  719. JavaVM *vm;
  720. GETJAVAVM(env, vm);
  721. assert(dataStoreGetListener != NULL);
  722. assert(dataStorePutListener != NULL);
  723. assert(packetSender != NULL);
  724. assert(frameListener != NULL);
  725. assert(configListener != NULL);
  726. assert(eventListener != NULL);
  727. //
  728. // OPTIONAL, pathChecker may be NULL
  729. //
  730. // assert(pathChecker != NULL);
  731. ZT_Node_Callbacks callbacks{};
  732. callbacks.stateGetFunction = &StateGetFunction;
  733. callbacks.statePutFunction = &StatePutFunction;
  734. callbacks.wirePacketSendFunction = &WirePacketSendFunction;
  735. callbacks.virtualNetworkFrameFunction = &VirtualNetworkFrameFunctionCallback;
  736. callbacks.virtualNetworkConfigFunction = &VirtualNetworkConfigFunctionCallback;
  737. callbacks.eventCallback = &EventCallback;
  738. callbacks.pathCheckFunction = &PathCheckFunction;
  739. callbacks.pathLookupFunction = &PathLookupFunction;
  740. ZT_Node_Config *nodeConfig = new ZT_Node_Config();
  741. nodeConfig->enableEncryptedHello = 0;
  742. nodeConfig->lowBandwidthMode = 0;
  743. //
  744. // a bit of a confusing dance here where ref and node both know about each other
  745. //
  746. JniRef *ref = new JniRef(
  747. now,
  748. vm,
  749. dataStoreGetListener,
  750. dataStorePutListener,
  751. packetSender,
  752. eventListener,
  753. frameListener,
  754. configListener,
  755. pathChecker,
  756. nodeConfig);
  757. ZT_Node *node;
  758. ZT_ResultCode rc = ZT_Node_new(
  759. &node,
  760. nodeConfig,
  761. ref,
  762. NULL,
  763. &callbacks,
  764. (int64_t)now);
  765. if (env->ExceptionCheck()) {
  766. LOGE("Exception creating Node");
  767. if(node)
  768. {
  769. ZT_Node_delete(node);
  770. node = NULL;
  771. }
  772. delete ref;
  773. ref = NULL;
  774. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  775. }
  776. if(rc != ZT_RESULT_OK)
  777. {
  778. LOGE("Error creating Node: %d", rc);
  779. resultObject = createResultObject(env, rc);
  780. if (env->ExceptionCheck() || resultObject == NULL) {
  781. return NULL;
  782. }
  783. if(node)
  784. {
  785. ZT_Node_delete(node);
  786. node = NULL;
  787. }
  788. delete ref;
  789. ref = NULL;
  790. return resultObject;
  791. }
  792. //
  793. // node is now updated
  794. //
  795. ref->node = node;
  796. if (!ref->finishInitializing()) {
  797. LOGE("finishInitializing() failed");
  798. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  799. }
  800. return resultObject;
  801. }
  802. /*
  803. * Class: com_zerotier_sdk_Node
  804. * Method: node_isInited
  805. * Signature: (J)Z
  806. */
  807. JNIEXPORT jboolean JNICALL Java_com_zerotier_sdk_Node_node_1isInited
  808. (JNIEnv *env, jobject obj, jlong nodeId) {
  809. return isInited(nodeId);
  810. }
  811. /*
  812. * Class: com_zerotier_sdk_Node
  813. * Method: node_delete
  814. * Signature: (J)V
  815. */
  816. JNIEXPORT void JNICALL Java_com_zerotier_sdk_Node_node_1delete(
  817. JNIEnv *env, jobject obj, jlong id)
  818. {
  819. LOGV("Destroying ZT_Node struct");
  820. int64_t nodeId = (int64_t)id;
  821. JniRef *ref = removeRef(nodeId);
  822. if (!ref) {
  823. return;
  824. }
  825. ZT_Node_delete(ref->node);
  826. delete ref;
  827. }
  828. /*
  829. * Class: com_zerotier_sdk_Node
  830. * Method: processVirtualNetworkFrame
  831. * Signature: (JJJJJII[B[J)Lcom/zerotier/sdk/ResultCode;
  832. */
  833. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processVirtualNetworkFrame(
  834. JNIEnv *env, jobject obj,
  835. jlong id,
  836. jlong in_now,
  837. jlong in_nwid,
  838. jlong in_sourceMac,
  839. jlong in_destMac,
  840. jint in_etherType,
  841. jint in_vlanId,
  842. jbyteArray in_frameData,
  843. jlongArray out_nextBackgroundTaskDeadline)
  844. {
  845. int64_t nodeId = (int64_t) id;
  846. ZT_Node *node = findNode(nodeId);
  847. unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline);
  848. if(nbtd_len < 1)
  849. {
  850. // array for next background task length has 0 elements!
  851. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  852. }
  853. int64_t now = (int64_t)in_now;
  854. uint64_t nwid = (uint64_t)in_nwid;
  855. uint64_t sourceMac = (uint64_t)in_sourceMac;
  856. uint64_t destMac = (uint64_t)in_destMac;
  857. unsigned int etherType = (unsigned int)in_etherType;
  858. unsigned int vlanId = (unsigned int)in_vlanId;
  859. unsigned int frameLength = env->GetArrayLength(in_frameData);
  860. void *frameData = env->GetPrimitiveArrayCritical(in_frameData, NULL);
  861. //
  862. // need local copy of frameData because arbitrary code may run in ZT_Node_processVirtualNetworkFrame and no other JNI work may happen between GetPrimitiveArrayCritical / ReleasePrimitiveArrayCritical
  863. //
  864. void *localData = malloc(frameLength);
  865. memcpy(localData, frameData, frameLength);
  866. env->ReleasePrimitiveArrayCritical(in_frameData, frameData, 0);
  867. int64_t nextBackgroundTaskDeadline = 0;
  868. ZT_ResultCode rc = ZT_Node_processVirtualNetworkFrame(
  869. node,
  870. NULL,
  871. now,
  872. nwid,
  873. sourceMac,
  874. destMac,
  875. etherType,
  876. vlanId,
  877. (const void*)localData,
  878. frameLength,
  879. &nextBackgroundTaskDeadline);
  880. if (env->ExceptionCheck()) {
  881. LOGE("Exception calling ZT_Node_processVirtualNetworkFrame");
  882. free(localData);
  883. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  884. }
  885. if (rc != ZT_RESULT_OK) {
  886. LOGE("ZT_Node_processVirtualNetworkFrame returned: %d", rc);
  887. }
  888. free(localData);
  889. jlong *outDeadline = (jlong*)env->GetPrimitiveArrayCritical(out_nextBackgroundTaskDeadline, NULL);
  890. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  891. env->ReleasePrimitiveArrayCritical(out_nextBackgroundTaskDeadline, outDeadline, 0);
  892. return createResultObject(env, rc);
  893. }
  894. /*
  895. * Class: com_zerotier_sdk_Node
  896. * Method: processWirePacket
  897. * Signature: (JJJLjava/net/InetSocketAddress;[B[J)Lcom/zerotier/sdk/ResultCode;
  898. */
  899. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket(
  900. JNIEnv *env, jobject obj,
  901. jlong id,
  902. jlong in_now,
  903. jlong in_localSocket,
  904. jobject in_remoteAddress,
  905. jbyteArray in_packetData,
  906. jlongArray out_nextBackgroundTaskDeadline)
  907. {
  908. int64_t nodeId = (int64_t) id;
  909. ZT_Node *node = findNode(nodeId);
  910. unsigned int nbtd_len = (unsigned int)env->GetArrayLength(out_nextBackgroundTaskDeadline);
  911. if(nbtd_len < 1)
  912. {
  913. LOGE("nbtd_len < 1");
  914. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  915. }
  916. int64_t now = (int64_t)in_now;
  917. sockaddr_storage remoteAddress = fromSocketAddressObject(env, in_remoteAddress);
  918. if (env->ExceptionCheck() || isSocketAddressEmpty(remoteAddress)) {
  919. return NULL;
  920. }
  921. unsigned int packetLength = (unsigned int)env->GetArrayLength(in_packetData);
  922. if(packetLength == 0)
  923. {
  924. LOGE("Empty packet?!?");
  925. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  926. }
  927. void *packetData = env->GetPrimitiveArrayCritical(in_packetData, NULL);
  928. //
  929. // need local copy of packetData because arbitrary code may run in ZT_Node_processWirePacket and no other JNI work may happen between GetPrimitiveArrayCritical / ReleasePrimitiveArrayCritical
  930. //
  931. void *localData = malloc(packetLength);
  932. memcpy(localData, packetData, packetLength);
  933. env->ReleasePrimitiveArrayCritical(in_packetData, packetData, 0);
  934. int64_t nextBackgroundTaskDeadline = 0;
  935. ZT_ResultCode rc = ZT_Node_processWirePacket(
  936. node,
  937. NULL,
  938. now,
  939. in_localSocket,
  940. &remoteAddress,
  941. localData,
  942. packetLength,
  943. &nextBackgroundTaskDeadline);
  944. if (env->ExceptionCheck()) {
  945. LOGE("Exception calling ZT_Node_processWirePacket");
  946. free(localData);
  947. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  948. }
  949. if(rc != ZT_RESULT_OK)
  950. {
  951. LOGE("ZT_Node_processWirePacket returned: %d", rc);
  952. }
  953. free(localData);
  954. jlong *outDeadline = (jlong*)env->GetPrimitiveArrayCritical(out_nextBackgroundTaskDeadline, NULL);
  955. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  956. env->ReleasePrimitiveArrayCritical(out_nextBackgroundTaskDeadline, outDeadline, 0);
  957. return createResultObject(env, rc);
  958. }
  959. /*
  960. * Class: com_zerotier_sdk_Node
  961. * Method: processBackgroundTasks
  962. * Signature: (JJ[J)Lcom/zerotier/sdk/ResultCode;
  963. */
  964. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processBackgroundTasks(
  965. JNIEnv *env, jobject obj,
  966. jlong id,
  967. jlong in_now,
  968. jlongArray out_nextBackgroundTaskDeadline)
  969. {
  970. int64_t nodeId = (int64_t) id;
  971. ZT_Node *node = findNode(nodeId);
  972. unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline);
  973. if(nbtd_len < 1)
  974. {
  975. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  976. }
  977. int64_t now = (int64_t)in_now;
  978. int64_t nextBackgroundTaskDeadline = 0;
  979. ZT_ResultCode rc = ZT_Node_processBackgroundTasks(node, NULL, now, &nextBackgroundTaskDeadline);
  980. if (env->ExceptionCheck()) {
  981. LOGE("Exception calling ZT_Node_processBackgroundTasks");
  982. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  983. }
  984. if (rc != ZT_RESULT_OK) {
  985. LOGE("ZT_Node_processBackgroundTasks returned: %d", rc);
  986. }
  987. jlong *outDeadline = (jlong*)env->GetPrimitiveArrayCritical(out_nextBackgroundTaskDeadline, NULL);
  988. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  989. env->ReleasePrimitiveArrayCritical(out_nextBackgroundTaskDeadline, outDeadline, 0);
  990. return createResultObject(env, rc);
  991. }
  992. /*
  993. * Class: com_zerotier_sdk_Node
  994. * Method: join
  995. * Signature: (JJ)Lcom/zerotier/sdk/ResultCode;
  996. */
  997. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_join(
  998. JNIEnv *env, jobject obj, jlong id, jlong in_nwid)
  999. {
  1000. int64_t nodeId = (int64_t) id;
  1001. ZT_Node *node = findNode(nodeId);
  1002. uint64_t nwid = (uint64_t)in_nwid;
  1003. ZT_ResultCode rc = ZT_Node_join(node, nwid, NULL, NULL);
  1004. if (env->ExceptionCheck()) {
  1005. LOGE("Exception calling ZT_Node_join");
  1006. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  1007. }
  1008. return createResultObject(env, rc);
  1009. }
  1010. /*
  1011. * Class: com_zerotier_sdk_Node
  1012. * Method: leave
  1013. * Signature: (JJ)Lcom/zerotier/sdk/ResultCode;
  1014. */
  1015. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_leave(
  1016. JNIEnv *env, jobject obj, jlong id, jlong in_nwid)
  1017. {
  1018. int64_t nodeId = (int64_t) id;
  1019. ZT_Node *node = findNode(nodeId);
  1020. uint64_t nwid = (uint64_t)in_nwid;
  1021. ZT_ResultCode rc = ZT_Node_leave(node, nwid, NULL, NULL);
  1022. if (env->ExceptionCheck()) {
  1023. LOGE("Exception calling ZT_Node_leave");
  1024. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  1025. }
  1026. return createResultObject(env, rc);
  1027. }
  1028. /*
  1029. * Class: com_zerotier_sdk_Node
  1030. * Method: multicastSubscribe
  1031. * Signature: (JJJJ)Lcom/zerotier/sdk/ResultCode;
  1032. */
  1033. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastSubscribe(
  1034. JNIEnv *env, jobject obj,
  1035. jlong id,
  1036. jlong in_nwid,
  1037. jlong in_multicastGroup,
  1038. jlong in_multicastAdi)
  1039. {
  1040. int64_t nodeId = (int64_t) id;
  1041. ZT_Node *node = findNode(nodeId);
  1042. uint64_t nwid = (uint64_t)in_nwid;
  1043. uint64_t multicastGroup = (uint64_t)in_multicastGroup;
  1044. unsigned long multicastAdi = (unsigned long)in_multicastAdi;
  1045. ZT_ResultCode rc = ZT_Node_multicastSubscribe(
  1046. node, NULL, nwid, multicastGroup, multicastAdi);
  1047. if (env->ExceptionCheck()) {
  1048. LOGE("Exception calling ZT_Node_multicastSubscribe");
  1049. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  1050. }
  1051. return createResultObject(env, rc);
  1052. }
  1053. /*
  1054. * Class: com_zerotier_sdk_Node
  1055. * Method: multicastUnsubscribe
  1056. * Signature: (JJJJ)Lcom/zerotier/sdk/ResultCode;
  1057. */
  1058. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastUnsubscribe(
  1059. JNIEnv *env, jobject obj,
  1060. jlong id,
  1061. jlong in_nwid,
  1062. jlong in_multicastGroup,
  1063. jlong in_multicastAdi)
  1064. {
  1065. int64_t nodeId = (int64_t) id;
  1066. ZT_Node *node = findNode(nodeId);
  1067. uint64_t nwid = (uint64_t)in_nwid;
  1068. uint64_t multicastGroup = (uint64_t)in_multicastGroup;
  1069. unsigned long multicastAdi = (unsigned long)in_multicastAdi;
  1070. ZT_ResultCode rc = ZT_Node_multicastUnsubscribe(
  1071. node, nwid, multicastGroup, multicastAdi);
  1072. if (env->ExceptionCheck()) {
  1073. LOGE("Exception calling ZT_Node_multicastUnsubscribe");
  1074. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  1075. }
  1076. return createResultObject(env, rc);
  1077. }
  1078. /*
  1079. * Class: com_zerotier_sdk_Node
  1080. * Method: orbit
  1081. * Signature: (JJJ)Lcom/zerotier/sdk/ResultCode;
  1082. */
  1083. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_orbit(
  1084. JNIEnv *env, jobject obj,
  1085. jlong id,
  1086. jlong in_moonWorldId,
  1087. jlong in_moonSeed)
  1088. {
  1089. int64_t nodeId = (int64_t)id;
  1090. ZT_Node *node = findNode(nodeId);
  1091. uint64_t moonWorldId = (uint64_t)in_moonWorldId;
  1092. uint64_t moonSeed = (uint64_t)in_moonSeed;
  1093. ZT_ResultCode rc = ZT_Node_orbit(node, NULL, moonWorldId, moonSeed);
  1094. if (env->ExceptionCheck()) {
  1095. LOGE("Exception calling ZT_Node_orbit");
  1096. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  1097. }
  1098. return createResultObject(env, rc);
  1099. }
  1100. /*
  1101. * Class: com_zerotier_sdk_Node
  1102. * Method: deorbit
  1103. * Signature: (JJ)L/com/zerotier/sdk/ResultCode;
  1104. */
  1105. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_deorbit(
  1106. JNIEnv *env, jobject obj,
  1107. jlong id,
  1108. jlong in_moonWorldId)
  1109. {
  1110. int64_t nodeId = (int64_t)id;
  1111. ZT_Node *node = findNode(nodeId);
  1112. uint64_t moonWorldId = (uint64_t)in_moonWorldId;
  1113. ZT_ResultCode rc = ZT_Node_deorbit(node, NULL, moonWorldId);
  1114. if (env->ExceptionCheck()) {
  1115. LOGE("Exception calling ZT_Node_deorbit");
  1116. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  1117. }
  1118. return createResultObject(env, rc);
  1119. }
  1120. /*
  1121. * Class: com_zerotier_sdk_Node
  1122. * Method: address
  1123. * Signature: (J)J
  1124. */
  1125. JNIEXPORT jlong JNICALL Java_com_zerotier_sdk_Node_address(
  1126. JNIEnv *env , jobject obj, jlong id)
  1127. {
  1128. int64_t nodeId = (int64_t) id;
  1129. ZT_Node *node = findNode(nodeId);
  1130. uint64_t address = ZT_Node_address(node);
  1131. return (jlong)address;
  1132. }
  1133. /*
  1134. * Class: com_zerotier_sdk_Node
  1135. * Method: status
  1136. * Signature: (J)Lcom/zerotier/sdk/NodeStatus;
  1137. */
  1138. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_status
  1139. (JNIEnv *env, jobject obj, jlong id)
  1140. {
  1141. int64_t nodeId = (int64_t) id;
  1142. ZT_Node *node = findNode(nodeId);
  1143. ZT_NodeStatus nodeStatus;
  1144. ZT_Node_status(node, &nodeStatus);
  1145. return newNodeStatus(env, nodeStatus);
  1146. }
  1147. /*
  1148. * Class: com_zerotier_sdk_Node
  1149. * Method: networkConfig
  1150. * Signature: (JJ)Lcom/zerotier/sdk/VirtualNetworkConfig;
  1151. */
  1152. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_networkConfig(
  1153. JNIEnv *env, jobject obj, jlong id, jlong nwid)
  1154. {
  1155. int64_t nodeId = (int64_t) id;
  1156. ZT_Node *node = findNode(nodeId);
  1157. ZT_VirtualNetworkConfig *vnetConfig = ZT_Node_networkConfig(node, nwid);
  1158. if (vnetConfig == NULL) {
  1159. LOGE("vnetConfig == NULL");
  1160. return NULL;
  1161. }
  1162. jobject vnetConfigObject = newNetworkConfig(env, *vnetConfig);
  1163. ZT_Node_freeQueryResult(node, vnetConfig);
  1164. return vnetConfigObject;
  1165. }
  1166. /*
  1167. * Class: com_zerotier_sdk_Node
  1168. * Method: version
  1169. * Signature: ()Lcom/zerotier/sdk/Version;
  1170. */
  1171. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_version(
  1172. JNIEnv *env, jobject obj)
  1173. {
  1174. int major = 0;
  1175. int minor = 0;
  1176. int revision = 0;
  1177. ZT_version(&major, &minor, &revision);
  1178. return newVersion(env, major, minor, revision);
  1179. }
  1180. /*
  1181. * Class: com_zerotier_sdk_Node
  1182. * Method: peers
  1183. * Signature: (J)[Lcom/zerotier/sdk/Peer;
  1184. */
  1185. JNIEXPORT jobjectArray JNICALL Java_com_zerotier_sdk_Node_peers(
  1186. JNIEnv *env, jobject obj, jlong id)
  1187. {
  1188. int64_t nodeId = (int64_t) id;
  1189. ZT_Node *node = findNode(nodeId);
  1190. ZT_PeerList *peerList = ZT_Node_peers(node);
  1191. if(peerList == NULL)
  1192. {
  1193. LOGE("ZT_Node_peers returned NULL");
  1194. return NULL;
  1195. }
  1196. jobjectArray peerArrayObj = newPeerArray(env, peerList->peers, peerList->peerCount);
  1197. ZT_Node_freeQueryResult(node, peerList);
  1198. peerList = NULL;
  1199. return peerArrayObj;
  1200. }
  1201. /*
  1202. * Class: com_zerotier_sdk_Node
  1203. * Method: networkConfigs
  1204. * Signature: (J)[Lcom/zerotier/sdk/VirtualNetworkConfig;
  1205. */
  1206. JNIEXPORT jobjectArray JNICALL Java_com_zerotier_sdk_Node_networkConfigs(
  1207. JNIEnv *env, jobject obj, jlong id)
  1208. {
  1209. int64_t nodeId = (int64_t) id;
  1210. ZT_Node *node = findNode(nodeId);
  1211. ZT_VirtualNetworkList *networkList = ZT_Node_networks(node);
  1212. if(networkList == NULL)
  1213. {
  1214. LOGE("ZT_Node_networks returned NULL");
  1215. return NULL;
  1216. }
  1217. jobjectArray networkListObject = newVirtualNetworkConfigArray(env, networkList->networks, networkList->networkCount);
  1218. ZT_Node_freeQueryResult(node, networkList);
  1219. return networkListObject;
  1220. }
  1221. #ifdef __cplusplus
  1222. } // extern "C"
  1223. #endif