source: ntrip/trunk/BNC/src/upload/bncuploadcaster.cpp@ 9737

Last change on this file since 9737 was 9737, checked in by stuerze, 23 months ago

minor changes

File size: 11.5 KB
Line 
1/* -------------------------------------------------------------------------
2 * BKG NTRIP Server
3 * -------------------------------------------------------------------------
4 *
5 * Class: bncUploadCaster
6 *
7 * Purpose: Connection to NTRIP Caster
8 *
9 * Author: L. Mervart
10 *
11 * Created: 29-Mar-2011
12 *
13 * Changes:
14 *
15 * -----------------------------------------------------------------------*/
16
17#include <math.h>
18#include "bncuploadcaster.h"
19#include "bncversion.h"
20#include "bnccore.h"
21#include "bnctableitem.h"
22#include "bncsettings.h"
23#include "bncsslconfig.h"
24
25using namespace std;
26
27// Constructor
28////////////////////////////////////////////////////////////////////////////
29bncUploadCaster::bncUploadCaster(const QString &mountpoint,
30 const QString &outHost, int outPort, const QString &ntripVersion,
31 const QString &userName, const QString &password, int iRow, int rate) {
32 bncSettings settings;
33
34 _mountpoint = mountpoint;
35 _casterOutHost = outHost;
36 _casterOutPort = outPort;
37 _ntripVersion = ntripVersion;
38 _userName = userName;
39 _password = password;
40 _outSocket = 0;
41 _sOpenTrial = 0;
42 _iRow = iRow;
43 _rate = rate;
44
45 if (_rate < 0) {
46 _rate = 0;
47 } else if (_rate > 60) {
48 _rate = 60;
49 }
50 _isToBeDeleted = false;
51
52 connect(this, SIGNAL(newMessage(QByteArray,bool)), BNC_CORE, SLOT(slotMessage(const QByteArray,bool)));
53
54 if (BNC_CORE->_uploadTableItems.find(_iRow) != BNC_CORE->_uploadTableItems.end()) {
55 connect(this, SIGNAL(newBytes(QByteArray,double)),
56 BNC_CORE->_uploadTableItems.value(iRow),
57 SLOT(slotNewBytes(const QByteArray,double)));
58 }
59 if (BNC_CORE->_uploadEphTableItems.find(_iRow) != BNC_CORE->_uploadEphTableItems.end()) {
60 connect(this, SIGNAL(newBytes(QByteArray,double)),
61 BNC_CORE->_uploadEphTableItems.value(iRow),
62 SLOT(slotNewBytes(const QByteArray,double)));
63 }
64
65 _sslIgnoreErrors = (Qt::CheckState(settings.value("sslIgnoreErrors").toInt()) == Qt::Checked);
66
67 qRegisterMetaType<QList<QSslError> >("QList<QSslError>");
68
69 _proxyOutHost = settings.value("proxyHost").toString();
70 _proxyOutPort = settings.value("proxyPort").toInt();
71 (_proxyOutHost.isEmpty()) ? _proxy = false : _proxy = true;
72
73 _secure = false;
74 if (_ntripVersion == "2s") {
75 if (!QSslSocket::supportsSsl()) {
76 emit(newMessage(
77 "For SSL support please install OpenSSL run-time libraries: Ntrip Version 2 is tried",
78 true));
79 _ntripVersion == "2";
80 } else {
81 _secure = true;
82 _casterOutPort = 443;
83 }
84 }
85
86 if (!_secure && _proxy) {
87 _postExtension = QString("http://%1:%2").arg(_casterOutHost).arg(_casterOutPort);
88 } else {
89 _postExtension = "";
90 }
91}
92
93// Safe Desctructor
94////////////////////////////////////////////////////////////////////////////
95void bncUploadCaster::deleteSafely() {
96 _isToBeDeleted = true;
97 if (!isRunning()) {
98 delete this;
99 }
100}
101
102// Destructor
103////////////////////////////////////////////////////////////////////////////
104bncUploadCaster::~bncUploadCaster() {
105 if (isRunning()) {
106 wait();
107 }
108 if (_outSocket) {
109 disconnect(_outSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(slotSslErrors(QList<QSslError>)));
110 disconnect(_outSocket, SIGNAL(disconnected()), this, SLOT(slotSslSocketConnectionClosed()));
111 delete _outSocket;
112 }
113}
114
115//
116////////////////////////////////////////////////////////////////////////////
117void bncUploadCaster::slotProxyAuthenticationRequired(const QNetworkProxy&,
118 QAuthenticator*) {
119 emit newMessage("slotProxyAuthenticationRequired", true);
120}
121
122// TSL/SSL
123 ////////////////////////////////////////////////////////////////////////////
124void bncUploadCaster::slotSslErrors(QList<QSslError> errors) {
125 QString msg = "SSL Error: ";
126 if (_outSocket) {
127 QSslCertificate cert = _outSocket->sslConfiguration().peerCertificate();
128 if (!cert.isNull() &&
129 cert.issuerInfo(QSslCertificate::OrganizationalUnitName).count() &&
130 cert.issuerInfo(QSslCertificate::Organization).count()) {
131 msg += QString("Server Certificate Issued by:\n" "%1\n%2\nCannot be verified\n")
132#if QT_VERSION >= 0x050000
133 .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName).at(0))
134 .arg(cert.issuerInfo(QSslCertificate::Organization).at(0));
135#else
136 .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName))
137 .arg(cert.issuerInfo(QSslCertificate::Organization));
138#endif
139 }
140
141 QListIterator<QSslError> it(errors);
142 while (it.hasNext()) {
143 const QSslError& err = it.next();
144 msg += err.errorString();
145 }
146
147 if (_sslIgnoreErrors) {
148 _outSocket->ignoreSslErrors();
149 emit(newMessage("BNC ignores SSL errors as configured", true));
150 } else {
151 emit(newMessage(msg.toLatin1(), true));
152 }
153 }
154}
155
156
157// Endless Loop
158////////////////////////////////////////////////////////////////////////////
159void bncUploadCaster::run() {
160 while (true) {
161 if (_isToBeDeleted) {
162 QThread::quit();
163 deleteLater();
164 return;
165 }
166 open();
167 if (_outSocket && _outSocket->state() == QAbstractSocket::ConnectedState) {
168 QMutexLocker locker(&_mutex);
169 if (_outBuffer.size() > 0) {
170 if (_ntripVersion == "1") {
171 _outSocket->write(_outBuffer);
172 _outSocket->flush();
173 } else {
174 QString chunkSize = QString("%1").arg(_outBuffer.size(), 0, 16, QLatin1Char('0'));
175 QByteArray chunkedData = chunkSize.toLatin1() + "\r\n" + _outBuffer + "\r\n";
176 _outSocket->write(chunkedData);
177 _outSocket->flush();
178 }
179 emit newBytes(_mountpoint.toLatin1(), _outBuffer.size());
180 }
181 }
182 if (_rate == 0) {
183 {
184 QMutexLocker locker(&_mutex);
185 _outBuffer.clear();
186 }
187 msleep(100); //sleep 0.1 sec
188 } else {
189 sleep(_rate);
190 }
191 }
192}
193
194// Start the Communication with NTRIP Caster
195////////////////////////////////////////////////////////////////////////////
196void bncUploadCaster::open() {
197 const int timeOut = 5000; // 5 seconds
198 bncSslConfig sslConfig;
199 QByteArray msg;
200
201 if (_mountpoint.isEmpty()) {
202 return;
203 }
204
205 if (_outSocket != 0) {
206 if (_outSocket->state() == QAbstractSocket::ConnectedState) {
207 return;
208 }
209 _outSocket->close();
210 }
211
212 double minDt = pow(2.0, _sOpenTrial);
213 if (++_sOpenTrial > 4) {
214 _sOpenTrial = 4;
215 }
216 if (_outSocketOpenTime.isValid()
217 && _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
218 return;
219 } else {
220 _outSocketOpenTime = QDateTime::currentDateTime();
221 }
222
223 _outSocket = new QSslSocket();
224 _outSocket->setProxy(QNetworkProxy::NoProxy);
225 _outSocket->setSslConfiguration(sslConfig);
226 connect(_outSocket, SIGNAL(disconnected()), this, SLOT(slotSslSocketConnectionClosed()));
227 connect(_outSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(slotSslErrors(QList<QSslError>)));
228
229 if (!_proxy) {
230 if (!connectToHost(_casterOutHost, _casterOutPort, _secure)) {
231 return;
232 }
233 } else {
234 if (_ntripVersion == "1") {
235 emit(newMessage("No proxy support in Ntrip Version 1 upload!", true));
236 _outSocket->close();
237 return;
238 }
239 connect(_outSocket, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)),
240 this,SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));
241
242 if (!connectToHost(_proxyOutHost, _proxyOutPort, false)) {
243 return;
244 }
245
246 if (_secure) {
247 msg = "CONNECT " + _casterOutHost.toLatin1() + ":"
248 + QString("%1").arg(_casterOutPort).toLatin1() + " HTTP/1.1\r\n"
249 + "Proxy-Connection: Keep-Alive\r\n"
250 + "Host: " + _casterOutHost.toLatin1() + "\r\n"
251 + "User-Agent: NTRIP BNC/" BNCVERSION " (" + BNC_OS + ")\r\n"
252 + "\r\n"; //cout << msg.toStdString().c_str();
253 _outSocket->write(msg);
254 _outSocket->waitForBytesWritten();
255 _outSocket->waitForReadyRead();
256
257 QByteArray ans = _outSocket->readAll(); //cout << ans.toStdString().c_str() << endl;
258 if (ans.indexOf("200") == -1) {
259 emit(newMessage("Proxy: Connection broken for " + _mountpoint.toLatin1()
260 + ": " + ans.left(ans.length() - 2), true));
261 _outSocket->close();
262 return;
263 } else {
264 emit(newMessage("Proxy: Connection established for " + _mountpoint.toLatin1(), true));
265 _sOpenTrial = 0;
266 _outSocket->setPeerVerifyName(_casterOutHost);
267 _outSocket->startClientEncryption();
268 if (!_outSocket->waitForEncrypted(timeOut)) {
269 emit(newMessage("Proxy/Caster: Encrypt timeout for " + _mountpoint.toLatin1() + " ("
270 + _casterOutHost.toLatin1() + ":"
271 + QString("%1) ").arg(_casterOutPort).toLatin1()
272 + _outSocket->errorString().toLatin1(), true));
273 return;
274 } else {
275 emit(newMessage("Proxy: SSL handshake completed for " + _mountpoint.toLatin1(), true));
276 }
277 }
278 }
279 }
280
281 if (_ntripVersion == "1") {
282 msg = "SOURCE " + _password.toLatin1() + " /" + _mountpoint.toLatin1()
283 + "\r\n" + "Source-Agent: NTRIP BNC/" BNCVERSION "\r\n\r\n";
284 } else {
285 msg = "POST " + _postExtension.toLatin1() + "/" + _mountpoint.toLatin1()
286 + " HTTP/1.1\r\n" + "Host: " + _casterOutHost.toLatin1() + "\r\n"
287 + "Ntrip-Version: Ntrip/2.0\r\n" + "Authorization: Basic "
288 + (_userName + ":" + _password).toLatin1().toBase64() + "\r\n"
289 + "User-Agent: NTRIP BNC/" BNCVERSION " (" + BNC_OS + ")\r\n"
290 + "Connection: close\r\n" + "Transfer-Encoding: chunked\r\n\r\n";
291 }
292 //cout << msg.toStdString().c_str();
293 _outSocket->write(msg);
294 _outSocket->waitForBytesWritten();
295 _outSocket->waitForReadyRead();
296
297 QByteArray ans = _outSocket->readAll(); //cout << "ans: " << ans.toStdString().c_str() << endl;
298
299 if (ans.indexOf("200") == -1) {
300 _outSocket->close();
301 emit(newMessage("Broadcaster: Connection broken for " + _mountpoint.toLatin1() + ": "
302 + ans.left(ans.length() - 2), true));
303 } else {
304 emit(newMessage("Broadcaster: Connection opened for " + _mountpoint.toLatin1(), true));
305 _sOpenTrial = 0;
306 }
307}
308
309// Try connection to NTRIP Caster or Proxy
310////////////////////////////////////////////////////////////////////////////
311bool bncUploadCaster::connectToHost(QString outHost, int outPort, bool encrypted) {
312 const int timeOut = 5000; // 5 seconds
313 if (encrypted) {
314 _outSocket->connectToHostEncrypted(outHost, outPort);
315 if (!_outSocket->waitForEncrypted(timeOut)) {
316 emit(newMessage(
317 "Broadcaster: Connect timeout for " + _mountpoint.toLatin1() + " ("
318 + outHost.toLatin1() + ":"
319 + QString("%1) ").arg(outPort).toLatin1()
320 + _outSocket->errorString().toLatin1(), true));
321 return false;
322 } else {
323 emit(newMessage("Broadcaster: SSL handshake completed for " + _mountpoint.toLatin1(), true));
324 }
325 } else {
326 _outSocket->connectToHost(outHost, outPort);
327 if (!_outSocket->waitForConnected(timeOut)) {
328 emit(newMessage(
329 "Broadcaster: Connect timeout for " + _mountpoint.toLatin1() + " ("
330 + outHost.toLatin1() + ":"
331 + QString("%1) ").arg(outPort).toLatin1()
332 + _outSocket->errorString().toLatin1(), true));
333 return false;
334 }
335 }
336 return true;
337}
338
339
340void bncUploadCaster::slotSslSocketConnectionClosed() {
341 disconnect(_outSocket, SIGNAL(disconnected()), this, SLOT(slotSslSocketConnectionClosed()));
342 disconnect(_outSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(slotSslErrors(QList<QSslError>)));
343 emit(newMessage("slotSslSocketConnectionClosed", true));
344 if (_outSocket) {
345 delete _outSocket;
346 _outSocket = 0;
347 }
348}
349
350
351
352
Note: See TracBrowser for help on using the repository browser.