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