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()));
102 void IdentServer::respond()
104 QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
107 qint64 transactionId = _socketId;
109 if (!socket->canReadLine()) {
113 QByteArray query = socket->readLine();
114 if (query.endsWith("\r\n"))
116 else if (query.endsWith("\n"))
119 QList<QByteArray> split = query.split(',');
121 bool success = false;
123 quint16 localPort = 0;
124 if (!split.empty()) {
125 localPort = split[0].trimmed().toUShort(&success, 10);
128 Request request{socket, localPort, query, transactionId, _requestId++};
130 request.respondError("INVALID-PORT");
132 else if (responseAvailable(request)) {
135 else if (lowestSocketId() < transactionId) {
136 _requestQueue.emplace_back(request);
139 request.respondError("NO-USER");
144 void Request::respondSuccess(const QString &user)
146 QString data = query + " : USERID : Quassel : " + user + "\r\n";
148 socket->write(data.toUtf8());
154 void Request::respondError(const QString &error)
156 QString data = query + " : ERROR : " + error + "\r\n";
158 socket->write(data.toUtf8());
164 bool IdentServer::responseAvailable(Request request) const
166 if (!_connections.contains(request.localPort)) {
170 request.respondSuccess(_connections[request.localPort]);
175 void IdentServer::addSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort,
176 const QHostAddress &peerAddress, quint16 peerPort, qint64 socketId)
178 Q_UNUSED(localAddress)
179 Q_UNUSED(peerAddress)
182 const CoreNetwork *network = qobject_cast<CoreNetwork *>(sender());
183 _connections[localPort] = network->coreSession()->strictCompliantIdent(identity);;
184 processWaiting(socketId);
188 void IdentServer::removeSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort,
189 const QHostAddress &peerAddress, quint16 peerPort, qint64 socketId)
192 Q_UNUSED(localAddress)
193 Q_UNUSED(peerAddress)
196 _connections.remove(localPort);
197 processWaiting(socketId);
201 qint64 IdentServer::addWaitingSocket()
203 qint64 newSocketId = _socketId++;
204 _waiting.push_back(newSocketId);
209 qint64 IdentServer::lowestSocketId() const
211 if (_waiting.empty()) {
212 return std::numeric_limits<qint64>::max();
215 return _waiting.front();
219 void IdentServer::removeWaitingSocket(qint64 socketId)
221 _waiting.remove(socketId);
225 void IdentServer::processWaiting(qint64 socketId)
227 removeWaitingSocket(socketId);
229 _requestQueue.remove_if([this, socketId](Request request) {
230 if (socketId < request.transactionId && responseAvailable(request)) {
233 else if (lowestSocketId() < request.transactionId) {
237 request.respondError("NO-USER");
244 bool operator==(const Request &a, const Request &b)
246 return a.requestId == b.requestId;