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