-
Notifications
You must be signed in to change notification settings - Fork 1
/
connection.cpp
220 lines (177 loc) · 5.87 KB
/
connection.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
#include "connection.h"
#include "nntpproxy.h"
#include <QByteArray>
#include <QSslSocket>
#include <QSslKey>
#include <QSslCertificate>
#include <QFile>
#include <QAbstractSocket>
Connection::Connection(qintptr aSocketDescriptor, bool ssl, bool servSocket, const char * aClassName):
QObject(), iSocketDescriptor(aSocketDescriptor), isSsl(ssl),
isServerSocket(servSocket), iSocket(Q_NULLPTR), iOutputCon(Q_NULLPTR),
iLogPrefix(aClassName)
{
iLogPrefix.append("[").append(QString::number(iSocketDescriptor)).append("] ");
#ifdef LOG_CONSTRUCTORS
QTextStream &is = NntpProxy::acquireLog(iLogPrefix);
is << "Constructor, isSsl: " << isSsl
<< ", isServerSocket: " << isServerSocket;
NntpProxy::releaseLog();
#endif
}
Connection::~Connection(){
#ifdef LOG_CONSTRUCTORS
_log("Destructor (deleting iSocket)");
#endif
if (iSocket && iSocket->isOpen())
iSocket->disconnectFromHost();
delete iSocket;
}
bool Connection::startTcpConnection(const char* aHost, ushort aPort){
_log("Starting connection...");
if (isSsl) {
if (!createSslSocket())
return false;
} else
iSocket = new QTcpSocket();
// if server side socket we attach it to the socketDescriptor
if (isServerSocket){
// Attach the socket to the native descriptor
if(!iSocket->setSocketDescriptor(iSocketDescriptor)) {
QString err("Setting socket Descriptor: ");
err += iSocket->errorString();
#ifdef LOG_CONNECTION_ERRORS_BEFORE_EMIT_SIGNALS
_log(err);
#endif
emit socketError(err);
return false;
}
} else {
iSocket->connectToHost(aHost, aPort);
// we need to wait...
if(!iSocket->waitForConnected(5000)){
QString err("connecting to host: ");
err += aHost;
err += ":";
err += QString::number(aPort);
err += ". socket error: ";
err += iSocket->errorString();
#ifdef LOG_CONNECTION_ERRORS_BEFORE_EMIT_SIGNALS
_log(err);
#endif
emit socketError(err);
return false;
}
}
// connect socket and signal
// note - Qt::DirectConnection is used because it's multithreaded
// This makes the slot to be invoked immediately, when the signal is emitted.
qRegisterMetaType<QAbstractSocket::SocketError>("SocketError" );
connect(iSocket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(iSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(onErrors(QAbstractSocket::SocketError)), Qt::QueuedConnection);
if (isSsl){
QSslSocket *ssl_socket = static_cast<QSslSocket*>(iSocket);
if (isServerSocket)
ssl_socket->startServerEncryption();
else
ssl_socket->startClientEncryption();
#ifdef LOG_SSL_STEPS
_log("> Encryption handshake done");
#endif
}
// If Server socket send Hello Message and wait for requests
if (isServerSocket){
iSocket->write(Nntp::getResponse(201));
} else {
do {
iSocket->waitForReadyRead();
} while (!iSocket->canReadLine());
QByteArray lineArr = iSocket->readLine();
if(strncmp(lineArr.constData(), Nntp::getResponse(200), 3) != 0){
QString err("Reading welcome message. Should start with 200... Server message: ");
err += lineArr.constData();
#ifdef LOG_CONNECTION_ERRORS_BEFORE_EMIT_SIGNALS
_log(err);
#endif
emit socketError(err);
return false;
}
}
_log("> Client Connected");
emit connected();
return true;
}
void Connection::startAsyncRead(){
_log("startAsyncRead");
connect(iSocket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
}
void Connection::closeConnection(){
_log("closeConnection");
disconnect(iSocket, SIGNAL(readyRead()), this, SLOT(readyRead()));
}
bool Connection::createSslSocket(){
_log("SSL socket");
QSslSocket *ssl_socket = new QSslSocket();
iSocket = ssl_socket;
connect(iSocket, SIGNAL(encrypted()), this, SLOT(onEncryptedSocket()));
connect(iSocket, SIGNAL(sslErrors(QList<QSslError>)),
this, SLOT(onSslErrors(QList<QSslError>)));
ssl_socket->setProtocol(QSsl::TlsV1_2);
QByteArray key;
QFile KeyFile(cSslPrivateKey);
if(KeyFile.open(QIODevice::ReadOnly)) {
key = KeyFile.readAll();
KeyFile.close();
} else {
QString err("Opening SSL key: ");
err += KeyFile.errorString();
#ifdef LOG_CONNECTION_ERRORS_BEFORE_EMIT_SIGNALS
_log(err);
#endif
emit socketError(err);
return false;
}
QSslKey sslKey(key, QSsl::Rsa);
ssl_socket->setPrivateKey(sslKey);
// Load server ssl certificate from file
QByteArray cert;
QFile CertFile(cSslCertificate);
if(CertFile.open(QIODevice::ReadOnly)) {
cert = CertFile.readAll();
CertFile.close();
} else {
QString err("Opening SSL certificate: ");
err += CertFile.errorString();
#ifdef LOG_CONNECTION_ERRORS_BEFORE_EMIT_SIGNALS
_log(err);
#endif
emit socketError(err);
return false;
}
QSslCertificate sslCert(cert);
ssl_socket->setLocalCertificate(sslCert);
return true;
}
void Connection::onEncryptedSocket(){
#ifdef LOG_SSL_STEPS
_log("> Socket Encrypted");
#endif
}
void Connection::onSslErrors(const QList<QSslError> &errors){
QString err("Error SSL Socket: ");
for(int i=0;i<errors.size();i++)
err += errors[i].errorString() + "\n";
#ifdef LOG_CONNECTION_ERRORS_BEFORE_EMIT_SIGNALS
_log(err);
#endif
emit socketError(err);
}
void Connection::onErrors(QAbstractSocket::SocketError) {
QString err("Error Socket: ");
err += iSocket->errorString();
#ifdef LOG_CONNECTION_ERRORS_BEFORE_EMIT_SIGNALS
_log(err);
#endif
emit socketError(err);
}