1 |
# Dobrica Pavlinusic, <dpavlin@rot13.org> 06/18/07 10:19:50 CEST |
# Dobrica Pavlinusic, <dpavlin@rot13.org> 06/18/07 10:19:50 CEST |
2 |
package CWMP::Server; |
package CWMP::Session; |
3 |
|
|
4 |
use strict; |
use strict; |
5 |
use warnings; |
use warnings; |
7 |
use base qw/Class::Accessor/; |
use base qw/Class::Accessor/; |
8 |
__PACKAGE__->mk_accessors( qw/ |
__PACKAGE__->mk_accessors( qw/ |
9 |
debug |
debug |
10 |
port |
store |
11 |
|
|
12 |
sock |
sock |
13 |
state |
state |
14 |
queue |
queue |
15 |
|
store |
16 |
/ ); |
/ ); |
17 |
|
|
18 |
use IO::Socket::INET; |
use HTTP::Daemon; |
19 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
20 |
|
use Carp qw/confess cluck croak/; |
21 |
|
use File::Slurp; |
22 |
|
|
23 |
use CWMP::Request; |
use CWMP::Request; |
24 |
use CWMP::Response; |
use CWMP::Response; |
25 |
use Carp qw/confess cluck/; |
use CWMP::Store; |
26 |
|
|
27 |
=head1 NAME |
=head1 NAME |
28 |
|
|
29 |
CWMP::Server - implement logic of CWMP protocol |
CWMP::Session - implement logic of CWMP protocol |
30 |
|
|
31 |
=head1 METHODS |
=head1 METHODS |
32 |
|
|
33 |
=head2 new |
=head2 new |
34 |
|
|
35 |
my $server = CWMP::Server->new({ port => 3333 }); |
my $server = CWMP::Session->new({ |
36 |
|
sock => $io_socket_object, |
37 |
|
store => 'state.db', |
38 |
|
queue => [ |
39 |
|
'GetRPCMethods', |
40 |
|
[ 'GetParameterValyes', 'InternetGatewayDevice.DeviceInfo.SerialNumber', 0 ], |
41 |
|
], |
42 |
|
debug => 1, |
43 |
|
}); |
44 |
|
|
45 |
=head2 run |
=cut |
46 |
|
|
47 |
$server->run(); |
sub new { |
48 |
|
my $class = shift; |
49 |
|
my $self = $class->SUPER::new( @_ ); |
50 |
|
|
51 |
=cut |
confess "need sock" unless $self->sock; |
52 |
|
|
53 |
sub run { |
$self->debug( 0 ) unless $self->debug; |
|
my $self = shift; |
|
54 |
|
|
55 |
my $listen = IO::Socket::INET->new( |
warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug; |
|
Listen => 5, |
|
|
# LocalAddr => 'localhost', |
|
|
LocalPort => $self->port, |
|
|
Proto => 'tcp', |
|
|
Blocking => 1, |
|
|
ReuseAddr => 1, |
|
|
); |
|
|
|
|
|
warn "waiting for request on port ", $self->port, $/; |
|
|
|
|
|
while ( my $sock = $listen->accept ) { |
|
|
$sock->autoflush(1); |
|
|
|
|
|
warn "connection from ", $sock->peerhost, "\n"; |
|
|
|
|
|
$self->sock( $sock ); # FIXME this will not work for multiple clients |
|
|
while ( $self->process_request ) { |
|
|
warn "...another one bites a dust...\n"; |
|
|
} |
|
56 |
|
|
57 |
warn "...returning to accepting new connections\n"; |
my $store_obj = CWMP::Store->new({ |
58 |
} |
debug => $self->debug, |
59 |
|
%{ $self->store }, |
60 |
|
}); |
61 |
|
|
62 |
|
croak "can't open ", dump( $self->store ), ": $!" unless $store_obj; |
63 |
|
|
64 |
|
# FIXME looks ugly. Should we have separate accessor for this? |
65 |
|
$self->store( $store_obj ); |
66 |
|
|
67 |
|
return $self; |
68 |
} |
} |
69 |
|
|
70 |
=head2 process_request |
=head2 process_request |
73 |
facilitate brain-dead concept of adding state to stateless protocol like |
facilitate brain-dead concept of adding state to stateless protocol like |
74 |
HTTP. |
HTTP. |
75 |
|
|
76 |
|
If used with debugging level of 3 or more, it will also create dumps of |
77 |
|
requests named C<< dump/nr.request >> where C<nr> is number from 0 to total number |
78 |
|
of requests in single session. |
79 |
|
|
80 |
=cut |
=cut |
81 |
|
|
82 |
|
my $dump_nr = 0; |
83 |
|
|
84 |
sub process_request { |
sub process_request { |
85 |
my $self = shift; |
my $self = shift; |
86 |
|
|
87 |
my $sock = $self->sock || die "no sock?"; |
my $sock = $self->sock || die "no sock?"; |
88 |
|
|
89 |
die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'IO::Socket::INET' ); |
# die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'Net::Server::Proto::TCP' ); |
90 |
|
|
91 |
if ( ! $sock->connected ) { |
if ( ! $sock->connected ) { |
92 |
warn "SOCKET NOT CONNECTED"; |
warn "SOCKET NOT CONNECTED\n"; |
93 |
return 0; |
return 0; |
94 |
} |
} |
95 |
|
|
96 |
$sock->autoflush( 1 ); |
bless $sock, 'HTTP::Daemon::ClientConn'; |
|
$sock->blocking( 1 ); |
|
|
|
|
|
### read the first line of response |
|
|
my $line = $sock->getline; |
|
|
return $self->error(400, "No Data") unless ( defined $line ); |
|
|
|
|
|
$line =~ s/[\r\n]+$//; |
|
|
if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) { |
|
|
warn "ERROR: $line\n"; |
|
|
return $self->error(400, "Bad request"); |
|
|
} |
|
|
my ($method, $req, $protocol) = ($1, $2, $3); |
|
|
warn "<<<< ", $sock->peerhost, " - - [" . localtime() . "] \"$method $req $protocol\"\n"; |
|
|
|
|
|
### read in other headers |
|
|
$self->read_headers || return $self->error(400, "Strange headers"); |
|
|
|
|
|
### do we support the type |
|
|
# if ($method !~ /GET|POST|HEAD/) { |
|
|
if ($method !~ /POST/) { |
|
|
return $self->error(400, "Unsupported Method"); |
|
|
} |
|
|
|
|
|
my $chunk; |
|
|
my $transfer_encoding = $self->header('Transfer-Encoding'); |
|
|
|
|
|
if ( $transfer_encoding && $transfer_encoding =~ qr/^chunked/i ) { |
|
|
|
|
|
my $len = 0; |
|
97 |
|
|
98 |
do { |
# why do I have to do this? |
99 |
|
# solution from http://use.perl.org/~Matts/journal/12896 |
100 |
|
${*$sock}{'httpd_daemon'} = HTTP::Daemon->new; |
101 |
|
|
102 |
warn "get chunk len\n" if $self->debug; |
my $r = $sock->get_request || confess "can't get_request"; |
|
|
|
|
my $hex; |
|
|
do { |
|
|
$hex = $sock->getline; |
|
|
$hex =~ s/[\n\r]+$//; |
|
|
} until ( $hex ne '' ); |
|
103 |
|
|
104 |
die "chunk size not valid hex: $hex" unless ( $hex =~ m/^[0-9a-f]+$/i); |
my $xml = $r->content; |
|
$len = hex( $hex ); |
|
105 |
|
|
106 |
warn "getting chunk of $len bytes\n" if $self->debug; |
my $size = length( $xml ); |
107 |
|
|
108 |
$sock->read( my $buff, $len ); |
warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n"; |
|
$chunk .= $buff; |
|
109 |
|
|
110 |
warn "--- $len bytes: --=>||$buff||<=--\n"; |
if ( $self->debug > 2 ) { |
111 |
|
my $file = sprintf("dump/%04d-%s.request", $dump_nr++, $sock->peerhost); |
112 |
} while ( $len > 0 ); |
write_file( $file, $r->as_string ); |
113 |
my $sep = $sock->getline; |
warn "### request dumped to file: $file\n"; |
|
die "expected separator, not ", dump( $sep ) if ( $sep !~ m/^[\n\r]+$/ ); |
|
|
|
|
|
} else { |
|
|
die "right now, we support only Transfer-Encoding: chunked"; |
|
114 |
} |
} |
115 |
|
|
|
my $size = length( $chunk ); |
|
|
|
|
|
warn "<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n"; |
|
|
|
|
116 |
my $state; |
my $state; |
117 |
|
|
118 |
if ( $size > 0 ) { |
if ( $size > 0 ) { |
119 |
|
|
120 |
die "no SOAPAction header in ",dump($chunk) unless defined ( $self->header('SOAPAction') ); |
die "no SOAPAction header in ",dump($xml) unless defined ( $r->header('SOAPAction') ); |
121 |
|
|
122 |
|
warn "## request payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
123 |
|
|
124 |
if ( $chunk ) { |
$state = CWMP::Request->parse( $xml ); |
|
warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug; |
|
125 |
|
|
126 |
$state = CWMP::Request->parse( $chunk ); |
warn "## acquired state = ", dump( $state ), "\n"; |
127 |
|
|
128 |
warn "acquired state = ", dump( $state ), "\n"; |
$self->state( $state ); |
129 |
|
$self->store->update_state( ID => $state->{ID}, $state ); |
130 |
|
|
131 |
$self->state( $state ); |
} else { |
132 |
|
|
133 |
} else { |
warn "## empty request, using last request state\n"; |
|
warn "empty request\n"; |
|
|
} |
|
134 |
|
|
|
} else { |
|
135 |
$state = $self->state; |
$state = $self->state; |
136 |
warn "last request state = ", dump( $state ), "\n"; |
delete( $state->{_dispatch} ); |
137 |
|
#warn "last request state = ", dump( $state ), "\n" if $self->debug > 1; |
138 |
} |
} |
139 |
|
|
140 |
|
|
143 |
'Content-Type: text/xml; charset="utf-8"', |
'Content-Type: text/xml; charset="utf-8"', |
144 |
'Server: AcmeCWMP/42', |
'Server: AcmeCWMP/42', |
145 |
'SOAPServer: AcmeCWMP/42' |
'SOAPServer: AcmeCWMP/42' |
146 |
)); |
)."\r\n"); |
147 |
|
|
148 |
$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} ); |
149 |
|
|
154 |
} elsif ( $dispatch = shift @{ $self->queue } ) { |
} elsif ( $dispatch = shift @{ $self->queue } ) { |
155 |
$xml = $self->dispatch( $dispatch ); |
$xml = $self->dispatch( $dispatch ); |
156 |
} elsif ( $size == 0 ) { |
} elsif ( $size == 0 ) { |
157 |
warn ">>> closing connection\n"; |
warn ">>> no more queued commands, closing connection\n"; |
158 |
return 0; |
return 0; |
159 |
} else { |
} else { |
160 |
warn ">>> empty response\n"; |
warn ">>> empty response\n"; |
165 |
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
166 |
$sock->send( $xml ) or die "can't send response"; |
$sock->send( $xml ) or die "can't send response"; |
167 |
|
|
168 |
warn "### request over"; |
warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n"; |
169 |
|
|
170 |
|
warn "### request over\n" if $self->debug; |
171 |
|
|
172 |
|
return 1; # next request |
173 |
}; |
}; |
174 |
|
|
175 |
=head2 dispatch |
=head2 dispatch |
176 |
|
|
177 |
$xml = $self->dispatch('Inform', $response_arguments ); |
$xml = $self->dispatch('Inform', $response_arguments ); |
178 |
|
|
179 |
|
If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >> |
180 |
|
|
181 |
=cut |
=cut |
182 |
|
|
183 |
sub dispatch { |
sub dispatch { |
184 |
my $self = shift; |
my $self = shift; |
185 |
|
|
186 |
my $dispatch = shift || die "no dispatch?"; |
my $dispatch = shift || die "no dispatch?"; |
187 |
|
my @args = @_; |
188 |
|
|
189 |
|
if ( ref($dispatch) eq 'ARRAY' ) { |
190 |
|
my @a = @$dispatch; |
191 |
|
$dispatch = shift @a; |
192 |
|
push @args, @a; |
193 |
|
} |
194 |
|
|
195 |
my $response = CWMP::Response->new({ debug => $self->debug }); |
my $response = CWMP::Response->new({ debug => $self->debug }); |
196 |
|
|
197 |
if ( $response->can( $dispatch ) ) { |
if ( $response->can( $dispatch ) ) { |
198 |
warn ">>> dispatching to $dispatch\n"; |
warn ">>> dispatching to $dispatch\n"; |
199 |
my $xml = $response->$dispatch( $self->state, @_ ) . "\r\n"; |
my $xml = $response->$dispatch( $self->state, @args ); |
200 |
warn "## response payload: ",length($xml)," bytes\n$xml\n"; |
warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
201 |
|
if ( $self->debug > 2 ) { |
202 |
|
my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost); |
203 |
|
write_file( $file, $xml ); |
204 |
|
warn "### response dump: $file\n"; |
205 |
|
} |
206 |
return $xml; |
return $xml; |
207 |
} else { |
} else { |
208 |
confess "can't dispatch to $dispatch"; |
confess "can't dispatch to $dispatch"; |
209 |
} |
} |
210 |
}; |
}; |
211 |
|
|
|
=head2 read_headers |
|
|
|
|
|
parse headers from request |
|
|
|
|
|
=cut |
|
|
|
|
|
sub read_headers { |
|
|
my $self = shift; |
|
|
|
|
|
$self->{headers} = {}; |
|
|
|
|
|
while (defined($_ = $self->sock->getline)) { |
|
|
s/[\r\n]+$//; |
|
|
last unless length $_; |
|
|
warn "-- $_\n"; |
|
|
return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x; |
|
|
$self->{headers}->{$1} = $2; |
|
|
} |
|
|
|
|
|
return 1; |
|
|
} |
|
|
|
|
|
=head2 header |
|
|
|
|
|
Getter for specific header |
|
|
|
|
|
$self->header('Cookies'); |
|
|
|
|
|
=cut |
|
|
|
|
|
sub header { |
|
|
my $self = shift; |
|
|
my $header = shift || die "no header?"; |
|
|
if ( defined( $self->{headers}->{$header} )) { |
|
|
return $self->{headers}->{$header}; |
|
|
} else { |
|
|
return; |
|
|
} |
|
|
} |
|
212 |
|
|
213 |
=head2 error |
=head2 error |
214 |
|
|