com_zerotierone_sdk_Node.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  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 "ZT1_jniutils.h"
  29. #include "ZT1_jnicache.h"
  30. #include <ZeroTierOne.h>
  31. #include <map>
  32. #include <string>
  33. #include <assert.h>
  34. #include <string.h>
  35. // global static JNI Cache Object
  36. JniCache cache;
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. namespace {
  41. struct JniRef
  42. {
  43. JniRef()
  44. : jvm(NULL)
  45. , node(NULL)
  46. , dataStoreGetListener(NULL)
  47. , dataStorePutListener(NULL)
  48. , packetSender(NULL)
  49. , eventListener(NULL)
  50. , frameListener(NULL)
  51. , configListener(NULL)
  52. {}
  53. ~JniRef()
  54. {
  55. JNIEnv *env = NULL;
  56. jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  57. env->DeleteGlobalRef(dataStoreGetListener);
  58. env->DeleteGlobalRef(dataStorePutListener);
  59. env->DeleteGlobalRef(packetSender);
  60. env->DeleteGlobalRef(eventListener);
  61. env->DeleteGlobalRef(frameListener);
  62. env->DeleteGlobalRef(configListener);
  63. }
  64. uint64_t id;
  65. JavaVM *jvm;
  66. ZT1_Node *node;
  67. jobject dataStoreGetListener;
  68. jobject dataStorePutListener;
  69. jobject packetSender;
  70. jobject eventListener;
  71. jobject frameListener;
  72. jobject configListener;
  73. };
  74. int VirtualNetworkConfigFunctionCallback(
  75. ZT1_Node *node,
  76. void *userData,
  77. uint64_t nwid,
  78. enum ZT1_VirtualNetworkConfigOperation operation,
  79. const ZT1_VirtualNetworkConfig *config)
  80. {
  81. JniRef *ref = (JniRef*)userData;
  82. JNIEnv *env = NULL;
  83. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  84. jclass configListenerClass = env->GetObjectClass(ref->configListener);
  85. if(configListenerClass == NULL)
  86. {
  87. LOGE("Couldn't find class for VirtualNetworkConfigListener instance");
  88. return -1;
  89. }
  90. jmethodID configListenerCallbackMethod = cache.findMethod(configListenerClass,
  91. "onNetworkConfigurationUpdated",
  92. "(JLcom/zerotier/sdk/VirtualNetworkConfigOperation;Lcom/zerotier/sdk/VirtualNetworkConfig;)I");
  93. if(configListenerCallbackMethod == NULL)
  94. {
  95. LOGE("Couldn't find onVirtualNetworkFrame() method");
  96. return -2;
  97. }
  98. jobject operationObject = createVirtualNetworkConfigOperation(env, operation);
  99. if(operationObject == NULL)
  100. {
  101. LOGE("Error creating VirtualNetworkConfigOperation object");
  102. return -3;
  103. }
  104. jobject networkConfigObject = newNetworkConfig(env, *config);
  105. if(networkConfigObject == NULL)
  106. {
  107. LOGE("Error creating VirtualNetworkConfig object");
  108. return -4;
  109. }
  110. return env->CallIntMethod(
  111. ref->configListener,
  112. configListenerCallbackMethod,
  113. (jlong)nwid, operationObject, networkConfigObject);
  114. }
  115. void VirtualNetworkFrameFunctionCallback(ZT1_Node *node,void *userData,
  116. uint64_t nwid,
  117. uint64_t sourceMac,
  118. uint64_t destMac,
  119. unsigned int etherType,
  120. unsigned int vlanid,
  121. const void *frameData,
  122. unsigned int frameLength)
  123. {
  124. JniRef *ref = (JniRef*)userData;
  125. assert(ref->node == node);
  126. JNIEnv *env = NULL;
  127. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  128. jclass frameListenerClass = env->GetObjectClass(ref->frameListener);
  129. if(frameListenerClass == NULL)
  130. {
  131. LOGE("Couldn't find class for VirtualNetworkFrameListener instance");
  132. return;
  133. }
  134. jmethodID frameListenerCallbackMethod = cache.findMethod(
  135. frameListenerClass,
  136. "onVirtualNetworkFrame", "(JJJJJ[B)V");
  137. if(frameListenerCallbackMethod == NULL)
  138. {
  139. LOGE("Couldn't find onVirtualNetworkFrame() method");
  140. return;
  141. }
  142. jbyteArray dataArray = env->NewByteArray(frameLength);
  143. env->SetByteArrayRegion(dataArray, 0, frameLength, (jbyte*)frameData);
  144. env->CallVoidMethod(ref->frameListener, frameListenerCallbackMethod, nwid, sourceMac, destMac, etherType, vlanid, dataArray);
  145. }
  146. void EventCallback(ZT1_Node *node,void *userData,enum ZT1_Event event, const void *data)
  147. {
  148. JniRef *ref = (JniRef*)userData;
  149. assert(ref->node == node);
  150. JNIEnv *env = NULL;
  151. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  152. jclass eventListenerClass = env->GetObjectClass(ref->eventListener);
  153. if(eventListenerClass == NULL)
  154. {
  155. LOGE("Couldn't class for EventListener instance");
  156. return;
  157. }
  158. jmethodID onEventMethod = cache.findMethod(eventListenerClass,
  159. "onEvent", "(Lcom/zerotier/sdk/Event;)V");
  160. if(onEventMethod == NULL)
  161. {
  162. LOGE("Couldn't find onEvent method");
  163. return;
  164. }
  165. jmethodID onOutOfDateMethod = cache.findMethod(eventListenerClass,
  166. "onOutOfDate", "(Lcom/zerotier/sdk/Version;)V");
  167. if(onOutOfDateMethod == NULL)
  168. {
  169. LOGE("Couldn't find onOutOfDate method");
  170. return;
  171. }
  172. jmethodID onNetworkErrorMethod = cache.findMethod(eventListenerClass,
  173. "onNetworkError", "(Lcom/zerotier/sdk/Event;Ljava/net/InetSocketAddress;)V");
  174. if(onNetworkErrorMethod == NULL)
  175. {
  176. LOGE("Couldn't find onNetworkError method");
  177. return;
  178. }
  179. jmethodID onTraceMethod = cache.findMethod(eventListenerClass,
  180. "onTrace", "(Ljava/lang/String;)V");
  181. if(onTraceMethod == NULL)
  182. {
  183. LOGE("Couldn't find onTrace method");
  184. return;
  185. }
  186. jobject eventObject = createEvent(env, event);
  187. if(eventObject == NULL)
  188. {
  189. return;
  190. }
  191. switch(event)
  192. {
  193. case ZT1_EVENT_UP:
  194. case ZT1_EVENT_OFFLINE:
  195. case ZT1_EVENT_ONLINE:
  196. case ZT1_EVENT_DOWN:
  197. case ZT1_EVENT_FATAL_ERROR_IDENTITY_COLLISION:
  198. {
  199. // call onEvent()
  200. env->CallVoidMethod(ref->eventListener, onEventMethod, eventObject);
  201. }
  202. break;
  203. case ZT1_EVENT_SAW_MORE_RECENT_VERSION:
  204. {
  205. // call onOutOfDate()
  206. if(data != NULL)
  207. {
  208. int *version = (int*)data;
  209. jobject verisonObj = newVersion(env, version[0], version[1], version[2], 0);
  210. env->CallVoidMethod(ref->eventListener, onOutOfDateMethod, verisonObj);
  211. }
  212. }
  213. break;
  214. case ZT1_EVENT_AUTHENTICATION_FAILURE:
  215. case ZT1_EVENT_INVALID_PACKET:
  216. {
  217. // call onNetworkError()
  218. if(data != NULL)
  219. {
  220. sockaddr_storage *addr = (sockaddr_storage*)data;
  221. jobject addressObj = newInetSocketAddress(env, *addr);
  222. env->CallVoidMethod(ref->eventListener, onNetworkErrorMethod, addressObj);
  223. }
  224. }
  225. break;
  226. case ZT1_EVENT_TRACE:
  227. {
  228. // call onTrace()
  229. if(data != NULL)
  230. {
  231. const char* message = (const char*)data;
  232. jstring messageStr = env->NewStringUTF(message);
  233. env->CallVoidMethod(ref->eventListener, onTraceMethod, messageStr);
  234. }
  235. }
  236. break;
  237. }
  238. }
  239. long DataStoreGetFunction(ZT1_Node *node,void *userData,
  240. const char *objectName,
  241. void *buffer,
  242. unsigned long bufferSize,
  243. unsigned long bufferIndex,
  244. unsigned long *out_objectSize)
  245. {
  246. JniRef *ref = (JniRef*)userData;
  247. JNIEnv *env = NULL;
  248. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  249. jclass dataStoreGetClass = env->GetObjectClass(ref->dataStoreGetListener);
  250. if(dataStoreGetClass == NULL)
  251. {
  252. LOGE("Couldn't find class for DataStoreGetListener instance");
  253. return -2;
  254. }
  255. jmethodID dataStoreGetCallbackMethod = cache.findMethod(
  256. dataStoreGetClass,
  257. "onDataStoreGet",
  258. "(Ljava/lang/String;[BJ[J)J");
  259. if(dataStoreGetCallbackMethod == NULL)
  260. {
  261. LOGE("Couldn't find onDataStoreGet method");
  262. return -2;
  263. }
  264. jstring nameStr = env->NewStringUTF(objectName);
  265. if(nameStr == NULL)
  266. {
  267. LOGE("Error creating name string object");
  268. return -2; // out of memory
  269. }
  270. jbyteArray bufferObj = env->NewByteArray(bufferSize);
  271. if(bufferObj == NULL)
  272. {
  273. LOGE("Error creating byte[] buffer of size: %lu", bufferSize);
  274. return -2;
  275. }
  276. jlongArray objectSizeObj = env->NewLongArray(1);
  277. if(objectSizeObj == NULL)
  278. {
  279. LOGE("Error creating long[1] array for actual object size");
  280. return -2; // couldn't create long[1] array
  281. }
  282. LOGV("Calling onDataStoreGet(%s, %p, %lu, %p)",
  283. objectName, buffer, bufferIndex, objectSizeObj);
  284. long retval = (long)env->CallLongMethod(
  285. ref->dataStoreGetListener, dataStoreGetCallbackMethod,
  286. nameStr, bufferObj, (jlong)bufferIndex, objectSizeObj);
  287. if(retval > 0)
  288. {
  289. jbyte *data = env->GetByteArrayElements(bufferObj, NULL);
  290. memcpy(buffer, data, retval);
  291. env->ReleaseByteArrayElements(bufferObj, data, JNI_ABORT);
  292. jlong *objSize = env->GetLongArrayElements(objectSizeObj, NULL);
  293. *out_objectSize = (unsigned long)objSize[0];
  294. env->ReleaseLongArrayElements(objectSizeObj, objSize, JNI_ABORT);
  295. }
  296. LOGI("Out Object Size: %lu", *out_objectSize);
  297. return retval;
  298. }
  299. int DataStorePutFunction(ZT1_Node *node,void *userData,
  300. const char *objectName,
  301. const void *buffer,
  302. unsigned long bufferSize,
  303. int secure)
  304. {
  305. JniRef *ref = (JniRef*)userData;
  306. JNIEnv *env = NULL;
  307. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  308. jclass dataStorePutClass = env->GetObjectClass(ref->dataStorePutListener);
  309. if(dataStorePutClass == NULL)
  310. {
  311. LOGE("Couldn't find class for DataStorePutListener instance");
  312. return -1;
  313. }
  314. jmethodID dataStorePutCallbackMethod = cache.findMethod(
  315. dataStorePutClass,
  316. "onDataStorePut",
  317. "(Ljava/lang/String;[BZ)I");
  318. if(dataStorePutCallbackMethod == NULL)
  319. {
  320. LOGE("Couldn't find onDataStorePut method");
  321. return -2;
  322. }
  323. jmethodID deleteMethod = cache.findMethod(dataStorePutClass,
  324. "onDelete", "(Ljava/lang/String;)I");
  325. if(deleteMethod == NULL)
  326. {
  327. LOGE("Couldn't find onDelete method");
  328. return -3;
  329. }
  330. jstring nameStr = env->NewStringUTF(objectName);
  331. if(buffer == NULL)
  332. {
  333. // delete operation
  334. return env->CallIntMethod(
  335. ref->dataStorePutListener, deleteMethod, nameStr);
  336. }
  337. else
  338. {
  339. // set operation
  340. jbyteArray bufferObj = env->NewByteArray(bufferSize);
  341. env->SetByteArrayRegion(bufferObj, 0, bufferSize, (jbyte*)buffer);
  342. bool bsecure = secure != 0;
  343. return env->CallIntMethod(ref->dataStorePutListener,
  344. dataStorePutCallbackMethod,
  345. nameStr, bufferObj, bsecure);
  346. }
  347. }
  348. int WirePacketSendFunction(ZT1_Node *node,void *userData,\
  349. const struct sockaddr_storage *address,
  350. const void *buffer,
  351. unsigned int bufferSize)
  352. {
  353. JniRef *ref = (JniRef*)userData;
  354. assert(ref->node == node);
  355. JNIEnv *env = NULL;
  356. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  357. jclass packetSenderClass = env->GetObjectClass(ref->packetSender);
  358. if(packetSenderClass == NULL)
  359. {
  360. LOGE("Couldn't find class for PacketSender instance");
  361. return -1;
  362. }
  363. jmethodID packetSenderCallbackMethod = cache.findMethod(packetSenderClass,
  364. "onSendPacketRequested", "(Ljava/net/InetSocketAddress;[B)I");
  365. if(packetSenderCallbackMethod == NULL)
  366. {
  367. LOGE("Couldn't find onSendPacketRequested method");
  368. return -2;
  369. }
  370. jobject addressObj = newInetSocketAddress(env, *address);
  371. jbyteArray bufferObj = env->NewByteArray(bufferSize);
  372. env->SetByteArrayRegion(bufferObj, 0, bufferSize, (jbyte*)buffer);
  373. int retval = env->CallIntMethod(ref->packetSender, packetSenderCallbackMethod, addressObj, bufferObj);
  374. LOGD("JNI Packet Sender returned: %d", retval);
  375. return retval;
  376. }
  377. typedef std::map<uint64_t, JniRef*> NodeMap;
  378. static NodeMap nodeMap;
  379. ZT1_Node* findNode(uint64_t nodeId)
  380. {
  381. NodeMap::iterator found = nodeMap.find(nodeId);
  382. if(found != nodeMap.end())
  383. {
  384. JniRef *ref = found->second;
  385. return ref->node;
  386. }
  387. return NULL;
  388. }
  389. }
  390. JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
  391. {
  392. cache.setJavaVM(vm);
  393. return JNI_VERSION_1_6;
  394. }
  395. JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
  396. {
  397. cache.clearCache();
  398. }
  399. /*
  400. * Class: com_zerotier_sdk_Node
  401. * Method: node_init
  402. * Signature: (J)Lcom/zerotier/sdk/ResultCode;
  403. */
  404. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_node_1init(
  405. JNIEnv *env, jobject obj, jlong now)
  406. {
  407. LOGV("Creating ZT1_Node struct");
  408. jobject resultObject = createResultObject(env, ZT1_RESULT_OK);
  409. ZT1_Node *node;
  410. JniRef *ref = new JniRef;
  411. ref->id = (uint64_t)now;
  412. env->GetJavaVM(&ref->jvm);
  413. jclass cls = env->GetObjectClass(obj);
  414. jfieldID fid = cache.findField(
  415. cls, "getListener", "Lcom/zerotier/sdk/DataStoreGetListener;");
  416. if(fid == NULL)
  417. {
  418. return NULL; // exception already thrown
  419. }
  420. jobject tmp = env->GetObjectField(obj, fid);
  421. if(tmp == NULL)
  422. {
  423. return NULL;
  424. }
  425. ref->dataStoreGetListener = env->NewGlobalRef(tmp);
  426. fid = cache.findField(
  427. cls, "putListener", "Lcom/zerotier/sdk/DataStorePutListener;");
  428. if(fid == NULL)
  429. {
  430. return NULL; // exception already thrown
  431. }
  432. tmp = env->GetObjectField(obj, fid);
  433. if(tmp == NULL)
  434. {
  435. return NULL;
  436. }
  437. ref->dataStorePutListener = env->NewGlobalRef(tmp);
  438. fid = cache.findField(
  439. cls, "sender", "Lcom/zerotier/sdk/PacketSender;");
  440. if(fid == NULL)
  441. {
  442. return NULL; // exception already thrown
  443. }
  444. tmp = env->GetObjectField(obj, fid);
  445. if(tmp == NULL)
  446. {
  447. return NULL;
  448. }
  449. ref->packetSender = env->NewGlobalRef(tmp);
  450. fid = cache.findField(
  451. cls, "frameListener", "Lcom/zerotier/sdk/VirtualNetworkFrameListener;");
  452. if(fid == NULL)
  453. {
  454. return NULL; // exception already thrown
  455. }
  456. tmp = env->GetObjectField(obj, fid);
  457. if(tmp == NULL)
  458. {
  459. return NULL;
  460. }
  461. ref->frameListener = env->NewGlobalRef(tmp);
  462. fid = cache.findField(
  463. cls, "configListener", "Lcom/zerotier/sdk/VirtualNetworkConfigListener;");
  464. if(fid == NULL)
  465. {
  466. return NULL; // exception already thrown
  467. }
  468. tmp = env->GetObjectField(obj, fid);
  469. if(tmp == NULL)
  470. {
  471. return NULL;
  472. }
  473. ref->configListener = env->NewGlobalRef(tmp);
  474. fid = cache.findField(
  475. cls, "eventListener", "Lcom/zerotier/sdk/EventListener;");
  476. if(fid == NULL)
  477. {
  478. return NULL;
  479. }
  480. tmp = env->GetObjectField(obj, fid);
  481. if(tmp == NULL)
  482. {
  483. return NULL;
  484. }
  485. ref->eventListener = env->NewGlobalRef(tmp);
  486. ZT1_ResultCode rc = ZT1_Node_new(
  487. &node,
  488. ref,
  489. (uint64_t)now,
  490. &DataStoreGetFunction,
  491. &DataStorePutFunction,
  492. &WirePacketSendFunction,
  493. &VirtualNetworkFrameFunctionCallback,
  494. &VirtualNetworkConfigFunctionCallback,
  495. &EventCallback);
  496. if(rc != ZT1_RESULT_OK)
  497. {
  498. LOGE("Error creating Node: %d", rc);
  499. resultObject = createResultObject(env, rc);
  500. if(node)
  501. {
  502. ZT1_Node_delete(node);
  503. node = NULL;
  504. }
  505. delete ref;
  506. ref = NULL;
  507. return resultObject;
  508. }
  509. ref->node = node;
  510. nodeMap.insert(std::make_pair(ref->id, ref));
  511. return resultObject;
  512. }
  513. /*
  514. * Class: com_zerotier_sdk_Node
  515. * Method: node_delete
  516. * Signature: (J)V
  517. */
  518. JNIEXPORT void JNICALL Java_com_zerotier_sdk_Node_node_1delete(
  519. JNIEnv *env, jobject obj, jlong id)
  520. {
  521. LOGV("Destroying ZT1_Node struct");
  522. uint64_t nodeId = (uint64_t)id;
  523. NodeMap::iterator found = nodeMap.find(nodeId);
  524. if(found != nodeMap.end())
  525. {
  526. JniRef *ref = found->second;
  527. nodeMap.erase(found);
  528. ZT1_Node_delete(ref->node);
  529. delete ref;
  530. ref = NULL;
  531. }
  532. else
  533. {
  534. LOGE("Attempted to delete a node that doesn't exist!");
  535. }
  536. }
  537. /*
  538. * Class: com_zerotier_sdk_Node
  539. * Method: processVirtualNetworkFrame
  540. * Signature: (JJJJJII[B[J)Lcom/zerotier/sdk/ResultCode;
  541. */
  542. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processVirtualNetworkFrame(
  543. JNIEnv *env, jobject obj,
  544. jlong id,
  545. jlong in_now,
  546. jlong in_nwid,
  547. jlong in_sourceMac,
  548. jlong in_destMac,
  549. jint in_etherType,
  550. jint in_vlanId,
  551. jbyteArray in_frameData,
  552. jlongArray out_nextBackgroundTaskDeadline)
  553. {
  554. uint64_t nodeId = (uint64_t) id;
  555. ZT1_Node *node = findNode(nodeId);
  556. if(node == NULL)
  557. {
  558. // cannot find valid node. We should never get here.
  559. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  560. }
  561. unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline);
  562. if(nbtd_len < 1)
  563. {
  564. // array for next background task length has 0 elements!
  565. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  566. }
  567. uint64_t now = (uint64_t)in_now;
  568. uint64_t nwid = (uint64_t)in_nwid;
  569. uint64_t sourceMac = (uint64_t)in_sourceMac;
  570. uint64_t destMac = (uint64_t)in_destMac;
  571. unsigned int etherType = (unsigned int)in_etherType;
  572. unsigned int vlanId = (unsigned int)in_vlanId;
  573. unsigned int frameLength = env->GetArrayLength(in_frameData);
  574. jbyte *frameData =env->GetByteArrayElements(in_frameData, NULL);
  575. uint64_t nextBackgroundTaskDeadline = 0;
  576. ZT1_ResultCode rc = ZT1_Node_processVirtualNetworkFrame(
  577. node,
  578. now,
  579. nwid,
  580. sourceMac,
  581. destMac,
  582. etherType,
  583. vlanId,
  584. (const void*)frameData,
  585. frameLength,
  586. &nextBackgroundTaskDeadline);
  587. jlong *outDeadline = env->GetLongArrayElements(out_nextBackgroundTaskDeadline, NULL);
  588. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  589. env->ReleaseLongArrayElements(out_nextBackgroundTaskDeadline, outDeadline, 0);
  590. env->ReleaseByteArrayElements(in_frameData, frameData, 0);
  591. return createResultObject(env, rc);
  592. }
  593. /*
  594. * Class: com_zerotier_sdk_Node
  595. * Method: processWirePacket
  596. * Signature: (JJLjava/net/InetSocketAddress;I[B[J)Lcom/zerotier/sdk/ResultCode;
  597. */
  598. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket(
  599. JNIEnv *env, jobject obj,
  600. jlong id,
  601. jlong in_now,
  602. jobject in_remoteAddress,
  603. jbyteArray in_packetData,
  604. jlongArray out_nextBackgroundTaskDeadline)
  605. {
  606. uint64_t nodeId = (uint64_t) id;
  607. ZT1_Node *node = findNode(nodeId);
  608. if(node == NULL)
  609. {
  610. // cannot find valid node. We should never get here.
  611. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  612. }
  613. unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline);
  614. if(nbtd_len < 1)
  615. {
  616. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  617. }
  618. uint64_t now = (uint64_t)in_now;
  619. // get the java.net.InetSocketAddress class and getAddress() method
  620. jclass inetAddressClass = cache.findClass("java/net/InetAddress");
  621. if(inetAddressClass == NULL)
  622. {
  623. // can't find java.net.InetAddress
  624. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  625. }
  626. jmethodID getAddressMethod = cache.findMethod(
  627. inetAddressClass, "getAddress", "()[B");
  628. if(getAddressMethod == NULL)
  629. {
  630. // cant find InetAddress.getAddres()
  631. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  632. }
  633. jclass InetSocketAddressClass = cache.findClass("java/net/InetSocketAddress");
  634. if(InetSocketAddressClass == NULL)
  635. {
  636. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  637. }
  638. jmethodID inetSockGetAddressMethod = cache.findMethod(
  639. InetSocketAddressClass, "getAddress", "()Ljava/net/InetAddress;");
  640. jobject addrObject = env->CallObjectMethod(in_remoteAddress, inetSockGetAddressMethod);
  641. if(addrObject == NULL)
  642. {
  643. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  644. }
  645. // Call InetAddress.getAddress()
  646. jbyteArray addressArray = (jbyteArray)env->CallObjectMethod(addrObject, getAddressMethod);
  647. if(addressArray == NULL)
  648. {
  649. // unable to call getAddress()
  650. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  651. }
  652. unsigned int addrSize = env->GetArrayLength(addressArray);
  653. // get the address bytes
  654. jbyte *addr = env->GetByteArrayElements(addressArray, NULL);
  655. sockaddr_storage remoteAddress = {};
  656. if(addrSize == 16)
  657. {
  658. // IPV6 address
  659. sockaddr_in6 ipv6 = {};
  660. ipv6.sin6_family = AF_INET6;
  661. memcpy(ipv6.sin6_addr.s6_addr, addr, 16);
  662. memcpy(&remoteAddress, &ipv6, sizeof(sockaddr_in6));
  663. }
  664. else if(addrSize == 4)
  665. {
  666. // IPV4 address
  667. sockaddr_in ipv4 = {};
  668. ipv4.sin_family = AF_INET;
  669. memcpy(&ipv4.sin_addr, addr, 4);
  670. memcpy(&remoteAddress, &ipv4, sizeof(sockaddr_in));
  671. }
  672. else
  673. {
  674. // unknown address type
  675. env->ReleaseByteArrayElements(addressArray, addr, 0);
  676. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  677. }
  678. unsigned int packetLength = env->GetArrayLength(in_packetData);
  679. jbyte *packetData = env->GetByteArrayElements(in_packetData, NULL);
  680. uint64_t nextBackgroundTaskDeadline = 0;
  681. ZT1_ResultCode rc = ZT1_Node_processWirePacket(
  682. node,
  683. now,
  684. &remoteAddress,
  685. packetData,
  686. packetLength,
  687. &nextBackgroundTaskDeadline);
  688. jlong *outDeadline = env->GetLongArrayElements(out_nextBackgroundTaskDeadline, NULL);
  689. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  690. env->ReleaseLongArrayElements(out_nextBackgroundTaskDeadline, outDeadline, 0);
  691. env->ReleaseByteArrayElements(addressArray, addr, 0);
  692. env->ReleaseByteArrayElements(in_packetData, packetData, 0);
  693. return createResultObject(env, rc);
  694. }
  695. /*
  696. * Class: com_zerotier_sdk_Node
  697. * Method: processBackgroundTasks
  698. * Signature: (JJ[J)Lcom/zerotier/sdk/ResultCode;
  699. */
  700. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processBackgroundTasks(
  701. JNIEnv *env, jobject obj,
  702. jlong id,
  703. jlong in_now,
  704. jlongArray out_nextBackgroundTaskDeadline)
  705. {
  706. uint64_t nodeId = (uint64_t) id;
  707. ZT1_Node *node = findNode(nodeId);
  708. if(node == NULL)
  709. {
  710. // cannot find valid node. We should never get here.
  711. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  712. }
  713. unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline);
  714. if(nbtd_len < 1)
  715. {
  716. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  717. }
  718. uint64_t now = (uint64_t)in_now;
  719. uint64_t nextBackgroundTaskDeadline = 0;
  720. ZT1_ResultCode rc = ZT1_Node_processBackgroundTasks(node, now, &nextBackgroundTaskDeadline);
  721. jlong *outDeadline = env->GetLongArrayElements(out_nextBackgroundTaskDeadline, NULL);
  722. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  723. env->ReleaseLongArrayElements(out_nextBackgroundTaskDeadline, outDeadline, 0);
  724. return createResultObject(env, rc);
  725. }
  726. /*
  727. * Class: com_zerotier_sdk_Node
  728. * Method: join
  729. * Signature: (JJ)Lcom/zerotier/sdk/ResultCode;
  730. */
  731. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_join(
  732. JNIEnv *env, jobject obj, jlong id, jlong in_nwid)
  733. {
  734. uint64_t nodeId = (uint64_t) id;
  735. ZT1_Node *node = findNode(nodeId);
  736. if(node == NULL)
  737. {
  738. // cannot find valid node. We should never get here.
  739. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  740. }
  741. uint64_t nwid = (uint64_t)in_nwid;
  742. ZT1_ResultCode rc = ZT1_Node_join(node, nwid);
  743. return createResultObject(env, rc);
  744. }
  745. /*
  746. * Class: com_zerotier_sdk_Node
  747. * Method: leave
  748. * Signature: (JJ)Lcom/zerotier/sdk/ResultCode;
  749. */
  750. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_leave(
  751. JNIEnv *env, jobject obj, jlong id, jlong in_nwid)
  752. {
  753. uint64_t nodeId = (uint64_t) id;
  754. ZT1_Node *node = findNode(nodeId);
  755. if(node == NULL)
  756. {
  757. // cannot find valid node. We should never get here.
  758. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  759. }
  760. uint64_t nwid = (uint64_t)in_nwid;
  761. ZT1_ResultCode rc = ZT1_Node_leave(node, nwid);
  762. return createResultObject(env, rc);
  763. }
  764. /*
  765. * Class: com_zerotier_sdk_Node
  766. * Method: multicastSubscribe
  767. * Signature: (JJJJ)Lcom/zerotier/sdk/ResultCode;
  768. */
  769. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastSubscribe(
  770. JNIEnv *env, jobject obj,
  771. jlong id,
  772. jlong in_nwid,
  773. jlong in_multicastGroup,
  774. jlong in_multicastAdi)
  775. {
  776. uint64_t nodeId = (uint64_t) id;
  777. ZT1_Node *node = findNode(nodeId);
  778. if(node == NULL)
  779. {
  780. // cannot find valid node. We should never get here.
  781. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  782. }
  783. uint64_t nwid = (uint64_t)in_nwid;
  784. uint64_t multicastGroup = (uint64_t)in_multicastGroup;
  785. unsigned long multicastAdi = (unsigned long)in_multicastAdi;
  786. ZT1_ResultCode rc = ZT1_Node_multicastSubscribe(
  787. node, nwid, multicastGroup, multicastAdi);
  788. return createResultObject(env, rc);
  789. }
  790. /*
  791. * Class: com_zerotier_sdk_Node
  792. * Method: multicastUnsubscribe
  793. * Signature: (JJJJ)Lcom/zerotier/sdk/ResultCode;
  794. */
  795. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastUnsubscribe(
  796. JNIEnv *env, jobject obj,
  797. jlong id,
  798. jlong in_nwid,
  799. jlong in_multicastGroup,
  800. jlong in_multicastAdi)
  801. {
  802. uint64_t nodeId = (uint64_t) id;
  803. ZT1_Node *node = findNode(nodeId);
  804. if(node == NULL)
  805. {
  806. // cannot find valid node. We should never get here.
  807. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  808. }
  809. uint64_t nwid = (uint64_t)in_nwid;
  810. uint64_t multicastGroup = (uint64_t)in_multicastGroup;
  811. unsigned long multicastAdi = (unsigned long)in_multicastAdi;
  812. ZT1_ResultCode rc = ZT1_Node_multicastUnsubscribe(
  813. node, nwid, multicastGroup, multicastAdi);
  814. return createResultObject(env, rc);
  815. }
  816. /*
  817. * Class: com_zerotier_sdk_Node
  818. * Method: address
  819. * Signature: (J)J
  820. */
  821. JNIEXPORT jlong JNICALL Java_com_zerotier_sdk_Node_address(
  822. JNIEnv *env , jobject obj, jlong id)
  823. {
  824. uint64_t nodeId = (uint64_t) id;
  825. ZT1_Node *node = findNode(nodeId);
  826. if(node == NULL)
  827. {
  828. // cannot find valid node. We should never get here.
  829. return 0;
  830. }
  831. uint64_t address = ZT1_Node_address(node);
  832. return (jlong)address;
  833. }
  834. /*
  835. * Class: com_zerotier_sdk_Node
  836. * Method: status
  837. * Signature: (J)Lcom/zerotier/sdk/NodeStatus;
  838. */
  839. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_status
  840. (JNIEnv *env, jobject obj, jlong id)
  841. {
  842. uint64_t nodeId = (uint64_t) id;
  843. ZT1_Node *node = findNode(nodeId);
  844. if(node == NULL)
  845. {
  846. // cannot find valid node. We should never get here.
  847. return 0;
  848. }
  849. jclass nodeStatusClass = NULL;
  850. jmethodID nodeStatusConstructor = NULL;
  851. // create a com.zerotier.sdk.NodeStatus object
  852. nodeStatusClass = cache.findClass("com/zerotier/sdk/NodeStatus");
  853. if(nodeStatusClass == NULL)
  854. {
  855. return NULL;
  856. }
  857. nodeStatusConstructor = cache.findMethod(
  858. nodeStatusClass, "<init>", "()V");
  859. if(nodeStatusConstructor == NULL)
  860. {
  861. return NULL;
  862. }
  863. jobject nodeStatusObj = env->NewObject(nodeStatusClass, nodeStatusConstructor);
  864. if(nodeStatusObj == NULL)
  865. {
  866. return NULL;
  867. }
  868. ZT1_NodeStatus nodeStatus;
  869. ZT1_Node_status(node, &nodeStatus);
  870. jfieldID addressField = NULL;
  871. jfieldID publicIdentityField = NULL;
  872. jfieldID secretIdentityField = NULL;
  873. jfieldID onlineField = NULL;
  874. addressField = cache.findField(nodeStatusClass, "address", "J");
  875. if(addressField == NULL)
  876. {
  877. return NULL;
  878. }
  879. publicIdentityField = cache.findField(nodeStatusClass, "publicIdentity", "Ljava/lang/String;");
  880. if(publicIdentityField == NULL)
  881. {
  882. return NULL;
  883. }
  884. secretIdentityField = cache.findField(nodeStatusClass, "secretIdentity", "Ljava/lang/String;");
  885. if(secretIdentityField == NULL)
  886. {
  887. return NULL;
  888. }
  889. onlineField = cache.findField(nodeStatusClass, "online", "Z");
  890. if(onlineField == NULL)
  891. {
  892. return NULL;
  893. }
  894. env->SetLongField(nodeStatusObj, addressField, nodeStatus.address);
  895. jstring pubIdentStr = env->NewStringUTF(nodeStatus.publicIdentity);
  896. if(pubIdentStr == NULL)
  897. {
  898. return NULL; // out of memory
  899. }
  900. env->SetObjectField(nodeStatusObj, publicIdentityField, pubIdentStr);
  901. jstring secIdentStr = env->NewStringUTF(nodeStatus.secretIdentity);
  902. if(secIdentStr == NULL)
  903. {
  904. return NULL; // out of memory
  905. }
  906. env->SetObjectField(nodeStatusObj, secretIdentityField, secIdentStr);
  907. env->SetBooleanField(nodeStatusObj, onlineField, nodeStatus.online);
  908. return nodeStatusObj;
  909. }
  910. /*
  911. * Class: com_zerotier_sdk_Node
  912. * Method: networkConfig
  913. * Signature: (J)Lcom/zerotier/sdk/VirtualNetworkConfig;
  914. */
  915. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_networkConfig(
  916. JNIEnv *env, jobject obj, jlong id, jlong nwid)
  917. {
  918. uint64_t nodeId = (uint64_t) id;
  919. ZT1_Node *node = findNode(nodeId);
  920. if(node == NULL)
  921. {
  922. // cannot find valid node. We should never get here.
  923. return 0;
  924. }
  925. ZT1_VirtualNetworkConfig *vnetConfig = ZT1_Node_networkConfig(node, nwid);
  926. jobject vnetConfigObject = newNetworkConfig(env, *vnetConfig);
  927. ZT1_Node_freeQueryResult(node, vnetConfig);
  928. return vnetConfigObject;
  929. }
  930. /*
  931. * Class: com_zerotier_sdk_Node
  932. * Method: version
  933. * Signature: (J)Lcom/zerotier/sdk/Version;
  934. */
  935. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_version(
  936. JNIEnv *env, jobject obj)
  937. {
  938. int major = 0;
  939. int minor = 0;
  940. int revision = 0;
  941. unsigned long featureFlags = 0;
  942. ZT1_version(&major, &minor, &revision, &featureFlags);
  943. return newVersion(env, major, minor, revision, featureFlags);
  944. }
  945. /*
  946. * Class: com_zerotier_sdk_Node
  947. * Method: peers
  948. * Signature: (J)[Lcom/zerotier/sdk/Peer;
  949. */
  950. JNIEXPORT jobjectArray JNICALL Java_com_zerotier_sdk_Node_peers(
  951. JNIEnv *env, jobject obj, jlong id)
  952. {
  953. uint64_t nodeId = (uint64_t) id;
  954. ZT1_Node *node = findNode(nodeId);
  955. if(node == NULL)
  956. {
  957. // cannot find valid node. We should never get here.
  958. return 0;
  959. }
  960. ZT1_PeerList *peerList = ZT1_Node_peers(node);
  961. if(peerList == NULL)
  962. {
  963. LOGE("ZT1_Node_peers returned NULL");
  964. return NULL;
  965. }
  966. int peerCount = peerList->peerCount * 100;
  967. LOGV("Ensure Local Capacity: %d", peerCount);
  968. if(env->EnsureLocalCapacity(peerCount))
  969. {
  970. LOGE("EnsureLocalCapacity failed!!");
  971. ZT1_Node_freeQueryResult(node, peerList);
  972. return NULL;
  973. }
  974. jclass peerClass = cache.findClass("com/zerotier/sdk/Peer");
  975. if(env->ExceptionCheck() || peerClass == NULL)
  976. {
  977. LOGE("Error finding Peer class");
  978. ZT1_Node_freeQueryResult(node, peerList);
  979. return NULL;
  980. }
  981. jobjectArray peerArrayObj = env->NewObjectArray(
  982. peerList->peerCount, peerClass, NULL);
  983. if(env->ExceptionCheck() || peerArrayObj == NULL)
  984. {
  985. LOGE("Error creating Peer[] array");
  986. ZT1_Node_freeQueryResult(node, peerList);
  987. return NULL;
  988. }
  989. for(unsigned int i = 0; i < peerList->peerCount; ++i)
  990. {
  991. jobject peerObj = newPeer(env, peerList->peers[i]);
  992. env->SetObjectArrayElement(peerArrayObj, i, peerObj);
  993. if(env->ExceptionCheck())
  994. {
  995. LOGE("Error assigning Peer object to array");
  996. break;
  997. }
  998. }
  999. ZT1_Node_freeQueryResult(node, peerList);
  1000. peerList = NULL;
  1001. return peerArrayObj;
  1002. }
  1003. /*
  1004. * Class: com_zerotier_sdk_Node
  1005. * Method: networks
  1006. * Signature: (J)[Lcom/zerotier/sdk/VirtualNetworkConfig;
  1007. */
  1008. JNIEXPORT jobjectArray JNICALL Java_com_zerotier_sdk_Node_networks(
  1009. JNIEnv *env, jobject obj, jlong id)
  1010. {
  1011. uint64_t nodeId = (uint64_t) id;
  1012. ZT1_Node *node = findNode(nodeId);
  1013. if(node == NULL)
  1014. {
  1015. // cannot find valid node. We should never get here.
  1016. return 0;
  1017. }
  1018. ZT1_VirtualNetworkList *networkList = ZT1_Node_networks(node);
  1019. if(networkList == NULL)
  1020. {
  1021. return NULL;
  1022. }
  1023. jclass vnetConfigClass = cache.findClass("com/zerotier/sdk/VirtualNetworkConfig");
  1024. if(env->ExceptionCheck() || vnetConfigClass == NULL)
  1025. {
  1026. LOGE("Error finding VirtualNetworkConfig class");
  1027. ZT1_Node_freeQueryResult(node, networkList);
  1028. return NULL;
  1029. }
  1030. jobjectArray networkListObject = env->NewObjectArray(
  1031. networkList->networkCount, vnetConfigClass, NULL);
  1032. if(env->ExceptionCheck() || networkListObject == NULL)
  1033. {
  1034. LOGE("Error creating VirtualNetworkConfig[] array");
  1035. ZT1_Node_freeQueryResult(node, networkList);
  1036. return NULL;
  1037. }
  1038. for(unsigned int i = 0; i < networkList->networkCount; ++i)
  1039. {
  1040. jobject networkObject = newNetworkConfig(env, networkList->networks[i]);
  1041. env->SetObjectArrayElement(networkListObject, i, networkObject);
  1042. if(env->ExceptionCheck())
  1043. {
  1044. LOGE("Error assigning VirtualNetworkConfig object to array");
  1045. break;
  1046. }
  1047. }
  1048. ZT1_Node_freeQueryResult(node, networkList);
  1049. return networkListObject;
  1050. }
  1051. #ifdef __cplusplus
  1052. } // extern "C"
  1053. #endif