source: ntrip/trunk/BNC/src/upload/bncuploadcaster.cpp@ 9740

Last change on this file since 9740 was 9740, checked in by stuerze, 3 months ago

minor changes

File size: 11.5 KB
Line 
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 *
13 * Changes:
14 *
15 * -----------------------------------------------------------------------*/
16
17#include <math.h>
18#include "bncuploadcaster.h"
19#include "bncversion.h"
20#include "bnccore.h"
21#include "bnctableitem.h"
22#include "bncsettings.h"
23#include "bncsslconfig.h"
24
25using namespace std;
26
27// Constructor
28////////////////////////////////////////////////////////////////////////////
29bncUploadCaster::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;
35 _casterOutHost = outHost;
36 _casterOutPort = outPort;
37 _ntripVersion = ntripVersion;
38 _userName = userName;
39 _password = password;
40 _outSocket = 0;
41 _sOpenTrial = 0;
42 _iRow = iRow;
43 _rate = rate;
44
45 if (_rate < 0) {
46 _rate = 0;
47 } else if (_rate > 60) {
48 _rate = 60;
49 }
50 _isToBeDeleted = false;
51
52 connect(this, SIGNAL(newMessage(QByteArray,bool)), BNC_CORE, SLOT(slotMessage(const QByteArray,bool)));
53
54 if (BNC_CORE->_uploadTableItems.find(_iRow) != BNC_CORE->_uploadTableItems.end()) {
55 connect(this, SIGNAL(newBytes(QByteArray,double)),
56 BNC_CORE->_uploadTableItems.value(iRow),
57 SLOT(slotNewBytes(const QByteArray,double)));
58 }
59 if (BNC_CORE->_uploadEphTableItems.find(_iRow) != BNC_CORE->_uploadEphTableItems.end()) {
60 connect(this, SIGNAL(newBytes(QByteArray,double)),
61 BNC_CORE->_uploadEphTableItems.value(iRow),
62 SLOT(slotNewBytes(const QByteArray,double)));
63 }
64
65 _sslIgnoreErrors = (Qt::CheckState(settings.value("sslIgnoreErrors").toInt()) == Qt::Checked);
66
67 _proxyOutHost = settings.value("proxyHost").toString();
68 _proxyOutPort = settings.value("proxyPort").toInt();
69 (_proxyOutHost.isEmpty()) ? _proxy = false : _proxy = true;
70
71 _secure = false;
72 if (_ntripVersion == "2s") {
73 if (!QSslSocket::supportsSsl()) {
74 emit(newMessage(
75 "For SSL support please install OpenSSL run-time libraries: Ntrip Version 2 is tried",
76 true));
77 _ntripVersion == "2";
78 } else {
79 _secure = true;
80 _casterOutPort = 443;
81 }
82 }
83
84 if (!_secure && _proxy) {
85 _postExtension = QString("http://%1:%2").arg(_casterOutHost).arg(_casterOutPort);
86 } else {
87 _postExtension = "";
88 }
89}
90
91// Safe Desctructor
92////////////////////////////////////////////////////////////////////////////
93void bncUploadCaster::deleteSafely() {
94 _isToBeDeleted = true;
95 if (!isRunning()) {
96 delete this;
97 }
98}
99
100// Destructor
101////////////////////////////////////////////////////////////////////////////
102bncUploadCaster::~bncUploadCaster() {
103 if (isRunning()) {
104 wait();
105 }
106 if (_outSocket) {
107 disconnect(_outSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(slotSslErrors(QList<QSslError>)));
108 disconnect(_outSocket, SIGNAL(disconnected()), this, SLOT(slotSslSocketConnectionClosed()));
109 delete _outSocket;
110 }
111}
112
113//
114////////////////////////////////////////////////////////////////////////////
115void bncUploadCaster::slotProxyAuthenticationRequired(const QNetworkProxy&,
116 QAuthenticator*) {
117 emit newMessage("slotProxyAuthenticationRequired", true);
118}
119
120// TSL/SSL
121 ////////////////////////////////////////////////////////////////////////////
122void bncUploadCaster::slotSslErrors(QList<QSslError> errors) {
123 QString msg = "SSL Error: ";
124 if (_outSocket) {
125 QSslCertificate cert = _outSocket->sslConfiguration().peerCertificate();
126 if (!cert.isNull() &&
127 cert.issuerInfo(QSslCertificate::OrganizationalUnitName).count() &&
128 cert.issuerInfo(QSslCertificate::Organization).count()) {
129 msg += QString("Server Certificate Issued by:\n" "%1\n%2\nCannot be verified\n")
130#if QT_VERSION >= 0x050000
131 .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName).at(0))
132 .arg(cert.issuerInfo(QSslCertificate::Organization).at(0));
133#else
134 .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName))
135 .arg(cert.issuerInfo(QSslCertificate::Organization));
136#endif
137 }
138
139 QListIterator<QSslError> it(errors);
140 while (it.hasNext()) {
141 const QSslError& err = it.next();
142 msg += err.errorString();
143 }
144 emit(newMessage(msg.toLatin1(), true));
145
146 if (_sslIgnoreErrors) {
147 _outSocket->ignoreSslErrors();
148 emit(newMessage("BNC ignores SSL errors as configured", true));
149 }
150 }
151}
152
153
154// Endless Loop
155////////////////////////////////////////////////////////////////////////////
156void bncUploadCaster::run() {
157 while (true) {
158 if (_isToBeDeleted) {
159 QThread::quit();
160 deleteLater();
161 return;
162 }
163 open();
164 if (_outSocket && _outSocket->state() == QAbstractSocket::ConnectedState) {
165 QMutexLocker locker(&_mutex);
166 if (_outBuffer.size() > 0) {
167 if (_ntripVersion == "1") {
168 _outSocket->write(_outBuffer);
169 _outSocket->flush();
170 } else {
171 QString chunkSize = QString("%1").arg(_outBuffer.size(), 0, 16, QLatin1Char('0'));
172 QByteArray chunkedData = chunkSize.toLatin1() + "\r\n" + _outBuffer + "\r\n";
173 _outSocket->write(chunkedData);
174 _outSocket->flush();
175 }
176 emit newBytes(_mountpoint.toLatin1(), _outBuffer.size());
177 }
178 }
179 if (_rate == 0) {
180 {
181 QMutexLocker locker(&_mutex);
182 _outBuffer.clear();
183 }
184 msleep(100); //sleep 0.1 sec
185 } else {
186 sleep(_rate);
187 }
188 }
189}
190
191// Start the Communication with NTRIP Caster
192////////////////////////////////////////////////////////////////////////////
193void bncUploadCaster::open() {
194 const int timeOut = 5000; // 5 seconds
195 QByteArray msg;
196
197 if (_mountpoint.isEmpty()) {
198 return;
199 }
200
201 if (_outSocket != 0) {
202 if (_outSocket->state() == QAbstractSocket::ConnectedState) {
203 return;
204 }
205 _outSocket->close();
206 }
207
208 double minDt = pow(2.0, _sOpenTrial);
209 if (++_sOpenTrial > 4) {
210 _sOpenTrial = 4;
211 }
212 if (_outSocketOpenTime.isValid()
213 && _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
214 return;
215 } else {
216 _outSocketOpenTime = QDateTime::currentDateTime();
217 }
218
219 _outSocket = new QSslSocket();
220 _outSocket->setProxy(QNetworkProxy::NoProxy);
221 _outSocket->setSslConfiguration(BNC_SSL_CONFIG);
222 connect(_outSocket, SIGNAL(disconnected()), this, SLOT(slotSslSocketConnectionClosed()));
223 connect(_outSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(slotSslErrors(QList<QSslError>)));
224
225 if (!_proxy) {
226 if (!connectToHost(_casterOutHost, _casterOutPort, _secure)) {
227 return;
228 }
229 } else {
230 if (_ntripVersion == "1") {
231 emit(newMessage("No proxy support in Ntrip Version 1 upload!", true));
232 _outSocket->close();
233 return;
234 }
235 connect(_outSocket, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)),
236 this,SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));
237
238 if (!connectToHost(_proxyOutHost, _proxyOutPort, false)) {
239 return;
240 }
241
242 if (_secure) {
243 msg = "CONNECT " + _casterOutHost.toLatin1() + ":"
244 + QString("%1").arg(_casterOutPort).toLatin1() + " HTTP/1.1\r\n"
245 + "Proxy-Connection: Keep-Alive\r\n"
246 + "Host: " + _casterOutHost.toLatin1() + "\r\n"
247 + "User-Agent: NTRIP BNC/" BNCVERSION " (" + BNC_OS + ")\r\n"
248 + "\r\n"; //cout << msg.toStdString().c_str();
249 _outSocket->write(msg);
250 _outSocket->waitForBytesWritten();
251 _outSocket->waitForReadyRead();
252
253 QByteArray ans = _outSocket->readAll(); //cout << ans.toStdString().c_str() << endl;
254 if (ans.indexOf("200") == -1) {
255 emit(newMessage("Proxy: Connection broken for " + _mountpoint.toLatin1()
256 + ": " + ans.left(ans.length() - 2), true));
257 _outSocket->close();
258 return;
259 } else {
260 emit(newMessage("Proxy: Connection established for " + _mountpoint.toLatin1(), true));
261 _sOpenTrial = 0;
262 _outSocket->setPeerVerifyName(_casterOutHost);
263 _outSocket->startClientEncryption();
264 if (!_outSocket->waitForEncrypted(timeOut)) {
265 emit(newMessage("Proxy/Caster: Encrypt timeout for " + _mountpoint.toLatin1() + " ("
266 + _casterOutHost.toLatin1() + ":"
267 + QString("%1) ").arg(_casterOutPort).toLatin1()
268 + _outSocket->errorString().toLatin1(), true));
269 return;
270 } else {
271 emit(newMessage("Proxy: SSL handshake completed for " + _mountpoint.toLatin1(), true));
272 }
273 }
274 }
275 }
276
277 if (_ntripVersion == "1") {
278 msg = "SOURCE " + _password.toLatin1() + " /" + _mountpoint.toLatin1()
279 + "\r\n" + "Source-Agent: NTRIP BNC/" BNCVERSION "\r\n\r\n";
280 } else {
281 msg = "POST " + _postExtension.toLatin1() + "/" + _mountpoint.toLatin1()
282 + " HTTP/1.1\r\n" + "Host: " + _casterOutHost.toLatin1() + "\r\n"
283 + "Ntrip-Version: Ntrip/2.0\r\n" + "Authorization: Basic "
284 + (_userName + ":" + _password).toLatin1().toBase64() + "\r\n"
285 + "User-Agent: NTRIP BNC/" BNCVERSION " (" + BNC_OS + ")\r\n"
286 + "Connection: close\r\n" + "Transfer-Encoding: chunked\r\n\r\n";
287 }
288 //cout << msg.toStdString().c_str();
289 _outSocket->write(msg);
290 _outSocket->waitForBytesWritten();
291 _outSocket->waitForReadyRead();
292
293 QByteArray ans = _outSocket->readAll(); //cout << "ans: " << ans.toStdString().c_str() << endl;
294
295 if (ans.indexOf("200") == -1) {
296 _outSocket->close();
297 emit(newMessage("Broadcaster: Connection broken for " + _mountpoint.toLatin1() + ": "
298 + ans.left(ans.length() - 2), true));
299 } else {
300 emit(newMessage("Broadcaster: Connection opened for " + _mountpoint.toLatin1(), true));
301 _sOpenTrial = 0;
302 }
303}
304
305// Try connection to NTRIP Caster or Proxy
306////////////////////////////////////////////////////////////////////////////
307bool bncUploadCaster::connectToHost(QString outHost, int outPort, bool encrypted) {
308 const int timeOut = 5000; // 5 seconds
309 if (encrypted) {
310 _outSocket->connectToHostEncrypted(outHost, outPort);
311 if (!_outSocket->waitForEncrypted(timeOut)) {
312 emit(newMessage(
313 "Broadcaster: Connect timeout for " + _mountpoint.toLatin1() + " ("
314 + outHost.toLatin1() + ":"
315 + QString("%1) ").arg(outPort).toLatin1()
316 + _outSocket->errorString().toLatin1(), true));
317 return false;
318 } else {
319 emit(newMessage("Broadcaster: SSL handshake completed for " + _mountpoint.toLatin1(), true));
320 }
321 } else {
322 _outSocket->connectToHost(outHost, outPort);
323 if (!_outSocket->waitForConnected(timeOut)) {
324 emit(newMessage(
325 "Broadcaster: Connect timeout for " + _mountpoint.toLatin1() + " ("
326 + outHost.toLatin1() + ":"
327 + QString("%1) ").arg(outPort).toLatin1()
328 + _outSocket->errorString().toLatin1(), true));
329 return false;
330 }
331 }
332 return true;
333}
334
335
336void bncUploadCaster::slotSslSocketConnectionClosed() {
337 disconnect(_outSocket, SIGNAL(disconnected()), this, SLOT(slotSslSocketConnectionClosed()));
338 disconnect(_outSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(slotSslErrors(QList<QSslError>)));
339 emit(newMessage("slotSslSocketConnectionClosed", true));
340 if (_outSocket) {
341 delete _outSocket;
342 _outSocket = 0;
343 }
344}
345
346
347
348
Note: See TracBrowser for help on using the repository browser.