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
|
#include "skynet.h"
#include "globals.h"
#include <QMutex>
//#include "dialogs/viewcertdialog.h"
#include "contacts.h"
#include "openssltool.h"
//#include <QMessageBox> //debug
//#include "dialogs/forgedcertdialog.h"
#include <QDebug>
SkyNet::SkyNet()
: nodes(config_dbname), QObject(0)
{
self_aware = false;
PICA_client_callbacks cbs = {
newmsg_cb,
msgok_cb,
channel_established_cb,
channel_failed,
accept_cb,
notfound_cb,
channel_closed_cb,
nodelist_cb,
peer_cert_verify_cb
};
PICA_client_init(&cbs);
nodelink = NULL;
connect(this, SIGNAL(PeerCertificateReceived(quint32,QString,bool*)), this, SLOT(verify_peer_cert(quint32,QString,bool*)),Qt::DirectConnection);
}
void SkyNet::nodethread_finished()
{
//удалить здесь завершившийся поток
for (int i=0;i<threads.count();i++)
{
if (threads[i]->isFinished())
{
delete threads.takeAt(i);
break;
}
}
if (threads.count() == 0)
{
self_aware = false;
emit LostSelfAwareness();
}
}
void SkyNet::nodethread_connected(QString addr, quint16 port, NodeThread *thread)
{
if (self_aware)
{
emit BecameSelfAware();
//restore old peer connections, if any
QList<quint32> c2c_peer_ids;
c2c_peer_ids = msgqueues.keys();
for (int i = 0; i < c2c_peer_ids.size(); i++)
{
int ret;
write_mutex.lock();
struct PICA_chaninfo *chan = NULL;
ret = PICA_create_channel(nodelink, c2c_peer_ids[i], &chan);
qDebug()<<"restoring channel to "<<c2c_peer_ids[i]<<" ret ="<<ret<<"\n";
write_mutex.unlock();
}
//start timer
timer_id = startTimer(10000);
}
}
void SkyNet::node_status_changed(QString addr, quint16 port, bool alive)
{
Nodes::NodeRecord nr = {addr, port};
nodes.UpdateStatus(nr, alive);
}
void SkyNet::verify_peer_cert(quint32 peer_id, QString cert_pem, bool *verified)
{
// ViewCertDialog vcd;
// vcd.SetCert(cert_pem);
// vcd.exec();
Contacts cnt(config_dbname, account_id);
QString stored_cert;
if ((stored_cert = cnt.GetContactCert(peer_id)).isEmpty())
{
cnt.SetContactCert(peer_id, cert_pem);
QString name = OpenSSLTool::NameFromCertString(cert_pem);
cnt.SetContactName(peer_id, name);
}
else
{//compare certificates
QString strip_pem[4] = {
"-----BEGIN CERTIFICATE-----",
"-----END CERTIFICATE-----",
"-----BEGIN X509 CERTIFICATE-----",
"-----END X509 CERTIFICATE-----"
};
QString stripped_stored_cert = stored_cert;
QString stripped_received_cert = cert_pem;
for (int i = 0; i < 4; i++)
{
stripped_stored_cert.replace(strip_pem[i], "");
stripped_received_cert.replace(strip_pem[i], "");
}
QByteArray stored_DER, received_DER;
stored_DER = QByteArray::fromBase64(stripped_stored_cert.toAscii().constData());
received_DER = QByteArray::fromBase64(stripped_received_cert.toAscii().constData());
if (stored_DER.size() != received_DER.size() ||
memcmp(stored_DER.constData(), received_DER.constData(), stored_DER.size()) != 0)
{//put scary message here
//QMessageBox mbx;
//mbx.setText(tr("Certificate is forged!!!"));
//mbx.exec();
//ForgedCertDialog fcd(peer_id, cert_pem, stored_cert);
//fcd.exec();
*verified = false;
emit CertificateForged(peer_id, cert_pem, stored_cert);
}
}
}
void SkyNet::timerEvent(QTimerEvent *e)
{
if (self_aware)
{
QList<quint32> c2c_peer_ids = msgqueues.keys();
write_mutex.lock();
c2c_peer_ids = filter_existing_chans(c2c_peer_ids);
for (int i = 0; i < c2c_peer_ids.size(); i++)
{
int ret;
struct PICA_chaninfo *chan = NULL;
ret = PICA_create_channel(nodelink, c2c_peer_ids[i], &chan);
qDebug()<<"restoring channel to "<<c2c_peer_ids[i]<<" ret ="<<ret<<" in timer event\n";
}
write_mutex.unlock();
}
else
{
killTimer(e->timerId());
Accounts::AccountRecord acc = this->CurrentAccount();
Join(acc);
}
}
void SkyNet::Join(Accounts::AccountRecord &accrec)
{
QList<Nodes::NodeRecord> nodelist;
skynet_account = accrec;
nodelist = nodes.GetNodes();
if (nodelist.count()==0)
{
status = QObject::tr("No known Pica Pica nodes");
return;
}
for (int i=0;i<nodelist.count() && !self_aware;i++)
{//TODO FIXME сделать запуск потоков порциями, а не все сразу
threads.append(new NodeThread(nodelist[i],&self_aware,skynet_account,&nodelink, &write_mutex));
connect(threads.last(), SIGNAL(finished()),this,SLOT(nodethread_finished()));
connect(threads.last(), SIGNAL(NodeStatusChanged(QString,quint16,bool)), this, SLOT(node_status_changed(QString,quint16,bool)));
connect(threads.last(), SIGNAL(ConnectedToNode(QString,quint16,NodeThread*)), this, SLOT(nodethread_connected(QString,quint16,NodeThread*)));
}
}
void SkyNet::Exit()
{
for (int i = 0; i< threads.count(); i++)
threads[i]->CloseThread();
self_aware = false;
killTimer(timer_id);
}
bool SkyNet::isSelfAware()
{
return self_aware;
}
void SkyNet::SendMessage(quint32 to, QString msg)
{
int ret = PICA_OK;
struct PICA_chaninfo *iptr;
write_mutex.lock();//<<
if ( (iptr = find_active_chan(to)) )
{
//write_mutex.lock();//<<
ret = PICA_send_msg(iptr, msg.toUtf8().data(), msg.toUtf8().size());
//write_mutex.unlock();//>>
if (ret != PICA_OK)
emit UnableToDeliver(to, msg);
write_mutex.unlock();//>>
return;
}
if (msgqueues.contains(to))
{
msgqueues[to].append(msg);
}
else
{
struct PICA_chaninfo *chan = NULL;
ret = PICA_create_channel(nodelink, to, &chan);
QList<QString> l;
l.append(msg);
msgqueues[to] = l;
}
write_mutex.unlock();//>>
if (ret != PICA_OK)
emit UnableToDeliver(to, msg);
}
void SkyNet::flush_msgqueue(quint32 to)
{
struct PICA_chaninfo *chan;
int ret;
if (self_aware && msgqueues.contains(to))
{
qDebug()<<"flushing "<<to<<" message queue\n";
if ((chan = find_active_chan(to)))
{
while( !msgqueues[to].empty() )
{
ret = PICA_send_msg(chan, msgqueues[to].first().toUtf8().data(), msgqueues[to].first().toUtf8().size());
if (ret != PICA_OK)
{
emit UnableToDeliver(to, msgqueues[to].first());
break;
}
msgqueues[to].removeFirst();
}
if (msgqueues[to].empty())
msgqueues.remove(to);
}
}
}
struct PICA_chaninfo * SkyNet::find_active_chan(quint32 peer_id)
{
if (!nodelink)
return NULL;
struct PICA_chaninfo *iptr = nodelink->chan_list_head;
while(iptr)
{
if (iptr->state == PICA_CHANSTATE_ACTIVE && iptr->peer_id == peer_id)
break;
iptr = iptr->next;
}
return iptr;
}
QList<quint32> SkyNet::filter_existing_chans(QList<quint32> peer_ids)
{
QList<quint32> ret = peer_ids;
if (!nodelink)
return ret;
struct PICA_chaninfo *iptr = nodelink->chan_list_head;
while(iptr)
{
if (peer_ids.contains(iptr->peer_id))
ret.removeOne(iptr->peer_id);
iptr = iptr->next;
}
return ret;
}
void SkyNet::emit_Delivered(quint32 to)
{
emit Delivered(to);
}
void SkyNet::emit_MessageReceived(quint32 from, QString msg)
{
emit MessageReceived(from, msg);
}
void SkyNet::emit_UnableToDeliver(quint32 to, QString msg)
{
emit UnableToDeliver(to, msg);
}
void SkyNet::emit_PeerCertificateReceived(quint32 peer_id, QString cert_pem, bool *verified)
{
emit PeerCertificateReceived(peer_id, cert_pem, verified);
}
//callbacks
//all callbacks are executed in separate thread, created in Nodethread instance, REMEMBER THAT !!!
// write_mutex is locked
void SkyNet::newmsg_cb(unsigned int peer_id, char *msgbuf, unsigned int nb, int type)
{
QString msg = QString::fromUtf8(msgbuf, nb);
skynet->emit_MessageReceived(peer_id, msg);
}
void SkyNet::msgok_cb(unsigned int peer_id)
{
skynet->emit_Delivered(peer_id);
}
void SkyNet::channel_established_cb(unsigned int peer_id)
{
skynet->flush_msgqueue(peer_id);
}
void SkyNet::channel_failed(unsigned int peer_id)
{
qDebug()<<"channel failed ("<<peer_id<<")\n";
}
int SkyNet::accept_cb(unsigned int caller_id)
{
return 1; //implement black list
}
void SkyNet::notfound_cb(unsigned int callee_id)
{
qDebug()<<"not found ("<<callee_id<<")\n";
/*
if (skynet->msgqueues.contains(callee_id))
{
while ( ! skynet->msgqueues[callee_id].empty())
{
skynet->emit_UnableToDeliver(callee_id, skynet->msgqueues[callee_id].last());
skynet->msgqueues[callee_id].removeLast();
}
// msgqueues.remove(callee_id) was forgotten
}
*/
}
void SkyNet::channel_closed_cb(unsigned int peer_id, int reason)
{
qDebug()<<"channel closed ("<<peer_id<<")\n";
}
void SkyNet::nodelist_cb(int type, void *addr_bin, const char *addr_str, unsigned int port)
{
Nodes::NodeRecord nr = {addr_str, port};
skynet->nodes.Add(nr);
}
int SkyNet::peer_cert_verify_cb(unsigned int peer_id, const char *cert_pem, unsigned int nb)
{
bool verified = true;
skynet->emit_PeerCertificateReceived(peer_id, QString::fromAscii(cert_pem, nb), &verified);
if (!verified)
return 0;
return 1;
}
|