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