ClusterGeoIpService.cpp 4.9 KB

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