ClusterGeoIpService.hpp 4.0 KB

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