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

Last change on this file since 9724 was 9724, checked in by stuerze, 2 years ago

minor changes

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