File: ArpWatch.pm

package info (click to toggle)
libpoe-component-pcap-perl 0.04-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 120 kB
  • sloc: perl: 396; makefile: 3
file content (172 lines) | stat: -rw-r--r-- 4,125 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/perl
package POE::Component::ArpWatch;

use strict;

use POE;

use Pcap;

use NetPacket::Ethernet qw( :types );
use NetPacket::ARP qw( :opcodes );

## Map arp opcode #'s to strings
my %arp_opcodes = (
		   NetPacket::ARP::ARP_OPCODE_REQUEST, 'ARP Request',
		   NetPacket::ARP::ARP_OPCODE_REPLY, 'ARP Reply',
		   NetPacket::ARP::RARP_OPCODE_REQUEST, 'RARP Request',
		   NetPacket::ARP::RARP_OPCODE_REPLY, 'RARP Reply',
		  );

##
## POE::Component::ArpWatch->spawn(
##				   [ Alias => 'arp_watch' ],
##				   [ Device => 'eth0' ],
##				   [ Dispatch => dispatch_state ],
##				   [ Session => dispatch_session ],
##				  )
##
sub spawn {
  my $class = shift;
  my %args = @_;

  $args{ Alias } ||= 'arp_watch';

  POE::Session->create( 
		       inline_states => {
					 _start => \&_start,
#					 _signal => \&_signal,
					 _stop => \&_stop,,
					 _dispatch => \&_dispatch,
					 set_dispatch => \&set_dispatch,
					 run => \&run,
					 pause => \&pause,
					 shutdown => \&shutdown,
					},
		       args => [
				$args{ Alias },   # ARG0
				$args{ Device },  # ARG1
				$args{ Dispatch },# ARG2
				$args{ Session }, # ARG3
			       ],
		      );

  return $args{ Alias };
}

sub _start {
  my ($kernel, $heap, $session, 
      $alias, $device, $target_state, $target_session ) 
    = @_[ KERNEL, HEAP, SESSION, ARG0..ARG3 ];

  POE::Component::Pcap->spawn( 
			      Alias => $alias . '_pcap',
			      Device => $device, 
			      Filter => 'arp',
			      Dispatch => '_dispatch',
			      Session => $session,
			     );

  $heap->{'pcap_session'} = $kernel->alias_resolve( $alias . '_pcap' );

  ## Set alias for ourselves and remember it
  $kernel->alias_set( $alias );
  $heap->{Alias} = $alias;

  ## Set dispatch target session and state if it was given
  if( defined( $target_session ) ) {
    $heap->{'target_session'} = $target_session;
    $heap->{'target_state'} = $target_state;
  }
}

sub set_dispatch {
  my( $heap, $sender, $target ) = @_[ HEAP, SENDER, ARG0 ];

  if( defined $target ) {
    ## Remember whome to forward results to
    $heap->{'target_session'} = $sender;
    $heap->{'target_state'} = $target;
  } else {
    ## Clear target
    delete $heap->{'target_session'};
    delete $heap->{'target_state'};
  }
}

sub run {
  $_[KERNEL]->post( $_[HEAP]->{'pcap_session'} => 'run' );
}

sub pause {
  $_[KERNEL]->post( $_[HEAP]->{'pcap_session'} => 'pause' );
}

sub _dispatch {
  my( $kernel, $heap, $packets ) =
    @_[ KERNEL, HEAP, ARG0 ];

  if( exists $heap->{'target_session'} ) {
    $kernel->post( $heap->{'target_session'}, 
		   $heap->{'target_state'}, 
		   _process_packet( @{ $_ } ) ) foreach( @{$packets} );
  }
}

sub _signal {
  # print "Got signal ", $_[ARG0], "\n";

  $_[KERNEL]->post( pcap => 'shutdown' );

  return 1
}

sub shutdown {
  my ( $kernel, $heap, $session, $sender ) 
    = @_[ KERNEL, HEAP, SESSION, SENDER ];
  my $alias = $heap->{Alias};

#  print "In shutdown for sid ", $session->ID, ", alias $alias\n"; 
#  print "shutdown by ", $sender->ID, "\n";

  $kernel->post( $heap->{'pcap_session'} => 'shutdown' );

  $kernel->alias_remove( $alias );

#  print "Out shutdown for sid ", $session->ID, ", alias $alias\n"; 
}

sub _stop {
  my ( $kernel, $heap, $session ) = @_[ KERNEL, HEAP, SESSION ];
  my $alias = $heap->{Alias};

#  print "In state_stop for sid ", $session->ID, ", alias $alias\n"; 

#  print "Out state_stop for sid ", $session->ID, ", alias $alias\n"; 
}

sub _process_packet {
  my( $hdr, $pkt ) = @_;

  my $arp = 
    NetPacket::ARP->decode( NetPacket::Ethernet->decode($pkt)->{data} );

  ## Return hashref with apropriate fields
  return { 
	  type => $arp_opcodes{ $arp->{opcode} },
	  tv_sec => $hdr->{tv_sec},
	  tv_usec => $hdr->{tv_usec},
	  source_haddr => _phys( $arp->{sha} ), 
	  source_ipaddr => _ipaddr( $arp->{spa} ),
	  target_haddr => _phys( $arp->{tha} ),
	  target_ipaddr => _ipaddr( $arp->{tpa} ),
	 }
}

## Pretty printing subs for addresses
sub _ipaddr { join( ".", unpack( "C4", pack( "N", oct( "0x". shift ) ) ) ) }
sub _phys { join( ":", grep {length} split( /(..)/, shift ) ) }

1;

__END__