7 |
use base qw/Class::Accessor/; |
use base qw/Class::Accessor/; |
8 |
__PACKAGE__->mk_accessors( qw/ |
__PACKAGE__->mk_accessors( qw/ |
9 |
debug |
debug |
10 |
store |
create_dump |
11 |
|
session |
12 |
|
|
13 |
sock |
sock |
14 |
state |
state |
17 |
|
|
18 |
use HTTP::Daemon; |
use HTTP::Daemon; |
19 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
20 |
use Carp qw/confess cluck croak/; |
use Carp qw/carp confess cluck croak/; |
21 |
use File::Slurp; |
use File::Slurp; |
22 |
|
|
23 |
use CWMP::Request; |
use CWMP::Request; |
24 |
use CWMP::Methods; |
use CWMP::Methods; |
25 |
use CWMP::Store; |
use CWMP::Store; |
26 |
|
|
27 |
|
#use Devel::LeakTrace::Fast; |
28 |
|
|
29 |
=head1 NAME |
=head1 NAME |
30 |
|
|
31 |
CWMP::Session - implement logic of CWMP protocol |
CWMP::Session - implement logic of CWMP protocol |
36 |
|
|
37 |
my $server = CWMP::Session->new({ |
my $server = CWMP::Session->new({ |
38 |
sock => $io_socket_object, |
sock => $io_socket_object, |
39 |
store => 'state.db', |
store => { ... }, |
40 |
debug => 1, |
debug => 1, |
41 |
|
create_dump => 1, |
42 |
}); |
}); |
43 |
|
|
44 |
=cut |
=cut |
48 |
my $self = $class->SUPER::new( @_ ); |
my $self = $class->SUPER::new( @_ ); |
49 |
|
|
50 |
confess "need sock" unless $self->sock; |
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"; |
53 |
|
|
54 |
$self->debug( 0 ) unless $self->debug; |
$self->debug( 0 ) unless $self->debug; |
55 |
|
|
56 |
warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug; |
warn "created ", __PACKAGE__, "(", dump( @_ ), ") for $peerhost\n" if $self->debug; |
57 |
|
|
58 |
my $store_obj = CWMP::Store->new({ |
my $store_obj = CWMP::Store->new({ |
59 |
debug => $self->debug, |
debug => $self->debug, |
65 |
# FIXME looks ugly. Should we have separate accessor for this? |
# FIXME looks ugly. Should we have separate accessor for this? |
66 |
$self->store( $store_obj ); |
$self->store( $store_obj ); |
67 |
|
|
68 |
|
$self->create_dump( 1 ) if $self->debug > 2; |
69 |
|
|
70 |
return $self; |
return $self; |
71 |
} |
} |
72 |
|
|
102 |
# solution from http://use.perl.org/~Matts/journal/12896 |
# solution from http://use.perl.org/~Matts/journal/12896 |
103 |
${*$sock}{'httpd_daemon'} = HTTP::Daemon->new; |
${*$sock}{'httpd_daemon'} = HTTP::Daemon->new; |
104 |
|
|
105 |
my $r = $sock->get_request || confess "can't get_request"; |
my $r = $sock->get_request; |
106 |
|
|
107 |
|
if ( ! $r ) { |
108 |
|
carp "can't get_request"; |
109 |
|
return 0; |
110 |
|
} |
111 |
|
|
112 |
my $xml = $r->content; |
my $xml = $r->content; |
113 |
|
|
118 |
$dump_nr++; |
$dump_nr++; |
119 |
my $file = sprintf("dump/%04d-%s.request", $dump_nr, $sock->peerhost); |
my $file = sprintf("dump/%04d-%s.request", $dump_nr, $sock->peerhost); |
120 |
|
|
121 |
if ( $self->debug > 2 ) { |
if ( $self->create_dump ) { |
122 |
write_file( $file, $r->as_string ); |
write_file( $file, $r->as_string ); |
123 |
warn "### request dumped to file: $file\n"; |
warn "### request dumped to file: $file\n" if $self->debug; |
124 |
} |
} |
125 |
|
|
126 |
my $state; |
my $state; |
133 |
|
|
134 |
$state = CWMP::Request->parse( $xml ); |
$state = CWMP::Request->parse( $xml ); |
135 |
|
|
136 |
if ( defined( $state->{_dispatch} ) && $self->debug > 2 ) { |
if ( defined( $state->{_dispatch} ) && $self->create_dump ) { |
137 |
my $type = sprintf("dump/%04d-%s-%s", $dump_nr, $sock->peerhost, $state->{_dispatch}); |
my $type = sprintf("dump/%04d-%s-%s", $dump_nr, $sock->peerhost, $state->{_dispatch}); |
138 |
symlink $file, $type || warn "can't symlink $file -> $type: $!"; |
symlink $file, $type || warn "can't symlink $file -> $type: $!"; |
139 |
} |
} |
140 |
|
|
141 |
warn "## acquired state = ", dump( $state ), "\n"; |
warn "## acquired state = ", dump( $state ), "\n" if $self->debug; |
142 |
|
|
143 |
|
if ( ! defined( $state->{DeviceID} ) ) { |
144 |
|
warn "## state with DeviceID, using old one...\n"; |
145 |
|
$state->{DeviceID} = $self->state->{DeviceID}; |
146 |
|
} |
147 |
|
|
148 |
$self->state( $state ); |
$self->state( $state ); |
149 |
$self->store->update_state( ID => $state->{ID}, $state ); |
$self->store->update_state( $state ); |
150 |
|
|
151 |
} else { |
} else { |
152 |
|
|
157 |
#warn "last request state = ", dump( $state ), "\n" if $self->debug > 1; |
#warn "last request state = ", dump( $state ), "\n" if $self->debug > 1; |
158 |
} |
} |
159 |
|
|
|
|
|
160 |
$sock->send(join("\r\n", |
$sock->send(join("\r\n", |
161 |
'HTTP/1.1 200 OK', |
'HTTP/1.1 200 OK', |
162 |
'Content-Type: text/xml; charset="utf-8"', |
'Content-Type: text/xml; charset="utf-8"', |
163 |
'Server: AcmeCWMP/42', |
'Server: PerlCWMP/42', |
164 |
'SOAPServer: AcmeCWMP/42' |
'SOAPServer: PerlCWMP/42' |
165 |
)."\r\n"); |
)."\r\n"); |
166 |
|
|
167 |
$sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} ); |
$sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} ); |
168 |
|
|
169 |
my $uid = $self->store->ID_to_uid( $state->{ID}, $state ); |
my $uid = $self->store->state_to_uid( $state ); |
170 |
|
|
171 |
|
my $to_uid = join(" ", grep { defined($_) } "to $uid", |
172 |
|
# board |
173 |
|
$state->{Parameter}->{'InternetGatewayDevice.DeviceInfo.HardwareVersion'}, |
174 |
|
# version |
175 |
|
$state->{Parameter}->{'InternetGatewayDevice.DeviceInfo.SoftwareVersion'}, |
176 |
|
# summary |
177 |
|
# $state->{Parameter}->{'InternetGatewayDevice.DeviceSummary'}, |
178 |
|
) . "\n"; |
179 |
|
|
180 |
my $queue = CWMP::Queue->new({ |
my $queue = CWMP::Queue->new({ |
181 |
id => $uid, |
id => $uid, |
189 |
} elsif ( $job = $queue->dequeue ) { |
} elsif ( $job = $queue->dequeue ) { |
190 |
$xml = $self->dispatch( $job->dispatch ); |
$xml = $self->dispatch( $job->dispatch ); |
191 |
} elsif ( $size == 0 ) { |
} elsif ( $size == 0 ) { |
192 |
warn ">>> no more queued commands, closing connection to $uid\n"; |
warn ">>> no more queued commands, no client pending, closing connection $to_uid"; |
193 |
return 0; |
$sock->close; |
194 |
|
return; |
195 |
} else { |
} else { |
196 |
warn ">>> empty response to $uid\n"; |
warn ">>> empty response $to_uid"; |
197 |
$state->{NoMoreRequests} = 1; |
$state->{NoMoreRequests} = 1; |
198 |
$xml = $self->dispatch( 'xml', sub {} ); |
$xml = $self->dispatch( 'xml', sub {} ); |
199 |
} |
} |
201 |
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
202 |
$sock->send( $xml ) or die "can't send response"; |
$sock->send( $xml ) or die "can't send response"; |
203 |
|
|
204 |
warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes to $uid\n"; |
warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes $to_uid"; |
205 |
|
|
206 |
$job->finish if $job; |
$job->finish if $job; |
207 |
warn "### request over for $uid\n" if $self->debug; |
warn "### request over for $uid\n" if $self->debug; |
220 |
sub dispatch { |
sub dispatch { |
221 |
my $self = shift; |
my $self = shift; |
222 |
|
|
|
warn "##!!! dispatch(",dump( @_ ),")\n"; |
|
|
|
|
223 |
my $dispatch = shift || die "no dispatch?"; |
my $dispatch = shift || die "no dispatch?"; |
224 |
my $args = shift; |
my $args = shift; |
225 |
|
|
229 |
warn ">>> dispatching to $dispatch with args ",dump( $args ),"\n"; |
warn ">>> dispatching to $dispatch with args ",dump( $args ),"\n"; |
230 |
my $xml = $response->$dispatch( $self->state, $args ); |
my $xml = $response->$dispatch( $self->state, $args ); |
231 |
warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
232 |
if ( $self->debug > 2 ) { |
if ( $self->create_dump ) { |
233 |
my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost); |
my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost); |
234 |
write_file( $file, $xml ); |
write_file( $file, $xml ); |
235 |
warn "### response dump: $file\n"; |
warn "### response dump: $file\n" if $self->debug; |
236 |
} |
} |
237 |
return $xml; |
return $xml; |
238 |
} else { |
} else { |