blib/lib/App/Netdisco/AnyEvent/Nbtstat.pm untested

filter: Statementstmt 0.0% 0 / 127 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 212 CC 15 SCAR 26.5
CC
15
Cov
0%
_decode_rr
42.0
new
20.0
_on_read
12.0
CRAP 14.1
1 package App::Netdisco::AnyEvent::Nbtstat;
2  
3 use strict;
4 use warnings;
5  
6 use Socket qw(AF_INET SOCK_DGRAM inet_aton sockaddr_in);
7 use List::Util ();
8 use Carp ();
9  
10 use AnyEvent::Loop;
11 use AnyEvent (); BEGIN { AnyEvent::common_sense }
12 use AnyEvent::Util ();
13  
14 sub new {
15 my ( $class, %args ) = @_;
16  
17 my $interval = $args{interval};
18 # This default should generate ~ 50 requests per second
19 $interval = 0.2 unless defined $interval;
20  
21 my $timeout = $args{timeout};
22  
23 # Timeout should be 250ms according to RFC1002, but we're going to double
24 $timeout = 0.5 unless defined $timeout;
25  
26 my $self = bless { interval => $interval, timeout => $timeout, %args },
27 $class;
28  
29 Scalar::Util::weaken( my $wself = $self );
30  
31 socket my $fh4, AF_INET, Socket::SOCK_DGRAM(), 0
32 or Carp::croak "Unable to create socket : $!";
33  
34 AnyEvent::Util::fh_nonblocking $fh4, 1;
35 $self->{fh4} = $fh4;
36 $self->{rw4} = AE::io $fh4, 0, sub {
37 if ( my $peer = recv $fh4, my $resp, 2048, 0 ) {
38 $wself->_on_read( $resp, $peer );
39 }
40 };
41  
42 # Nbtstat tasks
43 $self->{_tasks} = {};
44  
45 return $self;
46 }
47  
48 sub interval { @_ > 1 ? $_[0]->{interval} = $_[1] : $_[0]->{interval} }
49  
50 sub timeout { @_ > 1 ? $_[0]->{timeout} = $_[1] : $_[0]->{timeout} }
51  
52 sub nbtstat {
53 my ( $self, $host, $cb ) = @_;
54  
55 my $ip = inet_aton($host);
56 my $port = 137;
57  
58 my $request = {
59 host => $host,
60 results => {},
61 cb => $cb,
62 destination => scalar sockaddr_in( $port, $ip ),
63 };
64  
65 $self->{_tasks}{ $request->{destination} } = $request;
66  
67 my $delay = $self->interval * scalar keys %{ $self->{_tasks} || {} };
68  
69 # There's probably a better way to throttle the sends
70 # but this will work for now since we currently don't support retries
71 my $w; $w = AE::timer $delay, 0, sub {
72 undef $w;
73 $self->_send_request($request);
74 };
75  
76 return $self;
77 }
78  
79 sub _on_read {
80 my ( $self, $resp, $peer ) = @_;
81  
82 ($resp) = $resp =~ /^(.*)$/s
83 if AnyEvent::TAINT && $self->{untaint};
84  
85 # Find our task
86 my $request = $self->{_tasks}{$peer};
87  
88 return unless $request;
89  
90 $self->_store_result( $request, 'OK', $resp );
91  
92 return;
93 }
94  
95 sub _store_result {
96 my ( $self, $request, $status, $resp ) = @_;
97  
98 my $results = $request->{results};
99  
100 my @rr = ();
101 my $mac_address = "";
102  
103 if ( $status eq 'OK' && length($resp) > 56 ) {
104 my $num_names = unpack( "C", substr( $resp, 56 ) );
105 my $name_data = substr( $resp, 57 );
106  
107 for ( my $i = 0; $i < $num_names; $i++ ) {
108 my $rr_data = substr( $name_data, 18 * $i, 18 );
109 push @rr, _decode_rr($rr_data);
110 }
111  
112 $mac_address = join "-",
113 map { sprintf "%02X", $_ }
114 unpack( "C*", substr( $name_data, 18 * $num_names, 6 ) );
115 $results = {
116 'status' => 'OK',
117 'names' => \@rr,
118 'mac_address' => $mac_address
119 };
120 }
121 elsif ( $status eq 'OK' ) {
122 $results = { 'status' => 'SHORT' };
123 }
124 else {
125 $results = { 'status' => $status };
126 }
127  
128 # Clear request specific data
129 delete $request->{timer};
130  
131 # Cleanup
132 delete $self->{_tasks}{ $request->{destination} };
133  
134 # Done
135 $request->{cb}->($results);
136  
137 undef $request;
138  
139 return;
140 }
141  
142 sub _send_request {
143 my ( $self, $request ) = @_;
144  
145 my $msg = "";
146 # We use process id as identifier field, since don't have a need to
147 # unique responses beyond host / port queried
148 $msg .= pack( "n*", $$, 0, 1, 0, 0, 0 );
149 $msg .= _encode_name( "*", "\x00", 0 );
150 $msg .= pack( "n*", 0x21, 0x0001 );
151  
152 $request->{start} = time;
153  
154 $request->{timer} = AE::timer $self->timeout, 0, sub {
155 $self->_store_result( $request, 'TIMEOUT' );
156 };
157  
158 my $fh = $self->{fh4};
159  
160 send $fh, $msg, 0, $request->{destination}
161 or $self->_store_result( $request, 'ERROR' );
162  
163 return;
164 }
165  
166 sub _encode_name {
167 my $name = uc(shift);
168 my $pad = shift || "\x20";
169 my $suffix = shift || 0x00;
170  
171 $name .= $pad x ( 16 - length($name) );
172 substr( $name, 15, 1, chr( $suffix & 0xFF ) );
173  
174 my $encoded_name = "";
175 for my $c ( unpack( "C16", $name ) ) {
176 $encoded_name .= chr( ord('A') + ( ( $c & 0xF0 ) >> 4 ) );
177 $encoded_name .= chr( ord('A') + ( $c & 0xF ) );
178 }
179  
180 # Note that the _encode_name function doesn't add any scope,
181 # nor does it calculate the length (32), it just prefixes it
182 return "\x20" . $encoded_name . "\x00";
183 }
184  
185 sub _decode_rr {
186 my $rr_data = shift;
187  
188 my @nodetypes = qw/B-node P-node M-node H-node/;
189 my ( $name, $suffix, $flags ) = unpack( "a15Cn", $rr_data );
190 $name =~ tr/\x00-\x19/\./; # replace ctrl chars with "."
191 $name =~ s/\s+//g;
192  
193 my $rr = {};
194 $rr->{'name'} = $name;
195 $rr->{'suffix'} = $suffix;
196 $rr->{'G'} = ( $flags & 2**15 ) ? "GROUP" : "UNIQUE";
197 $rr->{'ONT'} = $nodetypes[ ( $flags >> 13 ) & 3 ];
198 $rr->{'DRG'} = ( $flags & 2**12 ) ? "Deregistering" : "Registered";
199 $rr->{'CNF'} = ( $flags & 2**11 ) ? "Conflict" : "";
200 $rr->{'ACT'} = ( $flags & 2**10 ) ? "Active" : "Inactive";
201 $rr->{'PRM'} = ( $flags & 2**9 ) ? "Permanent" : "";
202  
203 return $rr;
204 }
205  
206 1;
207 __END__