00001
00006 #include "interface.h"
00007
00008 #include <opie2/odebug.h>
00009 #include <opie2/oprocess.h>
00010 #include <opie2/owait.h>
00011
00012 #include <qpe/global.h>
00013
00014 #include <qapplication.h>
00015 #include <qdatetime.h>
00016 #include <qfile.h>
00017 #include <qdir.h>
00018 #include <qfileinfo.h>
00019 #include <qtextstream.h>
00020
00021 #define IFCONFIG "/sbin/ifconfig"
00022 #define IF_UP "/sbin/ifup"
00023 #define IF_DOWN "/sbin/ifdown"
00024 #define DHCP_INFO_DIR "/etc/dhcpc"
00025
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028
00029 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"){
00030 refresh();
00031 }
00032
00038 void Interface::setStatus(bool newStatus){
00039 if(status != newStatus){
00040 status = newStatus;
00041 refresh();
00042 }
00043 };
00044
00050 void Interface::setAttached(bool isAttached){
00051 attached = isAttached;
00052 emit(updateInterface(this));
00053 };
00054
00060 void Interface::setHardwareName(const QString &name){
00061 hardwareName = name;
00062 emit(updateInterface(this));
00063 };
00064
00070 void Interface::setModuleOwner(Module *owner){
00071 moduleOwner = owner;
00072 emit(updateInterface(this));
00073 };
00074
00075
00076 bool Interface::callProcess( const QStringList& names ) {
00077 Opie::Ui::OWait *owait = new Opie::Ui::OWait();
00078 Global::statusMessage( tr( "Restarting interface" ) );
00079
00080 owait->show();
00081 qApp->processEvents();
00082
00083 Opie::Core::OProcess restart;
00084 restart << names;
00085 if ( !restart.start(Opie::Core::OProcess::Block,
00086 Opie::Core::OProcess::NoCommunication ) ) {
00087 owarn << "unable to spawn command" << names << oendl;
00088 return false;
00089 }
00090 owait->hide();
00091 delete owait;
00092
00093 if ( restart.normalExit() || restart.exitStatus() != 0 )
00094 return false;
00095
00096 return true;
00097 }
00098
00102 void Interface::start(){
00103
00104 if(true == status){
00105 emit (updateMessage("Unable to start interface,\n already started"));
00106 return;
00107 }
00108
00109
00110 QStringList lst;
00111 lst << IF_UP;
00112 lst << name();
00113 if ( !callProcess(lst) ) {
00114 emit (updateMessage("Starting interface failed"));
00115 return;
00116 }
00117
00118 status = true;
00119 refresh();
00120 emit (updateMessage("Start successful"));
00121 }
00122
00126 void Interface::stop(){
00127
00128 if(false == status){
00129 emit (updateMessage("Unable to stop interface,\n already stopped"));
00130 return;
00131 }
00132
00133 QStringList lst;
00134 lst << IF_DOWN;
00135 lst << name();
00136
00137
00138 if( !callProcess( lst ) ){
00139 emit (updateMessage("Stopping interface failed"));
00140 return;
00141 }
00142
00143 status = false;
00144 refresh();
00145 emit (updateMessage("Stop successful"));
00146 }
00147
00151 void Interface::restart(){
00152 stop();
00153 start();
00154 }
00155
00161 bool Interface::refresh(){
00162
00163 if(status == false){
00164 macAddress = "";
00165 ip = "0.0.0.0";
00166 subnetMask = "0.0.0.0";
00167 broadcast = "";
00168 dhcp = false;
00169 dhcpServerIp = "";
00170 leaseObtained = "";
00171 leaseExpires = "";
00172 emit(updateInterface(this));
00173 return true;
00174 }
00175
00176 QString fileName = QString("/tmp/%1_ifconfig_info").arg(this->name());
00177 int ret = system(QString("LANG=C %1 %2 > %3").arg(IFCONFIG).arg(this->name()).arg(fileName).latin1());
00178 if(ret != 0){
00179 odebug << QString("Interface: Ifconfig return value: %1, is not 0").arg(ret).latin1() << oendl;
00180 return false;
00181 }
00182
00183 QFile file(fileName);
00184 if (!file.open(IO_ReadOnly)){
00185 odebug << QString("Interface: Can't open file: %1").arg(fileName).latin1() << oendl;
00186 return false;
00187 }
00188
00189
00190 macAddress = "";
00191 ip = "0.0.0.0";
00192 subnetMask = "0.0.0.0";
00193 broadcast = "";
00194
00195 QTextStream stream( &file );
00196 QString line;
00197 while ( !stream.eof() ) {
00198 line = stream.readLine();
00199 if(line.contains("HWaddr")){
00200 int mac = line.find("HWaddr");
00201 macAddress = line.mid(mac+7, line.length());
00202 }
00203 if(line.contains("inet addr")){
00204 int ipl = line.find("inet addr");
00205 int space = line.find(" ", ipl+10);
00206 ip = line.mid(ipl+10, space-ipl-10);
00207 }
00208 if(line.contains("Mask")){
00209 int mask = line.find("Mask");
00210 subnetMask = line.mid(mask+5, line.length());
00211 }
00212 if(line.contains("Bcast")){
00213 int mask = line.find("Bcast");
00214 int space = line.find(" ", mask+6);
00215 broadcast = line.mid(mask+6, space-mask-6);
00216 }
00217 }
00218 file.close();
00219 QFile::remove(fileName);
00220
00221
00222
00223 dhcpServerIp = "";
00224 leaseObtained = "";
00225 leaseExpires = "";
00226 dhcp = false;
00227
00228 QString dhcpDirectory(DHCP_INFO_DIR);
00229 QDir d(dhcpDirectory);
00230 if(!d.exists(dhcpDirectory))
00231 dhcpDirectory = "/var/run";
00232
00233
00234 QString dhcpFile(QString(dhcpDirectory+"/dhcpcd-%1.info").arg(this->name()));
00235
00236 if(!QFile::exists(dhcpFile)){
00237 emit(updateInterface(this));
00238 return true;
00239 }
00240
00241 file.setName(dhcpFile);
00242 if (!file.open(IO_ReadOnly)){
00243 odebug << QString("Interface: Can't open file: %1").arg(dhcpFile).latin1() << oendl;
00244 return false;
00245 }
00246
00247
00248 int leaseTime = 0;
00249 int renewalTime = 0;
00250
00251 stream.setDevice( &file );
00252 while ( !stream.eof() ) {
00253 line = stream.readLine();
00254 if(line.contains("DHCPSIADDR="))
00255 dhcpServerIp = line.mid(11, line.length());
00256 if(line.contains("LEASETIME="))
00257 leaseTime = line.mid(10, line.length()).toInt();
00258 if(line.contains("RENEWALTIME="))
00259 renewalTime = line.mid(12, line.length()).toInt();
00260 }
00261 file.close();
00262
00263
00264
00265
00266 dhcpFile = (QString(dhcpDirectory+"/dhcpcd-%1.pid").arg(this->name()));
00267 file.setName(dhcpFile);
00268 if (!file.open(IO_ReadOnly)){
00269 odebug << QString("Interface: Can't open file: %1").arg(dhcpFile).latin1() << oendl;
00270 return false;
00271 }
00272
00273 int pid = -1;
00274 stream.setDevice( &file );
00275 while ( !stream.eof() ) {
00276 line = stream.readLine();
00277 pid = line.toInt();
00278 }
00279 file.close();
00280
00281 if( pid == -1){
00282 odebug << "Interface: Could not get pid of dhcpc deamon." << oendl;
00283 return false;
00284 }
00285
00286
00287 fileName = (QString("/proc/%1/stat").arg(pid));
00288 file.setName(fileName);
00289 stream.setDevice( &file );
00290 if (!file.open(IO_ReadOnly)){
00291 odebug << QString("Interface: Can't open file: %1").arg(fileName).latin1() << oendl;
00292 return false;
00293 }
00294 while ( !stream.eof() ) {
00295 line = stream.readLine();
00296 }
00297 file.close();
00298 long time = 0;
00299
00300
00301 sscanf(line.latin1(), "%*d %*s %*c %*d %*d %*d %*d %*d %*u "
00302
00303 "%*u %*u %*u %*u %*u %*u %*d %*d %*d "
00304
00305 "%*d %*d %*d %lu", (long*) &time);
00306 time = time/100;
00307
00308 QDateTime datetime(QDateTime::currentDateTime());
00309
00310
00311 QFile f("/proc/uptime");
00312 if ( f.open(IO_ReadOnly) ) {
00313 QTextStream t( &f );
00314 int sec = 0;
00315 t >> sec;
00316 datetime = datetime.addSecs((-1*sec));
00317 f.close();
00318 }
00319 else{
00320 odebug << "Interface: Can't open /proc/uptime to retrive uptime." << oendl;
00321 return false;
00322 }
00323
00324 datetime = datetime.addSecs(time);
00325
00326
00327
00328 leaseObtained = datetime.toString();
00329
00330
00331 datetime = datetime.addSecs(leaseTime);
00332 leaseExpires = datetime.toString();
00333
00334 dhcp = true;
00335
00336 emit(updateInterface(this));
00337 return true;
00338 }
00339
00340
00341