2015-01-06 21:45:10 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Global Peer to Peer Ethernet
|
|
|
|
* Copyright (C) 2011-2015 ZeroTier Networks
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
|
|
|
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
*
|
|
|
|
* If you would like to embed ZeroTier into a commercial application or
|
|
|
|
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
|
|
|
* LLC. Start here: http://www.zerotier.com/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ZT_NETWORKCONFIGMASTER_HPP
|
|
|
|
#define ZT_NETWORKCONFIGMASTER_HPP
|
|
|
|
|
|
|
|
#include "Constants.hpp"
|
|
|
|
|
2015-01-09 23:25:41 +00:00
|
|
|
#define ZT_LOCAL_CONFIG_NETCONF_REDIS_HOST "netconf.redisHost"
|
|
|
|
#define ZT_LOCAL_CONFIG_NETCONF_REDIS_PORT "netconf.redisPort"
|
|
|
|
#define ZT_LOCAL_CONFIG_NETCONF_REDIS_PORT_DEFAULT 6379
|
|
|
|
#define ZT_LOCAL_CONFIG_NETCONF_REDIS_AUTH "netconf.redisAuth"
|
|
|
|
#define ZT_LOCAL_CONFIG_NETCONF_REDIS_DBNUM "netconf.redisDatabaseNumber"
|
|
|
|
#define ZT_LOCAL_CONFIG_NETCONF_REDIS_DBNUM_DEFAULT 0
|
|
|
|
|
2015-01-06 21:45:10 +00:00
|
|
|
#ifdef ZT_ENABLE_NETCONF_MASTER
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
2015-01-08 22:27:55 +00:00
|
|
|
#include <vector>
|
2015-01-06 21:45:10 +00:00
|
|
|
|
|
|
|
#include "Address.hpp"
|
|
|
|
#include "Dictionary.hpp"
|
|
|
|
#include "Mutex.hpp"
|
2015-01-07 01:16:54 +00:00
|
|
|
#include "InetAddress.hpp"
|
2015-01-06 21:45:10 +00:00
|
|
|
|
|
|
|
#include <hiredis/hiredis.h>
|
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
class RuntimeEnvironment;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Network configuration master -- responds to NETCONF requests
|
|
|
|
*
|
|
|
|
* This requires the 'hiredis' C library to build.
|
|
|
|
*/
|
|
|
|
class NetworkConfigMaster
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Create netconf master
|
|
|
|
*
|
|
|
|
* This doesn't connect to Redis until the first request is received.
|
|
|
|
*
|
|
|
|
* @param renv Runtime environment
|
|
|
|
* @param redisHost Hostname or IP of Redis server
|
|
|
|
* @param redisPort Redis IP port number
|
|
|
|
* @param redisPassword Redis AUTH password or NULL if none
|
|
|
|
* @param redisDatabaseNumber Redis database number (usually 0)
|
|
|
|
*/
|
|
|
|
NetworkConfigMaster(
|
|
|
|
const RuntimeEnvironment *renv,
|
|
|
|
const char *redisHost,
|
|
|
|
unsigned int redisPort,
|
|
|
|
const char *redisPassword,
|
|
|
|
unsigned int redisDatabaseNumber);
|
|
|
|
|
|
|
|
~NetworkConfigMaster();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a network config request, sending replies if necessary
|
|
|
|
*
|
|
|
|
* This is a blocking call, so rate is limited by Redis. It will fail
|
|
|
|
* and log its failure if the Redis server is not available or times out.
|
|
|
|
*
|
2015-01-07 01:16:54 +00:00
|
|
|
* @param fromAddr Originating IP address
|
2015-01-06 21:45:10 +00:00
|
|
|
* @param packetId 64-bit packet ID
|
2015-01-07 01:16:54 +00:00
|
|
|
* @param member Originating peer ZeroTier address
|
2015-01-06 21:45:10 +00:00
|
|
|
* @param nwid 64-bit network ID
|
|
|
|
* @param metaData Meta-data bundled with request (empty if none)
|
|
|
|
* @param haveTimestamp Timestamp requesting peer has or 0 if none or not included
|
|
|
|
*/
|
|
|
|
void doNetworkConfigRequest(
|
2015-01-07 01:16:54 +00:00
|
|
|
const InetAddress &fromAddr,
|
2015-01-06 21:45:10 +00:00
|
|
|
uint64_t packetId,
|
2015-01-07 01:16:54 +00:00
|
|
|
const Address &member,
|
2015-01-06 21:45:10 +00:00
|
|
|
uint64_t nwid,
|
|
|
|
const Dictionary &metaData,
|
|
|
|
uint64_t haveTimestamp);
|
|
|
|
|
|
|
|
private:
|
|
|
|
// These assume _lock is locked
|
|
|
|
bool _reconnect();
|
2015-01-07 01:16:54 +00:00
|
|
|
bool _hgetall(const char *key,Dictionary &hdata);
|
|
|
|
bool _hmset(const char *key,const Dictionary &hdata);
|
2015-01-06 21:45:10 +00:00
|
|
|
bool _hget(const char *key,const char *hashKey,std::string &value);
|
|
|
|
bool _hset(const char *key,const char *hashKey,const char *value);
|
2015-01-08 22:27:55 +00:00
|
|
|
bool _get(const char *key,std::string &value);
|
|
|
|
bool _smembers(const char *key,std::vector<std::string> &sdata);
|
2015-01-06 21:45:10 +00:00
|
|
|
|
2015-01-07 01:16:54 +00:00
|
|
|
bool _initNewMember(uint64_t nwid,const Address &member,const Dictionary &metaData,Dictionary &memberRecord);
|
|
|
|
bool _generateNetconf(uint64_t nwid,const Address &member,const Dictionary &metaData,std::string &netconf,uint64_t &ts);
|
|
|
|
|
2015-01-06 21:45:10 +00:00
|
|
|
Mutex _lock;
|
|
|
|
|
|
|
|
std::string _redisHost;
|
|
|
|
std::string _redisPassword;
|
|
|
|
unsigned int _redisPort;
|
|
|
|
unsigned int _redisDatabaseNumber;
|
|
|
|
|
|
|
|
const RuntimeEnvironment *RR;
|
|
|
|
redisContext *_rc;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif // ZT_ENABLE_NETCONF_MASTER
|
|
|
|
|
|
|
|
#endif
|