ZT_jniutils.h 5.5 KB

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