Diffstat (limited to 'noncore/applets/wirelessapplet/networkinfo.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/applets/wirelessapplet/networkinfo.cpp | 284 |
1 files changed, 0 insertions, 284 deletions
diff --git a/noncore/applets/wirelessapplet/networkinfo.cpp b/noncore/applets/wirelessapplet/networkinfo.cpp deleted file mode 100644 index e0c487b..0000000 --- a/noncore/applets/wirelessapplet/networkinfo.cpp +++ b/dev/null | |||
@@ -1,284 +0,0 @@ | |||
1 | /********************************************************************** | ||
2 | ** MNetwork* classes | ||
3 | ** | ||
4 | ** Encapsulate network information | ||
5 | ** | ||
6 | ** Copyright (C) 2002, Michael Lauer | ||
7 | ** mickey@tm.informatik.uni-frankfurt.de | ||
8 | ** http://www.Vanille.de | ||
9 | ** | ||
10 | ** Based on portions of the Wireless Extensions | ||
11 | ** Copyright (c) 1997-2002 Jean Tourrilhes <jt@hpl.hp.com> | ||
12 | ** | ||
13 | ** This file may be distributed and/or modified under the terms of the | ||
14 | ** GNU General Public License version 2 as published by the Free Software | ||
15 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
16 | ** packaging of this file. | ||
17 | ** | ||
18 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
19 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
20 | ** | ||
21 | **********************************************************************/ | ||
22 | |||
23 | #include "networkinfo.h" | ||
24 | |||
25 | #include <arpa/inet.h> | ||
26 | #include <sys/socket.h> | ||
27 | #include <linux/if_ether.h> | ||
28 | #include <netinet/ip.h> | ||
29 | #include <sys/ioctl.h> | ||
30 | #include <linux/wireless.h> | ||
31 | |||
32 | #include <unistd.h> | ||
33 | #include <math.h> | ||
34 | #include <errno.h> | ||
35 | #include <string.h> | ||
36 | |||
37 | #include <stdlib.h> | ||
38 | |||
39 | #include <qstring.h> | ||
40 | #include <qfile.h> | ||
41 | #include <qtextstream.h> | ||
42 | |||
43 | /* estimated wireless signal strength and noise level values | ||
44 | based on experimentation with Orinoco and Prism2 cards. | ||
45 | Seem to be correct, but please inform me, if you got values | ||
46 | outside these boundaries. :Mickey: */ | ||
47 | |||
48 | #define IW_UPPER 220 | ||
49 | #define IW_LOWER 140 | ||
50 | |||
51 | #define PROCNETDEV "/proc/net/dev" | ||
52 | #define PROCNETWIRELESS "/proc/net/wireless" | ||
53 | |||
54 | //#define MDEBUG 0 | ||
55 | #undef MDEBUG | ||
56 | |||
57 | //--------------------------------------------------------------------------- | ||
58 | // class MNetworkInterface | ||
59 | // | ||
60 | |||
61 | MNetworkInterface::MNetworkInterface( const char* name ) | ||
62 | :name( name ) | ||
63 | { | ||
64 | struct ifreq ifr; | ||
65 | struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr; | ||
66 | fd = socket( AF_INET, SOCK_DGRAM, 0 ); | ||
67 | } | ||
68 | |||
69 | MNetworkInterface::~MNetworkInterface() | ||
70 | { | ||
71 | if ( fd != -1 ) | ||
72 | close( fd ); | ||
73 | } | ||
74 | |||
75 | bool MNetworkInterface::updateStatistics() | ||
76 | { | ||
77 | return true; | ||
78 | } | ||
79 | |||
80 | //--------------------------------------------------------------------------- | ||
81 | // class MWirelessNetworkInterface | ||
82 | // | ||
83 | |||
84 | MWirelessNetworkInterface::MWirelessNetworkInterface( const char* n ) | ||
85 | :MNetworkInterface( n ) | ||
86 | { | ||
87 | signal = 0; | ||
88 | noise = 0; | ||
89 | quality = 0; | ||
90 | } | ||
91 | |||
92 | MWirelessNetworkInterface::~MWirelessNetworkInterface() | ||
93 | { | ||
94 | } | ||
95 | |||
96 | int MWirelessNetworkInterface::qualityPercent() | ||
97 | { | ||
98 | return ( quality*100 ) / 92; | ||
99 | } | ||
100 | |||
101 | int MWirelessNetworkInterface::signalPercent() | ||
102 | { | ||
103 | return ( ( signal-IW_LOWER ) * 100 ) / IW_UPPER; | ||
104 | } | ||
105 | |||
106 | int MWirelessNetworkInterface::noisePercent() | ||
107 | { | ||
108 | return ( ( noise-IW_LOWER ) * 100 ) / IW_UPPER; | ||
109 | } | ||
110 | |||
111 | bool MWirelessNetworkInterface::updateStatistics() | ||
112 | { | ||
113 | bool base = MNetworkInterface::updateStatistics(); | ||
114 | if ( !base ) | ||
115 | return false; | ||
116 | |||
117 | const char* buffer[200]; | ||
118 | |||
119 | struct iwreq iwr; | ||
120 | memset( &iwr, 0, sizeof( iwr ) ); | ||
121 | iwr.u.essid.pointer = (caddr_t) buffer; | ||
122 | iwr.u.essid.length = IW_ESSID_MAX_SIZE; | ||
123 | iwr.u.essid.flags = 0; | ||
124 | |||
125 | // check if it is an IEEE 802.11 standard conform | ||
126 | // wireless device by sending SIOCGIWESSID | ||
127 | // which also gives back the Extended Service Set ID | ||
128 | // (see IEEE 802.11 for more information) | ||
129 | |||
130 | strcpy( iwr.ifr_ifrn.ifrn_name, (const char*) name ); | ||
131 | int result = ioctl( fd, SIOCGIWESSID, &iwr ); | ||
132 | if ( result == 0 ) | ||
133 | { | ||
134 | hasWirelessExtensions = true; | ||
135 | iwr.u.essid.pointer[(unsigned int) iwr.u.essid.length-1] = '\0'; | ||
136 | essid = iwr.u.essid.pointer; | ||
137 | } | ||
138 | else essid = "*** Unknown ***"; | ||
139 | |||
140 | // Address of associated access-point | ||
141 | |||
142 | result = ioctl( fd, SIOCGIWAP, &iwr ); | ||
143 | if ( result == 0 ) | ||
144 | { | ||
145 | APAddr.sprintf( "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X", | ||
146 | iwr.u.ap_addr.sa_data[0]&0xff, | ||
147 | iwr.u.ap_addr.sa_data[1]&0xff, | ||
148 | iwr.u.ap_addr.sa_data[2]&0xff, | ||
149 | iwr.u.ap_addr.sa_data[3]&0xff, | ||
150 | iwr.u.ap_addr.sa_data[4]&0xff, | ||
151 | iwr.u.ap_addr.sa_data[5]&0xff ); | ||
152 | } else APAddr = "*** Unknown ***"; | ||
153 | |||
154 | iwr.u.data.pointer = (caddr_t) buffer; | ||
155 | iwr.u.data.length = IW_ESSID_MAX_SIZE; | ||
156 | iwr.u.data.flags = 0; | ||
157 | |||
158 | result = ioctl( fd, SIOCGIWNICKN, &iwr ); | ||
159 | if ( result == 0 ) | ||
160 | { | ||
161 | iwr.u.data.pointer[(unsigned int) iwr.u.data.length-1] = '\0'; | ||
162 | nick = iwr.u.data.pointer; | ||
163 | } else nick = "*** Unknown ***"; | ||
164 | |||
165 | result = ioctl( fd, SIOCGIWMODE, &iwr ); | ||
166 | if ( result == 0 ) | ||
167 | mode = ( iwr.u.mode == IW_MODE_ADHOC ) ? "Ad-Hoc" : "Managed"; | ||
168 | else mode = "*** Unknown ***"; | ||
169 | |||
170 | result = ioctl( fd, SIOCGIWFREQ, &iwr ); | ||
171 | if ( result == 0 ) | ||
172 | freq = double( iwr.u.freq.m ) * pow( 10, iwr.u.freq.e ) / 1000000000; | ||
173 | else freq = 0; | ||
174 | |||
175 | // gather link quality from /proc/net/wireless | ||
176 | |||
177 | char c; | ||
178 | QString status; | ||
179 | QString name; | ||
180 | QFile wfile( PROCNETWIRELESS ); | ||
181 | bool hasFile = wfile.open( IO_ReadOnly ); | ||
182 | QTextStream wstream( &wfile ); | ||
183 | if ( hasFile ) | ||
184 | { | ||
185 | wstream.readLine(); // skip the first two lines | ||
186 | wstream.readLine(); // because they only contain headers | ||
187 | } | ||
188 | if ( ( !hasFile ) || ( wstream.atEnd() ) ) | ||
189 | { | ||
190 | #ifdef MDEBUG | ||
191 | qDebug( "WIFIAPPLET: D'oh! Someone removed the card..." ); | ||
192 | #endif | ||
193 | quality = -1; | ||
194 | signal = IW_LOWER; | ||
195 | noise = IW_LOWER; | ||
196 | return false; | ||
197 | } | ||
198 | |||
199 | wstream >> name >> status >> quality >> c >> signal >> c >> noise; | ||
200 | |||
201 | #ifdef MDEBUG | ||
202 | if ( quality > 92 ) | ||
203 | qDebug( "WIFIAPPLET: D'oh! Quality %d > estimated max!\n", quality ); | ||
204 | |||
205 | if ( ( signal > IW_UPPER ) || ( signal < IW_LOWER ) ) | ||
206 | qDebug( "WIFIAPPLET: Doh! Strength %d > estimated max!\n", signal ); | ||
207 | |||
208 | if ( ( noise > IW_UPPER ) || ( noise < IW_LOWER ) ) | ||
209 | qDebug( "WIFIAPPLET: Doh! Noise %d > estimated max!\n", noise ); | ||
210 | #endif | ||
211 | |||
212 | return true; | ||
213 | |||
214 | } | ||
215 | |||
216 | //--------------------------------------------------------------------------- | ||
217 | // class Network | ||
218 | // | ||
219 | |||
220 | MNetwork::MNetwork() | ||
221 | { | ||
222 | procfile = PROCNETDEV; | ||
223 | } | ||
224 | |||
225 | MNetwork::~MNetwork() | ||
226 | { | ||
227 | } | ||
228 | |||
229 | //--------------------------------------------------------------------------- | ||
230 | // class WirelessNetwork | ||
231 | // | ||
232 | |||
233 | MWirelessNetwork::MWirelessNetwork() | ||
234 | { | ||
235 | procfile = PROCNETWIRELESS; | ||
236 | } | ||
237 | |||
238 | MWirelessNetwork::~MWirelessNetwork() | ||
239 | { | ||
240 | } | ||
241 | |||
242 | MNetworkInterface* MWirelessNetwork::createInterface( const char* n ) const | ||
243 | { | ||
244 | return new MWirelessNetworkInterface( n ); | ||
245 | } | ||
246 | |||
247 | //--------------------------------------------------------------------------- | ||
248 | // class NetworkInterface | ||
249 | // | ||
250 | |||
251 | MNetworkInterface* MNetwork::getFirstInterface() | ||
252 | { | ||
253 | enumerateInterfaces(); | ||
254 | InterfaceMapIterator it( interfaces ); | ||
255 | return ( it.count() > 0 ) ? it.toFirst() : 0; | ||
256 | } | ||
257 | |||
258 | void MNetwork::enumerateInterfaces() | ||
259 | { | ||
260 | interfaces.clear(); | ||
261 | QString str; | ||
262 | QFile f( procfile ); | ||
263 | bool hasFile = f.open( IO_ReadOnly ); | ||
264 | if ( !hasFile ) | ||
265 | return; | ||
266 | QTextStream s( &f ); | ||
267 | s.readLine(); | ||
268 | s.readLine(); | ||
269 | while ( !s.atEnd() ) | ||
270 | { | ||
271 | s >> str; | ||
272 | str.truncate( str.find( ':' ) ); | ||
273 | #ifdef MDEBUG | ||
274 | qDebug( "WIFIAPPLET: found interface '%s'", (const char*) str ); | ||
275 | #endif | ||
276 | interfaces.insert( str, createInterface( str ) ); | ||
277 | s.readLine(); | ||
278 | } | ||
279 | } | ||
280 | |||
281 | MNetworkInterface* MNetwork::createInterface( const char* n ) const | ||
282 | { | ||
283 | return new MNetworkInterface( n ); | ||
284 | } | ||