ClusterGeoIpService.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 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. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_CLUSTERGEOIPSERVICE_HPP
  27. #define ZT_CLUSTERGEOIPSERVICE_HPP
  28. #ifdef ZT_ENABLE_CLUSTER
  29. #include <stdint.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <vector>
  34. #include <string>
  35. #include <algorithm>
  36. #include "../node/Constants.hpp"
  37. #include "../node/Mutex.hpp"
  38. #include "../node/NonCopyable.hpp"
  39. #include "../node/InetAddress.hpp"
  40. namespace ZeroTier {
  41. /**
  42. * Loads a GeoIP CSV into memory for fast lookup, reloading as needed
  43. *
  44. * This was designed around the CSV from https://db-ip.com but can be used
  45. * with any similar GeoIP CSV database that is presented in the form of an
  46. * IP range and lat/long coordinates.
  47. *
  48. * It loads the whole database into memory, which can be kind of large. If
  49. * the CSV file changes, the changes are loaded automatically.
  50. */
  51. class ClusterGeoIpService : NonCopyable
  52. {
  53. public:
  54. ClusterGeoIpService();
  55. ~ClusterGeoIpService();
  56. /**
  57. * Load or reload CSV file
  58. *
  59. * CSV column indexes start at zero. CSVs can be quoted with single or
  60. * double quotes. Whitespace before or after commas is ignored. Backslash
  61. * may be used for escaping whitespace as well.
  62. *
  63. * @param pathToCsv Path to (uncompressed) CSV file
  64. * @param ipStartColumn Column with IP range start
  65. * @param ipEndColumn Column with IP range end (inclusive)
  66. * @param latitudeColumn Column with latitude
  67. * @param longitudeColumn Column with longitude
  68. * @return Number of valid records loaded or -1 on error (invalid file, not found, etc.)
  69. */
  70. inline long load(const char *pathToCsv,int ipStartColumn,int ipEndColumn,int latitudeColumn,int longitudeColumn)
  71. {
  72. Mutex::Lock _l(_lock);
  73. return _load(pathToCsv,ipStartColumn,ipEndColumn,latitudeColumn,longitudeColumn);
  74. }
  75. /**
  76. * Attempt to locate an IP
  77. *
  78. * This returns true if x, y, and z are set. If the return value is false
  79. * the values of x, y, and z are undefined.
  80. *
  81. * @param ip IPv4 or IPv6 address
  82. * @param x Reference to variable to receive X
  83. * @param y Reference to variable to receive Y
  84. * @param z Reference to variable to receive Z
  85. * @return True if coordinates were set
  86. */
  87. bool locate(const InetAddress &ip,int &x,int &y,int &z);
  88. /**
  89. * @return True if IP database/service is available for queries (otherwise locate() will always be false)
  90. */
  91. inline bool available() const
  92. {
  93. Mutex::Lock _l(_lock);
  94. return ((_v4db.size() + _v6db.size()) > 0);
  95. }
  96. private:
  97. struct _V4E
  98. {
  99. uint32_t start;
  100. uint32_t end;
  101. float lat,lon;
  102. int16_t x,y,z;
  103. inline bool operator<(const _V4E &e) const { return (start < e.start); }
  104. };
  105. struct _V6E
  106. {
  107. uint8_t start[16];
  108. uint8_t end[16];
  109. float lat,lon;
  110. int16_t x,y,z;
  111. inline bool operator<(const _V6E &e) const { return (memcmp(start,e.start,16) < 0); }
  112. };
  113. static void _parseLine(const char *line,std::vector<_V4E> &v4db,std::vector<_V6E> &v6db,int ipStartColumn,int ipEndColumn,int latitudeColumn,int longitudeColumn);
  114. long _load(const char *pathToCsv,int ipStartColumn,int ipEndColumn,int latitudeColumn,int longitudeColumn);
  115. std::string _pathToCsv;
  116. int _ipStartColumn;
  117. int _ipEndColumn;
  118. int _latitudeColumn;
  119. int _longitudeColumn;
  120. uint64_t _lastFileCheckTime;
  121. uint64_t _csvModificationTime;
  122. int64_t _csvFileSize;
  123. std::vector<_V4E> _v4db;
  124. std::vector<_V6E> _v6db;
  125. Mutex _lock;
  126. };
  127. } // namespace ZeroTier
  128. #endif // ZT_ENABLE_CLUSTER
  129. #endif