summaryrefslogtreecommitdiff
path: root/node/Topology.cpp
blob: 8acbb0273a68cf789e227a4298013a4e0728e39d (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
/*
 * ZeroTier One - Global Peer to Peer Ethernet
 * Copyright (C) 2012-2013  ZeroTier Networks LLC
 *
 * 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/
 */

#include <algorithm>

#include "Topology.hpp"
#include "NodeConfig.hpp"
#include "CMWC4096.hpp"

namespace ZeroTier {

#define ZT_KISSDB_HASH_TABLE_SIZE 32768
#define ZT_KISSDB_KEY_SIZE ZT_ADDRESS_LENGTH
#define ZT_KISSDB_VALUE_SIZE ZT_PEER_MAX_SERIALIZED_LENGTH

Topology::Topology(const RuntimeEnvironment *renv,const char *dbpath)
	throw(std::runtime_error) :
	_r(renv),
	_amSupernode(false)
{
	if (KISSDB_open(&_dbm,dbpath,KISSDB_OPEN_MODE_RWCREAT,ZT_KISSDB_HASH_TABLE_SIZE,ZT_KISSDB_KEY_SIZE,ZT_KISSDB_VALUE_SIZE)) {
		if (KISSDB_open(&_dbm,dbpath,KISSDB_OPEN_MODE_RWREPLACE,ZT_KISSDB_HASH_TABLE_SIZE,ZT_KISSDB_KEY_SIZE,ZT_KISSDB_VALUE_SIZE))
			throw std::runtime_error("unable to open peer database (rw/create)");
	}

	if ((_dbm.key_size != ZT_KISSDB_KEY_SIZE)||(_dbm.value_size != ZT_KISSDB_VALUE_SIZE)||(_dbm.hash_table_size != ZT_KISSDB_HASH_TABLE_SIZE)) {
		KISSDB_close(&_dbm);
		if (KISSDB_open(&_dbm,dbpath,KISSDB_OPEN_MODE_RWREPLACE,ZT_KISSDB_HASH_TABLE_SIZE,ZT_KISSDB_KEY_SIZE,ZT_KISSDB_VALUE_SIZE))
			throw std::runtime_error("unable to open peer database (recreate)");
	}

	Utils::lockDownFile(dbpath,false); // node.db caches secrets
}

Topology::~Topology()
{
	// Flush last changes to disk
	clean();
}

void Topology::setSupernodes(const std::map< Identity,std::vector<InetAddress> > &sn)
{
	Mutex::Lock _l(_supernodes_m);

	_supernodes = sn;
	_supernodeAddresses.clear();
	_supernodePeers.clear();

	for(std::map< Identity,std::vector<InetAddress> >::const_iterator i(sn.begin());i!=sn.end();++i) {
		if (i->first != _r->identity) {
			SharedPtr<Peer> p(getPeer(i->first.address()));
			if (!p)
				p = addPeer(SharedPtr<Peer>(new Peer(_r->identity,i->first)));
			for(std::vector<InetAddress>::const_iterator j(i->second.begin());j!=i->second.end();++j)
				p->setPathAddress(*j,true);
			_supernodePeers.push_back(p);
		}
		_supernodeAddresses.insert(i->first.address());
	}

	_amSupernode = (_supernodes.find(_r->identity) != _supernodes.end());
}

SharedPtr<Peer> Topology::addPeer(const SharedPtr<Peer> &peer)
{
	if (peer->address() == _r->identity.address()) {
		TRACE("BUG: addNewPeer() caught and ignored attempt to add peer for self");
		throw std::logic_error("cannot add peer for self");
	}

	SharedPtr<Peer> actualPeer;
	{
		Mutex::Lock _l(_activePeers_m);
		actualPeer = _activePeers.insert(std::pair< Address,SharedPtr<Peer> >(peer->address(),peer)).first->second;
	}

	uint64_t atmp[ZT_ADDRESS_LENGTH];
	actualPeer->address().copyTo(atmp,ZT_ADDRESS_LENGTH);

	Buffer<ZT_PEER_MAX_SERIALIZED_LENGTH> b;
	actualPeer->serialize(b);
	b.zeroUnused();

	_dbm_m.lock();
	if (KISSDB_put(&_dbm,atmp,b.data())) {
		TRACE("error writing %s to peerdb",actualPeer->address().toString().c_str());
	} else actualPeer->getAndResetDirty();
	_dbm_m.unlock();

	return actualPeer;
}

SharedPtr<Peer> Topology::getPeer(const Address &zta)
{
	if (zta == _r->identity.address()) {
		TRACE("BUG: ignored attempt to getPeer() for self, returned NULL");
		return SharedPtr<Peer>();
	}

	{
		Mutex::Lock _l(_activePeers_m);
		std::map< Address,SharedPtr<Peer> >::const_iterator ap(_activePeers.find(zta));
		if ((ap != _activePeers.end())&&(ap->second))
			return ap->second;
	}

	unsigned char ztatmp[ZT_ADDRESS_LENGTH];
	zta.copyTo(ztatmp,ZT_ADDRESS_LENGTH);

	Buffer<ZT_KISSDB_VALUE_SIZE> b(ZT_KISSDB_VALUE_SIZE);
	_dbm_m.lock();
	if (!KISSDB_get(&_dbm,ztatmp,b.data())) {
		_dbm_m.unlock();

		SharedPtr<Peer> p(new Peer());
		try {
			p->deserialize(b,0);
			Mutex::Lock _l(_activePeers_m);
			_activePeers[zta] = p;
			return p;
		} catch ( ... ) {
			TRACE("unexpected exception deserializing peer %s from peerdb",zta.toString().c_str());
			return SharedPtr<Peer>();
		}
	} else _dbm_m.unlock();

	return SharedPtr<Peer>();
}

SharedPtr<Peer> Topology::getBestSupernode(const Address *avoid,unsigned int avoidCount,bool strictAvoid) const
{
	SharedPtr<Peer> bestSupernode;
	unsigned int bestSupernodeLatency = 0xffff;
	uint64_t now = Utils::now();

	Mutex::Lock _l(_supernodes_m);

	if (_supernodePeers.empty())
		return bestSupernode;

	for(std::vector< SharedPtr<Peer> >::const_iterator sn=_supernodePeers.begin();sn!=_supernodePeers.end();) {
		for(unsigned int i=0;i<avoidCount;++i) {
			if (avoid[i] == (*sn)->address())
				goto skip_and_try_next_supernode;
		}
		if ((*sn)->hasActiveDirectPath(now)) {
			unsigned int l = (*sn)->latency();
			if (bestSupernode) {
				if ((l)&&(l < bestSupernodeLatency)) {
					bestSupernodeLatency = l;
					bestSupernode = *sn;
				}
			} else {
				if (l)
					bestSupernodeLatency = l;
				bestSupernode = *sn;
			}
		}
skip_and_try_next_supernode:
		++sn;
	}

	if ((bestSupernode)||(strictAvoid))
		return bestSupernode;

	for(std::vector< SharedPtr<Peer> >::const_iterator sn=_supernodePeers.begin();sn!=_supernodePeers.end();++sn) {
		if ((*sn)->hasActiveDirectPath(now)) {
			unsigned int l = (*sn)->latency();
			if (bestSupernode) {
				if ((l)&&(l < bestSupernodeLatency)) {
					bestSupernodeLatency = l;
					bestSupernode = *sn;
				}
			} else {
				if (l)
					bestSupernodeLatency = l;
				bestSupernode = *sn;
			}
		}
	}

	if (bestSupernode)
		return bestSupernode;

	return _supernodePeers[_r->prng->next32() % _supernodePeers.size()];
}

void Topology::clean()
{
	TRACE("cleaning caches and flushing modified peers to disk...");

	Mutex::Lock _l(_activePeers_m);
	for(std::map< Address,SharedPtr<Peer> >::iterator p(_activePeers.begin());p!=_activePeers.end();++p) {
		if (p->second->getAndResetDirty()) {
			try {
				uint64_t atmp[ZT_ADDRESS_LENGTH];
				p->second->identity().address().copyTo(atmp,ZT_ADDRESS_LENGTH);

				Buffer<ZT_PEER_MAX_SERIALIZED_LENGTH> b;
				p->second->serialize(b);
				b.zeroUnused();

				_dbm_m.lock();
				if (KISSDB_put(&_dbm,atmp,b.data())) {
					TRACE("error writing %s to peer.db",p->second->identity().address().toString().c_str());
				}
				_dbm_m.unlock();
			} catch ( ... ) {
				TRACE("unexpected exception flushing %s to peer.db",p->second->identity().address().toString().c_str());
			}
		}
	}
}

} // namespace ZeroTier