X-Git-Url: http://git.rot13.org/?p=perl-cwmp.git;a=blobdiff_plain;f=lib%2FCWMP%2FSession.pm;h=4ce8a07315c1061d9571b93fa9b9ad3e96d165b3;hp=52140c32c43afcafbcf86341c222d0d1a787d826;hb=c132d8c37368aad6f9ff64ec873f3648407044df;hpb=68d767eafbbf968a504e212120029d71e7be986a diff --git a/lib/CWMP/Session.pm b/lib/CWMP/Session.pm index 52140c3..4ce8a07 100644 --- a/lib/CWMP/Session.pm +++ b/lib/CWMP/Session.pm @@ -15,13 +15,10 @@ state store / ); -use HTTP::Daemon; use Data::Dump qw/dump/; use Carp qw/carp confess cluck croak/; -use File::Slurp; -use File::Path qw/mkpath/; -use CWMP::Request; +use CWMP::Parser; use CWMP::Methods; use CWMP::Store; @@ -48,14 +45,10 @@ sub new { my $class = shift; my $self = $class->SUPER::new( @_ ); - confess "need sock" unless $self->sock; confess "need store" unless $self->store; - my $peerhost = $self->sock->peerhost || confess "can't get sock->peerhost"; $self->debug( 0 ) unless $self->debug; - warn "created ", __PACKAGE__, "(", dump( @_ ), ") for $peerhost\n" if $self->debug; - my $store_obj = CWMP::Store->new({ debug => $self->debug, %{ $self->store }, @@ -71,6 +64,37 @@ sub new { return $self; } +my $vendor_data = { + 'InternetGatewayDevice.ManagementServer.PeriodicInformEnable' => 1, + 'InternetGatewayDevice.ManagementServer.PeriodicInformInterval' => 17, + 'InternetGatewayDevice.DeviceInfo.ProvisioningCode' => 'test provision', +}; + +our $set_tried; + +sub vendor_hook { + my ( $self, $uid, $stored, $queue ) = @_; + warn "# vendor_hook $uid ",dump($stored) if $self->debug > 2; + + my @refresh; + + foreach my $n ( keys %$vendor_data ) { + if ( defined $stored->{$n} && $vendor_data->{$n} ne $stored->{$n} ) { + next if $set_tried->{$uid}->{$n}++; + push @refresh, $n; + $queue->enqueue( 'SetParameterValues', { $n => $vendor_data->{$n} } ); + } + } + + if ( @refresh ) { + $queue->enqueue( 'GetParameterValues', [ @refresh ] ); + warn "vendor_hook $uid SetParameterValues ", dump( @refresh ); + return $self->dispatch( 'GetParameterValues', [ @refresh ] ); + } + + return; +} + =head2 process_request One request from client/response from server cycle. Call multiple times to @@ -83,74 +107,28 @@ of requests in single session. =cut -my $dump_by_ip; sub process_request { - my $self = shift; - - my $sock = $self->sock || die "no sock?"; - -# die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'Net::Server::Proto::TCP' ); - - if ( ! $sock->connected ) { - warn "SOCKET NOT CONNECTED\n"; - return 0; - } - - bless $sock, 'HTTP::Daemon::ClientConn'; - - # why do I have to do this? - # solution from http://use.perl.org/~Matts/journal/12896 - ${*$sock}{'httpd_daemon'} = HTTP::Daemon->new; - - my $r = $sock->get_request; - - if ( ! $r ) { - warn "WARNING: can't get_request\n"; - return 0; - } - - my $ip = $sock->peerhost || confess "can't get peerhost from sock: $!"; - - my $xml = $r->content; + my ( $self, $ip, $xml ) = @_; my $size = length( $xml ); - warn "<<<< $ip [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n"; - - my $dump_nr = $dump_by_ip->{$ip}++; - my $file = sprintf("./dump/%s/%04d.request", $ip, $dump_nr); - - if ( $self->create_dump ) { - mkpath "dump/$ip" unless -e "dump/$ip"; - write_file( $file, $r->as_string ); - warn "### request dumped to file: $file\n" if $self->debug; - } - my $state; if ( $size > 0 ) { - die "no SOAPAction header in ",dump($xml) unless defined ( $r->header('SOAPAction') ); - warn "## request payload: ",length($xml)," bytes\n$xml\n" if $self->debug; - $state = CWMP::Request->parse( $xml ); - - if ( defined( $state->{_trigger} ) && $self->create_dump ) { - my $type = sprintf("dump/%s/%04d-%s", $ip, $dump_nr, $state->{_trigger}); - $file =~ s!^.*?([^/]+)$!$1!; #!vim - symlink $file, $type || warn "can't symlink $file -> $type: $!"; - } + $state = CWMP::Parser->parse( $xml ); warn "## acquired state = ", dump( $state ), "\n" if $self->debug; - if ( ! defined( $state->{DeviceID} ) ) { + if ( ! defined( $state->{DeviceId} ) ) { if ( $self->state ) { - warn "## state without DeviceID, using old one...\n"; - $state->{DeviceID} = $self->state->{DeviceID}; + warn "## state without DeviceId, using old one...\n"; + $state->{DeviceId} = $self->state->{DeviceId}; } else { - warn "WARNING: state without DeviceID, and I don't have old one!\n"; + warn "WARNING: state without DeviceId, and I don't have old one!\n"; warn "## state = ",dump( $state ); } } @@ -167,15 +145,6 @@ sub process_request { #warn "last request state = ", dump( $state ), "\n" if $self->debug > 1; } - $sock->send(join("\r\n", - 'HTTP/1.1 200 OK', - 'Content-Type: text/xml; charset="utf-8"', - 'Server: PerlCWMP/42', - 'SOAPServer: PerlCWMP/42' - )."\r\n"); - - $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} ); - my $uid = $self->store->state_to_uid( $state ); my $to_uid = join(" ", grep { defined($_) } "to $uid", @@ -191,32 +160,80 @@ sub process_request { id => $uid, debug => $self->debug, }); - my $job; $xml = ''; if ( my $dispatch = $state->{_dispatch} ) { $xml = $self->dispatch( $dispatch ); - } elsif ( $job = $queue->dequeue ) { + } elsif ( my $job = $queue->dequeue ) { $xml = $self->dispatch( $job->dispatch ); - } elsif ( $size == 0 ) { - warn ">>> over, closing connection $to_uid"; - $sock->close; - return; + $job->finish; } else { - warn ">>> empty response $to_uid"; - $state->{NoMoreRequests} = 1; - $xml = $self->dispatch( 'xml', sub {} ); + my $stored = $self->store->get_state( $uid ); + if ( ! defined $stored->{ParameterInfo} ) { + $xml = $self->dispatch( 'GetParameterNames', [ 'InternetGatewayDevice.', 1 ] ); + } else { + my @params = + grep { m/\.$/ && ! m/\.\d+\.$/ } + keys %{ $stored->{ParameterInfo} } + ; + if ( @params ) { + warn "# GetParameterNames ", dump( @params ); + my $first = shift @params; + delete $stored->{ParameterInfo}->{$first}; + $xml = $self->dispatch( 'GetParameterNames', [ $first, 1 ] ); + foreach ( @params ) { + $queue->enqueue( 'GetParameterNames', [ $_, 1 ] ); + delete $stored->{ParameterInfo}->{ $_ }; + } + $self->store->set_state( $uid, $stored ); + } else { + + my @params = sort + grep { ! exists $stored->{Parameter}->{$_} } + grep { ! m/\.$/ && ! m/NumberOfEntries/ } + keys %{ $stored->{ParameterInfo} } + ; + if ( @params ) { + warn "# GetParameterValues ", dump( @params ); + my $first = shift @params; + $xml = $self->dispatch( 'GetParameterValues', [ $first ] ); + while ( @params ) { + my @chunk = splice @params, 0, 16; # FIXME 16 seems to be max + $queue->enqueue( 'GetParameterValues', [ @chunk ] ); + } + + } elsif ( $xml = $self->vendor_hook( $uid, $stored, $queue ) ) { + + warn "vendor_hook triggered\n"; + + } else { + + warn ">>> empty response $to_uid"; + $state->{NoMoreRequests} = 1; + $xml = ''; + + } + } + } } - $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); - $sock->send( $xml ) or die "can't send response"; + my $status = length($xml) ? 200 : 204; + + my $out = join("\r\n", + "HTTP/1.1 $status OK", + 'Content-Type: text/xml; charset="utf-8"', + 'Server: Perl-CWMP/42', + 'SOAPServer: Perl-CWMP/42' + ) . "\r\n"; - warn ">>>> " . $ip . " [" . localtime() . "] sent ", length( $xml )," bytes $to_uid"; + $out .= "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" if $state->{ID}; + + $out .= "Content-Length: " . length( $xml ) . "\r\n\r\n"; + $out .= $xml if length($xml); - $job->finish if $job; warn "### request over for $uid\n" if $self->debug; - return 1; # next request + return $out; # next request }; =head2 dispatch @@ -239,13 +256,6 @@ sub dispatch { warn ">>> dispatching to $dispatch with args ",dump( $args ),"\n"; my $xml = $response->$dispatch( $self->state, $args ); warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; - if ( $self->create_dump ) { - my $ip = $self->sock->peerhost || confess "can't get sock->peerhost: $!"; - my $dump_nr = $dump_by_ip->{$ip}++; - my $file = sprintf("dump/%s/%04d.response", $ip, $dump_nr ); - write_file( $file, $xml ); - warn "### response dump: $file\n" if $self->debug; - } return $xml; } else { confess "can't dispatch to $dispatch";