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/;
27 #use Devel::LeakTrace::Fast;
31 CWMP::Session - implement logic of CWMP protocol
37 my $server = CWMP::Session->new({
38 sock => $io_socket_object,
48 my $self = $class->SUPER::new( @_ );
50 confess "need sock" unless $self->sock;
51 confess "need store" unless $self->store;
52 my $peerhost = $self->sock->peerhost || confess "can't get sock->peerhost";
54 $self->debug( 0 ) unless $self->debug;
56 warn "created ", __PACKAGE__, "(", dump( @_ ), ") for $peerhost\n" if $self->debug;
58 my $store_obj = CWMP::Store->new({
59 debug => $self->debug,
63 croak "can't open ", dump( $self->store ), ": $!" unless $store_obj;
65 # FIXME looks ugly. Should we have separate accessor for this?
66 $self->store( $store_obj );
68 $self->create_dump( 1 ) if $self->debug > 2;
73 =head2 process_request
75 One request from client/response from server cycle. Call multiple times to
76 facilitate brain-dead concept of adding state to stateless protocol like
79 If used with debugging level of 3 or more, it will also create dumps of
80 requests named C<< dump/nr.request >> where C<nr> is number from 0 to total number
81 of requests in single session.
90 my $sock = $self->sock || die "no sock?";
92 # die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'Net::Server::Proto::TCP' );
94 if ( ! $sock->connected ) {
95 warn "SOCKET NOT CONNECTED\n";
99 bless $sock, 'HTTP::Daemon::ClientConn';
101 # why do I have to do this?
102 # solution from http://use.perl.org/~Matts/journal/12896
103 ${*$sock}{'httpd_daemon'} = HTTP::Daemon->new;
105 my $r = $sock->get_request;
108 carp "can't get_request";
112 my $xml = $r->content;
114 my $size = length( $xml );
116 warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n";
119 my $file = sprintf("dump/%04d-%s.request", $dump_nr, $sock->peerhost);
121 if ( $self->create_dump ) {
122 write_file( $file, $r->as_string );
123 warn "### request dumped to file: $file\n" if $self->debug;
130 die "no SOAPAction header in ",dump($xml) unless defined ( $r->header('SOAPAction') );
132 warn "## request payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
134 $state = CWMP::Request->parse( $xml );
136 if ( defined( $state->{_dispatch} ) && $self->create_dump ) {
137 my $type = sprintf("dump/%04d-%s-%s", $dump_nr, $sock->peerhost, $state->{_dispatch});
138 symlink $file, $type || warn "can't symlink $file -> $type: $!";
141 warn "## acquired state = ", dump( $state ), "\n" if $self->debug;
143 if ( ! defined( $state->{DeviceID} ) ) {
144 warn "## state with DeviceID, using old one...\n";
145 $state->{DeviceID} = $self->state->{DeviceID};
148 $self->state( $state );
149 $self->store->update_state( $state );
153 warn "## empty request, using last request state\n";
155 $state = $self->state;
156 delete( $state->{_dispatch} );
157 #warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
160 $sock->send(join("\r\n",
162 'Content-Type: text/xml; charset="utf-8"',
163 'Server: PerlCWMP/42',
164 'SOAPServer: PerlCWMP/42'
167 $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} );
169 my $uid = $self->store->state_to_uid( $state );
171 my $to_uid = join(" ", grep { defined($_) } "to $uid",
173 $state->{Parameter}->{'InternetGatewayDevice.DeviceInfo.HardwareVersion'},
175 $state->{Parameter}->{'InternetGatewayDevice.DeviceInfo.SoftwareVersion'},
177 # $state->{Parameter}->{'InternetGatewayDevice.DeviceSummary'},
180 my $queue = CWMP::Queue->new({
182 debug => $self->debug,
187 if ( my $dispatch = $state->{_dispatch} ) {
188 $xml = $self->dispatch( $dispatch );
189 } elsif ( $job = $queue->dequeue ) {
190 $xml = $self->dispatch( $job->dispatch );
191 } elsif ( $size == 0 ) {
192 warn ">>> over, closing connection $to_uid";
196 warn ">>> empty response $to_uid";
197 $state->{NoMoreRequests} = 1;
198 $xml = $self->dispatch( 'xml', sub {} );
201 $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );
202 $sock->send( $xml ) or die "can't send response";
204 warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes $to_uid";
206 $job->finish if $job;
207 warn "### request over for $uid\n" if $self->debug;
209 return 1; # next request
214 $xml = $self->dispatch('Inform', $response_arguments );
216 If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >>
223 my $dispatch = shift || die "no dispatch?";
226 my $response = CWMP::Methods->new({ debug => $self->debug });
228 if ( $response->can( $dispatch ) ) {
229 warn ">>> dispatching to $dispatch with args ",dump( $args ),"\n";
230 my $xml = $response->$dispatch( $self->state, $args );
231 warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
232 if ( $self->create_dump ) {
233 my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost);
234 write_file( $file, $xml );
235 warn "### response dump: $file\n" if $self->debug;
239 confess "can't dispatch to $dispatch";
246 return $self->error( 501, 'System error' );
251 my ($self, $number, $msg) = @_;
253 $self->sock->send( "HTTP/1.1 $number $msg\r\n" );
254 warn "Error - $number - $msg\n";
255 return 0; # close connection