blib/lib/Net/SSH/Perl/Subsystem/Server.pm untested

filter: Statementstmt 0.0% 0 / 76 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 4 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 131 SCAR 53.5
CC
14
Cov
0%
serve
72.0
process_incoming
30.0
init
6.0
CRAP 210.0
1 package Net::SSH::Perl::Subsystem::Server;
2 use strict;
3 use warnings;
4  
5 use Net::SSH::Perl::Buffer;
6 use Carp qw( croak );
7 use Symbol qw( gensym );
8  
9 sub new {
10 my $class = shift;
11 my $ss = bless { }, $class;
12 $ss->init(@_);
13 }
14  
15 sub buffer_class { 'Net::SSH::Perl::Buffer' }
16  
17 sub init {
18 my $ss = shift;
19 my %param = @_;
20  
21 $ss->{in} = _dup('STDIN', '<');
22 $ss->{out} = _dup('STDOUT', '>');
23  
24 $ss->{iq} = $ss->buffer_class->new( MP => 'SSH2' );
25 $ss->{oq} = $ss->buffer_class->new( MP => 'SSH2' );
26  
27 if (my $log = $param{Log}) {
28 my $fh = $ss->{log_fh} = gensym;
29 open $fh, '>>', $log or die "Can't open logfile $log: $!";
30 select((select($fh), $|=1)[0]);
31 }
32  
33 $ss;
34 }
35  
36 sub log {
37 my $ss = shift;
38 return unless my $fh = $ss->{log_fh};
39 print $fh "$ss [$$] @_\n";
40 }
41  
42 sub register_handler { $_[0]->{_handlers}{$_[1]} = $_[2] }
43 sub handler_for { $_[0]->{_handlers}{$_[1]} }
44  
45 sub serve {
46 my $ss = shift;
47  
48 while () {
49 vec(my $rbits, fileno($ss->{in}), 1) = 1;
50 vec(my $wbits, fileno($ss->{out}), 1) = 1 if $ss->{oq}->length;
51  
52 croak "select error: $!"
53 unless select($rbits, $wbits, undef, undef);
54  
55 if (vec($rbits, fileno($ss->{in}), 1)) {
56 my $len = sysread $ss->{in}, my $buf, 4*4096;
57 warn("read eof"), return unless $len;
58 croak "read error" unless $len > 0;
59 $ss->{iq}->append($buf);
60 }
61  
62 if ($ss->{oq}->length) {
63 my $len = syswrite $ss->{out}, $ss->{oq}->bytes;
64 croak "write error" if $len < 0;
65 $ss->{oq}->bytes(0, $len, '');
66 }
67  
68 $ss->process_incoming;
69 }
70 }
71  
72 sub process_incoming {
73 my $ss = shift;
74 my $iq = $ss->{iq};
75 return unless $iq->length >= 5;
76 my $len = unpack "N", $iq->bytes(0, 4);
77 croak "Message too long" if $len > 256 * 1024;
78 return if $iq->length < $len + 4;
79 $iq->bytes(0, 4, '');
80 my $msg = $ss->buffer_class->new( MP => 'SSH2' );
81 $msg->append( $iq->bytes(0, $len, '') );
82 my $type = $msg->get_int8;
83 if (my $code = $ss->handler_for($type)) {
84 $code->($ss, $msg);
85 }
86 }
87  
88 sub send_msg {
89 my $ss = shift;
90 my($msg) = @_;
91 $ss->{oq}->put_int32($msg->length);
92 $ss->{oq}->append($msg->bytes);
93 }
94  
95 sub _dup {
96 my($fh, $mode) = @_;
97 my $dup = Symbol::gensym;
98 my $str = "${mode}&$fh";
99 open ($dup, $str) or die "Could not dupe: $!\n"; ## no critic
100 $dup;
101 }
102  
103 1;
104  
105 =head1 NAME
106  
107 Net::SSH::Perl::Subsystem::Server - Server infrastructure for SSH subsystems
108  
109 =head1 SYNOPSIS
110  
111 use Net::SSH::Perl::Subsystem::Server;
112 use base qw( Net::SSH::Perl::Subsystem::Server );
113  
114 use constant MSG_FOO => 1;
115  
116 sub init {
117 my $ss = shift;
118 $ss->SUPER::init(@_);
119  
120 $ss->register_handler(MSG_FOO, \&handle_foo);
121 }
122  
123 sub handle_foo {
124 my $ss = shift;
125 my($msg) = @_;
126 print "Got MSG_FOO message!\n";
127 }
128  
129 =head1 DESCRIPTION
130  
131 I<Net::SSH::Perl::Subsystem::Server> is a generic subclass that can
132 be used to build servers for SSH-2 subsystems. A subsystem is a
133 network protocol that runs on top of a secure, encrypted SSH
134 connection between two machines: it allows the user and developer
135 to build a secure network protocol without worrying about the
136 details of that security, because it inherits the secure tunnel
137 from SSH.
138  
139 I<Subsystem::Server> provides basic functionality needed by
140 all subsystem servers. A subsystem daemon is started up by the
141 sshd when a request comes in for that subsystem; sshd and the
142 subsystem daemon then talk to each other through pipes, and data
143 that the daemon wishes to send to the subsystem client is sent
144 over the network through the SSH secure tunnel. I<Subsystem::Server>
145 handles the talking to the sshd, and lets the application
146 developer focus on designing the network protocol and handling
147 requests from the subsystem client.
148  
149 =head1 USAGE
150  
151 I<Net::SSH::Perl::Subsystem::Server> is meant to be used as a base
152 class for subsystem servers. With that in mind, general usage should
153 follow the example above in the I<SYNOPSIS>:
154  
155 =over 4
156  
157 =item * Initialization
158  
159 If you want your subclass to do anything, you'll want to override
160 the I<init> method so that you can set up handlers for specific
161 types of messages from the subsystem client. For each message
162 type, you need to associate the type with a subroutine reference
163 that will be invoked when a message of that type is received
164 by the core server. You do this by calling the I<register_handler>
165 method (see below).
166  
167 =item * Message Handling
168  
169 When the core server receives new messages from the client, it
170 grabs the first byte from the incoming stream; the first byte is
171 a packed 8-bit integer representing the type of the message. This
172 identifier is used to look up the message handler to handle this
173 particular type of message.
174  
175 =back
176  
177 These are the public methods in which your subclass will be most
178 interested:
179  
180 =head2 $ss->init(%args)
181  
182 Initializes the subsystem server object. This is where you'll
183 want to set up your message handlers (using I<register_handler>)
184 and perhaps perform any other protocol-specific initialization.
185  
186 Make sure that your I<init> method returns the I<$ss> object
187 on success; failure to return I<init> should be an indication
188 of failure to calling code.
189  
190 I<%args> can contain whatever you like it to contain. The base
191 class I<Net::SSH::Perl::Subsystem::Server> takes these
192 parameters in I<%args>:
193  
194 =over 4
195  
196 =item * Log
197  
198 The location of a file on disk where you can write messages
199 to be logged. This is the file to which messages sent to
200 the I<log> method (below) will be written.
201  
202 This is an optional argument; if not specified, no log file
203 will be used, and calls to I<log> will be silently ignored.
204  
205 =back
206  
207 =head2 $ss->register_handler($type, $code)
208  
209 Configures the subsystem server I<$ss> such that any message
210 sent from the client whose type is I<$type> will automatically
211 invoke the subroutine reference I<$code>. This is how you build
212 protocol-specific functionality into your subsystem: you
213 associate message types with methods.
214  
215 The subroutine reference I<$code> will be invoked and given
216 two arguments: I<$ss>, the instance of the subsystem server
217 that is blessed into your subclass, and I<$msg>, a buffer in
218 the class I<Net::SSH::Perl::Buffer> (although you can choose
219 a different buffer class--see I<buffer_class>, below).
220  
221 =head2 $ss->send_msg($msg)
222  
223 Sends the message I<$msg> to the client. Or, in more technical
224 terms, adds the message I<$msg> to the server's output queue, to
225 be written back to the client the next time through the select
226 loop.
227  
228 I<$msg> should be a buffer in the class I<Net::SSH::Perl::Buffer>
229 (although you can choose a different buffer class--see
230 I<buffer_class>, below).
231  
232 =head2 $ss->serve
233  
234 Enters the select loop, waiting for requests from the client.
235 Users of your class should call this method when they're
236 ready to start serving clients.
237  
238 =head2 $ss->log($message)
239  
240 Writes the log message I<$message> to the log file, if one was
241 specified as the I<Log> argument to I<init> (or, rather, to the
242 constructor).
243  
244 If a log file was not specified, returns silently.
245  
246 =head2 $ss->buffer_class
247  
248 By default, messages are represented by I<Net::SSH::Perl::Buffer>
249 objects. You can alter this by overriding the I<buffer_class>
250 method; it should return the name of an alternate class. Be aware
251 that this alternate class I<must> conform to the interface used
252 by I<Net::SSH::Perl::Buffer>, so you may be best off subclassing
253 that class and adding in your own functionality.
254  
255 =head1 NOTES
256  
257 It should be noted that the external interface (API) to this
258 module is alpha, and could change.
259  
260 =head1 AUTHOR & COPYRIGHTS
261  
262 Please see the Net::SSH::Perl manpage for author, copyright,
263 and license information.
264  
265 =cut