-
Notifications
You must be signed in to change notification settings - Fork 7
/
InVpn.hpp
99 lines (78 loc) · 2.3 KB
/
InVpn.hpp
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
#include <QObject>
#include <QSslKey>
#include <QSslCertificate>
#include <QSslSocket>
#include <QTimer>
#include <QPointer>
#include <QSettings>
#include "QTap.hpp"
#include "InVpnSslServer.hpp"
class InVpnNode;
struct invpn_route_info {
qint64 stamp;
QPointer<InVpnNode> peer;
};
class InVpn: public QObject {
Q_OBJECT;
public:
InVpn();
void connectTo(const QString &id, const QHostAddress &addr, quint16 port);
public slots:
void reloadSettings();
void quit();
void restart();
void packet(const QByteArray &src_hw, const QByteArray &dst_hw, const QByteArray &data);
bool isValid();
void accept(QSslSocket*);
void sslErrors(const QList<QSslError>&);
void socketReady();
void socketLost();
void socketError(QAbstractSocket::SocketError);
void announce();
void tryConnect();
void announcedRoute(const QByteArray &mac, InVpnNode *peer, qint64 stamp, const QHostAddress&, quint16 port, const QByteArray &pkt);
void cleanupRoutes();
void route(const QByteArray&); // route a 0x80 packet to appropriate node
void routeBroadcast(const QByteArray&); // route a 0x81 packet to appropriate nodes
void routeAdminBroadcast(const QByteArray&); // route a 0x02 packet to appropriate nodes & reply to it
void routeAdmin(const QByteArray&); // route a 0x03 packet
signals:
void broadcast(const QByteArray&);
private:
QTap *tap;
InVpnSslServer *server;
qint64 broadcastId();
qint64 bc_last_id;
QMap<QByteArray, InVpnNode*> nodes;
QMap<QByteArray, struct invpn_route_info> routes;
QSslKey ssl_key;
QSslCertificate ssl_cert;
QList<QSslCertificate> ssl_ca;
QByteArray mac;
QTimer announce_timer;
QTimer connect_timer;
QTimer route_timer;
// settings
QString config_file;
void parseCmdLine();
QString conf_cache_file;
QString conf_key_path;
QString conf_cert_path;
QString conf_ca_path;
QString conf_db_path;
QString conf_init_seed; // initial peer if none found
int conf_port;
int tap_fd_restore;
bool conf_no_incoming;
bool conf_no_relay;
QSettings *settings;
QSettings *cache;
};
// helper
static inline QString invpn_socket_name(const QAbstractSocket *s) {
QHostAddress h = s->peerAddress();
if (h.protocol() == QAbstractSocket::IPv4Protocol) {
return h.toString()+QString(":")+QString::number(s->peerPort());
}
return QString("[")+h.toString()+QString("]:")+QString::number(s->peerPort());
}