com_zerotierone_sdk_Node.cpp 42 KB

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