com_zerotierone_sdk_Node.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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 <ZeroTierOne.h>
  29. #include <map>
  30. #include <string>
  31. #include <assert.h>
  32. #include <string.h>
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. namespace {
  37. jobject createResultObject(JNIEnv *env, ZT1_ResultCode code);
  38. jobject createVirtualNetworkStatus(JNIEnv *env, ZT1_VirtualNetworkStatus status);
  39. jobject createEvent(JNIEnv *env, ZT1_Event event);
  40. struct JniRef
  41. {
  42. JniRef()
  43. : env(NULL)
  44. , node(NULL)
  45. , dataStoreGetListener(NULL)
  46. , dataStorePutListener(NULL)
  47. , packetSender(NULL)
  48. , frameListener(NULL)
  49. , configListener(NULL)
  50. {}
  51. uint64_t id;
  52. JNIEnv *env;
  53. ZT1_Node *node;
  54. jobject dataStoreGetListener;
  55. jobject dataStorePutListener;
  56. jobject packetSender;
  57. jobject frameListener;
  58. jobject configListener;
  59. };
  60. int VirtualNetworkConfigFunctionCallback(ZT1_Node *node,void *userData,uint64_t,enum ZT1_VirtualNetworkConfigOperation,const ZT1_VirtualNetworkConfig *)
  61. {
  62. JniRef *ref = (JniRef*)userData;
  63. assert(ref->node == node);
  64. JNIEnv *env = ref->env;
  65. return 0;
  66. }
  67. void VirtualNetworkFrameFunctionCallback(ZT1_Node *node,void *userData,uint64_t,uint64_t,uint64_t,unsigned int,unsigned int,const void *,unsigned int)
  68. {
  69. JniRef *ref = (JniRef*)userData;
  70. assert(ref->node == node);
  71. JNIEnv *env = ref->env;
  72. }
  73. void EventCallback(ZT1_Node *node,void *userData,enum ZT1_Event,const void *)
  74. {
  75. JniRef *ref = (JniRef*)userData;
  76. assert(ref->node == node);
  77. JNIEnv *env = ref->env;
  78. }
  79. long DataStoreGetFunction(ZT1_Node *node,void *userData,const char *,void *,unsigned long,unsigned long,unsigned long *)
  80. {
  81. JniRef *ref = (JniRef*)userData;
  82. assert(ref->node == node);
  83. JNIEnv *env = ref->env;
  84. return 0;
  85. }
  86. int DataStorePutFunction(ZT1_Node *node,void *userData,const char *,const void *,unsigned long,int)
  87. {
  88. JniRef *ref = (JniRef*)userData;
  89. assert(ref->node == node);
  90. JNIEnv *env = ref->env;
  91. return 0;
  92. }
  93. int WirePacketSendFunction(ZT1_Node *node,void *userData,const struct sockaddr_storage *,unsigned int,const void *,unsigned int)
  94. {
  95. JniRef *ref = (JniRef*)userData;
  96. assert(ref->node == node);
  97. JNIEnv *env = ref->env;
  98. return 0;
  99. }
  100. typedef std::map<uint64_t, JniRef*> NodeMap;
  101. static NodeMap nodeMap;
  102. jobject createResultObject(JNIEnv *env, ZT1_ResultCode code)
  103. {
  104. // cache the class so we don't have to
  105. // look it up every time we need to create a java
  106. // ResultCode object
  107. static jclass resultClass = NULL;
  108. jobject resultObject = NULL;
  109. if(resultClass == NULL)
  110. {
  111. resultClass = env->FindClass("com/zerotierone/sdk/ResultCode");
  112. if(resultClass == NULL)
  113. {
  114. return NULL; // exception thrown
  115. }
  116. }
  117. std::string fieldName;
  118. switch(code)
  119. {
  120. case ZT1_RESULT_OK:
  121. fieldName = "ZT1_RESULT_OK";
  122. break;
  123. case ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY:
  124. fieldName = "ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY";
  125. break;
  126. case ZT1_RESULT_FATAL_ERROR_DATA_STORE_FAILED:
  127. fieldName = "ZT1_RESULT_FATAL_ERROR_DATA_STORE_FAILED";
  128. break;
  129. case ZT1_RESULT_ERROR_NETWORK_NOT_FOUND:
  130. fieldName = "ZT1_RESULT_ERROR_NETWORK_NOT_FOUND";
  131. break;
  132. case ZT1_RESULT_FATAL_ERROR_INTERNAL:
  133. default:
  134. fieldName = "ZT1_RESULT_FATAL_ERROR_INTERNAL";
  135. break;
  136. }
  137. jfieldID enumField = env->GetStaticFieldID(resultClass, fieldName.c_str(), "Lcom/zerotierone/sdk/ResultCode;");
  138. resultObject = env->GetStaticObjectField(resultClass, enumField);
  139. return resultObject;
  140. }
  141. ZT1_Node* findNode(uint64_t nodeId)
  142. {
  143. NodeMap::iterator found = nodeMap.find(nodeId);
  144. if(found != nodeMap.end())
  145. {
  146. JniRef *ref = found->second;
  147. return ref->node;
  148. }
  149. return NULL;
  150. }
  151. jobject createVirtualNetworkStatus(JNIEnv *env, ZT1_VirtualNetworkStatus status)
  152. {
  153. static jclass statusClass = NULL;
  154. jobject statusObject = NULL;
  155. if(statusClass == NULL)
  156. {
  157. statusClass = env->FindClass("com/zerotierone/sdk/VirtualNetworkStatus");
  158. if(statusClass == NULL)
  159. {
  160. return NULL; // exception thrown
  161. }
  162. }
  163. std::string fieldName;
  164. switch(status)
  165. {
  166. case ZT1_NETWORK_STATUS_REQUESTING_CONFIGURATION:
  167. fieldName = "NETWORK_STATUS_REQUESTING_CONFIGURATION";
  168. break;
  169. case ZT1_NETWORK_STATUS_OK:
  170. fieldName = "NETWORK_STATUS_OK";
  171. break;
  172. case ZT1_NETWORK_STATUS_ACCESS_DENIED:
  173. fieldName = "NETWORK_STATUS_ACCESS_DENIED";
  174. break;
  175. case ZT1_NETWORK_STATUS_NOT_FOUND:
  176. fieldName = "NETWORK_STATUS_NOT_FOUND";
  177. break;
  178. case ZT1_NETWORK_STATUS_PORT_ERROR:
  179. fieldName = "NETWORK_STATUS_PORT_ERROR";
  180. break;
  181. case ZT1_NETWORK_STATUS_CLIENT_TOO_OLD:
  182. fieldName = "NETWORK_STATUS_CLIENT_TOO_OLD";
  183. break;
  184. }
  185. jfieldID enumField = env->GetStaticFieldID(statusClass, fieldName.c_str(), "Lcom/zerotierone/sdk/VirtualNetworkStatus;");
  186. statusObject = env->GetStaticObjectField(statusClass, enumField);
  187. return statusObject;
  188. }
  189. jobject createEvent(JNIEnv *env, ZT1_Event event)
  190. {
  191. static jclass eventClass = NULL;
  192. jobject eventObject = NULL;
  193. if(eventClass == NULL)
  194. {
  195. eventClass = env->FindClass("com/zerotierone/sdk/Event");
  196. if(eventClass == NULL)
  197. {
  198. return NULL;
  199. }
  200. }
  201. std::string fieldName;
  202. switch(event)
  203. {
  204. case ZT1_EVENT_UP:
  205. fieldName = "EVENT_UP";
  206. break;
  207. case ZT1_EVENT_OFFLINE:
  208. fieldName = "EVENT_OFFLINE";
  209. break;
  210. case ZT1_EVENT_DOWN:
  211. fieldName = "EVENT_DOWN";
  212. break;
  213. case ZT1_EVENT_FATAL_ERROR_IDENTITY_COLLISION:
  214. fieldName = "EVENT_FATAL_ERROR_IDENTITY_COLLISION";
  215. break;
  216. case ZT1_EVENT_AUTHENTICATION_FAILURE:
  217. fieldName = "EVENT_AUTHENTICATION_FAILURE";
  218. break;
  219. case ZT1_EVENT_INVALID_PACKET:
  220. fieldName = "EVENT_INVALID_PACKET";
  221. break;
  222. case ZT1_EVENT_TRACE:
  223. fieldName = "EVENT_TRACE";
  224. break;
  225. }
  226. jfieldID enumField = env->GetStaticFieldID(eventClass, fieldName.c_str(), "Lcom/zerotierone/sdk/Event;");
  227. eventObject = env->GetStaticObjectField(eventClass, enumField);
  228. return eventObject;
  229. }
  230. }
  231. /*
  232. * Class: com_zerotierone_sdk_Node
  233. * Method: node_init
  234. * Signature: (J)Lcom/zerotierone/sdk/ResultCode;
  235. */
  236. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_node_1init
  237. (JNIEnv *env, jobject obj, jlong now)
  238. {
  239. jobject resultObject = createResultObject(env, ZT1_RESULT_OK);
  240. ZT1_Node *node;
  241. JniRef *ref = new JniRef;
  242. ZT1_ResultCode rc = ZT1_Node_new(
  243. &node,
  244. ref,
  245. (uint64_t)now,
  246. &DataStoreGetFunction,
  247. &DataStorePutFunction,
  248. &WirePacketSendFunction,
  249. &VirtualNetworkFrameFunctionCallback,
  250. &VirtualNetworkConfigFunctionCallback,
  251. &EventCallback);
  252. if(rc != ZT1_RESULT_OK)
  253. {
  254. resultObject = createResultObject(env, rc);
  255. if(node)
  256. {
  257. ZT1_Node_delete(node);
  258. node = NULL;
  259. }
  260. delete ref;
  261. ref = NULL;
  262. return resultObject;
  263. }
  264. ref->id = (uint64_t)now;
  265. ref->env = env;
  266. ref->node = node;
  267. jclass cls = env->GetObjectClass(obj);
  268. jfieldID fid = env->GetFieldID(
  269. cls, "getListener", "Lcom/zerotierone/sdk/DataStoreGetListener;");
  270. if(fid == NULL)
  271. {
  272. return NULL; // exception already thrown
  273. }
  274. ref->dataStoreGetListener = env->GetObjectField(obj, fid);
  275. if(ref->dataStoreGetListener == NULL)
  276. {
  277. return NULL;
  278. }
  279. fid = env->GetFieldID(
  280. cls, "putListener", "Lcom/zerotierone/sdk/DataStorePutLisetner;");
  281. if(fid == NULL)
  282. {
  283. return NULL; // exception already thrown
  284. }
  285. ref->dataStorePutListener = env->GetObjectField(obj, fid);
  286. if(ref->dataStorePutListener == NULL)
  287. {
  288. return NULL;
  289. }
  290. fid = env->GetFieldID(
  291. cls, "sender", "Lcom/zerotierone/sdk/PacketSender;");
  292. if(fid == NULL)
  293. {
  294. return NULL; // exception already thrown
  295. }
  296. ref->packetSender = env->GetObjectField(obj, fid);
  297. if(ref->packetSender == NULL)
  298. {
  299. return NULL;
  300. }
  301. fid = env->GetFieldID(
  302. cls, "frameListener", "Lcom/zerotierone/sdk/VirtualNetworkFrameListener;");
  303. if(fid == NULL)
  304. {
  305. return NULL; // exception already thrown
  306. }
  307. ref->frameListener = env->GetObjectField(obj, fid);
  308. if(ref->frameListener = NULL)
  309. {
  310. return NULL;
  311. }
  312. fid = env->GetFieldID(
  313. cls, "configListener", "Lcom/zerotierone/sdk/VirtualNetworkConfigListener;");
  314. if(fid == NULL)
  315. {
  316. return NULL; // exception already thrown
  317. }
  318. ref->configListener = env->GetObjectField(obj, fid);
  319. if(ref->configListener == NULL)
  320. {
  321. return NULL;
  322. }
  323. nodeMap.insert(std::make_pair(ref->id, ref));
  324. return resultObject;
  325. }
  326. /*
  327. * Class: com_zerotierone_sdk_Node
  328. * Method: node_delete
  329. * Signature: (J)V
  330. */
  331. JNIEXPORT void JNICALL Java_com_zerotierone_sdk_Node_node_1delete
  332. (JNIEnv *env, jobject obj, jlong id)
  333. {
  334. uint64_t nodeId = (uint64_t)id;
  335. NodeMap::iterator found = nodeMap.find(nodeId);
  336. if(found != nodeMap.end())
  337. {
  338. JniRef *ref = found->second;
  339. nodeMap.erase(found);
  340. ZT1_Node_delete(ref->node);
  341. delete ref;
  342. ref = NULL;
  343. }
  344. }
  345. /*
  346. * Class: com_zerotierone_sdk_Node
  347. * Method: processVirtualNetworkFrame
  348. * Signature: (JJJJJII[B[J)Lcom/zerotierone/sdk/ResultCode;
  349. */
  350. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_processVirtualNetworkFrame
  351. (JNIEnv *env, jobject obj,
  352. jlong id,
  353. jlong in_now,
  354. jlong in_nwid,
  355. jlong in_sourceMac,
  356. jlong in_destMac,
  357. jint in_etherType,
  358. jint in_vlanId,
  359. jbyteArray in_frameData,
  360. jlongArray out_nextBackgroundTaskDeadline)
  361. {
  362. uint64_t nodeId = (uint64_t) id;
  363. ZT1_Node *node = findNode(nodeId);
  364. if(node == NULL)
  365. {
  366. // cannot find valid node. We should never get here.
  367. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  368. }
  369. unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline);
  370. if(nbtd_len < 1)
  371. {
  372. // array for next background task length has 0 elements!
  373. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  374. }
  375. uint64_t now = (uint64_t)in_now;
  376. uint64_t nwid = (uint64_t)in_nwid;
  377. uint64_t sourceMac = (uint64_t)in_sourceMac;
  378. uint64_t destMac = (uint64_t)in_destMac;
  379. unsigned int etherType = (unsigned int)in_etherType;
  380. unsigned int vlanId = (unsigned int)in_vlanId;
  381. unsigned int frameLength = env->GetArrayLength(in_frameData);
  382. jbyte *frameData =env->GetByteArrayElements(in_frameData, NULL);
  383. uint64_t nextBackgroundTaskDeadline = 0;
  384. ZT1_ResultCode rc = ZT1_Node_processVirtualNetworkFrame(
  385. node,
  386. now,
  387. nwid,
  388. sourceMac,
  389. destMac,
  390. etherType,
  391. vlanId,
  392. (const void*)frameData,
  393. frameLength,
  394. &nextBackgroundTaskDeadline);
  395. jlong *outDeadline = env->GetLongArrayElements(out_nextBackgroundTaskDeadline, NULL);
  396. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  397. env->ReleaseLongArrayElements(out_nextBackgroundTaskDeadline, outDeadline, 0);
  398. env->ReleaseByteArrayElements(in_frameData, frameData, 0);
  399. return createResultObject(env, rc);
  400. }
  401. /*
  402. * Class: com_zerotierone_sdk_Node
  403. * Method: processWirePacket
  404. * Signature: (JJLjava/net/InetAddress;I[B[J)Lcom/zerotierone/sdk/ResultCode;
  405. */
  406. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_processWirePacket
  407. (JNIEnv *env, jobject obj,
  408. jlong id,
  409. jlong in_now,
  410. jobject in_remoteAddress,
  411. jint in_linkDesparation,
  412. jbyteArray in_packetData,
  413. jlongArray out_nextBackgroundTaskDeadline)
  414. {
  415. uint64_t nodeId = (uint64_t) id;
  416. ZT1_Node *node = findNode(nodeId);
  417. if(node == NULL)
  418. {
  419. // cannot find valid node. We should never get here.
  420. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  421. }
  422. unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline);
  423. if(nbtd_len < 1)
  424. {
  425. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  426. }
  427. uint64_t now = (uint64_t)in_now;
  428. unsigned int linkDesparation = (unsigned int)in_linkDesparation;
  429. // get the java.net.InetAddress class and getAddress() method
  430. jclass inetAddressClass = env->FindClass("java/net/InetAddress");
  431. if(inetAddressClass == NULL)
  432. {
  433. // can't find java.net.InetAddress
  434. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  435. }
  436. jmethodID getAddressMethod = env->GetMethodID(
  437. inetAddressClass, "getAddress", "()[B");
  438. if(getAddressMethod == NULL)
  439. {
  440. // cant find InetAddress.getAddres()
  441. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  442. }
  443. // Call InetAddress.getAddress()
  444. jbyteArray addressArray = (jbyteArray)env->CallObjectMethod(in_remoteAddress, getAddressMethod);
  445. if(addressArray == NULL)
  446. {
  447. // unable to call getAddress()
  448. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  449. }
  450. unsigned int addrSize = env->GetArrayLength(addressArray);
  451. // get the address bytes
  452. jbyte *addr = env->GetByteArrayElements(addressArray, NULL);
  453. sockaddr_storage remoteAddress = {};
  454. if(addrSize == 16)
  455. {
  456. // IPV6 address
  457. sockaddr_in6 ipv6 = {};
  458. ipv6.sin6_family = AF_INET6;
  459. memcpy(ipv6.sin6_addr.s6_addr, addr, 16);
  460. memcpy(&remoteAddress, &ipv6, sizeof(sockaddr_in6));
  461. }
  462. else if(addrSize = 4)
  463. {
  464. // IPV4 address
  465. sockaddr_in ipv4 = {};
  466. ipv4.sin_family = AF_INET;
  467. memcpy(&ipv4.sin_addr, addr, 4);
  468. memcpy(&remoteAddress, &ipv4, sizeof(sockaddr_in));
  469. }
  470. else
  471. {
  472. // unknown address type
  473. env->ReleaseByteArrayElements(addressArray, addr, 0);
  474. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  475. }
  476. unsigned int packetLength = env->GetArrayLength(in_packetData);
  477. jbyte *packetData = env->GetByteArrayElements(in_packetData, NULL);
  478. uint64_t nextBackgroundTaskDeadline = 0;
  479. ZT1_ResultCode rc = ZT1_Node_processWirePacket(
  480. node,
  481. now,
  482. &remoteAddress,
  483. linkDesparation,
  484. packetData,
  485. packetLength,
  486. &nextBackgroundTaskDeadline);
  487. jlong *outDeadline = env->GetLongArrayElements(out_nextBackgroundTaskDeadline, NULL);
  488. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  489. env->ReleaseLongArrayElements(out_nextBackgroundTaskDeadline, outDeadline, 0);
  490. env->ReleaseByteArrayElements(addressArray, addr, 0);
  491. env->ReleaseByteArrayElements(in_packetData, packetData, 0);
  492. return createResultObject(env, ZT1_RESULT_OK);
  493. }
  494. /*
  495. * Class: com_zerotierone_sdk_Node
  496. * Method: processBackgroundTasks
  497. * Signature: (JJ[J)Lcom/zerotierone/sdk/ResultCode;
  498. */
  499. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_processBackgroundTasks
  500. (JNIEnv *env, jobject obj,
  501. jlong id,
  502. jlong in_now,
  503. jlongArray out_nextBackgroundTaskDeadline)
  504. {
  505. uint64_t nodeId = (uint64_t) id;
  506. ZT1_Node *node = findNode(nodeId);
  507. if(node == NULL)
  508. {
  509. // cannot find valid node. We should never get here.
  510. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  511. }
  512. unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline);
  513. if(nbtd_len < 1)
  514. {
  515. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  516. }
  517. uint64_t now = (uint64_t)in_now;
  518. uint64_t nextBackgroundTaskDeadline = 0;
  519. ZT1_ResultCode rc = ZT1_Node_processBackgroundTasks(node, now, &nextBackgroundTaskDeadline);
  520. jlong *outDeadline = env->GetLongArrayElements(out_nextBackgroundTaskDeadline, NULL);
  521. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  522. env->ReleaseLongArrayElements(out_nextBackgroundTaskDeadline, outDeadline, 0);
  523. return createResultObject(env, rc);
  524. }
  525. /*
  526. * Class: com_zerotierone_sdk_Node
  527. * Method: join
  528. * Signature: (JJ)Lcom/zerotierone/sdk/ResultCode;
  529. */
  530. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_join
  531. (JNIEnv *env, jobject obj, jlong id, jlong in_nwid)
  532. {
  533. uint64_t nodeId = (uint64_t) id;
  534. ZT1_Node *node = findNode(nodeId);
  535. if(node == NULL)
  536. {
  537. // cannot find valid node. We should never get here.
  538. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  539. }
  540. uint64_t nwid = (uint64_t)in_nwid;
  541. ZT1_ResultCode rc = ZT1_Node_join(node, nwid);
  542. return createResultObject(env, rc);
  543. }
  544. /*
  545. * Class: com_zerotierone_sdk_Node
  546. * Method: leave
  547. * Signature: (JJ)Lcom/zerotierone/sdk/ResultCode;
  548. */
  549. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_leave
  550. (JNIEnv *env, jobject obj, jlong id, jlong in_nwid)
  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. uint64_t nwid = (uint64_t)in_nwid;
  560. ZT1_ResultCode rc = ZT1_Node_leave(node, nwid);
  561. return createResultObject(env, rc);
  562. }
  563. /*
  564. * Class: com_zerotierone_sdk_Node
  565. * Method: multicastSubscribe
  566. * Signature: (JJJJ)Lcom/zerotierone/sdk/ResultCode;
  567. */
  568. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_multicastSubscribe
  569. (JNIEnv *env, jobject obj,
  570. jlong id,
  571. jlong in_nwid,
  572. jlong in_multicastGroup,
  573. jlong in_multicastAdi)
  574. {
  575. uint64_t nodeId = (uint64_t) id;
  576. ZT1_Node *node = findNode(nodeId);
  577. if(node == NULL)
  578. {
  579. // cannot find valid node. We should never get here.
  580. return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
  581. }
  582. uint64_t nwid = (uint64_t)in_nwid;
  583. uint64_t multicastGroup = (uint64_t)in_multicastGroup;
  584. uint64_t multicastAdi = (uint64_t)in_multicastAdi;
  585. ZT1_ResultCode rc = ZT1_Node_multicastSubscribe(
  586. node, nwid, multicastGroup, multicastAdi);
  587. return createResultObject(env, rc);
  588. }
  589. /*
  590. * Class: com_zerotierone_sdk_Node
  591. * Method: multicastUnsubscribe
  592. * Signature: (JJJJ)Lcom/zerotierone/sdk/ResultCode;
  593. */
  594. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_multicastUnsubscribe
  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_multicastUnsubscribe(
  612. node, nwid, multicastGroup, multicastAdi);
  613. return createResultObject(env, rc);
  614. }
  615. /*
  616. * Class: com_zerotierone_sdk_Node
  617. * Method: address
  618. * Signature: (J)J
  619. */
  620. JNIEXPORT jlong JNICALL Java_com_zerotierone_sdk_Node_address
  621. (JNIEnv *env , jobject obj, jlong id)
  622. {
  623. uint64_t nodeId = (uint64_t) id;
  624. ZT1_Node *node = findNode(nodeId);
  625. if(node == NULL)
  626. {
  627. // cannot find valid node. We should never get here.
  628. return 0;
  629. }
  630. uint64_t address = ZT1_Node_address(node);
  631. return (jlong)address;
  632. }
  633. /*
  634. * Class: com_zerotierone_sdk_Node
  635. * Method: status
  636. * Signature: (J)Lcom/zerotierone/sdk/NodeStatus;
  637. */
  638. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_status
  639. (JNIEnv *env, jobject obj, jlong id)
  640. {
  641. uint64_t nodeId = (uint64_t) id;
  642. ZT1_Node *node = findNode(nodeId);
  643. if(node == NULL)
  644. {
  645. // cannot find valid node. We should never get here.
  646. return 0;
  647. }
  648. // static so we only have to look these up once
  649. static jclass nodeStatusClass = NULL;
  650. static jmethodID nodeStatusConstructor = NULL;
  651. // create a com.zerotierone.sdk.NodeStatus object
  652. if(nodeStatusClass == NULL)
  653. {
  654. nodeStatusClass = env->FindClass("com/zerotierone/sdk/NodeStatus");
  655. if(nodeStatusClass == NULL)
  656. {
  657. return NULL;
  658. }
  659. }
  660. if(nodeStatusConstructor == NULL)
  661. {
  662. nodeStatusConstructor = env->GetMethodID(
  663. nodeStatusClass, "<init>", "()V");
  664. if(nodeStatusConstructor == NULL)
  665. {
  666. return NULL;
  667. }
  668. }
  669. jobject nodeStatusObj = env->NewObject(nodeStatusClass, nodeStatusConstructor);
  670. if(nodeStatusObj == NULL)
  671. {
  672. return NULL;
  673. }
  674. ZT1_NodeStatus nodeStatus;
  675. ZT1_Node_status(node, &nodeStatus);
  676. // TODO: copy data from C to Java
  677. return nodeStatusObj;
  678. }
  679. /*
  680. * Class: com_zerotierone_sdk_Node
  681. * Method: networkConfig
  682. * Signature: (J)Lcom/zerotierone/sdk/VirtualNetworkConfig;
  683. */
  684. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_networkConfig
  685. (JNIEnv *env, jobject obj, jlong id, jlong nwid)
  686. {
  687. uint64_t nodeId = (uint64_t) id;
  688. ZT1_Node *node = findNode(nodeId);
  689. if(node == NULL)
  690. {
  691. // cannot find valid node. We should never get here.
  692. return 0;
  693. }
  694. // create a com.zerotierone.sdk.VirtualNetworkConfig object
  695. jclass vnetConfigClass = env->FindClass("com/zerotierone/sdk/VirtualNetworkConfig");
  696. if(vnetConfigClass == NULL)
  697. {
  698. return NULL;
  699. }
  700. jmethodID vnetConfigConstructor = env->GetMethodID(
  701. vnetConfigClass, "<init>", "()V");
  702. if(vnetConfigConstructor == NULL)
  703. {
  704. return NULL;
  705. }
  706. jobject vnetConfigObj = env->NewObject(vnetConfigClass, vnetConfigConstructor);
  707. if(vnetConfigObj == NULL)
  708. {
  709. return NULL;
  710. }
  711. ZT1_VirtualNetworkConfig *vnetConfig = ZT1_Node_networkConfig(node, nwid);
  712. // TODO: copy data from C to Java
  713. return NULL;
  714. }
  715. /*
  716. * Class: com_zerotierone_sdk_Node
  717. * Method: version
  718. * Signature: (J)Lcom/zerotierone/sdk/Version;
  719. */
  720. JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_version
  721. (JNIEnv *env, jobject obj)
  722. {
  723. // create a com.zerotierone.sdk.Version object
  724. jclass versionClass = env->FindClass("com/zerotierone/sdk/Version");
  725. if(versionClass == NULL)
  726. {
  727. return NULL;
  728. }
  729. jmethodID versionConstructor = env->GetMethodID(
  730. versionClass, "<init>", "()V");
  731. if(versionConstructor == NULL)
  732. {
  733. return NULL;
  734. }
  735. jobject versionObj = env->NewObject(versionClass, versionConstructor);
  736. if(versionObj == NULL)
  737. {
  738. return NULL;
  739. }
  740. int major = 0;
  741. int minor = 0;
  742. int revision = 0;
  743. unsigned long featureFlags = 0;
  744. ZT1_version(&major, &minor, &revision, &featureFlags);
  745. // copy data to Version object
  746. static jfieldID majorField = NULL;
  747. static jfieldID minorField = NULL;
  748. static jfieldID revisionField = NULL;
  749. static jfieldID featureFlagsField = NULL;
  750. if(majorField == NULL)
  751. {
  752. majorField = env->GetFieldID(versionClass, "major", "Lcom/zerotierone/sdk/Version;");
  753. if(majorField = NULL)
  754. {
  755. return NULL;
  756. }
  757. }
  758. if(minorField == NULL)
  759. {
  760. minorField = env->GetFieldID(versionClass, "minor", "Lcom/zerotierone/sdk/Version;");
  761. if(minorField == NULL)
  762. {
  763. return NULL;
  764. }
  765. }
  766. if(revisionField == NULL)
  767. {
  768. revisionField = env->GetFieldID(versionClass, "revision", "Lcom/zerotierone/sdk/Version;");
  769. if(revisionField == NULL)
  770. {
  771. return NULL;
  772. }
  773. }
  774. if(featureFlagsField == NULL)
  775. {
  776. featureFlagsField = env->GetFieldID(versionClass, "featureFlags", "Lcom/zerotierone/sdk/Version;");
  777. if(featureFlagsField == NULL)
  778. {
  779. return NULL;
  780. }
  781. }
  782. env->SetIntField(versionObj, majorField, (jint)major);
  783. env->SetIntField(versionObj, minorField, (jint)minor);
  784. env->SetIntField(versionObj, revisionField, (jint)revision);
  785. env->SetLongField(versionObj, featureFlagsField, (jlong)featureFlags);
  786. return versionObj;
  787. }
  788. #ifdef __cplusplus
  789. } // extern "C"
  790. #endif