| [1378] | 1 | /* -------------------------------------------------------------------------
|
|---|
| 2 | * BKG NTRIP Client
|
|---|
| 3 | * -------------------------------------------------------------------------
|
|---|
| 4 | *
|
|---|
| 5 | * Class: bncNetQueryV2
|
|---|
| 6 | *
|
|---|
| 7 | * Purpose: Blocking Network Requests (NTRIP Version 2)
|
|---|
| 8 | *
|
|---|
| 9 | * Author: L. Mervart
|
|---|
| 10 | *
|
|---|
| 11 | * Created: 27-Dec-2008
|
|---|
| 12 | *
|
|---|
| [7612] | 13 | * Changes:
|
|---|
| [1378] | 14 | *
|
|---|
| 15 | * -----------------------------------------------------------------------*/
|
|---|
| 16 |
|
|---|
| [1583] | 17 | #include <iostream>
|
|---|
| 18 |
|
|---|
| [1379] | 19 | #include "bncnetqueryv2.h"
|
|---|
| [1535] | 20 | #include "bncsettings.h"
|
|---|
| [2011] | 21 | #include "bncversion.h"
|
|---|
| [3349] | 22 | #include "bncsslconfig.h"
|
|---|
| [3359] | 23 | #include "bncsettings.h"
|
|---|
| [1378] | 24 |
|
|---|
| 25 | // Constructor
|
|---|
| 26 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| [3337] | 27 | bncNetQueryV2::bncNetQueryV2(bool secure) {
|
|---|
| 28 | _secure = secure;
|
|---|
| [1378] | 29 | _manager = new QNetworkAccessManager(this);
|
|---|
| [9706] | 30 | connect(_manager, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)),
|
|---|
| 31 | this, SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));
|
|---|
| [1378] | 32 | _reply = 0;
|
|---|
| 33 | _eventLoop = new QEventLoop(this);
|
|---|
| [1583] | 34 | _firstData = true;
|
|---|
| [1378] | 35 | _status = init;
|
|---|
| [3359] | 36 |
|
|---|
| 37 | bncSettings settings;
|
|---|
| [9795] | 38 | _sslIgnoreErrors = (Qt::CheckState(settings.value("sslIgnoreErrors").toInt()) == Qt::Checked);
|
|---|
| [3359] | 39 |
|
|---|
| [9795] | 40 | if (_secure ) {
|
|---|
| 41 | if (!QSslSocket::supportsSsl()) {
|
|---|
| 42 | BNC_CORE->slotMessage("No SSL support, install OpenSSL run-time libraries", true);
|
|---|
| 43 | stop();
|
|---|
| 44 | }
|
|---|
| 45 | // Generate filenames to consider a potential client certificate
|
|---|
| 46 | _crtFileName = settings.value("sslClientCertPath").toString() + _url.host() + QString(".%1.crt").arg(_url.port());
|
|---|
| 47 | _keyFileName = settings.value("sslClientCertPath").toString() + _url.host() + QString(".%1.key").arg(_url.port());
|
|---|
| [3352] | 48 | }
|
|---|
| [9795] | 49 |
|
|---|
| [1378] | 50 | }
|
|---|
| 51 |
|
|---|
| 52 | // Destructor
|
|---|
| 53 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 54 | bncNetQueryV2::~bncNetQueryV2() {
|
|---|
| 55 | delete _eventLoop;
|
|---|
| [7704] | 56 | if (_reply) {
|
|---|
| 57 | _reply->abort();
|
|---|
| 58 | delete _reply;
|
|---|
| 59 | }
|
|---|
| [1378] | 60 | delete _manager;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| [1713] | 63 | // Stop (quit event loop)
|
|---|
| [1390] | 64 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 65 | void bncNetQueryV2::stop() {
|
|---|
| [1393] | 66 | if (_reply) {
|
|---|
| [1395] | 67 | _reply->abort();
|
|---|
| [7704] | 68 | delete _reply;
|
|---|
| 69 | _reply = 0;
|
|---|
| [1393] | 70 | }
|
|---|
| [1394] | 71 | _eventLoop->quit();
|
|---|
| [1398] | 72 | _status = finished;
|
|---|
| [1390] | 73 | }
|
|---|
| 74 |
|
|---|
| [1389] | 75 | // End of Request
|
|---|
| 76 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| [1378] | 77 | void bncNetQueryV2::slotFinished() {
|
|---|
| [1704] | 78 | _eventLoop->quit();
|
|---|
| 79 | if (_reply && _reply->error() != QNetworkReply::NoError) {
|
|---|
| 80 | _status = error;
|
|---|
| [9723] | 81 | if (!_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray().isEmpty()) {
|
|---|
| [8203] | 82 | emit newMessage(_url.path().toLatin1().replace(0,1,"") +
|
|---|
| [7612] | 83 | ": NetQueryV2: server replied: " +
|
|---|
| [1704] | 84 | _reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray(),
|
|---|
| 85 | true);
|
|---|
| [9723] | 86 | } else {
|
|---|
| 87 | emit newMessage(_url.path().toLatin1().replace(0,1,"") +
|
|---|
| 88 | ": NetQueryV2: server replied: " +
|
|---|
| 89 | _reply->errorString().toLatin1(),
|
|---|
| 90 | true);
|
|---|
| 91 | }
|
|---|
| [1704] | 92 | }
|
|---|
| 93 | else {
|
|---|
| [1378] | 94 | _status = finished;
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| [7612] | 98 | //
|
|---|
| [1405] | 99 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| [7612] | 100 | void bncNetQueryV2::slotProxyAuthenticationRequired(const QNetworkProxy&,
|
|---|
| [1405] | 101 | QAuthenticator*) {
|
|---|
| 102 | emit newMessage("slotProxyAuthenticationRequired", true);
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| [1389] | 105 | // Start request, block till the next read
|
|---|
| [1378] | 106 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| [1380] | 107 | void bncNetQueryV2::startRequest(const QUrl& url, const QByteArray& gga) {
|
|---|
| [1389] | 108 | startRequestPrivate(url, gga, false);
|
|---|
| [1378] | 109 | }
|
|---|
| 110 |
|
|---|
| [6787] | 111 | // Start request, block till the next read
|
|---|
| 112 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 113 | void bncNetQueryV2::keepAliveRequest(const QUrl& url, const QByteArray& gga) {
|
|---|
| 114 | startRequestPrivate(url, gga, false);
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| [1389] | 117 | // Start Request (Private Method)
|
|---|
| [1378] | 118 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| [1389] | 119 | void bncNetQueryV2::startRequestPrivate(const QUrl& url, const QByteArray& gga,
|
|---|
| 120 | bool full) {
|
|---|
| [1378] | 121 |
|
|---|
| 122 | _status = running;
|
|---|
| 123 |
|
|---|
| 124 | // Default scheme and path
|
|---|
| 125 | // -----------------------
|
|---|
| [1509] | 126 | _url = url;
|
|---|
| 127 | if (_url.scheme().isEmpty()) {
|
|---|
| [3337] | 128 | if (_secure) {
|
|---|
| 129 | _url.setScheme("https");
|
|---|
| 130 | }
|
|---|
| 131 | else {
|
|---|
| 132 | _url.setScheme("http");
|
|---|
| 133 | }
|
|---|
| [1378] | 134 | }
|
|---|
| [1509] | 135 | if (_url.path().isEmpty()) {
|
|---|
| 136 | _url.setPath("/");
|
|---|
| [1378] | 137 | }
|
|---|
| 138 |
|
|---|
| 139 | // Network Request
|
|---|
| 140 | // ---------------
|
|---|
| [9795] | 141 | bncSslConfig sslConfig = BNC_SSL_CONFIG;
|
|---|
| 142 |
|
|---|
| 143 | if (_secure) {
|
|---|
| 144 | QFile clientCrtFile(_crtFileName);
|
|---|
| 145 | QFile privateKeyFile(_keyFileName);
|
|---|
| 146 | if ( clientCrtFile.exists() && privateKeyFile.exists()) {
|
|---|
| 147 | // set local certificate if available
|
|---|
| 148 | clientCrtFile.open(QIODevice::ReadOnly);
|
|---|
| 149 | QSslCertificate clientCrt(&clientCrtFile);
|
|---|
| 150 | sslConfig.setLocalCertificate(clientCrt);
|
|---|
| 151 | // set private key if available
|
|---|
| 152 | privateKeyFile.open(QIODevice::ReadOnly);
|
|---|
| 153 | QSslKey privateKey(&privateKeyFile, QSsl::Rsa);
|
|---|
| 154 | sslConfig.setPrivateKey(privateKey);
|
|---|
| 155 | }
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| [1716] | 158 | QNetworkRequest request;
|
|---|
| [9795] | 159 | request.setSslConfiguration(sslConfig);
|
|---|
| [1716] | 160 | request.setUrl(_url);
|
|---|
| [8203] | 161 | request.setRawHeader("Host" , _url.host().toLatin1());
|
|---|
| [1716] | 162 | request.setRawHeader("Ntrip-Version", "Ntrip/2.0");
|
|---|
| [8203] | 163 | request.setRawHeader("User-Agent" , "NTRIP BNC/" BNCVERSION " (" BNC_OS ")");
|
|---|
| [1509] | 164 | if (!_url.userName().isEmpty()) {
|
|---|
| [8203] | 165 | QString uName = QUrl::fromPercentEncoding(_url.userName().toLatin1());
|
|---|
| 166 | QString passW = QUrl::fromPercentEncoding(_url.password().toLatin1());
|
|---|
| [7612] | 167 | request.setRawHeader("Authorization", "Basic " +
|
|---|
| [8203] | 168 | (uName + ":" + passW).toLatin1().toBase64());
|
|---|
| [7612] | 169 | }
|
|---|
| [1389] | 170 | if (!gga.isEmpty()) {
|
|---|
| [1716] | 171 | request.setRawHeader("Ntrip-GGA", gga);
|
|---|
| [1389] | 172 | }
|
|---|
| [1716] | 173 | request.setRawHeader("Connection" , "close");
|
|---|
| [1378] | 174 |
|
|---|
| [7851] | 175 | if (_reply) {
|
|---|
| 176 | delete _reply;
|
|---|
| 177 | _reply = 0;
|
|---|
| 178 | }
|
|---|
| [1716] | 179 | _reply = _manager->get(request);
|
|---|
| [1378] | 180 |
|
|---|
| 181 | // Connect Signals
|
|---|
| 182 | // ---------------
|
|---|
| 183 | connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
|
|---|
| [9722] | 184 | connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
|
|---|
| [9721] | 185 | connect(_reply, SIGNAL(sslErrors(QList<QSslError>)),this, SLOT(slotSslErrors(QList<QSslError>)));
|
|---|
| [1378] | 186 | if (!full) {
|
|---|
| [9722] | 187 | connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
|
|---|
| [1378] | 188 | }
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | // Start Request, wait for its completion
|
|---|
| 192 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 193 | void bncNetQueryV2::waitForRequestResult(const QUrl& url, QByteArray& outData) {
|
|---|
| 194 |
|
|---|
| 195 | // Send Request
|
|---|
| 196 | // ------------
|
|---|
| [1389] | 197 | startRequestPrivate(url, "", true);
|
|---|
| [1378] | 198 |
|
|---|
| 199 | // Wait Loop
|
|---|
| 200 | // ---------
|
|---|
| [1394] | 201 | _eventLoop->exec();
|
|---|
| [1378] | 202 |
|
|---|
| 203 | // Copy Data and Return
|
|---|
| 204 | // --------------------
|
|---|
| [7813] | 205 | if (_reply) {
|
|---|
| 206 | outData = _reply->readAll();
|
|---|
| 207 | }
|
|---|
| [1378] | 208 | }
|
|---|
| 209 |
|
|---|
| 210 | // Wait for next data
|
|---|
| 211 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 212 | void bncNetQueryV2::waitForReadyRead(QByteArray& outData) {
|
|---|
| 213 |
|
|---|
| 214 | // Wait Loop
|
|---|
| 215 | // ---------
|
|---|
| 216 | if (!_reply->bytesAvailable()) {
|
|---|
| [1394] | 217 | _eventLoop->exec();
|
|---|
| [1378] | 218 | }
|
|---|
| [9723] | 219 | if (!_reply) {
|
|---|
| 220 | return;
|
|---|
| 221 | }
|
|---|
| [1378] | 222 |
|
|---|
| [1701] | 223 | // Check NTRIPv2 error code
|
|---|
| 224 | // ------------------------
|
|---|
| 225 | if (_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
|
|---|
| 226 | _reply->abort();
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| [1378] | 229 | // Append Data
|
|---|
| 230 | // -----------
|
|---|
| [1701] | 231 | else {
|
|---|
| 232 | outData.append(_reply->readAll());
|
|---|
| [1583] | 233 | }
|
|---|
| [1378] | 234 | }
|
|---|
| [1712] | 235 |
|
|---|
| [7612] | 236 | // TSL/SSL
|
|---|
| [3332] | 237 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| [3349] | 238 | void bncNetQueryV2::slotSslErrors(QList<QSslError> errors) {
|
|---|
| [3332] | 239 |
|
|---|
| [9723] | 240 | QString msg = "SSL Error: ";
|
|---|
| [3351] | 241 | QSslCertificate cert = _reply->sslConfiguration().peerCertificate();
|
|---|
| [9708] | 242 | if (!cert.isNull() &&
|
|---|
| 243 | cert.issuerInfo(QSslCertificate::OrganizationalUnitName).count() &&
|
|---|
| 244 | cert.issuerInfo(QSslCertificate::Organization).count()) {
|
|---|
| [9719] | 245 |
|
|---|
| [3354] | 246 | msg += QString("Server Certificate Issued by:\n"
|
|---|
| 247 | "%1\n%2\nCannot be verified\n")
|
|---|
| [8203] | 248 | #if QT_VERSION >= 0x050000
|
|---|
| 249 | .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName).at(0))
|
|---|
| 250 | .arg(cert.issuerInfo(QSslCertificate::Organization).at(0));
|
|---|
| 251 | #else
|
|---|
| [3354] | 252 | .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName))
|
|---|
| 253 | .arg(cert.issuerInfo(QSslCertificate::Organization));
|
|---|
| [8203] | 254 | #endif
|
|---|
| [3351] | 255 | }
|
|---|
| [9719] | 256 |
|
|---|
| [9723] | 257 | QListIterator<QSslError> it(errors);
|
|---|
| [3363] | 258 | while (it.hasNext()) {
|
|---|
| 259 | const QSslError& err = it.next();
|
|---|
| [9723] | 260 | msg += err.errorString();
|
|---|
| [3353] | 261 | }
|
|---|
| [3346] | 262 |
|
|---|
| [7513] | 263 | if (_sslIgnoreErrors) {
|
|---|
| [3353] | 264 | _reply->ignoreSslErrors();
|
|---|
| [9723] | 265 | BNC_CORE->slotMessage("BNC ignores SSL errors as configured", true);
|
|---|
| [3353] | 266 | }
|
|---|
| 267 | else {
|
|---|
| [9742] | 268 | BNC_CORE->slotMessage(msg.toLatin1(), true);
|
|---|
| [3353] | 269 | stop();
|
|---|
| 270 | }
|
|---|
| [9723] | 271 | return;
|
|---|
| [3332] | 272 | }
|
|---|