ZT_jniutils.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  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. #ifndef ZT_jniutils_h_
  19. #define ZT_jniutils_h_
  20. #include <jni.h>
  21. #include <ZeroTierOne.h>
  22. #include <limits> // for numeric_limits
  23. #if defined(__ANDROID__)
  24. #include <android/log.h>
  25. #if !defined(NDEBUG)
  26. #define LOGV(...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
  27. #define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
  28. #else
  29. #define LOGV(...)
  30. #define LOGD(...)
  31. #endif
  32. #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
  33. #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
  34. #else
  35. #if !defined(NDEBUG)
  36. #define LOGV(...) fprintf(stdout, __VA_ARGS__)
  37. #define LOGD(...) fprintf(stdout, __VA_ARGS__)
  38. #else
  39. #define LOGV(...)
  40. #define LOGD(...)
  41. #endif
  42. #define LOGI(...) fprintf(stdout, __VA_ARGS__)
  43. #define LOGE(...) fprintf(stdout, __VA_ARGS__)
  44. #endif
  45. //
  46. // Call GetEnv and assert if there is an error
  47. //
  48. #define GETENV(env, vm) \
  49. do { \
  50. jint getEnvRet; \
  51. assert(vm); \
  52. if ((getEnvRet = vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6)) != JNI_OK) { \
  53. LOGE("Error calling GetEnv: %d", getEnvRet); \
  54. assert(false && "Error calling GetEnv"); \
  55. } \
  56. } while (false)
  57. //
  58. // Call GetJavaVM and assert if there is an error
  59. //
  60. #define GETJAVAVM(env, vm) \
  61. do { \
  62. jint getJavaVMRet; \
  63. if ((getJavaVMRet = env->GetJavaVM(&vm)) != 0) { \
  64. LOGE("Error calling GetJavaVM: %d", getJavaVMRet); \
  65. assert(false && "Error calling GetJavaVM"); \
  66. } \
  67. } while (false)
  68. const jsize JSIZE_MAX = std::numeric_limits<jsize>::max();
  69. jobject createResultObject(JNIEnv *env, ZT_ResultCode code);
  70. jobject createVirtualNetworkStatus(JNIEnv *env, ZT_VirtualNetworkStatus status);
  71. jobject createVirtualNetworkType(JNIEnv *env, ZT_VirtualNetworkType type);
  72. jobject createEvent(JNIEnv *env, ZT_Event event);
  73. jobject createPeerRole(JNIEnv *env, ZT_PeerRole role);
  74. jobject createVirtualNetworkConfigOperation(JNIEnv *env, ZT_VirtualNetworkConfigOperation op);
  75. jobject newInetSocketAddress(JNIEnv *env, const sockaddr_storage &addr);
  76. jobject newInetAddress(JNIEnv *env, const sockaddr_storage &addr);
  77. jobject newPeer(JNIEnv *env, const ZT_Peer &peer);
  78. jobject newPeerPhysicalPath(JNIEnv *env, const ZT_PeerPhysicalPath &ppp);
  79. jobject newNetworkConfig(JNIEnv *env, const ZT_VirtualNetworkConfig &config);
  80. jobject newVersion(JNIEnv *env, int major, int minor, int rev);
  81. jobject newVirtualNetworkRoute(JNIEnv *env, const ZT_VirtualNetworkRoute &route);
  82. jobject newVirtualNetworkDNS(JNIEnv *env, const ZT_VirtualNetworkDNS &dns);
  83. jobject newNodeStatus(JNIEnv *env, const ZT_NodeStatus &status);
  84. jobjectArray newPeerArray(JNIEnv *env, const ZT_Peer *peers, size_t count);
  85. jobjectArray newVirtualNetworkConfigArray(JNIEnv *env, const ZT_VirtualNetworkConfig *networks, size_t count);
  86. jobjectArray newPeerPhysicalPathArray(JNIEnv *env, const ZT_PeerPhysicalPath *paths, size_t count);
  87. jobjectArray newInetSocketAddressArray(JNIEnv *env, const sockaddr_storage *addresses, size_t count);
  88. jobjectArray newVirtualNetworkRouteArray(JNIEnv *env, const ZT_VirtualNetworkRoute *routes, size_t count);
  89. //
  90. // log functions only for newArrayObject below
  91. //
  92. void newArrayObject_logCount(size_t count);
  93. void newArrayObject_log(const char *msg);
  94. //
  95. // function template for creating array objects
  96. //
  97. template <typename T, jobject (*F)(JNIEnv *, const T &)>
  98. jobjectArray newArrayObject(JNIEnv *env, const T *buffer, size_t count, jclass clazz) {
  99. if (count > JSIZE_MAX) {
  100. newArrayObject_logCount(count);
  101. return NULL;
  102. }
  103. jsize jCount = static_cast<jsize>(count);
  104. jobjectArray arrayObj = env->NewObjectArray(jCount, clazz, NULL);
  105. if (env->ExceptionCheck() || arrayObj == NULL) {
  106. newArrayObject_log("Error creating array object");
  107. return NULL;
  108. }
  109. for (jsize i = 0; i < jCount; i++) {
  110. jobject obj = F(env, buffer[i]);
  111. if(env->ExceptionCheck() || obj == NULL) {
  112. return NULL;
  113. }
  114. env->SetObjectArrayElement(arrayObj, i, obj);
  115. if(env->ExceptionCheck()) {
  116. newArrayObject_log("Error assigning object to array");
  117. return NULL;
  118. }
  119. env->DeleteLocalRef(obj);
  120. }
  121. return arrayObj;
  122. }
  123. jbyteArray newByteArray(JNIEnv *env, const unsigned char *bytes, size_t count);
  124. jbyteArray newByteArray(JNIEnv *env, size_t count);
  125. #endif // ZT_jniutils_h_