ClusterGeoIpService.hpp 2.3 KB

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