com_zerotierone_sdk_Node.cpp 29 KB

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