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