-
Notifications
You must be signed in to change notification settings - Fork 7
/
InVpnNode.cpp
161 lines (141 loc) · 3.88 KB
/
InVpnNode.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
#include "InVpnNode.hpp"
#include "InVpn.hpp"
#include <qendian.h>
InVpnNode::InVpnNode(InVpn *_parent, const QByteArray &_mac): QObject(_parent) {
parent = _parent;
mac = _mac;
link = NULL;
last_bcast = 0;
}
bool InVpnNode::setLink(QSslSocket *_link) {
if (link != NULL) return false;
link = _link;
readbuf.clear();
connect(link, SIGNAL(disconnected()), this, SLOT(socketLost()));
connect(link, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));
connect(link, SIGNAL(readyRead()), this, SLOT(socketRead()));
link->setSocketOption(QAbstractSocket::LowDelayOption, 1);
link->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
return true;
}
bool InVpnNode::checkStamp(qint64 id) {
if (id <= last_bcast) return false;
last_bcast = id;
return true;
}
const QByteArray &InVpnNode::getMac() const {
return mac;
}
void InVpnNode::socketRead() {
QSslSocket *s = qobject_cast<QSslSocket*>(sender());
if ((s!=link) || (link == NULL)) return;
readbuf += link->readAll();
while(true) {
if (readbuf.size() < 2) break;
quint16 len = *(quint16*)readbuf.constData();
len = qFromBigEndian(len);
if (readbuf.size() < len+2) break;
QByteArray pkt = readbuf.left(len+2);
readbuf.remove(0, len+2);
handlePacket(pkt);
}
}
void InVpnNode::handlePacket(const QByteArray&pkt) {
int type = (unsigned char)pkt.at(2);
switch(type) {
case 0x00:
// details on a directly connected node. Generate packet 0x01 from this
{
QByteArray new_pkt;
new_pkt.append((char)0x01);
new_pkt.append(pkt.mid(3));// version + id + mac + port
QHostAddress h = link->peerAddress();
switch(h.protocol()) {
case QAbstractSocket::IPv4Protocol:
{
new_pkt.append((char)0x01);
quint32 ip = qToBigEndian(h.toIPv4Address());
new_pkt.append((char*)&ip, 4);
}
break;
case QAbstractSocket::IPv6Protocol:
{
new_pkt.append((char)0x02);
Q_IPV6ADDR ip = h.toIPv6Address();
new_pkt.append((char*)&ip, 16);
}
break;
default:
new_pkt.append((char)0x00);
break;
}
// prepend length
quint16 len = new_pkt.size();
len = qToBigEndian(len);
new_pkt.prepend((char*)&len, 2);
qint64 stamp = qFromBigEndian(*(qint64*)pkt.mid(4, 8).constData());
quint16 port = qFromBigEndian(*(quint16*)(pkt.constData()+18));
parent->announcedRoute(pkt.mid(12, 6), this, stamp, h, port, new_pkt);
}
break;
case 0x01:
// details on a remote node
{
qint64 stamp = qFromBigEndian(*(qint64*)pkt.mid(4, 8).constData());
QHostAddress h;
quint16 port = qFromBigEndian(*(quint16*)(pkt.constData()+18));
int type = pkt.at(20);
switch(type) {
case 1:
{
quint32 ip = qFromBigEndian(*(quint32*)(pkt.constData()+21));
h.setAddress(ip);
}
break;
case 2:
{
Q_IPV6ADDR ip = *(Q_IPV6ADDR*)(pkt.constData()+21);
h.setAddress(ip);
}
break;
}
parent->announcedRoute(pkt.mid(12, 6), this, stamp, h, port, pkt);
}
break;
case 0x02:
// admin packet broadcast + traceroute
parent->routeAdminBroadcast(pkt);
break;
case 0x03:
// admin packet route
parent->routeAdmin(pkt);
break;
case 0x80:
// targetted packet
parent->route(pkt);
break;
case 0x81:
// broadcast - let the sender manage it
parent->routeBroadcast(pkt);
break;
}
}
void InVpnNode::socketLost() {
QSslSocket *s = qobject_cast<QSslSocket*>(sender());
if ((s!=link) || (link == NULL)) return;
link->deleteLater();
link = NULL;
}
void InVpnNode::socketError(QAbstractSocket::SocketError) {
QSslSocket *s = qobject_cast<QSslSocket*>(sender());
if ((s!=link) || (link == NULL)) return;
link->deleteLater();
link = NULL;
}
bool InVpnNode::isLinked() const {
return (link != NULL);
}
void InVpnNode::push(const QByteArray&msg) {
if (link == NULL) return;
link->write(msg);
}