ClusterGeoIpService.cpp 4.6 KB

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