com_zerotierone_sdk_Node.cpp 39 KB

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