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

Last change on this file since 10541 was 10227, checked in by stuerze, 14 months ago

minor changes

File size: 12.2 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;
[9795]81 // Generate filenames to consider a potential client certificate and private key
82 _crtFileName = settings.value("sslClientCertPath").toString() + _casterOutHost + QString(".%1.crt").arg(_casterOutPort);
83 _keyFileName = settings.value("sslClientCertPath").toString() + _casterOutHost + QString(".%1.key").arg(_casterOutPort);
[9714]84 }
85 }
86
[9715]87 if (!_secure && _proxy) {
88 _postExtension = QString("http://%1:%2").arg(_casterOutHost).arg(_casterOutPort);
89 } else {
90 _postExtension = "";
[9714]91 }
[3172]92}
93
[3207]94// Safe Desctructor
95////////////////////////////////////////////////////////////////////////////
96void bncUploadCaster::deleteSafely() {
97 _isToBeDeleted = true;
[3208]98 if (!isRunning()) {
99 delete this;
100 }
[3207]101}
102
[3172]103// Destructor
104////////////////////////////////////////////////////////////////////////////
105bncUploadCaster::~bncUploadCaster() {
[3208]106 if (isRunning()) {
107 wait();
108 }
[7661]109 if (_outSocket) {
110 delete _outSocket;
111 }
[3172]112}
113
[9707]114//
115////////////////////////////////////////////////////////////////////////////
116void bncUploadCaster::slotProxyAuthenticationRequired(const QNetworkProxy&,
[9715]117 QAuthenticator*) {
[9707]118 emit newMessage("slotProxyAuthenticationRequired", true);
119}
120
[9726]121// TSL/SSL
122 ////////////////////////////////////////////////////////////////////////////
123void bncUploadCaster::slotSslErrors(QList<QSslError> errors) {
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()) {
[9736]130 msg += QString("Server Certificate Issued by:\n" "%1\n%2\nCannot be verified\n")
[9726]131#if QT_VERSION >= 0x050000
132 .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName).at(0))
133 .arg(cert.issuerInfo(QSslCertificate::Organization).at(0));
134#else
135 .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName))
136 .arg(cert.issuerInfo(QSslCertificate::Organization));
137#endif
138 }
139
140 QListIterator<QSslError> it(errors);
141 while (it.hasNext()) {
142 const QSslError& err = it.next();
143 msg += err.errorString();
144 }
[9740]145 emit(newMessage(msg.toLatin1(), true));
[9726]146 }
147}
148
149
[3226]150// Endless Loop
151////////////////////////////////////////////////////////////////////////////
152void bncUploadCaster::run() {
153 while (true) {
154 if (_isToBeDeleted) {
155 QThread::quit();
156 deleteLater();
157 return;
158 }
159 open();
160 if (_outSocket && _outSocket->state() == QAbstractSocket::ConnectedState) {
161 QMutexLocker locker(&_mutex);
[4808]162 if (_outBuffer.size() > 0) {
[9707]163 if (_ntripVersion == "1") {
164 _outSocket->write(_outBuffer);
[10227]165 _outSocket->flush();
[9715]166 } else {
[9732]167 QString chunkSize = QString("%1").arg(_outBuffer.size(), 0, 16, QLatin1Char('0'));
168 QByteArray chunkedData = chunkSize.toLatin1() + "\r\n" + _outBuffer + "\r\n";
[9707]169 _outSocket->write(chunkedData);
[10227]170 _outSocket->flush();
[9707]171 }
[8204]172 emit newBytes(_mountpoint.toLatin1(), _outBuffer.size());
[4808]173 }
[3226]174 }
[4809]175 if (_rate == 0) {
[4985]176 {
177 QMutexLocker locker(&_mutex);
178 _outBuffer.clear();
179 }
[8708]180 msleep(100); //sleep 0.1 sec
[9715]181 } else {
[4809]182 sleep(_rate);
183 }
[3226]184 }
185}
186
[3172]187// Start the Communication with NTRIP Caster
188////////////////////////////////////////////////////////////////////////////
189void bncUploadCaster::open() {
[9715]190 const int timeOut = 5000; // 5 seconds
191 QByteArray msg;
[3172]192
193 if (_mountpoint.isEmpty()) {
194 return;
195 }
196
[9748]197 if (_outSocket != 0 &&
198 _outSocket->state() == QAbstractSocket::ConnectedState) {
199 return;
[3172]200 }
[9712]201
[9748]202 delete _outSocket; _outSocket = 0;
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
218 if (_sslIgnoreErrors) {
219 _outSocket->ignoreSslErrors();
220 } else {
[9795]221 bncSslConfig sslConfig = BNC_SSL_CONFIG;
222 QFile clientCrtFile(_crtFileName);
223 QFile privateKeyFile(_keyFileName);
224 if ( clientCrtFile.exists() && privateKeyFile.exists()) {
225 // set local certificate
226 clientCrtFile.open(QIODevice::ReadOnly);
227 QSslCertificate clientCrt(&clientCrtFile);
228 sslConfig.setLocalCertificate(clientCrt);
229 // set private key if available
230 privateKeyFile.open(QIODevice::ReadOnly);
231 QSslKey privateKey(&privateKeyFile, QSsl::Rsa);
232 sslConfig.setPrivateKey(privateKey);
233 }
234 _outSocket->setSslConfiguration(sslConfig);
[9741]235 connect(_outSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(slotSslErrors(QList<QSslError>)));
236 }
237
[9730]238 if (!_proxy) {
239 if (!connectToHost(_casterOutHost, _casterOutPort, _secure)) {
240 return;
241 }
242 } else {
[9715]243 if (_ntripVersion == "1") {
244 emit(newMessage("No proxy support in Ntrip Version 1 upload!", true));
[9748]245 delete _outSocket; _outSocket = 0;
[9713]246 return;
247 }
[9715]248 connect(_outSocket, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)),
249 this,SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));
250
251 if (!connectToHost(_proxyOutHost, _proxyOutPort, false)) {
252 return;
253 }
254
255 if (_secure) {
256 msg = "CONNECT " + _casterOutHost.toLatin1() + ":"
257 + QString("%1").arg(_casterOutPort).toLatin1() + " HTTP/1.1\r\n"
258 + "Proxy-Connection: Keep-Alive\r\n"
259 + "Host: " + _casterOutHost.toLatin1() + "\r\n"
260 + "User-Agent: NTRIP BNC/" BNCVERSION " (" + BNC_OS + ")\r\n"
[9743]261 + "\r\n";
[9715]262 _outSocket->write(msg);
263 _outSocket->waitForBytesWritten();
264 _outSocket->waitForReadyRead();
[9737]265
[9743]266 QByteArray ans = _outSocket->readAll();
[9715]267 if (ans.indexOf("200") == -1) {
[9743]268 int l = ans.indexOf("\r\n", 0);
[9851]269 emit(newMessage("Proxy: Connection broken for " + _mountpoint.toLatin1() + "@" +
270 _casterOutHost.toLatin1() + ":" + QString("%1").arg(_casterOutPort).toLatin1() + ": " + ans.left(l), true));
[9748]271 delete _outSocket; _outSocket = 0;
[9715]272 return;
273 } else {
[9853]274 emit(newMessage("Proxy: Connection established for " + _mountpoint.toLatin1()+ "@" +
275 _casterOutHost.toLatin1() + ":" + QString("%1").arg(_casterOutPort).toLatin1() , true));
[9715]276 _sOpenTrial = 0;
277 _outSocket->setPeerVerifyName(_casterOutHost);
278 _outSocket->startClientEncryption();
279 if (!_outSocket->waitForEncrypted(timeOut)) {
[9851]280 emit(newMessage("Proxy/Caster: Encrypt timeout for " + _mountpoint.toLatin1() + "@"
[9732]281 + _casterOutHost.toLatin1() + ":"
282 + QString("%1) ").arg(_casterOutPort).toLatin1()
283 + _outSocket->errorString().toLatin1(), true));
[9748]284 delete _outSocket; _outSocket = 0;
[9715]285 return;
286 } else {
[9851]287 emit(newMessage("Proxy: SSL handshake completed for " + _mountpoint.toLatin1() + "@" +
288 _casterOutHost.toLatin1() + ":" + QString("%1").arg(_casterOutPort).toLatin1(), true));
[9715]289 }
290 }
291 }
[9707]292 }
293
294 if (_ntripVersion == "1") {
[9715]295 msg = "SOURCE " + _password.toLatin1() + " /" + _mountpoint.toLatin1()
296 + "\r\n" + "Source-Agent: NTRIP BNC/" BNCVERSION "\r\n\r\n";
[10221]297 _outSocket->write(msg);
298 _outSocket->waitForBytesWritten();
299 _outSocket->waitForReadyRead();
[9715]300 } else {
301 msg = "POST " + _postExtension.toLatin1() + "/" + _mountpoint.toLatin1()
302 + " HTTP/1.1\r\n" + "Host: " + _casterOutHost.toLatin1() + "\r\n"
303 + "Ntrip-Version: Ntrip/2.0\r\n" + "Authorization: Basic "
304 + (_userName + ":" + _password).toLatin1().toBase64() + "\r\n"
305 + "User-Agent: NTRIP BNC/" BNCVERSION " (" + BNC_OS + ")\r\n"
306 + "Connection: close\r\n" + "Transfer-Encoding: chunked\r\n\r\n";
[10221]307 _outSocket->write(msg);
308 _outSocket->waitForBytesWritten();
309 _outSocket->waitForReadyRead();
[9707]310 }
[3172]311
[9743]312 QByteArray ans = _outSocket->readAll();
[9715]313
314 if (ans.indexOf("200") == -1) {
[9748]315 delete _outSocket; _outSocket = 0;
[9743]316 int l = ans.indexOf("\r\n", 0);
[9853]317 emit(newMessage("Broadcaster: Connection broken for " + _mountpoint.toLatin1() + "@" +
[9851]318 _casterOutHost.toLatin1() + ":" + QString("%1").arg(_casterOutPort).toLatin1() +
319 ": " + ans.left(l), true));
[9715]320 } else {
[9851]321 emit(newMessage("Broadcaster: Connection opened for " + _mountpoint.toLatin1() + "@" +
322 _casterOutHost.toLatin1() + ":" + QString("%1").arg(_casterOutPort).toLatin1() , true));
[3172]323 _sOpenTrial = 0;
324 }
325}
326
[9715]327// Try connection to NTRIP Caster or Proxy
328////////////////////////////////////////////////////////////////////////////
329bool bncUploadCaster::connectToHost(QString outHost, int outPort, bool encrypted) {
330 const int timeOut = 5000; // 5 seconds
331 if (encrypted) {
332 _outSocket->connectToHostEncrypted(outHost, outPort);
333 if (!_outSocket->waitForEncrypted(timeOut)) {
334 emit(newMessage(
[9851]335 "Broadcaster: Connect timeout for " + _mountpoint.toLatin1() + "@"
[9715]336 + outHost.toLatin1() + ":"
337 + QString("%1) ").arg(outPort).toLatin1()
338 + _outSocket->errorString().toLatin1(), true));
[9748]339 delete _outSocket; _outSocket = 0;
[9715]340 return false;
[9717]341 } else {
[9851]342 emit(newMessage("Broadcaster: SSL handshake completed for " + _mountpoint.toLatin1() + "@" +
343 _casterOutHost.toLatin1() + ":" + QString("%1").arg(_casterOutPort).toLatin1(), true));
[9715]344 }
345 } else {
346 _outSocket->connectToHost(outHost, outPort);
347 if (!_outSocket->waitForConnected(timeOut)) {
[9851]348 emit(newMessage("Broadcaster: Connect timeout for " + _mountpoint.toLatin1() + "@"
[9715]349 + outHost.toLatin1() + ":"
350 + QString("%1) ").arg(outPort).toLatin1()
351 + _outSocket->errorString().toLatin1(), true));
[9748]352 delete _outSocket; _outSocket = 0;
[9715]353 return false;
354 }
355 }
356 return true;
357}
358
359
Note: See TracBrowser for help on using the repository browser.