com_zerotierone_sdk_Node.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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 <ZeroTierOne.h>
  30. #include <map>
  31. #include <string>
  32. #include <assert.h>
  33. #include <string.h>
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. namespace {
  38. struct JniRef
  39. {
  40. JniRef()
  41. : env(NULL)
  42. , node(NULL)
  43. , dataStoreGetListener(NULL)
  44. , dataStorePutListener(NULL)
  45. , packetSender(NULL)
  46. , frameListener(NULL)
  47. , configListener(NULL)
  48. {}
  49. uint64_t id;
  50. JNIEnv *env;
  51. ZT1_Node *node;
  52. jobject dataStoreGetListener;
  53. jobject dataStorePutListener;
  54. jobject packetSender;
  55. jobject frameListener;
  56. jobject configListener;
  57. };
  58. int VirtualNetworkConfigFunctionCallback(
  59. ZT1_Node *node,
  60. void *userData,
  61. uint64_t nwid,
  62. enum ZT1_VirtualNetworkConfigOperation operation,
  63. const ZT1_VirtualNetworkConfig *config)
  64. {
  65. JniRef *ref = (JniRef*)userData;
  66. assert(ref->node == node);
  67. JNIEnv *env = ref->env;
  68. static jclass configListenerClass = NULL;
  69. static jmethodID callbackMethod = NULL;
  70. if(configListenerClass == NULL)
  71. {
  72. configListenerClass = env->GetObjectClass(ref->configListener);
  73. if(configListenerClass == NULL)
  74. {
  75. return -1;
  76. }
  77. }
  78. if(callbackMethod == NULL)
  79. {
  80. callbackMethod = env->GetMethodID(configListenerClass,
  81. "onNetworkConfigurationUpdated",
  82. "(JLcom/zerotierone/sdk/VirtualNetworkConfigOperation;Lcom/zerotierone/sdk/VirtualNetworkConfig;)I");
  83. if(callbackMethod == NULL)
  84. {
  85. return -2;
  86. }
  87. }
  88. jobject operationObject = createVirtualNetworkConfigOperation(env, operation);
  89. if(operationObject == NULL)
  90. {
  91. return -3;
  92. }
  93. jobject networkConfigObject = newNetworkConfig(env, *config);
  94. if(networkConfigObject == NULL)
  95. {
  96. return -4;
  97. }
  98. return env->CallIntMethod(ref->configListener, callbackMethod, (jlong)nwid, operationObject, networkConfigObject);
  99. }
  100. void VirtualNetworkFrameFunctionCallback(ZT1_Node *node,void *userData,
  101. uint64_t nwid,
  102. uint64_t sourceMac,
  103. uint64_t destMac,
  104. unsigned int etherType,
  105. unsigned int vlanid,
  106. const void *frameData,
  107. unsigned int frameLength)
  108. {
  109. JniRef *ref = (JniRef*)userData;
  110. assert(ref->node == node);
  111. JNIEnv *env = ref->env;
  112. static jclass frameListenerClass = NULL;
  113. static jmethodID callbackMethod = NULL;
  114. if(frameListenerClass == NULL)
  115. {
  116. frameListenerClass = env->GetObjectClass(ref->frameListener);
  117. if(frameListenerClass == NULL)
  118. {
  119. return;
  120. }
  121. }
  122. if(callbackMethod == NULL)
  123. {
  124. callbackMethod = env->GetMethodID(frameListenerClass,
  125. "onVirtualNetworkFrame", "(JJJJJ[B)V");
  126. if(callbackMethod == NULL)
  127. {
  128. return;
  129. }
  130. }
  131. jbyteArray dataArray = env->NewByteArray(frameLength);
  132. env->SetByteArrayRegion(dataArray, 0, frameLength, (jbyte*)frameData);
  133. env->CallVoidMethod(ref->frameListener, callbackMethod, nwid, sourceMac, destMac, etherType, vlanid, dataArray);
  134. }
  135. void EventCallback(ZT1_Node *node,void *userData,enum ZT1_Event event, const void *data)
  136. {
  137. JniRef *ref = (JniRef*)userData;
  138. assert(ref->node == node);
  139. JNIEnv *env = ref->env;
  140. }
  141. long DataStoreGetFunction(ZT1_Node *node,void *userData,
  142. const char *objectName,
  143. void *buffer,
  144. unsigned long bufferSize,
  145. unsigned long bufferIndex,
  146. unsigned long *out_objectSize)
  147. {
  148. JniRef *ref = (JniRef*)userData;
  149. assert(ref->node == node);
  150. JNIEnv *env = ref->env;
  151. static jclass dataStoreGetClass = NULL;
  152. static jmethodID callbackMethod = NULL;
  153. if(dataStoreGetClass == NULL)
  154. {
  155. dataStoreGetClass = env->GetObjectClass(ref->dataStoreGetListener);
  156. if(dataStoreGetClass == NULL)
  157. {
  158. return -2;
  159. }
  160. }
  161. if(callbackMethod == NULL)
  162. {
  163. callbackMethod = env->GetMethodID(dataStoreGetClass,
  164. "onDataStoreGet",
  165. "(Ljava/lang/String;[BJ[J)J");
  166. if(callbackMethod == NULL)
  167. {
  168. return -2;
  169. }
  170. }
  171. jstring nameStr = env->NewStringUTF(objectName);
  172. jbyteArray bufferObj = env->NewByteArray(bufferSize);
  173. jlongArray objectSizeObj = env->NewLongArray(1);
  174. long retval = env->CallLongMethod(
  175. dataStoreGetClass, callbackMethod, nameStr, bufferObj, bufferIndex, objectSizeObj);
  176. env->GetByteArrayRegion(bufferObj, 0, bufferSize, (jbyte*)buffer);
  177. env->GetLongArrayRegion(objectSizeObj, 0, 1, (jlong*)&out_objectSize);
  178. env->ReleaseByteArrayElements(bufferObj, (jbyte*)buffer, 0);
  179. env->ReleaseLongArrayElements(objectSizeObj, (jlong*)&out_objectSize, 0);
  180. return retval;
  181. }
  182. int DataStorePutFunction(ZT1_Node *node,void *userData,
  183. const char *objectName,
  184. const void *buffer,
  185. unsigned long bufferSize,
  186. int secure)
  187. {
  188. JniRef *ref = (JniRef*)userData;
  189. assert(ref->node == node);
  190. JNIEnv *env = ref->env;
  191. static jclass dataStorePutClass = NULL;
  192. static jmethodID callbackMethod = NULL;
  193. static jmethodID deleteMethod = NULL;
  194. if(dataStorePutClass == NULL)
  195. {
  196. dataStorePutClass = env->GetObjectClass(ref->dataStorePutListener);
  197. if(dataStorePutClass == NULL)
  198. {
  199. return -1;
  200. }
  201. }
  202. if(callbackMethod == NULL)
  203. {
  204. callbackMethod = env->GetMethodID(dataStorePutClass,
  205. "onDataStorePut",
  206. "(Ljava/lang/String;[BZ)I");
  207. if(callbackMethod == NULL)
  208. {
  209. return -2;
  210. }
  211. }
  212. if(deleteMethod == NULL)
  213. {
  214. deleteMethod = env->GetMethodID(dataStorePutClass,
  215. "onDelete", "(Ljava/lang/String;)I");
  216. if(deleteMethod == NULL)
  217. {
  218. return -3;
  219. }
  220. }
  221. jstring nameStr = env->NewStringUTF(objectName);
  222. if(buffer == NULL)
  223. {
  224. // delete operation
  225. return env->CallIntMethod(dataStorePutClass, deleteMethod, nameStr);
  226. }
  227. else
  228. {
  229. // set operation
  230. jbyteArray bufferObj = env->NewByteArray(bufferSize);
  231. env->SetByteArrayRegion(bufferObj, 0, bufferSize, (jbyte*)buffer);
  232. bool secure = secure != 0;
  233. return env->CallIntMethod(dataStorePutClass, callbackMethod,
  234. nameStr, bufferObj, secure);
  235. }
  236. }
  237. int WirePacketSendFunction(ZT1_Node *node,void *userData,const struct sockaddr_storage *,unsigned int,const void *,unsigned int)
  238. {
  239. JniRef *ref = (JniRef*)userData;
  240. assert(ref->node == node);
  241. JNIEnv *env = ref->env;
  242. return 0;
  243. }
  244. typedef std::map<uint64_t, JniRef*> NodeMap;
  245. static NodeMap nodeMap;
  246. ZT1_Node* findNode(uint64_t nodeId)
  247. {
  248. NodeMap::iterator found = nodeMap.find(nodeId);
  249. if(found != nodeMap.end())
  250. {
  251. JniRef *ref = found->second;
  252. return ref->node;
  253. }
  254. return NULL;
  255. }
  256. }
  257. /*
  258. * Class: com_zerotierone_sdk_Node
  259. * Method: node_init
  260. * Signature: (J)Lcom/zerotierone/sdk/ResultCode;
  261. */
  262. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_node_1init(
  263. JNIEnv *env, jobject obj, jlong now)
  264. {
  265. jobject resultObject = createResultObject(env, ZT1_RESULT_OK);
  266. ZT1_Node *node;
  267. JniRef *ref = new JniRef;
  268. ZT1_ResultCode rc = ZT1_Node_new(
  269. &node,
  270. ref,
  271. (uint64_t)now,
  272. &DataStoreGetFunction,
  273. &DataStorePutFunction,
  274. &WirePacketSendFunction,
  275. &VirtualNetworkFrameFunctionCallback,
  276. &VirtualNetworkConfigFunctionCallback,
  277. &EventCallback);
  278. if(rc != ZT1_RESULT_OK)
  279. {
  280. resultObject = createResultObject(env, rc);
  281. if(node)
  282. {
  283. ZT1_Node_delete(node);
  284. node = NULL;
  285. }
  286. delete ref;
  287. ref = NULL;
  288. return resultObject;
  289. }
  290. ref->id = (uint64_t)now;
  291. ref->env = env;
  292. ref->node = node;
  293. jclass cls = env->GetObjectClass(obj);
  294. jfieldID fid = env->GetFieldID(
  295. cls, "getListener", "Lcom/zerotierone/sdk/DataStoreGetListener;");
  296. if(fid == NULL)
  297. {
  298. return NULL; // exception already thrown
  299. }
  300. ref->dataStoreGetListener = env->GetObjectField(obj, fid);
  301. if(ref->dataStoreGetListener == NULL)
  302. {
  303. return NULL;
  304. }
  305. fid = env->GetFieldID(
  306. cls, "putListener", "Lcom/zerotierone/sdk/DataStorePutLisetner;");
  307. if(fid == NULL)
  308. {
  309. return NULL; // exception already thrown
  310. }
  311. ref->dataStorePutListener = env->GetObjectField(obj, fid);
  312. if(ref->dataStorePutListener == NULL)
  313. {
  314. return NULL;
  315. }
  316. fid = env->GetFieldID(
  317. cls, "sender", "Lcom/zerotierone/sdk/PacketSender;");
  318. if(fid == NULL)
  319. {
  320. return NULL; // exception already thrown
  321. }
  322. ref->packetSender = env->GetObjectField(obj, fid);
  323. if(ref->packetSender == NULL)
  324. {
  325. return NULL;
  326. }
  327. fid = env->GetFieldID(
  328. cls, "frameListener", "Lcom/zerotierone/sdk/VirtualNetworkFrameListener;");
  329. if(fid == NULL)
  330. {
  331. return NULL; // exception already thrown
  332. }
  333. ref->frameListener = env->GetObjectField(obj, fid);
  334. if(ref->frameListener = NULL)
  335. {
  336. return NULL;
  337. }
  338. fid = env->GetFieldID(
  339. cls, "configListener", "Lcom/zerotierone/sdk/VirtualNetworkConfigListener;");
  340. if(fid == NULL)
  341. {
  342. return NULL; // exception already thrown
  343. }
  344. ref->configListener = env->GetObjectField(obj, fid);
  345. if(ref->configListener == NULL)
  346. {
  347. return NULL;
  348. }
  349. nodeMap.insert(std::make_pair(ref->id, ref));
  350. return resultObject;
  351. }
  352. /*
  353. * Class: com_zerotierone_sdk_Node
  354. * Method: node_delete
  355. * Signature: (J)V
  356. */
  357. JNIEXPORT void JNICALL Java_com_zerotierone_sdk_Node_node_1delete(
  358. JNIEnv *env, jobject obj, jlong id)
  359. {
  360. uint64_t nodeId = (uint64_t)id;
  361. NodeMap::iterator found = nodeMap.find(nodeId);
  362. if(found != nodeMap.end())
  363. {
  364. JniRef *ref = found->second;
  365. nodeMap.erase(found);
  366. ZT1_Node_delete(ref->node);
  367. delete ref;
  368. ref = NULL;
  369. }
  370. }
  371. /*
  372. * Class: com_zerotierone_sdk_Node
  373. * Method: processVirtualNetworkFrame
  374. * Signature: (JJJJJII[B[J)Lcom/zerotierone/sdk/ResultCode;
  375. */
  376. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_processVirtualNetworkFrame(
  377. JNIEnv *env, jobject obj,
  378. jlong id,
  379. jlong in_now,
  380. jlong in_nwid,
  381. jlong in_sourceMac,
  382. jlong in_destMac,
  383. jint in_etherType,
  384. jint in_vlanId,
  385. jbyteArray in_frameData,
  386. jlongArray out_nextBackgroundTaskDeadline)
  387. {
  388. uint64_t nodeId = (uint64_t) id;
  389. ZT1_Node *node = findNode(nodeId);
  390. if(node == NULL)
  391. {
  392. // cannot find valid node. We should never get here.
  393. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  394. }
  395. unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline);
  396. if(nbtd_len < 1)
  397. {
  398. // array for next background task length has 0 elements!
  399. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  400. }
  401. uint64_t now = (uint64_t)in_now;
  402. uint64_t nwid = (uint64_t)in_nwid;
  403. uint64_t sourceMac = (uint64_t)in_sourceMac;
  404. uint64_t destMac = (uint64_t)in_destMac;
  405. unsigned int etherType = (unsigned int)in_etherType;
  406. unsigned int vlanId = (unsigned int)in_vlanId;
  407. unsigned int frameLength = env->GetArrayLength(in_frameData);
  408. jbyte *frameData =env->GetByteArrayElements(in_frameData, NULL);
  409. uint64_t nextBackgroundTaskDeadline = 0;
  410. ZT1_ResultCode rc = ZT1_Node_processVirtualNetworkFrame(
  411. node,
  412. now,
  413. nwid,
  414. sourceMac,
  415. destMac,
  416. etherType,
  417. vlanId,
  418. (const void*)frameData,
  419. frameLength,
  420. &nextBackgroundTaskDeadline);
  421. jlong *outDeadline = env->GetLongArrayElements(out_nextBackgroundTaskDeadline, NULL);
  422. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  423. env->ReleaseLongArrayElements(out_nextBackgroundTaskDeadline, outDeadline, 0);
  424. env->ReleaseByteArrayElements(in_frameData, frameData, 0);
  425. return createResultObject(env, rc);
  426. }
  427. /*
  428. * Class: com_zerotierone_sdk_Node
  429. * Method: processWirePacket
  430. * Signature: (JJLjava/net/InetAddress;I[B[J)Lcom/zerotierone/sdk/ResultCode;
  431. */
  432. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_processWirePacket(
  433. JNIEnv *env, jobject obj,
  434. jlong id,
  435. jlong in_now,
  436. jobject in_remoteAddress,
  437. jint in_linkDesparation,
  438. jbyteArray in_packetData,
  439. jlongArray out_nextBackgroundTaskDeadline)
  440. {
  441. uint64_t nodeId = (uint64_t) id;
  442. ZT1_Node *node = findNode(nodeId);
  443. if(node == NULL)
  444. {
  445. // cannot find valid node. We should never get here.
  446. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  447. }
  448. unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline);
  449. if(nbtd_len < 1)
  450. {
  451. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  452. }
  453. uint64_t now = (uint64_t)in_now;
  454. unsigned int linkDesparation = (unsigned int)in_linkDesparation;
  455. // get the java.net.InetAddress class and getAddress() method
  456. jclass inetAddressClass = env->FindClass("java/net/InetAddress");
  457. if(inetAddressClass == NULL)
  458. {
  459. // can't find java.net.InetAddress
  460. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  461. }
  462. jmethodID getAddressMethod = env->GetMethodID(
  463. inetAddressClass, "getAddress", "()[B");
  464. if(getAddressMethod == NULL)
  465. {
  466. // cant find InetAddress.getAddres()
  467. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  468. }
  469. // Call InetAddress.getAddress()
  470. jbyteArray addressArray = (jbyteArray)env->CallObjectMethod(in_remoteAddress, getAddressMethod);
  471. if(addressArray == NULL)
  472. {
  473. // unable to call getAddress()
  474. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  475. }
  476. unsigned int addrSize = env->GetArrayLength(addressArray);
  477. // get the address bytes
  478. jbyte *addr = env->GetByteArrayElements(addressArray, NULL);
  479. sockaddr_storage remoteAddress = {};
  480. if(addrSize == 16)
  481. {
  482. // IPV6 address
  483. sockaddr_in6 ipv6 = {};
  484. ipv6.sin6_family = AF_INET6;
  485. memcpy(ipv6.sin6_addr.s6_addr, addr, 16);
  486. memcpy(&remoteAddress, &ipv6, sizeof(sockaddr_in6));
  487. }
  488. else if(addrSize = 4)
  489. {
  490. // IPV4 address
  491. sockaddr_in ipv4 = {};
  492. ipv4.sin_family = AF_INET;
  493. memcpy(&ipv4.sin_addr, addr, 4);
  494. memcpy(&remoteAddress, &ipv4, sizeof(sockaddr_in));
  495. }
  496. else
  497. {
  498. // unknown address type
  499. env->ReleaseByteArrayElements(addressArray, addr, 0);
  500. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  501. }
  502. unsigned int packetLength = env->GetArrayLength(in_packetData);
  503. jbyte *packetData = env->GetByteArrayElements(in_packetData, NULL);
  504. uint64_t nextBackgroundTaskDeadline = 0;
  505. ZT1_ResultCode rc = ZT1_Node_processWirePacket(
  506. node,
  507. now,
  508. &remoteAddress,
  509. linkDesparation,
  510. packetData,
  511. packetLength,
  512. &nextBackgroundTaskDeadline);
  513. jlong *outDeadline = env->GetLongArrayElements(out_nextBackgroundTaskDeadline, NULL);
  514. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  515. env->ReleaseLongArrayElements(out_nextBackgroundTaskDeadline, outDeadline, 0);
  516. env->ReleaseByteArrayElements(addressArray, addr, 0);
  517. env->ReleaseByteArrayElements(in_packetData, packetData, 0);
  518. return createResultObject(env, ZT1_RESULT_OK);
  519. }
  520. /*
  521. * Class: com_zerotierone_sdk_Node
  522. * Method: processBackgroundTasks
  523. * Signature: (JJ[J)Lcom/zerotierone/sdk/ResultCode;
  524. */
  525. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_processBackgroundTasks(
  526. JNIEnv *env, jobject obj,
  527. jlong id,
  528. jlong in_now,
  529. jlongArray out_nextBackgroundTaskDeadline)
  530. {
  531. uint64_t nodeId = (uint64_t) id;
  532. ZT1_Node *node = findNode(nodeId);
  533. if(node == NULL)
  534. {
  535. // cannot find valid node. We should never get here.
  536. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  537. }
  538. unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline);
  539. if(nbtd_len < 1)
  540. {
  541. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  542. }
  543. uint64_t now = (uint64_t)in_now;
  544. uint64_t nextBackgroundTaskDeadline = 0;
  545. ZT1_ResultCode rc = ZT1_Node_processBackgroundTasks(node, now, &nextBackgroundTaskDeadline);
  546. jlong *outDeadline = env->GetLongArrayElements(out_nextBackgroundTaskDeadline, NULL);
  547. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  548. env->ReleaseLongArrayElements(out_nextBackgroundTaskDeadline, outDeadline, 0);
  549. return createResultObject(env, rc);
  550. }
  551. /*
  552. * Class: com_zerotierone_sdk_Node
  553. * Method: join
  554. * Signature: (JJ)Lcom/zerotierone/sdk/ResultCode;
  555. */
  556. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_join(
  557. JNIEnv *env, jobject obj, jlong id, jlong in_nwid)
  558. {
  559. uint64_t nodeId = (uint64_t) id;
  560. ZT1_Node *node = findNode(nodeId);
  561. if(node == NULL)
  562. {
  563. // cannot find valid node. We should never get here.
  564. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  565. }
  566. uint64_t nwid = (uint64_t)in_nwid;
  567. ZT1_ResultCode rc = ZT1_Node_join(node, nwid);
  568. return createResultObject(env, rc);
  569. }
  570. /*
  571. * Class: com_zerotierone_sdk_Node
  572. * Method: leave
  573. * Signature: (JJ)Lcom/zerotierone/sdk/ResultCode;
  574. */
  575. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_leave(
  576. JNIEnv *env, jobject obj, jlong id, jlong in_nwid)
  577. {
  578. uint64_t nodeId = (uint64_t) id;
  579. ZT1_Node *node = findNode(nodeId);
  580. if(node == NULL)
  581. {
  582. // cannot find valid node. We should never get here.
  583. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  584. }
  585. uint64_t nwid = (uint64_t)in_nwid;
  586. ZT1_ResultCode rc = ZT1_Node_leave(node, nwid);
  587. return createResultObject(env, rc);
  588. }
  589. /*
  590. * Class: com_zerotierone_sdk_Node
  591. * Method: multicastSubscribe
  592. * Signature: (JJJJ)Lcom/zerotierone/sdk/ResultCode;
  593. */
  594. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_multicastSubscribe(
  595. JNIEnv *env, jobject obj,
  596. jlong id,
  597. jlong in_nwid,
  598. jlong in_multicastGroup,
  599. jlong in_multicastAdi)
  600. {
  601. uint64_t nodeId = (uint64_t) id;
  602. ZT1_Node *node = findNode(nodeId);
  603. if(node == NULL)
  604. {
  605. // cannot find valid node. We should never get here.
  606. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  607. }
  608. uint64_t nwid = (uint64_t)in_nwid;
  609. uint64_t multicastGroup = (uint64_t)in_multicastGroup;
  610. uint64_t multicastAdi = (uint64_t)in_multicastAdi;
  611. ZT1_ResultCode rc = ZT1_Node_multicastSubscribe(
  612. node, nwid, multicastGroup, multicastAdi);
  613. return createResultObject(env, rc);
  614. }
  615. /*
  616. * Class: com_zerotierone_sdk_Node
  617. * Method: multicastUnsubscribe
  618. * Signature: (JJJJ)Lcom/zerotierone/sdk/ResultCode;
  619. */
  620. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_multicastUnsubscribe(
  621. JNIEnv *env, jobject obj,
  622. jlong id,
  623. jlong in_nwid,
  624. jlong in_multicastGroup,
  625. jlong in_multicastAdi)
  626. {
  627. uint64_t nodeId = (uint64_t) id;
  628. ZT1_Node *node = findNode(nodeId);
  629. if(node == NULL)
  630. {
  631. // cannot find valid node. We should never get here.
  632. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  633. }
  634. uint64_t nwid = (uint64_t)in_nwid;
  635. uint64_t multicastGroup = (uint64_t)in_multicastGroup;
  636. uint64_t multicastAdi = (uint64_t)in_multicastAdi;
  637. ZT1_ResultCode rc = ZT1_Node_multicastUnsubscribe(
  638. node, nwid, multicastGroup, multicastAdi);
  639. return createResultObject(env, rc);
  640. }
  641. /*
  642. * Class: com_zerotierone_sdk_Node
  643. * Method: address
  644. * Signature: (J)J
  645. */
  646. JNIEXPORT jlong JNICALL Java_com_zerotierone_sdk_Node_address(
  647. JNIEnv *env , jobject obj, jlong id)
  648. {
  649. uint64_t nodeId = (uint64_t) id;
  650. ZT1_Node *node = findNode(nodeId);
  651. if(node == NULL)
  652. {
  653. // cannot find valid node. We should never get here.
  654. return 0;
  655. }
  656. uint64_t address = ZT1_Node_address(node);
  657. return (jlong)address;
  658. }
  659. /*
  660. * Class: com_zerotierone_sdk_Node
  661. * Method: status
  662. * Signature: (J)Lcom/zerotierone/sdk/NodeStatus;
  663. */
  664. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_status
  665. (JNIEnv *env, jobject obj, jlong id)
  666. {
  667. uint64_t nodeId = (uint64_t) id;
  668. ZT1_Node *node = findNode(nodeId);
  669. if(node == NULL)
  670. {
  671. // cannot find valid node. We should never get here.
  672. return 0;
  673. }
  674. // static so we only have to look these up once
  675. static jclass nodeStatusClass = NULL;
  676. static jmethodID nodeStatusConstructor = NULL;
  677. // create a com.zerotierone.sdk.NodeStatus object
  678. if(nodeStatusClass == NULL)
  679. {
  680. nodeStatusClass = env->FindClass("com/zerotierone/sdk/NodeStatus");
  681. if(nodeStatusClass == NULL)
  682. {
  683. return NULL;
  684. }
  685. }
  686. if(nodeStatusConstructor == NULL)
  687. {
  688. nodeStatusConstructor = env->GetMethodID(
  689. nodeStatusClass, "<init>", "()V");
  690. if(nodeStatusConstructor == NULL)
  691. {
  692. return NULL;
  693. }
  694. }
  695. jobject nodeStatusObj = env->NewObject(nodeStatusClass, nodeStatusConstructor);
  696. if(nodeStatusObj == NULL)
  697. {
  698. return NULL;
  699. }
  700. ZT1_NodeStatus nodeStatus;
  701. ZT1_Node_status(node, &nodeStatus);
  702. static jfieldID addressField = NULL;
  703. static jfieldID publicIdentityField = NULL;
  704. static jfieldID secretIdentityField = NULL;
  705. static jfieldID onlineField = NULL;
  706. if(addressField == NULL)
  707. {
  708. addressField = env->GetFieldID(nodeStatusClass, "address", "J");
  709. if(addressField == NULL)
  710. {
  711. return NULL;
  712. }
  713. }
  714. if(publicIdentityField == NULL)
  715. {
  716. publicIdentityField = env->GetFieldID(nodeStatusClass, "publicIdentity", "Ljava/lang/String;");
  717. if(publicIdentityField == NULL)
  718. {
  719. return NULL;
  720. }
  721. }
  722. if(secretIdentityField == NULL)
  723. {
  724. secretIdentityField = env->GetFieldID(nodeStatusClass, "secretIdentity", "Ljava/lang/String;");
  725. if(secretIdentityField == NULL)
  726. {
  727. return NULL;
  728. }
  729. }
  730. if(onlineField == NULL)
  731. {
  732. onlineField = env->GetFieldID(nodeStatusClass, "online", "Z");
  733. if(onlineField == NULL)
  734. {
  735. return NULL;
  736. }
  737. }
  738. env->SetLongField(nodeStatusObj, addressField, nodeStatus.address);
  739. jstring pubIdentStr = env->NewStringUTF(nodeStatus.publicIdentity);
  740. if(pubIdentStr == NULL)
  741. {
  742. return NULL; // out of memory
  743. }
  744. env->SetObjectField(nodeStatusObj, publicIdentityField, pubIdentStr);
  745. jstring secIdentStr = env->NewStringUTF(nodeStatus.secretIdentity);
  746. if(secIdentStr == NULL)
  747. {
  748. return NULL; // out of memory
  749. }
  750. env->SetObjectField(nodeStatusObj, secretIdentityField, secIdentStr);
  751. env->SetBooleanField(nodeStatusObj, onlineField, nodeStatus.online);
  752. return nodeStatusObj;
  753. }
  754. /*
  755. * Class: com_zerotierone_sdk_Node
  756. * Method: networkConfig
  757. * Signature: (J)Lcom/zerotierone/sdk/VirtualNetworkConfig;
  758. */
  759. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_networkConfig(
  760. JNIEnv *env, jobject obj, jlong id, jlong nwid)
  761. {
  762. uint64_t nodeId = (uint64_t) id;
  763. ZT1_Node *node = findNode(nodeId);
  764. if(node == NULL)
  765. {
  766. // cannot find valid node. We should never get here.
  767. return 0;
  768. }
  769. ZT1_VirtualNetworkConfig *vnetConfig = ZT1_Node_networkConfig(node, nwid);
  770. jobject vnetConfigObject = newNetworkConfig(env, *vnetConfig);
  771. ZT1_Node_freeQueryResult(node, vnetConfig);
  772. return vnetConfigObject;
  773. }
  774. /*
  775. * Class: com_zerotierone_sdk_Node
  776. * Method: version
  777. * Signature: (J)Lcom/zerotierone/sdk/Version;
  778. */
  779. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_version(
  780. JNIEnv *env, jobject obj)
  781. {
  782. // create a com.zerotierone.sdk.Version object
  783. jclass versionClass = env->FindClass("com/zerotierone/sdk/Version");
  784. if(versionClass == NULL)
  785. {
  786. return NULL;
  787. }
  788. jmethodID versionConstructor = env->GetMethodID(
  789. versionClass, "<init>", "()V");
  790. if(versionConstructor == NULL)
  791. {
  792. return NULL;
  793. }
  794. jobject versionObj = env->NewObject(versionClass, versionConstructor);
  795. if(versionObj == NULL)
  796. {
  797. return NULL;
  798. }
  799. int major = 0;
  800. int minor = 0;
  801. int revision = 0;
  802. unsigned long featureFlags = 0;
  803. ZT1_version(&major, &minor, &revision, &featureFlags);
  804. // copy data to Version object
  805. static jfieldID majorField = NULL;
  806. static jfieldID minorField = NULL;
  807. static jfieldID revisionField = NULL;
  808. static jfieldID featureFlagsField = NULL;
  809. if(majorField == NULL)
  810. {
  811. majorField = env->GetFieldID(versionClass, "major", "I");
  812. if(majorField = NULL)
  813. {
  814. return NULL;
  815. }
  816. }
  817. if(minorField == NULL)
  818. {
  819. minorField = env->GetFieldID(versionClass, "minor", "I");
  820. if(minorField == NULL)
  821. {
  822. return NULL;
  823. }
  824. }
  825. if(revisionField == NULL)
  826. {
  827. revisionField = env->GetFieldID(versionClass, "revision", "I");
  828. if(revisionField == NULL)
  829. {
  830. return NULL;
  831. }
  832. }
  833. if(featureFlagsField == NULL)
  834. {
  835. featureFlagsField = env->GetFieldID(versionClass, "featureFlags", "J");
  836. if(featureFlagsField == NULL)
  837. {
  838. return NULL;
  839. }
  840. }
  841. env->SetIntField(versionObj, majorField, (jint)major);
  842. env->SetIntField(versionObj, minorField, (jint)minor);
  843. env->SetIntField(versionObj, revisionField, (jint)revision);
  844. env->SetLongField(versionObj, featureFlagsField, (jlong)featureFlags);
  845. return versionObj;
  846. }
  847. /*
  848. * Class: com_zerotierone_sdk_Node
  849. * Method: peers
  850. * Signature: (J)Ljava/util/ArrayList;
  851. */
  852. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_peers(
  853. JNIEnv *env, jobject obj, jlong id)
  854. {
  855. uint64_t nodeId = (uint64_t) id;
  856. ZT1_Node *node = findNode(nodeId);
  857. if(node == NULL)
  858. {
  859. // cannot find valid node. We should never get here.
  860. return 0;
  861. }
  862. ZT1_PeerList *peerList = ZT1_Node_peers(node);
  863. if(peerList == NULL)
  864. {
  865. return NULL;
  866. }
  867. jobject peerListObject = newArrayList(env);
  868. if(peerListObject == NULL)
  869. {
  870. ZT1_Node_freeQueryResult(node, peerList);
  871. return NULL;
  872. }
  873. for(unsigned int i = 0; i < peerList->peerCount; ++i)
  874. {
  875. jobject peerObj = newPeer(env, peerList->peers[i]);
  876. appendItemToArrayList(env, peerListObject, peerObj);
  877. }
  878. ZT1_Node_freeQueryResult(node, peerList);
  879. peerList = NULL;
  880. return peerListObject;
  881. }
  882. /*
  883. * Class: com_zerotierone_sdk_Node
  884. * Method: networks
  885. * Signature: (J)Ljava/util/ArrayList;
  886. */
  887. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_networks(
  888. JNIEnv *env, jobject obj, jlong id)
  889. {
  890. uint64_t nodeId = (uint64_t) id;
  891. ZT1_Node *node = findNode(nodeId);
  892. if(node == NULL)
  893. {
  894. // cannot find valid node. We should never get here.
  895. return 0;
  896. }
  897. ZT1_VirtualNetworkList *networkList = ZT1_Node_networks(node);
  898. if(networkList == NULL)
  899. {
  900. return NULL;
  901. }
  902. jobject networkListObject = newArrayList(env);
  903. if(networkListObject == NULL)
  904. {
  905. ZT1_Node_freeQueryResult(node, networkList);
  906. return NULL;
  907. }
  908. for(unsigned int i = 0; i < networkList->networkCount; ++i)
  909. {
  910. jobject networkObject = newNetworkConfig(env, networkList->networks[i]);
  911. appendItemToArrayList(env, networkListObject, networkObject);
  912. }
  913. ZT1_Node_freeQueryResult(node, networkList);
  914. return networkListObject;
  915. }
  916. #ifdef __cplusplus
  917. } // extern "C"
  918. #endif