ClusterGeoIpService.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #ifndef ZT_CLUSTERGEOIPSERVICE_HPP
  28. #define ZT_CLUSTERGEOIPSERVICE_HPP
  29. #ifdef ZT_ENABLE_CLUSTER
  30. #include <vector>
  31. #include <map>
  32. #include <string>
  33. #include "../node/Constants.hpp"
  34. #include "../node/InetAddress.hpp"
  35. #include "../node/Mutex.hpp"
  36. #include "../osdep/Thread.hpp"
  37. namespace ZeroTier {
  38. /**
  39. * Runs the Cluster GeoIP service in the background and resolves geoIP queries
  40. */
  41. class ClusterGeoIpService
  42. {
  43. public:
  44. /**
  45. * @param pathToExe Path to cluster geo-resolution service executable
  46. */
  47. ClusterGeoIpService(const char *pathToExe);
  48. ~ClusterGeoIpService();
  49. /**
  50. * Attempt to locate an IP
  51. *
  52. * This returns true if x, y, and z are set. Otherwise it returns false
  53. * and a geo-locate job is ordered in the background. This usually takes
  54. * 500-1500ms to complete, after which time results will be available.
  55. * If false is returned the supplied coordinate variables are unchanged.
  56. *
  57. * @param ip IPv4 or IPv6 address
  58. * @param x Reference to variable to receive X
  59. * @param y Reference to variable to receive Y
  60. * @param z Reference to variable to receive Z
  61. * @return True if coordinates were set
  62. */
  63. bool locate(const InetAddress &ip,int &x,int &y,int &z);
  64. void threadMain()
  65. throw();
  66. private:
  67. const std::string _pathToExe;
  68. int _sOutputFd;
  69. int _sInputFd;
  70. volatile long _sPid;
  71. volatile bool _run;
  72. Thread _thread;
  73. Mutex _sOutputLock;
  74. struct _CE { uint64_t ts; int x,y,z; };
  75. std::map< InetAddress,_CE > _cache;
  76. Mutex _cache_m;
  77. };
  78. } // namespace ZeroTier
  79. #endif // ZT_ENABLE_CLUSTER
  80. #endif