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