com_zerotierone_sdk_Node.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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. env->GetByteArrayRegion(bufferObj, 0, retval, (jbyte*)buffer);
  290. env->GetLongArrayRegion(objectSizeObj, 0, 1, (jlong*)out_objectSize);
  291. env->DeleteLocalRef(bufferObj);
  292. env->DeleteLocalRef(objectSizeObj);
  293. }
  294. LOGI("Out Object Size: %lu", *out_objectSize);
  295. return retval;
  296. }
  297. int DataStorePutFunction(ZT1_Node *node,void *userData,
  298. const char *objectName,
  299. const void *buffer,
  300. unsigned long bufferSize,
  301. int secure)
  302. {
  303. JniRef *ref = (JniRef*)userData;
  304. JNIEnv *env = NULL;
  305. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  306. jclass dataStorePutClass = env->GetObjectClass(ref->dataStorePutListener);
  307. if(dataStorePutClass == NULL)
  308. {
  309. LOGE("Couldn't find class for DataStorePutListener instance");
  310. return -1;
  311. }
  312. jmethodID dataStorePutCallbackMethod = cache.findMethod(
  313. dataStorePutClass,
  314. "onDataStorePut",
  315. "(Ljava/lang/String;[BZ)I");
  316. if(dataStorePutCallbackMethod == NULL)
  317. {
  318. LOGE("Couldn't find onDataStorePut method");
  319. return -2;
  320. }
  321. jmethodID deleteMethod = cache.findMethod(dataStorePutClass,
  322. "onDelete", "(Ljava/lang/String;)I");
  323. if(deleteMethod == NULL)
  324. {
  325. LOGE("Couldn't find onDelete method");
  326. return -3;
  327. }
  328. jstring nameStr = env->NewStringUTF(objectName);
  329. if(buffer == NULL)
  330. {
  331. // delete operation
  332. return env->CallIntMethod(
  333. ref->dataStorePutListener, deleteMethod, nameStr);
  334. }
  335. else
  336. {
  337. // set operation
  338. jbyteArray bufferObj = env->NewByteArray(bufferSize);
  339. env->SetByteArrayRegion(bufferObj, 0, bufferSize, (jbyte*)buffer);
  340. bool bsecure = secure != 0;
  341. return env->CallIntMethod(ref->dataStorePutListener,
  342. dataStorePutCallbackMethod,
  343. nameStr, bufferObj, bsecure);
  344. }
  345. }
  346. int WirePacketSendFunction(ZT1_Node *node,void *userData,\
  347. const struct sockaddr_storage *address,
  348. const void *buffer,
  349. unsigned int bufferSize)
  350. {
  351. JniRef *ref = (JniRef*)userData;
  352. assert(ref->node == node);
  353. JNIEnv *env = NULL;
  354. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  355. jclass packetSenderClass = env->GetObjectClass(ref->packetSender);
  356. if(packetSenderClass == NULL)
  357. {
  358. LOGE("Couldn't find class for PacketSender instance");
  359. return -1;
  360. }
  361. jmethodID packetSenderCallbackMethod = cache.findMethod(packetSenderClass,
  362. "onSendPacketRequested", "(Ljava/net/InetSocketAddress;[B)I");
  363. if(packetSenderCallbackMethod == NULL)
  364. {
  365. LOGE("Couldn't find onSendPacketRequested method");
  366. return -2;
  367. }
  368. jobject addressObj = newInetSocketAddress(env, *address);
  369. jbyteArray bufferObj = env->NewByteArray(bufferSize);
  370. env->SetByteArrayRegion(bufferObj, 0, bufferSize, (jbyte*)buffer);
  371. int retval = env->CallIntMethod(ref->packetSender, packetSenderCallbackMethod, addressObj, bufferObj);
  372. LOGD("JNI Packet Sender returned: %d", retval);
  373. return retval;
  374. }
  375. typedef std::map<uint64_t, JniRef*> NodeMap;
  376. static NodeMap nodeMap;
  377. ZT1_Node* findNode(uint64_t nodeId)
  378. {
  379. NodeMap::iterator found = nodeMap.find(nodeId);
  380. if(found != nodeMap.end())
  381. {
  382. JniRef *ref = found->second;
  383. return ref->node;
  384. }
  385. return NULL;
  386. }
  387. }
  388. JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
  389. {
  390. cache.setJavaVM(vm);
  391. return JNI_VERSION_1_6;
  392. }
  393. JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
  394. {
  395. cache.clearCache();
  396. }
  397. /*
  398. * Class: com_zerotier_sdk_Node
  399. * Method: node_init
  400. * Signature: (J)Lcom/zerotier/sdk/ResultCode;
  401. */
  402. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_node_1init(
  403. JNIEnv *env, jobject obj, jlong now)
  404. {
  405. LOGV("Creating ZT1_Node struct");
  406. jobject resultObject = createResultObject(env, ZT1_RESULT_OK);
  407. ZT1_Node *node;
  408. JniRef *ref = new JniRef;
  409. ref->id = (uint64_t)now;
  410. env->GetJavaVM(&ref->jvm);
  411. jclass cls = env->GetObjectClass(obj);
  412. jfieldID fid = cache.findField(
  413. cls, "getListener", "Lcom/zerotier/sdk/DataStoreGetListener;");
  414. if(fid == NULL)
  415. {
  416. return NULL; // exception already thrown
  417. }
  418. jobject tmp = env->GetObjectField(obj, fid);
  419. if(tmp == NULL)
  420. {
  421. return NULL;
  422. }
  423. ref->dataStoreGetListener = env->NewGlobalRef(tmp);
  424. fid = cache.findField(
  425. cls, "putListener", "Lcom/zerotier/sdk/DataStorePutListener;");
  426. if(fid == NULL)
  427. {
  428. return NULL; // exception already thrown
  429. }
  430. tmp = env->GetObjectField(obj, fid);
  431. if(tmp == NULL)
  432. {
  433. return NULL;
  434. }
  435. ref->dataStorePutListener = env->NewGlobalRef(tmp);
  436. fid = cache.findField(
  437. cls, "sender", "Lcom/zerotier/sdk/PacketSender;");
  438. if(fid == NULL)
  439. {
  440. return NULL; // exception already thrown
  441. }
  442. tmp = env->GetObjectField(obj, fid);
  443. if(tmp == NULL)
  444. {
  445. return NULL;
  446. }
  447. ref->packetSender = env->NewGlobalRef(tmp);
  448. fid = cache.findField(
  449. cls, "frameListener", "Lcom/zerotier/sdk/VirtualNetworkFrameListener;");
  450. if(fid == NULL)
  451. {
  452. return NULL; // exception already thrown
  453. }
  454. tmp = env->GetObjectField(obj, fid);
  455. if(tmp == NULL)
  456. {
  457. return NULL;
  458. }
  459. ref->frameListener = env->NewGlobalRef(tmp);
  460. fid = cache.findField(
  461. cls, "configListener", "Lcom/zerotier/sdk/VirtualNetworkConfigListener;");
  462. if(fid == NULL)
  463. {
  464. return NULL; // exception already thrown
  465. }
  466. tmp = env->GetObjectField(obj, fid);
  467. if(tmp == NULL)
  468. {
  469. return NULL;
  470. }
  471. ref->configListener = env->NewGlobalRef(tmp);
  472. fid = cache.findField(
  473. cls, "eventListener", "Lcom/zerotier/sdk/EventListener;");
  474. if(fid == NULL)
  475. {
  476. return NULL;
  477. }
  478. tmp = env->GetObjectField(obj, fid);
  479. if(tmp == NULL)
  480. {
  481. return NULL;
  482. }
  483. ref->eventListener = env->NewGlobalRef(tmp);
  484. ZT1_ResultCode rc = ZT1_Node_new(
  485. &node,
  486. ref,
  487. (uint64_t)now,
  488. &DataStoreGetFunction,
  489. &DataStorePutFunction,
  490. &WirePacketSendFunction,
  491. &VirtualNetworkFrameFunctionCallback,
  492. &VirtualNetworkConfigFunctionCallback,
  493. &EventCallback);
  494. if(rc != ZT1_RESULT_OK)
  495. {
  496. LOGE("Error creating Node: %d", rc);
  497. resultObject = createResultObject(env, rc);
  498. if(node)
  499. {
  500. ZT1_Node_delete(node);
  501. node = NULL;
  502. }
  503. delete ref;
  504. ref = NULL;
  505. return resultObject;
  506. }
  507. ref->node = node;
  508. nodeMap.insert(std::make_pair(ref->id, ref));
  509. return resultObject;
  510. }
  511. /*
  512. * Class: com_zerotier_sdk_Node
  513. * Method: node_delete
  514. * Signature: (J)V
  515. */
  516. JNIEXPORT void JNICALL Java_com_zerotier_sdk_Node_node_1delete(
  517. JNIEnv *env, jobject obj, jlong id)
  518. {
  519. LOGV("Destroying ZT1_Node struct");
  520. uint64_t nodeId = (uint64_t)id;
  521. NodeMap::iterator found = nodeMap.find(nodeId);
  522. if(found != nodeMap.end())
  523. {
  524. JniRef *ref = found->second;
  525. nodeMap.erase(found);
  526. ZT1_Node_delete(ref->node);
  527. delete ref;
  528. ref = NULL;
  529. }
  530. else
  531. {
  532. LOGE("Attempted to delete a node that doesn't exist!");
  533. }
  534. }
  535. /*
  536. * Class: com_zerotier_sdk_Node
  537. * Method: processVirtualNetworkFrame
  538. * Signature: (JJJJJII[B[J)Lcom/zerotier/sdk/ResultCode;
  539. */
  540. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processVirtualNetworkFrame(
  541. JNIEnv *env, jobject obj,
  542. jlong id,
  543. jlong in_now,
  544. jlong in_nwid,
  545. jlong in_sourceMac,
  546. jlong in_destMac,
  547. jint in_etherType,
  548. jint in_vlanId,
  549. jbyteArray in_frameData,
  550. jlongArray out_nextBackgroundTaskDeadline)
  551. {
  552. uint64_t nodeId = (uint64_t) id;
  553. ZT1_Node *node = findNode(nodeId);
  554. if(node == NULL)
  555. {
  556. // cannot find valid node. We should never get here.
  557. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  558. }
  559. unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline);
  560. if(nbtd_len < 1)
  561. {
  562. // array for next background task length has 0 elements!
  563. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  564. }
  565. uint64_t now = (uint64_t)in_now;
  566. uint64_t nwid = (uint64_t)in_nwid;
  567. uint64_t sourceMac = (uint64_t)in_sourceMac;
  568. uint64_t destMac = (uint64_t)in_destMac;
  569. unsigned int etherType = (unsigned int)in_etherType;
  570. unsigned int vlanId = (unsigned int)in_vlanId;
  571. unsigned int frameLength = env->GetArrayLength(in_frameData);
  572. jbyte *frameData =env->GetByteArrayElements(in_frameData, NULL);
  573. uint64_t nextBackgroundTaskDeadline = 0;
  574. ZT1_ResultCode rc = ZT1_Node_processVirtualNetworkFrame(
  575. node,
  576. now,
  577. nwid,
  578. sourceMac,
  579. destMac,
  580. etherType,
  581. vlanId,
  582. (const void*)frameData,
  583. frameLength,
  584. &nextBackgroundTaskDeadline);
  585. jlong *outDeadline = env->GetLongArrayElements(out_nextBackgroundTaskDeadline, NULL);
  586. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  587. env->ReleaseLongArrayElements(out_nextBackgroundTaskDeadline, outDeadline, 0);
  588. env->ReleaseByteArrayElements(in_frameData, frameData, 0);
  589. return createResultObject(env, rc);
  590. }
  591. /*
  592. * Class: com_zerotier_sdk_Node
  593. * Method: processWirePacket
  594. * Signature: (JJLjava/net/InetSocketAddress;I[B[J)Lcom/zerotier/sdk/ResultCode;
  595. */
  596. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket(
  597. JNIEnv *env, jobject obj,
  598. jlong id,
  599. jlong in_now,
  600. jobject in_remoteAddress,
  601. jbyteArray in_packetData,
  602. jlongArray out_nextBackgroundTaskDeadline)
  603. {
  604. uint64_t nodeId = (uint64_t) id;
  605. ZT1_Node *node = findNode(nodeId);
  606. if(node == NULL)
  607. {
  608. // cannot find valid node. We should never get here.
  609. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  610. }
  611. unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline);
  612. if(nbtd_len < 1)
  613. {
  614. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  615. }
  616. uint64_t now = (uint64_t)in_now;
  617. // get the java.net.InetSocketAddress class and getAddress() method
  618. jclass inetAddressClass = cache.findClass("java/net/InetAddress");
  619. if(inetAddressClass == NULL)
  620. {
  621. // can't find java.net.InetAddress
  622. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  623. }
  624. jmethodID getAddressMethod = cache.findMethod(
  625. inetAddressClass, "getAddress", "()[B");
  626. if(getAddressMethod == NULL)
  627. {
  628. // cant find InetAddress.getAddres()
  629. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  630. }
  631. jclass InetSocketAddressClass = cache.findClass("java/net/InetSocketAddress");
  632. if(InetSocketAddressClass == NULL)
  633. {
  634. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  635. }
  636. jmethodID inetSockGetAddressMethod = cache.findMethod(
  637. InetSocketAddressClass, "getAddress", "()Ljava/net/InetAddress;");
  638. jobject addrObject = env->CallObjectMethod(in_remoteAddress, inetSockGetAddressMethod);
  639. if(addrObject == NULL)
  640. {
  641. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  642. }
  643. // Call InetAddress.getAddress()
  644. jbyteArray addressArray = (jbyteArray)env->CallObjectMethod(addrObject, getAddressMethod);
  645. if(addressArray == NULL)
  646. {
  647. // unable to call getAddress()
  648. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  649. }
  650. unsigned int addrSize = env->GetArrayLength(addressArray);
  651. // get the address bytes
  652. jbyte *addr = env->GetByteArrayElements(addressArray, NULL);
  653. sockaddr_storage remoteAddress = {};
  654. if(addrSize == 16)
  655. {
  656. // IPV6 address
  657. sockaddr_in6 ipv6 = {};
  658. ipv6.sin6_family = AF_INET6;
  659. memcpy(ipv6.sin6_addr.s6_addr, addr, 16);
  660. memcpy(&remoteAddress, &ipv6, sizeof(sockaddr_in6));
  661. }
  662. else if(addrSize == 4)
  663. {
  664. // IPV4 address
  665. sockaddr_in ipv4 = {};
  666. ipv4.sin_family = AF_INET;
  667. memcpy(&ipv4.sin_addr, addr, 4);
  668. memcpy(&remoteAddress, &ipv4, sizeof(sockaddr_in));
  669. }
  670. else
  671. {
  672. // unknown address type
  673. env->ReleaseByteArrayElements(addressArray, addr, 0);
  674. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  675. }
  676. unsigned int packetLength = env->GetArrayLength(in_packetData);
  677. jbyte *packetData = env->GetByteArrayElements(in_packetData, NULL);
  678. uint64_t nextBackgroundTaskDeadline = 0;
  679. ZT1_ResultCode rc = ZT1_Node_processWirePacket(
  680. node,
  681. now,
  682. &remoteAddress,
  683. packetData,
  684. packetLength,
  685. &nextBackgroundTaskDeadline);
  686. jlong *outDeadline = env->GetLongArrayElements(out_nextBackgroundTaskDeadline, NULL);
  687. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  688. env->ReleaseLongArrayElements(out_nextBackgroundTaskDeadline, outDeadline, 0);
  689. env->ReleaseByteArrayElements(addressArray, addr, 0);
  690. env->ReleaseByteArrayElements(in_packetData, packetData, 0);
  691. return createResultObject(env, rc);
  692. }
  693. /*
  694. * Class: com_zerotier_sdk_Node
  695. * Method: processBackgroundTasks
  696. * Signature: (JJ[J)Lcom/zerotier/sdk/ResultCode;
  697. */
  698. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processBackgroundTasks(
  699. JNIEnv *env, jobject obj,
  700. jlong id,
  701. jlong in_now,
  702. jlongArray out_nextBackgroundTaskDeadline)
  703. {
  704. uint64_t nodeId = (uint64_t) id;
  705. ZT1_Node *node = findNode(nodeId);
  706. if(node == NULL)
  707. {
  708. // cannot find valid node. We should never get here.
  709. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  710. }
  711. unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline);
  712. if(nbtd_len < 1)
  713. {
  714. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  715. }
  716. uint64_t now = (uint64_t)in_now;
  717. uint64_t nextBackgroundTaskDeadline = 0;
  718. ZT1_ResultCode rc = ZT1_Node_processBackgroundTasks(node, now, &nextBackgroundTaskDeadline);
  719. jlong *outDeadline = env->GetLongArrayElements(out_nextBackgroundTaskDeadline, NULL);
  720. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  721. env->ReleaseLongArrayElements(out_nextBackgroundTaskDeadline, outDeadline, 0);
  722. return createResultObject(env, rc);
  723. }
  724. /*
  725. * Class: com_zerotier_sdk_Node
  726. * Method: join
  727. * Signature: (JJ)Lcom/zerotier/sdk/ResultCode;
  728. */
  729. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_join(
  730. JNIEnv *env, jobject obj, jlong id, jlong in_nwid)
  731. {
  732. uint64_t nodeId = (uint64_t) id;
  733. ZT1_Node *node = findNode(nodeId);
  734. if(node == NULL)
  735. {
  736. // cannot find valid node. We should never get here.
  737. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  738. }
  739. uint64_t nwid = (uint64_t)in_nwid;
  740. ZT1_ResultCode rc = ZT1_Node_join(node, nwid);
  741. return createResultObject(env, rc);
  742. }
  743. /*
  744. * Class: com_zerotier_sdk_Node
  745. * Method: leave
  746. * Signature: (JJ)Lcom/zerotier/sdk/ResultCode;
  747. */
  748. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_leave(
  749. JNIEnv *env, jobject obj, jlong id, jlong in_nwid)
  750. {
  751. uint64_t nodeId = (uint64_t) id;
  752. ZT1_Node *node = findNode(nodeId);
  753. if(node == NULL)
  754. {
  755. // cannot find valid node. We should never get here.
  756. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  757. }
  758. uint64_t nwid = (uint64_t)in_nwid;
  759. ZT1_ResultCode rc = ZT1_Node_leave(node, nwid);
  760. return createResultObject(env, rc);
  761. }
  762. /*
  763. * Class: com_zerotier_sdk_Node
  764. * Method: multicastSubscribe
  765. * Signature: (JJJJ)Lcom/zerotier/sdk/ResultCode;
  766. */
  767. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastSubscribe(
  768. JNIEnv *env, jobject obj,
  769. jlong id,
  770. jlong in_nwid,
  771. jlong in_multicastGroup,
  772. jlong in_multicastAdi)
  773. {
  774. uint64_t nodeId = (uint64_t) id;
  775. ZT1_Node *node = findNode(nodeId);
  776. if(node == NULL)
  777. {
  778. // cannot find valid node. We should never get here.
  779. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  780. }
  781. uint64_t nwid = (uint64_t)in_nwid;
  782. uint64_t multicastGroup = (uint64_t)in_multicastGroup;
  783. unsigned long multicastAdi = (unsigned long)in_multicastAdi;
  784. ZT1_ResultCode rc = ZT1_Node_multicastSubscribe(
  785. node, nwid, multicastGroup, multicastAdi);
  786. return createResultObject(env, rc);
  787. }
  788. /*
  789. * Class: com_zerotier_sdk_Node
  790. * Method: multicastUnsubscribe
  791. * Signature: (JJJJ)Lcom/zerotier/sdk/ResultCode;
  792. */
  793. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastUnsubscribe(
  794. JNIEnv *env, jobject obj,
  795. jlong id,
  796. jlong in_nwid,
  797. jlong in_multicastGroup,
  798. jlong in_multicastAdi)
  799. {
  800. uint64_t nodeId = (uint64_t) id;
  801. ZT1_Node *node = findNode(nodeId);
  802. if(node == NULL)
  803. {
  804. // cannot find valid node. We should never get here.
  805. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  806. }
  807. uint64_t nwid = (uint64_t)in_nwid;
  808. uint64_t multicastGroup = (uint64_t)in_multicastGroup;
  809. unsigned long multicastAdi = (unsigned long)in_multicastAdi;
  810. ZT1_ResultCode rc = ZT1_Node_multicastUnsubscribe(
  811. node, nwid, multicastGroup, multicastAdi);
  812. return createResultObject(env, rc);
  813. }
  814. /*
  815. * Class: com_zerotier_sdk_Node
  816. * Method: address
  817. * Signature: (J)J
  818. */
  819. JNIEXPORT jlong JNICALL Java_com_zerotier_sdk_Node_address(
  820. JNIEnv *env , jobject obj, jlong id)
  821. {
  822. uint64_t nodeId = (uint64_t) id;
  823. ZT1_Node *node = findNode(nodeId);
  824. if(node == NULL)
  825. {
  826. // cannot find valid node. We should never get here.
  827. return 0;
  828. }
  829. uint64_t address = ZT1_Node_address(node);
  830. return (jlong)address;
  831. }
  832. /*
  833. * Class: com_zerotier_sdk_Node
  834. * Method: status
  835. * Signature: (J)Lcom/zerotier/sdk/NodeStatus;
  836. */
  837. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_status
  838. (JNIEnv *env, jobject obj, jlong id)
  839. {
  840. uint64_t nodeId = (uint64_t) id;
  841. ZT1_Node *node = findNode(nodeId);
  842. if(node == NULL)
  843. {
  844. // cannot find valid node. We should never get here.
  845. return 0;
  846. }
  847. jclass nodeStatusClass = NULL;
  848. jmethodID nodeStatusConstructor = NULL;
  849. // create a com.zerotier.sdk.NodeStatus object
  850. nodeStatusClass = cache.findClass("com/zerotier/sdk/NodeStatus");
  851. if(nodeStatusClass == NULL)
  852. {
  853. return NULL;
  854. }
  855. nodeStatusConstructor = cache.findMethod(
  856. nodeStatusClass, "<init>", "()V");
  857. if(nodeStatusConstructor == NULL)
  858. {
  859. return NULL;
  860. }
  861. jobject nodeStatusObj = env->NewObject(nodeStatusClass, nodeStatusConstructor);
  862. if(nodeStatusObj == NULL)
  863. {
  864. return NULL;
  865. }
  866. ZT1_NodeStatus nodeStatus;
  867. ZT1_Node_status(node, &nodeStatus);
  868. jfieldID addressField = NULL;
  869. jfieldID publicIdentityField = NULL;
  870. jfieldID secretIdentityField = NULL;
  871. jfieldID onlineField = NULL;
  872. addressField = cache.findField(nodeStatusClass, "address", "J");
  873. if(addressField == NULL)
  874. {
  875. return NULL;
  876. }
  877. publicIdentityField = cache.findField(nodeStatusClass, "publicIdentity", "Ljava/lang/String;");
  878. if(publicIdentityField == NULL)
  879. {
  880. return NULL;
  881. }
  882. secretIdentityField = cache.findField(nodeStatusClass, "secretIdentity", "Ljava/lang/String;");
  883. if(secretIdentityField == NULL)
  884. {
  885. return NULL;
  886. }
  887. onlineField = cache.findField(nodeStatusClass, "online", "Z");
  888. if(onlineField == NULL)
  889. {
  890. return NULL;
  891. }
  892. env->SetLongField(nodeStatusObj, addressField, nodeStatus.address);
  893. jstring pubIdentStr = env->NewStringUTF(nodeStatus.publicIdentity);
  894. if(pubIdentStr == NULL)
  895. {
  896. return NULL; // out of memory
  897. }
  898. env->SetObjectField(nodeStatusObj, publicIdentityField, pubIdentStr);
  899. jstring secIdentStr = env->NewStringUTF(nodeStatus.secretIdentity);
  900. if(secIdentStr == NULL)
  901. {
  902. return NULL; // out of memory
  903. }
  904. env->SetObjectField(nodeStatusObj, secretIdentityField, secIdentStr);
  905. env->SetBooleanField(nodeStatusObj, onlineField, nodeStatus.online);
  906. return nodeStatusObj;
  907. }
  908. /*
  909. * Class: com_zerotier_sdk_Node
  910. * Method: networkConfig
  911. * Signature: (J)Lcom/zerotier/sdk/VirtualNetworkConfig;
  912. */
  913. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_networkConfig(
  914. JNIEnv *env, jobject obj, jlong id, jlong nwid)
  915. {
  916. uint64_t nodeId = (uint64_t) id;
  917. ZT1_Node *node = findNode(nodeId);
  918. if(node == NULL)
  919. {
  920. // cannot find valid node. We should never get here.
  921. return 0;
  922. }
  923. ZT1_VirtualNetworkConfig *vnetConfig = ZT1_Node_networkConfig(node, nwid);
  924. jobject vnetConfigObject = newNetworkConfig(env, *vnetConfig);
  925. ZT1_Node_freeQueryResult(node, vnetConfig);
  926. return vnetConfigObject;
  927. }
  928. /*
  929. * Class: com_zerotier_sdk_Node
  930. * Method: version
  931. * Signature: (J)Lcom/zerotier/sdk/Version;
  932. */
  933. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_version(
  934. JNIEnv *env, jobject obj)
  935. {
  936. int major = 0;
  937. int minor = 0;
  938. int revision = 0;
  939. unsigned long featureFlags = 0;
  940. ZT1_version(&major, &minor, &revision, &featureFlags);
  941. return newVersion(env, major, minor, revision, featureFlags);
  942. }
  943. /*
  944. * Class: com_zerotier_sdk_Node
  945. * Method: peers
  946. * Signature: (J)[Lcom/zerotier/sdk/Peer;
  947. */
  948. JNIEXPORT jobjectArray JNICALL Java_com_zerotier_sdk_Node_peers(
  949. JNIEnv *env, jobject obj, jlong id)
  950. {
  951. uint64_t nodeId = (uint64_t) id;
  952. ZT1_Node *node = findNode(nodeId);
  953. if(node == NULL)
  954. {
  955. // cannot find valid node. We should never get here.
  956. return 0;
  957. }
  958. ZT1_PeerList *peerList = ZT1_Node_peers(node);
  959. if(peerList == NULL)
  960. {
  961. LOGE("ZT1_Node_peers returned NULL");
  962. return NULL;
  963. }
  964. int peerCount = peerList->peerCount * 100;
  965. LOGV("Ensure Local Capacity: %d", peerCount);
  966. if(env->EnsureLocalCapacity(peerCount))
  967. {
  968. LOGE("EnsureLocalCapacity failed!!");
  969. ZT1_Node_freeQueryResult(node, peerList);
  970. return NULL;
  971. }
  972. jclass peerClass = cache.findClass("com/zerotier/sdk/Peer");
  973. if(env->ExceptionCheck() || peerClass == NULL)
  974. {
  975. LOGE("Error finding Peer class");
  976. ZT1_Node_freeQueryResult(node, peerList);
  977. return NULL;
  978. }
  979. jobjectArray peerArrayObj = env->NewObjectArray(
  980. peerList->peerCount, peerClass, NULL);
  981. if(env->ExceptionCheck() || peerArrayObj == NULL)
  982. {
  983. LOGE("Error creating Peer[] array");
  984. ZT1_Node_freeQueryResult(node, peerList);
  985. return NULL;
  986. }
  987. for(unsigned int i = 0; i < peerList->peerCount; ++i)
  988. {
  989. jobject peerObj = newPeer(env, peerList->peers[i]);
  990. env->SetObjectArrayElement(peerArrayObj, i, peerObj);
  991. if(env->ExceptionCheck())
  992. {
  993. LOGE("Error assigning Peer object to array");
  994. break;
  995. }
  996. }
  997. ZT1_Node_freeQueryResult(node, peerList);
  998. peerList = NULL;
  999. return peerArrayObj;
  1000. }
  1001. /*
  1002. * Class: com_zerotier_sdk_Node
  1003. * Method: networks
  1004. * Signature: (J)[Lcom/zerotier/sdk/VirtualNetworkConfig;
  1005. */
  1006. JNIEXPORT jobjectArray JNICALL Java_com_zerotier_sdk_Node_networks(
  1007. JNIEnv *env, jobject obj, jlong id)
  1008. {
  1009. uint64_t nodeId = (uint64_t) id;
  1010. ZT1_Node *node = findNode(nodeId);
  1011. if(node == NULL)
  1012. {
  1013. // cannot find valid node. We should never get here.
  1014. return 0;
  1015. }
  1016. ZT1_VirtualNetworkList *networkList = ZT1_Node_networks(node);
  1017. if(networkList == NULL)
  1018. {
  1019. return NULL;
  1020. }
  1021. jclass vnetConfigClass = cache.findClass("com/zerotier/sdk/VirtualNetworkConfig");
  1022. if(env->ExceptionCheck() || vnetConfigClass == NULL)
  1023. {
  1024. LOGE("Error finding VirtualNetworkConfig class");
  1025. ZT1_Node_freeQueryResult(node, networkList);
  1026. return NULL;
  1027. }
  1028. jobjectArray networkListObject = env->NewObjectArray(
  1029. networkList->networkCount, vnetConfigClass, NULL);
  1030. if(env->ExceptionCheck() || networkListObject == NULL)
  1031. {
  1032. LOGE("Error creating VirtualNetworkConfig[] array");
  1033. ZT1_Node_freeQueryResult(node, networkList);
  1034. return NULL;
  1035. }
  1036. for(unsigned int i = 0; i < networkList->networkCount; ++i)
  1037. {
  1038. jobject networkObject = newNetworkConfig(env, networkList->networks[i]);
  1039. env->SetObjectArrayElement(networkListObject, i, networkObject);
  1040. if(env->ExceptionCheck())
  1041. {
  1042. LOGE("Error assigning VirtualNetworkConfig object to array");
  1043. break;
  1044. }
  1045. }
  1046. ZT1_Node_freeQueryResult(node, networkList);
  1047. return networkListObject;
  1048. }
  1049. #ifdef __cplusplus
  1050. } // extern "C"
  1051. #endif