Networksettings API API Documentation

interface.cpp

00001 
00006 #include "interface.h"
00007 #include <qdatetime.h>
00008 #include <qfile.h>
00009 #include <qdir.h>
00010 #include <qfileinfo.h>
00011 #include <qtextstream.h>
00012 
00013 #define IFCONFIG "/sbin/ifconfig"
00014 #define DHCP_INFO_DIR "/etc/dhcpc"
00015 
00016 #include <stdio.h>
00017 #include <stdlib.h>
00018 
00019 Interface::Interface(QObject * parent, const char * name, bool newSatus): QObject(parent, name), hardwareName("Unknown"), moduleOwner(NULL), status(newSatus), attached(false), dhcp(false), macAddress(""), ip("0.0.0.0"), broadcast(""), subnetMask("0.0.0.0"){
00020   refresh();
00021 }
00022 
00028 void Interface::setStatus(bool newStatus){
00029   if(status != newStatus){
00030     status = newStatus;
00031     refresh();
00032   }
00033 };
00034 
00040 void Interface::setAttached(bool isAttached){
00041   attached = isAttached;
00042   emit(updateInterface(this));
00043 };
00044 
00050 void Interface::setHardwareName(const QString &name){
00051   hardwareName = name;
00052   emit(updateInterface(this));
00053 };
00054 
00060 void Interface::setModuleOwner(Module *owner){
00061   moduleOwner = owner;
00062   emit(updateInterface(this));
00063 };
00064 
00065 
00069 void Interface::start(){
00070   // check to see if we are already running.
00071   if(true == status){
00072     emit (updateMessage("Unable to start interface,\n already started"));
00073     return;
00074   }
00075 
00076   int ret = system(QString("%1 %2 up").arg(IFCONFIG).arg(this->name()).latin1());
00077   // See if it was successful...
00078   if(ret != 0){
00079     emit (updateMessage("Starting interface failed"));
00080     return;
00081   }
00082 
00083   status = true;
00084   refresh();
00085   emit (updateMessage("Start successful"));
00086 }
00087 
00091 void Interface::stop(){
00092   // check to see if we are already stopped.
00093   if(false == status){
00094     emit (updateMessage("Unable to stop interface,\n already stopped"));
00095     return;
00096   }
00097 
00098   int ret = system(QString("%1 %2 down").arg(IFCONFIG).arg(this->name()).latin1());
00099   if(ret != 0){
00100     emit (updateMessage("Stopping interface failed"));
00101     return;
00102   }
00103 
00104   status = false;
00105   refresh();
00106   emit (updateMessage("Stop successful"));
00107 }
00108 
00112 void Interface::restart(){
00113   stop();
00114   start();
00115 }
00116 
00122 bool Interface::refresh(){
00123   // See if we are up.
00124   if(status == false){
00125     macAddress = "";
00126     ip = "0.0.0.0";
00127     subnetMask = "0.0.0.0";
00128     broadcast = "";
00129     dhcp = false;
00130     dhcpServerIp = "";
00131     leaseObtained = "";
00132     leaseExpires = "";
00133     emit(updateInterface(this));
00134     return true;
00135   }
00136 
00137   QString fileName = QString("/tmp/%1_ifconfig_info").arg(this->name());
00138   int ret = system(QString("LANG=C %1 %2 > %3").arg(IFCONFIG).arg(this->name()).arg(fileName).latin1());
00139   if(ret != 0){
00140     qDebug(QString("Interface: Ifconfig return value: %1, is not 0").arg(ret).latin1());
00141     return false;
00142   }
00143 
00144   QFile file(fileName);
00145   if (!file.open(IO_ReadOnly)){
00146     qDebug(QString("Interface: Can't open file: %1").arg(fileName).latin1());
00147     return false;
00148   }
00149 
00150   // Set to the defaults
00151   macAddress = "";
00152   ip = "0.0.0.0";
00153   subnetMask = "0.0.0.0";
00154   broadcast = "";
00155 
00156   QTextStream stream( &file );
00157   QString line;
00158   while ( !stream.eof() ) {
00159     line = stream.readLine();
00160     if(line.contains("HWaddr")){
00161       int mac = line.find("HWaddr");
00162       macAddress = line.mid(mac+7, line.length());
00163     }
00164     if(line.contains("inet addr")){
00165       int ipl = line.find("inet addr");
00166       int space = line.find(" ", ipl+10);
00167       ip = line.mid(ipl+10, space-ipl-10);
00168     }
00169     if(line.contains("Mask")){
00170       int mask = line.find("Mask");
00171       subnetMask = line.mid(mask+5, line.length());
00172     }
00173     if(line.contains("Bcast")){
00174       int mask = line.find("Bcast");
00175       int space = line.find(" ", mask+6);
00176       broadcast = line.mid(mask+6, space-mask-6);
00177     }
00178   }
00179   file.close();
00180   QFile::remove(fileName);
00181 
00182   // DHCP TESTING
00183   // reset DHCP info
00184   dhcpServerIp = "";
00185   leaseObtained = "";
00186   leaseExpires = "";
00187   dhcp = false;
00188 
00189   QString dhcpDirectory(DHCP_INFO_DIR);
00190   QDir d(dhcpDirectory);
00191   if(!d.exists(dhcpDirectory))
00192     dhcpDirectory = "/var/run";
00193 
00194   // See if we have
00195   QString dhcpFile(QString(dhcpDirectory+"/dhcpcd-%1.info").arg(this->name()));
00196   // If there is no DHCP information then exit now with no errors.
00197   if(!QFile::exists(dhcpFile)){
00198     emit(updateInterface(this));
00199     return true;
00200   }
00201 
00202   file.setName(dhcpFile);
00203   if (!file.open(IO_ReadOnly)){
00204     qDebug(QString("Interface: Can't open file: %1").arg(dhcpFile).latin1());
00205     return false;
00206   }
00207 
00208   // leaseTime and renewalTime and used if pid and deamon exe can be accessed.
00209   int leaseTime = 0;
00210   int renewalTime = 0;
00211 
00212   stream.setDevice( &file );
00213   while ( !stream.eof() ) {
00214     line = stream.readLine();
00215     if(line.contains("DHCPSIADDR="))
00216       dhcpServerIp = line.mid(11, line.length());
00217     if(line.contains("LEASETIME="))
00218       leaseTime = line.mid(10, line.length()).toInt();
00219     if(line.contains("RENEWALTIME="))
00220       renewalTime = line.mid(12, line.length()).toInt();
00221   }
00222   file.close();
00223   //qDebug(QString("Interface: leaseTime: %1").arg(leaseTime).latin1());
00224   //qDebug(QString("Interface: renewalTime: %1").arg(renewalTime).latin1());
00225 
00226   // Get the pid of the deamond
00227   dhcpFile = (QString(dhcpDirectory+"/dhcpcd-%1.pid").arg(this->name()));
00228   file.setName(dhcpFile);
00229   if (!file.open(IO_ReadOnly)){
00230     qDebug(QString("Interface: Can't open file: %1").arg(dhcpFile).latin1());
00231     return false;
00232   }
00233 
00234   int pid = -1;
00235   stream.setDevice( &file );
00236   while ( !stream.eof() ) {
00237     line = stream.readLine();
00238     pid = line.toInt();
00239   }
00240   file.close();
00241 
00242   if( pid == -1){
00243     qDebug("Interface: Could not get pid of dhcpc deamon.");
00244     return false;
00245   }
00246 
00247   // Get the start running time of the deamon
00248   fileName = (QString("/proc/%1/stat").arg(pid));
00249   file.setName(fileName);
00250   stream.setDevice( &file );
00251   if (!file.open(IO_ReadOnly)){
00252     qDebug(QString("Interface: Can't open file: %1").arg(fileName).latin1());
00253     return false;
00254   }
00255   while ( !stream.eof() ) {
00256     line = stream.readLine();
00257   }
00258   file.close();
00259   long time = 0;
00260   // Grab the start time
00261   //                     pid com state ppid pgrp session tty_nr tpgid flags
00262   sscanf(line.latin1(), "%*d %*s %*c   %*d  %*d  %*d     %*d    %*d   %*u "
00263   //   minflt cminflt majflt cmajflt utime stime cutime cstime priority
00264       "%*u    %*u    %*u    %*u     %*u   %*u   %*d   %*d   %*d "
00265   //   nice 0   itrealvalue starttime
00266       "%*d  %*d %*d %lu", (long*) &time);
00267   time = time/100;
00268 
00269   QDateTime datetime(QDateTime::currentDateTime());
00270 
00271   // Get the uptime of the computer.
00272   QFile f("/proc/uptime");
00273   if ( f.open(IO_ReadOnly) ) {    // file opened successfully
00274     QTextStream t( &f );        // use a text stream
00275     int sec = 0;
00276     t >> sec;
00277     datetime = datetime.addSecs((-1*sec));
00278     f.close();
00279   }
00280   else{
00281     qDebug("Interface: Can't open /proc/uptime to retrive uptime.");
00282     return false;
00283   }
00284 
00285   datetime = datetime.addSecs(time);
00286   //qDebug(QString("Interface: %1 %2").arg(datetime.toString()).arg(pid).latin1());
00287 
00288   // Calculate the start and renew times
00289   leaseObtained = datetime.toString();
00290 
00291   // Calculate the start and renew times
00292   datetime = datetime.addSecs(leaseTime);
00293   leaseExpires = datetime.toString();
00294 
00295   dhcp = true;
00296 
00297   emit(updateInterface(this));
00298   return true;
00299 }
00300 
00301 // interface.cpp
00302 
KDE Logo
This file is part of the documentation for OPIE Version 1.0.
Documentation copyright © 1997-2003 the KDE developers. 2003 OPIE developers
Generated on Tue Feb 10 20:26:44 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001