2015-04-01 00:53:34 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
|
|
|
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
|
|
|
*
|
|
|
|
* 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_NODE_HPP
|
|
|
|
#define ZT_NODE_HPP
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "Constants.hpp"
|
|
|
|
|
|
|
|
#include "../include/ZeroTierOne.h"
|
|
|
|
|
|
|
|
#include "InetAddress.hpp"
|
|
|
|
#include "Mutex.hpp"
|
|
|
|
#include "MAC.hpp"
|
2015-04-01 23:27:14 +00:00
|
|
|
#include "Network.hpp"
|
2015-04-01 00:53:34 +00:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
class RuntimeEnvironment;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of Node object as defined in CAPI
|
|
|
|
*
|
|
|
|
* The pointer returned by ZT1_Node_new() is an instance of this class.
|
|
|
|
*/
|
|
|
|
class Node
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Node(
|
2015-04-02 02:09:18 +00:00
|
|
|
uint64_t now,
|
2015-04-03 20:14:37 +00:00
|
|
|
ZT1_DataStoreGetFunction dataStoreGetFunction,
|
|
|
|
ZT1_DataStorePutFunction dataStorePutFunction,
|
|
|
|
ZT1_WirePacketSendFunction wirePacketSendFunction,
|
|
|
|
ZT1_VirtualNetworkFrameFunction virtualNetworkFrameFunction,
|
2015-04-06 23:52:52 +00:00
|
|
|
ZT1_VirtualNetworkConfigFunction virtualNetworkConfigFunction,
|
2015-04-08 02:31:11 +00:00
|
|
|
ZT1_StatusCallback statusCallback,
|
|
|
|
const char *overrideRootTopology);
|
2015-04-01 00:53:34 +00:00
|
|
|
|
|
|
|
~Node();
|
|
|
|
|
|
|
|
// Public API Functions ----------------------------------------------------
|
|
|
|
|
2015-04-01 21:59:44 +00:00
|
|
|
ZT1_ResultCode processWirePacket(
|
2015-04-01 00:53:34 +00:00
|
|
|
uint64_t now,
|
2015-04-01 21:59:44 +00:00
|
|
|
const struct sockaddr_storage *remoteAddress,
|
2015-04-03 23:52:53 +00:00
|
|
|
unsigned int linkDesperation,
|
2015-04-01 21:59:44 +00:00
|
|
|
const void *packetData,
|
|
|
|
unsigned int packetLength,
|
2015-04-08 02:31:11 +00:00
|
|
|
uint64_t *nextBackgroundTaskDeadline);
|
2015-04-01 21:59:44 +00:00
|
|
|
ZT1_ResultCode processVirtualNetworkFrame(
|
|
|
|
uint64_t now,
|
|
|
|
uint64_t nwid,
|
|
|
|
uint64_t sourceMac,
|
|
|
|
uint64_t destMac,
|
|
|
|
unsigned int etherType,
|
|
|
|
unsigned int vlanId,
|
|
|
|
const void *frameData,
|
|
|
|
unsigned int frameLength,
|
2015-04-08 02:31:11 +00:00
|
|
|
uint64_t *nextBackgroundTaskDeadline);
|
|
|
|
ZT1_ResultCode processBackgroundTasks(uint64_t now,uint64_t *nextBackgroundTaskDeadline);
|
2015-04-01 00:53:34 +00:00
|
|
|
ZT1_ResultCode join(uint64_t nwid);
|
|
|
|
ZT1_ResultCode leave(uint64_t nwid);
|
2015-04-06 21:55:40 +00:00
|
|
|
ZT1_ResultCode multicastSubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
|
|
|
|
ZT1_ResultCode multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
|
2015-04-01 00:53:34 +00:00
|
|
|
void status(ZT1_NodeStatus *status);
|
|
|
|
ZT1_PeerList *peers();
|
2015-04-07 21:11:47 +00:00
|
|
|
ZT1_VirtualNetworkConfig *networkConfig(uint64_t nwid);
|
2015-04-07 01:27:24 +00:00
|
|
|
ZT1_VirtualNetworkList *networks();
|
2015-04-01 00:53:34 +00:00
|
|
|
void freeQueryResult(void *qr);
|
2015-04-01 01:17:11 +00:00
|
|
|
void setNetconfMaster(void *networkConfigMasterInstance);
|
2015-04-01 00:53:34 +00:00
|
|
|
|
|
|
|
// Internal functions ------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Time as of last call to run()
|
|
|
|
*/
|
|
|
|
inline uint64_t now() const throw() { return _now; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enqueue a ZeroTier message to be sent
|
|
|
|
*
|
|
|
|
* @param addr Destination address
|
|
|
|
* @param data Packet data
|
|
|
|
* @param len Packet length
|
2015-04-03 00:54:56 +00:00
|
|
|
* @param desperation Link desperation for reaching this address
|
|
|
|
* @return True if packet appears to have been sent
|
2015-04-01 00:53:34 +00:00
|
|
|
*/
|
2015-04-03 23:52:53 +00:00
|
|
|
inline bool putPacket(const InetAddress &addr,const void *data,unsigned int len,unsigned int desperation)
|
2015-04-01 00:53:34 +00:00
|
|
|
{
|
2015-04-06 23:52:52 +00:00
|
|
|
return (_wirePacketSendFunction(
|
|
|
|
reinterpret_cast<ZT1_Node *>(this),
|
|
|
|
reinterpret_cast<const struct sockaddr_storage *>(&addr),
|
|
|
|
desperation,
|
|
|
|
data,
|
|
|
|
len) == 0);
|
2015-04-01 00:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enqueue a frame to be injected into a tap device (port)
|
|
|
|
*
|
|
|
|
* @param nwid Network ID
|
|
|
|
* @param source Source MAC
|
|
|
|
* @param dest Destination MAC
|
|
|
|
* @param etherType 16-bit ethernet type
|
|
|
|
* @param vlanId VLAN ID or 0 if none
|
|
|
|
* @param data Frame data
|
|
|
|
* @param len Frame length
|
|
|
|
*/
|
|
|
|
inline void putFrame(uint64_t nwid,const MAC &source,const MAC &dest,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
|
|
|
|
{
|
2015-04-06 23:52:52 +00:00
|
|
|
_virtualNetworkFrameFunction(
|
|
|
|
reinterpret_cast<ZT1_Node *>(this),
|
|
|
|
nwid,
|
|
|
|
source.toInt(),
|
|
|
|
dest.toInt(),
|
|
|
|
etherType,
|
|
|
|
vlanId,
|
|
|
|
data,
|
|
|
|
len);
|
2015-04-01 00:53:34 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 23:27:14 +00:00
|
|
|
inline SharedPtr<Network> network(uint64_t nwid)
|
2015-04-01 00:53:34 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock _l(_networks_m);
|
2015-04-01 23:27:14 +00:00
|
|
|
std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid));
|
|
|
|
return ((nw == _networks.end()) ? SharedPtr<Network>() : nw->second);
|
2015-04-01 00:53:34 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 01:27:24 +00:00
|
|
|
inline std::vector< SharedPtr<Network> > allNetworks() const
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_networks_m);
|
|
|
|
std::vector< SharedPtr<Network> > nw;
|
|
|
|
for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
|
|
|
|
nw.push_back(n->second);
|
|
|
|
return nw;
|
|
|
|
}
|
|
|
|
|
2015-04-08 02:31:11 +00:00
|
|
|
inline unsigned int coreDesperation() const throw() { return _coreDesperation; }
|
|
|
|
|
2015-04-03 20:14:37 +00:00
|
|
|
inline bool dataStorePut(const char *name,const void *data,unsigned int len,bool secure) { return (_dataStorePutFunction(reinterpret_cast<ZT1_Node *>(this),name,data,len,(int)secure) == 0); }
|
2015-04-02 02:09:18 +00:00
|
|
|
inline bool dataStorePut(const char *name,const std::string &data,bool secure) { return dataStorePut(name,(const void *)data.data(),(unsigned int)data.length(),secure); }
|
2015-04-06 21:50:53 +00:00
|
|
|
inline void dataStoreDelete(const char *name) { _dataStorePutFunction(reinterpret_cast<ZT1_Node *>(this),name,(const void *)0,0,0); }
|
|
|
|
std::string dataStoreGet(const char *name);
|
2015-04-02 02:09:18 +00:00
|
|
|
|
2015-04-06 21:50:53 +00:00
|
|
|
inline void postEvent(ZT1_Event ev) { _statusCallback(reinterpret_cast<ZT1_Node *>(this),ev); }
|
2015-04-02 02:09:18 +00:00
|
|
|
|
2015-04-07 01:27:24 +00:00
|
|
|
inline int configureVirtualNetworkPort(uint64_t nwid,ZT1_VirtualNetworkConfigOperation op,const ZT1_VirtualNetworkConfig *nc) { return _virtualNetworkConfigFunction(reinterpret_cast<ZT1_Node *>(this),nwid,op,nc); }
|
2015-04-06 23:52:52 +00:00
|
|
|
|
2015-04-06 21:50:53 +00:00
|
|
|
void postNewerVersionIfNewer(unsigned int major,unsigned int minor,unsigned int rev);
|
2015-04-02 02:09:18 +00:00
|
|
|
|
2015-04-01 00:53:34 +00:00
|
|
|
private:
|
|
|
|
RuntimeEnvironment *RR;
|
|
|
|
|
2015-04-03 20:14:37 +00:00
|
|
|
ZT1_DataStoreGetFunction _dataStoreGetFunction;
|
|
|
|
ZT1_DataStorePutFunction _dataStorePutFunction;
|
|
|
|
ZT1_WirePacketSendFunction _wirePacketSendFunction;
|
|
|
|
ZT1_VirtualNetworkFrameFunction _virtualNetworkFrameFunction;
|
2015-04-06 23:52:52 +00:00
|
|
|
ZT1_VirtualNetworkConfigFunction _virtualNetworkConfigFunction;
|
2015-04-03 20:14:37 +00:00
|
|
|
ZT1_StatusCallback _statusCallback;
|
2015-04-01 00:53:34 +00:00
|
|
|
|
|
|
|
//Dictionary _localConfig; // persisted as local.conf
|
|
|
|
//Mutex _localConfig_m;
|
|
|
|
|
2015-04-01 23:27:14 +00:00
|
|
|
std::map< uint64_t,SharedPtr<Network> > _networks;
|
2015-04-01 00:53:34 +00:00
|
|
|
Mutex _networks_m;
|
|
|
|
|
2015-04-08 02:31:11 +00:00
|
|
|
Mutex _backgroundTasksLock;
|
|
|
|
|
|
|
|
uint64_t _now;
|
|
|
|
uint64_t _startTimeAfterInactivity;
|
|
|
|
uint64_t _lastPingCheck;
|
|
|
|
uint64_t _lastHousekeepingRun;
|
|
|
|
unsigned int _coreDesperation;
|
2015-04-06 21:50:53 +00:00
|
|
|
unsigned int _newestVersionSeen[3]; // major, minor, revision
|
2015-04-01 00:53:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|