blib/lib/Net/Interface/Wireless/FreeBSD.pm

filter: Statementstmt 15.9% 18 / 113 Branchbran 0.0% 0 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 57.1% 4 / 7 Podpod 100% 3 / 3 totaltotal 15.3% 25 / 163 CC 21 SCAR 41.9
CC
21
Cov
12%
ListScan
90.0
Scan
90.0
ListWirelessInterfaces
30.0
CRAP 65.7
↑ Summary
1 package Net::Interface::Wireless::FreeBSD;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our @ISA = qw(Exporter);
9 2 our @EXPORT = qw();
10 2 our @EXPORT_OK = qw(ListWirelessInterfaces ListScan);
11 2 our %EXPORT_TAGS = ();
12  
13 =head1 NAME
14  
15 Net::Interface::Wireless::FreeBSD - Get information for wireless interfactes.
16  
17 =head1 VERSION
18  
19 Version 1.0.0
20  
21 =cut
22  
23 2 our $VERSION = '1.0.0';
24  
25  
26 =head1 SYNOPSIS
27  
28 use Net::Interface::Wireless::FreeBSD qw/ListWirelessInterfaces/;
29  
30 #gets a array of wireless interfaces
31 my @wifs=ListWirelessInterfaces();
32  
33 #get a list of visible APs with exporting the Listscan function for the first wireless device found
34 my %scanhash=Net::Interface::Wireless::FreeBSD::ListScan($wifs[0]);
35  
36 while(my ($ssid, $ap)=each(%scanhash)){
37 print "SSID: ".$ssid.
38 "\nBSSID: ".$ap->{bssid}.
39 "\nChannel: ".$ap->{channel}.
40 "\nSNR: ".$ap->{snr}.
41 "\nRate: ".$ap->{rate}.
42 "\nBeacon Interval: ".$ap->{int}.
43 "\nESS: ".$ap->{ess}.
44 "\nIBSS".$ap->{ibss}.
45 "\nPrivacy: ".$ap->{privacy}.
46 "\nShort Preanble: ".$ap->{"short preamble"}.
47 "\nShort Time Slot: ".$ap->{"short time slot"}."\n\n";
48 };
49  
50 =head1 EXPORT
51  
52 ListWirelessInterfaces
53 ListScan
54  
55 =head1 FUNCTIONS
56  
57 =head2 ListWirelessInterfaces
58  
59 This looks in "/dev/net/" and checks to see if any of them there are
60 recognized interfaces.
61  
62 Currently it recognizes an(4), ath(4), awi(4), ipw(4), iwi(4),
63 ral(4), ural(4), and wi(4).
64  
65 =cut
66  
67 0 ▶sub ListWirelessInterfaces {
Subroutine ListWirelessInterfaces: not called CC=5 SCAR=34.0
Pod documented
68 #a list of wireless interface bases
69 0 my @ifs=("an", "ath", "awi", "ipw", "iwi", "ral", "ural", "wi");
70
71 #try to open the dir that contains the interfaces
72 #return undef upon failure
73 0 ▶ if(!opendir(GETINTERFACES, "/dev/net/")){
Branch: 1 branch
BranchTrueFalseTotal
unless (opendir GETINTERFACES, "/dev/net/") 0 0 0
74 0 return undef;
75 };
76 0 my @foundifs=readdir(GETINTERFACES);
77 0 closedir(GETINTERFACES);
78
79 0 my @wifs=();
80
81 0 my $foundifsInt=0;
82 0 ▶ while(defined($foundifs[$foundifsInt])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $foundifs[$foundifsInt]) 0 0 0
83 0 my $ifsInt=0;
84 #go through and checks the various interfaces
85 0 ▶ while(defined($ifs[$ifsInt])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ifs[$ifsInt]) 0 0 0
86 #builds the check as otherwise it generates errors if it is done in the regex
87 0 my $test="^".$ifs[$ifsInt]."[0123456789]*\$";
88 #checks if the interface matches
89 0 ▶ if($foundifs[$foundifsInt] =~ /$test/){
Branch: 1 branch
BranchTrueFalseTotal
if ($foundifs[$foundifsInt] =~ /$test/) 0 0 0
90 0 push(@wifs, $foundifs[$foundifsInt]);
91 };
92
93 0 $ifsInt++;
94 };
95
96 0 $foundifsInt++;
97 };
98
99 0 return @wifs;
100 };
101  
102 =head2 ListScan($interface)
103  
104 Fetch a list of seen APs. This can be used as a non-super user. It just lists seen APs.
105  
106 This function returns undef upon error and upon success a hash. For info on the hash return,
107 please see the section SCAN RETURN.
108  
109 =cut
110  
111 0 ▶sub ListScan{
Subroutine ListScan: not called CC=9 SCAR=45.0
Pod documented
112 0 my $if=$_[0];
113
114 0 ▶ if (!-e "/dev/net/".$if){
Branch: 1 branch
BranchTrueFalseTotal
unless (-e '/dev/net/' . $if) 0 0 0
115 0 warn("'".$if."' is a non-existant interface.");
116 0 return undef;
117 };
118
119 0 my %scan=();
120
121 0 my @rawscan=`ifconfig -v $if list scan`;
122
123 #tests the first row to see if it matches the expected return for it...
124 #if not, assume there was a error and warn...
125 0 ▶ if($rawscan[0] eq "SSID BSSID CHAN RATE S:N INT CAPS\n"){
Branch: 1 branch
BranchTrueFalseTotal
if ($rawscan[0] eq "SSID BSSID CHAN RATE S:N INT CAPS\n") { } 0 0 0
126  
127 0 my $rawscanInt=1;
128 0 ▶ while(defined($rawscan[$rawscanInt])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rawscan[$rawscanInt]) 0 0 0
129 0 chomp($rawscan[$rawscanInt]);
130 #get the first 34 characters... which will include the
131 0 my $ssid=substr($rawscan[$rawscanInt], 0, 33);
132
133 #remove any trailing spaces. from the ssid
134 0 $ssid=~s/ +$//;
135
136 0 $scan{$ssid}={};
137
138 #sets the bssid
139 0 $scan{$ssid}{bssid}=substr($rawscan[$rawscanInt], 34, 17);
140
141 #gets everything after the bssid
142 0 my $restofstring=substr($rawscan[$rawscanInt], 51);
143
144 #changes multiple spaces into on
145 0 $restofstring =~ s/ +/ /g;
146
147 0 my @rosa=split(/ /, $restofstring);
148  
149 #gets the channel
150 0 $scan{$ssid}{channel}=$rosa[0];
151  
152 #gets the rate
153 0 $scan{$ssid}{rate}=$rosa[1];
154  
155 #gets the signal noise ratio
156 0 $scan{$ssid}{snr}=$rosa[2];
157  
158 #gets the beacon interval
159 0 $scan{$ssid}{int}=$rosa[3];
160  
161 #defines if it is a ESS notwork or not
162 0 ▶ if($rosa[4] =~ /E/){
Branch: 1 branch
BranchTrueFalseTotal
if ($rosa[4] =~ /E/) { } 0 0 0
163 0 $scan{$ssid}{ess}="1";
164 }else{
165 0 $scan{$ssid}{ess}="0";
166 };
167  
168 #defines if it is in IBSS/ad-hoc mode or not
169 0 ▶ if($rosa[4] =~ /I/){
Branch: 1 branch
BranchTrueFalseTotal
if ($rosa[4] =~ /I/) { } 0 0 0
170 0 $scan{$ssid}{ibss}="1";
171 }else{
172 0 $scan{$ssid}{ibss}="0";
173 };
174  
175 #defines if any sort of encryption is in use
176 0 ▶ if($rosa[4] =~ /P/){
Branch: 1 branch
BranchTrueFalseTotal
if ($rosa[4] =~ /P/) { } 0 0 0
177 0 $scan{$ssid}{privacy}="1";
178 }else{
179 0 $scan{$ssid}{privacy}="0";
180 };
181  
182 #defines if the network is using short preamble or not
183 0 ▶ if($rosa[4] =~ /S/){
Branch: 1 branch
BranchTrueFalseTotal
if ($rosa[4] =~ /S/) { } 0 0 0
184 0 $scan{$ssid}{"short preamble"}="1";
185 }else{
186 0 $scan{$ssid}{"short preabmle"}="0";
187 };
188  
189 #defines if any sort of short time slot or not
190 0 ▶ if($rosa[4] =~ /s/){
Branch: 1 branch
BranchTrueFalseTotal
if ($rosa[4] =~ /s/) { } 0 0 0
191 0 $scan{$ssid}{"short time slot"}="1";
192 }else{
193 0 $scan{$ssid}{"short time slot"}="0";
194 };
195  
196 0 $rawscanInt++;
197 };
198
199 }else{
200 # warn("The first line returned from 'ifconfig -v ".$if." list scan' did not match".
201 # '"SSID BSSID CHAN RATE S:N INT CAPS\n".');
202 0 return undef;
203 };
204
205 0 return %scan;
206 };
207  
208 =head2 Scan($interface)
209  
210 Fetch a list of seen APs, but can only be ran by a super user. This initiates a actual scan.
211  
212 You may have to potential reassociate with a AP afterwards if you are connected to one.
213  
214 This function returns undef upon error and upon success a hash. For info on the hash return,
215 please see the section SCAN RETURN.
216  
217 =cut
218  
219 0 ▶sub Scan{
Subroutine Scan: not called CC=9 SCAR=45.0
Pod documented
220 0 my $if=$_[0];
221
222 0 ▶ if (!-e "/dev/net/".$if){
Branch: 1 branch
BranchTrueFalseTotal
unless (-e '/dev/net/' . $if) 0 0 0
223 0 warn("'".$if."' is a non-existant interface.");
224 0 return undef;
225 };
226
227 0 my %scan=();
228
229 0 my @rawscan=`ifconfig -v $if scan`;
230
231 #tests the first row to see if it matches the expected return for it...
232 #if not, assume there was a error and warn...
233 0 ▶ if($rawscan[0] eq "SSID BSSID CHAN RATE S:N INT CAPS\n"){
Branch: 1 branch
BranchTrueFalseTotal
if ($rawscan[0] eq "SSID BSSID CHAN RATE S:N INT CAPS\n") { } 0 0 0
234  
235 0 my $rawscanInt=1;
236 0 ▶ while(defined($rawscan[$rawscanInt])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rawscan[$rawscanInt]) 0 0 0
237 0 chomp($rawscan[$rawscanInt]);
238 #get the first 34 characters... which will include the
239 0 my $ssid=substr($rawscan[$rawscanInt], 0, 33);
240
241 #remove any trailing spaces. from the ssid
242 0 $ssid=~s/ +$//;
243
244 0 $scan{$ssid}={};
245
246 #sets the bssid
247 0 $scan{$ssid}{bssid}=substr($rawscan[$rawscanInt], 34, 17);
248
249 #gets everything after the bssid
250 0 my $restofstring=substr($rawscan[$rawscanInt], 51);
251
252 #changes multiple spaces into on
253 0 $restofstring =~ s/ +/ /g;
254
255 0 my @rosa=split(/ /, $restofstring);
256  
257 #gets the channel
258 0 $scan{$ssid}{channel}=$rosa[0];
259  
260 #gets the rate
261 0 $scan{$ssid}{rate}=$rosa[1];
262  
263 #gets the signal noise ratio
264 0 $scan{$ssid}{snr}=$rosa[2];
265  
266 #gets the beacon interval
267 0 $scan{$ssid}{int}=$rosa[3];
268  
269 #defines if it is a ESS notwork or not
270 0 ▶ if($rosa[4] =~ /E/){
Branch: 1 branch
BranchTrueFalseTotal
if ($rosa[4] =~ /E/) { } 0 0 0
271 0 $scan{$ssid}{ess}="1";
272 }else{
273 0 $scan{$ssid}{ess}="0";
274 };
275  
276 #defines if it is in IBSS/ad-hoc mode or not
277 0 ▶ if($rosa[4] =~ /I/){
Branch: 1 branch
BranchTrueFalseTotal
if ($rosa[4] =~ /I/) { } 0 0 0
278 0 $scan{$ssid}{ibss}="1";
279 }else{
280 0 $scan{$ssid}{ibss}="0";
281 };
282  
283 #defines if any sort of encryption is in use
284 0 ▶ if($rosa[4] =~ /P/){
Branch: 1 branch
BranchTrueFalseTotal
if ($rosa[4] =~ /P/) { } 0 0 0
285 0 $scan{$ssid}{privacy}="1";
286 }else{
287 0 $scan{$ssid}{privacy}="0";
288 };
289  
290 #defines if the network is using short preamble or not
291 0 ▶ if($rosa[4] =~ /S/){
Branch: 1 branch
BranchTrueFalseTotal
if ($rosa[4] =~ /S/) { } 0 0 0
292 0 $scan{$ssid}{"short preamble"}="1";
293 }else{
294 0 $scan{$ssid}{"short preabmle"}="0";
295 };
296  
297 #defines if any sort of short time slot or not
298 0 ▶ if($rosa[4] =~ /s/){
Branch: 1 branch
BranchTrueFalseTotal
if ($rosa[4] =~ /s/) { } 0 0 0
299 0 $scan{$ssid}{"short time slot"}="1";
300 }else{
301 0 $scan{$ssid}{"short time slot"}="0";
302 };
303  
304 0 $rawscanInt++;
305 };
306
307 }else{
308 0 warn("The first line returned from 'ifconfig -v ".$if." list scan' did not match".
309 '"SSID BSSID CHAN RATE S:N INT CAPS\n".');
310 0 return undef;
311 };
312
313 0 return %scan;
314 };
315  
316 =head1 SCAN RETURN
317  
318 The return is a hash, whose keys are the seen SSIDs. The value of each key is then another hash.
319 See below for a list of keys for that hash.
320  
321 =over
322  
323 =item bssid
324  
325 The BSSID of the base station in question.
326  
327 =item channel
328  
329 The channel the base station is operating on.
330  
331 =item snr
332  
333 The signal to noise ratio of a connection.
334  
335  
336 =item rate
337  
338 The base rate for the AP. This is what it is set to, not a guarrentee you will get it.
339  
340 =item int
341  
342 The beacon interval for the base station.
343  
344 =item ess
345  
346 A boolean value for if the AP is in ESS mode or not. This is always defined, even if
347 not in ESS mode.
348  
349 =item ibss
350  
351 A boolean value for if the AP is in IBSS/ad-hoc mode or not. This is always defined, even if
352 not in IBSS/ad-hoc mode.
353  
354 =item privacy
355  
356 Wether or not the AP any sort of encryption enabled. This is always defined, even if
357 not it is using encryption.
358  
359 =item short preable
360  
361 Wether or not the AP is set for short preable. This is always defined, even if
362 not it set to short preable.
363  
364 =item short time slot
365  
366 Wether or not the AP is set for short time slot. This is always defined, even if
367 not it set to short time slot.
368  
369 =back
370  
371 =head1 AUTHOR
372  
373 Zane C. Bowers, C<< <vvelox at vvelox.net> >>
374  
375 =head1 BUGS
376  
377 Please report any bugs or feature requests to C<bug-net-interface-wireless-freebsd at rt.cpan.org>, or through
378 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Interface-Wireless-FreeBSD>. I will be notified, and then you'll
379 automatically be notified of progress on your bug as I make changes.
380  
381 =head1 SUPPORT
382  
383 You can find documentation for this module with the perldoc command.
384  
385 perldoc Net::Interface::Wireless::FreeBSD
386  
387  
388 You can also look for information at:
389  
390 =over 4
391  
392 =item * RT: CPAN's request tracker
393  
394 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Interface-Wireless-FreeBSD>
395  
396 =item * AnnoCPAN: Annotated CPAN documentation
397  
398 L<http://annocpan.org/dist/Net-Interface-Wireless-FreeBSD>
399  
400 =item * CPAN Ratings
401  
402 L<http://cpanratings.perl.org/d/Net-Interface-Wireless-FreeBSD>
403  
404 =item * Search CPAN
405  
406 L<http://search.cpan.org/dist/Net-Interface-Wireless-FreeBSD>
407  
408 =back
409  
410  
411 =head1 ACKNOWLEDGEMENTS
412  
413  
414 =head1 COPYRIGHT & LICENSE
415  
416 Copyright 2008 Zane C. Bowers, all rights reserved.
417  
418 This program is free software; you can redistribute it and/or modify it
419 under the same terms as Perl itself.
420  
421  
422 =cut
423  
424 2 1; # End of Net::Interface::Wireless::FreeBSD
↑ Summary