From f1e28920f6018da7b6ed3bb5e118b0b302ebeb26 Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Wed, 22 Feb 2012 11:59:46 +0100 Subject: code formatting --- src/fbgui/networkdiscovery.cpp | 459 ++++++++++++++++------------------------- src/fbgui/networkdiscovery.h | 154 +++++++------- 2 files changed, 261 insertions(+), 352 deletions(-) diff --git a/src/fbgui/networkdiscovery.cpp b/src/fbgui/networkdiscovery.cpp index ab14923..4e08e80 100644 --- a/src/fbgui/networkdiscovery.cpp +++ b/src/fbgui/networkdiscovery.cpp @@ -46,24 +46,9 @@ NetworkDiscovery::~NetworkDiscovery() { /** * initialize all important class members and start the main work. * - * @param serverIp - * the ip of the server with which we are testing the connectivity. - * * @param userChoice * true if the user wishes to have a user choice. true: the chooseInterfaceDialog will be showed. * - * @param autoUp - * true if we want to "auto Up" all down interfaces. - * - * @param pathToLogFile - * the path to the log file. - * - * @param serverPath - * the path to the server socket (default value: DEFAULT_QTSOCKETADDRESS "/var/tmp/qt_c_socket_default") - * - * @param pathToExe - * the path to the customdhcpcd exe. (default value: #define DEFAULT_QTSOCKETADDRESS "/var/tmp/qt_c_socket_default") - * * @param args * additional arguments for the customdhcpcd client. (default value: NULL) */ @@ -130,11 +115,13 @@ void NetworkDiscovery::initAndRun(bool userChoice, QStringList* args) { if (args != NULL && !args->isEmpty()) { LOG4CXX_DEBUG(ndcLogger, "added additional args"); _dhcpcdArguments.append(*args); - } emit - updateStatus("start mainwork"); + } + emit updateStatus("start mainwork"); mainWork(); } - +//------------------------------------------------------------------------- +// Main Network Discovery Flow +//------------------------------------------------------------------------- /** * @brief the main work. Here we start with searching for usable interfaces and check the IsRunning state. * @@ -149,8 +136,7 @@ void NetworkDiscovery::mainWork() { getListOfNetworkInterfaces(); - emit - updateStatus("check if interfaces are in running state"); + emit updateStatus("check if interfaces are in running state"); _timer = new QTimer(this); connect(_timer, SIGNAL(timeout()), this, SLOT(checkForIsRunning())); _timer->start(1000); @@ -208,6 +194,28 @@ void NetworkDiscovery::getListOfNetworkInterfaces() { } } +/** + * This Method implements a blacklist. + * + * This Method implements a blacklist. We check the fist character + * of the interface name. if this letter is in the list, we return true. + * True means, that this interface won't be put into the result list of + * getListOfNetworkInterfaces(). + * + * @param i + * is a interface name. + * + * @return bool + * returns true: if the interface name i starts with a letter in the blacklist. + * + * returns false: else + * + * @see NetworkDiscovery::getListOfNetworkInterfaces() + */ +bool NetworkDiscovery::checkBlackList(QString i) { + return (i.startsWith("v", Qt::CaseInsensitive) || i.startsWith("d", Qt::CaseInsensitive)); +} + /** * only called if autoUp == true. * check the IsRunning flag of each interface in the _ifDownList. @@ -229,8 +237,7 @@ void NetworkDiscovery::checkForIsRunning() { // shut down timer _timer->stop(); delete _timer; - //emit readyForRun(); - slotReadyForRun(); + readyForRun(); } } @@ -240,14 +247,15 @@ void NetworkDiscovery::checkForIsRunning() { * if the _ifUpList is empty, this method emits an abortBoot signal. * connected to the readyForRun signal. */ -void NetworkDiscovery::slotReadyForRun() { +void NetworkDiscovery::readyForRun() { if (_ifUpList.size() > 0) { foreach(QString i, _ifUpList) { LOG4CXX_DEBUG(ndcLogger, "emit addInterface : " << i); emit addInterface(i); } _numberOfProcesses = _ifUpList.size(); - emit updateStatus("start dhcp client for each interface"); + emit + updateStatus("start dhcp client for each interface"); runDHCPCD(_ifUpList); } else { LOG4CXX_DEBUG(ndcLogger, @@ -261,7 +269,9 @@ void NetworkDiscovery::slotReadyForRun() { return; } } - +//------------------------------------------------------------------------- +// DHCPCD Control +//------------------------------------------------------------------------- /** * call for every interface in the list the runDHCPCD method. * @@ -385,6 +395,20 @@ void NetworkDiscovery::handleProcessFinished(int exitCode, } } +/**/ +void NetworkDiscovery::killDHCPCD() { + LOG4CXX_DEBUG(ndcLogger, "kill cdhcpcd processes"); + QProcess * p = new QProcess(this); + p->start("killall cdhcpcd"); + p->waitForFinished(); + if (p->exitCode() > 0) + LOG4CXX_DEBUG(ndcLogger, "[tryAgain] " << p->errorString()); + + delete p; +} +//------------------------------------------------------------------------- +// Connectivity Testing +//------------------------------------------------------------------------- /** * checks the connectivity. tries to open a TCP connection to the * server (see _serverIp). For this it adjusts the routing table. @@ -398,7 +422,9 @@ void NetworkDiscovery::handleProcessFinished(int exitCode, * @return * true: connection is possible * false: connection not possible - */bool NetworkDiscovery::checkConnectivity(QString ifName) { + */ +bool NetworkDiscovery::checkConnectivity(QString ifName) { + int mss = 0; // get gateway address @@ -433,7 +459,8 @@ void NetworkDiscovery::handleProcessFinished(int exitCode, } } -/**/bool NetworkDiscovery::checkConnectivityViaTcp() { +/**/ + bool NetworkDiscovery::checkConnectivityViaTcp() { return checkConnectivityViaTcp(gServerIp); } /** @@ -445,7 +472,8 @@ void NetworkDiscovery::handleProcessFinished(int exitCode, * @return * true: connection is possible * false: connection not possible - */bool NetworkDiscovery::checkConnectivityViaTcp(QString server) { + */ + bool NetworkDiscovery::checkConnectivityViaTcp(QString server) { // check connectivity via tcp connection LOG4CXX_DEBUG(ndcLogger, "check connectivity to server: " << server); // do host lookup @@ -462,6 +490,86 @@ void NetworkDiscovery::handleProcessFinished(int exitCode, delete tcpSocket; } +//------------------------------------------------------------------------- +// Manual Configuration +//------------------------------------------------------------------------- + +/** + * replace the dhcp configuration with the manual config, entered by the user. + * if we can not establish a connection with the entered values, reset to the old + * dhcp values. + * + * @param result + * a json object formated string. + * + * @return + * 0 if everything ok + */ +int NetworkDiscovery::ip4_setManualConfiguration(QVariantMap result) { + QList dns; + dns.append(result["dns"].toString()); + _networkManager->ip4_setManualConfiguration(result["ifname"].toString(), + result["ipaddr"].toString(), result["netmask"].toString(), + result["broadcast"].toString(), result["gateway"].toString(), 0, + AF_INET, "/etc/", dns); + + LOG4CXX_DEBUG(ndcLogger, "set man conf. and check connectivity"); + + if (!checkConnectivityViaTcp(gServerIp)) { + LOG4CXX_DEBUG(ndcLogger, "no connectivity. reset conf."); + InterfaceConfiguration * ifc = _ifcMap.value(result["ifname"].toString(), + NULL); + if (ifc != NULL) { + dns.clear(); + dns = ifc->getDnsservers().trimmed().split(" "); + _networkManager->ip4_setManualConfiguration( + result["ifname"].toString(), ifc->getIpAddress(), + ifc->getNetmask(), ifc->getBroadcast(), ifc->getGateway(), 0, + AF_INET, "/etc/", dns); + } + + return 0; + }LOG4CXX_DEBUG( + ndcLogger, + "emit signal continueBootWithoutCheck(" << result["ifname"].toString() << ")"); + emit + continueBootWithoutCheck(result["ifname"].toString()); + return 0; +} + +//------------------------------------------------------------------------- +// Socket Connection Handling +//------------------------------------------------------------------------- + +/** + * connected to the new client arrived signal. + * connects the client readyRead signal with the handleNewInput slot. + */ +void NetworkDiscovery::handleNewConnection() { + LOG4CXX_DEBUG(ndcLogger, "New Connection arrived"); + + /*QLocalSocket **/ + _client = _server->nextPendingConnection(); + _clients.insert(_client, _client); + connect(_client, SIGNAL(disconnected()), this, + SLOT(handleClientDisconnect())); + connect(_client, SIGNAL(readyRead()), this, SLOT(handleNewInput())); +} + +/** + * called when a client disconnects. + */ +void NetworkDiscovery::handleClientDisconnect() { + QLocalSocket* socket = qobject_cast(QObject::sender()); + + QLocalSocket * client = _clients.value(socket); + + LOG4CXX_DEBUG(ndcLogger, "disconnect client"); + handleNewInput(client); + client->deleteLater(); +} + + /** * same function as handleNewInput() but with a client as parameter. * @@ -630,271 +738,68 @@ void NetworkDiscovery::handleNewInputLine(QLocalSocket * client, } } +//------------------------------------------------------------------------- +// Public member access +//------------------------------------------------------------------------- /** - * replace the default route. sets af automatically to AF_INET - * - * @param ifName - * interface name - * - * @param gateway - * gateway address - * - * @param mss - * mss value (i think this is the metric. in most cases this value is 0) - */ -int NetworkDiscovery::ip4_replaceDefaultRoute(QString ifName, QString gateway, - int mss) { - return _networkManager->replaceDefaultRoute(ifName, gateway, mss, AF_INET); -} - -/** - * replace the dhcp configuration with the manual config, entered by the user. - * if we can not establish a connection with the entered values, reset to the old - * dhcp values. + * @brief Return the InterfaceConfiguration for the given interface. * - * @param result - * a json object formated string. + * @param Interface name as QString * - * @return - * 0 if everything ok + * @return InterfaceConfiguration* */ -int NetworkDiscovery::ip4_setManualConfiguration(QVariantMap result) { - QList dns; - dns.append(result["dns"].toString()); - _networkManager->ip4_setManualConfiguration(result["ifname"].toString(), - result["ipaddr"].toString(), result["netmask"].toString(), - result["broadcast"].toString(), result["gateway"].toString(), 0, - AF_INET, "/etc/", dns); - - LOG4CXX_DEBUG(ndcLogger, "set man conf. and check connectivity"); - - if (!checkConnectivityViaTcp(gServerIp)) { - LOG4CXX_DEBUG(ndcLogger, "no connectivity. reset conf."); - InterfaceConfiguration * ifc = _ifcMap.value(result["ifname"].toString(), - NULL); - if (ifc != NULL) { - dns.clear(); - dns = ifc->getDnsservers().trimmed().split(" "); - _networkManager->ip4_setManualConfiguration( - result["ifname"].toString(), ifc->getIpAddress(), - ifc->getNetmask(), ifc->getBroadcast(), ifc->getGateway(), 0, - AF_INET, "/etc/", dns); - } - - return 0; - }LOG4CXX_DEBUG( - ndcLogger, - "emit signal continueBootWithoutCheck(" << result["ifname"].toString() << ")"); - emit - continueBootWithoutCheck(result["ifname"].toString()); - return 0; -} - -/** - * returns the gateway address, written into the dhcp config file. - * - * @param ifName - * name of the interface. - * - * @return - * gateway address as string. or empty string if no interface config was found. - */ -QString NetworkDiscovery::getGatewayForInterface(QString ifName) { - InterfaceConfiguration * ifConf = _ifcMap.value(ifName, NULL); - if (ifConf != NULL) { - return ifConf->getGateway(); - } else { - LOG4CXX_DEBUG(ndcLogger, "could not find interface configuration"); - return ""; - } - -} - -/** - * reads the log file. - * - * @return the log file as one string. - */ -QString NetworkDiscovery::readLogFile() { - // path to log file is in _pathToLogFile. initialized in initAndRun(). - QString retval("the log file:\n"); - QFile logFile(logFilePath); - if (logFile.exists()) { - if (logFile.open(QIODevice::ReadOnly | QIODevice::Text)) { - while (!logFile.atEnd()) { - retval.append(logFile.readLine()); - } - } else { - LOG4CXX_DEBUG(ndcLogger, "could not open log file"); - retval = "could not open log file"; - } - } else { - LOG4CXX_DEBUG(ndcLogger, "log file does not exist at: " << logFilePath); - retval = " log file does not exist at: " + logFilePath; - } - return retval; - -} - -/**/ -void NetworkDiscovery::killDHCPCD() { - LOG4CXX_DEBUG(ndcLogger, "kill cdhcpcd processes"); - QProcess * p = new QProcess(this); - p->start("killall cdhcpcd"); - p->waitForFinished(); - if (p->exitCode() > 0) - LOG4CXX_DEBUG(ndcLogger, "[tryAgain] " << p->errorString()); - - delete p; -} - -/**/ -void NetworkDiscovery::prepareTryAgain() { - // kill all cdhcpcd processes - killDHCPCD(); - - /* - foreach(Q_PID pid , _pidsList) { - if (kill(pid,SIGKILL) <= -1) - qDebug() << " error: trying to kill process: " << pid << " error: " << strerror(errno); - } - */ - // reset everything - //delete _networkManager; - //delete _server; - foreach(QProcess* p, _clientProcessToIfNameMap.keys()) - { - delete p; - } - foreach(InterfaceConfiguration* i, _ifcMap.values()) - { - delete i; - } -} - -/* - *TODO: to be bug fixed - *TODO: do it with kill and not QProcess("killall cdhcpcd") - */ -void NetworkDiscovery::tryAgain() { - prepareTryAgain(); - initAndRun(_userChoice); -} - -/**/ InterfaceConfiguration* NetworkDiscovery::getInterfaceConfig(QString ifName) { return _ifcMap.value(ifName, NULL); } /** - * connected to the new client arrived signal. - * connects the client readyRead signal with the handleNewInput slot. + * @brief Returns the list of interfaces that are up. + * + * @return The QList member. */ -void NetworkDiscovery::handleNewConnection() { - LOG4CXX_DEBUG(ndcLogger, "New Connection arrived"); - - /*QLocalSocket **/ - _client = _server->nextPendingConnection(); - _clients.insert(_client, _client); - connect(_client, SIGNAL(disconnected()), this, - SLOT(handleClientDisconnect())); - connect(_client, SIGNAL(readyRead()), this, SLOT(handleNewInput())); +QList NetworkDiscovery::getIfUpList() { + return _ifUpList; } -/** - * called when a client disconnects. - */ -void NetworkDiscovery::handleClientDisconnect() { - QLocalSocket* socket = qobject_cast(QObject::sender()); - QLocalSocket * client = _clients.value(socket); - - LOG4CXX_DEBUG(ndcLogger, "disconnect client"); - handleNewInput(client); - client->deleteLater(); -} - -/** - * This Method implements a blacklist. - * - * This Method implements a blacklist. We check the fist character - * of the interface name. if this letter is in the list, we return true. - * True means, that this interface won't be put into the result list of - * getListOfNetworkInterfaces(). - * - * @param i - * is a interface name. - * - * @return bool - * returns true: if the interface name i starts with a letter in the blacklist. - * - * returns false: else - * - * @see NetworkDiscovery::getListOfNetworkInterfaces() - */bool NetworkDiscovery::checkBlackList(QString i) { - if (i.startsWith("v", Qt::CaseInsensitive)) { - return true; - } else if (i.startsWith("d", Qt::CaseInsensitive)) { - return true; - } else { - return false; - } -} /**/ QString NetworkDiscovery::GetErrorStr() { return _errorStr; } -////////////////////////////////////////////////////////////////////////////////////// - -/** - * not used so far. checks the carrier state using the sysfs library. - * if carrier = 1 ==> the interface is running. - * interfaces have to be up in order to get right results. - * - * @param interface - * name of the interface - */bool NetworkDiscovery::checkCarrierState(QString interface) { - - LOG4CXX_DEBUG(ndcLogger, "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) { - LOG4CXX_DEBUG(ndcLogger, - "carrier is 1. Cable is plugged. return true"); - return true; - } else { - LOG4CXX_DEBUG( ndcLogger, - "carrier is 0. Cable is unplugged. return false"); - return false; - } - } else { - LOG4CXX_DEBUG(ndcLogger, "conversion error"); - } - } - } - } else { - LOG4CXX_DEBUG(ndcLogger, "attrlist is Null"); - } - sysfs_close_class_device(class_device); - - return true; -} - -QList NetworkDiscovery::getIfUpList() { - return _ifUpList; -} + //------------------------------------------------------------------------- + // Bugged Methods.... + //------------------------------------------------------------------------- + + /* + *TODO: to be bug fixed + *TODO: do it with kill and not QProcess("killall cdhcpcd") + */ + void NetworkDiscovery::tryAgain() { + prepareTryAgain(); + initAndRun(_userChoice); + } + /**/ + void NetworkDiscovery::prepareTryAgain() { + // kill all cdhcpcd processes + killDHCPCD(); + + /* + foreach(Q_PID pid , _pidsList) { + if (kill(pid,SIGKILL) <= -1) + qDebug() << " error: trying to kill process: " << pid << " error: " << strerror(errno); + } + */ + // reset everything + //delete _networkManager; + //delete _server; + foreach(QProcess* p, _clientProcessToIfNameMap.keys()) + { + delete p; + } + foreach(InterfaceConfiguration* i, _ifcMap.values()) + { + delete i; + } + } diff --git a/src/fbgui/networkdiscovery.h b/src/fbgui/networkdiscovery.h index 4f057e0..f98566f 100644 --- a/src/fbgui/networkdiscovery.h +++ b/src/fbgui/networkdiscovery.h @@ -11,8 +11,6 @@ * General information about OpenSLX can be found under http://openslx.org */ - - #ifndef NetworkDiscovery_H #define NetworkDiscovery_H @@ -30,94 +28,100 @@ #include "interfaceconfiguration.h" #include "networkmanager.h" - #include "dhcp.h" #include "globals.h" #include "../common/fbgui.h" -class NetworkDiscovery: public QObject -{ - Q_OBJECT +class NetworkDiscovery: public QObject { +Q_OBJECT public: - NetworkDiscovery(QObject *parent = 0); - ~NetworkDiscovery(); - - void initAndRun(bool userChoice, QStringList* args = NULL); - int ip4_replaceDefaultRoute(QString ifName, QString gateway, int mss); - QString getGatewayForInterface(QString ifName); - - int ip4_setManualConfiguration(QVariantMap result); - QString readLogFile(); - void tryAgain(); - void prepareTryAgain(); - bool checkConnectivity(QString ifName); - bool checkConnectivityViaTcp(); - InterfaceConfiguration* getInterfaceConfig(QString ifName); - QString GetErrorStr(); - QList getIfUpList(); + NetworkDiscovery(QObject *parent = 0); + ~NetworkDiscovery(); + + void initAndRun(bool userChoice, QStringList* args = NULL); + int ip4_replaceDefaultRoute(QString ifName, QString gateway, int mss); + + + int ip4_setManualConfiguration(QVariantMap result); + void tryAgain(); + void prepareTryAgain(); + bool checkConnectivity(QString ifName); + bool checkConnectivityViaTcp(); + + // Public member access + QList getIfUpList(); + InterfaceConfiguration* getInterfaceConfig(QString ifName); + QString GetErrorStr(); + + QString getGatewayForInterface(QString ifName); + public slots: - void handleNewConnection(); - void handleNewInput(); - void handleNewInputLine(QLocalSocket * client, QString data); - void handleProcessFinished(int exitCode, QProcess::ExitStatus exitStatus); - void handleProcessStarted(); - void handleClientDisconnect(); - void slotReadyForRun(); + void checkForIsRunning(); + + void handleNewConnection(); + void handleClientDisconnect(); - void checkForIsRunning(); + void handleNewInput(); + void handleNewInputLine(QLocalSocket * client, QString data); + void handleProcessStarted(); + void handleProcessFinished(int exitCode, QProcess::ExitStatus exitStatus); + + void readyForRun(); signals: - void addInterface(const QString &ifName); - void changeProgressBarValue(const QString & ifName, const int $newValue); - void connectionEstablished(QString ifName); - void abortBoot(QString msg); - void updateIfStatus(QString ifName, QString status); - void updateStatus(QString status); - void allProcessesFinished(); - void continueBoot(QString ifName); - void continueBootWithoutCheck(QString ifName); - void setManualConfInterfaces(QString jsonArr); - - void readyForRun(); + void addInterface(const QString &ifName); + void changeProgressBarValue(const QString & ifName, const int $newValue); + void connectionEstablished(QString ifName); + void abortBoot(QString msg); + void updateIfStatus(QString ifName, QString status); + void updateStatus(QString status); + void allProcessesFinished(); + void continueBoot(QString ifName); + void continueBootWithoutCheck(QString ifName); + void setManualConfInterfaces(QString jsonArr); + + //void readyForRun(); private: - QLocalServer *_server; - QMap _clients; - QLocalSocket *_client; - QMap _ifNameToClient; - QMap _clientProcessToIfNameMap; - QStringList _dhcpcdArguments; - int _numberOfProcesses; - NetworkManager* _networkManager; - bool _userChoice; - bool _blocked; - QList _ifUpList; - QList _ifDownList; - int _ifUpCountdown; - QTimer*_timer; - QList _pidsList; - QString _errorStr; - QMap _ifcMap; - void mainWork(); - - void killDHCPCD(); - - void handleNewInput(QLocalSocket * client); - - void runDHCPCD(QList &interfaces); - void runDHCPCD(QString interface); - - bool checkCarrierState(QString interface); - bool checkConnectivityViaTcp(QString server); - - bool checkBlackList(QString i); - - void getListOfNetworkInterfaces(); - void getListOfNetworkInterfacesWithAutoUp(); + + void mainWork(); + + void killDHCPCD(); + + void handleNewInput(QLocalSocket * client); + + void runDHCPCD(QList &interfaces); + void runDHCPCD(QString interface); + + bool checkCarrierState(QString interface); + bool checkConnectivityViaTcp(QString server); + + bool checkBlackList(QString i); + + void getListOfNetworkInterfaces(); + void getListOfNetworkInterfacesWithAutoUp(); + + QLocalServer *_server; + QMap _clients; + QLocalSocket *_client; + QMap _ifNameToClient; + QMap _clientProcessToIfNameMap; + QStringList _dhcpcdArguments; + int _numberOfProcesses; + NetworkManager* _networkManager; + bool _userChoice; + bool _blocked; + QList _ifUpList; + QList _ifDownList; + int _ifUpCountdown; + QTimer*_timer; + QList _pidsList; + QString _errorStr; + QMap _ifcMap; }; #endif // NetworkDiscovery_H -- cgit v1.2.3-55-g7522