| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 | 
							- /*
 
-  * 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 "RuntimeEnvironment.hpp"
 
- #include "InetAddress.hpp"
 
- #include "Mutex.hpp"
 
- #include "MAC.hpp"
 
- #include "Network.hpp"
 
- #include "Path.hpp"
 
- #include "Salsa20.hpp"
 
- #undef TRACE
 
- #ifdef ZT_TRACE
 
- #define TRACE(f,...) RR->node->postTrace(__FILE__,__LINE__,f,##__VA_ARGS__)
 
- #else
 
- #define TRACE(f,...) {}
 
- #endif
 
- namespace ZeroTier {
 
- /**
 
-  * 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(
 
- 		uint64_t now,
 
- 		void *uptr,
 
- 		ZT1_DataStoreGetFunction dataStoreGetFunction,
 
- 		ZT1_DataStorePutFunction dataStorePutFunction,
 
- 		ZT1_WirePacketSendFunction wirePacketSendFunction,
 
- 		ZT1_VirtualNetworkFrameFunction virtualNetworkFrameFunction,
 
- 		ZT1_VirtualNetworkConfigFunction virtualNetworkConfigFunction,
 
- 		ZT1_EventCallback eventCallback,
 
- 		const char *overrideRootTopology);
 
- 	~Node();
 
- 	// Public API Functions ----------------------------------------------------
 
- 	ZT1_ResultCode processWirePacket(
 
- 		uint64_t now,
 
- 		const struct sockaddr_storage *remoteAddress,
 
- 		const void *packetData,
 
- 		unsigned int packetLength,
 
- 		volatile uint64_t *nextBackgroundTaskDeadline);
 
- 	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,
 
- 		volatile uint64_t *nextBackgroundTaskDeadline);
 
- 	ZT1_ResultCode processBackgroundTasks(uint64_t now,volatile uint64_t *nextBackgroundTaskDeadline);
 
- 	ZT1_ResultCode join(uint64_t nwid);
 
- 	ZT1_ResultCode leave(uint64_t nwid);
 
- 	ZT1_ResultCode multicastSubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
 
- 	ZT1_ResultCode multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
 
- 	uint64_t address() const;
 
- 	void status(ZT1_NodeStatus *status) const;
 
- 	ZT1_PeerList *peers() const;
 
- 	ZT1_VirtualNetworkConfig *networkConfig(uint64_t nwid) const;
 
- 	ZT1_VirtualNetworkList *networks() const;
 
- 	void freeQueryResult(void *qr);
 
- 	int addLocalInterfaceAddress(const struct sockaddr_storage *addr,int metric,ZT1_LocalInterfaceAddressTrust trust);
 
- 	void clearLocalInterfaceAddresses();
 
- 	void setNetconfMaster(void *networkControllerInstance);
 
- 	// 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
 
- 	 * @return True if packet appears to have been sent
 
- 	 */
 
- 	inline bool putPacket(const InetAddress &addr,const void *data,unsigned int len)
 
- 	{
 
- 		return (_wirePacketSendFunction(
 
- 			reinterpret_cast<ZT1_Node *>(this),
 
- 			_uPtr,
 
- 			reinterpret_cast<const struct sockaddr_storage *>(&addr),
 
- 			data,
 
- 			len) == 0);
 
- 	}
 
- 	/**
 
- 	 * 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)
 
- 	{
 
- 		_virtualNetworkFrameFunction(
 
- 			reinterpret_cast<ZT1_Node *>(this),
 
- 			_uPtr,
 
- 			nwid,
 
- 			source.toInt(),
 
- 			dest.toInt(),
 
- 			etherType,
 
- 			vlanId,
 
- 			data,
 
- 			len);
 
- 	}
 
- 	inline SharedPtr<Network> network(uint64_t nwid) const
 
- 	{
 
- 		Mutex::Lock _l(_networks_m);
 
- 		return _network(nwid);
 
- 	}
 
- 	inline std::vector< SharedPtr<Network> > allNetworks() const
 
- 	{
 
- 		std::vector< SharedPtr<Network> > nw;
 
- 		Mutex::Lock _l(_networks_m);
 
- 		nw.reserve(_networks.size());
 
- 		for(std::vector< std::pair< uint64_t, SharedPtr<Network> > >::const_iterator i=_networks.begin();i!=_networks.end();++i)
 
- 			nw.push_back(i->second);
 
- 		return nw;
 
- 	}
 
- 	/**
 
- 	 * @return Potential direct paths to me a.k.a. local interface addresses
 
- 	 */
 
