summaryrefslogtreecommitdiff
path: root/noncore/net/networksetup/interfaces/interface.cpp
Unidiff
Diffstat (limited to 'noncore/net/networksetup/interfaces/interface.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/net/networksetup/interfaces/interface.cpp297
1 files changed, 0 insertions, 297 deletions
diff --git a/noncore/net/networksetup/interfaces/interface.cpp b/noncore/net/networksetup/interfaces/interface.cpp
deleted file mode 100644
index d964961..0000000
--- a/noncore/net/networksetup/interfaces/interface.cpp
+++ b/dev/null
@@ -1,297 +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), hardwareName("Unknown"), moduleOwner(NULL), status(newSatus), attached(false), dhcp(false), macAddress(""), ip("0.0.0.0"), broadcast(""), subnetMask("0.0.0.0"){
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(const 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 emit (updateMessage("Unable to start interface,\n already started"));
68 return;
69 }
70
71 int ret = system(QString("%1 %2 up").arg(IFCONFIG).arg(this->name()).latin1());
72 // See if it was successfull...
73 if(ret != 0){
74 emit (updateMessage("Starting interface failed"));
75 return;
76 }
77
78 status = true;
79 refresh();
80 emit (updateMessage("Start successfull"));
81}
82
83/**
84 * Try to stop the interface.
85 */
86void Interface::stop(){
87 // check to see if we are already stopped.
88 if(false == status){
89 emit (updateMessage("Unable to stop interface,\n already stopped"));
90 return;
91 }
92
93 int ret = system(QString("%1 %2 down").arg(IFCONFIG).arg(this->name()).latin1());
94 if(ret != 0){
95 emit (updateMessage("Stopping interface failed"));
96 return;
97 }
98
99 status = false;
100 refresh();
101 emit (updateMessage("Stop successfull"));
102}
103
104/**
105 * Try to restart the interface.
106 */
107void Interface::restart(){
108 stop();
109 start();
110}
111
112/**
113 * Try to refresh the information about the interface.
114 * First call ifconfig, then check the dhcp-info file
115 * @return bool true if successfull.
116 */
117bool Interface::refresh(){
118 // See if we are up.
119 if(status == false){
120 macAddress = "";
121 ip = "0.0.0.0";
122 subnetMask = "0.0.0.0";
123 broadcast = "";
124 dhcp = false;
125 dhcpServerIp = "";
126 leaseObtained = "";
127 leaseExpires = "";
128 emit(updateInterface(this));
129 return true;
130 }
131
132 QString fileName = QString("/tmp/%1_ifconfig_info").arg(this->name());
133 int ret = system(QString("%1 %2 > %3").arg(IFCONFIG).arg(this->name()).arg(fileName).latin1());
134 if(ret != 0){
135 qDebug(QString("Interface: Ifconfig return value: %1, is not 0").arg(ret).latin1());
136 return false;
137 }
138
139 QFile file(fileName);
140 if (!file.open(IO_ReadOnly)){
141 qDebug(QString("Interface: Can't open file: %1").arg(fileName).latin1());
142 return false;
143 }
144
145 // Set to the defaults
146 macAddress = "";
147 ip = "0.0.0.0";
148 subnetMask = "0.0.0.0";
149 broadcast = "";
150
151 QTextStream stream( &file );
152 QString line;
153 while ( !stream.eof() ) {
154 line = stream.readLine();
155 if(line.contains("HWaddr")){
156 int mac = line.find("HWaddr");
157 macAddress = line.mid(mac+7, line.length());
158 }
159 if(line.contains("inet addr")){
160 int ipl = line.find("inet addr");
161 int space = line.find(" ", ipl+10);
162 ip = line.mid(ipl+10, space-ipl-10);
163 }
164 if(line.contains("Mask")){
165 int mask = line.find("Mask");
166 subnetMask = line.mid(mask+5, line.length());
167 }
168 if(line.contains("Bcast")){
169 int mask = line.find("Bcast");
170 int space = line.find(" ", mask+6);
171 broadcast = line.mid(mask+6, space-mask-6);
172 }
173 }
174 file.close();
175 QFile::remove(fileName);
176
177 // DHCP TESTING
178 // reset DHCP info
179 dhcpServerIp = "";
180 leaseObtained = "";
181 leaseExpires = "";
182 dhcp = false;
183
184 QString dhcpDirectory(DHCP_INFO_DIR);
185 QDir d(dhcpDirectory);
186 if(!d.exists(dhcpDirectory))
187 dhcpDirectory = "/var/run";
188
189 // See if we have
190 QString dhcpFile(QString(dhcpDirectory+"/dhcpcd-%1.info").arg(this->name()));
191 // If there is no DHCP information then exit now with no errors.
192 if(!QFile::exists(dhcpFile)){
193 emit(updateInterface(this));
194 return true;
195 }
196
197 file.setName(dhcpFile);
198 if (!file.open(IO_ReadOnly)){
199 qDebug(QString("Interface: Can't open file: %1").arg(dhcpFile).latin1());
200 return false;
201 }
202
203 // leaseTime and renewalTime and used if pid and deamon exe can be accessed.
204 int leaseTime = 0;
205 int renewalTime = 0;
206
207 stream.setDevice( &file );
208 while ( !stream.eof() ) {
209 line = stream.readLine();
210 if(line.contains("DHCPSIADDR="))
211 dhcpServerIp = line.mid(11, line.length());
212 if(line.contains("LEASETIME="))
213 leaseTime = line.mid(10, line.length()).toInt();
214 if(line.contains("RENEWALTIME="))
215 renewalTime = line.mid(12, line.length()).toInt();
216 }
217 file.close();
218 //qDebug(QString("Interface: leaseTime: %1").arg(leaseTime).latin1());
219 //qDebug(QString("Interface: renewalTime: %1").arg(renewalTime).latin1());
220
221 // Get the pid of the deamond
222 dhcpFile = (QString(dhcpDirectory+"/dhcpcd-%1.pid").arg(this->name()));
223 file.setName(dhcpFile);
224 if (!file.open(IO_ReadOnly)){
225 qDebug(QString("Interface: Can't open file: %1").arg(dhcpFile).latin1());
226 return false;
227 }
228
229 int pid = -1;
230 stream.setDevice( &file );
231 while ( !stream.eof() ) {
232 line = stream.readLine();
233 pid = line.toInt();
234 }
235 file.close();
236
237 if( pid == -1){
238 qDebug("Interface: Could not get pid of dhcpc deamon.");
239 return false;
240 }
241
242 // Get the start running time of the deamon
243 fileName = (QString("/proc/%1/stat").arg(pid));
244 file.setName(fileName);
245 stream.setDevice( &file );
246 if (!file.open(IO_ReadOnly)){
247 qDebug(QString("Interface: Can't open file: %1").arg(fileName).latin1());
248 return false;
249 }
250 while ( !stream.eof() ) {
251 line = stream.readLine();
252 }
253 file.close();
254 long time = 0;
255 // Grab the start time
256 // pid com state ppid pgrp session tty_nr tpgid flags
257 sscanf(line.latin1(), "%*d %*s %*c %*d %*d %*d %*d %*d %*u "
258 // minflt cminflt majflt cmajflt utime stime cutime cstime priority
259 "%*u %*u %*u %*u %*u %*u %*d %*d %*d "
260 // nice 0 itrealvalue starttime
261 "%*d %*d %*d %lu", (long*) &time);
262 time = time/100;
263
264 QDateTime datetime(QDateTime::currentDateTime());
265
266 // Get the uptime of the computer.
267 QFile f("/proc/uptime");
268 if ( f.open(IO_ReadOnly) ) { // file opened successfully
269 QTextStream t( &f ); // use a text stream
270 int sec = 0;
271 t >> sec;
272 datetime = datetime.addSecs((-1*sec));
273 f.close();
274 }
275 else{
276 qDebug("Interface: Can't open /proc/uptime to retrive uptime.");
277 return false;
278 }
279
280 datetime = datetime.addSecs(time);
281 //qDebug(QString("Interface: %1 %2").arg(datetime.toString()).arg(pid).latin1());
282
283 // Calculate the start and renew times
284 leaseObtained= datetime.toString();
285
286 // Calculate the start and renew times
287 datetime = datetime.addSecs(leaseTime);
288 leaseExpires = datetime.toString();
289
290 dhcp = true;
291
292 emit(updateInterface(this));
293 return true;
294}
295
296// interface.cpp
297