lib/Net/SinFP3/Input/ArpDiscover.pm untested

filter: Statementstmt 0.0% 0 / 110 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 169 CC 11 SCAR 35.7
CC
11
Cov
0%
_arpDiscover
90.0
new
6.0
init
6.0
CRAP 35.7
1 #
2 # $Id: ArpDiscover.pm,v 008243d3e89a 2018/07/21 14:54:07 gomor $
3 #
4 package Net::SinFP3::Input::ArpDiscover;
5 use strict;
6 use warnings;
7  
8 use base qw(Net::SinFP3::Input);
9 __PACKAGE__->cgBuildIndices;
10  
11 use Net::SinFP3::Next::IpPort;
12  
13 use Net::Libdnet::Arp;
14 use Net::Frame::Layer::ETH qw(:consts);
15 use Net::Frame::Layer::ARP qw(:consts);
16  
17 sub give {
18 return [
19 'Net::SinFP3::Next::IpPort',
20 ];
21 }
22  
23 sub new {
24 my $self = shift->SUPER::new(
25 @_,
26 );
27  
28 my $global = $self->global;
29 my $log = $global->log;
30  
31 if (! defined($global->port)) {
32 $log->fatal("You must provide a `port' attribute in Global object");
33 }
34  
35 return $self;
36 }
37  
38 sub _getArpPkt {
39 my $self = shift;
40 my ($ip) = @_;
41  
42 my $global = $self->global;
43  
44 my $eth = Net::Frame::Layer::ETH->new(
45 type => NF_ETH_TYPE_ARP,
46 src => $global->mac,
47 );
48 my $arp = Net::Frame::Layer::ARP->new(
49 opCode => NF_ARP_OPCODE_REQUEST,
50 srcIp => $global->ip,
51 dstIp => $ip,
52 src => $global->mac,
53 );
54 my $request = Net::Frame::Simple->new(
55 layers => [ $eth, $arp ],
56 );
57 return $request;
58 }
59  
60 sub _arpDiscover {
61 my $self = shift;
62  
63 my $global = $self->global;
64 my $log = $global->log;
65  
66 my $cacheArp = $global->cacheArp;
67  
68 # We will also look in ARP cache table
69 my $arp = Net::Libdnet::Arp->new;
70  
71 my @list = ();
72 my %reply = ();
73 my $ipList = ();
74 # User want to overwrite default scanning option (local subnet)
75 if ($global->target) {
76 $ipList = $global->expandSubnet(subnet => $global->target);
77 }
78 else {
79 $ipList = $global->expandSubnet(subnet => $global->subnet);
80 }
81 for my $ip (@$ipList) {
82 # We scan ARP for everyone but our own IP
83 next if $ip eq $global->ip;
84  
85 # XXX: move to Global so there is one place for ARP cache handling
86 my $mac;
87 if (defined($cacheArp->{$ip})) {
88 $mac = $cacheArp->{$ip};
89 $reply{$ip} = $mac;
90 }
91 elsif ($mac = $arp->get($ip)) {
92 $log->verbose("Found $mac for $ip in ARP cache");
93 $cacheArp->{$ip} = $mac;
94 $reply{$ip} = $mac;
95 }
96 else {
97 # If it is not in ARP cache yet
98 push @list, $self->_getArpPkt($ip);
99 }
100 }
101  
102 my $oWrite = $global->getWriteL2 or return;
103 $oWrite->open or return;
104  
105 my $oDump = $global->getDumpOnline(
106 filter => 'arp',
107 ) or return;
108 $oDump->start or return;
109  
110 for my $t (1..3) {
111 for my $r (@list) {
112 my $dstIp = $r->ref->{ARP}->dstIp;
113 $oWrite->send($r->raw) unless exists $reply{$dstIp};
114 }
115 until ($oDump->timeout) {
116 if (my $h = $oDump->next) {
117 my $r = Net::Frame::Simple->newFromDump($h);
118 next unless $r->ref->{ARP}->opCode eq NF_ARP_OPCODE_REPLY;
119 my $srcIp = $r->ref->{ARP}->srcIp;
120 unless (exists $reply{$srcIp}) {
121 my $mac = $r->ref->{ARP}->src;
122 $log->info("Received $mac for $srcIp");
123 $reply{$srcIp} = $r->ref->{ARP}->src;
124  
125 # Put it in ARP cache table
126 $cacheArp->{$srcIp} = $mac;
127 }
128 }
129 }
130 $oDump->timeoutReset;
131 }
132  
133 $oWrite->close;
134 $oDump->stop;
135  
136 for (keys %reply) {
137 $log->verbose(sprintf("%-16s => %s", $_, $reply{$_}));
138 }
139  
140 return \%reply;
141 }
142  
143 # http://tools.ietf.org/html/rfc2373
144 sub _mac2eui64 {
145 my $self = shift;
146 my ($mac) = @_;
147  
148 my @b = split(':', $mac);
149 my $b0 = hex($b[0]) ^ 2;
150  
151 return sprintf("fe80::%x%x:%xff:fe%x:%x%x", $b0, hex($b[1]), hex($b[2]),
152 hex($b[3]), hex($b[4]), hex($b[5]));
153 }
154  
155 sub init {
156 my $self = shift->SUPER::init(@_) or return;
157  
158 my $global = $self->global;
159 my $log = $global->log;
160  
161 my $ipMacList = $self->_arpDiscover;
162 my $portList = $global->portList;
163  
164 my @nextList = ();
165 for my $ip (keys %$ipMacList) {
166 my $mac = $ipMacList->{$ip};
167 if ($global->ipv6) {
168 $log->debug("Converting MAC [$mac] to IPv6 with EUI64");
169 $ip = $self->_mac2eui64($mac);
170 }
171 for my $port (@$portList) {
172 push @nextList, Net::SinFP3::Next::IpPort->new(
173 global => $self->global,
174 ip => $ip,
175 port => $port,
176 mac => $mac,
177 );
178 }
179 }
180 $self->nextList(\@nextList);
181  
182 return 1;
183 }
184  
185 sub run {
186 my $self = shift->SUPER::run(@_) or return;
187  
188 my @nextList = $self->nextList;
189 my $next = shift @nextList;
190 $self->nextList(\@nextList);
191  
192 return $next;
193 }
194  
195 1;
196  
197 __END__