RPC.hpp 5.2 KB

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