| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | ############################################################################ | ||
| 5 | # GuessProtocol | ||
| 6 | # tries to guess protocol of connection and redirect it to the appropriate | ||
| 7 | # protocol specific handler | ||
| 8 | ############################################################################ | ||
| 9 | |||
| 10 | package Net::Inspect::L5::GuessProtocol; | ||
| 11 | use base 'Net::Inspect::Connection'; | ||
| 12 | use fields qw(meta fwd protocols); | ||
| 13 | |||
| 14 | use constant EXPIRE => 300; | ||
| 15 | |||
| 16 | sub new { | ||
| 17 | my ($class,@protocols) = @_; | ||
| 18 | if ( ! ref($class) ) { | ||
| 19 | my $self = $class->SUPER::new( | ||
| 20 | Net::Inspect::Flow::Any->new('guess_protocol')); | ||
| 21 | $self->{upper_flow}->attach($_) for(@protocols); | ||
| 22 | return $self; | ||
| 23 | } else { | ||
| 24 | return $class->SUPER::new(); # just clone | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | sub attach { shift->{upper_flow}->attach(@_) } | ||
| 29 | sub detach { shift->{upper_flow}->detach(@_) } | ||
| 30 | sub attached { shift->{upper_flow}->attached } | ||
| 31 | |||
| 32 | # forward expire to fwd flow | ||
| 33 | sub expire { | ||
| 34 | my ($self,$time) = @_; | ||
| 35 | if ( my $obj = $self->{fwd} ) { | ||
| 36 | return $obj->expire($time); | ||
| 37 | } | ||
| 38 | return $self->SUPER::expire($time); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub syn { 1 } | ||
| 42 | sub new_connection { | ||
| 43 | my ($self,$meta) = @_; | ||
| 44 | my $obj = $self->new; # clone | ||
| 45 | $obj->{meta} = $meta; | ||
| 46 | $obj->{expire} = $meta->{time} + EXPIRE; | ||
| 47 | return $obj; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub in { | ||
| 51 | my ($self,$dir,$data,$eof,$time) = @_; | ||
| 52 | |||
| 53 | if ( my $obj = $self->{fwd} ) { | ||
| 54 | return $obj->in($dir,$data,$eof,$time); | ||
| 55 | } | ||
| 56 | |||
| 57 | # let attached flows guess: | ||
| 58 | # if they return an object they get used | ||
| 59 | # they might detach themself if they are definitly not responsable | ||
| 60 | if ( my ($obj,$n) = $self->{upper_flow}->guess_protocol( | ||
| 61 | $self,$dir,$data,$eof,$time,$self->{meta}) ) { | ||
| 62 | $self->{fwd} = $obj; | ||
| 63 | # might consume not alle from the last data | ||
| 64 | return $n; | ||
| 65 | } | ||
| 66 | |||
| 67 | # guessing objects must keep data for replaying if necessary, so | ||
| 68 | # consider everything consumed | ||
| 69 | return length($data); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub fatal { | ||
| 73 | my ($self,$reason,$dir,$time) = @_; | ||
| 74 | my $obj = $self->{fwd} or return; | ||
| 75 | return $obj->fatal($reason,$dir,$time); | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | 1; | ||
| 80 | |||
| 81 | __END__ |