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

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

minor changes

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