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

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

minor changes

File size: 11.5 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(sslErrors(QList<QSslError>)), this, SLOT(slotSslErrors(QList<QSslError>)));
108 disconnect(_outSocket, SIGNAL(disconnected()), this, SLOT(slotSslSocketConnectionClosed()));
[7661]109 delete _outSocket;
110 }
[3172]111}
112
[9707]113//
114////////////////////////////////////////////////////////////////////////////
115void bncUploadCaster::slotProxyAuthenticationRequired(const QNetworkProxy&,
[9715]116 QAuthenticator*) {
[9707]117 emit newMessage("slotProxyAuthenticationRequired", true);
118}
119
[9726]120// TSL/SSL
121 ////////////////////////////////////////////////////////////////////////////
122void bncUploadCaster::slotSslErrors(QList<QSslError> errors) {
[9727]123 emit(newMessage("slotSslErrors", true));
[9726]124 QString msg = "SSL Error: ";
125 if (_outSocket) {
126 QSslCertificate cert = _outSocket->sslConfiguration().peerCertificate();
127 if (!cert.isNull() &&
128 cert.issuerInfo(QSslCertificate::OrganizationalUnitName).count() &&
129 cert.issuerInfo(QSslCertificate::Organization).count()) {
130 msg += QString("Server Certificate Issued by:\n"
131 "%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 emit(newMessage(msg.toLatin1(), true));
147
148 if (_sslIgnoreErrors) {
149 _outSocket->ignoreSslErrors();
150 emit(newMessage("BNC ignores SSL errors as configured", true));
151 }
152 }
153}
154
155
[3226]156// Endless Loop
157////////////////////////////////////////////////////////////////////////////
158void bncUploadCaster::run() {
159 while (true) {
160 if (_isToBeDeleted) {
161 QThread::quit();
162 deleteLater();
163 return;
164 }
165 open();
166 if (_outSocket && _outSocket->state() == QAbstractSocket::ConnectedState) {
167 QMutexLocker locker(&_mutex);
[4808]168 if (_outBuffer.size() > 0) {
[9707]169 if (_ntripVersion == "1") {
170 _outSocket->write(_outBuffer);
171 _outSocket->flush();
[9715]172 } else {
[9732]173 QString chunkSize = QString("%1").arg(_outBuffer.size(), 0, 16, QLatin1Char('0'));
174 QByteArray chunkedData = chunkSize.toLatin1() + "\r\n" + _outBuffer + "\r\n";
[9707]175 _outSocket->write(chunkedData);
176 _outSocket->flush();
177 }
[8204]178 emit newBytes(_mountpoint.toLatin1(), _outBuffer.size());
[4808]179 }
[3226]180 }
[4809]181 if (_rate == 0) {
[4985]182 {
183 QMutexLocker locker(&_mutex);
184 _outBuffer.clear();
185 }
[8708]186 msleep(100); //sleep 0.1 sec
[9715]187 } else {
[4809]188 sleep(_rate);
189 }
[3226]190 }
191}
192
[3172]193// Start the Communication with NTRIP Caster
194////////////////////////////////////////////////////////////////////////////
195void bncUploadCaster::open() {
[9715]196 const int timeOut = 5000; // 5 seconds
[9717]197 bncSslConfig sslConfig;
[9715]198 QByteArray msg;
[3172]199
200 if (_mountpoint.isEmpty()) {
201 return;
202 }
203
[9712]204 if (_outSocket != 0) {
205 if (_outSocket->state() == QAbstractSocket::ConnectedState) {
206 return;
207 }
[9731]208 _outSocket->close();
[3172]209 }
[9712]210
[9715]211 double minDt = pow(2.0, _sOpenTrial);
[3172]212 if (++_sOpenTrial > 4) {
213 _sOpenTrial = 4;
214 }
[9715]215 if (_outSocketOpenTime.isValid()
216 && _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
[3172]217 return;
[9715]218 } else {
[3172]219 _outSocketOpenTime = QDateTime::currentDateTime();
220 }
221
[9707]222 _outSocket = new QSslSocket();
[9728]223 _outSocket->setProxy(QNetworkProxy::NoProxy);
[9726]224 _outSocket->setSslConfiguration(sslConfig);
[9727]225 connect(_outSocket, SIGNAL(disconnected()), this, SLOT(slotSslSocketConnectionClosed()));
[9726]226 connect(_outSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(slotSslErrors(QList<QSslError>)));
[9715]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"
[9734]251 + "\r\n"; //cout << msg.toStdString().c_str();
[9715]252 _outSocket->write(msg);
253 _outSocket->waitForBytesWritten();
254 _outSocket->waitForReadyRead();
[9734]255 QByteArray ans = _outSocket->readAll(); //cout << ans.toStdString().c_str() << endl;
[9715]256 if (ans.indexOf("200") == -1) {
[9732]257 emit(newMessage("Proxy: Connection broken for " + _mountpoint.toLatin1()
258 + ": " + ans.left(ans.length() - 2), true));
[9729]259 _outSocket->close();
[9715]260 return;
261 } else {
[9732]262 emit(newMessage("Proxy: Connection established for " + _mountpoint.toLatin1(), true));
[9715]263 _sOpenTrial = 0;
264 _outSocket->setPeerVerifyName(_casterOutHost);
265 _outSocket->startClientEncryption();
266 if (!_outSocket->waitForEncrypted(timeOut)) {
[9732]267 emit(newMessage("Proxy/Caster: Encrypt timeout for " + _mountpoint.toLatin1() + " ("
268 + _casterOutHost.toLatin1() + ":"
269 + QString("%1) ").arg(_casterOutPort).toLatin1()
270 + _outSocket->errorString().toLatin1(), true));
[9715]271 return;
272 } else {
273 emit(newMessage("Proxy: SSL handshake completed for " + _mountpoint.toLatin1(), true));
274 }
275 }
276 }
[9707]277 }
278
279 if (_ntripVersion == "1") {
[9715]280 msg = "SOURCE " + _password.toLatin1() + " /" + _mountpoint.toLatin1()
281 + "\r\n" + "Source-Agent: NTRIP BNC/" BNCVERSION "\r\n\r\n";
282 } else {
283 msg = "POST " + _postExtension.toLatin1() + "/" + _mountpoint.toLatin1()
284 + " HTTP/1.1\r\n" + "Host: " + _casterOutHost.toLatin1() + "\r\n"
285 + "Ntrip-Version: Ntrip/2.0\r\n" + "Authorization: Basic "
286 + (_userName + ":" + _password).toLatin1().toBase64() + "\r\n"
287 + "User-Agent: NTRIP BNC/" BNCVERSION " (" + BNC_OS + ")\r\n"
288 + "Connection: close\r\n" + "Transfer-Encoding: chunked\r\n\r\n";
[9707]289 }
[9732]290 //cout << msg.toStdString().c_str();
[3172]291 _outSocket->write(msg);
292 _outSocket->waitForBytesWritten();
293 _outSocket->waitForReadyRead();
294
[9730]295 QByteArray ans = "";
296 if (_outSocket->canReadLine()) {
[9732]297 ans = _outSocket->readLine(); //cout << "ans: " << ans.toStdString().c_str() << endl;
[9730]298 }
[9715]299
300 if (ans.indexOf("200") == -1) {
[9729]301 _outSocket->close();
[9732]302 emit(newMessage("Broadcaster: Connection broken for " + _mountpoint.toLatin1() + ": "
303 + ans.left(ans.length() - 2), true));
[9715]304 } else {
[9732]305 emit(newMessage("Broadcaster: Connection opened for " + _mountpoint.toLatin1(), true));
[3172]306 _sOpenTrial = 0;
307 }
308}
309
[9715]310// Try connection to NTRIP Caster or Proxy
311////////////////////////////////////////////////////////////////////////////
312bool bncUploadCaster::connectToHost(QString outHost, int outPort, bool encrypted) {
313 const int timeOut = 5000; // 5 seconds
314 if (encrypted) {
315 _outSocket->connectToHostEncrypted(outHost, outPort);
316 if (!_outSocket->waitForEncrypted(timeOut)) {
317 emit(newMessage(
318 "Broadcaster: Connect timeout for " + _mountpoint.toLatin1() + " ("
319 + outHost.toLatin1() + ":"
320 + QString("%1) ").arg(outPort).toLatin1()
321 + _outSocket->errorString().toLatin1(), true));
322 return false;
[9717]323 } else {
324 emit(newMessage("Broadcaster: SSL handshake completed for " + _mountpoint.toLatin1(), true));
[9715]325 }
326 } else {
327 _outSocket->connectToHost(outHost, outPort);
328 if (!_outSocket->waitForConnected(timeOut)) {
329 emit(newMessage(
330 "Broadcaster: Connect timeout for " + _mountpoint.toLatin1() + " ("
331 + outHost.toLatin1() + ":"
332 + QString("%1) ").arg(outPort).toLatin1()
333 + _outSocket->errorString().toLatin1(), true));
334 return false;
335 }
336 }
337 return true;
338}
339
340
[9727]341void bncUploadCaster::slotSslSocketConnectionClosed() {
[9728]342 disconnect(_outSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(slotSslErrors(QList<QSslError>)));
[9727]343 emit(newMessage("slotSslSocketConnectionClosed", true));
[9731]344 if (_outSocket) {
345 delete _outSocket;
346 _outSocket = 0;
347 }
[9727]348}
[9715]349
350
351
352
Note: See TracBrowser for help on using the repository browser.