/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright (C) 2015 Simon Stuerz * * Copyright (C) 2014 Michael Zanetti * * * * This file is part of guh. * * * * Guh is free software: you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation, version 2 of the License. * * * * Guh is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with guh. If not, see . * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include "tcpserver.h" #include "loggingcategories.h" #include "guhsettings.h" #include #include namespace guhserver { TcpServer::TcpServer(QObject *parent) : QObject(parent) { // Timer for scanning network interfaces ever 5 seconds // Note: QNetworkConfigurationManager does not work on embedded platforms m_timer = new QTimer(this); m_timer->setInterval(5000); connect (m_timer, &QTimer::timeout, this, &TcpServer::onTimeout); // load JSON-RPC server settings GuhSettings settings(GuhSettings::SettingsRoleGlobal); qCDebug(dcConnection) << "Loading TCP server settings from:" << settings.fileName(); settings.beginGroup("JSONRPC"); // load port m_port = settings.value("port", 1234).toUInt(); // load interfaces QStringList interfaceList = settings.value("interfaces", QStringList("all")).toStringList(); if (interfaceList.contains("all")) { m_networkInterfaces = QNetworkInterface::allInterfaces(); } else { foreach (const QNetworkInterface &interface, QNetworkInterface::allInterfaces()) { if (interfaceList.contains(interface.name())) { m_networkInterfaces.append(interface); } } } // load IP versions (IPv4, IPv6 or any) m_ipVersions = settings.value("ip", QStringList("any")).toStringList(); settings.endGroup(); } void TcpServer::sendData(const QList &clients, const QByteArray &data) { foreach (const QUuid &client, clients) { sendData(client, data); } } void TcpServer::sendData(const QUuid &clientId, const QByteArray &data) { QTcpSocket *client = m_clientList.value(clientId); if (client) { client->write(data); } } void TcpServer::newClientConnected() { // got a new client connected QTcpServer *server = qobject_cast(sender()); QTcpSocket *newConnection = server->nextPendingConnection(); qCDebug(dcConnection) << "new client connected:" << newConnection->peerAddress().toString(); QUuid clientId = QUuid::createUuid(); // append the new client to the client list m_clientList.insert(clientId, newConnection); connect(newConnection, SIGNAL(readyRead()),this,SLOT(readPackage())); connect(newConnection,SIGNAL(disconnected()),this,SLOT(slotClientDisconnected())); emit clientConnected(clientId); } void TcpServer::readPackage() { QTcpSocket *client = qobject_cast(sender()); qCDebug(dcConnection) << "data comming from" << client->peerAddress().toString(); QByteArray message; while (client->canReadLine()) { QByteArray dataLine = client->readLine(); qCDebug(dcConnection) << "line in:" << dataLine; message.append(dataLine); if (dataLine.endsWith('\n')) { emit dataAvailable(m_clientList.key(client), message); message.clear(); } } } void TcpServer::slotClientDisconnected() { QTcpSocket *client = qobject_cast(sender()); qCDebug(dcConnection) << "client disconnected:" << client->peerAddress().toString(); QUuid clientId = m_clientList.key(client); m_clientList.take(clientId)->deleteLater(); } void TcpServer::onTimeout() { // // check all networkinterfaces // bool ipV4 = m_ipVersions.contains("IPv4"); // bool ipV6 = m_ipVersions.contains("IPv6"); // if (m_ipVersions.contains("any")) { // ipV4 = ipV6 = true; // } // QList m_serversToCreate; // QList m_serversToDelete; // // check all available interfaces // foreach (const QNetworkInterface &interface, m_networkInterfaces) { // QList addresseEntries = interface.addressEntries(); // QList addresses; // foreach (QNetworkAddressEntry entry, addresseEntries) { // if (ipV4 && entry.ip().protocol() == QAbstractSocket::IPv4Protocol) { // addresses.append(entry.ip()); // } // if (ipV6 && entry.ip().protocol() == QAbstractSocket::IPv6Protocol) { // addresses.append(entry.ip()); // } // } // // check each host address of this interface // foreach (QTcpServer *s, m_serverList) { // if (!addresses.contains(s->serverAddress())) // return; // } // } } bool TcpServer::startServer() { qCDebug(dcConnection) << "----------------------------"; qCDebug(dcConnection) << "JSON-RPC server listening on:"; qCDebug(dcConnection) << "----------------------------"; bool ipV4 = m_ipVersions.contains("IPv4"); bool ipV6 = m_ipVersions.contains("IPv6"); if (m_ipVersions.contains("any")) { ipV4 = ipV6 = true; } foreach (const QNetworkInterface &interface, m_networkInterfaces) { QList addresseEntries = interface.addressEntries(); QList addresses; foreach (QNetworkAddressEntry entry, addresseEntries) { if (ipV4 && entry.ip().protocol() == QAbstractSocket::IPv4Protocol) { addresses.append(entry.ip()); } if (ipV6 && entry.ip().protocol() == QAbstractSocket::IPv6Protocol) { addresses.append(entry.ip()); } } foreach(const QHostAddress &address, addresses){ QTcpServer *server = new QTcpServer(this); if(server->listen(address, m_port)) { qCDebug(dcConnection) << "\tname |" << interface.name(); qCDebug(dcConnection) << "\tip |" << address.toString(); qCDebug(dcConnection) << "\tport |" << m_port; qCDebug(dcConnection) << "\tmac |" << interface.hardwareAddress(); qCDebug(dcConnection) << "\t-----+-------------------"; connect(server, SIGNAL(newConnection()), SLOT(newClientConnected())); m_serverList.insert(QUuid::createUuid(), server); } else { qCWarning(dcConnection) << "ERROR: can not listen to" << interface.name() << address.toString() << m_port; delete server; } } } if (m_serverList.empty()) return false; return true; } bool TcpServer::stopServer() { // Listen on all Networkinterfaces foreach (QTcpServer *server, m_serverList) { qCDebug(dcConnection) << "close server " << server->serverAddress().toString(); server->close(); delete server; } if (!m_serverList.empty()) return false; return true; } }