App-Netdisco
view release on metacpan - search on metacpan
view release on metacpan or search on metacpan
lib/App/Netdisco/AnyEvent/Nbtstat.pm view on Meta::CPAN
package App::Netdisco::AnyEvent::Nbtstat;
use strict;
use warnings;
use Socket qw(AF_INET SOCK_DGRAM inet_aton sockaddr_in);
use List::Util ();
use Carp ();
use AnyEvent (); BEGIN { AnyEvent::common_sense }
use AnyEvent::Util ();
sub new {
my ( $class, %args ) = @_;
my $interval = $args{interval};
# This default should generate ~ 50 requests per second
$interval = 0.2 unless defined $interval;
my $timeout = $args{timeout};
# Timeout should be 250ms according to RFC1002, but we're going to double
$timeout = 0.5 unless defined $timeout;
my $self = bless { interval => $interval, timeout => $timeout, %args },
$class;
Scalar::Util::weaken( my $wself = $self );
socket my $fh4, AF_INET, Socket::SOCK_DGRAM(), 0
or Carp::croak "Unable to create socket : $!";
AnyEvent::Util::fh_nonblocking $fh4, 1;
$self->{fh4} = $fh4;
$self->{rw4} = AE::io $fh4, 0, sub {
if ( my $peer = recv $fh4, my $resp, 2048, 0 ) {
$wself->_on_read( $resp, $peer );
}
};
# Nbtstat tasks
$self->{_tasks} = {};
return $self;
}
sub interval { @_ > 1 ? $_[0]->{interval} = $_[1] : $_[0]->{interval} }
sub timeout { @_ > 1 ? $_[0]->{timeout} = $_[1] : $_[0]->{timeout} }
sub nbtstat {
my ( $self, $host, $cb ) = @_;
my $ip = inet_aton($host);
my $port = 137;
my $request = {
host => $host,
results => {},
cb => $cb,
destination => scalar sockaddr_in( $port, $ip ),
};
$self->{_tasks}{ $request->{destination} } = $request;
my $delay = $self->interval * scalar keys %{ $self->{_tasks} || {} };
# There's probably a better way to throttle the sends
# but this will work for now since we currently don't support retries
my $w; $w = AE::timer $delay, 0, sub {
undef $w;
$self->_send_request($request);
};
return $self;
}
sub _on_read {
my ( $self, $resp, $peer ) = @_;
($resp) = $resp =~ /^(.*)$/s
if AnyEvent::TAINT && $self->{untaint};
view all matches for this distributionview release on metacpan - search on metacpan
( run in 0.496 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )