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