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

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

minor changes

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