Wlan/API.pm

filter: Statementstmt 24.6% 34 / 138 Branchbran 5.5% 2 / 36 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 47.6% 10 / 21 Podpod 0.0% 0 / 11 totaltotal 21.1% 46 / 218 CC 25 SCAR 24.5
CC
25
Cov
20%
WlanGetAvailableNetworkList
30.0
unpack_struct
20.0
WlanOpenHandle
12.0
CRAP 11.6
« Wlan.pm ↑ Summary
1 package Win32::Wlan::API;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use vars qw($VERSION $wlan_available %API @signatures @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 $VERSION = '0.07';
12  
13 # This is used to determine whether we have a 64bit Win32::API
14 # or a 32bit Win32::API - a pointer is 8 or 4 bytes wide
15 0 ▶sub Zero() { "\0" x $Config{ptrsize} };
Subroutine Zero: not called CC=1 SCAR=6.9
Pod undocumented
16  
17 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 @signatures = (
19 ['WlanOpenHandle' => 'IIPP' => 'I'],
20 ['WlanCloseHandle' => 'II' => 'I'],
21 ['WlanFreeMemory' => 'I' => 'I'],
22 ['WlanEnumInterfaces' => 'IIP' => 'I'],
23 ['WlanQueryInterface' => 'IPIIPPI' => 'I'],
24 ['WlanGetAvailableNetworkList' => 'IPIIP' => 'N'],
25 );
26  
27 1 @EXPORT_OK = (qw<$wlan_available WlanQueryCurrentConnection>, map { $_->[0] } @signatures);
28 };
29  
30 1 ▶use constant ERROR_NDIS_DOT11_POWER_STATE_INVALID => 0x80342002;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 not_ready => 0,
34 connected => 1,
35 ad_hoc_network_formed => 2,
36 disconnecting => 3,
37 disconnected => 4,
38 associating => 5,
39 discovering => 6,
40 authenticating => 7
41 1 };
42  
43 1 ▶if (! load_functions()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not load_functions()) { } 1 0 1
44 # Wlan functions are not available
45 1 $wlan_available = 0;
46 } else {
47 0 $wlan_available = 1;
48 };
49  
50 0 ▶sub Win32_Error_String {
Subroutine Win32_Error_String: not called CC=2 SCAR=17.9
Pod undocumented
51 0 my( $rc ) = @_;
52 0 ▶ return eval { require Win32; Win32::FormatMessage($rc) } || "Error $rc"
Condition0%0/2
eval { do { require Win32; Win32::FormatMessage($rc) } } || "Error $rc"
l!l
00
MC/DC0%0/1
eval { do { require Win32; Win32::FormatMessage($rc) } } || "Error $rc"
eval { do { require Win32; Win32::FormatMessage($rc) } }
Truth table0%0/2
A
A= eval { do { require Win32; Win32::FormatMessage($rc) } }
Aresult
11
00
53 }
54  
55 0 ▶sub unpack_struct {
Subroutine unpack_struct: not called CC=4 SCAR=30.0
Pod undocumented
56 # Unpacks a string into a hash
57 # according to a key/unpack template structure
58 0 my $desc = shift;
59 0 my @keys;
60 0 my $template = '';
61  
62 0 for (0..$#{$desc}) {
63 0 ▶ if ($_ % 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ % 2) { } 0 0 0
elsif ($desc->[$_] ne '') { } 0 0 0
64 0 $template .= $desc->[ $_ ]
65 } elsif ($desc->[ $_ ] ne '') {
66 0 push @keys, $desc->[ $_ ]
67 };
68 };
69  
70 0 my %res;
71 0 @res{ @keys } = unpack $template, shift;
72 0 %res
73 }
74  
75 0 ▶sub WlanOpenHandle {
Subroutine WlanOpenHandle: not called CC=3 SCAR=24.8
Pod undocumented
76 0 ▶ croak "Wlan functions are not available" unless $wlan_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $Win32::Wlan::API::wlan_available 0 0 0
77 0 my $version = Zero;
78 0 my $handle = Zero;
79 0 my $rc;
80 0 $wlan_available = 0; # Assume unavailibility
81 0 ▶ ($rc = $API{ WlanOpenHandle }->Call(2,0,$version,$handle)) == 0
Branch: 1 branch
BranchTrueFalseTotal
($rc = $Win32::Wlan::API::API{"WlanOpenHandle"}->Call(2, 0, $version, $handle)) == 0 or croak(Win32_Error_String($rc)) 0 0 0
82 or croak Win32_Error_String($rc);
83 0 $wlan_available = 1; # Ok, finally
84 0 my $h = unpack "V", $handle;
85 0 $h
86 };
87  
88 0 ▶sub WlanCloseHandle {
Subroutine WlanCloseHandle: not called CC=3 SCAR=24.8
Pod undocumented
89 0 ▶ croak "Wlan functions are not available" unless $wlan_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $Win32::Wlan::API::wlan_available 0 0 0
90 0 my ($handle) = @_;
91 0 ▶ $API{ WlanCloseHandle }->Call($handle,0) == 0
Branch: 1 branch
BranchTrueFalseTotal
$Win32::Wlan::API::API{"WlanCloseHandle"}->Call($handle, 0) == 0 or croak($^E) 0 0 0
92 or croak $^E;
93 };
94  
95 0 ▶sub WlanFreeMemory {
Subroutine WlanFreeMemory: not called CC=2 SCAR=17.9
Pod undocumented
96 0 ▶ croak "Wlan functions are not available" unless $wlan_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $Win32::Wlan::API::wlan_available 0 0 0
97 0 my ($block) = @_;
98 0 $API{ WlanFreeMemory }->Call($block);
99 };
100  
101 0 ▶sub _unpack_counted_array {
Subroutine _unpack_counted_array: not called CC=3 SCAR=24.8
102 0 my ($pointer,$template,$size) = @_;
103 0 my $info = unpack 'P8', $pointer;
104 0 my ($count,$curr) = unpack 'VV', $info;
105 0 my $data = unpack "P" . (8+$count*$size), $pointer;
106 0 my @items = unpack "x8 ($template)$count", $data;
107 0 my @res;
108 0 ▶ if ($count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count) 0 0 0
109 0 my $elements_per_item = @items / $count;
110 0 ▶ while (@items) {
Branch: 1 branch
BranchTrueFalseTotal
if (@items) 0 0 0
111 0 push @res, [splice @items, 0, $elements_per_item ]
112 };
113 };
114 @res
115 0 };
116  
117 0 ▶sub WlanEnumInterfaces {
Subroutine WlanEnumInterfaces: not called CC=3 SCAR=24.8
Pod undocumented
118 0 ▶ croak "Wlan functions are not available" unless $wlan_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $Win32::Wlan::API::wlan_available 0 0 0
119 0 my ($handle) = @_;
120 0 my $interfaces = Zero;
121 0 ▶ $API{ WlanEnumInterfaces }->Call($handle,0,$interfaces) == 0
Branch: 1 branch
BranchTrueFalseTotal
$Win32::Wlan::API::API{"WlanEnumInterfaces"}->Call($handle, 0, $interfaces) == 0 or croak($^E) 0 0 0
122 or croak $^E;
123 0 my @items = _unpack_counted_array($interfaces,'a16 a512 V',16+512+4);
124 @items = map {
125 # First element is the GUUID of the interface
126 # Name is in 16bit UTF
127 0 $_->[1] = decode('UTF-16LE' => $_->[1]);
128 0 $_->[1] =~ s/\0+$//;
129 # The third element is the status of the interface
130  
131 +{
132 0 guuid => $_->[0],
133 name => $_->[1],
134 status => $_->[2],
135 };
136 } @items;
137  
138 0 $interfaces = unpack 'V', $interfaces;
139 0 WlanFreeMemory($interfaces);
140 @items
141 0 };
142  
143 0 ▶sub WlanQueryInterface {
Subroutine WlanQueryInterface: not called CC=3 SCAR=24.8
Pod undocumented
144 0 ▶ croak "Wlan functions are not available" unless $wlan_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $Win32::Wlan::API::wlan_available 0 0 0
145 0 my ($handle,$interface,$op) = @_;
146 0 my $size = Zero;
147 0 my $data = Zero;
148 0 ▶ $API{ WlanQueryInterface }->Call($handle, $interface, $op, 0, $size, $data, 0) == 0
Branch: 1 branch
BranchTrueFalseTotal
$Win32::Wlan::API::API{"WlanQueryInterface"}->Call($handle, $interface, $op, 0, $size, $data, 0) == 0 or return 0 0 0
149 or return;
150  
151 0 $size = unpack 'V', $size;
152 0 my $payload = unpack "P$size", $data;
153  
154 0 $data = unpack 'V', $data;
155 0 WlanFreeMemory($data);
156 0 $payload
157 };
158  
159 =head2 C<< WlanCurrentConnection( $handle, $interface ) >>
160  
161 Returns a hashref containing the following keys
162  
163 =over 4
164  
165 =item *
166  
167 C<< state >> - state of the interface
168  
169 One of the following
170  
171 Win32::Wlan::API::not_ready => 0,
172 Win32::Wlan::API::connected => 1,
173 Win32::Wlan::API::ad_hoc_network_formed => 2,
174 Win32::Wlan::API::disconnecting => 3,
175 Win32::Wlan::API::disconnected => 4,
176 Win32::Wlan::API::associating => 5,
177 Win32::Wlan::API::discovering => 6,
178 Win32::Wlan::API::authenticating => 7
179  
180 =item *
181  
182 C<< mode >>
183  
184 =item *
185  
186 C<< profile_name >>
187  
188 C<< bss_type >>
189  
190 infrastructure = 1,
191 independent = 2,
192 any = 3
193  
194 =item *
195  
196 auth_algorithm
197  
198 DOT11_AUTH_ALGO_80211_OPEN = 1,
199 DOT11_AUTH_ALGO_80211_SHARED_KEY = 2,
200 DOT11_AUTH_ALGO_WPA = 3,
201 DOT11_AUTH_ALGO_WPA_PSK = 4,
202 DOT11_AUTH_ALGO_WPA_NONE = 5,
203 DOT11_AUTH_ALGO_RSNA = 6, # wpa2
204 DOT11_AUTH_ALGO_RSNA_PSK = 7, # wpa2
205 DOT11_AUTH_ALGO_IHV_START = 0x80000000,
206 DOT11_AUTH_ALGO_IHV_END = 0xffffffff
207  
208 =item *
209  
210 cipher_algorithm
211  
212 DOT11_CIPHER_ALGO_NONE = 0x00,
213 DOT11_CIPHER_ALGO_WEP40 = 0x01,
214 DOT11_CIPHER_ALGO_TKIP = 0x02,
215 DOT11_CIPHER_ALGO_CCMP = 0x04,
216 DOT11_CIPHER_ALGO_WEP104 = 0x05,
217 DOT11_CIPHER_ALGO_WPA_USE_GROUP = 0x100,
218 DOT11_CIPHER_ALGO_RSN_USE_GROUP = 0x100,
219 DOT11_CIPHER_ALGO_WEP = 0x101,
220 DOT11_CIPHER_ALGO_IHV_START = 0x80000000,
221 DOT11_CIPHER_ALGO_IHV_END = 0xffffffff
222  
223 =back
224  
225 =cut
226  
227 0 ▶sub WlanQueryCurrentConnection {
Subroutine WlanQueryCurrentConnection: not called CC=3 SCAR=24.8
Pod undocumented
228 0 my ($handle,$interface) = @_;
229 0 ▶ my $info = WlanQueryInterface($handle,$interface,7) || '';
Condition0%0/2
WlanQueryInterface($handle, $interface, 7) || ''
l!l
00
MC/DC0%0/1
WlanQueryInterface($handle, $interface, 7) || ''
WlanQueryInterface($handle, $interface, 7)
Truth table0%0/2
A
A= WlanQueryInterface($handle, $interface, 7)
Aresult
11
00
230  
231 0 my @WLAN_CONNECTION_ATTRIBUTES = (
232 state => 'V',
233 mode => 'V',
234 profile_name => 'a512',
235 # WLAN_ASSOCIATION_ATTRIBUTES
236 ssid_len => 'V',
237 ssid => 'a32',
238 bss_type => 'V',
239 mac_address => 'a6',
240 dummy => 'a2', # ???
241 phy_type => 'V',
242 phy_index => 'V',
243 signal_quality => 'V',
244 rx_rate => 'V',
245 tx_rate => 'V',
246 security_enabled => 'V', # BOOL
247 onex_enabled => 'V', # BOOL
248 auth_algorithm => 'V',
249 cipher_algorithm => 'V',
250 );
251  
252 0 my %res = unpack_struct(\@WLAN_CONNECTION_ATTRIBUTES, $info);
253  
254 0 ▶ $res{ profile_name } = decode('UTF-16LE', $res{ profile_name }) || '';
Condition0%0/2
&decode('UTF-16LE', $res{'profile_name'}) || ''
l!l
00
MC/DC0%0/1
&decode('UTF-16LE', $res{'profile_name'}) || ''
&decode('UTF-16LE', $res{'profile_name'})
Truth table0%0/2
A
A= &decode('UTF-16LE', $res{'profile_name'})
Aresult
11
00
255 0 $res{ profile_name } =~ s/\0+$//;
256 0 $res{ ssid } = substr $res{ ssid }, 0, $res{ ssid_len };
257  
258 0 $res{ mac_address } = sprintf "%02x:%02x:%02x:%02x:%02x:%02x", unpack 'C*', $res{ mac_address };
259  
260 0 %res
261 }
262  
263 0 ▶sub WlanGetAvailableNetworkList {
Subroutine WlanGetAvailableNetworkList: not called CC=5 SCAR=34.0
Pod undocumented
264 0 my ($handle,$interface,$flags) = @_;
265 0 ▶ $flags ||= 0;
Condition0%0/2
$flags ||= 0
l!l
00
MC/DC0%0/1
$flags ||= 0
$flags
Truth table0%0/2
A
A= $flags
Aresult
11
00
266 0 my $list = Zero;
267 0 my $rc = $API{ WlanGetAvailableNetworkList }->Call($handle,$interface,$flags,0,$list);
268 0 ▶ if( $rc == ERROR_NDIS_DOT11_POWER_STATE_INVALID()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc == 2150899714) 0 0 0
269 0 return;
270 }
271 0 ▶ if( $rc != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc != 0) 0 0 0
272 0 croak Win32_Error_String($rc);
273 }
274 # name ssid_len ssid bss bssids connectable
275 0 my @items = _unpack_counted_array($list, join( '',
276 'a512', # name
277 'V', # ssid_len
278 'a32', # ssid
279 'V', # bss
280 'V', # bssids
281 'V', # connectable
282 'V', # notConnectableReason,
283 'V', # PhysTypes
284 'V8', # PhysType elements
285 'V', # More PhysTypes
286 'V', # wlanSignalQuality from 0=-100dbm to 100=-50dbm, linear
287 'V', # bSecurityEnabled;
288 'V', # dot11DefaultAuthAlgorithm;
289 'V', # dot11DefaultCipherAlgorithm;
290 'V', # dwFlags
291 'V', # dwReserved;
292 ), 512+4+32+20*4);
293 0 for (@items) {
294 0 my %info;
295 0 @info{qw( name ssid_len ssid bss bssids connectable notConnectableReason
296 phystype_count )} = splice @$_, 0, 8;
297 0 $info{ phystypes }= [splice @$_, 0, 8];
298 0 @info{qw( has_more_phystypes
299 signal_quality
300 security_enabled
301 default_auth_algorithm
302 default_cipher_algorithm
303 flags
304 reserved
305 )} = @$_;
306  
307 # Decode the elements
308 0 $info{ ssid } = substr( $info{ ssid }, 0, $info{ ssid_len });
309 0 $info{ name } = decode('UTF-16LE', $info{ name });
310 0 $info{ name } =~ s/\0+$//;
311 0 splice @{$info{ phystypes }}, $info{ phystype_count };
312  
313 0 $_ = \%info;
314 };
315  
316 0 $list = unpack 'V', $list;
317 0 WlanFreeMemory($list);
318 @items
319 0 }
320  
321 1 ▶sub load_functions {
Subroutine load_functions: called CC=4 SCAR=22.9
Pod undocumented
322 1 my $ok = eval {
323 1 require Win32::API;
324 0 1
325 };
326 1 ▶ return if ! $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 0 1
327 0 for my $sig (@signatures) {
328 0 $API{ $sig->[0] } = eval {
329 0 Win32::API->new( 'wlanapi.dll', @$sig );
330 };
331 0 ▶ if (! $API{ $sig->[0] }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Win32::Wlan::API::API{$sig->[0]}) 0 0 0
332 return
333 0 };
334 };
335 0 1
336 };
337  
338 1 1;
339  
340 __END__
« Wlan.pm ↑ Summary