From 46da51f8701747376999dcca8f034e41eeb206e0 Mon Sep 17 00:00:00 2001 From: Niklas Date: Thu, 29 Sep 2011 14:24:48 +0200 Subject: did some refactoring. added a user choice screen which is shown 2 seconds before the magic begins. (press F5 to get a choice) --- LogReceiver/html/networkdiscovery_userchoice.html | 22 ++++++ LogReceiver/nd.qrc | 1 + LogReceiver/ndgui.cpp | 44 +++++++++--- LogReceiver/ndgui.h | 16 +++-- LogReceiver/networkdiscovery.cpp | 87 ++++++++++++----------- LogReceiver/networkdiscovery.h | 21 +++--- LogReceiver/networkmanager.cpp | 74 ++++++++++++++++++- LogReceiver/networkmanager.h | 7 +- 8 files changed, 199 insertions(+), 73 deletions(-) create mode 100644 LogReceiver/html/networkdiscovery_userchoice.html diff --git a/LogReceiver/html/networkdiscovery_userchoice.html b/LogReceiver/html/networkdiscovery_userchoice.html new file mode 100644 index 0000000..9cf165f --- /dev/null +++ b/LogReceiver/html/networkdiscovery_userchoice.html @@ -0,0 +1,22 @@ + + + + + + + + + + + +
+

Network Discovery

+
+
+

Please press F5 if you want to choose which interface to use.