- 	inline std::vector<Path> directPaths() const
 
- 	{
 
- 		Mutex::Lock _l(_directPaths_m);
 
- 		return _directPaths;
 
- 	}
 
- 	inline bool dataStorePut(const char *name,const void *data,unsigned int len,bool secure) { return (_dataStorePutFunction(reinterpret_cast<ZT1_Node *>(this),_uPtr,name,data,len,(int)secure) == 0); }
 
- 	inline bool dataStorePut(const char *name,const std::string &data,bool secure) { return dataStorePut(name,(const void *)data.data(),(unsigned int)data.length(),secure); }
 
- 	inline void dataStoreDelete(const char *name) { _dataStorePutFunction(reinterpret_cast<ZT1_Node *>(this),_uPtr,name,(const void *)0,0,0); }
 
- 	std::string dataStoreGet(const char *name);
 
- 	/**
 
- 	 * Post an event to the external user
 
- 	 *
 
- 	 * @param ev Event type
 
- 	 * @param md Meta-data (default: NULL/none)
 
- 	 */
 
- 	inline void postEvent(ZT1_Event ev,const void *md = (const void *)0) { _eventCallback(reinterpret_cast<ZT1_Node *>(this),_uPtr,ev,md); }
 
- 	/**
 
- 	 * Update virtual network port configuration
 
- 	 *
 
- 	 * @param nwid Network ID
 
- 	 * @param op Configuration operation
 
- 	 * @param nc Network configuration
 
- 	 */
 
- 	inline int configureVirtualNetworkPort(uint64_t nwid,ZT1_VirtualNetworkConfigOperation op,const ZT1_VirtualNetworkConfig *nc) { return _virtualNetworkConfigFunction(reinterpret_cast<ZT1_Node *>(this),_uPtr,nwid,op,nc); }
 
- 	/**
 
- 	 * @return True if we appear to be online
 
- 	 */
 
- 	inline bool online() const throw() { return _online; }
 
- 	/**
 
- 	 * If this version is newer than the newest we've seen, post a new version seen event
 
- 	 */
 
- 	void postNewerVersionIfNewer(unsigned int major,unsigned int minor,unsigned int rev);
 
- #ifdef ZT_TRACE
 
- 	void postTrace(const char *module,unsigned int line,const char *fmt,...);
 
- #endif
 
- 	/**
 
- 	 * @return Next 64-bit random number (not for cryptographic use)
 
- 	 */
 
- 	uint64_t prng();
 
- private:
 
- 	inline SharedPtr<Network> _network(uint64_t nwid) const
 
- 	{
 
- 		// assumes _networks_m is locked
 
- 		for(std::vector< std::pair< uint64_t, SharedPtr<Network> > >::const_iterator i=_networks.begin();i!=_networks.end();++i) {
 
- 			if (i->first == nwid)
 
- 				return i->second;
 
- 		}
 
- 		return SharedPtr<Network>();
 
- 	}
 
- 	RuntimeEnvironment _RR;
 
- 	RuntimeEnvironment *RR;
 
- 	void *_uPtr; // _uptr (lower case) is reserved in Visual Studio :P
 
- 	ZT1_DataStoreGetFunction _dataStoreGetFunction;
 
- 	ZT1_DataStorePutFunction _dataStorePutFunction;
 
- 	ZT1_WirePacketSendFunction _wirePacketSendFunction;
 
- 	ZT1_VirtualNetworkFrameFunction _virtualNetworkFrameFunction;
 
- 	ZT1_VirtualNetworkConfigFunction _virtualNetworkConfigFunction;
 
- 	ZT1_EventCallback _eventCallback;
 
- 	std::vector< std::pair< uint64_t, SharedPtr<Network> > > _networks;
 
- 	Mutex _networks_m;
 
- 	std::vector<Path> _directPaths;
 
- 	Mutex _directPaths_m;
 
- 	Mutex _backgroundTasksLock;
 
- 	unsigned int _prngStreamPtr;
 
- 	Salsa20 _prng;
 
- 	uint64_t _prngStream[16]; // repeatedly encrypted with _prng to yield a high-quality non-crypto PRNG stream
 
- 	uint64_t _now;
 
- 	uint64_t _lastPingCheck;
 
- 	uint64_t _lastHousekeepingRun;
 
- 	unsigned int _newestVersionSeen[3]; // major, minor, revision
 
- 	bool _online;
 
- };
 
- } // namespace ZeroTier
 
- #endif
 
 
  |