#include #include #include #include #include #include #include #include #include "logreceiver.h" #include #include #include "status.h" #include "dhcp.h" LogReceiver::LogReceiver() { server = new QLocalServer(this); } LogReceiver::~LogReceiver() { } void LogReceiver::initAndRun(QString serverPath, QString pathToExe , QStringList* args ) { if (serverPath != DEFAULT_QTSOCKETADDRESS) { dhcpcdArguments.append("-q"); dhcpcdArguments.append(serverPath); } if (!server->listen(serverPath)) { /* QMessageBox::critical(this, tr("LogReceiver"), tr( "Unable to start the server: %1.") .arg(server->errorString())); close(); */ // emit signal to the gui that a critial error occoured qDebug() << "--- \t [LogReceiver::initAndRun] Unable to start server:" << server->errorString(); return; } connect(server, SIGNAL(newConnection()), this, SLOT(handleNewConnection())); QList list = getListOfNetworkInterfaces(); //checkCarrierState(list); pathToDhcpcdExe = pathToExe; if(args != NULL && ! args->isEmpty()) { qDebug() << "--- \t [LogReceiver::initAndRun] added additional args"; dhcpcdArguments.append(*args); } //dhcpcdArguments.append("-d"); QString ifName("eth0"); runDHCPCD(ifName); } void LogReceiver::handleNewConnection() { qDebug() << "New Connection arrived"; QLocalSocket * client = server ->nextPendingConnection(); clients.insert(client, client); connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater())); connect(client, SIGNAL(readyRead()), this, SLOT(handleNewInput())); } void LogReceiver::handleNewInput() { QObject* sender = const_cast (QObject::sender()); QLocalSocket* socket = static_cast (sender); QLocalSocket * client = clients.value(socket); QString data(client->readAll()); data = data.trimmed(); QStringList lines = data.split("\n"); for (int i=0; i < lines.length(); i++) { handleNewInputLine(lines.at(i)); } } void LogReceiver::handleNewInputLine(QString data) { QString logMsg(data); QString interface = logMsg.section(";",0,0); QString s_state = logMsg.section(";", 1, 1); QString s_subState = logMsg.section(";", 2, 2); QString msg = logMsg.section(";", 3, 3); int pBar = indexToIfaceNameMap.value(interface.trimmed(), -1); /* if(pBar < 0) { qDebug() << "--- \t no pBar with index: " << pBar; } else { qDebug() << "--- \t get pBar for Interface: " << interface.trimmed() << "index: " << pBar; } */ int st = s_state.trimmed().toInt(); int sst = s_subState.trimmed().toInt(); qDebug() << logMsg; switch (st) { case LOG_INFO: switch (sst) { case DHCP_DISCOVER: emit changeProgressBarValue(pBar, 10); break; case DHCP_OFFER: emit changeProgressBarValue(pBar, 20); break; case DHCP_REQUEST: emit changeProgressBarValue(pBar, 30); break; case DHCP_ACK: emit changeProgressBarValue(pBar, 40); break; case DHCP_NAK: emit changeProgressBarValue(pBar, 40); break; case DHCPCD_ARP_TEST: emit changeProgressBarValue(pBar, 50); break; case DHCP_DECLINE: emit changeProgressBarValue(pBar, 60); break; case DHCP_RELEASE: break; case DHCP_INFORM: break; case DHCPCD_CONFIGURE: emit changeProgressBarValue(pBar, 70); break; case DHCPCD_WRITE: emit changeProgressBarValue(pBar, 80); break; case DHCPCD_EXIT: emit changeProgressBarValue(pBar, 100); break; case DHCPCD_LOG: default: break; } break; case LOG_ERR: qDebug() << "received stat_error"; break; default: qDebug() << logMsg; } } QList LogReceiver::getListOfNetworkInterfaces() { QList nIList = QNetworkInterface::allInterfaces(); QList result; int i = 0; foreach(QNetworkInterface nI, nIList) { if (((!(nI.flags() & QNetworkInterface::CanBroadcast)|| nI.flags() & QNetworkInterface::IsLoopBack) || nI.flags() & QNetworkInterface::IsPointToPoint) || checkBlackList(nI.humanReadableName())) { continue; } if (!checkCarrierState(nI.humanReadableName())) { continue; } // qDebug() << nI.humanReadableName(); result.append(nI); interfacesMap.insert(i, nI); indexToIfaceNameMap.insert(nI.humanReadableName(), i); emit addNewInterface(nI.humanReadableName(), i); i++; } return result; } void LogReceiver::runDHCPCD(QList &interfaces) { foreach(QNetworkInterface ni, interfaces) { dhcpcdArguments.append(ni.humanReadableName()); QProcess * p = new QProcess(this); qDebug() << dhcpcdArguments; clientProcesses.insert(p->pid(),p); p->start(pathToDhcpcdExe,dhcpcdArguments); connect(p, SIGNAL(started()), this, SLOT(handleProcessStarted())); connect(p, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(handleProcessFinished(int, QProcess::ExitStatus))); dhcpcdArguments.removeLast(); } } void LogReceiver::runDHCPCD(QString interface) { dhcpcdArguments.append(interface); QProcess * p = new QProcess(this); qDebug() << dhcpcdArguments; clientProcesses.insert(p->pid(),p); p->start(pathToDhcpcdExe,dhcpcdArguments); connect(p, SIGNAL(started()), this, SLOT(handleProcessStarted())); connect(p, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(handleProcessFinished(int, QProcess::ExitStatus))); dhcpcdArguments.removeLast(); } void LogReceiver::checkCarrierState(QList &interfaces) { foreach(QNetworkInterface nI, interfaces) { if(checkCarrierState(nI.humanReadableName())) { // everything is fine, cable is plugged, // go on with the next interface continue; } else { // cable is unplugged, // remove interface out of the list, // remove interface out ot the index maps int i = indexToIfaceNameMap.value(nI.humanReadableName(), -1); if(i < 0) { qDebug() << "--- \t [LogReceiver::checkCarrierState] no interface with name:" << nI.humanReadableName(); } else { indexToIfaceNameMap.remove(nI.humanReadableName()); interfacesMap.remove(i); interfaces.removeAt(i); } } } } bool LogReceiver::checkCarrierState(QString interface) { qDebug() << "check carrier state for interface " << interface; QByteArray ba = interface.toAscii(); const char * iface = ba.data(); struct sysfs_class_device *class_device = sysfs_open_class_device("net", iface); struct dlist *attrlist = sysfs_get_classdev_attributes(class_device); if (attrlist != NULL) { struct sysfs_attribute *attr = NULL; dlist_for_each_data(attrlist, attr, struct sysfs_attribute) { if (strcmp("carrier", attr->name) == 0) { QString value(attr->value); bool ok = false; bool * pok = &ok; int v = value.toInt(pok); if (*pok) { if (v == 1) { qDebug() << "carrier is 1. Cable is plugged. return true"; return true; } else { qDebug() << "carrier is 0. Cable is unplugged. return false"; return false; } } else { qDebug() << "conversion error"; } } } } else { qDebug() << "attrlist is Null"; } sysfs_close_class_device(class_device); return true; } void LogReceiver::checkInternetConnection(/*QList &interfaces*/) { qDebug() << "check internet connection"; QList allConfigs = manager.allConfigurations(QNetworkConfiguration::Discovered); foreach(QNetworkConfiguration nC, allConfigs) { qDebug() << nC.name(); if(nC.isValid()) { qDebug() << "config is valid"; qDebug() << "try to open a new session"; QNetworkSession session = new QNetworkSession(nC, this); conntect(session,SIGNAL(error), this, SLOT(handleSessionError())); conntect(session,SIGNAL(opened), this, SLOT(handleSessionOpened())); session.open(); session.waitForOpened(); } } qDebug() << "check internet connection done"; } void LogReceiver::handleProcessFinished(int exitCode, QProcess::ExitStatus exitStatus) { QProcess* p = qobject_cast(QObject::sender()); QProcess * client = clientProcesses.value(p->pid(),0); if(client <= 0) { qDebug() << "--- \t [LogReceiver::handleProcessFinished] haven't found process!"; } else { qDebug() << "process finished: " << client->pid() << exitCode << exitStatus; checkInternetConnection(); } } void LogReceiver::handleProcessStarted() { qDebug() << "process started: "; } bool LogReceiver::checkBlackList(QString i) { if (i.startsWith("v", Qt::CaseInsensitive)) { return true; } else if(i.startsWith("d", Qt::CaseInsensitive)) { return true; } else { return false; } }