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