summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings/interface.cpp
Unidiff
Diffstat (limited to 'noncore/settings/networksettings/interface.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/settings/networksettings/interface.cpp287
1 files changed, 0 insertions, 287 deletions
diff --git a/noncore/settings/networksettings/interface.cpp b/noncore/settings/networksettings/interface.cpp
deleted file mode 100644
index 929b3a1..0000000
--- a/noncore/settings/networksettings/interface.cpp
+++ b/dev/null
@@ -1,287 +0,0 @@
1#include "interface.h"
2#include <qdatetime.h>
3#include <qfile.h>
4#include <qdir.h>
5#include <qfileinfo.h>
6#include <qtextstream.h>
7
8#define IFCONFIG "/sbin/ifconfig"
9#define DHCP_INFO_DIR "/etc/dhcpc"
10
11#include <stdio.h>
12#include <stdlib.h>
13
14Interface::Interface(QObject * parent, const char * name, bool newSatus): QObject(parent, name), status(newSatus), attached(false), hardwareName("Unknown"), moduleOwner(NULL), macAddress(""), ip("0.0.0.0"), broadcast(""), subnetMask("0.0.0.0"), dhcp(false){
15 refresh();
16}
17
18/**
19 * Set status
20 * @param newStatus - the new status
21 * emit updateInterface
22 */
23void Interface::setStatus(bool newStatus){
24 if(status != newStatus){
25 status = newStatus;
26 refresh();
27 }
28};
29
30/**
31 * Set if attached or not (802.11 card pulled out for example)
32 * @param isAttached - if attached
33 * emit updateInterface
34 */
35void Interface::setAttached(bool isAttached){
36 attached = isAttached;
37 emit(updateInterface(this));
38};
39
40/**
41 * Set Hardware name
42 * @param name - the new name
43 * emit updateInterface
44 */
45void Interface::setHardwareName(QString name){
46 hardwareName = name;
47 emit(updateInterface(this));
48};
49
50/**
51 * Set Module owner
52 * @param owner - the new owner
53 * emit updateInterface
54 */
55void Interface::setModuleOwner(Module *owner){
56 moduleOwner = owner;
57 emit(updateInterface(this));
58};
59
60
61/**
62 * Try to start the interface.
63 */
64void Interface::start(){
65 // check to see if we are already running.
66 if(true == status)
67 return;
68
69 int ret = system(QString("%1 %2 up").arg(IFCONFIG).arg(this->name()).latin1());
70 // See if it was successfull...
71 if(ret != 0)
72 return;
73
74 status = true;
75 refresh();
76}
77
78/**
79 * Try to stop the interface.
80 */
81void Interface::stop(){
82 // check to see if we are already stopped.
83 if(false == status)
84 return;
85
86 int ret = system(QString("%1 %2 down").arg(IFCONFIG).arg(this->name()).latin1());
87 if(ret != 0)
88 return;
89
90 status = true;
91 refresh();
92}
93
94/**
95 * Try to restart the interface.
96 */
97void Interface::restart(){
98 stop();
99 start();
100}
101
102/**
103 * Try to refresh the information about the interface.
104 * First call ifconfig, then check the dhcp-info file
105 * @return bool true if successfull.
106 */
107bool Interface::refresh(){
108 // See if we are up.
109 if(status == false){
110 macAddress = "";
111 ip = "0.0.0.0";
112 subnetMask = "0.0.0.0";
113 broadcast = "";
114 dhcp = false;
115 dhcpServerIp = "";
116 leaseObtained = "";
117 leaseExpires = "";
118 emit(updateInterface(this));
119 return true;
120 }
121
122 QString fileName = QString("/tmp/%1_ifconfig_info").arg(this->name());
123 int ret = system(QString("%1 %2 > %3").arg(IFCONFIG).arg(this->name()).arg(fileName).latin1());
124 if(ret != 0){
125 qDebug(QString("Interface: Ifconfig return value: %1, is not 0").arg(ret).latin1());
126 return false;
127 }
128
129 QFile file(fileName);
130 if (!file.open(IO_ReadOnly)){
131 qDebug(QString("Interface: Can't open file: %1").arg(fileName).latin1());
132 return false;
133 }
134
135 // Set to the defaults
136 macAddress = "";
137 ip = "0.0.0.0";
138 subnetMask = "0.0.0.0";
139 broadcast = "";
140
141 QTextStream stream( &file );
142 QString line;
143 while ( !stream.eof() ) {
144 line = stream.readLine();
145 if(line.contains("HWaddr")){
146 int mac = line.find("HWaddr");
147 macAddress = line.mid(mac+7, line.length());
148 }
149 if(line.contains("inet addr")){
150 int ipl = line.find("inet addr");
151 int space = line.find(" ", ipl+10);
152 ip = line.mid(ipl+10, space-ipl-10);
153 }
154 if(line.contains("Mask")){
155 int mask = line.find("Mask");
156 subnetMask = line.mid(mask+5, line.length());
157 }
158 if(line.contains("Bcast")){
159 int mask = line.find("Bcast");
160 int space = line.find(" ", mask+6);
161 broadcast = line.mid(mask+6, space-mask-6);
162 }
163 }
164 file.close();
165 QFile::remove(fileName);
166
167 // DHCP TESTING
168 // reset DHCP info
169 dhcpServerIp = "";
170 leaseObtained = "";
171 leaseExpires = "";
172 dhcp = false;
173
174 QString dhcpDirectory(DHCP_INFO_DIR);
175 QDir d(dhcpDirectory);
176 if(!d.exists(dhcpDirectory))
177 dhcpDirectory = "/var/run";
178
179 // See if we have
180 QString dhcpFile(QString(dhcpDirectory+"/dhcpcd-%1.info").arg(this->name()));
181 // If there is no DHCP information then exit now with no errors.
182 if(!QFile::exists(dhcpFile)){
183 emit(updateInterface(this));
184 return true;
185 }
186
187 file.setName(dhcpFile);
188 if (!file.open(IO_ReadOnly)){
189 qDebug(QString("Interface: Can't open file: %1").arg(dhcpFile).latin1());
190 return false;
191 }
192
193 // leaseTime and renewalTime and used if pid and deamon exe can be accessed.
194 int leaseTime = 0;
195 int renewalTime = 0;
196
197 stream.setDevice( &file );
198 while ( !stream.eof() ) {
199 line = stream.readLine();
200 if(line.contains("DHCPSIADDR="))
201 dhcpServerIp = line.mid(11, line.length());
202 if(line.contains("LEASETIME="))
203 leaseTime = line.mid(10, line.length()).toInt();
204 if(line.contains("RENEWALTIME="))
205 renewalTime = line.mid(12, line.length()).toInt();
206 }
207 file.close();
208 //qDebug(QString("Interface: leaseTime: %1").arg(leaseTime).latin1());
209 //qDebug(QString("Interface: renewalTime: %1").arg(renewalTime).latin1());
210
211 // Get the pid of the deamond
212 dhcpFile = (QString(dhcpDirectory+"/dhcpcd-%1.pid").arg(this->name()));
213 file.setName(dhcpFile);
214 if (!file.open(IO_ReadOnly)){
215 qDebug(QString("Interface: Can't open file: %1").arg(dhcpFile).latin1());
216 return false;
217 }
218
219 int pid = -1;
220 stream.setDevice( &file );
221 while ( !stream.eof() ) {
222 line = stream.readLine();
223 pid = line.toInt();
224 }
225 file.close();
226
227 if( pid == -1){
228 qDebug("Interface: Could not get pid of dhcpc deamon.");
229 return false;
230 }
231
232 // Get the start running time of the deamon
233 fileName = (QString("/proc/%1/stat").arg(pid));
234 file.setName(fileName);
235 stream.setDevice( &file );
236 if (!file.open(IO_ReadOnly)){
237 qDebug(QString("Interface: Can't open file: %1").arg(fileName).latin1());
238 return false;
239 }
240 while ( !stream.eof() ) {
241 line = stream.readLine();
242 }
243 file.close();
244 long time = 0;
245 // Grab the start time
246 // pid com state ppid pgrp session tty_nr tpgid flags
247 sscanf(line.latin1(), "%*d %*s %*c %*d %*d %*d %*d %*d %*u "
248 // minflt cminflt majflt cmajflt utime stime cutime cstime priority
249 "%*u %*u %*u %*u %*u %*u %*d %*d %*d "
250 // nice 0 itrealvalue starttime
251 "%*d %*d %*d %lu", (long*) &time);
252 time = time/100;
253
254 QDateTime datetime(QDateTime::currentDateTime());
255
256 // Get the uptime of the computer.
257 QFile f("/proc/uptime");
258 if ( f.open(IO_ReadOnly) ) { // file opened successfully
259 QTextStream t( &f ); // use a text stream
260 int sec = 0;
261 t >> sec;
262 datetime = datetime.addSecs((-1*sec));
263 f.close();
264 }
265 else{
266 qDebug("Interface: Can't open /proc/uptime to retrive uptime.");
267 return false;
268 }
269
270 datetime = datetime.addSecs(time);
271 //qDebug(QString("Interface: %1 %2").arg(datetime.toString()).arg(pid).latin1());
272
273 // Calculate the start and renew times
274 leaseObtained= datetime.toString();
275
276 // Calculate the start and renew times
277 datetime = datetime.addSecs(leaseTime);
278 leaseExpires = datetime.toString();
279
280 dhcp = true;
281
282 emit(updateInterface(this));
283 return true;
284}
285
286// interface.cpp
287