summaryrefslogtreecommitdiff
path: root/node/Node.hpp
blob: 9575170616de5b8758b902716081232dc6a1517a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * 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"
#include "Network.hpp"

#undef TRACE
#ifdef ZT_TRACE
#define TRACE(f,...) RR->node->postTrace(__FILE__,__LINE__,f,##__VA_ARGS__)
#else
#define TRACE(f,...) {}
#endif

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(
		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,
		unsigned int linkDesperation,
		const void *packetData,
		unsigned int packetLength,
		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,
		uint64_t *nextBackgroundTaskDeadline);
	ZT1_ResultCode processBackgroundTasks(uint64_t now,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);
	void setNetconfMaster(void *networkConfigMasterInstance);

	// 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
	 * @param desperation Link desperation for reaching this address
	 * @return True if packet appears to have been sent
	 */
	inline bool putPacket(const InetAddress &addr,const void *data,unsigned int len,unsigned int desperation)
	{
		return (_wirePacketSendFunction(
			reinterpret_cast<ZT1_Node *>(this),
			_uptr,
			reinterpret_cast<const struct sockaddr_storage *>(&addr),
			desperation,
			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)
	{
		Mutex::Lock _l(_networks_m);
		std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid));
		return ((nw == _networks.end()) ? SharedPtr<Network>() : nw->second);
	}

	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;
	}

	/**
	 * Get an overall current level of desperation
	 *
	 * The current level of desperation is based on how recently an upstream
	 * (a.k.a. supernode) peer has spoken to us. As such, it will change and
	 * return to 0 once something like tunneling (higher desperation link) is
	 * active. As a result, actual link desperation for outgoing messages
	 * should be the max of either this or the most recent link desperation
	 * for an incoming message from a given address. See Path.hpp and Peer.hpp.
	 *
	 * In other words think of this as 'the desperation we should try to
	 * escalate to right now.'
	 *
	 * @return Overall system level of desperation
	 */
	inline unsigned int coreDesperation() const throw() { return _coreDesperation; }

	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

private:
	RuntimeEnvironment *RR;

	void *_uptr;

	ZT1_DataStoreGetFunction _dataStoreGetFunction;
	ZT1_DataStorePutFunction _dataStorePutFunction;
	ZT1_WirePacketSendFunction _wirePacketSendFunction;
	ZT1_VirtualNetworkFrameFunction _virtualNetworkFrameFunction;
	ZT1_VirtualNetworkConfigFunction _virtualNetworkConfigFunction;
	ZT1_EventCallback _eventCallback;

	//Dictionary _localConfig; // persisted as local.conf
	//Mutex _localConfig_m;

	std::map< uint64_t,SharedPtr<Network> > _networks;
	Mutex _networks_m;

	Mutex _backgroundTasksLock;

	uint64_t _now;
	uint64_t _startTimeAfterInactivity;
	uint64_t _lastPingCheck;
	uint64_t _lastHousekeepingRun;
	unsigned int _coreDesperation;
	unsigned int _newestVersionSeen[3]; // major, minor, revision
	bool _online;
};

} // namespace ZeroTier

#endif