Improve identd
authorJanne Koschinski <janne@kuschku.de>
Mon, 26 Aug 2019 14:51:18 +0000 (16:51 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Thu, 9 Jan 2020 19:09:05 +0000 (20:09 +0100)
- add debug output
- improve formatting of identd responses

src/core/identserver.cpp
src/core/identserver.h

index 62d68a9..1335a6c 100644 (file)
@@ -150,17 +150,22 @@ void IdentServer::respond()
     else if (query.endsWith("\n"))
         query.chop(1);
 
+    qDebug() << "Received identd query" << query << "from" << socket->peerAddress();
+
     QList<QByteArray> split = query.split(',');
 
-    bool success = false;
+    bool successLocalPort = false;
+    bool successRemotePort = false;
 
     quint16 localPort = 0;
-    if (!split.empty()) {
-        localPort = split[0].trimmed().toUShort(&success, 10);
+    quint16 remotePort = 0;
+    if (split.length() == 2) {
+        localPort = split[0].trimmed().toUShort(&successLocalPort, 10);
+        remotePort = split[1].trimmed().toUShort(&successRemotePort, 10);
     }
 
-    Request request{socket, localPort, query, transactionId, _requestId++};
-    if (!success) {
+    Request request{socket, localPort, remotePort, query, transactionId, _requestId++};
+    if (!successLocalPort || !successRemotePort) {
         request.respondError("INVALID-PORT");
     }
     else if (responseAvailable(request)) {
@@ -177,20 +182,28 @@ void IdentServer::respond()
 void Request::respondSuccess(const QString& user)
 {
     if (socket) {
-        QString data = query + " : USERID : Quassel : " + user + "\r\n";
+        QString data = QString("%1, %2 : USERID : Quassel : %3\r\n")
+            .arg(QString::number(localPort))
+            .arg(QString::number(remotePort))
+            .arg(user);
+        qDebug() << "answering identd request from" << socket->peerAddress() << "with" << data;
         socket->write(data.toUtf8());
         socket->flush();
-        socket->close();
+        QTimer::singleShot(DISCONNECTION_TIMEOUT, socket, &QTcpSocket::close);
     }
 }
 
 void Request::respondError(const QString& error)
 {
     if (socket) {
-        QString data = query + " : ERROR : " + error + "\r\n";
+        QString data = QString("%1, %2 : ERROR : %3\r\n")
+            .arg(QString::number(localPort))
+            .arg(QString::number(remotePort))
+            .arg(error);
+        qDebug() << "answering identd request from" << socket->peerAddress() << "with" << data;
         socket->write(data.toUtf8());
         socket->flush();
-        socket->close();
+        QTimer::singleShot(DISCONNECTION_TIMEOUT, socket, &QTcpSocket::close);
     }
 }
 
@@ -217,7 +230,7 @@ void IdentServer::addSocket(const CoreIdentity* identity,
 
     const CoreNetwork* network = qobject_cast<CoreNetwork*>(sender());
     _connections[localPort] = network->coreSession()->strictCompliantIdent(identity);
-    ;
+
     processWaiting(socketId);
 }
 
index f8fa094..6c7340d 100644 (file)
@@ -35,6 +35,7 @@ struct Request
 {
     QPointer<QTcpSocket> socket;
     uint16_t localPort;
+    uint16_t remotePort;
     QString query;
     qint64 transactionId;
     qint64 requestId;
@@ -43,6 +44,8 @@ struct Request
 
     void respondSuccess(const QString& user);
     void respondError(const QString& error);
+
+    const static int DISCONNECTION_TIMEOUT = 500;
 };
 
 class IdentServer : public QObject