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