Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

QxtNetwork: introducing QxtSslConnectionManager

  • Loading branch information...
commit 87891100337b11e3fa31ad61058a6915fe2415ab 1 parent cd72e9e
ahigerd authored
View
1  include/QxtNetwork/QxtSslConnectionManager
@@ -0,0 +1 @@
+#include "qxtsslconnectionmanager.h"
View
2  src/network/network.pri
@@ -22,6 +22,7 @@ HEADERS += qxtpop3statreply.h
HEADERS += qxtpop3listreply.h
HEADERS += qxtpop3retrreply.h
HEADERS += qxtsslserver.h
+HEADERS += qxtsslconnectionmanager.h
SOURCES += qxtjsonrpccall.cpp
SOURCES += qxtjsonrpcclient.cpp
@@ -36,6 +37,7 @@ SOURCES += qxtxmlrpc_p.cpp
SOURCES += qxtpop3.cpp
SOURCES += qxtpop3reply.cpp
SOURCES += qxtsslserver.cpp
+SOURCES += qxtsslconnectionmanager.cpp
contains(DEFINES,HAVE_OPENSSL){
HEADERS += qxtsshchannel.h
View
106 src/network/qxtsslconnectionmanager.cpp
@@ -0,0 +1,106 @@
+/****************************************************************************
+ **
+ ** Copyright (C) Qxt Foundation. Some rights reserved.
+ **
+ ** This file is part of the QxtNetwork module of the Qxt library.
+ **
+ ** This library is free software; you can redistribute it and/or modify it
+ ** under the terms of the Common Public License, version 1.0, as published
+ ** by IBM, and/or under the terms of the GNU Lesser General Public License,
+ ** version 2.1, as published by the Free Software Foundation.
+ **
+ ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
+ ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
+ ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
+ ** FITNESS FOR A PARTICULAR PURPOSE.
+ **
+ ** You should have received a copy of the CPL and the LGPL along with this
+ ** file. See the LICENSE file and the cpl1.0.txt/lgpl-2.1.txt files
+ ** included with the source distribution for more information.
+ ** If you did not receive a copy of the licenses, contact the Qxt Foundation.
+ **
+ ** <http://libqxt.org> <foundation@libqxt.org>
+ **
+ ****************************************************************************/
+
+#include "qxtsslconnectionmanager.h"
+#include "qxttcpconnectionmanager_p.h"
+#include <QSslKey>
+
+/*!
+ * \class QxtSslConnectionManager
+ * \inmodule QxtNetwork
+ * \brief The QxtSslConnectionManager class accepts SSL connections and maintains a connection pool
+ *
+ * QxtSslConnectionManager is a standardized interface for accepting and tracking
+ * incoming SSL connections.
+ *
+ * Each incoming connection is assigned an arbitrary, opaque client ID number. This
+ * number can be used to retrieve the QSslSocket associated with it. A list of IDs
+ * for all current connections can be retrieved with the clients() function.
+ *
+ * Like QxtSslServer, QxtSslConnectionManager can listen for incoming connections on
+ * a specified interface and port. Unlike QTcpServer, but like QxtSslServer,
+ * overriding incomingConnection() is not recommended.
+ *
+ * \sa QxtSslServer, QxtSslConnectionManager
+ */
+
+#ifndef QT_NO_OPENSSL
+
+/*!
+ * Constructs a new QxtSslConnectionManager object with the specified \a parent.
+ */
+QxtSslConnectionManager::QxtSslConnectionManager(QObject* parent) : QxtTcpConnectionManager(parent)
+{
+ // no QXT_INIT_PRIVATE here because we're borrowing QxtTcpConnectionManager's
+ setAutoEncrypt(true);
+}
+
+void QxtSslConnectionManager::setLocalCertificate(const QSslCertificate& cert)
+{
+ qxt_d().setLocalCertificate(cert);
+}
+
+void QxtSslConnectionManager::setLocalCertificate(const QString& path, QSsl::EncodingFormat format)
+{
+ qxt_d().setLocalCertificate(path, format);
+}
+
+QSslCertificate QxtSslConnectionManager::localCertificate() const
+{
+ return qxt_d().localCertificate();
+}
+
+void QxtSslConnectionManager::setPrivateKey(const QSslKey& key)
+{
+ qxt_d().setPrivateKey(key);
+}
+
+void QxtSslConnectionManager::setPrivateKey(const QString& path, QSsl::KeyAlgorithm algo, QSsl::EncodingFormat format, const QByteArray& passPhrase)
+{
+ qxt_d().setPrivateKey(path, algo, format, passPhrase);
+}
+
+QSslKey QxtSslConnectionManager::privateKey() const
+{
+ return qxt_d().privateKey();
+}
+
+void QxtSslConnectionManager::setAutoEncrypt(bool on)
+{
+ qxt_d().setAutoEncrypt(on);
+}
+
+bool QxtSslConnectionManager::autoEncrypt() const
+{
+ return qxt_d().autoEncrypt();
+}
+
+QIODevice* QxtSslConnectionManager::incomingConnection(int socketDescriptor)
+{
+ qxt_d().incomingConnection(socketDescriptor);
+ return qxt_d().nextPendingConnection();
+}
+
+#endif
View
57 src/network/qxtsslconnectionmanager.h
@@ -0,0 +1,57 @@
+/****************************************************************************
+ **
+ ** Copyright (C) Qxt Foundation. Some rights reserved.
+ **
+ ** This file is part of the QxtNetwork module of the Qxt library.
+ **
+ ** This library is free software; you can redistribute it and/or modify it
+ ** under the terms of the Common Public License, version 1.0, as published
+ ** by IBM, and/or under the terms of the GNU Lesser General Public License,
+ ** version 2.1, as published by the Free Software Foundation.
+ **
+ **
+ ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
+ ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
+ ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
+ ** FITNESS FOR A PARTICULAR PURPOSE.
+ **
+ ** You should have received a copy of the CPL and the LGPL along with this
+ ** file. See the LICENSE file and the cpl1.0.txt/lgpl-2.1.txt files
+ ** included with the source distribution for more information.
+ ** If you did not receive a copy of the licenses, contact the Qxt Foundation.
+ **
+ ** <http://libqxt.org> <foundation@libqxt.org>
+ **
+ ****************************************************************************/
+
+#ifndef QXTSSLCONNECTIONMANAGER_H
+#define QXTSSLCONNECTIONMANAGER_H
+
+#include <qxttcpconnectionmanager.h>
+#ifndef QT_NO_OPENSSL
+#include <QSslSocket>
+
+class QXT_NETWORK_EXPORT QxtSslConnectionManager : public QxtTcpConnectionManager
+{
+ Q_OBJECT
+public:
+ QxtSslConnectionManager(QObject* parent);
+
+ void setLocalCertificate(const QSslCertificate& cert);
+ void setLocalCertificate(const QString& path, QSsl::EncodingFormat format = QSsl::Pem);
+ QSslCertificate localCertificate() const;
+
+ void setPrivateKey(const QSslKey& key);
+ void setPrivateKey(const QString& path, QSsl::KeyAlgorithm algo = QSsl::Rsa,
+ QSsl::EncodingFormat format = QSsl::Pem, const QByteArray& passPhrase = QByteArray());
+ QSslKey privateKey() const;
+
+ void setAutoEncrypt(bool on);
+ bool autoEncrypt() const;
+
+protected:
+ virtual QIODevice* incomingConnection(int socketDescriptor);
+};
+
+#endif
+#endif
View
14 src/network/qxtsslserver.cpp
@@ -44,6 +44,8 @@
* you wish to serve up a different certificate based on some property of the connection or some data
* negotiated before beginning encryption.
*
+ * Unlike QTcpServer, overriding QxtSslServer::incomingConnection() is not recommended.
+ *
* QxtSslServer is only available if Qt was compiled with OpenSSL support.
*/
@@ -195,12 +197,12 @@ void QxtSslServer::incomingConnection(int socketDescriptor)
if(socket->setSocketDescriptor(socketDescriptor)) {
socket->setLocalCertificate(qxt_d().localCertificate);
socket->setPrivateKey(qxt_d().privateKey);
- if(parent()){
- connect(socket, SIGNAL(sslErrors(const QList<QSslError>&)),
- parent(), SLOT(sslErrors(const QList<QSslError>&)));
- connect(socket, SIGNAL(peerVerifyError(const QSslError&)),
- parent(), SLOT(peerVerifyError(const QSslError&)));
- }
+ if(parent()){
+ connect(socket, SIGNAL(sslErrors(const QList<QSslError>&)),
+ parent(), SLOT(sslErrors(const QList<QSslError>&)));
+ connect(socket, SIGNAL(peerVerifyError(const QSslError&)),
+ parent(), SLOT(peerVerifyError(const QSslError&)));
+ }
qxt_d().pendingConnections.enqueue(socket);
// emit newConnection(); // removed: QTcpServerPrivate emits this for us
if(qxt_d().autoEncrypt) socket->startServerEncryption();
View
15 src/network/qxttcpconnectionmanager.cpp
@@ -43,9 +43,10 @@
* Like QTcpServer, QxtTcpConnectionManager can listen for incoming connections on
* a specified interface and port, and like QTcpServer you may override the
* incomingConnection() function to change the handling of new connections. This
- * is, for instance, where you would create a QSslSocket to encrypt communications.
+ * is, for instance, where you could create a QSslSocket to encrypt communications
+ * (but see QxtSslConnectionManager).
*
- * \sa QTcpServer
+ * \sa QTcpServer, QxtSslConnectionManager
*/
/*!
@@ -54,9 +55,17 @@
QxtTcpConnectionManager::QxtTcpConnectionManager(QObject* parent) : QxtAbstractConnectionManager(parent)
{
QXT_INIT_PRIVATE(QxtTcpConnectionManager);
+#ifndef QT_NO_OPENSSL
+ qxt_d().setAutoEncrypt(false);
+#endif
}
-QxtTcpConnectionManagerPrivate::QxtTcpConnectionManagerPrivate() : QTcpServer(0)
+QxtTcpConnectionManagerPrivate::QxtTcpConnectionManagerPrivate()
+#ifndef QT_NO_OPENSSL
+: QxtSslServer(0)
+#else
+: QTcpServer(0)
+#endif
{
QObject::connect(&mapper, SIGNAL(mapped(QObject*)), this, SLOT(socketDisconnected(QObject*)));
}
View
2  src/network/qxttcpconnectionmanager.h
@@ -49,7 +49,7 @@ class QXT_NETWORK_EXPORT QxtTcpConnectionManager : public QxtAbstractConnectionM
virtual QIODevice* incomingConnection(int socketDescriptor);
virtual void removeConnection(QIODevice* device, quint64 clientID);
-private:
+protected: // for QxtSslConnectionManager
QXT_DECLARE_PRIVATE(QxtTcpConnectionManager)
};
View
9 src/network/qxttcpconnectionmanager_p.h
@@ -31,9 +31,16 @@
#include <QTcpSocket>
#include <QSignalMapper>
+#ifdef QT_NO_OPENSSL
class QxtTcpConnectionManagerPrivate : public QTcpServer, public QxtPrivate<QxtTcpConnectionManager>
+#else
+#include "qxtsslserver.h"
+#include <QSslSocket>
+class QxtTcpConnectionManagerPrivate : public QxtSslServer, public QxtPrivate<QxtTcpConnectionManager>
+#endif
{
- Q_OBJECT
+Q_OBJECT
+friend class QxtSslConnectionManager;
public:
QxtTcpConnectionManagerPrivate();
QXT_DECLARE_PUBLIC(QxtTcpConnectionManager)
Please sign in to comment.
Something went wrong with that request. Please try again.