RPC.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  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. #ifndef _ZT_RPC_HPP
  28. #define _ZT_RPC_HPP
  29. #include <stdint.h>
  30. #include <stdexcept>
  31. #include <map>
  32. #include <vector>
  33. #include <utility>
  34. #include "Constants.hpp"
  35. #include "NonCopyable.hpp"
  36. #include "Mutex.hpp"
  37. #include "Address.hpp"
  38. namespace ZeroTier {
  39. class RuntimeEnvironment;
  40. /**
  41. * Peer or method not found
  42. */
  43. #define ZT_RPC_ERROR_NOT_FOUND -1
  44. /**
  45. * A runtime error occurred
  46. */
  47. #define ZT_RPC_ERROR_RUNTIME -2
  48. /**
  49. * Call was expired without response from target
  50. */
  51. #define ZT_RPC_ERROR_EXPIRED_NO_RESPONSE -3
  52. /**
  53. * Call was cancelled (or RPC is shutting down)
  54. */
  55. #define ZT_RPC_ERROR_CANCELLED -4
  56. /**
  57. * RPC request and result handler
  58. */
  59. class RPC : NonCopyable
  60. {
  61. public:
  62. #ifndef __WINDOWS__
  63. /**
  64. * A local service accessible by RPC, non-Windows only for now
  65. *
  66. * Each service DLL must export these functions:
  67. *
  68. * int ZeroTierPluginInit();
  69. * int ZeroTierPluginDo(unsigned int,const unsigned int *,const void **,const unsigned int **,const void ***);
  70. * void ZeroTierPluginFree(int,const unsigned int *,const void **);
  71. * void ZeroTierPluginDestroy();
  72. *
  73. * Init is called on library load, Destroy on unload. Do() may
  74. * be called from multiple threads concurrently, so any locking
  75. * is the responsibility of the library. These must have C
  76. * function signatures (extern "C" in C++).
  77. *
  78. * Do's arguments are: the number of paramters, the size of each parameter in bytes,
  79. * and each parameter's contents. The last two arguments are result parameters. The
  80. * first result parameter must be set to an array of integers describing the size of
  81. * each result. The second is set to an array of pointers to actual results. The number
  82. * of results (size of both arrays) is returned. If Do() returns zero or negative,
  83. * these result paremeters are not used by the caller and don't need to be set.
  84. *
  85. * After the caller is done with Do()'s results, it calls ZeroTierPluginFree() to
  86. * free them. This may also be called concurrently. Free() takes the number of
  87. * results, the array of result sizes, and the result array.
  88. */
  89. class LocalService : NonCopyable
  90. {
  91. public:
  92. /**
  93. * @param dllPath Path to DLL/shared object
  94. * @throws std::invalid_argument Unable to properly load or resolve symbol(s) in DLL
  95. */
  96. LocalService(const char *dllPath)
  97. throw(std::invalid_argument);
  98. ~LocalService();
  99. /**
  100. * Call the DLL, return result
  101. *
  102. * @param args Input arguments
  103. * @return Results from DLL
  104. * @throws std::runtime_error Error calling DLL
  105. */
  106. std::pair< int,std::vector<std::string> > operator()(const std::vector<std::string> &args);
  107. private:
  108. void *_handle;
  109. void *_init;
  110. void *_do;
  111. void *_free;
  112. void *_destroy;
  113. };
  114. #endif
  115. RPC(const RuntimeEnvironment *renv);
  116. ~RPC();
  117. /**
  118. * Used by PacketDecoder to call local RPC methods
  119. *
  120. * @param name Name of locally loaded method to call
  121. * @param args Arguments to method
  122. * @return Return value of method, and results (negative first item and empty vector means error)
  123. */
  124. std::pair< int,std::vector<std::string> > callLocal(const std::string &name,const std::vector<std::string> &args);
  125. /**
  126. * Load a plugin
  127. *
  128. * @param name Name of RPC function
  129. * @param path Path to plugin DLL
  130. * @throws std::invalid_argument Unable to properly load or resolve symbol(s) in DLL
  131. */
  132. void loadLocal(const char *name,const char *path)
  133. throw(std::invalid_argument);
  134. /**
  135. * Call a remote service
  136. *
  137. * @param peer Peer to call on
  138. * @param name Name of remote function
  139. * @param args Arguments to remote function
  140. * @param handler Handler to call on result
  141. * @param arg First argument to handler
  142. * @return Call ID (packet ID of sent packet)
  143. */
  144. uint64_t callRemote(
  145. const Address &peer,
  146. const std::string &name,
  147. const std::vector<std::string> &args,
  148. void (*handler)(void *,uint64_t,const Address &,int,const std::vector<std::string> &),
  149. void *arg)
  150. throw(std::invalid_argument,std::out_of_range);
  151. /**
  152. * Periodically called to clean up, such as by expiring remote calls
  153. */
  154. void clean();
  155. private:
  156. const RuntimeEnvironment *_r;
  157. #ifndef __WINDOWS__
  158. std::map<std::string,LocalService *> _rpcServices;
  159. Mutex _rpcServices_m;
  160. #endif
  161. struct RemoteCallOutstanding
  162. {
  163. uint64_t callTime;
  164. Address peer;
  165. void (*handler)(void *,uint64_t,const Address &,int,const std::vector<std::string> &);
  166. void *arg;
  167. };
  168. std::map<uint64_t,RemoteCallOutstanding> _remoteCallsOutstanding;
  169. Mutex _remoteCallsOutstanding_m;
  170. };
  171. } // namespace ZeroTier
  172. #endif