+
+ + + diff --git a/LogReceiver/nd.qrc b/LogReceiver/nd.qrc index 1719e6c..686419e 100644 --- a/LogReceiver/nd.qrc +++ b/LogReceiver/nd.qrc @@ -22,5 +22,6 @@ html/css/images/ui-icons_ffd27a_256x240.png html/css/images/ui-icons_ffffff_256x240.png html/continueBoot.html + html/networkdiscovery_userchoice.html diff --git a/LogReceiver/ndgui.cpp b/LogReceiver/ndgui.cpp index cb2a2f9..be01224 100644 --- a/LogReceiver/ndgui.cpp +++ b/LogReceiver/ndgui.cpp @@ -3,6 +3,11 @@ ndgui::ndgui(QMainWindow *parent) : QMainWindow(parent) { + _started = false; + _userChoice = false; + + createAction(); + connect(&networkDiscovery, SIGNAL(addInterface(const QString &)), this, SLOT(addInterface( const QString &))); connect(&networkDiscovery, SIGNAL(changeProgressBarValue(const QString & , const int& )), this, SLOT(updateIfProgressBar(const QString & , const int&))); connect(&networkDiscovery, SIGNAL(connectionEstablished(QString)), this, SLOT(handleConnectionEstablished(QString))); @@ -11,34 +16,53 @@ ndgui::ndgui(QMainWindow *parent) : connect(&networkDiscovery, SIGNAL(allProcessesFinished()), this, SLOT(handleAllProcessesFinished())); connect(&networkDiscovery, SIGNAL(continueBoot(QString, int)), this, SLOT(continueBoot(QString, int))); - _started = false; - _manualConfInterfaces = "[\"NO INTERFACE\"]"; + _webView = new QWebView(this); connect(_webView->page()->mainFrame(), SIGNAL( javaScriptWindowObjectCleared()), this, SLOT(attachToDOM())); - connect(_webView, SIGNAL(loadFinished(bool)), this, - SLOT(startNetworkDiscovery())); - - - setCentralWidget(_webView); - _webView->load(QUrl("qrc:html/networkdiscovery.html")); - _webView->show(); setWindowTitle(tr("NetD")); setAttribute(Qt::WA_QuitOnClose, true); setWindowFlags(Qt::FramelessWindowHint); + _webView->load(QUrl("qrc:html/networkdiscovery_userchoice.html")); + _webView->show(); + + QTimer::singleShot(2000, this, SLOT(prepareNetworkDiscover())); + } ndgui::~ndgui() { } +void ndgui::createAction() { + _allowUserChoice = new QAction(tr("&quit"), this); + _allowUserChoice->setShortcut(QKeySequence(Qt::Key_F5)); + connect(_allowUserChoice, SIGNAL(triggered()), this, SLOT(setUserChoiceTrue())); + this->addAction(_allowUserChoice); +} + +void ndgui::setUserChoiceTrue() { + _userChoice = true; +} + +void ndgui::prepareNetworkDiscover() { + connect(_webView, SIGNAL(loadFinished(bool)), this, + SLOT(startNetworkDiscovery())); + this->removeAction(_allowUserChoice); + + _webView->load(QUrl("qrc:html/networkdiscovery.html")); + _webView->show(); +} + void ndgui::startNetworkDiscovery(){ + + disconnect(_webView,SIGNAL(loadFinished(bool)), this, SLOT(startNetworkDiscovery())); if(!_started) { _started = true; - networkDiscovery.initAndRun("209.85.148.105", true,"/var/tmp/qt_c_socket_custom"); + networkDiscovery.initAndRun("209.85.148.105", _userChoice,"/var/tmp/qt_c_socket_custom"); } else { qDebug() << "NetworkDiscovery already started"; diff --git a/LogReceiver/ndgui.h b/LogReceiver/ndgui.h index f841cc9..e0c12d1 100644 --- a/LogReceiver/ndgui.h +++ b/LogReceiver/ndgui.h @@ -11,7 +11,6 @@ class ndgui: public QMainWindow { Q_OBJECT -Q_PROPERTY(QString manualConfInterfaces READ manualConfInterfaces WRITE setManualConfInterfaces) public: ndgui(QMainWindow *parent = 0); @@ -30,6 +29,7 @@ public slots: void continueBoot(QString ifName, int userChoice); void showLog(); + void prepareNetworkDiscover(); void startNetworkDiscovery(); /*test for html gui version*/ @@ -41,19 +41,21 @@ public slots: void updateIfProgressBar(const QString &ifName, const int& percent); void notifyCall(QString msg); - // property functions - void setManualConfInterfaces(QString jsonArray) { - _manualConfInterfaces = jsonArray; - } - QString manualConfInterfaces() const - {return _manualConfInterfaces;} +private slots: + void setUserChoiceTrue(); private: + void createAction(); + + bool _userChoice; + bool _started; QWebView * _webView; + QAction * _allowUserChoice; + NetworkDiscovery networkDiscovery; QList _ifNameList; // maps interfaceName to its gateway diff --git a/LogReceiver/networkdiscovery.cpp b/LogReceiver/networkdiscovery.cpp index 1677573..a526a6f 100644 --- a/LogReceiver/networkdiscovery.cpp +++ b/LogReceiver/networkdiscovery.cpp @@ -4,7 +4,7 @@ NetworkDiscovery::NetworkDiscovery(QObject *parent) { - server = new QLocalServer(this); + _server = new QLocalServer(this); } NetworkDiscovery::~NetworkDiscovery() { @@ -17,12 +17,22 @@ void NetworkDiscovery::initAndRun(QString serverIp, bool userChoice,QString serv _serverIp = serverIp; _userChoice = userChoice; _blocked = false; + _pathToDhcpcdExe = pathToExe; + _numberOfProcesses = 0; if (serverPath != DEFAULT_QTSOCKETADDRESS) { - dhcpcdArguments.append("-q"); - dhcpcdArguments.append(serverPath); + _dhcpcdArguments.append("-q"); + _dhcpcdArguments.append(serverPath); } - if (!server->listen(serverPath)) { + /* delete the file at serverPath. this is necessary since in case the application crashes, the file still + * exists which leads to an error. + */ + + if(QFile::exists(serverPath)) { + QFile::remove(serverPath); + } + + if (!_server->listen(serverPath)) { /* QMessageBox::critical(this, tr("NetworkDiscovery"), tr( "Unable to start the server: %1.") .arg(server->errorString())); @@ -31,17 +41,17 @@ void NetworkDiscovery::initAndRun(QString serverIp, bool userChoice,QString serv // emit signal to the gui that a critial error occoured QString errorInfo("Unable to start server: "); qDebug() << "--- \t [NetworkDiscovery::initAndRun] " + errorInfo - << server->errorString(); + << _server->errorString(); emit - abortBoot(errorInfo + server->errorString()); + abortBoot(errorInfo + _server->errorString()); return; } - connect(server, SIGNAL(newConnection()), this, SLOT(handleNewConnection())); + connect(_server, SIGNAL(newConnection()), this, SLOT(handleNewConnection())); + - pathToDhcpcdExe = pathToExe; // check if the path to the customdhcpcd file is correct - QFileInfo fInfo(pathToDhcpcdExe); + QFileInfo fInfo(_pathToDhcpcdExe); if (!fInfo.exists()) { qDebug() << "could not find customdhcpcd exe. Please check the path to this file."; @@ -52,10 +62,10 @@ void NetworkDiscovery::initAndRun(QString serverIp, bool userChoice,QString serv if (args != NULL && !args->isEmpty()) { qDebug() << "--- \t [NetworkDiscovery::initAndRun] added additional args"; - dhcpcdArguments.append(*args); + _dhcpcdArguments.append(*args); } - numberOfProcesses = 0; + // start the main work: @@ -67,8 +77,7 @@ void NetworkDiscovery::initAndRun(QString serverIp, bool userChoice,QString serv //dhcpcdArguments.append("-d"); - QString ifName("eth0"); - numberOfProcesses = list.size(); + _numberOfProcesses = list.size(); runDHCPCD(list); } else { qDebug() << "list is empty. Have not found usable interface."; @@ -79,24 +88,23 @@ void NetworkDiscovery::initAndRun(QString serverIp, bool userChoice,QString serv } int NetworkDiscovery::replaceDefaultRoute(QString &ifName, QString &gateway, int af, int mss) { - networkManager.replaceDefaultRoute(ifName, gateway, mss, AF_INET); + _networkManager.replaceDefaultRoute(ifName, gateway, mss, AF_INET); } int NetworkDiscovery::ip4_replaceDefaultRoute(QString ifName, QString gateway, int mss) { - networkManager.replaceDefaultRoute(ifName, gateway, mss, AF_INET); + _networkManager.replaceDefaultRoute(ifName, gateway, mss, AF_INET); } int NetworkDiscovery::ip4_setManualConfiguration(QVariantMap result) { QList dns; dns.append(result["dns"].toString()); - networkManager.ip4_setManualConfiguration(result["ifname"].toString(), + _networkManager.ip4_setManualConfiguration(result["ifname"].toString(), result["ipaddr"].toString(), result["netmask"].toString(), result["broadcast"].toString(), result["gateway"].toString(), 0, AF_INET, - true, "/etc/", dns); @@ -108,14 +116,13 @@ int NetworkDiscovery::ip4_setManualConfiguration(QVariantMap result) { if(ifc != NULL) { dns.clear(); dns = ifc->getDnsservers().trimmed().split(" "); - networkManager.ip4_setManualConfiguration(result["ifname"].toString(), + _networkManager.ip4_setManualConfiguration(result["ifname"].toString(), ifc->getIpAddress(), ifc->getNetmask(), ifc->getBroadcast(), ifc->getGateway(), 0, AF_INET, - true, "/etc/", dns); } @@ -145,7 +152,7 @@ QList NetworkDiscovery::getListOfNetworkInterfaces() { continue; } if(!(nI.flags() & QNetworkInterface::IsUp)) { - networkManager.bringInterfaceUpDown(nI.humanReadableName(), true); + _networkManager.bringInterfaceUP(nI.humanReadableName()); } if (!checkCarrierState(nI.humanReadableName())) { continue; @@ -222,18 +229,18 @@ void NetworkDiscovery::runDHCPCD(QList &interfaces) { void NetworkDiscovery::runDHCPCD(QString interface) { emit updateStatusLabel(interface, "start DHCP"); - dhcpcdArguments.append(interface); + _dhcpcdArguments.append(interface); QProcess * p = new QProcess(this); - qDebug() << dhcpcdArguments; + qDebug() << _dhcpcdArguments; - clientProcessToIfNameMap.insert(p, interface); - qDebug() << clientProcessToIfNameMap; - p->start(pathToDhcpcdExe, dhcpcdArguments); + _clientProcessToIfNameMap.insert(p, interface); + qDebug() << _clientProcessToIfNameMap; + 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(); + _dhcpcdArguments.removeLast(); } bool NetworkDiscovery::checkConnectivity(QString ifName) { @@ -247,7 +254,7 @@ bool NetworkDiscovery::checkConnectivity(QString ifName) { _ifcMap.insert(ifName, ifConf); // replace default route - qDebug() << networkManager.replaceDefaultRoute(ifName, + qDebug() << _networkManager.replaceDefaultRoute(ifName, ifConf->getGateway(), 0, AF_INET); if (checkConnectivityViaTcp(_serverIp)) { @@ -289,11 +296,11 @@ void NetworkDiscovery::handleNewConnection() { qDebug() << "New Connection arrived"; /*QLocalSocket **/ - client = server ->nextPendingConnection(); - clients.insert(client, client); - connect(client, SIGNAL(disconnected()), this, + _client = _server ->nextPendingConnection(); + _clients.insert(_client, _client); + connect(_client, SIGNAL(disconnected()), this, SLOT(handleClientDisconnect())); - connect(client, SIGNAL(readyRead()), this, SLOT(handleNewInput())); + connect(_client, SIGNAL(readyRead()), this, SLOT(handleNewInput())); } /** @@ -302,7 +309,7 @@ void NetworkDiscovery::handleNewConnection() { void NetworkDiscovery::handleClientDisconnect() { QLocalSocket* socket = qobject_cast (QObject::sender()); - QLocalSocket * client = clients.value(socket); + QLocalSocket * client = _clients.value(socket); qDebug() << "disconnect client"; handleNewInput(client); @@ -336,7 +343,7 @@ void NetworkDiscovery::handleNewInput(QLocalSocket * client) { void NetworkDiscovery::handleNewInput() { QLocalSocket* socket = qobject_cast (QObject::sender()); - QLocalSocket * client = clients.value(socket); + QLocalSocket * client = _clients.value(socket); QString data(client->read(DHCP_MESSAGE_SIZE)); client->write("ACK", ACK_SIZE); client->waitForBytesWritten(); @@ -380,9 +387,9 @@ void NetworkDiscovery::handleNewInputLine(QLocalSocket * client, QString data) { int sst = s_subState.trimmed().toInt(); //qDebug() << logMsg; - if (ifNameToClient.size() < numberOfProcesses && !ifNameToClient.contains( + if (_ifNameToClient.size() < _numberOfProcesses && !_ifNameToClient.contains( interface)) { - ifNameToClient.insert(interface, client); + _ifNameToClient.insert(interface, client); } switch (st) { @@ -505,7 +512,7 @@ void NetworkDiscovery::handleProcessFinished(int exitCode, QProcess::ExitStatus exitStatus) { QProcess* p = qobject_cast (QObject::sender()); - QString ifName = clientProcessToIfNameMap.value(p, "ifName"); + QString ifName = _clientProcessToIfNameMap.value(p, "ifName"); if (!_blocked) { //_blocked becomes true, if _userChoice is false and we found a usable interface if (ifName.compare("ifName") == 0) { qDebug() @@ -524,12 +531,12 @@ void NetworkDiscovery::handleProcessFinished(int exitCode, } } if (!_blocked) { //_blocked becomes true, if _userChoice is false and we found a usable interface - QLocalSocket *client = ifNameToClient.value(ifName, 0); + QLocalSocket *client = _ifNameToClient.value(ifName, 0); if (client != 0) { handleNewInput(client); } - numberOfProcesses = numberOfProcesses - 1; - if (numberOfProcesses <= 0 && _userChoice) { + _numberOfProcesses = _numberOfProcesses - 1; + if (_numberOfProcesses <= 0 && _userChoice) { emit allProcessesFinished(); } } @@ -546,7 +553,7 @@ void NetworkDiscovery::handleProcessFinished(int exitCode, */ void NetworkDiscovery::handleProcessStarted() { QProcess* p = qobject_cast (QObject::sender()); - QString ifName = clientProcessToIfNameMap.value(p, "ifName"); + QString ifName = _clientProcessToIfNameMap.value(p, "ifName"); qDebug() << "process started for interface:" << ifName; } diff --git a/LogReceiver/networkdiscovery.h b/LogReceiver/networkdiscovery.h index cf09d58..913a5b4 100644 --- a/LogReceiver/networkdiscovery.h +++ b/LogReceiver/networkdiscovery.h @@ -72,18 +72,15 @@ signals: void setManualConfInterfaces(QString jsonArr); private: - QLocalServer *server; - quint16 blockSize; - //QMap interfacesMap; - //QMap indexToIfaceNameMap; - QMap clients; - QLocalSocket * client; - QMap ifNameToClient; - QMap clientProcessToIfNameMap; - QString pathToDhcpcdExe; - QStringList dhcpcdArguments; - int numberOfProcesses; - NetworkManager networkManager; + QLocalServer *_server; + QMap _clients; + QLocalSocket * _client; + QMap _ifNameToClient; + QMap _clientProcessToIfNameMap; + QString _pathToDhcpcdExe; + QStringList _dhcpcdArguments; + int _numberOfProcesses; + NetworkManager _networkManager; bool _userChoice; bool _blocked; QString _serverIp; diff --git a/LogReceiver/networkmanager.cpp b/LogReceiver/networkmanager.cpp index 80aa8a1..55fd475 100644 --- a/LogReceiver/networkmanager.cpp +++ b/LogReceiver/networkmanager.cpp @@ -87,6 +87,34 @@ int NetworkManager::replaceDefaultRoute(QString ifname, QString gateway, return retval; } + +/** + * The method brings an interface up. + * + * @param ifname + * the name of the interface + * + * @return + * 0 -> success + * -1 -> error + */ +int NetworkManager::bringInterfaceUP(QString ifname) { + return bringInterfaceUpDown(ifname, true); +} + +/** + * The method brings an interface down. + * + * @param ifname + * the name of the interface + * + * @return + * 0 -> success + * -1 -> error + */ +int NetworkManager::bringInterfaceDown(QString ifname) { + return bringInterfaceUpDown(ifname, false); +} /** * This method brings an interface up or down. * @@ -145,11 +173,42 @@ int NetworkManager::bringInterfaceUpDown(QString ifname, bool up) { return retval; } +/** + * This method is used when the manual configuration is needed. + * + * This method is used when the manual configuration is needed. + * First we bring up the interface. Than we configure the interface with + * our manual entered configuration dates. + * After that we replace the old default route with the new and + * write a resolv.conf. + * + * @param ifname + * name of the interface which we are about to configure. + * + * @param ipAddress + * the new IP-Address. + * + * @param netmask + * the netmask of the IP-Address. + * + * @param broadcast + * the broadcast address. + * @param gateway + * the gateway address. + * @param metric + * do not exactly know why we need this. in most cases this should be 0. + * @param af + * the address type. Either AF_INET for IPv4 or AF_INET6 for IPv6. + * @param pathToResolvConf + * the path to the resolf.conf file. in most cases "/etc/". + * @param nameServer + * the name server addresses. + */ int NetworkManager::ip4_setManualConfiguration(QString ifname, QString ipAddress, QString netmask, - QString broadcast, QString gateway, int metric, int af, bool up, QString pathToResolvConf, QList nameServer) { + QString broadcast, QString gateway, int metric, int af, QString pathToResolvConf, QList nameServer) { //bring the interface up - bringInterfaceUpDown(ifname, up); + bringInterfaceUP(ifname); //set configuration ip4_configureInterface(ifname, ipAddress, broadcast, netmask,af); //set default route @@ -223,6 +282,17 @@ int NetworkManager::ip4_configureInterface(QString ifname, QString ipAddress, return retval; } +/** + * This Method returns the length of the IP-Address netmask prefix. + * + * @param ipAddr + * the IP-address + * + * @param netmask + * the netmask of the IP-address. + * @return + * the length of the IP-Address netmask prefix + */ int NetworkManager::ip4_netmaskToPrefix(QString ipAddr, QString netmask) { int retval = -1; QNetworkAddressEntry nae; diff --git a/LogReceiver/networkmanager.h b/LogReceiver/networkmanager.h index 1191ced..0c47e30 100644 --- a/LogReceiver/networkmanager.h +++ b/LogReceiver/networkmanager.h @@ -33,15 +33,18 @@ public: int action); int replaceDefaultRoute(QString ifname, QString gateway, int metric, int af); - int bringInterfaceUpDown(QString ifname, bool up); + + int bringInterfaceUP(QString ifname); + int bringInterfaceDown(QString ifname); int ip4_setManualConfiguration(QString ifname, QString ipAddress, QString netmask, - QString broadcast, QString gateway, int metric, int af, bool up, QString pathToResolvConf, QList nameServer); + QString broadcast, QString gateway, int metric, int af, QString pathToResolvConf, QList nameServer); int ip4_configureInterface(QString ifname, QString ipAddress, QString broadcast, QString netmask, int af); int writeResolvConf(QString path, QString ifname, QList nameServer); private: + int bringInterfaceUpDown(QString ifname, bool up); int ip4_netmaskToPrefix(QString ipAddr, QString netmask); int sync_address(const char *iface, int ifindex, int family, -- cgit v1.2.3-55-g7522