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