From d3612f403d0122a6929837764fad849634b26e1b Mon Sep 17 00:00:00 2001 From: Niklas Date: Tue, 20 Sep 2011 16:46:11 +0200 Subject: the checkConnectivity method is working now. we use the rtnl functions of the libnl. --- LogReceiver/logreceiver.cpp | 111 ++------------------------------- LogReceiver/logreceiver.h | 6 +- LogReceiver/ndgui.cpp | 4 +- LogReceiver/ndgui.h | 3 + LogReceiver/routemanager.cpp | 142 ++++++++++++++++++++----------------------- LogReceiver/routemanager.h | 18 +++--- 6 files changed, 86 insertions(+), 198 deletions(-) diff --git a/LogReceiver/logreceiver.cpp b/LogReceiver/logreceiver.cpp index 8dc2938..b034013 100644 --- a/LogReceiver/logreceiver.cpp +++ b/LogReceiver/logreceiver.cpp @@ -179,45 +179,7 @@ void LogReceiver::runDHCPCD(QString interface) { dhcpcdArguments.removeLast(); } -void LogReceiver::checkInternetConnection(QString ifName) { - QString command("ping"); - QStringList argList; - QString timeout("1"); - QString total("2"); - int exitCode = -1; - QString destination("www.google.de"); - argList << "-I" << "ifName" << "-W" << timeout << "-c" << total - << destination; - argList.replace(1, ifName); - QProcess * p = new QProcess(this); - p->start(command, argList); - p->waitForFinished(); - exitCode = p->exitCode(); - if (exitCode > 0) { - qDebug() << "no internet connection with interface" << ifName; - //remove interface from list and inform user via debug console - emit updateStatusLabel(ifName, "connection not possible"); - } else if (exitCode == 0) { - qDebug() << "internet: check passed! for interface" << ifName; - emit - updateStatusLabel(ifName, "connection possible"); - emit connectionEstablished(ifName, ""); - } -} - -void LogReceiver::checkInternetConnection(QList &interfaces) { - foreach(QString nI, interfaces) - { - checkInternetConnection(nI); - } -} - void LogReceiver::checkConnectivity(QString ifName) { - QString command("route"); - QStringList argList; - QString gateway(" "); - QByteArray ba; - struct in_addr destination, netmask, gw; int metric = 0; // get gateway address @@ -226,37 +188,13 @@ void LogReceiver::checkConnectivity(QString ifName) { interfaceconfiguration ifConf; ifConf.readConfigOutOfFile(pathToGatewayFile); - // delete default route - ba = ifName.toAscii(); - char *ifname = ba.data(); - inet_aton("0.0.0.0", &destination); - inet_aton("0.0.0.0", &netmask); - ba = ifConf.getGateway().toAscii(); - const char * gwaddr = ba.data(); - inet_aton(gwaddr,&gw); - //del_route(ifname, destination, netmask, gw, metric); - - QProcess * p = new QProcess(this); - /* - argList << "del" << "default"; - - p->start(command, argList); - p->waitForFinished(); - */ - // add new default route - argList.clear(); - qDebug() << "add default route with:" << ifConf.getGateway() - << ifConf.getInterface(); - argList << "add" << "default" << "gw" << ifConf.getGateway() - << ifConf.getInterface(); - p = new QProcess(this); - p->start(command, argList); - p->waitForFinished(); + // replace default route + qDebug() << rm.replaceDefaultRoute(ifName, ifConf.getGateway(), 0, AF_INET); // check connectivity via tcp connection QTcpSocket *tcpSocket = new QTcpSocket(this); tcpSocket->connectToHost(QString("209.85.148.105"), 80); - if (!tcpSocket->waitForConnected(2000)) { + if (!tcpSocket->waitForConnected(500)) { qDebug() << "no internet connection with interface" << ifName; qDebug() << tcpSocket->errorString(); emit updateStatusLabel(ifName, "connection not possible"); @@ -264,51 +202,10 @@ void LogReceiver::checkConnectivity(QString ifName) { qDebug() << "internet: check passed! for interface" << ifName; emit updateStatusLabel(ifName, "connection possible"); - emit connectionEstablished(ifName, gateway); + emit connectionEstablished(ifName, ifConf.getGateway()); } } -void LogReceiver::checkInternetConnectionViaTCP(QString ifName) { -/* - bool canStartIAP = (configurationManager.capabilities() - & QNetworkConfigurationManager::CanStartAndStopInterfaces); - QList configs = - configurationManager.allConfigurations(); - QNetworkConfiguration cfg; - foreach(QNetworkConfiguration nC, configs) - { - if (nC.name() == ifName) { - qDebug() << "found config" << nC.name(); - cfg = nC; - break; - } - } - if (!cfg.isValid() || (!canStartIAP && cfg.state() - != QNetworkConfiguration::Active)) { - qDebug() << "config is not valid" << cfg.name(); - return; - } - QNetworkSession *session = new QNetworkSession(cfg, this); - session->open(); - if (session->waitForOpened(-1)) { - - qDebug() << "used interface for connectivity check:" - << session->interface().humanReadableName(); - QTcpSocket *tcpSocket = new QTcpSocket(this); - tcpSocket->connectToHost(QString("209.85.148.105"), 80); - if (!tcpSocket->waitForConnected(2000)) { - qDebug() << tcpSocket->errorString(); - emit updateStatusLabel(ifName, "connection not possible"); - } else { - emit updateStatusLabel(ifName, "connection possible"); - emit connectionEstablished(ifName, ""); - } - } else { - qDebug() << "couldn't open session"; - } - session->close(); - */ -} /** * diff --git a/LogReceiver/logreceiver.h b/LogReceiver/logreceiver.h index f22e619..f5bdae1 100644 --- a/LogReceiver/logreceiver.h +++ b/LogReceiver/logreceiver.h @@ -14,6 +14,7 @@ #include #include "interfaceconfiguration.h" +#include "routemanager.h" //#include //#include #include "status.h" @@ -76,7 +77,7 @@ private: QNetworkConfigurationManager configurationManager; QNetworkAccessManager *accessManager; int numberOfProcesses; - //const char ack[ACK_SIZE]; + routemanager rm; void handleNewInput(QLocalSocket * client); @@ -84,9 +85,6 @@ private: void runDHCPCD(QString interface); QListcheckCarrierState(QList &interfaces); bool checkCarrierState(QString interface); - void checkInternetConnection(QString ifName); - void checkInternetConnection(QList &interfaces); - void checkInternetConnectionViaTCP(QString ifName); void checkConnectivity(QString ifName); QList getListOfNetworkInterfaces(); bool checkBlackList(QString i); diff --git a/LogReceiver/ndgui.cpp b/LogReceiver/ndgui.cpp index 3fe9c65..7d638c0 100644 --- a/LogReceiver/ndgui.cpp +++ b/LogReceiver/ndgui.cpp @@ -187,5 +187,7 @@ void ndgui::showLog() /*test html gui version*/ /* slots */ void ndgui::addInterface() { - + QWebView *view; + view->page()->mainFrame(); + QWebElement element; } diff --git a/LogReceiver/ndgui.h b/LogReceiver/ndgui.h index 90af3a3..b320770 100644 --- a/LogReceiver/ndgui.h +++ b/LogReceiver/ndgui.h @@ -2,6 +2,9 @@ #define NDGUI_H #include +#include +#include +#include #include #include #include diff --git a/LogReceiver/routemanager.cpp b/LogReceiver/routemanager.cpp index 3063b21..98ce2ad 100644 --- a/LogReceiver/routemanager.cpp +++ b/LogReceiver/routemanager.cpp @@ -16,115 +16,105 @@ routemanager::~routemanager() { // TODO Auto-generated destructor stub } -int routemanager::addRoute(QString ifname, QString destination, - QString netmask, QString gateway, int metric) { - //struct in_addr destination, netmask, gateway; - //add_route(); - return doRoute(destination, gateway, AF_INET, 0); -} - -int routemanager::addRoute6(QString ifname, QString destination, - QString netmask, QString gateway, int metric) { - //struct in_addr destination, netmask, gateway; - //add_route(); - return doRoute(destination, gateway, AF_INET6, 0); -} - - -int routemanager::delRoute(QString ifname, QString destination, - QString netmask, QString gateway, int metric) { - /*struct in_addr ds, nm, gw; - ba = ifname.toAscii(); - const char *in = ba.constData(); - inet_aton("0.0.0.0", &ds); - inet_aton("0.0.0.0", &nm); - ba = gateway.toAscii(); - char * gwaddr = ba.data(); - inet_aton(gwaddr,&gw); - del_route(in, ds, nm, gw, metric); - */ - return doRoute(destination, gateway, AF_INET, 1); -} - -int routemanager::delRoute6(QString ifname, QString destination, - QString netmask, QString gateway, int metric) { - return doRoute(destination, gateway, AF_INET6, 1); -} - /** - * This method adds or deletes a route. - * This method adds or deletes a route. According to the action, - * you can delete a route or add a route from/to the - * main routing table. In most cases, this method will be used for deleting - * or adding a default rout. To keep it modular, it is possible + * This method adds /replaces the default route. + * This method adds /replaces the default route. + * To keep it modular, it is possible * to specify an ip address family. * - * @param destination - * the destination address (e.g: 0.0.0.0) + * @param ifName + * the interface name * * @param gateway * the gateway address (e.g: 192.168.0.254) - * + * @param mss + * the mss. * @param af * specify the family type of the ip address. * possible values are: AF_INET for an IPv4 address * AF_INET6 for an IPv6 address * - * @param action - * possible values are: 0: perform add route - * 1: perform delete route - * + * @return - * return 1 if an error happened. + * return -1 if an error happened. * return 0 if everything was ok. */ -int routemanager::doRoute(QString destination, QString gateway, int af, int action) { +int routemanager::replaceDefaultRoute(QString ifname, QString gateway, + int mss, int af) { + struct nl_cache *cache; struct nl_handle* rtsock; - struct nl_addr * dst; struct nl_addr * gw; struct rtnl_route * route; - int retval; + int retval, iface_idx; - ba = destination.toAscii(); - char *dstaddr = ba.data(); + QByteArray ba_ifn = ifname.toAscii(); + char * ifn = ba_ifn.data(); - ba = gateway.toAscii(); - char * gwaddr = ba.data(); + QByteArray ba_gw = gateway.toAscii(); + char * gwaddr = ba_gw.data(); + + qDebug() << "---doRoute() gwaddr" << gwaddr; - if (!(dst = nl_addr_parse(dstaddr, af))) { - qDebug() << "Invalid network address given:" << dstaddr; - return -1; - } if (!(gw = nl_addr_parse(gwaddr, af))) { - qDebug() << "Invalid router address given: " << gwaddr; - nl_addr_put(dst); + printf("Invalid router address given: %s\n", gwaddr); return -1; } - route = rtnl_route_alloc(); - rtnl_route_set_family(route, af); - rtnl_route_set_scope(route, RT_SCOPE_UNIVERSE); - rtnl_route_set_dst(route, dst); - rtnl_route_set_gateway(route, gw); - rtsock = nl_handle_alloc(); nl_connect(rtsock, NETLINK_ROUTE); - if (action == 0) { - rtnl_route_add(rtsock, route, 0); - } - else if (action == 1) { - rtnl_route_del(rtsock, route, 0); + if ((cache = rtnl_link_alloc_cache(rtsock)) == NULL) { + printf("error with link cache alloc \n"); } - else { - qDebug() << "unknown action:" << action; + + iface_idx = rtnl_link_name2i(cache, ifn); + + route = rtnl_route_alloc(); + rtnl_route_set_scope(route, RT_SCOPE_UNIVERSE); + rtnl_route_set_gateway(route, gw); + rtnl_route_set_oif(route, iface_idx); + + if (mss > 0) { + rtnl_route_set_metric(route, RTAX_ADVMSS, mss); } + retval = rtnl_route_add(rtsock, route, NLM_F_REPLACE); + qDebug() << "return value:" << retval << ":" << strerror(-retval); rtnl_route_put(route); - nl_addr_put(dst); nl_addr_put(gw); nl_handle_destroy(rtsock); - return 0; + return retval; } + +/** + * This method adds or deletes a route. + * This method adds or deletes a route. According to the action, + * you can delete a route or add a route from/to the + * main routing table. In most cases, this method will be used for deleting + * or adding a default rout. To keep it modular, it is possible + * to specify an ip address family. + * + * @param ifName + * the interface name. + * @param destination + * the destination address (e.g: 0.0.0.0) + * + * @param gateway + * the gateway address (e.g: 192.168.0.254) + * + * @param af + * specify the family type of the ip address. + * possible values are: AF_INET for an IPv4 address + * AF_INET6 for an IPv6 address + * + * @param action + * possible values are: 0: perform add route + * 1: perform delete route + * + * @return + * return 1 if an error happened. + * return 0 if everything was ok. + */ +int routemanager::doRoute(QString ifName, QString destination, QString gateway, int af, int action) {return 0;} diff --git a/LogReceiver/routemanager.h b/LogReceiver/routemanager.h index 0566801..ee48122 100644 --- a/LogReceiver/routemanager.h +++ b/LogReceiver/routemanager.h @@ -11,31 +11,29 @@ #include #include #include +#include #include #include +#include #include #include -class routemanager : public QObject{ +class routemanager: public QObject { Q_OBJECT public: routemanager(); virtual ~routemanager(); - int addRoute(QString ifname, QString destination, QString netmask, QString gateway, int metric); - int delRoute(QString ifname, QString destination, QString netmask, QString gateway, int metric); - int addRoute6(QString ifname, QString destination, - QString netmask, QString gateway, int metric); - int delRoute6(QString ifname, QString destination, - QString netmask, QString gateway, int metric); - int doRoute(QString destination, QString gateway, int af, int action); + int doRoute(QString ifName, QString destination, QString gateway, int af, + int action); + int replaceDefaultRoute(QString ifname, QString gateway, int metric, + int af); private: - QByteArray ba; -}; +}; #endif /* ROUTEMANAGER_H_ */ -- cgit v1.2.3-55-g7522