From: Manuel Nickschas Date: Sun, 17 Jun 2018 22:03:25 +0000 (+0200) Subject: identd: Cleanup X-Git-Tag: 0.13-rc1~44 X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=commitdiff_plain;h=2e83624ce2642589a5b1f298e4993c51d22caafe identd: Cleanup Fix formatting and const-correctness. --- diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 2097b6c1..a7646083 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -41,7 +41,8 @@ set(SOURCES storage.cpp # needed for automoc - coreeventmanager.h) + coreeventmanager.h +) set(LIBS ) diff --git a/src/core/core.cpp b/src/core/core.cpp index 1ff4adab..e3c79314 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -221,7 +221,7 @@ bool Core::init() if (Quassel::isOptionSet("oidentd")) { _oidentdConfigGenerator = new OidentdConfigGenerator(this); } - + if (Quassel::isOptionSet("ident-daemon")) { _identServer = new IdentServer(_strictIdentEnabled, this); @@ -670,7 +670,9 @@ bool Core::startListening() if (!success) quError() << qPrintable(tr("Could not open any network interfaces to listen on!")); - if (_identServer != nullptr) _identServer->startListening(); + if (_identServer) { + _identServer->startListening(); + } return success; } @@ -678,7 +680,9 @@ bool Core::startListening() void Core::stopListening(const QString &reason) { - if (_identServer != nullptr) _identServer->stopListening(reason); + if (_identServer) { + _identServer->stopListening(reason); + } bool wasListening = false; if (_server.isListening()) { diff --git a/src/core/core.h b/src/core/core.h index 7da4f227..e7a8c635 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -40,12 +40,12 @@ #include "authenticator.h" #include "bufferinfo.h" #include "deferredptr.h" +#include "identserver.h" #include "message.h" #include "oidentdconfiggenerator.h" #include "sessionthread.h" #include "storage.h" #include "types.h" -#include "identserver.h" class CoreAuthHandler; class CoreSession; @@ -807,7 +807,7 @@ private: QDateTime _startTime; IdentServer *_identServer {nullptr}; - + bool _initialized{false}; bool _configured{false}; diff --git a/src/core/corenetwork.h b/src/core/corenetwork.h index 668f3140..44dcd06c 100644 --- a/src/core/corenetwork.h +++ b/src/core/corenetwork.h @@ -453,7 +453,7 @@ private: #else QTcpSocket socket; #endif - qint64 _socketId; + qint64 _socketId{0}; CoreUserInputHandler *_userInputHandler; diff --git a/src/core/identserver.cpp b/src/core/identserver.cpp index 2518f860..e8f89bbd 100644 --- a/src/core/identserver.cpp +++ b/src/core/identserver.cpp @@ -18,20 +18,23 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ -#include -#include +#include #include "corenetwork.h" #include "identserver.h" +#include "logger.h" -IdentServer::IdentServer(bool strict, QObject *parent) : QObject(parent), _strict(strict), _socketId(0), _requestId(0) { +IdentServer::IdentServer(bool strict, QObject *parent) + : QObject(parent) + , _strict(strict) +{ connect(&_server, SIGNAL(newConnection()), this, SLOT(incomingConnection())); connect(&_v6server, SIGNAL(newConnection()), this, SLOT(incomingConnection())); } -IdentServer::~IdentServer() = default; -bool IdentServer::startListening() { +bool IdentServer::startListening() +{ uint16_t port = Quassel::optionValue("ident-port").toUShort(); bool success = false; @@ -46,25 +49,27 @@ bool IdentServer::startListening() { } if (_server.listen(QHostAddress("127.0.0.1"), port)) { - success = true; - quInfo() << qPrintable( tr("Listening for identd clients on IPv4 %1 port %2") .arg("127.0.0.1") .arg(_server.serverPort()) ); + + success = true; } if (!success) { - quError() << qPrintable( - tr("Identd could not open any network interfaces to listen on! No identd functionality will be available")); + quError() << qPrintable(tr("Identd could not open any network interfaces to listen on! No identd functionality will be available")); } return success; } -void IdentServer::stopListening(const QString &msg) { + +void IdentServer::stopListening(const QString &msg) +{ bool wasListening = false; + if (_server.isListening()) { wasListening = true; _server.close(); @@ -73,6 +78,7 @@ void IdentServer::stopListening(const QString &msg) { wasListening = true; _v6server.close(); } + if (wasListening) { if (msg.isEmpty()) quInfo() << "No longer listening for identd clients."; @@ -81,8 +87,10 @@ void IdentServer::stopListening(const QString &msg) { } } -void IdentServer::incomingConnection() { - auto *server = qobject_cast(sender()); + +void IdentServer::incomingConnection() +{ + auto server = qobject_cast(sender()); Q_ASSERT(server); while (server->hasPendingConnections()) { QTcpSocket *socket = server->nextPendingConnection(); @@ -90,7 +98,9 @@ void IdentServer::incomingConnection() { } } -void IdentServer::respond() { + +void IdentServer::respond() +{ QTcpSocket *socket = qobject_cast(sender()); Q_ASSERT(socket); @@ -118,16 +128,21 @@ void IdentServer::respond() { Request request{socket, localPort, query, transactionId, _requestId++}; if (!success) { request.respondError("INVALID-PORT"); - } else if (responseAvailable(request)) { + } + else if (responseAvailable(request)) { // success - } else if (lowestSocketId() < transactionId) { + } + else if (lowestSocketId() < transactionId) { _requestQueue.emplace_back(request); - } else { + } + else { request.respondError("NO-USER"); } } -void Request::respondSuccess(const QString &user) { + +void Request::respondSuccess(const QString &user) +{ QString data = query + " : USERID : Quassel : " + user + "\r\n"; socket->write(data.toUtf8()); @@ -135,7 +150,9 @@ void Request::respondSuccess(const QString &user) { socket->close(); } -void Request::respondError(const QString &error) { + +void Request::respondError(const QString &error) +{ QString data = query + " : ERROR : " + error + "\r\n"; socket->write(data.toUtf8()); @@ -143,18 +160,21 @@ void Request::respondError(const QString &error) { socket->close(); } -bool IdentServer::responseAvailable(Request request) { + +bool IdentServer::responseAvailable(Request request) const +{ if (!_connections.contains(request.localPort)) { return false; } - QString user = _connections[request.localPort]; - request.respondSuccess(user); + request.respondSuccess(_connections[request.localPort]); return true; } -bool IdentServer::addSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, - const QHostAddress &peerAddress, quint16 peerPort, qint64 socketId) { + +void IdentServer::addSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, + const QHostAddress &peerAddress, quint16 peerPort, qint64 socketId) +{ Q_UNUSED(localAddress) Q_UNUSED(peerAddress) Q_UNUSED(peerPort) @@ -162,12 +182,12 @@ bool IdentServer::addSocket(const CoreIdentity *identity, const QHostAddress &lo const CoreNetwork *network = qobject_cast(sender()); _connections[localPort] = network->coreSession()->strictCompliantIdent(identity);; processWaiting(socketId); - return true; } -bool IdentServer::removeSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, - const QHostAddress &peerAddress, quint16 peerPort, qint64 socketId) { +void IdentServer::removeSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, + const QHostAddress &peerAddress, quint16 peerPort, qint64 socketId) +{ Q_UNUSED(identity) Q_UNUSED(localAddress) Q_UNUSED(peerAddress) @@ -175,16 +195,19 @@ bool IdentServer::removeSocket(const CoreIdentity *identity, const QHostAddress _connections.remove(localPort); processWaiting(socketId); - return true; } -qint64 IdentServer::addWaitingSocket() { + +qint64 IdentServer::addWaitingSocket() +{ qint64 newSocketId = _socketId++; _waiting.push_back(newSocketId); return newSocketId; } -qint64 IdentServer::lowestSocketId() { + +qint64 IdentServer::lowestSocketId() const +{ if (_waiting.empty()) { return std::numeric_limits::max(); } @@ -192,24 +215,33 @@ qint64 IdentServer::lowestSocketId() { return _waiting.front(); } -void IdentServer::removeWaitingSocket(qint64 socketId) { + +void IdentServer::removeWaitingSocket(qint64 socketId) +{ _waiting.remove(socketId); } -void IdentServer::processWaiting(qint64 socketId) { + +void IdentServer::processWaiting(qint64 socketId) +{ removeWaitingSocket(socketId); - _requestQueue.remove_if([=](Request request) { + + _requestQueue.remove_if([this, socketId](Request request) { if (socketId < request.transactionId && responseAvailable(request)) { return true; - } else if (lowestSocketId() < request.transactionId) { + } + else if (lowestSocketId() < request.transactionId) { return false; - } else { + } + else { request.respondError("NO-USER"); return true; } }); } -bool operator==(const Request &a, const Request &b) { + +bool operator==(const Request &a, const Request &b) +{ return a.requestId == b.requestId; } diff --git a/src/core/identserver.h b/src/core/identserver.h index 3c0fd2a9..31204b2b 100644 --- a/src/core/identserver.h +++ b/src/core/identserver.h @@ -20,12 +20,18 @@ #pragma once +#include + +#include +#include +#include #include #include #include "coreidentity.h" -struct Request { +struct Request +{ QTcpSocket *socket; uint16_t localPort; QString query; @@ -38,29 +44,30 @@ struct Request { void respondError(const QString &error); }; -class IdentServer : public QObject { -Q_OBJECT + +class IdentServer : public QObject +{ + Q_OBJECT + public: - IdentServer(bool strict, QObject *parent); - ~IdentServer() override; + IdentServer(bool strict, QObject *parent = nullptr); bool startListening(); void stopListening(const QString &msg); qint64 addWaitingSocket(); + public slots: - bool addSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort, qint64 socketId); - bool removeSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort, qint64 socketId); + void addSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort, qint64 socketId); + void removeSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort, qint64 socketId); private slots: void incomingConnection(); void respond(); private: - bool responseAvailable(Request request); - - QString sysIdentForIdentity(const CoreIdentity *identity) const; + bool responseAvailable(Request request) const; - qint64 lowestSocketId(); + qint64 lowestSocketId() const; void processWaiting(qint64 socketId); @@ -73,6 +80,6 @@ private: QHash _connections; std::list _requestQueue; std::list _waiting; - qint64 _socketId; - qint64 _requestId; + qint64 _socketId{0}; + qint64 _requestId{0}; };