1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
23 #include "corenetwork.h"
24 #include "identserver.h"
27 IdentServer::IdentServer(bool strict, QObject *parent)
31 connect(&_server, SIGNAL(newConnection()), this, SLOT(incomingConnection()));
32 connect(&_v6server, SIGNAL(newConnection()), this, SLOT(incomingConnection()));
36 bool IdentServer::startListening()
38 uint16_t port = Quassel::optionValue("ident-port").toUShort();
41 if (_v6server.listen(QHostAddress("::1"), port)) {
42 quInfo() << qPrintable(
43 tr("Listening for identd clients on IPv6 %1 port %2")
45 .arg(_v6server.serverPort())
51 if (_server.listen(QHostAddress("127.0.0.1"), port)) {
52 quInfo() << qPrintable(
53 tr("Listening for identd clients on IPv4 %1 port %2")
55 .arg(_server.serverPort())
62 quError() << qPrintable(tr("Identd could not open any network interfaces to listen on! No identd functionality will be available"));
69 void IdentServer::stopListening(const QString &msg)
71 bool wasListening = false;
73 if (_server.isListening()) {
77 if (_v6server.isListening()) {
84 quInfo() << "No longer listening for identd clients.";
86 quInfo() << qPrintable(msg);
91 void IdentServer::incomingConnection()
93 auto server = qobject_cast<QTcpServer *>(sender());
95 while (server->hasPendingConnections()) {
96 QTcpSocket *socket = server->nextPendingConnection();
97 connect(socket, SIGNAL(readyRead()), this, SLOT(respond()));
98 connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
103 void IdentServer::respond()
105 QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
108 qint64 transactionId = _socketId;
110 if (!socket->canReadLine()) {
114 QByteArray query = socket->readLine();
115 if (query.endsWith("\r\n"))
117 else if (query.endsWith("\n"))
120 QList<QByteArray> split = query.split(',');
122 bool success = false;
124 quint16 localPort = 0;
125 if (!split.empty()) {
126 localPort = split[0].trimmed().toUShort(&success, 10);
129 Request request{socket, localPort, query, transactionId, _requestId++};
131 request.respondError("INVALID-PORT");
133 else if (responseAvailable(request)) {
136 else if (lowestSocketId() < transactionId) {
137 _requestQueue.emplace_back(request);
140 request.respondError("NO-USER");
145 void Request::respondSuccess(const QString &user)
148 QString data = query + " : USERID : Quassel : " + user + "\r\n";
149 socket->write(data.toUtf8());
156 void Request::respondError(const QString &error)
159 QString data = query + " : ERROR : " + error + "\r\n";
160 socket->write(data.toUtf8());
167 bool IdentServer::responseAvailable(Request request) const
169 if (!_connections.contains(request.localPort)) {
173 request.respondSuccess(_connections[request.localPort]);
178 void IdentServer::addSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort,
179 const QHostAddress &peerAddress, quint16 peerPort, qint64 socketId)
181 Q_UNUSED(localAddress)
182 Q_UNUSED(peerAddress)
185 const CoreNetwork *network = qobject_cast<CoreNetwork *>(sender());
186 _connections[localPort] = network->coreSession()->strictCompliantIdent(identity);;
187 processWaiting(socketId);
191 void IdentServer::removeSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort,
192 const QHostAddress &peerAddress, quint16 peerPort, qint64 socketId)
195 Q_UNUSED(localAddress)
196 Q_UNUSED(peerAddress)
199 _connections.remove(localPort);
200 processWaiting(socketId);
204 qint64 IdentServer::addWaitingSocket()
206 qint64 newSocketId = _socketId++;
207 _waiting.push_back(newSocketId);
212 qint64 IdentServer::lowestSocketId() const
214 if (_waiting.empty()) {
215 return std::numeric_limits<qint64>::max();
218 return _waiting.front();
222 void IdentServer::removeWaitingSocket(qint64 socketId)
224 _waiting.remove(socketId);
228 void IdentServer::processWaiting(qint64 socketId)
230 removeWaitingSocket(socketId);
232 _requestQueue.remove_if([this, socketId](Request request) {
233 if (socketId < request.transactionId && responseAvailable(request)) {
236 else if (lowestSocketId() < request.transactionId) {
240 request.respondError("NO-USER");
247 bool operator==(const Request &a, const Request &b)
249 return a.requestId == b.requestId;