RPC.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 __WINDOWS__
  28. #include <dlfcn.h>
  29. #endif
  30. #include "Utils.hpp"
  31. #include "RuntimeEnvironment.hpp"
  32. #include "RPC.hpp"
  33. #include "Switch.hpp"
  34. #include "Topology.hpp"
  35. namespace ZeroTier {
  36. RPC::LocalService::LocalService(const char *dllPath)
  37. throw(std::invalid_argument) :
  38. _handle((void *)0),
  39. _init((void *)0),
  40. _do((void *)0),
  41. _free((void *)0),
  42. _destroy((void *)0)
  43. {
  44. _handle = dlopen(dllPath,RTLD_LAZY|RTLD_LOCAL);
  45. if (!_handle)
  46. throw std::invalid_argument("Unable to load DLL: dlopen() failed");
  47. _init = dlsym(_handle,"ZeroTierPluginInit");
  48. if (!_init) {
  49. dlclose(_handle);
  50. throw std::invalid_argument("Unable to resolve symbol ZeroTierPluginInit in DLL");
  51. }
  52. _do = dlsym(_handle,"ZeroTierPluginDo");
  53. if (!_do) {
  54. dlclose(_handle);
  55. throw std::invalid_argument("Unable to resolve symbol ZeroTierPluginDo in DLL");
  56. }
  57. _free = dlsym(_handle,"ZeroTierPluginFree");
  58. if (!_free) {
  59. dlclose(_handle);
  60. throw std::invalid_argument("Unable to resolve symbol ZeroTierPluginFree in DLL");
  61. }
  62. _destroy = dlsym(_handle,"ZeroTierPluginDestroy");
  63. if (!_destroy) {
  64. dlclose(_handle);
  65. throw std::invalid_argument("Unable to resolve symbol ZeroTierPluginDestroy in DLL");
  66. }
  67. if (((int (*)())_init)() < 0) {
  68. dlclose(_handle);
  69. throw std::invalid_argument("ZeroTierPluginInit() returned error");
  70. }
  71. }
  72. RPC::LocalService::~LocalService()
  73. {
  74. if (_handle) {
  75. if (_destroy)
  76. ((void (*)())_destroy)();
  77. dlclose(_handle);
  78. }
  79. }
  80. std::pair< int,std::vector<std::string> > RPC::LocalService::operator()(const std::vector<std::string> &args)
  81. {
  82. unsigned int alengths[4096];
  83. const void *argptrs[4096];
  84. const unsigned int *rlengths = (const unsigned int *)0;
  85. const void **resultptrs = (const void **)0;
  86. std::vector<std::string> results;
  87. if (args.size() > 4096)
  88. throw std::runtime_error("args[] too long");
  89. for(unsigned int i=0;i<args.size();++i) {
  90. alengths[i] = args[i].length();
  91. argptrs[i] = (const void *)args[i].data();
  92. }
  93. int rcount = ((int (*)(unsigned int,const unsigned int *,const void **,const unsigned int **,const void ***))_do)((unsigned int)args.size(),alengths,argptrs,&rlengths,&resultptrs);
  94. for(int i=0;i<rcount;++i)
  95. results.push_back(std::string((const char *)resultptrs[i],rlengths[i]));
  96. ((void (*)(int,const unsigned int *,const void **))_free)(rcount,rlengths,resultptrs);
  97. return std::pair< int,std::vector<std::string> >(rcount,results);
  98. }
  99. RPC::RPC(const RuntimeEnvironment *renv) :
  100. _r(renv)
  101. {
  102. }
  103. RPC::~RPC()
  104. {
  105. for(std::map<uint64_t,RemoteCallOutstanding>::iterator co(_remoteCallsOutstanding.begin());co!=_remoteCallsOutstanding.end();++co) {
  106. if (co->second.handler)
  107. co->second.handler(co->second.arg,co->first,co->second.peer,ZT_RPC_ERROR_CANCELLED,std::vector<std::string>());
  108. }
  109. for(std::map<std::string,LocalService *>::iterator s(_rpcServices.begin());s!=_rpcServices.end();++s)
  110. delete s->second;
  111. }
  112. std::pair< int,std::vector<std::string> > RPC::callLocal(const std::string &name,const std::vector<std::string> &args)
  113. {
  114. Mutex::Lock _l(_rpcServices_m);
  115. std::map<std::string,LocalService *>::iterator s(_rpcServices.find(name));
  116. if (s == _rpcServices.end())
  117. return std::pair< int,std::vector<std::string> >(ZT_RPC_ERROR_NOT_FOUND,std::vector<std::string>());
  118. return ((*(s->second))(args));
  119. }
  120. uint64_t RPC::callRemote(
  121. const Address &peer,
  122. const std::string &name,
  123. const std::vector<std::string> &args,
  124. void (*handler)(void *,uint64_t,const Address &,int,const std::vector<std::string> &),
  125. void *arg)
  126. throw(std::invalid_argument,std::out_of_range)
  127. {
  128. Packet outp(peer,_r->identity.address(),Packet::VERB_RPC);
  129. if (name.length() > 0xffff)
  130. throw std::invalid_argument("function name too long");
  131. outp.append((uint16_t)name.length());
  132. outp.append(name);
  133. for(std::vector<std::string>::const_iterator a(args.begin());a!=args.end();++a) {
  134. if (a->length() > 0xffff)
  135. throw std::invalid_argument("argument too long");
  136. outp.append((uint16_t)a->length());
  137. outp.append(*a);
  138. }
  139. outp.compress();
  140. uint64_t id = outp.packetId();
  141. {
  142. Mutex::Lock _l(_remoteCallsOutstanding_m);
  143. RemoteCallOutstanding &rc = _remoteCallsOutstanding[id];
  144. rc.callTime = Utils::now();
  145. rc.peer = peer;
  146. rc.handler = handler;
  147. rc.arg = arg;
  148. }
  149. _r->sw->send(outp,true);
  150. return id;
  151. }
  152. void RPC::clean()
  153. {
  154. Mutex::Lock _l(_remoteCallsOutstanding_m);
  155. uint64_t now = Utils::now();
  156. for(std::map<uint64_t,RemoteCallOutstanding>::iterator co(_remoteCallsOutstanding.begin());co!=_remoteCallsOutstanding.end();) {
  157. if ((now - co->second.callTime) >= ZT_RPC_TIMEOUT) {
  158. if (co->second.handler)
  159. co->second.handler(co->second.arg,co->first,co->second.peer,ZT_RPC_ERROR_EXPIRED_NO_RESPONSE,std::vector<std::string>());
  160. _remoteCallsOutstanding.erase(co++);
  161. } else ++co;
  162. }
  163. }
  164. } // namespace ZeroTier