ZeroTierOne/service/ClusterGeoIpService.hpp

144 lines
4.0 KiB
C++
Raw Normal View History

2015-10-22 17:18:05 +00:00
/*
* ZeroTier One - Network Virtualization Everywhere
2016-01-12 22:04:55 +00:00
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
2015-10-22 17:18:05 +00:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ZT_CLUSTERGEOIPSERVICE_HPP
#define ZT_CLUSTERGEOIPSERVICE_HPP
#ifdef ZT_ENABLE_CLUSTER
2016-04-18 17:21:38 +00:00
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
2015-10-22 17:18:05 +00:00
#include <vector>
#include <string>
2016-04-18 17:21:38 +00:00
#include <algorithm>
2015-10-22 17:18:05 +00:00
#include "../node/Constants.hpp"
#include "../node/Mutex.hpp"
#include "../node/NonCopyable.hpp"
#include "../node/InetAddress.hpp"
2015-10-22 17:18:05 +00:00
namespace ZeroTier {
/**
* Loads a GeoIP CSV into memory for fast lookup, reloading as needed
2016-04-18 17:21:38 +00:00
*
* This was designed around the CSV from https://db-ip.com but can be used
* with any similar GeoIP CSV database that is presented in the form of an
* IP range and lat/long coordinates.
*
* It loads the whole database into memory, which can be kind of large. If
* the CSV file changes, the changes are loaded automatically.
2015-10-22 17:18:05 +00:00
*/
class ClusterGeoIpService : NonCopyable
2015-10-22 17:18:05 +00:00
{
public:
2016-04-18 17:21:38 +00:00
ClusterGeoIpService();
~ClusterGeoIpService();
2015-10-22 17:18:05 +00:00
/**
2016-04-18 17:21:38 +00:00
* Load or reload CSV file
*
* CSV column indexes start at zero. CSVs can be quoted with single or
* double quotes. Whitespace before or after commas is ignored. Backslash
* may be used for escaping whitespace as well.
*
* @param pathToCsv Path to (uncompressed) CSV file
* @param ipStartColumn Column with IP range start
* @param ipEndColumn Column with IP range end (inclusive)
* @param latitudeColumn Column with latitude
* @param longitudeColumn Column with longitude
* @return Number of valid records loaded or -1 on error (invalid file, not found, etc.)
2015-10-22 17:18:05 +00:00
*/
2016-04-18 17:21:38 +00:00
inline long load(const char *pathToCsv,int ipStartColumn,int ipEndColumn,int latitudeColumn,int longitudeColumn)
{
Mutex::Lock _l(_lock);
return _load(pathToCsv,ipStartColumn,ipEndColumn,latitudeColumn,longitudeColumn);
}
2015-10-22 17:18:05 +00:00
/**
* Attempt to locate an IP
*
2016-04-18 17:21:38 +00:00
* This returns true if x, y, and z are set. If the return value is false
* the values of x, y, and z are undefined.
2015-10-22 17:18:05 +00:00
*
* @param ip IPv4 or IPv6 address
* @param x Reference to variable to receive X
* @param y Reference to variable to receive Y
* @param z Reference to variable to receive Z
* @return True if coordinates were set
*/
bool locate(const InetAddress &ip,int &x,int &y,int &z);
/**
* @return True if IP database/service is available for queries (otherwise locate() will always be false)
*/
inline bool available() const
{
Mutex::Lock _l(_lock);
return ((_v4db.size() + _v6db.size()) > 0);
}
2016-04-18 17:21:38 +00:00
private:
2016-04-18 17:21:38 +00:00
struct _V4E
{
uint32_t start;
uint32_t end;
float lat,lon;
int16_t x,y,z;
2016-04-18 17:21:38 +00:00
inline bool operator<(const _V4E &e) const { return (start < e.start); }
};
struct _V6E
{
uint8_t start[16];
uint8_t end[16];
float lat,lon;
int16_t x,y,z;
2016-04-18 17:21:38 +00:00
inline bool operator<(const _V6E &e) const { return (memcmp(start,e.start,16) < 0); }
};
static void _parseLine(const char *line,std::vector<_V4E> &v4db,std::vector<_V6E> &v6db,int ipStartColumn,int ipEndColumn,int latitudeColumn,int longitudeColumn);
long _load(const char *pathToCsv,int ipStartColumn,int ipEndColumn,int latitudeColumn,int longitudeColumn);
std::string _pathToCsv;
int _ipStartColumn;
int _ipEndColumn;
int _latitudeColumn;
int _longitudeColumn;
uint64_t _lastFileCheckTime;
uint64_t _csvModificationTime;
int64_t _csvFileSize;
2016-04-18 17:21:38 +00:00
std::vector<_V4E> _v4db;
std::vector<_V6E> _v6db;
Mutex _lock;
2015-10-22 17:18:05 +00:00
};
} // namespace ZeroTier
#endif // ZT_ENABLE_CLUSTER
#endif