ZT_jniutils.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. #include "ZT_jniutils.h"
  2. #include "ZT_jnilookup.h"
  3. #include <string>
  4. #include <assert.h>
  5. extern JniLookup lookup;
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. jobject createResultObject(JNIEnv *env, ZT_ResultCode code)
  10. {
  11. jclass resultClass = NULL;
  12. jobject resultObject = NULL;
  13. resultClass = lookup.findClass("com/zerotier/sdk/ResultCode");
  14. if(resultClass == NULL)
  15. {
  16. LOGE("Couldnt find ResultCode class");
  17. return NULL; // exception thrown
  18. }
  19. std::string fieldName;
  20. switch(code)
  21. {
  22. case ZT_RESULT_OK:
  23. LOGV("ZT_RESULT_OK");
  24. fieldName = "RESULT_OK";
  25. break;
  26. case ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY:
  27. LOGV("ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY");
  28. fieldName = "RESULT_FATAL_ERROR_OUT_OF_MEMORY";
  29. break;
  30. case ZT_RESULT_FATAL_ERROR_DATA_STORE_FAILED:
  31. LOGV("RESULT_FATAL_ERROR_DATA_STORE_FAILED");
  32. fieldName = "RESULT_FATAL_ERROR_DATA_STORE_FAILED";
  33. break;
  34. case ZT_RESULT_ERROR_NETWORK_NOT_FOUND:
  35. LOGV("RESULT_FATAL_ERROR_DATA_STORE_FAILED");
  36. fieldName = "RESULT_ERROR_NETWORK_NOT_FOUND";
  37. break;
  38. case ZT_RESULT_FATAL_ERROR_INTERNAL:
  39. default:
  40. LOGV("RESULT_FATAL_ERROR_DATA_STORE_FAILED");
  41. fieldName = "RESULT_FATAL_ERROR_INTERNAL";
  42. break;
  43. }
  44. jfieldID enumField = lookup.findStaticField(resultClass, fieldName.c_str(), "Lcom/zerotier/sdk/ResultCode;");
  45. if(env->ExceptionCheck() || enumField == NULL)
  46. {
  47. LOGE("Error on FindStaticField");
  48. return NULL;
  49. }
  50. resultObject = env->GetStaticObjectField(resultClass, enumField);
  51. if(env->ExceptionCheck() || resultObject == NULL)
  52. {
  53. LOGE("Error on GetStaticObjectField");
  54. }
  55. return resultObject;
  56. }
  57. jobject createVirtualNetworkStatus(JNIEnv *env, ZT_VirtualNetworkStatus status)
  58. {
  59. jobject statusObject = NULL;
  60. jclass statusClass = lookup.findClass("com/zerotier/sdk/VirtualNetworkStatus");
  61. if(statusClass == NULL)
  62. {
  63. return NULL; // exception thrown
  64. }
  65. std::string fieldName;
  66. switch(status)
  67. {
  68. case ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION:
  69. fieldName = "NETWORK_STATUS_REQUESTING_CONFIGURATION";
  70. break;
  71. case ZT_NETWORK_STATUS_OK:
  72. fieldName = "NETWORK_STATUS_OK";
  73. break;
  74. case ZT_NETWORK_STATUS_ACCESS_DENIED:
  75. fieldName = "NETWORK_STATUS_ACCESS_DENIED";
  76. break;
  77. case ZT_NETWORK_STATUS_NOT_FOUND:
  78. fieldName = "NETWORK_STATUS_NOT_FOUND";
  79. break;
  80. case ZT_NETWORK_STATUS_PORT_ERROR:
  81. fieldName = "NETWORK_STATUS_PORT_ERROR";
  82. break;
  83. case ZT_NETWORK_STATUS_CLIENT_TOO_OLD:
  84. fieldName = "NETWORK_STATUS_CLIENT_TOO_OLD";
  85. break;
  86. }
  87. jfieldID enumField = lookup.findStaticField(statusClass, fieldName.c_str(), "Lcom/zerotier/sdk/VirtualNetworkStatus;");
  88. statusObject = env->GetStaticObjectField(statusClass, enumField);
  89. return statusObject;
  90. }
  91. jobject createEvent(JNIEnv *env, ZT_Event event)
  92. {
  93. jclass eventClass = NULL;
  94. jobject eventObject = NULL;
  95. eventClass = lookup.findClass("com/zerotier/sdk/Event");
  96. if(eventClass == NULL)
  97. {
  98. return NULL;
  99. }
  100. std::string fieldName;
  101. switch(event)
  102. {
  103. case ZT_EVENT_UP:
  104. fieldName = "EVENT_UP";
  105. break;
  106. case ZT_EVENT_OFFLINE:
  107. fieldName = "EVENT_OFFLINE";
  108. break;
  109. case ZT_EVENT_ONLINE:
  110. fieldName = "EVENT_ONLINE";
  111. break;
  112. case ZT_EVENT_DOWN:
  113. fieldName = "EVENT_DOWN";
  114. break;
  115. case ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION:
  116. fieldName = "EVENT_FATAL_ERROR_IDENTITY_COLLISION";
  117. break;
  118. case ZT_EVENT_TRACE:
  119. fieldName = "EVENT_TRACE";
  120. break;
  121. }
  122. jfieldID enumField = lookup.findStaticField(eventClass, fieldName.c_str(), "Lcom/zerotier/sdk/Event;");
  123. eventObject = env->GetStaticObjectField(eventClass, enumField);
  124. return eventObject;
  125. }
  126. jobject createPeerRole(JNIEnv *env, ZT_PeerRole role)
  127. {
  128. jclass peerRoleClass = NULL;
  129. jobject peerRoleObject = NULL;
  130. peerRoleClass = lookup.findClass("com/zerotier/sdk/PeerRole");
  131. if(peerRoleClass == NULL)
  132. {
  133. return NULL;
  134. }
  135. std::string fieldName;
  136. switch(role)
  137. {
  138. case ZT_PEER_ROLE_LEAF:
  139. fieldName = "PEER_ROLE_LEAF";
  140. break;
  141. case ZT_PEER_ROLE_RELAY:
  142. fieldName = "PEER_ROLE_RELAY";
  143. break;
  144. case ZT_PEER_ROLE_ROOT:
  145. fieldName = "PEER_ROLE_ROOTS";
  146. break;
  147. }
  148. jfieldID enumField = lookup.findStaticField(peerRoleClass, fieldName.c_str(), "Lcom/zerotier/sdk/PeerRole;");
  149. peerRoleObject = env->GetStaticObjectField(peerRoleClass, enumField);
  150. return peerRoleObject;
  151. }
  152. jobject createVirtualNetworkType(JNIEnv *env, ZT_VirtualNetworkType type)
  153. {
  154. jclass vntypeClass = NULL;
  155. jobject vntypeObject = NULL;
  156. vntypeClass = lookup.findClass("com/zerotier/sdk/VirtualNetworkType");
  157. if(env->ExceptionCheck() || vntypeClass == NULL)
  158. {
  159. return NULL;
  160. }
  161. std::string fieldName;
  162. switch(type)
  163. {
  164. case ZT_NETWORK_TYPE_PRIVATE:
  165. fieldName = "NETWORK_TYPE_PRIVATE";
  166. break;
  167. case ZT_NETWORK_TYPE_PUBLIC:
  168. fieldName = "NETWORK_TYPE_PUBLIC";
  169. break;
  170. }
  171. jfieldID enumField = lookup.findStaticField(vntypeClass, fieldName.c_str(), "Lcom/zerotier/sdk/VirtualNetworkType;");
  172. vntypeObject = env->GetStaticObjectField(vntypeClass, enumField);
  173. return vntypeObject;
  174. }
  175. jobject createVirtualNetworkConfigOperation(JNIEnv *env, ZT_VirtualNetworkConfigOperation op)
  176. {
  177. jclass vnetConfigOpClass = NULL;
  178. jobject vnetConfigOpObject = NULL;
  179. vnetConfigOpClass = lookup.findClass("com/zerotier/sdk/VirtualNetworkConfigOperation");
  180. if(env->ExceptionCheck() || vnetConfigOpClass == NULL)
  181. {
  182. return NULL;
  183. }
  184. std::string fieldName;
  185. switch(op)
  186. {
  187. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP:
  188. fieldName = "VIRTUAL_NETWORK_CONFIG_OPERATION_UP";
  189. break;
  190. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE:
  191. fieldName = "VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE";
  192. break;
  193. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN:
  194. fieldName = "VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN";
  195. break;
  196. case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY:
  197. fieldName = "VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY";
  198. break;
  199. }
  200. jfieldID enumField = lookup.findStaticField(vnetConfigOpClass, fieldName.c_str(), "Lcom/zerotier/sdk/VirtualNetworkConfigOperation;");
  201. vnetConfigOpObject = env->GetStaticObjectField(vnetConfigOpClass, enumField);
  202. return vnetConfigOpObject;
  203. }
  204. jobject newInetAddress(JNIEnv *env, const sockaddr_storage &addr)
  205. {
  206. LOGV("newInetAddress");
  207. jclass inetAddressClass = NULL;
  208. jmethodID inetAddress_getByAddress = NULL;
  209. inetAddressClass = lookup.findClass("java/net/InetAddress");
  210. if(env->ExceptionCheck() || inetAddressClass == NULL)
  211. {
  212. LOGE("Error finding InetAddress class");
  213. return NULL;
  214. }
  215. inetAddress_getByAddress = lookup.findStaticMethod(
  216. inetAddressClass, "getByAddress", "([B)Ljava/net/InetAddress;");
  217. if(env->ExceptionCheck() || inetAddress_getByAddress == NULL)
  218. {
  219. LOGE("Erorr finding getByAddress() static method");
  220. return NULL;
  221. }
  222. jobject inetAddressObj = NULL;
  223. switch(addr.ss_family)
  224. {
  225. case AF_INET6:
  226. {
  227. sockaddr_in6 *ipv6 = (sockaddr_in6*)&addr;
  228. jbyteArray buff = env->NewByteArray(16);
  229. if(buff == NULL)
  230. {
  231. LOGE("Error creating IPV6 byte array");
  232. return NULL;
  233. }
  234. env->SetByteArrayRegion(buff, 0, 16, (jbyte*)ipv6->sin6_addr.s6_addr);
  235. inetAddressObj = env->CallStaticObjectMethod(
  236. inetAddressClass, inetAddress_getByAddress, buff);
  237. }
  238. break;
  239. case AF_INET:
  240. {
  241. sockaddr_in *ipv4 = (sockaddr_in*)&addr;
  242. jbyteArray buff = env->NewByteArray(4);
  243. if(buff == NULL)
  244. {
  245. LOGE("Error creating IPV4 byte array");
  246. return NULL;
  247. }
  248. env->SetByteArrayRegion(buff, 0, 4, (jbyte*)&ipv4->sin_addr);
  249. inetAddressObj = env->CallStaticObjectMethod(
  250. inetAddressClass, inetAddress_getByAddress, buff);
  251. }
  252. break;
  253. }
  254. if(env->ExceptionCheck() || inetAddressObj == NULL) {
  255. LOGE("Error creating InetAddress object");
  256. return NULL;
  257. }
  258. return inetAddressObj;
  259. }
  260. jobject newInetSocketAddress(JNIEnv *env, const sockaddr_storage &addr)
  261. {
  262. LOGV("newInetSocketAddress Called");
  263. jclass inetSocketAddressClass = NULL;
  264. jmethodID inetSocketAddress_constructor = NULL;
  265. inetSocketAddressClass = lookup.findClass("java/net/InetSocketAddress");
  266. if(env->ExceptionCheck() || inetSocketAddressClass == NULL)
  267. {
  268. LOGE("Error finding InetSocketAddress Class");
  269. return NULL;
  270. }
  271. jobject inetAddressObject = newInetAddress(env, addr);
  272. if(env->ExceptionCheck() || inetAddressObject == NULL)
  273. {
  274. LOGE("Error creating new inet address");
  275. return NULL;
  276. }
  277. inetSocketAddress_constructor = lookup.findMethod(
  278. inetSocketAddressClass, "<init>", "(Ljava/net/InetAddress;I)V");
  279. if(env->ExceptionCheck() || inetSocketAddress_constructor == NULL)
  280. {
  281. LOGE("Error finding InetSocketAddress constructor");
  282. return NULL;
  283. }
  284. int port = 0;
  285. switch(addr.ss_family)
  286. {
  287. case AF_INET6:
  288. {
  289. LOGV("IPV6 Address");
  290. sockaddr_in6 *ipv6 = (sockaddr_in6*)&addr;
  291. port = ntohs(ipv6->sin6_port);
  292. LOGV("Port %d", port);
  293. }
  294. break;
  295. case AF_INET:
  296. {
  297. LOGV("IPV4 Address");
  298. sockaddr_in *ipv4 = (sockaddr_in*)&addr;
  299. port = ntohs(ipv4->sin_port);
  300. LOGV("Port: %d", port);
  301. }
  302. break;
  303. default:
  304. {
  305. LOGE("ERROR: addr.ss_family is not set or unknown");
  306. break;
  307. }
  308. };
  309. jobject inetSocketAddressObject = env->NewObject(inetSocketAddressClass, inetSocketAddress_constructor, inetAddressObject, port);
  310. if(env->ExceptionCheck() || inetSocketAddressObject == NULL) {
  311. LOGE("Error creating InetSocketAddress object");
  312. }
  313. return inetSocketAddressObject;
  314. }
  315. jobject newMulticastGroup(JNIEnv *env, const ZT_MulticastGroup &mc)
  316. {
  317. jclass multicastGroupClass = NULL;
  318. jmethodID multicastGroup_constructor = NULL;
  319. jfieldID macField = NULL;
  320. jfieldID adiField = NULL;
  321. multicastGroupClass = lookup.findClass("com/zerotier/sdk/MulticastGroup");
  322. if(env->ExceptionCheck() || multicastGroupClass == NULL)
  323. {
  324. return NULL;
  325. }
  326. multicastGroup_constructor = lookup.findMethod(
  327. multicastGroupClass, "<init>", "()V");
  328. if(env->ExceptionCheck() || multicastGroup_constructor == NULL)
  329. {
  330. return NULL;
  331. }
  332. jobject multicastGroupObj = env->NewObject(multicastGroupClass, multicastGroup_constructor);
  333. if(env->ExceptionCheck() || multicastGroupObj == NULL)
  334. {
  335. return NULL;
  336. }
  337. macField = lookup.findField(multicastGroupClass, "mac", "J");
  338. if(env->ExceptionCheck() || macField == NULL)
  339. {
  340. return NULL;
  341. }
  342. adiField = lookup.findField(multicastGroupClass, "adi", "J");
  343. if(env->ExceptionCheck() || adiField == NULL)
  344. {
  345. return NULL;
  346. }
  347. env->SetLongField(multicastGroupObj, macField, mc.mac);
  348. env->SetLongField(multicastGroupObj, adiField, mc.adi);
  349. return multicastGroupObj;
  350. }
  351. jobject newPeerPhysicalPath(JNIEnv *env, const ZT_PeerPhysicalPath &ppp)
  352. {
  353. LOGV("newPeerPhysicalPath Called");
  354. jclass pppClass = NULL;
  355. jfieldID addressField = NULL;
  356. jfieldID lastSendField = NULL;
  357. jfieldID lastReceiveField = NULL;
  358. jfieldID activeField = NULL;
  359. jfieldID preferredField = NULL;
  360. jmethodID ppp_constructor = NULL;
  361. pppClass = lookup.findClass("com/zerotier/sdk/PeerPhysicalPath");
  362. if(env->ExceptionCheck() || pppClass == NULL)
  363. {
  364. LOGE("Error finding PeerPhysicalPath class");
  365. return NULL;
  366. }
  367. addressField = lookup.findField(pppClass, "address", "Ljava/net/InetSocketAddress;");
  368. if(env->ExceptionCheck() || addressField == NULL)
  369. {
  370. LOGE("Error finding address field");
  371. return NULL;
  372. }
  373. lastSendField = lookup.findField(pppClass, "lastSend", "J");
  374. if(env->ExceptionCheck() || lastSendField == NULL)
  375. {
  376. LOGE("Error finding lastSend field");
  377. return NULL;
  378. }
  379. lastReceiveField = lookup.findField(pppClass, "lastReceive", "J");
  380. if(env->ExceptionCheck() || lastReceiveField == NULL)
  381. {
  382. LOGE("Error finding lastReceive field");
  383. return NULL;
  384. }
  385. activeField = lookup.findField(pppClass, "active", "Z");
  386. if(env->ExceptionCheck() || activeField == NULL)
  387. {
  388. LOGE("Error finding active field");
  389. return NULL;
  390. }
  391. preferredField = lookup.findField(pppClass, "preferred", "Z");
  392. if(env->ExceptionCheck() || preferredField == NULL)
  393. {
  394. LOGE("Error finding preferred field");
  395. return NULL;
  396. }
  397. ppp_constructor = lookup.findMethod(pppClass, "<init>", "()V");
  398. if(env->ExceptionCheck() || ppp_constructor == NULL)
  399. {
  400. LOGE("Error finding PeerPhysicalPath constructor");
  401. return NULL;
  402. }
  403. jobject pppObject = env->NewObject(pppClass, ppp_constructor);
  404. if(env->ExceptionCheck() || pppObject == NULL)
  405. {
  406. LOGE("Error creating PPP object");
  407. return NULL; // out of memory
  408. }
  409. jobject addressObject = newInetSocketAddress(env, ppp.address);
  410. if(env->ExceptionCheck() || addressObject == NULL) {
  411. LOGE("Error creating InetSocketAddress object");
  412. return NULL;
  413. }
  414. env->SetObjectField(pppObject, addressField, addressObject);
  415. env->SetLongField(pppObject, lastSendField, ppp.lastSend);
  416. env->SetLongField(pppObject, lastReceiveField, ppp.lastReceive);
  417. env->SetBooleanField(pppObject, activeField, ppp.active);
  418. env->SetBooleanField(pppObject, preferredField, ppp.preferred);
  419. if(env->ExceptionCheck()) {
  420. LOGE("Exception assigning fields to PeerPhysicalPath object");
  421. }
  422. return pppObject;
  423. }
  424. jobject newPeer(JNIEnv *env, const ZT_Peer &peer)
  425. {
  426. LOGV("newPeer called");
  427. jclass peerClass = NULL;
  428. jfieldID addressField = NULL;
  429. jfieldID lastUnicastFrameField = NULL;
  430. jfieldID lastMulticastFrameField = NULL;
  431. jfieldID versionMajorField = NULL;
  432. jfieldID versionMinorField = NULL;
  433. jfieldID versionRevField = NULL;
  434. jfieldID latencyField = NULL;
  435. jfieldID roleField = NULL;
  436. jfieldID pathsField = NULL;
  437. jmethodID peer_constructor = NULL;
  438. peerClass = lookup.findClass("com/zerotier/sdk/Peer");
  439. if(env->ExceptionCheck() || peerClass == NULL)
  440. {
  441. LOGE("Error finding Peer class");
  442. return NULL;
  443. }
  444. addressField = lookup.findField(peerClass, "address", "J");
  445. if(env->ExceptionCheck() || addressField == NULL)
  446. {
  447. LOGE("Error finding address field of Peer object");
  448. return NULL;
  449. }
  450. lastUnicastFrameField = lookup.findField(peerClass, "lastUnicastFrame", "J");
  451. if(env->ExceptionCheck() || lastUnicastFrameField == NULL)
  452. {
  453. LOGE("Error finding lastUnicastFrame field of Peer object");
  454. return NULL;
  455. }
  456. lastMulticastFrameField = lookup.findField(peerClass, "lastMulticastFrame", "J");
  457. if(env->ExceptionCheck() || lastMulticastFrameField == NULL)
  458. {
  459. LOGE("Error finding lastMulticastFrame field of Peer object");
  460. return NULL;
  461. }
  462. versionMajorField = lookup.findField(peerClass, "versionMajor", "I");
  463. if(env->ExceptionCheck() || versionMajorField == NULL)
  464. {
  465. LOGE("Error finding versionMajor field of Peer object");
  466. return NULL;
  467. }
  468. versionMinorField = lookup.findField(peerClass, "versionMinor", "I");
  469. if(env->ExceptionCheck() || versionMinorField == NULL)
  470. {
  471. LOGE("Error finding versionMinor field of Peer object");
  472. return NULL;
  473. }
  474. versionRevField = lookup.findField(peerClass, "versionRev", "I");
  475. if(env->ExceptionCheck() || versionRevField == NULL)
  476. {
  477. LOGE("Error finding versionRev field of Peer object");
  478. return NULL;
  479. }
  480. latencyField = lookup.findField(peerClass, "latency", "I");
  481. if(env->ExceptionCheck() || latencyField == NULL)
  482. {
  483. LOGE("Error finding latency field of Peer object");
  484. return NULL;
  485. }
  486. roleField = lookup.findField(peerClass, "role", "Lcom/zerotier/sdk/PeerRole;");
  487. if(env->ExceptionCheck() || roleField == NULL)
  488. {
  489. LOGE("Error finding role field of Peer object");
  490. return NULL;
  491. }
  492. pathsField = lookup.findField(peerClass, "paths", "[Lcom/zerotier/sdk/PeerPhysicalPath;");
  493. if(env->ExceptionCheck() || pathsField == NULL)
  494. {
  495. LOGE("Error finding paths field of Peer object");
  496. return NULL;
  497. }
  498. peer_constructor = lookup.findMethod(peerClass, "<init>", "()V");
  499. if(env->ExceptionCheck() || peer_constructor == NULL)
  500. {
  501. LOGE("Error finding Peer constructor");
  502. return NULL;
  503. }
  504. jobject peerObject = env->NewObject(peerClass, peer_constructor);
  505. if(env->ExceptionCheck() || peerObject == NULL)
  506. {
  507. LOGE("Error creating Peer object");
  508. return NULL; // out of memory
  509. }
  510. env->SetLongField(peerObject, addressField, (jlong)peer.address);
  511. env->SetLongField(peerObject, lastUnicastFrameField, (jlong)peer.lastUnicastFrame);
  512. env->SetLongField(peerObject, lastMulticastFrameField, (jlong)peer.lastMulticastFrame);
  513. env->SetIntField(peerObject, versionMajorField, peer.versionMajor);
  514. env->SetIntField(peerObject, versionMinorField, peer.versionMinor);
  515. env->SetIntField(peerObject, versionRevField, peer.versionRev);
  516. env->SetIntField(peerObject, latencyField, peer.latency);
  517. env->SetObjectField(peerObject, roleField, createPeerRole(env, peer.role));
  518. jclass peerPhysicalPathClass = lookup.findClass("com/zerotier/sdk/PeerPhysicalPath");
  519. if(env->ExceptionCheck() || peerPhysicalPathClass == NULL)
  520. {
  521. LOGE("Error finding PeerPhysicalPath class");
  522. return NULL;
  523. }
  524. jobjectArray arrayObject = env->NewObjectArray(
  525. peer.pathCount, peerPhysicalPathClass, NULL);
  526. if(env->ExceptionCheck() || arrayObject == NULL)
  527. {
  528. LOGE("Error creating PeerPhysicalPath[] array");
  529. return NULL;
  530. }
  531. for(unsigned int i = 0; i < peer.pathCount; ++i)
  532. {
  533. jobject path = newPeerPhysicalPath(env, peer.paths[i]);
  534. env->SetObjectArrayElement(arrayObject, i, path);
  535. if(env->ExceptionCheck()) {
  536. LOGE("exception assigning PeerPhysicalPath to array");
  537. break;
  538. }
  539. }
  540. env->SetObjectField(peerObject, pathsField, arrayObject);
  541. return peerObject;
  542. }
  543. jobject newNetworkConfig(JNIEnv *env, const ZT_VirtualNetworkConfig &vnetConfig)
  544. {
  545. jclass vnetConfigClass = NULL;
  546. jmethodID vnetConfig_constructor = NULL;
  547. jfieldID nwidField = NULL;
  548. jfieldID macField = NULL;
  549. jfieldID nameField = NULL;
  550. jfieldID statusField = NULL;
  551. jfieldID typeField = NULL;
  552. jfieldID mtuField = NULL;
  553. jfieldID dhcpField = NULL;
  554. jfieldID bridgeField = NULL;
  555. jfieldID broadcastEnabledField = NULL;
  556. jfieldID portErrorField = NULL;
  557. jfieldID enabledField = NULL;
  558. jfieldID netconfRevisionField = NULL;
  559. jfieldID multicastSubscriptionsField = NULL;
  560. jfieldID assignedAddressesField = NULL;
  561. vnetConfigClass = lookup.findClass("com/zerotier/sdk/VirtualNetworkConfig");
  562. if(vnetConfigClass == NULL)
  563. {
  564. LOGE("Couldn't find com.zerotier.sdk.VirtualNetworkConfig");
  565. return NULL;
  566. }
  567. vnetConfig_constructor = lookup.findMethod(
  568. vnetConfigClass, "<init>", "()V");
  569. if(env->ExceptionCheck() || vnetConfig_constructor == NULL)
  570. {
  571. LOGE("Couldn't find VirtualNetworkConfig Constructor");
  572. return NULL;
  573. }
  574. jobject vnetConfigObj = env->NewObject(vnetConfigClass, vnetConfig_constructor);
  575. if(env->ExceptionCheck() || vnetConfigObj == NULL)
  576. {
  577. LOGE("Error creating new VirtualNetworkConfig object");
  578. return NULL;
  579. }
  580. nwidField = lookup.findField(vnetConfigClass, "nwid", "J");
  581. if(env->ExceptionCheck() || nwidField == NULL)
  582. {
  583. LOGE("Error getting nwid field");
  584. return NULL;
  585. }
  586. macField = lookup.findField(vnetConfigClass, "mac", "J");
  587. if(env->ExceptionCheck() || macField == NULL)
  588. {
  589. LOGE("Error getting mac field");
  590. return NULL;
  591. }
  592. nameField = lookup.findField(vnetConfigClass, "name", "Ljava/lang/String;");
  593. if(env->ExceptionCheck() || nameField == NULL)
  594. {
  595. LOGE("Error getting name field");
  596. return NULL;
  597. }
  598. statusField = lookup.findField(vnetConfigClass, "status", "Lcom/zerotier/sdk/VirtualNetworkStatus;");
  599. if(env->ExceptionCheck() || statusField == NULL)
  600. {
  601. LOGE("Error getting status field");
  602. return NULL;
  603. }
  604. typeField = lookup.findField(vnetConfigClass, "type", "Lcom/zerotier/sdk/VirtualNetworkType;");
  605. if(env->ExceptionCheck() || typeField == NULL)
  606. {
  607. LOGE("Error getting type field");
  608. return NULL;
  609. }
  610. mtuField = lookup.findField(vnetConfigClass, "mtu", "I");
  611. if(env->ExceptionCheck() || mtuField == NULL)
  612. {
  613. LOGE("Error getting mtu field");
  614. return NULL;
  615. }
  616. dhcpField = lookup.findField(vnetConfigClass, "dhcp", "Z");
  617. if(env->ExceptionCheck() || dhcpField == NULL)
  618. {
  619. LOGE("Error getting dhcp field");
  620. return NULL;
  621. }
  622. bridgeField = lookup.findField(vnetConfigClass, "bridge", "Z");
  623. if(env->ExceptionCheck() || bridgeField == NULL)
  624. {
  625. LOGE("Error getting bridge field");
  626. return NULL;
  627. }
  628. broadcastEnabledField = lookup.findField(vnetConfigClass, "broadcastEnabled", "Z");
  629. if(env->ExceptionCheck() || broadcastEnabledField == NULL)
  630. {
  631. LOGE("Error getting broadcastEnabled field");
  632. return NULL;
  633. }
  634. portErrorField = lookup.findField(vnetConfigClass, "portError", "I");
  635. if(env->ExceptionCheck() || portErrorField == NULL)
  636. {
  637. LOGE("Error getting portError field");
  638. return NULL;
  639. }
  640. enabledField = lookup.findField(vnetConfigClass, "enabled", "Z");
  641. if(env->ExceptionCheck() || enabledField == NULL)
  642. {
  643. LOGE("Error getting enabled field");
  644. return NULL;
  645. }
  646. netconfRevisionField = lookup.findField(vnetConfigClass, "netconfRevision", "J");
  647. if(env->ExceptionCheck() || netconfRevisionField == NULL)
  648. {
  649. LOGE("Error getting netconfRevision field");
  650. return NULL;
  651. }
  652. multicastSubscriptionsField = lookup.findField(vnetConfigClass, "multicastSubscriptions", "[Lcom/zerotier/sdk/MulticastGroup;");
  653. if(env->ExceptionCheck() || multicastSubscriptionsField == NULL)
  654. {
  655. LOGE("Error getting multicastSubscriptions field");
  656. return NULL;
  657. }
  658. assignedAddressesField = lookup.findField(vnetConfigClass, "assignedAddresses", "[Ljava/net/InetSocketAddress;");
  659. if(env->ExceptionCheck() || assignedAddressesField == NULL)
  660. {
  661. LOGE("Error getting assignedAddresses field");
  662. return NULL;
  663. }
  664. env->SetLongField(vnetConfigObj, nwidField, vnetConfig.nwid);
  665. env->SetLongField(vnetConfigObj, macField, vnetConfig.mac);
  666. jstring nameStr = env->NewStringUTF(vnetConfig.name);
  667. if(env->ExceptionCheck() || nameStr == NULL)
  668. {
  669. return NULL; // out of memory
  670. }
  671. env->SetObjectField(vnetConfigObj, nameField, nameStr);
  672. jobject statusObject = createVirtualNetworkStatus(env, vnetConfig.status);
  673. if(env->ExceptionCheck() || statusObject == NULL)
  674. {
  675. return NULL;
  676. }
  677. env->SetObjectField(vnetConfigObj, statusField, statusObject);
  678. jobject typeObject = createVirtualNetworkType(env, vnetConfig.type);
  679. if(env->ExceptionCheck() || typeObject == NULL)
  680. {
  681. return NULL;
  682. }
  683. env->SetObjectField(vnetConfigObj, typeField, typeObject);
  684. env->SetIntField(vnetConfigObj, mtuField, (int)vnetConfig.mtu);
  685. env->SetBooleanField(vnetConfigObj, dhcpField, vnetConfig.dhcp);
  686. env->SetBooleanField(vnetConfigObj, bridgeField, vnetConfig.bridge);
  687. env->SetBooleanField(vnetConfigObj, broadcastEnabledField, vnetConfig.broadcastEnabled);
  688. env->SetBooleanField(vnetConfigObj, enabledField, vnetConfig.enabled);
  689. env->SetIntField(vnetConfigObj, portErrorField, vnetConfig.portError);
  690. jclass multicastGroupClass = lookup.findClass("com/zerotier/sdk/MulticastGroup");
  691. if(env->ExceptionCheck() || multicastGroupClass == NULL)
  692. {
  693. LOGE("Error finding MulticastGroup class");
  694. return NULL;
  695. }
  696. jobjectArray mcastSubsArrayObj = env->NewObjectArray(
  697. vnetConfig.multicastSubscriptionCount, multicastGroupClass, NULL);
  698. if(env->ExceptionCheck() || mcastSubsArrayObj == NULL) {
  699. LOGE("Error creating MulticastGroup[] array");
  700. return NULL;
  701. }
  702. for(unsigned int i = 0; i < vnetConfig.multicastSubscriptionCount; ++i)
  703. {
  704. jobject mcastObj = newMulticastGroup(env, vnetConfig.multicastSubscriptions[i]);
  705. env->SetObjectArrayElement(mcastSubsArrayObj, i, mcastObj);
  706. if(env->ExceptionCheck())
  707. {
  708. LOGE("Error assigning MulticastGroup to array");
  709. }
  710. }
  711. env->SetObjectField(vnetConfigObj, multicastSubscriptionsField, mcastSubsArrayObj);
  712. jclass inetSocketAddressClass = lookup.findClass("java/net/InetSocketAddress");
  713. if(env->ExceptionCheck() || inetSocketAddressClass == NULL)
  714. {
  715. LOGE("Error finding InetSocketAddress class");
  716. return NULL;
  717. }
  718. jobjectArray assignedAddrArrayObj = env->NewObjectArray(
  719. vnetConfig.assignedAddressCount, inetSocketAddressClass, NULL);
  720. if(env->ExceptionCheck() || assignedAddrArrayObj == NULL)
  721. {
  722. LOGE("Error creating InetSocketAddress[] array");
  723. return NULL;
  724. }
  725. for(unsigned int i = 0; i < vnetConfig.assignedAddressCount; ++i)
  726. {
  727. jobject inetAddrObj = newInetSocketAddress(env, vnetConfig.assignedAddresses[i]);
  728. env->SetObjectArrayElement(assignedAddrArrayObj, i, inetAddrObj);
  729. if(env->ExceptionCheck())
  730. {
  731. LOGE("Error assigning InetSocketAddress to array");
  732. return NULL;
  733. }
  734. }
  735. env->SetObjectField(vnetConfigObj, assignedAddressesField, assignedAddrArrayObj);
  736. return vnetConfigObj;
  737. }
  738. jobject newVersion(JNIEnv *env, int major, int minor, int rev, long featureFlags)
  739. {
  740. // create a com.zerotier.sdk.Version object
  741. jclass versionClass = NULL;
  742. jmethodID versionConstructor = NULL;
  743. versionClass = lookup.findClass("com/zerotier/sdk/Version");
  744. if(env->ExceptionCheck() || versionClass == NULL)
  745. {
  746. return NULL;
  747. }
  748. versionConstructor = lookup.findMethod(
  749. versionClass, "<init>", "()V");
  750. if(env->ExceptionCheck() || versionConstructor == NULL)
  751. {
  752. return NULL;
  753. }
  754. jobject versionObj = env->NewObject(versionClass, versionConstructor);
  755. if(env->ExceptionCheck() || versionObj == NULL)
  756. {
  757. return NULL;
  758. }
  759. // copy data to Version object
  760. jfieldID majorField = NULL;
  761. jfieldID minorField = NULL;
  762. jfieldID revisionField = NULL;
  763. jfieldID featureFlagsField = NULL;
  764. majorField = lookup.findField(versionClass, "major", "I");
  765. if(env->ExceptionCheck() || majorField == NULL)
  766. {
  767. return NULL;
  768. }
  769. minorField = lookup.findField(versionClass, "minor", "I");
  770. if(env->ExceptionCheck() || minorField == NULL)
  771. {
  772. return NULL;
  773. }
  774. revisionField = lookup.findField(versionClass, "revision", "I");
  775. if(env->ExceptionCheck() || revisionField == NULL)
  776. {
  777. return NULL;
  778. }
  779. featureFlagsField = lookup.findField(versionClass, "featureFlags", "J");
  780. if(env->ExceptionCheck() || featureFlagsField == NULL)
  781. {
  782. return NULL;
  783. }
  784. env->SetIntField(versionObj, majorField, (jint)major);
  785. env->SetIntField(versionObj, minorField, (jint)minor);
  786. env->SetIntField(versionObj, revisionField, (jint)rev);
  787. env->SetLongField(versionObj, featureFlagsField, (jlong)featureFlags);
  788. return versionObj;
  789. }
  790. #ifdef __cplusplus
  791. }
  792. #endif