summaryrefslogtreecommitdiff
path: root/noncore/net/networksetup/interface.cpp
Unidiff
Diffstat (limited to 'noncore/net/networksetup/interface.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/net/networksetup/interface.cpp16
1 files changed, 11 insertions, 5 deletions
diff --git a/noncore/net/networksetup/interface.cpp b/noncore/net/networksetup/interface.cpp
index 5b21364..1f32093 100644
--- a/noncore/net/networksetup/interface.cpp
+++ b/noncore/net/networksetup/interface.cpp
@@ -1,236 +1,242 @@
1#include "interface.h" 1#include "interface.h"
2#include <qdatetime.h> 2#include <qdatetime.h>
3#include <qfile.h> 3#include <qfile.h>
4#include <qdir.h>
4#include <qfileinfo.h> 5#include <qfileinfo.h>
5#include <qtextstream.h> 6#include <qtextstream.h>
6 7
7#define IFCONFIG "/sbin/ifconfig" 8#define IFCONFIG "/sbin/ifconfig"
8#define HDCP_INFO_DIR "/etc/dhcpc" 9#define HDCP_INFO_DIR "/etc/dhcpc"
9 10
10#include <stdio.h> 11#include <stdio.h>
11#include <stdlib.h> 12#include <stdlib.h>
12 13
13Interface::Interface(QString name, bool newSatus): status(newSatus), attached(false), interfaceName(name), hardareName("Unknown"), moduleOwner(NULL), macAddress(""), ip("0.0.0.0"), broadcast(""), subnetMask("0.0.0.0"), dhcp(false){ 14Interface::Interface(QString name, bool newSatus): status(newSatus), attached(false), interfaceName(name), hardareName("Unknown"), moduleOwner(NULL), macAddress(""), ip("0.0.0.0"), broadcast(""), subnetMask("0.0.0.0"), dhcp(false){
14 refresh(); 15 refresh();
15} 16}
16 17
17/** 18/**
18 * Try to start the interface. 19 * Try to start the interface.
19 * @return bool true if successfull. 20 * @return bool true if successfull.
20 */ 21 */
21bool Interface::start(){ 22bool Interface::start(){
22 // check to see if we are already running. 23 // check to see if we are already running.
23 if(status) 24 if(status)
24 return false; 25 return false;
25 26
26 int ret = system(QString("%1 %2 up").arg(IFCONFIG).arg(interfaceName).latin1()); 27 int ret = system(QString("%1 %2 up").arg(IFCONFIG).arg(interfaceName).latin1());
27 if(ret != 0) 28 if(ret != 0)
28 return false; 29 return false;
29 30
30 status = true; 31 status = true;
31 refresh(); 32 refresh();
32 return true; 33 return true;
33} 34}
34 35
35/** 36/**
36 * Try to stop the interface. 37 * Try to stop the interface.
37 * @return bool true if successfull. 38 * @return bool true if successfull.
38 */ 39 */
39bool Interface::stop(){ 40bool Interface::stop(){
40 // check to see if we are already stopped. 41 // check to see if we are already stopped.
41 if(status == false) 42 if(status == false)
42 return false; 43 return false;
43 44
44 int ret = system(QString("%1 %2 down").arg(IFCONFIG).arg(interfaceName).latin1()); 45 int ret = system(QString("%1 %2 down").arg(IFCONFIG).arg(interfaceName).latin1());
45 if(ret != 0) 46 if(ret != 0)
46 return false; 47 return false;
47 48
48 status = true; 49 status = true;
49 refresh(); 50 refresh();
50 return true; 51 return true;
51} 52}
52/** 53/**
53 * Try to restart the interface. 54 * Try to restart the interface.
54 * @return bool true if successfull. 55 * @return bool true if successfull.
55 */ 56 */
56bool Interface::restart(){ 57bool Interface::restart(){
57 return (stop() && start()); 58 return (stop() && start());
58} 59}
59 60
60/** 61/**
61 * Try to refresh the information about the interface. 62 * Try to refresh the information about the interface.
62 * First call ifconfig, then check the dhcp-info file 63 * First call ifconfig, then check the dhcp-info file
63 * @return bool true if successfull. 64 * @return bool true if successfull.
64 */ 65 */
65bool Interface::refresh(){ 66bool Interface::refresh(){
66 // See if we are up. 67 // See if we are up.
67 if(status == false){ 68 if(status == false){
68 macAddress = ""; 69 macAddress = "";
69 ip = "0.0.0.0"; 70 ip = "0.0.0.0";
70 subnetMask = "0.0.0.0"; 71 subnetMask = "0.0.0.0";
71 broadcast = ""; 72 broadcast = "";
72 dhcp = false; 73 dhcp = false;
73 dhcpServerIp = ""; 74 dhcpServerIp = "";
74 leaseObtained = ""; 75 leaseObtained = "";
75 leaseExpires = ""; 76 leaseExpires = "";
76 return true; 77 return true;
77 } 78 }
78 79
79 QString fileName = QString("/tmp/%1_ifconfig_info").arg(interfaceName); 80 QString fileName = QString("/tmp/%1_ifconfig_info").arg(interfaceName);
80 int ret = system(QString("%1 %2 > %3").arg(IFCONFIG).arg(interfaceName).arg(fileName).latin1()); 81 int ret = system(QString("%1 %2 > %3").arg(IFCONFIG).arg(interfaceName).arg(fileName).latin1());
81 if(ret != 0){ 82 if(ret != 0){
82 qDebug(QString("Interface: Ifconfig return value: %1, is not 0").arg(ret).latin1()); 83 qDebug(QString("Interface: Ifconfig return value: %1, is not 0").arg(ret).latin1());
83 return false; 84 return false;
84 } 85 }
85 86
86 QFile file(fileName); 87 QFile file(fileName);
87 if (!file.open(IO_ReadOnly)){ 88 if (!file.open(IO_ReadOnly)){
88 qDebug(QString("Interface: Can't open file: %1").arg(fileName).latin1()); 89 qDebug(QString("Interface: Can't open file: %1").arg(fileName).latin1());
89 return false; 90 return false;
90 } 91 }
91 92
92 // Set to the defaults 93 // Set to the defaults
93 macAddress = ""; 94 macAddress = "";
94 ip = "0.0.0.0"; 95 ip = "0.0.0.0";
95 subnetMask = "0.0.0.0"; 96 subnetMask = "0.0.0.0";
96 broadcast = ""; 97 broadcast = "";
97 98
98 QTextStream stream( &file ); 99 QTextStream stream( &file );
99 QString line; 100 QString line;
100 while ( !stream.eof() ) { 101 while ( !stream.eof() ) {
101 line = stream.readLine(); 102 line = stream.readLine();
102 if(line.contains("HWaddr")){ 103 if(line.contains("HWaddr")){
103 int mac = line.find("HWaddr"); 104 int mac = line.find("HWaddr");
104 macAddress = line.mid(mac+7, line.length()); 105 macAddress = line.mid(mac+7, line.length());
105 } 106 }
106 if(line.contains("inet addr")){ 107 if(line.contains("inet addr")){
107 int ipl = line.find("inet addr"); 108 int ipl = line.find("inet addr");
108 int space = line.find(" ", ipl+10); 109 int space = line.find(" ", ipl+10);
109 ip = line.mid(ipl+10, space-ipl-10); 110 ip = line.mid(ipl+10, space-ipl-10);
110 } 111 }
111 if(line.contains("Mask")){ 112 if(line.contains("Mask")){
112 int mask = line.find("Mask"); 113 int mask = line.find("Mask");
113 subnetMask = line.mid(mask+5, line.length()); 114 subnetMask = line.mid(mask+5, line.length());
114 } 115 }
115 if(line.contains("Bcast")){ 116 if(line.contains("Bcast")){
116 int mask = line.find("Bcast"); 117 int mask = line.find("Bcast");
117 int space = line.find(" ", mask+6); 118 int space = line.find(" ", mask+6);
118 broadcast = line.mid(mask+6, space-mask-6); 119 broadcast = line.mid(mask+6, space-mask-6);
119 } 120 }
120 } 121 }
121 file.close(); 122 file.close();
122 QFile::remove(fileName); 123 QFile::remove(fileName);
123 124
124 // DHCP TESTING 125 // DHCP TESTING
125 // reset DHCP info 126 // reset DHCP info
126 dhcpServerIp = ""; 127 dhcpServerIp = "";
127 leaseObtained = ""; 128 leaseObtained = "";
128 leaseExpires = ""; 129 leaseExpires = "";
129 dhcp = false; 130 dhcp = false;
130 131
132 QString dhcpDirectory(HDCP_INFO_DIR);
133 QDir d(dhcpDirectory);
134 if(!d.exists(dhcpDirectory))
135 dhcpDirectory = "/var/run";
136
131 // See if we have 137 // See if we have
132 QString dhcpFile(QString(HDCP_INFO_DIR "/dhcpcd-%1.info").arg(interfaceName)); 138 QString dhcpFile(QString(dhcpDirectory+"/dhcpcd-%1.info").arg(interfaceName));
133 // If there is no DHCP information then exit now with no errors. 139 // If there is no DHCP information then exit now with no errors.
134 if(!QFile::exists(dhcpFile)){ 140 if(!QFile::exists(dhcpFile)){
135 return true; 141 return true;
136 } 142 }
137 143
138 file.setName(dhcpFile); 144 file.setName(dhcpFile);
139 if (!file.open(IO_ReadOnly)){ 145 if (!file.open(IO_ReadOnly)){
140 qDebug(QString("Interface: Can't open file: %1").arg(dhcpFile).latin1()); 146 qDebug(QString("Interface: Can't open file: %1").arg(dhcpFile).latin1());
141 return false; 147 return false;
142 } 148 }
143 149
144 // leaseTime and renewalTime and used if pid and deamon exe can be accessed. 150 // leaseTime and renewalTime and used if pid and deamon exe can be accessed.
145 int leaseTime = 0; 151 int leaseTime = 0;
146 int renewalTime = 0; 152 int renewalTime = 0;
147 153
148 stream.setDevice( &file ); 154 stream.setDevice( &file );
149 while ( !stream.eof() ) { 155 while ( !stream.eof() ) {
150 line = stream.readLine(); 156 line = stream.readLine();
151 if(line.contains("DHCPSID=")) 157 if(line.contains("DHCPSIADDR="))
152 dhcpServerIp = line.mid(8, line.length()); 158 dhcpServerIp = line.mid(11, line.length());
153 if(line.contains("LEASETIME=")) 159 if(line.contains("LEASETIME="))
154 leaseTime = line.mid(10, line.length()).toInt(); 160 leaseTime = line.mid(10, line.length()).toInt();
155 if(line.contains("RENEWALTIME=")) 161 if(line.contains("RENEWALTIME="))
156 renewalTime = line.mid(12, line.length()).toInt(); 162 renewalTime = line.mid(12, line.length()).toInt();
157 } 163 }
158 file.close(); 164 file.close();
159 //qDebug(QString("Interface: leaseTime: %1").arg(leaseTime).latin1()); 165 //qDebug(QString("Interface: leaseTime: %1").arg(leaseTime).latin1());
160 //qDebug(QString("Interface: renewalTime: %1").arg(renewalTime).latin1()); 166 //qDebug(QString("Interface: renewalTime: %1").arg(renewalTime).latin1());
161 167
162 // Get the pid of the deamond 168 // Get the pid of the deamond
163 dhcpFile = (QString(HDCP_INFO_DIR "/dhcpcd-%1.pid").arg(interfaceName)); 169 dhcpFile = (QString(dhcpDirectory+"/dhcpcd-%1.pid").arg(interfaceName));
164 file.setName(dhcpFile); 170 file.setName(dhcpFile);
165 if (!file.open(IO_ReadOnly)){ 171 if (!file.open(IO_ReadOnly)){
166 qDebug(QString("Interface: Can't open file: %1").arg(dhcpFile).latin1()); 172 qDebug(QString("Interface: Can't open file: %1").arg(dhcpFile).latin1());
167 return false; 173 return false;
168 } 174 }
169 175
170 int pid = -1; 176 int pid = -1;
171 stream.setDevice( &file ); 177 stream.setDevice( &file );
172 while ( !stream.eof() ) { 178 while ( !stream.eof() ) {
173 line = stream.readLine(); 179 line = stream.readLine();
174 pid = line.toInt(); 180 pid = line.toInt();
175 } 181 }
176 file.close(); 182 file.close();
177 183
178 if( pid == -1){ 184 if( pid == -1){
179 qDebug("Interface: Could not get pid of dhcpc deamon."); 185 qDebug("Interface: Could not get pid of dhcpc deamon.");
180 return false; 186 return false;
181 } 187 }
182 188
183 // Get the start running time of the deamon 189 // Get the start running time of the deamon
184 fileName = (QString("/proc/%1/stat").arg(pid)); 190 fileName = (QString("/proc/%1/stat").arg(pid));
185 file.setName(fileName); 191 file.setName(fileName);
186 stream.setDevice( &file ); 192 stream.setDevice( &file );
187 if (!file.open(IO_ReadOnly)){ 193 if (!file.open(IO_ReadOnly)){
188 qDebug(QString("Interface: Can't open file: %1").arg(fileName).latin1()); 194 qDebug(QString("Interface: Can't open file: %1").arg(fileName).latin1());
189 return false; 195 return false;
190 } 196 }
191 while ( !stream.eof() ) { 197 while ( !stream.eof() ) {
192 line = stream.readLine(); 198 line = stream.readLine();
193 } 199 }
194 file.close(); 200 file.close();
195 long time = 0; 201 long time = 0;
196 // Grab the start time 202 // Grab the start time
197 // pid com state ppid pgrp session tty_nr tpgid flags 203 // pid com state ppid pgrp session tty_nr tpgid flags
198 sscanf(line.latin1(), "%*d %*s %*c %*d %*d %*d %*d %*d %*u " 204 sscanf(line.latin1(), "%*d %*s %*c %*d %*d %*d %*d %*d %*u "
199 // minflt cminflt majflt cmajflt utime stime cutime cstime priority 205 // minflt cminflt majflt cmajflt utime stime cutime cstime priority
200 "%*u %*u %*u %*u %*u %*u %*d %*d %*d " 206 "%*u %*u %*u %*u %*u %*u %*d %*d %*d "
201 // nice 0 itrealvalue starttime 207 // nice 0 itrealvalue starttime
202 "%*d %*d %*d %lu", (long*) &time); 208 "%*d %*d %*d %lu", (long*) &time);
203 time = time/100; 209 time = time/100;
204 210
205 QDateTime datetime(QDateTime::currentDateTime()); 211 QDateTime datetime(QDateTime::currentDateTime());
206 212
207 // Get the uptime of the computer. 213 // Get the uptime of the computer.
208 QFile f("/proc/uptime"); 214 QFile f("/proc/uptime");
209 if ( f.open(IO_ReadOnly) ) { // file opened successfully 215 if ( f.open(IO_ReadOnly) ) { // file opened successfully
210 QTextStream t( &f ); // use a text stream 216 QTextStream t( &f ); // use a text stream
211 int sec = 0; 217 int sec = 0;
212 t >> sec; 218 t >> sec;
213 datetime = datetime.addSecs((-1*sec)); 219 datetime = datetime.addSecs((-1*sec));
214 f.close(); 220 f.close();
215 } 221 }
216 else{ 222 else{
217 qDebug("Interface: Can't open /proc/uptime to retrive uptime."); 223 qDebug("Interface: Can't open /proc/uptime to retrive uptime.");
218 return false; 224 return false;
219 } 225 }
220 226
221 datetime = datetime.addSecs(time); 227 datetime = datetime.addSecs(time);
222 //qDebug(QString("Interface: %1 %2").arg(datetime.toString()).arg(pid).latin1()); 228 //qDebug(QString("Interface: %1 %2").arg(datetime.toString()).arg(pid).latin1());
223 229
224 // Calculate the start and renew times 230 // Calculate the start and renew times
225 leaseObtained= datetime.toString(); 231 leaseObtained= datetime.toString();
226 232
227 // Calculate the start and renew times 233 // Calculate the start and renew times
228 datetime = datetime.addSecs(leaseTime); 234 datetime = datetime.addSecs(leaseTime);
229 leaseExpires = datetime.toString(); 235 leaseExpires = datetime.toString();
230 236
231 dhcp = true; 237 dhcp = true;
232 return true; 238 return true;
233} 239}
234 240
235// interface.cpp 241// interface.cpp
236 242