-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconmngr.cpp
585 lines (499 loc) · 18.8 KB
/
conmngr.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/*
This file is part of LANchat.
LANchat is free software: you can redistribute it and/or modify
it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE
Version 3 as published by the Free Software Foundation.
LANchat 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with LANchat. If not, see <http://www.gnu.org/licenses/>.
*/
#include <conmngr.h>
/*Protocol:
I name it chain and I tell you why: messages are not broadcasted into subnet
each node relays messages to another node. Each node can be connected at most
to two other nodes, its graph is like this t->o->o->o->o->h
when a new node enters the network it broadcasts a UDP "advertize" packet
which contains its name and address. None of the nodes connects to this new
node except the node on the head so it contacts the new node and makes a
TCP connection with it. Now the new node becomes the head. Former head sends
a UNN packet containing its own ip/nick registration list. One scenario is
when a node in the middle of the chain gets shut down. When this happens
The chain is broken and we will have 2 separate chains. [Remeber our chain
should never become a circle.] To handle this situation the node that was
in front of gone node broadcasts a special packet "TDC" saying my tail connection
is dropped. The node which was at the tail of gone node responds to this packet by
connecting to the broadcaster node. Other nodes respond to this packet by removing
its ip/nick pair from their registration lists. One problem here is that each node
should know the origin of each message. So when the head connects to a new node it
sends the ip and nickname associated with that ip through the network. Each node
registers that ip/nick pair. We also include the ip and nickname in the packet we
want to send. So any recieving node can check the ip/nick pair against its own list
to see they match or not. If not the node ifers that the sender has change its nickname.
Remember that each node's IP is the sole identifier of that node.
*/
/*Packet types
adz : UDP advertize
con : For tcp hanshake start
hsh : respond to con
tdc : Tail Dropped Connection: when the initiator drops the connection
*/
ConnectionManager::ConnectionManager()
{
udpSocket = nullptr;
listenSocket= nullptr;
connectionFrom= nullptr;
connectionTo= nullptr;
nickIpPairList= nullptr;
incommingTcp = nullptr;
outgoingTcp = nullptr;
nickname = "MyNick";
LANCHATSIG.append("LANchat");
}
void ConnectionManager::StartNetwork(QHostAddress subnet, quint32 netmaskOnes, bool broadcast)
{
LogMessage("Connection Manager Started.");
QList<QNetworkInterface> il = QNetworkInterface::allInterfaces();
bool found = false;
//Creating subnet mask from the number user has entered
netmask = 0;
netmask = ~netmask;
netmask <<= 32 - netmaskOnes;
for(int i=0 ; i<il.size();++i)
{
//LogMessage(il[i].humanReadableName() + ':');
if( (il[i].flags() & QNetworkInterface::IsLoopBack) || !(il[i].flags() & QNetworkInterface::IsUp) || !(il[i].flags() & QNetworkInterface::CanBroadcast)) continue;
QList<QNetworkAddressEntry> addList = il[i].addressEntries();
for(int j = 0; j < addList.size();++j)
{
//LogMessage(" " + addList[j].ip().toString() + "/" + addList[j].netmask().toString() +"="+QHostAddress(addList[j].netmask().toIPv4Address() & addList[j].ip().toIPv4Address()).toString());
if((addList[j].ip().protocol() == QAbstractSocket::IPv4Protocol )&&((subnet.toIPv4Address() & netmask)== (addList[j].netmask().toIPv4Address() & addList[j].ip().toIPv4Address())))
{
listenAddress = addList[j].ip();
LogMessage("\"" + il[i].humanReadableName() + "\" interface selected." );
found = true;
break;
}
}
if(found) break;
}
if(!found)
{
LogMessage("No suitable interface found. Using whatever comes up!");
listenAddress = QHostAddress::Any;
}
nickIpPairList = new NickIpPairMap;
listenSocket = new QTcpServer(this);
udpSocket = new QUdpSocket(this);
if(!udpSocket->bind(listenAddress, LISTEN_PORT))
{
QString s("Binding of UDP socket failed.");
throw s;
}
if(!listenSocket->listen(listenAddress, LISTEN_PORT))
{
QString s("Listening to TCP socket failed.");
throw s;
}
if( !connect(listenSocket, SIGNAL(newConnection()), this, SLOT(OnIncommingConnection())))
{
QString s("Connecting newConnection()->OnIncommingConnection() failed.");
throw s;
}
if(!connect(udpSocket, SIGNAL(readyRead()), this, SLOT(OnPendingDatagrams())))
{
QString s("Connecting readyRead()->OnPendingDatagrams() failed.");
throw s;
}
QByteArray broadcastBytes = CreateADZPacket();
LogMessage("Our address is " + listenSocket->serverAddress().toString() /*+ ":" + LISTEN_PORT*/);
//Broadcasting ourselves
if(broadcast)
{
udpSocket->writeDatagram(broadcastBytes, QHostAddress(listenSocket->serverAddress().toIPv4Address() | ~netmask), LISTEN_PORT);
}else
{
quint32 a = listenSocket->serverAddress().toIPv4Address() & netmask;
for(quint32 i= 1 ; ~((i+a)|netmask) != 0; ++i)
{
udpSocket->writeDatagram(broadcastBytes.data(), broadcastBytes.size(), QHostAddress(a+i), LISTEN_PORT);
//LogMessage(QHostAddress(a+i).toString());
}
}
LogMessage("Advertise sent.");
}
void ConnectionManager::OnReadTcpTo()
{
QByteArray readBytes;
readBytes.resize(connectionTo->bytesAvailable());
readBytes = connectionTo->read(connectionTo->bytesAvailable());
if(!readBytes.startsWith(LANCHATSIG))
{
LogMessage("TCP packet was not from LANchat, ignored.");
return;
}
QStringList payloads;
PacketTypes type = DisassemblePacket(&readBytes, &payloads);
switch(type)
{
case MSG:
OnMSGPacket(&payloads, false);
break;
default:
LogMessage("Unexpected packet type from acceptor.");
}
}
void ConnectionManager::OnReadTcpFrom()
{
QByteArray readBytes;
readBytes.resize(connectionFrom->bytesAvailable());
readBytes = connectionFrom->read(connectionFrom->bytesAvailable());
if(!readBytes.startsWith(LANCHATSIG))
{
LogMessage("TCP packet was not from LANchat, ignored.");
return;
}
QStringList payloads;
PacketTypes type = DisassemblePacket(&readBytes, &payloads);
switch(type)
{
case MSG:
OnMSGPacket(&payloads, true);
break;
default:
LogMessage("Unexpected packet type from initiator.");
}
}
void ConnectionManager::OnIncommingConnection()
{
LogMessage("Incomming TCP connection...");
if(incommingTcp != nullptr) delete incommingTcp; //We only keep track of the last TCP made and wait for CON
incommingTcp = listenSocket->nextPendingConnection();
connect(incommingTcp, SIGNAL(readyRead()), this, SLOT(OnReadTcpIncomming()));
connect(incommingTcp, SIGNAL(disconnected()), this, SLOT(OnTcpIncommingDisconnect()));
}
void ConnectionManager::OnPendingDatagrams()
{
while (udpSocket->hasPendingDatagrams())
{
QByteArray datagram;
QHostAddress udpPeer;
quint16 peerPort;
QStringList payloads;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(), udpSocket->pendingDatagramSize(), &udpPeer, &peerPort);
char dummy[50];
LogMessage("UDP datagram received from " + udpPeer.toString() + ':' + QString(_itoa(peerPort, dummy, 10)));
if(!datagram.startsWith("LANchat"))
{
LogMessage("UDP datagram was not from LANchat, ignored.");
return;
}
PacketTypes packetType = DisassemblePacket(&datagram, &payloads);
if(packetType == ADZ)
{
OnADZPacket(&payloads);
}
}
}
QString ConnectionManager::ReadMessage()
{
QString temp = readBuffer;
readBuffer.clear();
return temp;
}
void ConnectionManager::WriteMessage(QString message)
{
if(connectionFrom != nullptr) connectionFrom->write(CreateMSGPacket(message));
if(connectionTo != nullptr) connectionTo->write(CreateMSGPacket(message));
}
void ConnectionManager::OnHandshakeOutgoing()
{
outgoingTcp->write(CreateCONPacket());
LogMessage("Sent CON packet.");
}
void ConnectionManager::OnTcpToDisconnect()
{
connectionTo->deleteLater();
connectionTo = nullptr;
LogMessage("Disconnected from our acceptor.");
}
void ConnectionManager::OnTcpFromDisconnect()
{
connectionFrom->deleteLater();
connectionFrom = nullptr;
LogMessage("Disconnected from our initiator.");
}
void ConnectionManager::OnReadTcpIncomming()
{
QByteArray readBytes;
QStringList payloads;
PacketTypes type;
readBytes.resize(incommingTcp->bytesAvailable());
readBytes = incommingTcp->read(incommingTcp->bytesAvailable());
if(!readBytes.startsWith(LANCHATSIG))
{
incommingTcp->deleteLater();
incommingTcp = nullptr;
LogMessage("Incomming TCP connection was not from LANchat, connection closed.");
return;
}
else
type = DisassemblePacket(&readBytes, &payloads);
if(type != CON)
{
incommingTcp->deleteLater();
incommingTcp = nullptr;
LogMessage("Peer didn't start with CON, connection closed.");
return;
}
else
OnCONPacket(&payloads);
}
void ConnectionManager::OnReadTcpOutgoing()
{
QByteArray readBytes;
QStringList payloads;
PacketTypes type;
readBytes.resize(outgoingTcp->bytesAvailable());
readBytes = outgoingTcp->read(outgoingTcp->bytesAvailable());
if(!readBytes.startsWith(LANCHATSIG))
{
outgoingTcp->deleteLater();
outgoingTcp = nullptr;
LogMessage("Peer response didn't start with LANchat, connection closed.");
return;
}
else
type = DisassemblePacket(&readBytes, &payloads);
if(type != HSH)
{
outgoingTcp->deleteLater();
outgoingTcp = nullptr;
LogMessage("Peer didn't respond with HSH, connection closed.");
return;
}
else
OnHSHPacket(&payloads);
}
void ConnectionManager::OnADZPacket(QStringList *packet)
{
QString nick, IP;
nick = (*packet)[1];
IP = (*packet)[2];
if(listenSocket->serverAddress().toString() == IP )
{
LogMessage("UDP datagram was ours, ignored.");
return;
}
LogMessage("UDP datagram has advertise flag.");
if(connectionTo == nullptr)
{
if(outgoingTcp != nullptr) delete outgoingTcp;
//Not sure if it is a right thing to do
//It means we were connecting to another peer. Then we got another advertize so we let go of the first one.
//and try to connect to new born peer and leaving first one alone
outgoingTcp = new QTcpSocket(this);
connect(outgoingTcp, SIGNAL(connected()), this, SLOT(OnHandshakeOutgoing()));
connect(outgoingTcp, SIGNAL(readyRead()), this, SLOT(OnReadTcpOutgoing()));
connect(outgoingTcp, SIGNAL(disconnected()), this, SLOT(OnTcpOutgoingDisconnect()));
//connect(connectionTo, SIGNAL(disconnected()), this, SLOT(OnTCPInitiatorDisconnect()));
outgoingTcp->connectToHost(QHostAddress(IP), LISTEN_PORT); //should use events to intercept
LogMessage("Connecting to advertiser....");
}
else
{
LogMessage("Already connected to another peer, ignoring advertise datagram.");
return; //We have already made a connection to a peer
}
}
void ConnectionManager::OnMSGPacket(QStringList *payloads, bool isFromTail)
{
if(isFromTail)
{
if(connectionTo != nullptr) connectionTo->write(CreateMSGPacket(QString((*payloads)[3])));
}
else
{
if(connectionFrom != nullptr) connectionFrom->write(CreateMSGPacket(QString((*payloads)[3])));
}
QHostAddress senderIP = QHostAddress ((*payloads)[1]);
QString senderNick = (*payloads)[2];
if(senderNick != nickIpPairList->at( senderIP ))
{
nickIpPairList->at( senderIP) = senderNick;
LogMessage("Peer with IP " + senderIP.toString() + "changed nickname from " + nickIpPairList->at( senderIP) + " to " + senderNick);
nickIpPairList->at( senderIP) = senderNick;
}
readBuffer = senderNick + ": " + (*payloads)[3];
emit NewMessage();
}
void ConnectionManager::OnCONPacket(QStringList *packet)
{
QString IP = (*packet)[2];
incommingTcp->disconnect();
connectionFrom = incommingTcp;
incommingTcp = nullptr;
//if sock ip != manifested ip client is behind a NAT
if(connectionFrom->peerAddress().toString() != IP)
{
LogMessage("Peer with IP \"" + connectionFrom->peerAddress().toString() + "\" is behind NAT.");
/* do something*/
}
connect(connectionFrom, SIGNAL(readyRead()), this, SLOT(OnReadTcpFrom()));
connect(connectionFrom, SIGNAL(disconnected()), this, SLOT(OnTcpFromDisconnect()));
connectionFrom->write(CreateHSHPacket());
LogMessage("Accepted TCP connection from (" + IP + ")");
int numberOfPeers = ((*packet)[1]).toInt();
for(int i =2 ; i < (numberOfPeers+2);i+=2)
{
nickIpPairList->insert(NickIpPair(QHostAddress((*packet)[i]), QString((*packet)[i+1])));
}
emit NicknamesChanged();
}
void ConnectionManager::OnHSHPacket(QStringList *payload)
{
outgoingTcp->disconnect();
connectionTo = outgoingTcp;
outgoingTcp = nullptr;
//connect(connectionTo, SIGNAL(connected()), this, SLOT(OnHandshakeOutgoing()));
connect(connectionTo, SIGNAL(readyRead()), this, SLOT(OnReadTcpTo()));
connect(connectionTo, SIGNAL(disconnected()), this, SLOT(OnTcpToDisconnect()));
nickIpPairList->insert(NickIpPair(QHostAddress((*payload)[1]), (*payload)[2]));
emit NicknamesChanged();
LogMessage("Advertizer accepted the connection.");
}
QByteArray ConnectionManager::CreateADZPacket()
{
QByteArray packet;
packet.append(LANCHATSIG);
char tmp[2];
_itoa(ADZ, tmp, 10);
packet.append(QString(tmp) + "\r\n");
packet.append(nickname + "\r\n");
packet.append(listenSocket->serverAddress().toString() + "\r\n");
return packet;
}
QByteArray ConnectionManager::CreateCONPacket()
{
QByteArray packet;
packet.append(LANCHATSIG);
char tmp[2];
_itoa(CON, tmp, 10);
packet.append(QString(tmp) + "\r\n");
_itoa(nickIpPairList->size()+1, tmp, 10);
packet.append(QString(tmp) + "\r\n");
//Adding our own IP/nick
packet.append(listenSocket->serverAddress().toString() + "\r\n");
packet.append(nickname + "\r\n");
//Adding other known IP/nicks
for(NickIpCIter it = nickIpPairList->cbegin();it != nickIpPairList->cend() ;++it)
{
//We add nick/ip pairs of all we know to new peer that we have connected to
packet.append(it->first.toString() + "\r\n");
packet.append(it->second + "\r\n");
}
return packet;
}
QByteArray ConnectionManager::CreateHSHPacket()
{
QByteArray packet;
packet.append(LANCHATSIG);
char tmp[2];
_itoa(HSH, tmp, 10);
packet.append(QString(tmp) + "\r\n");
packet.append(listenSocket->serverAddress().toString() + "\r\n");
packet.append(nickname + "\r\n");
return packet;
}
QByteArray ConnectionManager::CreateMSGPacket(QString message)
{
QByteArray packet;
packet.append(LANCHATSIG);
char tmp[2];
_itoa(MSG, tmp, 10);
packet.append(QString(tmp) + "\r\n");
//should also put own ip/nick pair here
packet.append(listenAddress.toString() + "\r\n");
packet.append(nickname + "\r\n");
packet.append(message.toUtf8() + "\r\n");
return packet;
}
ConnectionManager::PacketTypes ConnectionManager::DisassemblePacket(QByteArray *packet, QStringList *payloads)
{
int indexOfCRLF, numberOfPayloads;
int oldIndexOfCRLF = 7; //Passing "LANchat"
indexOfCRLF = packet->indexOf("\r\n", oldIndexOfCRLF);
QString packetType = packet->mid(oldIndexOfCRLF, indexOfCRLF - oldIndexOfCRLF);
payloads->append(packetType);
switch(packetType.toInt())
{
case ADZ:
numberOfPayloads=2;
break;
case CON:
oldIndexOfCRLF = indexOfCRLF;
indexOfCRLF = packet->indexOf("\r\n", oldIndexOfCRLF + 2);
payloads->append(packet->mid(oldIndexOfCRLF+2,indexOfCRLF-(oldIndexOfCRLF+2)));
numberOfPayloads = packet->mid(oldIndexOfCRLF+2,indexOfCRLF-(oldIndexOfCRLF+2)).toInt()*2;
break;
case HSH:
numberOfPayloads=2;
break;
case UNN:
oldIndexOfCRLF = indexOfCRLF;
indexOfCRLF = packet->indexOf("\r\n", oldIndexOfCRLF + 2);
numberOfPayloads = packet->mid(oldIndexOfCRLF+2,indexOfCRLF-(oldIndexOfCRLF+2)).toInt();
break;
case TDC:
//Someone's tail connection is dropped
break;
case MSG:
numberOfPayloads=3;
break;
}
for(int i=0; i < numberOfPayloads; ++i)
{
oldIndexOfCRLF = indexOfCRLF;
indexOfCRLF = packet->indexOf("\r\n", oldIndexOfCRLF + 2);
payloads->append(packet->mid(oldIndexOfCRLF+2,indexOfCRLF-(oldIndexOfCRLF+2)));
}
return static_cast<PacketTypes>(packetType.toInt());
}
void ConnectionManager::OnTcpIncommingDisconnect()
{
LogMessage("Failed to establish connection from " + incommingTcp->peerAddress().toString());
incommingTcp->deleteLater();
incommingTcp = nullptr;
}
void ConnectionManager::OnTcpOutgoingDisconnect()
{
LogMessage("Failed to connect to " + outgoingTcp->peerAddress().toString());
outgoingTcp->deleteLater();
outgoingTcp = nullptr;
}
void ConnectionManager::StopNetwork()
{
LogMessage("Disconnected.");
this->disconnect();
if(connectionTo != nullptr) delete connectionTo;
if(connectionFrom != nullptr) delete connectionFrom;
if(nickIpPairList != nullptr) delete nickIpPairList;
if(outgoingTcp != nullptr) delete outgoingTcp;
if(incommingTcp != nullptr) delete incommingTcp;
if(udpSocket != nullptr) delete udpSocket;
if(listenSocket != nullptr) delete listenSocket;
connectionTo = nullptr;
connectionFrom = nullptr;
nickIpPairList = nullptr;
outgoingTcp =nullptr;
incommingTcp=nullptr;
udpSocket=nullptr;
listenSocket= nullptr;
}
ConnectionManager::~ConnectionManager()
{
StopNetwork();
}