[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 |
|
---|
| 25 | using namespace std;
|
---|
| 26 |
|
---|
| 27 | // Constructor
|
---|
| 28 | ////////////////////////////////////////////////////////////////////////////
|
---|
[9715] | 29 | bncUploadCaster::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 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 96 | void bncUploadCaster::deleteSafely() {
|
---|
| 97 | _isToBeDeleted = true;
|
---|
[3208] | 98 | if (!isRunning()) {
|
---|
| 99 | delete this;
|
---|
| 100 | }
|
---|
[3207] | 101 | }
|
---|
| 102 |
|
---|
[3172] | 103 | // Destructor
|
---|
| 104 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 105 | bncUploadCaster::~bncUploadCaster() {
|
---|
[3208] | 106 | if (isRunning()) {
|
---|
| 107 | wait();
|
---|
| 108 | }
|
---|
[7661] | 109 | if (_outSocket) {
|
---|
| 110 | delete _outSocket;
|
---|
| 111 | }
|
---|
[3172] | 112 | }
|
---|
| 113 |
|
---|
[9707] | 114 | //
|
---|
| 115 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 116 | void bncUploadCaster::slotProxyAuthenticationRequired(const QNetworkProxy&,
|
---|
[9715] | 117 | QAuthenticator*) {
|
---|
[9707] | 118 | emit newMessage("slotProxyAuthenticationRequired", true);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[9726] | 121 | // TSL/SSL
|
---|
| 122 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 123 | void 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 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 152 | void 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);
|
---|
[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 | }
|
---|
[9757] | 170 | _outSocket->flush();
|
---|
[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 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 188 | void bncUploadCaster::open() {
|
---|
[9715] | 189 | const int timeOut = 5000; // 5 seconds
|
---|
| 190 | QByteArray msg;
|
---|
[3172] | 191 |
|
---|
| 192 | if (_mountpoint.isEmpty()) {
|
---|
| 193 | return;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[9748] | 196 | if (_outSocket != 0 &&
|
---|
| 197 | _outSocket->state() == QAbstractSocket::ConnectedState) {
|
---|
| 198 | return;
|
---|
[3172] | 199 | }
|
---|
[9712] | 200 |
|
---|
[9748] | 201 | delete _outSocket; _outSocket = 0;
|
---|
| 202 |
|
---|
[9715] | 203 | double minDt = pow(2.0, _sOpenTrial);
|
---|
[3172] | 204 | if (++_sOpenTrial > 4) {
|
---|
| 205 | _sOpenTrial = 4;
|
---|
| 206 | }
|
---|
[9715] | 207 | if (_outSocketOpenTime.isValid()
|
---|
| 208 | && _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
|
---|
[3172] | 209 | return;
|
---|
[9715] | 210 | } else {
|
---|
[3172] | 211 | _outSocketOpenTime = QDateTime::currentDateTime();
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[9707] | 214 | _outSocket = new QSslSocket();
|
---|
[9728] | 215 | _outSocket->setProxy(QNetworkProxy::NoProxy);
|
---|
[9741] | 216 |
|
---|
| 217 | if (_sslIgnoreErrors) {
|
---|
| 218 | _outSocket->ignoreSslErrors();
|
---|
| 219 | } else {
|
---|
[9795] | 220 | bncSslConfig sslConfig = BNC_SSL_CONFIG;
|
---|
| 221 | QFile clientCrtFile(_crtFileName);
|
---|
| 222 | QFile privateKeyFile(_keyFileName);
|
---|
| 223 | if ( clientCrtFile.exists() && privateKeyFile.exists()) {
|
---|
| 224 | // set local certificate
|
---|
| 225 | clientCrtFile.open(QIODevice::ReadOnly);
|
---|
| 226 | QSslCertificate clientCrt(&clientCrtFile);
|
---|
| 227 | sslConfig.setLocalCertificate(clientCrt);
|
---|
| 228 | // set private key if available
|
---|
| 229 | privateKeyFile.open(QIODevice::ReadOnly);
|
---|
| 230 | QSslKey privateKey(&privateKeyFile, QSsl::Rsa);
|
---|
| 231 | sslConfig.setPrivateKey(privateKey);
|
---|
| 232 | }
|
---|
| 233 | _outSocket->setSslConfiguration(sslConfig);
|
---|
[9741] | 234 | connect(_outSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(slotSslErrors(QList<QSslError>)));
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[9730] | 237 | if (!_proxy) {
|
---|
| 238 | if (!connectToHost(_casterOutHost, _casterOutPort, _secure)) {
|
---|
| 239 | return;
|
---|
| 240 | }
|
---|
| 241 | } else {
|
---|
[9715] | 242 | if (_ntripVersion == "1") {
|
---|
| 243 | emit(newMessage("No proxy support in Ntrip Version 1 upload!", true));
|
---|
[9748] | 244 | delete _outSocket; _outSocket = 0;
|
---|
[9713] | 245 | return;
|
---|
| 246 | }
|
---|
[9715] | 247 | connect(_outSocket, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)),
|
---|
| 248 | this,SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));
|
---|
| 249 |
|
---|
| 250 | if (!connectToHost(_proxyOutHost, _proxyOutPort, false)) {
|
---|
| 251 | return;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | if (_secure) {
|
---|
| 255 | msg = "CONNECT " + _casterOutHost.toLatin1() + ":"
|
---|
| 256 | + QString("%1").arg(_casterOutPort).toLatin1() + " HTTP/1.1\r\n"
|
---|
| 257 | + "Proxy-Connection: Keep-Alive\r\n"
|
---|
| 258 | + "Host: " + _casterOutHost.toLatin1() + "\r\n"
|
---|
| 259 | + "User-Agent: NTRIP BNC/" BNCVERSION " (" + BNC_OS + ")\r\n"
|
---|
[9743] | 260 | + "\r\n";
|
---|
[9715] | 261 | _outSocket->write(msg);
|
---|
| 262 | _outSocket->waitForBytesWritten();
|
---|
| 263 | _outSocket->waitForReadyRead();
|
---|
[9737] | 264 |
|
---|
[9743] | 265 | QByteArray ans = _outSocket->readAll();
|
---|
[9715] | 266 | if (ans.indexOf("200") == -1) {
|
---|
[9743] | 267 | int l = ans.indexOf("\r\n", 0);
|
---|
[9851] | 268 | emit(newMessage("Proxy: Connection broken for " + _mountpoint.toLatin1() + "@" +
|
---|
| 269 | _casterOutHost.toLatin1() + ":" + QString("%1").arg(_casterOutPort).toLatin1() + ": " + ans.left(l), true));
|
---|
[9748] | 270 | delete _outSocket; _outSocket = 0;
|
---|
[9715] | 271 | return;
|
---|
| 272 | } else {
|
---|
[9853] | 273 | emit(newMessage("Proxy: Connection established for " + _mountpoint.toLatin1()+ "@" +
|
---|
| 274 | _casterOutHost.toLatin1() + ":" + QString("%1").arg(_casterOutPort).toLatin1() , true));
|
---|
[9715] | 275 | _sOpenTrial = 0;
|
---|
| 276 | _outSocket->setPeerVerifyName(_casterOutHost);
|
---|
| 277 | _outSocket->startClientEncryption();
|
---|
| 278 | if (!_outSocket->waitForEncrypted(timeOut)) {
|
---|
[9851] | 279 | emit(newMessage("Proxy/Caster: Encrypt timeout for " + _mountpoint.toLatin1() + "@"
|
---|
[9732] | 280 | + _casterOutHost.toLatin1() + ":"
|
---|
| 281 | + QString("%1) ").arg(_casterOutPort).toLatin1()
|
---|
| 282 | + _outSocket->errorString().toLatin1(), true));
|
---|
[9748] | 283 | delete _outSocket; _outSocket = 0;
|
---|
[9715] | 284 | return;
|
---|
| 285 | } else {
|
---|
[9851] | 286 | emit(newMessage("Proxy: SSL handshake completed for " + _mountpoint.toLatin1() + "@" +
|
---|
| 287 | _casterOutHost.toLatin1() + ":" + QString("%1").arg(_casterOutPort).toLatin1(), true));
|
---|
[9715] | 288 | }
|
---|
| 289 | }
|
---|
| 290 | }
|
---|
[9707] | 291 | }
|
---|
| 292 |
|
---|
| 293 | if (_ntripVersion == "1") {
|
---|
[9715] | 294 | msg = "SOURCE " + _password.toLatin1() + " /" + _mountpoint.toLatin1()
|
---|
| 295 | + "\r\n" + "Source-Agent: NTRIP BNC/" BNCVERSION "\r\n\r\n";
|
---|
| 296 | } else {
|
---|
| 297 | msg = "POST " + _postExtension.toLatin1() + "/" + _mountpoint.toLatin1()
|
---|
| 298 | + " HTTP/1.1\r\n" + "Host: " + _casterOutHost.toLatin1() + "\r\n"
|
---|
| 299 | + "Ntrip-Version: Ntrip/2.0\r\n" + "Authorization: Basic "
|
---|
| 300 | + (_userName + ":" + _password).toLatin1().toBase64() + "\r\n"
|
---|
| 301 | + "User-Agent: NTRIP BNC/" BNCVERSION " (" + BNC_OS + ")\r\n"
|
---|
| 302 | + "Connection: close\r\n" + "Transfer-Encoding: chunked\r\n\r\n";
|
---|
[9707] | 303 | }
|
---|
[3172] | 304 | _outSocket->write(msg);
|
---|
| 305 | _outSocket->waitForBytesWritten();
|
---|
| 306 | _outSocket->waitForReadyRead();
|
---|
| 307 |
|
---|
[9743] | 308 | QByteArray ans = _outSocket->readAll();
|
---|
[9715] | 309 |
|
---|
| 310 | if (ans.indexOf("200") == -1) {
|
---|
[9748] | 311 | delete _outSocket; _outSocket = 0;
|
---|
[9743] | 312 | int l = ans.indexOf("\r\n", 0);
|
---|
[9853] | 313 | emit(newMessage("Broadcaster: Connection broken for " + _mountpoint.toLatin1() + "@" +
|
---|
[9851] | 314 | _casterOutHost.toLatin1() + ":" + QString("%1").arg(_casterOutPort).toLatin1() +
|
---|
| 315 | ": " + ans.left(l), true));
|
---|
[9715] | 316 | } else {
|
---|
[9851] | 317 | emit(newMessage("Broadcaster: Connection opened for " + _mountpoint.toLatin1() + "@" +
|
---|
| 318 | _casterOutHost.toLatin1() + ":" + QString("%1").arg(_casterOutPort).toLatin1() , true));
|
---|
[3172] | 319 | _sOpenTrial = 0;
|
---|
| 320 | }
|
---|
| 321 | }
|
---|
| 322 |
|
---|
[9715] | 323 | // Try connection to NTRIP Caster or Proxy
|
---|
| 324 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 325 | bool bncUploadCaster::connectToHost(QString outHost, int outPort, bool encrypted) {
|
---|
| 326 | const int timeOut = 5000; // 5 seconds
|
---|
| 327 | if (encrypted) {
|
---|
| 328 | _outSocket->connectToHostEncrypted(outHost, outPort);
|
---|
| 329 | if (!_outSocket->waitForEncrypted(timeOut)) {
|
---|
| 330 | emit(newMessage(
|
---|
[9851] | 331 | "Broadcaster: Connect timeout for " + _mountpoint.toLatin1() + "@"
|
---|
[9715] | 332 | + outHost.toLatin1() + ":"
|
---|
| 333 | + QString("%1) ").arg(outPort).toLatin1()
|
---|
| 334 | + _outSocket->errorString().toLatin1(), true));
|
---|
[9748] | 335 | delete _outSocket; _outSocket = 0;
|
---|
[9715] | 336 | return false;
|
---|
[9717] | 337 | } else {
|
---|
[9851] | 338 | emit(newMessage("Broadcaster: SSL handshake completed for " + _mountpoint.toLatin1() + "@" +
|
---|
| 339 | _casterOutHost.toLatin1() + ":" + QString("%1").arg(_casterOutPort).toLatin1(), true));
|
---|
[9715] | 340 | }
|
---|
| 341 | } else {
|
---|
| 342 | _outSocket->connectToHost(outHost, outPort);
|
---|
| 343 | if (!_outSocket->waitForConnected(timeOut)) {
|
---|
[9851] | 344 | emit(newMessage("Broadcaster: Connect timeout for " + _mountpoint.toLatin1() + "@"
|
---|
[9715] | 345 | + outHost.toLatin1() + ":"
|
---|
| 346 | + QString("%1) ").arg(outPort).toLatin1()
|
---|
| 347 | + _outSocket->errorString().toLatin1(), true));
|
---|
[9748] | 348 | delete _outSocket; _outSocket = 0;
|
---|
[9715] | 349 | return false;
|
---|
| 350 | }
|
---|
| 351 | }
|
---|
| 352 | return true;
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 |
|
---|