-
Notifications
You must be signed in to change notification settings - Fork 1
/
MeshCore.cpp
451 lines (369 loc) · 11.1 KB
/
MeshCore.cpp
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include "MeshCore.h"
#include "MessageCore.h"
#include "CredentialManager.h"
#include "Connection.h"
#include "ConnectionPool.h"
#include "PeerID.h"
#include "BValue.h"
#include "SystemIDs.h"
using namespace RAIN;
extern CredentialManager *globalCredentialManager;
/** a meshcore is responsible for trying to maintain the stability of the mesh,
* and maintaining a list of the peers on the mesh
* \param mc the messagecore to be used by this meshcore */
MeshCore::MeshCore(MessageCore *mc)
{
this->mc = mc;
this->tickCount = 0;
this->peerlist.push_back(globalCredentialManager->GetMyPeerID());
}
MeshCore::~MeshCore()
{
while (this->peerlist.size() > 0)
{
delete this->peerlist.at(0);
this->peerlist.erase(this->peerlist.begin());
}
}
void MeshCore::Tick()
{
this->tickCount--;
if (this->tickCount <= 0)
{
this->CheckConnections();
this->tickCount = CHECK_INTERVAL;
}
}
void MeshCore::CheckConnections()
{
int n = this->mc->app->connections->Count(true);
if (n < MIN_CONNECTIONS)
{
int n_all = this->mc->app->connections->Count(false);
if (n_all > n)
{
this->mc->app->connections->TryConnections();
}
PeerID *p = this->GetRandomPeer();
if (p != NULL)
{
this->mc->app->connections->ConnectPeer(p);
}
}
}
/** this is the callback used by messagecore when a message is received for the
* meshcore
*
* \param msg pointer to the message received */
void MeshCore::HandleMessage(Message *msg)
{
BENC_SAFE_CAST(msg->headers["Event"], eventstr, String);
switch (msg->status)
{
case MSG_STATUS_LOCAL:
if (eventstr && eventstr->getString() == "Client-Connection-Made")
{
/* we just made a new connection successfully, so say hello */
Message *hello = new Message();
hello->status = MSG_STATUS_OUTGOING;
hello->subsystem = MESHCORE_ID;
hello->PrepareBroadcast();
hello->headers["Event"] = new BEnc::String("Hello");
hello->headers["Listening-On"] = new BEnc::Integer(DEFAULT_PORT);
hello->headers["PeerList"] = this->GetPeerList();
this->mc->PushMessage(hello);
}
break;
case MSG_STATUS_INCOMING:
if (eventstr && eventstr->getString() == "Hello")
{
if (msg->headers.count("PeerList"))
this->MergePeerList(msg->headers["PeerList"]);
/* we have received a hello, so we reply with our full peerlist */
Message *pl = new Message();
pl->status = MSG_STATUS_OUTGOING;
pl->subsystem = MESHCORE_ID;
pl->headers["Event"] = new BEnc::String("Peerlist");
pl->headers["Destination"] = new BEnc::String("Peer");
pl->headers["From"] = globalCredentialManager->GetMyPeerID()->AsBValue();
pl->headers["To"] = msg->GetReply();
pl->headers["PeerList"] = this->GetPeerList();
this->mc->PushMessage(pl);
} else if (eventstr && eventstr->getString() == "Goodbye") {
/* a peer said goodbye, so we unmerge them from our peerlist - they
* never actually do this yet */
BENC_SAFE_CAST(msg->headers["Remote-UID"], remoteuid, String);
if (remoteuid)
this->UnmergePeer(remoteuid->getString());
} else if (eventstr && eventstr->getString() == "Peerlist") {
/* we got a peerlist from someone, so merge */
this->MergePeerList(msg->headers["PeerList"]);
}
}
}
int MeshCore::GetMessageHandlerID()
{
return MESHCORE_ID;
}
/** imports the identifiers in message <b>msg</b> into our peerlist
*
* \param msg the message from which to import identifiers */
void MeshCore::ImportIdentifiers(Message *msg)
{
if (msg->headers.count("From"))
this->MergeIdentifier(msg->headers["From"]);
if (msg->headers.count("To"))
this->MergeIdentifier(msg->headers["To"]);
wxLogVerbose("MeshCore::ImportIdentifiers ended");
}
/** merges a single identifier into the peerlist
*
* \param v the benc value with an encoded peerid */
void MeshCore::MergeIdentifier(BEnc::Value *v)
{
/* unwrap the peerid */
PeerID *pid = new PeerID(v);
/* we always need the hash, and can safely discard
* the peerid if we don't have it because it's useless */
if (pid->hash.IsEmpty())
{
delete pid;
return;
}
/* see if we have it already */
PeerID *current = this->GetPeer(pid->hash);
if (current)
{
/* we do, so merge in any extra data from the received id */
wxLogVerbose("MeshCore::MergeIdentifier: merging %s", CryptoHelper::HashToHex(pid->hash, true).c_str());
current->Merge(pid);
delete pid;
} else {
/* we don't (this is a new peerid to us) so add it to the peerlist */
wxLogVerbose("MeshCore::MergeIdentifier: adding %s", CryptoHelper::HashToHex(pid->hash, true).c_str());
this->peerlist.push_back(pid);
}
}
/** returns the number of peers known by the peerlist with certain characteristics
*
* \param t a member of the PEER_TYPE enum
* \param selfok if true, this function will include us in the result
*/
int MeshCore::GetPeerCount(PEER_TYPE t, bool selfok)
{
if (t == PEER_TYPE_ALL)
return this->peerlist.size();
std::vector<PeerID*>::iterator it = this->peerlist.begin();
int total = 0;
while (it != this->peerlist.end())
{
PeerID *pid = *it;
if (!selfok && pid->HashMatches(globalCredentialManager->GetMyPeerID()->hash))
{
it++;
continue;
}
switch (t)
{
case PEER_TYPE_CONNECTABLE: if (pid->address != "")
total++;
break;
case PEER_TYPE_KNOWN: if (pid->certificate != NULL)
total++;
break;
case PEER_TYPE_UNKNOWN: if (pid->certificate == NULL)
total++;
break;
}
it++;
}
return total;
}
/** similar to getpeercount, except it returns a random peerid with certain
* characteristics, or NULL if impossible to fulfill
*
* if selfok is true, this might return our own peerid */
//TODO: rewrite because this is really stupid
PeerID * MeshCore::GetRandomPeer(PEER_TYPE t, bool selfok)
{
int n = this->GetPeerCount(t, selfok);
if (n == 0)
return NULL;
size_t i = -1;
bool ok = false;
while (!ok)
{
i = 1 + this->peerlist.size() * float(rand() / RAND_MAX);
if (i < 0 || i >= this->peerlist.size())
continue;
switch (t)
{
case PEER_TYPE_CONNECTABLE: ok = (this->peerlist.at(i)->address != "");
break;
case PEER_TYPE_KNOWN: ok = (this->peerlist.at(i)->certificate != NULL);
break;
case PEER_TYPE_UNKNOWN: ok = (this->peerlist.at(i)->certificate == NULL);
break;
}
if (ok && this->peerlist.at(i)->HashMatches(globalCredentialManager->GetMyPeerID()->hash))
ok = selfok;
}
return this->peerlist.at(i);
}
/** returns the hash of a peer that piecequeue can send a piece to */
wxString RAIN::MeshCore::GetPieceTarget()
{
RAIN::PeerID *p = this->GetRandomPeer(RAIN::PEER_TYPE_CONNECTABLE);
if (p)
return p->hash;
else
return "";
}
/** imports the given peerlist (usually taken from a reply to a hello message) into the current peerlist
* incoming peerlist is a hash of hashes:
* pkhash => 'addr' => address string
* 'cert' => certificate string
* or
* pkhash => 'addr' => address string
* or
* pkhash => 'cert' => certificate string
* or
* pkhash => (empty hash)
* where the values are not known */
bool MeshCore::MergePeerList(BEnc::Value *v)
{
/* if the merge was useful, i will be nonzero at the end of this */
int i = 0;
// TODO: consolidate with peerid changes
if (v == NULL)
return false;
if (v->type == BENC_VAL_DICT)
{
BENC_SAFE_CAST(v, vd, Dictionary);
std::map <wxString, BEnc::Value*, BEnc::Cmp>::iterator it = vd->hash.begin();
while (it != vd->hash.end())
{
wxString hash = it->first;
if (hash.Length() == 0)
{
it++;
continue;
}
BENC_SAFE_CAST(it->second, t, Dictionary);
PeerID *pid = this->GetPeer(hash);
if (pid)
{
PEERID_WANTS wants = pid->GetWants();
/* the stored peer wants a certificate (or more) and we have a cert */
if ((wants == PEERID_WANTS_CERT || wants == PEERID_WANTS_ALL) && t->hasDictValue("cert"))
{
BENC_SAFE_CAST(t->getDictValue("cert"), certstr, String);
wxString x509_str = certstr->getString();
unsigned char *x509_b = (unsigned char *) x509_str.c_str();
pid->certificate = d2i_X509(NULL, &x509_b, x509_str.Length());
i++;
}
/* same sort of thing with address */
if ((wants == PEERID_WANTS_ADDR || wants == PEERID_WANTS_ALL) && t->hasDictValue("addr"))
{
BENC_SAFE_CAST(t->getDictValue("addr"), addrstr, String);
pid->address = addrstr->getString();
i++;
}
} else {
/* we've never seen this peer hash before, so create a new
* peerid for it */
PeerID *newpid = new PeerID();
newpid->hash = hash;
if (t->hasDictValue("addr"))
{
BENC_SAFE_CAST(t->getDictValue("addr"), addrstr, String);
newpid->address = addrstr->getString();
}
if (t->hasDictValue("cert"))
{
BENC_SAFE_CAST(t->getDictValue("cert"), certstr, String);
wxString x509_str = certstr->getString();
unsigned char *x509_b = (unsigned char *) x509_str.c_str();
newpid->certificate = d2i_X509(NULL, &x509_b, x509_str.Length());
}
this->peerlist.push_back(newpid);
i++;
}
it++;
}
}
/* return true if we actually added/merged any new peers */
return (i > 0);
}
/** removes a peer based on a keyhash 'p', returning true if we did anything */
bool MeshCore::UnmergePeer(const wxString &p)
{
std::vector<PeerID*>::iterator it = this->peerlist.begin();
while (it != this->peerlist.end())
{
if ((*it)->HashMatches(p))
{
delete *it;
this->peerlist.erase(it);
return true;
}
it++;
}
return false;
}
/* returns a benc::value containing our known peers */
BEnc::Value * MeshCore::GetPeerList()
{
BEnc::Dictionary *root = new BEnc::Dictionary();
std::vector<PeerID*>::iterator it = this->peerlist.begin();
while (it != this->peerlist.end())
{
PeerID *pid = *it;
BEnc::Dictionary *child = new BEnc::Dictionary();
if (pid->address != "")
child->hash["addr"] = new BEnc::String(pid->address);
if (pid->certificate)
child->hash["cert"] = new BEnc::String(pid->CertificateAsString());
root->hash[pid->hash] = child;
it++;
}
return root;
}
/** returns the peerid of the peer with hash <b>hash</b>, or null if unknown */
PeerID * MeshCore::GetPeer(const wxString& hash)
{
std::vector<PeerID*>::iterator it = this->peerlist.begin();
while (it != this->peerlist.end())
{
PeerID *pid = *it;
if (pid->HashMatches(hash))
{
return pid;
}
it++;
}
return NULL;
}
/** similar to above, but by index instead of hash */
PeerID * MeshCore::GetPeer(size_t i)
{
if (i < this->peerlist.size())
return this->peerlist.at(i);
else
return NULL;
}
PeerID * MeshCore::GetPeerByCommonName(const wxString& cn)
{
std::vector<PeerID*>::iterator it = this->peerlist.begin();
while (it != this->peerlist.end())
{
PeerID *pid = *it;
if (pid->GetCertificateField(NID_commonName) == cn)
{
return pid;
}
it++;
}
return NULL;
}