1 # Dobrica Pavlinusic, <dpavlin@rot13.org> 06/18/07 10:19:50 CEST
7 use base qw/Class::Accessor/;
8 __PACKAGE__->mk_accessors( qw/
19 use Data::Dump qw/dump/;
20 use Carp qw/carp confess cluck croak/;
22 use File::Path qw/mkpath/;
28 #use Devel::LeakTrace::Fast;
32 CWMP::Session - implement logic of CWMP protocol
38 my $server = CWMP::Session->new({
39 sock => $io_socket_object,
49 my $self = $class->SUPER::new( @_ );
51 confess "need sock" unless $self->sock;
52 confess "need store" unless $self->store;
53 my $peerhost = $self->sock->peerhost || confess "can't get sock->peerhost";
55 $self->debug( 0 ) unless $self->debug;
57 warn "created ", __PACKAGE__, "(", dump( @_ ), ") for $peerhost\n" if $self->debug;
59 my $store_obj = CWMP::Store->new({
60 debug => $self->debug,
64 croak "can't open ", dump( $self->store ), ": $!" unless $store_obj;
66 # FIXME looks ugly. Should we have separate accessor for this?
67 $self->store( $store_obj );
69 $self->create_dump( 1 ) if $self->debug > 2;
74 =head2 process_request
76 One request from client/response from server cycle. Call multiple times to
77 facilitate brain-dead concept of adding state to stateless protocol like
80 If used with debugging level of 3 or more, it will also create dumps of
81 requests named C<< dump/nr.request >> where C<nr> is number from 0 to total number
82 of requests in single session.
91 my $sock = $self->sock || die "no sock?";
93 # die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'Net::Server::Proto::TCP' );
95 if ( ! $sock->connected ) {
96 warn "SOCKET NOT CONNECTED\n";
100 bless $sock, 'HTTP::Daemon::ClientConn';
102 # why do I have to do this?
103 # solution from http://use.perl.org/~Matts/journal/12896
104 ${*$sock}{'httpd_daemon'} = HTTP::Daemon->new;
106 my $r = $sock->get_request;
109 warn "WARNING: can't get_request\n";
113 my $ip = $sock->peerhost || confess "can't get peerhost from sock: $!";
115 my $xml = $r->content;
117 my $size = length( $xml );
119 warn "<<<< $ip [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n";
121 my $dump_nr = $dump_by_ip->{$ip}++;
122 my $file = sprintf("./dump/%s/%04d.request", $ip, $dump_nr);
124 if ( $self->create_dump ) {
125 mkpath "dump/$ip" unless -e "dump/$ip";
126 write_file( $file, $r->as_string );
127 warn "### request dumped to file: $file\n" if $self->debug;
134 die "no SOAPAction header in ",dump($xml) unless defined ( $r->header('SOAPAction') );
136 warn "## request payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
138 $state = CWMP::Request->parse( $xml );
140 if ( defined( $state->{_trigger} ) && $self->create_dump ) {
141 my $type = sprintf("dump/%s/%04d-%s", $ip, $dump_nr, $state->{_trigger});
142 $file =~ s!^.*?([^/]+)$!$1!; #!vim
143 symlink $file, $type || warn "can't symlink $file -> $type: $!";
146 warn "## acquired state = ", dump( $state ), "\n" if $self->debug;
148 if ( ! defined( $state->{DeviceID} ) ) {
149 if ( $self->state ) {
150 warn "## state without DeviceID, using old one...\n";
151 $state->{DeviceID} = $self->state->{DeviceID};
153 warn "WARNING: state without DeviceID, and I don't have old one!\n";
154 warn "## state = ",dump( $state );
158 $self->state( $state );
159 $self->store->update_state( $state );
163 warn "## empty request, using last request state\n";
165 $state = $self->state;
166 delete( $state->{_dispatch} );
167 #warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
170 $sock->send(join("\r\n",
172 'Content-Type: text/xml; charset="utf-8"',
173 'Server: PerlCWMP/42',
174 'SOAPServer: PerlCWMP/42'
177 $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} );
179 my $uid = $self->store->state_to_uid( $state );
181 my $to_uid = join(" ", grep { defined($_) } "to $uid",
183 $state->{Parameter}->{'InternetGatewayDevice.DeviceInfo.HardwareVersion'},
185 $state->{Parameter}->{'InternetGatewayDevice.DeviceInfo.SoftwareVersion'},
187 # $state->{Parameter}->{'InternetGatewayDevice.DeviceSummary'},
190 my $queue = CWMP::Queue->new({
192 debug => $self->debug,
197 if ( my $dispatch = $state->{_dispatch} ) {
198 $xml = $self->dispatch( $dispatch );
199 } elsif ( $job = $queue->dequeue ) {
200 $xml = $self->dispatch( $job->dispatch );
201 } elsif ( $size == 0 ) {
202 warn ">>> over, closing connection $to_uid";
206 warn ">>> empty response $to_uid";
207 $state->{NoMoreRequests} = 1;
208 $xml = $self->dispatch( 'xml', sub {} );
211 $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );
212 $sock->send( $xml ) or die "can't send response";
214 warn ">>>> " . $ip . " [" . localtime() . "] sent ", length( $xml )," bytes $to_uid";
216 $job->finish if $job;
217 warn "### request over for $uid\n" if $self->debug;
219 return 1; # next request
224 $xml = $self->dispatch('Inform', $response_arguments );
226 If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >>
233 my $dispatch = shift || die "no dispatch?";
236 my $response = CWMP::Methods->new({ debug => $self->debug });
238 if ( $response->can( $dispatch ) ) {
239 warn ">>> dispatching to $dispatch with args ",dump( $args ),"\n";
240 my $xml = $response->$dispatch( $self->state, $args );
241 warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
242 if ( $self->create_dump ) {
243 my $ip = $self->sock->peerhost || confess "can't get sock->peerhost: $!";
244 my $dump_nr = $dump_by_ip->{$ip}++;
245 my $file = sprintf("dump/%s/%04d.response", $ip, $dump_nr );
246 write_file( $file, $xml );
247 warn "### response dump: $file\n" if $self->debug;
251 confess "can't dispatch to $dispatch";
258 return $self->error( 501, 'System error' );
263 my ($self, $number, $msg) = @_;
265 $self->sock->send( "HTTP/1.1 $number $msg\r\n" );
266 warn "Error - $number - $msg\n";
267 return 0; # close connection