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

Last change on this file since 9748 was 9748, checked in by stuerze, 2 years ago

minor changes

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