identd: Cleanup
authorManuel Nickschas <sputnick@quassel-irc.org>
Sun, 17 Jun 2018 22:03:25 +0000 (00:03 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Mon, 18 Jun 2018 19:25:50 +0000 (21:25 +0200)
Fix formatting and const-correctness.

src/core/CMakeLists.txt
src/core/core.cpp
src/core/core.h
src/core/corenetwork.h
src/core/identserver.cpp
src/core/identserver.h

index 2097b6c..a764608 100644 (file)
@@ -41,7 +41,8 @@ set(SOURCES
     storage.cpp
 
     # needed for automoc
-    coreeventmanager.h)
+    coreeventmanager.h
+)
 
 set(LIBS )
 
index 1ff4ada..e3c7931 100644 (file)
@@ -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()) {
index 7da4f22..e7a8c63 100644 (file)
 #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};
 
index 668f314..44dcd06 100644 (file)
@@ -453,7 +453,7 @@ private:
 #else
     QTcpSocket socket;
 #endif
-    qint64 _socketId;
+    qint64 _socketId{0};
 
     CoreUserInputHandler *_userInputHandler;
 
index 2518f86..e8f89bb 100644 (file)
  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
  ***************************************************************************/
 
-#include <logger.h>
-#include <set>
+#include <limits>
 
 #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<QTcpServer *>(sender());
+
+void IdentServer::incomingConnection()
+{
+    auto server = qobject_cast<QTcpServer *>(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<QTcpSocket *>(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<CoreNetwork *>(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<qint64>::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;
 }
index 3c0fd2a..31204b2 100644 (file)
 
 #pragma once
 
+#include <list>
+
+#include <QHash>
+#include <QObject>
+#include <QString>
 #include <QTcpServer>
 #include <QTcpSocket>
 
 #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<uint16_t, QString> _connections;
     std::list<Request> _requestQueue;
     std::list<qint64> _waiting;
-    qint64 _socketId;
-    qint64 _requestId;
+    qint64 _socketId{0};
+    qint64 _requestId{0};
 };