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