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

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