ClusterGeoIpService.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. #ifdef ZT_ENABLE_CLUSTER
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdint.h>
  32. #include <unistd.h>
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <signal.h>
  36. #include <errno.h>
  37. #include "ClusterGeoIpService.hpp"
  38. #include "../node/Utils.hpp"
  39. #include "../osdep/OSUtils.hpp"
  40. #define ZT_CLUSTERGEOIPSERVICE_INTERNAL_CACHE_TTL (60 * 60 * 1000)
  41. namespace ZeroTier {
  42. ClusterGeoIpService::ClusterGeoIpService(const char *pathToExe) :
  43. _pathToExe(pathToExe),
  44. _sOutputFd(-1),
  45. _sInputFd(-1),
  46. _sPid(0),
  47. _run(true)
  48. {
  49. _thread = Thread::start(this);
  50. }
  51. ClusterGeoIpService::~ClusterGeoIpService()
  52. {
  53. _run = false;
  54. long p = _sPid;
  55. if (p > 0) {
  56. ::kill(p,SIGTERM);
  57. Thread::sleep(500);
  58. ::kill(p,SIGKILL);
  59. }
  60. Thread::join(_thread);
  61. }
  62. bool ClusterGeoIpService::locate(const InetAddress &ip,int &x,int &y,int &z)
  63. {
  64. const uint64_t now = OSUtils::now();
  65. bool r = false;
  66. {
  67. Mutex::Lock _l(_cache_m);
  68. std::map< InetAddress,_CE >::iterator c(_cache.find(ip));
  69. if (c != _cache.end()) {
  70. x = c->second.x;
  71. y = c->second.y;
  72. z = c->second.z;
  73. if ((now - c->second.ts) < ZT_CLUSTERGEOIPSERVICE_INTERNAL_CACHE_TTL)
  74. return true;
  75. else r = true; // return true but refresh as well
  76. }
  77. }
  78. {
  79. Mutex::Lock _l(_sOutputLock);
  80. if (_sOutputFd >= 0) {
  81. std::string ips(ip.toIpString());
  82. ips.push_back('\n');
  83. ::write(_sOutputFd,ips.data(),ips.length());
  84. }
  85. }
  86. return r;
  87. }
  88. void ClusterGeoIpService::threadMain()
  89. throw()
  90. {
  91. char linebuf[65536];
  92. char buf[65536];
  93. long n,lineptr;
  94. while (_run) {
  95. {
  96. Mutex::Lock _l(_sOutputLock);
  97. _sOutputFd = -1;
  98. _sInputFd = -1;
  99. _sPid = 0;
  100. int stdinfds[2] = { 0,0 }; // sub-process's stdin, our output
  101. int stdoutfds[2] = { 0,0 }; // sub-process's stdout, our input
  102. ::pipe(stdinfds);
  103. ::pipe(stdoutfds);
  104. long p = (long)::vfork();
  105. if (p < 0) {
  106. Thread::sleep(500);
  107. continue;
  108. } else if (p == 0) {
  109. ::close(stdinfds[1]);
  110. ::close(stdoutfds[0]);
  111. ::dup2(stdinfds[0],STDIN_FILENO);
  112. ::dup2(stdoutfds[1],STDOUT_FILENO);
  113. ::execl(_pathToExe.c_str(),_pathToExe.c_str(),(const char *)0);
  114. ::exit(1);
  115. } else {
  116. ::close(stdinfds[0]);
  117. ::close(stdoutfds[1]);
  118. _sOutputFd = stdinfds[1];
  119. _sInputFd = stdoutfds[0];
  120. _sPid = p;
  121. }
  122. }
  123. lineptr = 0;
  124. while (_run) {
  125. n = ::read(_sInputFd,buf,sizeof(buf));
  126. if (n <= 0) {
  127. if (errno == EINTR)
  128. continue;
  129. else break;
  130. }
  131. for(long i=0;i<n;++i) {
  132. if (lineptr > sizeof(linebuf))
  133. lineptr = 0;
  134. if ((buf[i] == '\n')||(buf[i] == '\r')) {
  135. linebuf[lineptr] = (char)0;
  136. if (lineptr > 0) {
  137. try {
  138. std::vector<std::string> result(Utils::split(linebuf,",","",""));
  139. if ((result.size() >= 7)&&(result[1] == "1")) {
  140. InetAddress rip(result[0],0);
  141. if ((rip.ss_family == AF_INET)||(rip.ss_family == AF_INET6)) {
  142. _CE ce;
  143. ce.ts = OSUtils::now();
  144. ce.x = (int)::strtol(result[4].c_str(),(char **)0,10);
  145. ce.y = (int)::strtol(result[5].c_str(),(char **)0,10);
  146. ce.z = (int)::strtol(result[6].c_str(),(char **)0,10);
  147. {
  148. Mutex::Lock _l2(_cache_m);
  149. _cache[rip] = ce;
  150. }
  151. }
  152. }
  153. } catch ( ... ) {}
  154. }
  155. lineptr = 0;
  156. } else linebuf[lineptr++] = buf[i];
  157. }
  158. }
  159. ::close(_sOutputFd);
  160. ::close(_sInputFd);
  161. ::kill(_sPid,SIGTERM);
  162. Thread::sleep(250);
  163. ::kill(_sPid,SIGKILL);
  164. ::waitpid(_sPid,(int *)0,0);
  165. }
  166. }
  167. } // namespace ZeroTier
  168. #endif // ZT_ENABLE_CLUSTER