Diffstat (limited to 'noncore/applets/wirelessapplet/networkinfo.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/applets/wirelessapplet/networkinfo.cpp | 267 |
1 files changed, 267 insertions, 0 deletions
diff --git a/noncore/applets/wirelessapplet/networkinfo.cpp b/noncore/applets/wirelessapplet/networkinfo.cpp new file mode 100644 index 0000000..dc5c3be --- a/dev/null +++ b/noncore/applets/wirelessapplet/networkinfo.cpp | |||
@@ -0,0 +1,267 @@ | |||
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 | //--------------------------------------------------------------------------- | ||
55 | // class MNetworkInterface | ||
56 | // | ||
57 | |||
58 | MNetworkInterface::MNetworkInterface( const char* name ) | ||
59 | :name( name ) | ||
60 | { | ||
61 | struct ifreq ifr; | ||
62 | struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr; | ||
63 | fd = socket( AF_INET, SOCK_DGRAM, 0 ); | ||
64 | } | ||
65 | |||
66 | MNetworkInterface::~MNetworkInterface() | ||
67 | { | ||
68 | if ( fd != -1 ) | ||
69 | close( fd ); | ||
70 | } | ||
71 | |||
72 | void MNetworkInterface::updateStatistics() | ||
73 | { | ||
74 | } | ||
75 | |||
76 | //--------------------------------------------------------------------------- | ||
77 | // class MWirelessNetworkInterface | ||
78 | // | ||
79 | |||
80 | MWirelessNetworkInterface::MWirelessNetworkInterface( const char* n ) | ||
81 | :MNetworkInterface( n ) | ||
82 | { | ||
83 | signal = 0; | ||
84 | noise = 0; | ||
85 | quality = 0; | ||
86 | } | ||
87 | |||
88 | MWirelessNetworkInterface::~MWirelessNetworkInterface() | ||
89 | { | ||
90 | } | ||
91 | |||
92 | int MWirelessNetworkInterface::qualityPercent() | ||
93 | { | ||
94 | return ( quality*100 ) / 92; | ||
95 | } | ||
96 | |||
97 | int MWirelessNetworkInterface::signalPercent() | ||
98 | { | ||
99 | return ( ( signal-IW_LOWER ) * 100 ) / IW_UPPER; | ||
100 | } | ||
101 | |||
102 | int MWirelessNetworkInterface::noisePercent() | ||
103 | { | ||
104 | return ( ( noise-IW_LOWER ) * 100 ) / IW_UPPER; | ||
105 | } | ||
106 | |||
107 | void MWirelessNetworkInterface::updateStatistics() | ||
108 | { | ||
109 | MNetworkInterface::updateStatistics(); | ||
110 | |||
111 | const char* buffer[200]; | ||
112 | |||
113 | struct iwreq iwr; | ||
114 | memset( &iwr, 0, sizeof( iwr ) ); | ||
115 | iwr.u.essid.pointer = (caddr_t) buffer; | ||
116 | iwr.u.essid.length = IW_ESSID_MAX_SIZE; | ||
117 | iwr.u.essid.flags = 0; | ||
118 | |||
119 | // check if it is an IEEE 802.11 standard conform | ||
120 | // wireless device by sending SIOCGIWESSID | ||
121 | // which also gives back the Extended Service Set ID | ||
122 | // (see IEEE 802.11 for more information) | ||
123 | |||
124 | strcpy( iwr.ifr_ifrn.ifrn_name, (const char*) name ); | ||
125 | int result = ioctl( fd, SIOCGIWESSID, &iwr ); | ||
126 | if ( result == 0 ) | ||
127 | { | ||
128 | hasWirelessExtensions = true; | ||
129 | iwr.u.essid.pointer[(unsigned int) iwr.u.essid.length-1] = '\0'; | ||
130 | essid = iwr.u.essid.pointer; | ||
131 | } | ||
132 | else essid = "*** Unknown ***"; | ||
133 | |||
134 | // Address of associated access-point | ||
135 | |||
136 | result = ioctl( fd, SIOCGIWAP, &iwr ); | ||
137 | if ( result == 0 ) | ||
138 | { | ||
139 | APAddr.sprintf( "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X", | ||
140 | iwr.u.ap_addr.sa_data[0]&0xff, | ||
141 | iwr.u.ap_addr.sa_data[1]&0xff, | ||
142 | iwr.u.ap_addr.sa_data[2]&0xff, | ||
143 | iwr.u.ap_addr.sa_data[3]&0xff, | ||
144 | iwr.u.ap_addr.sa_data[4]&0xff, | ||
145 | iwr.u.ap_addr.sa_data[5]&0xff ); | ||
146 | } else APAddr = "*** Unknown ***"; | ||
147 | |||
148 | iwr.u.data.pointer = (caddr_t) buffer; | ||
149 | iwr.u.data.length = IW_ESSID_MAX_SIZE; | ||
150 | iwr.u.data.flags = 0; | ||
151 | |||
152 | result = ioctl( fd, SIOCGIWNICKN, &iwr ); | ||
153 | if ( result == 0 ) | ||
154 | { | ||
155 | iwr.u.data.pointer[(unsigned int) iwr.u.data.length-1] = '\0'; | ||
156 | nick = iwr.u.data.pointer; | ||
157 | } else nick = "*** Unknown ***"; | ||
158 | |||
159 | result = ioctl( fd, SIOCGIWMODE, &iwr ); | ||
160 | if ( result == 0 ) | ||
161 | mode = ( iwr.u.mode == IW_MODE_ADHOC ) ? "Ad-Hoc" : "Managed"; | ||
162 | else mode = "*** Unknown ***"; | ||
163 | |||
164 | result = ioctl( fd, SIOCGIWFREQ, &iwr ); | ||
165 | if ( result == 0 ) | ||
166 | freq = double( iwr.u.freq.m ) * pow( 10, iwr.u.freq.e ) / 1000000000; | ||
167 | else freq = 0; | ||
168 | |||
169 | // gather link quality from /proc/net/wireless | ||
170 | |||
171 | char c; | ||
172 | QString status; | ||
173 | QString name; | ||
174 | QFile wfile( PROCNETWIRELESS ); | ||
175 | wfile.open( IO_ReadOnly ); | ||
176 | QTextStream wstream( &wfile ); | ||
177 | wstream.readLine(); // skip the first two lines | ||
178 | wstream.readLine(); // because they only contain headers | ||
179 | |||
180 | if ( wstream.atEnd() ) | ||
181 | { | ||
182 | qDebug( "WIFIAPPLET: D'oh! Someone removed the card..." ); | ||
183 | quality = -1; | ||
184 | signal = IW_LOWER; | ||
185 | noise = IW_LOWER; | ||
186 | return; | ||
187 | } | ||
188 | |||
189 | wstream >> name >> status >> quality >> c >> signal >> c >> noise; | ||
190 | |||
191 | if ( quality > 92 ) | ||
192 | qDebug( "WIFIAPPLET: D'oh! Quality %d > estimated max!\n", quality ); | ||
193 | if ( ( signal > IW_UPPER ) || ( signal < IW_LOWER ) ) | ||
194 | qDebug( "WIFIAPPLET: Doh! Strength %d > estimated max!\n", signal ); | ||
195 | if ( ( noise > IW_UPPER ) || ( noise < IW_LOWER ) ) | ||
196 | qDebug( "WIFIAPPLET: Doh! Noise %d > estimated max!\n", noise ); | ||
197 | } | ||
198 | |||
199 | //--------------------------------------------------------------------------- | ||
200 | // class Network | ||
201 | // | ||
202 | |||
203 | MNetwork::MNetwork() | ||
204 | { | ||
205 | qDebug( "MNetwork::MNetwork()" ); | ||
206 | procfile = "/proc/net/dev"; | ||
207 | } | ||
208 | |||
209 | MNetwork::~MNetwork() | ||
210 | { | ||
211 | qDebug( "MNetwork::~MNetwork()" ); | ||
212 | } | ||
213 | |||
214 | //--------------------------------------------------------------------------- | ||
215 | // class WirelessNetwork | ||
216 | // | ||
217 | |||
218 | MWirelessNetwork::MWirelessNetwork() | ||
219 | { | ||
220 | qDebug( "MWirelessNetwork::MWirelessNetwork()" ); | ||
221 | procfile = "/proc/net/wireless"; | ||
222 | } | ||
223 | |||
224 | MWirelessNetwork::~MWirelessNetwork() | ||
225 | { | ||
226 | qDebug( "MWirelessNetwork::~MWirelessNetwork()" ); | ||
227 | } | ||
228 | |||
229 | MNetworkInterface* MWirelessNetwork::createInterface( const char* n ) const | ||
230 | { | ||
231 | return new MWirelessNetworkInterface( n ); | ||
232 | } | ||
233 | |||
234 | //--------------------------------------------------------------------------- | ||
235 | // class NetworkInterface | ||
236 | // | ||
237 | |||
238 | MNetworkInterface* MNetwork::getFirstInterface() | ||
239 | { | ||
240 | enumerateInterfaces(); | ||
241 | InterfaceMapIterator it( interfaces ); | ||
242 | return ( it.count() > 0 ) ? it.toFirst() : 0; | ||
243 | } | ||
244 | |||
245 | void MNetwork::enumerateInterfaces() | ||
246 | { | ||
247 | interfaces.clear(); | ||
248 | QString str; | ||
249 | QFile f( procfile ); | ||
250 | f.open( IO_ReadOnly ); | ||
251 | QTextStream s( &f ); | ||
252 | s.readLine(); | ||
253 | s.readLine(); | ||
254 | while ( !s.atEnd() ) | ||
255 | { | ||
256 | s >> str; | ||
257 | str.truncate( str.find( ':' ) ); | ||
258 | qDebug( "WIFIAPPLET: found interface '%s'", (const char*) str ); | ||
259 | interfaces.insert( str, createInterface( str ) ); | ||
260 | s.readLine(); | ||
261 | } | ||
262 | } | ||
263 | |||
264 | MNetworkInterface* MNetwork::createInterface( const char* n ) const | ||
265 | { | ||
266 | return new MNetworkInterface( n ); | ||
267 | } | ||