2
0

com_zerotierone_sdk_Node.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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_jnicache.h"
  29. #include "ZT_jniutils.h"
  30. #include <ZeroTierOne.h>
  31. #include "Mutex.hpp"
  32. #include <map>
  33. #include <string>
  34. #include <cassert>
  35. #include <cstring>
  36. #define LOG_TAG "Node"
  37. namespace {
  38. struct JniRef
  39. {
  40. JniRef()
  41. : jvm(NULL)
  42. , node(NULL)
  43. , dataStoreGetListener(NULL)
  44. , dataStorePutListener(NULL)
  45. , packetSender(NULL)
  46. , eventListener(NULL)
  47. , frameListener(NULL)
  48. , configListener(NULL)
  49. , pathChecker(NULL)
  50. , callbacks(NULL)
  51. , inited()
  52. {
  53. callbacks = (ZT_Node_Callbacks*)malloc(sizeof(ZT_Node_Callbacks));
  54. memset(callbacks, 0, sizeof(ZT_Node_Callbacks));
  55. }
  56. ~JniRef()
  57. {
  58. JNIEnv *env = NULL;
  59. jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  60. env->DeleteGlobalRef(dataStoreGetListener);
  61. env->DeleteGlobalRef(dataStorePutListener);
  62. env->DeleteGlobalRef(packetSender);
  63. env->DeleteGlobalRef(eventListener);
  64. env->DeleteGlobalRef(frameListener);
  65. env->DeleteGlobalRef(configListener);
  66. env->DeleteGlobalRef(pathChecker);
  67. free(callbacks);
  68. callbacks = NULL;
  69. }
  70. int64_t id;
  71. JavaVM *jvm;
  72. ZT_Node *node;
  73. jobject dataStoreGetListener;
  74. jobject dataStorePutListener;
  75. jobject packetSender;
  76. jobject eventListener;
  77. jobject frameListener;
  78. jobject configListener;
  79. jobject pathChecker;
  80. ZT_Node_Callbacks *callbacks;
  81. bool inited;
  82. bool finishInitializing();
  83. };
  84. int VirtualNetworkConfigFunctionCallback(
  85. ZT_Node *node,
  86. void *userData,
  87. void *threadData,
  88. uint64_t nwid,
  89. void **,
  90. enum ZT_VirtualNetworkConfigOperation operation,
  91. const ZT_VirtualNetworkConfig *config)
  92. {
  93. LOGV("VirtualNetworkConfigFunctionCallback");
  94. JniRef *ref = (JniRef*)userData;
  95. JNIEnv *env = NULL;
  96. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  97. if (ref->configListener == NULL) {
  98. LOGE("configListener is NULL");
  99. return -100;
  100. }
  101. jobject operationObject = createVirtualNetworkConfigOperation(env, operation);
  102. if(env->ExceptionCheck() || operationObject == NULL)
  103. {
  104. return -101;
  105. }
  106. if (config == NULL) {
  107. LOGE("Config is NULL");
  108. return -102;
  109. }
  110. jobject networkConfigObject = newNetworkConfig(env, *config);
  111. if(env->ExceptionCheck() || networkConfigObject == NULL)
  112. {
  113. return -103;
  114. }
  115. return env->CallIntMethod(
  116. ref->configListener,
  117. VirtualNetworkConfigListener_onNetworkConfigurationUpdated_method,
  118. (jlong)nwid, operationObject, networkConfigObject);
  119. }
  120. void VirtualNetworkFrameFunctionCallback(ZT_Node *node,
  121. void *userData,
  122. void *threadData,
  123. uint64_t nwid,
  124. void**,
  125. uint64_t sourceMac,
  126. uint64_t destMac,
  127. unsigned int etherType,
  128. unsigned int vlanid,
  129. const void *frameData,
  130. unsigned int frameLength)
  131. {
  132. LOGV("VirtualNetworkFrameFunctionCallback");
  133. #ifndef NDEBUG
  134. unsigned char* local = (unsigned char*)frameData;
  135. LOGV("Type Bytes: 0x%02x%02x", local[12], local[13]);
  136. #endif
  137. JniRef *ref = (JniRef*)userData;
  138. assert(ref->node == node);
  139. JNIEnv *env = NULL;
  140. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  141. if (ref->frameListener == NULL) {
  142. LOGE("frameListener is NULL");
  143. return;
  144. }
  145. const unsigned char *bytes = static_cast<const unsigned char*>(frameData);
  146. jbyteArray dataArray = newByteArray(env, bytes, frameLength);
  147. if(env->ExceptionCheck() || dataArray == NULL)
  148. {
  149. return;
  150. }
  151. env->CallVoidMethod(ref->frameListener, VirtualNetworkFrameListener_onVirtualNetworkFrame_method, (jlong)nwid, (jlong)sourceMac, (jlong)destMac, (jlong)etherType, (jlong)vlanid, dataArray);
  152. }
  153. void EventCallback(ZT_Node *node,
  154. void *userData,
  155. void *threadData,
  156. enum ZT_Event event,
  157. const void *data) {
  158. LOGV("EventCallback");
  159. JniRef *ref = (JniRef *) userData;
  160. if (ref->node != node && event != ZT_EVENT_UP) {
  161. LOGE("Nodes not equal. ref->node %p, node %p. Event: %d", ref->node, node, event);
  162. return;
  163. }
  164. JNIEnv *env = NULL;
  165. ref->jvm->GetEnv((void **) &env, JNI_VERSION_1_6);
  166. if (ref->eventListener == NULL) {
  167. LOGE("eventListener is NULL");
  168. return;
  169. }
  170. jobject eventObject = createEvent(env, event);
  171. if (env->ExceptionCheck() || eventObject == NULL) {
  172. return;
  173. }
  174. switch (event) {
  175. case ZT_EVENT_UP: {
  176. LOGD("Event Up");
  177. env->CallVoidMethod(ref->eventListener, EventListener_onEvent_method, eventObject);
  178. break;
  179. }
  180. case ZT_EVENT_OFFLINE: {
  181. LOGD("Event Offline");
  182. env->CallVoidMethod(ref->eventListener, EventListener_onEvent_method, eventObject);
  183. break;
  184. }
  185. case ZT_EVENT_ONLINE: {
  186. LOGD("Event Online");
  187. env->CallVoidMethod(ref->eventListener, EventListener_onEvent_method, eventObject);
  188. break;
  189. }
  190. case ZT_EVENT_DOWN: {
  191. LOGD("Event Down");
  192. env->CallVoidMethod(ref->eventListener, EventListener_onEvent_method, eventObject);
  193. break;
  194. }
  195. case ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION: {
  196. LOGV("Identity Collision");
  197. // call onEvent()
  198. env->CallVoidMethod(ref->eventListener, EventListener_onEvent_method, eventObject);
  199. }
  200. break;
  201. case ZT_EVENT_TRACE: {
  202. LOGV("Trace Event");
  203. // call onTrace()
  204. if (data != NULL) {
  205. const char *message = (const char *) data;
  206. jstring messageStr = env->NewStringUTF(message);
  207. env->CallVoidMethod(ref->eventListener, EventListener_onTrace_method, messageStr);
  208. }
  209. }
  210. break;
  211. case ZT_EVENT_USER_MESSAGE:
  212. case ZT_EVENT_REMOTE_TRACE:
  213. default:
  214. break;
  215. }
  216. }
  217. void StatePutFunction(
  218. ZT_Node *node,
  219. void *userData,
  220. void *threadData,
  221. enum ZT_StateObjectType type,
  222. const uint64_t id[2],
  223. const void *buffer,
  224. int bufferLength) {
  225. char p[4096] = {0};
  226. bool secure = false;
  227. switch (type) {
  228. case ZT_STATE_OBJECT_IDENTITY_PUBLIC:
  229. snprintf(p, sizeof(p), "identity.public");
  230. break;
  231. case ZT_STATE_OBJECT_IDENTITY_SECRET:
  232. snprintf(p, sizeof(p), "identity.secret");
  233. secure = true;
  234. break;
  235. case ZT_STATE_OBJECT_PLANET:
  236. snprintf(p, sizeof(p), "planet");
  237. break;
  238. case ZT_STATE_OBJECT_MOON:
  239. snprintf(p, sizeof(p), "moons.d/%.16llx.moon", (unsigned long long)id[0]);
  240. break;
  241. case ZT_STATE_OBJECT_NETWORK_CONFIG:
  242. snprintf(p, sizeof(p), "networks.d/%.16llx.conf", (unsigned long long)id[0]);
  243. break;
  244. case ZT_STATE_OBJECT_PEER:
  245. snprintf(p, sizeof(p), "peers.d/%.10llx", (unsigned long long)id[0]);
  246. break;
  247. default:
  248. return;
  249. }
  250. if (strlen(p) < 1) {
  251. return;
  252. }
  253. JniRef *ref = (JniRef*)userData;
  254. JNIEnv *env = NULL;
  255. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  256. if (ref->dataStorePutListener == NULL) {
  257. LOGE("dataStorePutListener is NULL");
  258. return;
  259. }
  260. jstring nameStr = env->NewStringUTF(p);
  261. if (bufferLength >= 0) {
  262. LOGD("JNI: Write file: %s", p);
  263. const unsigned char *bytes = static_cast<const unsigned char *>(buffer);
  264. jbyteArray bufferObj = newByteArray(env, bytes, bufferLength);
  265. if(env->ExceptionCheck() || bufferObj == NULL)
  266. {
  267. return;
  268. }
  269. env->CallIntMethod(ref->dataStorePutListener,
  270. DataStorePutListener_onDataStorePut_method,
  271. nameStr, bufferObj, secure);
  272. } else {
  273. LOGD("JNI: Delete file: %s", p);
  274. env->CallIntMethod(ref->dataStorePutListener, DataStorePutListener_onDelete_method, nameStr);
  275. }
  276. }
  277. int StateGetFunction(
  278. ZT_Node *node,
  279. void *userData,
  280. void *threadData,
  281. ZT_StateObjectType type,
  282. const uint64_t id[2],
  283. void *buffer,
  284. unsigned int bufferLength) {
  285. char p[4096] = {0};
  286. switch (type) {
  287. case ZT_STATE_OBJECT_IDENTITY_PUBLIC:
  288. snprintf(p, sizeof(p), "identity.public");
  289. break;
  290. case ZT_STATE_OBJECT_IDENTITY_SECRET:
  291. snprintf(p, sizeof(p), "identity.secret");
  292. break;
  293. case ZT_STATE_OBJECT_PLANET:
  294. snprintf(p, sizeof(p), "planet");
  295. break;
  296. case ZT_STATE_OBJECT_MOON:
  297. snprintf(p, sizeof(p), "moons.d/%.16llx.moon", (unsigned long long)id[0]);
  298. break;
  299. case ZT_STATE_OBJECT_NETWORK_CONFIG:
  300. snprintf(p, sizeof(p), "networks.d/%.16llx.conf", (unsigned long long)id[0]);
  301. break;
  302. case ZT_STATE_OBJECT_PEER:
  303. snprintf(p, sizeof(p), "peers.d/%.10llx", (unsigned long long)id[0]);
  304. break;
  305. default:
  306. return -100;
  307. }
  308. if (strlen(p) < 1) {
  309. return -101;
  310. }
  311. JniRef *ref = (JniRef*)userData;
  312. JNIEnv *env = NULL;
  313. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  314. if (ref->dataStoreGetListener == NULL) {
  315. LOGE("dataStoreGetListener is NULL");
  316. return -102;
  317. }
  318. jstring nameStr = env->NewStringUTF(p);
  319. if(nameStr == NULL)
  320. {
  321. LOGE("Error creating name string object");
  322. return -103; // out of memory
  323. }
  324. jbyteArray bufferObj = newByteArray(env, bufferLength);
  325. if(env->ExceptionCheck() || bufferObj == NULL)
  326. {
  327. return -104;
  328. }
  329. LOGV("Calling onDataStoreGet(%s, %p)", p, buffer);
  330. int retval = (int)env->CallLongMethod(
  331. ref->dataStoreGetListener,
  332. DataStoreGetListener_onDataStoreGet_method,
  333. nameStr,
  334. bufferObj);
  335. LOGV("onDataStoreGet returned %d", retval);
  336. if(retval > 0)
  337. {
  338. void *data = env->GetPrimitiveArrayCritical(bufferObj, NULL);
  339. memcpy(buffer, data, retval);
  340. env->ReleasePrimitiveArrayCritical(bufferObj, data, 0);
  341. }
  342. return retval;
  343. }
  344. int WirePacketSendFunction(ZT_Node *node,
  345. void *userData,
  346. void *threadData,
  347. int64_t localSocket,
  348. const struct sockaddr_storage *remoteAddress,
  349. const void *buffer,
  350. unsigned int bufferSize,
  351. unsigned int ttl)
  352. {
  353. LOGV("WirePacketSendFunction(%lld, %p, %p, %d)", (long long)localSocket, remoteAddress, buffer, bufferSize);
  354. JniRef *ref = (JniRef*)userData;
  355. assert(ref->node == node);
  356. JNIEnv *env = NULL;
  357. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  358. if (ref->packetSender == NULL) {
  359. LOGE("packetSender is NULL");
  360. return -100;
  361. }
  362. jobject remoteAddressObj = newInetSocketAddress(env, *remoteAddress);
  363. const unsigned char *bytes = static_cast<const unsigned char *>(buffer);
  364. jbyteArray bufferObj = newByteArray(env, bytes, bufferSize);
  365. if (env->ExceptionCheck() || bufferObj == NULL)
  366. {
  367. return -101;
  368. }
  369. int retval = env->CallIntMethod(ref->packetSender, PacketSender_onSendPacketRequested_method, localSocket, remoteAddressObj, bufferObj);
  370. LOGV("JNI Packet Sender returned: %d", retval);
  371. return retval;
  372. }
  373. int PathCheckFunction(ZT_Node *node,
  374. void *userPtr,
  375. void *threadPtr,
  376. uint64_t address,
  377. int64_t localSocket,
  378. const struct sockaddr_storage *remoteAddress)
  379. {
  380. JniRef *ref = (JniRef*)userPtr;
  381. assert(ref->node == node);
  382. if(ref->pathChecker == NULL) {
  383. return true;
  384. }
  385. JNIEnv *env = NULL;
  386. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  387. //
  388. // was:
  389. // struct sockaddr_storage nullAddress = {0};
  390. //
  391. // but was getting this warning:
  392. // warning: suggest braces around initialization of subobject
  393. //
  394. // when building ZeroTierOne
  395. //
  396. struct sockaddr_storage nullAddress;
  397. //
  398. // It is possible to assume knowledge about internals of sockaddr_storage and construct
  399. // correct 0-initializer, but it is simpler to just treat sockaddr_storage as opaque and
  400. // use memset here to fill with 0
  401. //
  402. // This is also done in InetAddress.hpp for InetAddress
  403. //
  404. memset(&nullAddress, 0, sizeof(sockaddr_storage));
  405. jobject remoteAddressObj = NULL;
  406. if(memcmp(remoteAddress, &nullAddress, sizeof(sockaddr_storage)) != 0)
  407. {
  408. remoteAddressObj = newInetSocketAddress(env, *remoteAddress);
  409. }
  410. return env->CallBooleanMethod(ref->pathChecker, PathChecker_onPathCheck_method, address, localSocket, remoteAddressObj);
  411. }
  412. int PathLookupFunction(ZT_Node *node,
  413. void *userPtr,
  414. void *threadPtr,
  415. uint64_t address,
  416. int ss_family,
  417. struct sockaddr_storage *result)
  418. {
  419. JniRef *ref = (JniRef*)userPtr;
  420. assert(ref->node == node);
  421. if(ref->pathChecker == NULL) {
  422. return false;
  423. }
  424. JNIEnv *env = NULL;
  425. ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
  426. jobject sockAddressObject = env->CallObjectMethod(ref->pathChecker, PathChecker_onPathLookup_method, address, ss_family);
  427. if(sockAddressObject == NULL)
  428. {
  429. LOGE("Unable to call onPathLookup implementation");
  430. return false;
  431. }
  432. *result = fromSocketAddressObject(env, sockAddressObject);
  433. if (env->ExceptionCheck() || isSocketAddressEmpty(*result)) {
  434. return false;
  435. }
  436. return true;
  437. }
  438. typedef std::map<int64_t, JniRef*> NodeMap;
  439. NodeMap nodeMap;
  440. ZeroTier::Mutex nodeMapMutex;
  441. bool isInited(int64_t nodeId) {
  442. ZeroTier::Mutex::Lock lock(nodeMapMutex);
  443. NodeMap::iterator found = nodeMap.find(nodeId);
  444. if (found == nodeMap.end()) {
  445. //
  446. // not in map yet, or has been removed from map
  447. //
  448. return false;
  449. }
  450. JniRef *ref = found->second;
  451. assert(ref);
  452. return ref->inited;
  453. }
  454. bool JniRef::finishInitializing() {
  455. ZeroTier::Mutex::Lock lock(nodeMapMutex);
  456. NodeMap::iterator found = nodeMap.find(id);
  457. if (found != nodeMap.end()) {
  458. //
  459. // already in map
  460. //
  461. LOGE("Cannot finish initializing; node is already in map");
  462. return false;
  463. }
  464. nodeMap.insert(std::make_pair(id, this));
  465. assert(!inited);
  466. inited = true;
  467. return true;
  468. }
  469. ZT_Node* findNode(int64_t nodeId)
  470. {
  471. ZeroTier::Mutex::Lock lock(nodeMapMutex);
  472. NodeMap::iterator found = nodeMap.find(nodeId);
  473. assert(found != nodeMap.end());
  474. JniRef *ref = found->second;
  475. assert(ref);
  476. return ref->node;
  477. }
  478. JniRef *removeRef(int64_t nodeId) {
  479. ZeroTier::Mutex::Lock lock(nodeMapMutex);
  480. NodeMap::iterator found = nodeMap.find(nodeId);
  481. if (found == nodeMap.end()) {
  482. return nullptr;
  483. }
  484. JniRef *ref = found->second;
  485. assert(ref);
  486. nodeMap.erase(nodeId);
  487. return ref;
  488. }
  489. }
  490. #ifdef __cplusplus
  491. extern "C" {
  492. #endif
  493. JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
  494. {
  495. setupJNICache(vm);
  496. return JNI_VERSION_1_6;
  497. }
  498. JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
  499. {
  500. teardownJNICache(vm);
  501. }
  502. /*
  503. * Class: com_zerotier_sdk_Node
  504. * Method: node_init
  505. * Signature: (JLcom/zerotier/sdk/DataStoreGetListener;Lcom/zerotier/sdk/DataStorePutListener;Lcom/zerotier/sdk/PacketSender;Lcom/zerotier/sdk/EventListener;Lcom/zerotier/sdk/VirtualNetworkFrameListener;Lcom/zerotier/sdk/VirtualNetworkConfigListener;Lcom/zerotier/sdk/PathChecker;)Lcom/zerotier/sdk/ResultCode;
  506. */
  507. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_node_1init(
  508. JNIEnv *env, jobject obj, jlong now, jobject dataStoreGetListener,
  509. jobject dataStorePutListener, jobject packetSender, jobject eventListener,
  510. jobject frameListener, jobject configListener,
  511. jobject pathChecker)
  512. {
  513. LOGV("Creating ZT_Node struct");
  514. jobject resultObject = ResultCode_RESULT_OK_enum;
  515. ZT_Node *node;
  516. JniRef *ref = new JniRef;
  517. ref->id = (int64_t)now;
  518. env->GetJavaVM(&ref->jvm);
  519. if(dataStoreGetListener == NULL)
  520. {
  521. return NULL;
  522. }
  523. ref->dataStoreGetListener = env->NewGlobalRef(dataStoreGetListener);
  524. if(dataStorePutListener == NULL)
  525. {
  526. return NULL;
  527. }
  528. ref->dataStorePutListener = env->NewGlobalRef(dataStorePutListener);
  529. if(packetSender == NULL)
  530. {
  531. return NULL;
  532. }
  533. ref->packetSender = env->NewGlobalRef(packetSender);
  534. if(frameListener == NULL)
  535. {
  536. return NULL;
  537. }
  538. ref->frameListener = env->NewGlobalRef(frameListener);
  539. if(configListener == NULL)
  540. {
  541. return NULL;
  542. }
  543. ref->configListener = env->NewGlobalRef(configListener);
  544. if(eventListener == NULL)
  545. {
  546. return NULL;
  547. }
  548. ref->eventListener = env->NewGlobalRef(eventListener);
  549. if(pathChecker != NULL)
  550. {
  551. ref->pathChecker = env->NewGlobalRef(pathChecker);
  552. }
  553. ref->callbacks->stateGetFunction = &StateGetFunction;
  554. ref->callbacks->statePutFunction = &StatePutFunction;
  555. ref->callbacks->wirePacketSendFunction = &WirePacketSendFunction;
  556. ref->callbacks->virtualNetworkFrameFunction = &VirtualNetworkFrameFunctionCallback;
  557. ref->callbacks->virtualNetworkConfigFunction = &VirtualNetworkConfigFunctionCallback;
  558. ref->callbacks->eventCallback = &EventCallback;
  559. ref->callbacks->pathCheckFunction = &PathCheckFunction;
  560. ref->callbacks->pathLookupFunction = &PathLookupFunction;
  561. ZT_ResultCode rc = ZT_Node_new(
  562. &node,
  563. ref,
  564. NULL,
  565. ref->callbacks,
  566. (int64_t)now);
  567. if(rc != ZT_RESULT_OK)
  568. {
  569. LOGE("Error creating Node: %d", rc);
  570. resultObject = createResultObject(env, rc);
  571. if (env->ExceptionCheck() || resultObject == NULL) {
  572. return NULL;
  573. }
  574. if(node)
  575. {
  576. ZT_Node_delete(node);
  577. node = NULL;
  578. }
  579. delete ref;
  580. ref = NULL;
  581. return resultObject;
  582. }
  583. //
  584. // node is now updated
  585. //
  586. ref->node = node;
  587. if (!ref->finishInitializing()) {
  588. LOGE("finishInitializing() failed");
  589. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  590. }
  591. return resultObject;
  592. }
  593. /*
  594. * Class: com_zerotier_sdk_Node
  595. * Method: node_isInited
  596. * Signature: (J)Z
  597. */
  598. JNIEXPORT jboolean JNICALL Java_com_zerotier_sdk_Node_node_1isInited
  599. (JNIEnv *env, jobject obj, jlong nodeId) {
  600. return isInited(nodeId);
  601. }
  602. /*
  603. * Class: com_zerotier_sdk_Node
  604. * Method: node_delete
  605. * Signature: (J)V
  606. */
  607. JNIEXPORT void JNICALL Java_com_zerotier_sdk_Node_node_1delete(
  608. JNIEnv *env, jobject obj, jlong id)
  609. {
  610. LOGV("Destroying ZT_Node struct");
  611. int64_t nodeId = (int64_t)id;
  612. JniRef *ref = removeRef(nodeId);
  613. if (!ref) {
  614. return;
  615. }
  616. ZT_Node_delete(ref->node);
  617. delete ref;
  618. }
  619. /*
  620. * Class: com_zerotier_sdk_Node
  621. * Method: processVirtualNetworkFrame
  622. * Signature: (JJJJJII[B[J)Lcom/zerotier/sdk/ResultCode;
  623. */
  624. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processVirtualNetworkFrame(
  625. JNIEnv *env, jobject obj,
  626. jlong id,
  627. jlong in_now,
  628. jlong in_nwid,
  629. jlong in_sourceMac,
  630. jlong in_destMac,
  631. jint in_etherType,
  632. jint in_vlanId,
  633. jbyteArray in_frameData,
  634. jlongArray out_nextBackgroundTaskDeadline)
  635. {
  636. int64_t nodeId = (int64_t) id;
  637. ZT_Node *node = findNode(nodeId);
  638. unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline);
  639. if(nbtd_len < 1)
  640. {
  641. // array for next background task length has 0 elements!
  642. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  643. }
  644. int64_t now = (int64_t)in_now;
  645. uint64_t nwid = (uint64_t)in_nwid;
  646. uint64_t sourceMac = (uint64_t)in_sourceMac;
  647. uint64_t destMac = (uint64_t)in_destMac;
  648. unsigned int etherType = (unsigned int)in_etherType;
  649. unsigned int vlanId = (unsigned int)in_vlanId;
  650. unsigned int frameLength = env->GetArrayLength(in_frameData);
  651. void *frameData = env->GetPrimitiveArrayCritical(in_frameData, NULL);
  652. void *localData = malloc(frameLength);
  653. memcpy(localData, frameData, frameLength);
  654. env->ReleasePrimitiveArrayCritical(in_frameData, frameData, 0);
  655. int64_t nextBackgroundTaskDeadline = 0;
  656. ZT_ResultCode rc = ZT_Node_processVirtualNetworkFrame(
  657. node,
  658. NULL,
  659. now,
  660. nwid,
  661. sourceMac,
  662. destMac,
  663. etherType,
  664. vlanId,
  665. (const void*)localData,
  666. frameLength,
  667. &nextBackgroundTaskDeadline);
  668. free(localData);
  669. jlong *outDeadline = (jlong*)env->GetPrimitiveArrayCritical(out_nextBackgroundTaskDeadline, NULL);
  670. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  671. env->ReleasePrimitiveArrayCritical(out_nextBackgroundTaskDeadline, outDeadline, 0);
  672. return createResultObject(env, rc);
  673. }
  674. /*
  675. * Class: com_zerotier_sdk_Node
  676. * Method: processWirePacket
  677. * Signature: (JJJLjava/net/InetSocketAddress;[B[J)Lcom/zerotier/sdk/ResultCode;
  678. */
  679. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket(
  680. JNIEnv *env, jobject obj,
  681. jlong id,
  682. jlong in_now,
  683. jlong in_localSocket,
  684. jobject in_remoteAddress,
  685. jbyteArray in_packetData,
  686. jlongArray out_nextBackgroundTaskDeadline)
  687. {
  688. int64_t nodeId = (int64_t) id;
  689. ZT_Node *node = findNode(nodeId);
  690. unsigned int nbtd_len = (unsigned int)env->GetArrayLength(out_nextBackgroundTaskDeadline);
  691. if(nbtd_len < 1)
  692. {
  693. LOGE("nbtd_len < 1");
  694. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  695. }
  696. int64_t now = (int64_t)in_now;
  697. sockaddr_storage remoteAddress = fromSocketAddressObject(env, in_remoteAddress);
  698. if (env->ExceptionCheck() || isSocketAddressEmpty(remoteAddress)) {
  699. return NULL;
  700. }
  701. unsigned int packetLength = (unsigned int)env->GetArrayLength(in_packetData);
  702. if(packetLength == 0)
  703. {
  704. LOGE("Empty packet?!?");
  705. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  706. }
  707. void *packetData = env->GetPrimitiveArrayCritical(in_packetData, NULL);
  708. void *localData = malloc(packetLength);
  709. memcpy(localData, packetData, packetLength);
  710. env->ReleasePrimitiveArrayCritical(in_packetData, packetData, 0);
  711. int64_t nextBackgroundTaskDeadline = 0;
  712. ZT_ResultCode rc = ZT_Node_processWirePacket(
  713. node,
  714. NULL,
  715. now,
  716. in_localSocket,
  717. &remoteAddress,
  718. localData,
  719. packetLength,
  720. &nextBackgroundTaskDeadline);
  721. if(rc != ZT_RESULT_OK)
  722. {
  723. LOGE("ZT_Node_processWirePacket returned: %d", rc);
  724. }
  725. free(localData);
  726. jlong *outDeadline = (jlong*)env->GetPrimitiveArrayCritical(out_nextBackgroundTaskDeadline, NULL);
  727. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  728. env->ReleasePrimitiveArrayCritical(out_nextBackgroundTaskDeadline, outDeadline, 0);
  729. return createResultObject(env, rc);
  730. }
  731. /*
  732. * Class: com_zerotier_sdk_Node
  733. * Method: processBackgroundTasks
  734. * Signature: (JJ[J)Lcom/zerotier/sdk/ResultCode;
  735. */
  736. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processBackgroundTasks(
  737. JNIEnv *env, jobject obj,
  738. jlong id,
  739. jlong in_now,
  740. jlongArray out_nextBackgroundTaskDeadline)
  741. {
  742. int64_t nodeId = (int64_t) id;
  743. ZT_Node *node = findNode(nodeId);
  744. unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline);
  745. if(nbtd_len < 1)
  746. {
  747. return ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
  748. }
  749. int64_t now = (int64_t)in_now;
  750. int64_t nextBackgroundTaskDeadline = 0;
  751. ZT_ResultCode rc = ZT_Node_processBackgroundTasks(node, NULL, now, &nextBackgroundTaskDeadline);
  752. jlong *outDeadline = (jlong*)env->GetPrimitiveArrayCritical(out_nextBackgroundTaskDeadline, NULL);
  753. outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
  754. env->ReleasePrimitiveArrayCritical(out_nextBackgroundTaskDeadline, outDeadline, 0);
  755. return createResultObject(env, rc);
  756. }
  757. /*
  758. * Class: com_zerotier_sdk_Node
  759. * Method: join
  760. * Signature: (JJ)Lcom/zerotier/sdk/ResultCode;
  761. */
  762. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_join(
  763. JNIEnv *env, jobject obj, jlong id, jlong in_nwid)
  764. {
  765. int64_t nodeId = (int64_t) id;
  766. ZT_Node *node = findNode(nodeId);
  767. uint64_t nwid = (uint64_t)in_nwid;
  768. ZT_ResultCode rc = ZT_Node_join(node, nwid, NULL, NULL);
  769. return createResultObject(env, rc);
  770. }
  771. /*
  772. * Class: com_zerotier_sdk_Node
  773. * Method: leave
  774. * Signature: (JJ)Lcom/zerotier/sdk/ResultCode;
  775. */
  776. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_leave(
  777. JNIEnv *env, jobject obj, jlong id, jlong in_nwid)
  778. {
  779. int64_t nodeId = (int64_t) id;
  780. ZT_Node *node = findNode(nodeId);
  781. uint64_t nwid = (uint64_t)in_nwid;
  782. ZT_ResultCode rc = ZT_Node_leave(node, nwid, NULL, NULL);
  783. return createResultObject(env, rc);
  784. }
  785. /*
  786. * Class: com_zerotier_sdk_Node
  787. * Method: multicastSubscribe
  788. * Signature: (JJJJ)Lcom/zerotier/sdk/ResultCode;
  789. */
  790. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastSubscribe(
  791. JNIEnv *env, jobject obj,
  792. jlong id,
  793. jlong in_nwid,
  794. jlong in_multicastGroup,
  795. jlong in_multicastAdi)
  796. {
  797. int64_t nodeId = (int64_t) id;
  798. ZT_Node *node = findNode(nodeId);
  799. uint64_t nwid = (uint64_t)in_nwid;
  800. uint64_t multicastGroup = (uint64_t)in_multicastGroup;
  801. unsigned long multicastAdi = (unsigned long)in_multicastAdi;
  802. ZT_ResultCode rc = ZT_Node_multicastSubscribe(
  803. node, NULL, nwid, multicastGroup, multicastAdi);
  804. return createResultObject(env, rc);
  805. }
  806. /*
  807. * Class: com_zerotier_sdk_Node
  808. * Method: multicastUnsubscribe
  809. * Signature: (JJJJ)Lcom/zerotier/sdk/ResultCode;
  810. */
  811. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastUnsubscribe(
  812. JNIEnv *env, jobject obj,
  813. jlong id,
  814. jlong in_nwid,
  815. jlong in_multicastGroup,
  816. jlong in_multicastAdi)
  817. {
  818. int64_t nodeId = (int64_t) id;
  819. ZT_Node *node = findNode(nodeId);
  820. uint64_t nwid = (uint64_t)in_nwid;
  821. uint64_t multicastGroup = (uint64_t)in_multicastGroup;
  822. unsigned long multicastAdi = (unsigned long)in_multicastAdi;
  823. ZT_ResultCode rc = ZT_Node_multicastUnsubscribe(
  824. node, nwid, multicastGroup, multicastAdi);
  825. return createResultObject(env, rc);
  826. }
  827. /*
  828. * Class: com_zerotier_sdk_Node
  829. * Method: orbit
  830. * Signature: (JJJ)Lcom/zerotier/sdk/ResultCode;
  831. */
  832. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_orbit(
  833. JNIEnv *env, jobject obj,
  834. jlong id,
  835. jlong in_moonWorldId,
  836. jlong in_moonSeed)
  837. {
  838. int64_t nodeId = (int64_t)id;
  839. ZT_Node *node = findNode(nodeId);
  840. uint64_t moonWorldId = (uint64_t)in_moonWorldId;
  841. uint64_t moonSeed = (uint64_t)in_moonSeed;
  842. ZT_ResultCode rc = ZT_Node_orbit(node, NULL, moonWorldId, moonSeed);
  843. return createResultObject(env, rc);
  844. }
  845. /*
  846. * Class: com_zerotier_sdk_Node
  847. * Method: deorbit
  848. * Signature: (JJ)L/com/zerotier/sdk/ResultCode;
  849. */
  850. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_deorbit(
  851. JNIEnv *env, jobject obj,
  852. jlong id,
  853. jlong in_moonWorldId)
  854. {
  855. int64_t nodeId = (int64_t)id;
  856. ZT_Node *node = findNode(nodeId);
  857. uint64_t moonWorldId = (uint64_t)in_moonWorldId;
  858. ZT_ResultCode rc = ZT_Node_deorbit(node, NULL, moonWorldId);
  859. return createResultObject(env, rc);
  860. }
  861. /*
  862. * Class: com_zerotier_sdk_Node
  863. * Method: address
  864. * Signature: (J)J
  865. */
  866. JNIEXPORT jlong JNICALL Java_com_zerotier_sdk_Node_address(
  867. JNIEnv *env , jobject obj, jlong id)
  868. {
  869. int64_t nodeId = (int64_t) id;
  870. ZT_Node *node = findNode(nodeId);
  871. uint64_t address = ZT_Node_address(node);
  872. return (jlong)address;
  873. }
  874. /*
  875. * Class: com_zerotier_sdk_Node
  876. * Method: status
  877. * Signature: (J)Lcom/zerotier/sdk/NodeStatus;
  878. */
  879. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_status
  880. (JNIEnv *env, jobject obj, jlong id)
  881. {
  882. int64_t nodeId = (int64_t) id;
  883. ZT_Node *node = findNode(nodeId);
  884. ZT_NodeStatus nodeStatus;
  885. ZT_Node_status(node, &nodeStatus);
  886. return newNodeStatus(env, nodeStatus);
  887. }
  888. /*
  889. * Class: com_zerotier_sdk_Node
  890. * Method: networkConfig
  891. * Signature: (JJ)Lcom/zerotier/sdk/VirtualNetworkConfig;
  892. */
  893. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_networkConfig(
  894. JNIEnv *env, jobject obj, jlong id, jlong nwid)
  895. {
  896. int64_t nodeId = (int64_t) id;
  897. ZT_Node *node = findNode(nodeId);
  898. ZT_VirtualNetworkConfig *vnetConfig = ZT_Node_networkConfig(node, nwid);
  899. jobject vnetConfigObject = newNetworkConfig(env, *vnetConfig);
  900. ZT_Node_freeQueryResult(node, vnetConfig);
  901. return vnetConfigObject;
  902. }
  903. /*
  904. * Class: com_zerotier_sdk_Node
  905. * Method: version
  906. * Signature: ()Lcom/zerotier/sdk/Version;
  907. */
  908. JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_version(
  909. JNIEnv *env, jobject obj)
  910. {
  911. int major = 0;
  912. int minor = 0;
  913. int revision = 0;
  914. ZT_version(&major, &minor, &revision);
  915. return newVersion(env, major, minor, revision);
  916. }
  917. /*
  918. * Class: com_zerotier_sdk_Node
  919. * Method: peers
  920. * Signature: (J)[Lcom/zerotier/sdk/Peer;
  921. */
  922. JNIEXPORT jobjectArray JNICALL Java_com_zerotier_sdk_Node_peers(
  923. JNIEnv *env, jobject obj, jlong id)
  924. {
  925. int64_t nodeId = (int64_t) id;
  926. ZT_Node *node = findNode(nodeId);
  927. ZT_PeerList *peerList = ZT_Node_peers(node);
  928. if(peerList == NULL)
  929. {
  930. LOGE("ZT_Node_peers returned NULL");
  931. return NULL;
  932. }
  933. jobjectArray peerArrayObj = newPeerArray(env, peerList->peers, peerList->peerCount);
  934. ZT_Node_freeQueryResult(node, peerList);
  935. peerList = NULL;
  936. return peerArrayObj;
  937. }
  938. /*
  939. * Class: com_zerotier_sdk_Node
  940. * Method: networks
  941. * Signature: (J)[Lcom/zerotier/sdk/VirtualNetworkConfig;
  942. */
  943. JNIEXPORT jobjectArray JNICALL Java_com_zerotier_sdk_Node_networks(
  944. JNIEnv *env, jobject obj, jlong id)
  945. {
  946. int64_t nodeId = (int64_t) id;
  947. ZT_Node *node = findNode(nodeId);
  948. ZT_VirtualNetworkList *networkList = ZT_Node_networks(node);
  949. if(networkList == NULL)
  950. {
  951. return NULL;
  952. }
  953. jobjectArray networkListObject = newVirtualNetworkConfigArray(env, networkList->networks, networkList->networkCount);
  954. ZT_Node_freeQueryResult(node, networkList);
  955. return networkListObject;
  956. }
  957. #ifdef __cplusplus
  958. } // extern "C"
  959. #endif