ClusterGeoIpService.cpp 4.9 KB

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