blib/lib/Net/SSH/Perl/Channel.pm untested

filter: Statementstmt 0.0% 0 / 191 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 62 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 359 SCAR 68.4
CC
30
Cov
0%
prepare_for_select
42.0
process_buffers
30.0
rcvd_oclose
30.0
CRAP 930.0
1 package Net::SSH::Perl::Channel;
2 use strict;
3 use warnings;
4  
5 use Net::SSH::Perl::Buffer;
6 use Net::SSH::Perl::Constants qw( :msg2 :channels );
7  
8 use Carp qw( croak );
9 use Scalar::Util qw(weaken);
10  
11 sub new {
12 my $class = shift;
13 my($ssh, $mgr) = (shift, shift);
14 my $c = bless { ssh => $ssh, mgr => $mgr, @_ }, $class;
15 weaken $c->{ssh};
16 weaken $c->{mgr};
17 $c->init;
18 $ssh->debug("channel $c->{id}: new [$c->{remote_name}]");
19 $c;
20 }
21  
22 sub init {
23 my $c = shift;
24 $c->{id} = $c->{mgr}->new_channel_id;
25 $c->{type} = SSH_CHANNEL_OPENING;
26 $c->{input} = Net::SSH::Perl::Buffer->new( MP => 'SSH2' );
27 $c->{output} = Net::SSH::Perl::Buffer->new( MP => 'SSH2' );
28 $c->{extended} = Net::SSH::Perl::Buffer->new( MP => 'SSH2' );
29 $c->{ostate} = CHAN_OUTPUT_OPEN;
30 $c->{istate} = CHAN_INPUT_OPEN;
31 $c->{flags} = 0;
32 $c->{remote_window} = 0;
33 $c->{local_window} ||= 32 * 1024;
34 $c->{local_window_max} = $c->{local_window};
35 $c->{local_consumed} = 0;
36 $c->{local_maxpacket} ||= 16 * 1024;
37 $c->{ctype} ||= 'session';
38 $c->{remote_name} ||= 'client-session';
39 }
40  
41 sub open {
42 my $c = shift;
43 my $ssh = $c->{ssh};
44 $ssh->debug("Requesting channel_open for channel $c->{id}.");
45 my $packet = $ssh->packet_start(SSH2_MSG_CHANNEL_OPEN);
46 $packet->put_str($c->{ctype});
47 $packet->put_int32($c->{id});
48 $packet->put_int32($c->{local_window});
49 $packet->put_int32($c->{local_maxpacket});
50 $packet->send;
51 }
52  
53 sub request {
54 my $c = shift;
55 my $packet = $c->request_start(@_);
56 $packet->send;
57 }
58  
59 sub request_start {
60 my $c = shift;
61 my($service, $want_reply) = @_;
62 my $ssh = $c->{ssh};
63 $ssh->debug("Requesting service $service on channel $c->{id}.");
64 my $packet = $ssh->packet_start(SSH2_MSG_CHANNEL_REQUEST);
65 $packet->put_int32($c->{remote_id});
66 $packet->put_str($service);
67 $packet->put_int8($want_reply);
68 return $packet;
69 }
70  
71 sub send_data {
72 my $c = shift;
73 my($buf) = @_;
74 $c->{input}->append($buf);
75 }
76  
77 sub process_outgoing {
78 my $c = shift;
79 return unless ($c->{istate} == CHAN_INPUT_OPEN ||
80 $c->{istate} == CHAN_INPUT_WAIT_DRAIN) &&
81 $c->{input}->length > 0;
82 my $len = $c->{input}->length;
83 $len = $c->{remote_window} if $len > $c->{remote_window};
84 $len = $c->{remote_maxpacket} if $len > $c->{remote_maxpacket};
85 my $data = $c->{input}->bytes(0, $len, '');
86 my $packet = $c->{ssh}->packet_start(SSH2_MSG_CHANNEL_DATA);
87 $packet->put_int32($c->{remote_id});
88 $packet->put_str($data);
89 $packet->send;
90 $c->{remote_window} -= $len;
91 }
92  
93 sub check_window {
94 my $c = shift;
95 if ($c->{type} == SSH_CHANNEL_OPEN &&
96 !($c->{flags} & (CHAN_CLOSE_SENT | CHAN_CLOSE_RCVD)) &&
97 $c->{local_window} < $c->{local_window_max}/2 &&
98 $c->{local_consumed} > 0) {
99 my $packet = $c->{ssh}->packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
100 $packet->put_int32($c->{remote_id});
101 $packet->put_int32($c->{local_consumed});
102 $packet->send;
103 $c->{ssh}->debug("channel $c->{id}: window $c->{local_window} sent adjust $c->{local_consumed}");
104 $c->{local_window} += $c->{local_consumed};
105 $c->{local_consumed} = 0;
106 }
107 }
108  
109 sub prepare_for_select {
110 my $c = shift;
111 my($rb, $wb) = @_;
112 if ($c->{rfd} && $c->{istate} == CHAN_INPUT_OPEN &&
113 $c->{remote_window} > 0 &&
114 $c->{input}->length < $c->{remote_window}) {
115 $rb->add($c->{rfd});
116 }
117 if ($c->{wfd} &&
118 $c->{ostate} == CHAN_OUTPUT_OPEN ||
119 $c->{ostate} == CHAN_OUTPUT_WAIT_DRAIN) {
120 if ($c->{output}->length > 0) {
121 $wb->add($c->{wfd});
122 }
123 elsif ($c->{ostate} == CHAN_OUTPUT_WAIT_DRAIN &&
124 $c->{extended}->length == 0) {
125 $c->obuf_empty;
126 }
127 }
128 if ($c->{efd} && $c->{extended}->length > 0) {
129 $wb->add($c->{efd});
130 }
131 }
132  
133 sub process_buffers {
134 my $c = shift;
135 my($rready, $wready) = @_;
136  
137 my %fd = (output => $c->{wfd}, extended => $c->{efd});
138 for my $buf (keys %fd) {
139 if ($fd{$buf} && grep { $fd{$buf} == $_ } @$wready) {
140 if (my $r = $c->{handlers}{"_${buf}_buffer"}) {
141 $r->{code}->( $c, $c->{$buf}, @{ $r->{extra} } );
142 }
143 else {
144 #warn "No handler for '$buf' buffer set up";
145 }
146 $c->{local_consumed} += $c->{$buf}->length
147 if $buf eq "output";
148 $c->{$buf}->empty;
149 }
150 }
151  
152 if ($c->{rfd} && grep { $c->{rfd} == $_ } @$rready) {
153 my $buf;
154 sysread $c->{rfd}, $buf, 8192;
155 ($buf) = $buf =~ /(.*)/s;
156 $c->send_data($buf);
157 }
158 }
159  
160 sub rcvd_ieof {
161 my $c = shift;
162 $c->{ssh}->debug("channel $c->{id}: rcvd eof");
163 if ($c->{ostate} && $c->{ostate} == CHAN_OUTPUT_OPEN) {
164 $c->{ssh}->debug("channel $c->{id}: output open -> drain");
165 $c->{ostate} = CHAN_OUTPUT_WAIT_DRAIN;
166 }
167 }
168  
169 sub obuf_empty {
170 my $c = shift;
171 $c->{ssh}->debug("channel $c->{id}: obuf empty");
172 if ($c->{output}->length) {
173 warn "internal error: obuf_empty $c->{id} for non empty buffer";
174 return;
175 }
176 if ($c->{ostate} == CHAN_OUTPUT_WAIT_DRAIN) {
177 $c->{ssh}->debug("channel $c->{id}: output drain -> closed");
178 $c->shutdown_write;
179 $c->{ostate} = CHAN_OUTPUT_CLOSED;
180 }
181 else {
182 warn "channel $c->{id}: internal error: obuf_empty for ostate $c->{ostate}";
183 }
184 }
185  
186 sub drain_outgoing {
187 my $c = shift;
188 $c->register_handler(SSH2_MSG_CHANNEL_WINDOW_ADJUST, sub {
189 $_[0]->{ssh}->break_client_loop
190 });
191 while ($c->{input}->length) {
192 $c->process_outgoing;
193 $c->{ssh}->client_loop if $c->{input}->length;
194 }
195 $c->drop_handler(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
196 $c->{ssh}->restore_client_loop;
197 }
198  
199 sub shutdown_write {
200 my $c = shift;
201 $c->{output}->empty;
202 return if $c->{type} == SSH_CHANNEL_LARVAL;
203 $c->{ssh}->debug("channel $c->{id}: close_write");
204  
205 ## XXX: have to check for socket ($c->{socket}) and either
206 ## do shutdown or close of file descriptor.
207 }
208  
209 sub delete_if_full_closed {
210 my $c = shift;
211 if ($c->{istate} == CHAN_INPUT_CLOSED && $c->{ostate} == CHAN_OUTPUT_CLOSED) {
212 unless ($c->{flags} & CHAN_CLOSE_SENT) {
213 $c->send_close;
214 }
215 if (($c->{flags} & CHAN_CLOSE_SENT) && ($c->{flags} & CHAN_CLOSE_RCVD)) {
216 $c->{ssh}->debug("channel $c->{id}: full closed");
217 return 1;
218 }
219 }
220 return 0;
221 }
222  
223 sub send_close {
224 my $c = shift;
225 $c->{ssh}->debug("channel $c->{id}: send close");
226 if ($c->{ostate} != CHAN_OUTPUT_CLOSED ||
227 $c->{istate} != CHAN_INPUT_CLOSED) {
228 warn "channel $c->{id}: internal error: cannot send close for istate/ostate $c->{istate}/$c->{ostate}";
229 }
230 elsif ($c->{flags} & CHAN_CLOSE_SENT) {
231 warn "channel $c->{id}: internal error: already sent close";
232 }
233 else {
234 my $packet = $c->{ssh}->packet_start(SSH2_MSG_CHANNEL_CLOSE);
235 $packet->put_int32($c->{remote_id});
236 $packet->send;
237 $c->{flags} |= CHAN_CLOSE_SENT;
238 }
239 }
240  
241 sub rcvd_oclose {
242 my $c = shift;
243 $c->{ssh}->debug("channel $c->{id}: rcvd close");
244 $c->{flags} |= CHAN_CLOSE_RCVD;
245 if ($c->{type} == SSH_CHANNEL_LARVAL) {
246 $c->{ostate} = CHAN_OUTPUT_CLOSED;
247 $c->{istate} = CHAN_INPUT_CLOSED;
248 return;
249 }
250 if ($c->{ostate} == CHAN_OUTPUT_OPEN) {
251 $c->{ssh}->debug("channel $c->{id}: output open -> drain");
252 $c->{ostate} = CHAN_OUTPUT_WAIT_DRAIN;
253 }
254 if ($c->{istate} == CHAN_INPUT_OPEN) {
255 $c->{ssh}->debug("channel $c->{id}: input open -> closed");
256 $c->shutdown_read;
257 }
258 elsif ($c->{istate} == CHAN_INPUT_WAIT_DRAIN) {
259 $c->{ssh}->debug("channel $c->{id}: input drain -> closed");
260 $c->send_eof;
261 }
262 $c->{istate} = CHAN_INPUT_CLOSED;
263 }
264  
265 sub shutdown_read {
266 my $c = shift;
267 return if $c->{type} == SSH_CHANNEL_LARVAL;
268 $c->{input}->empty;
269 $c->{ssh}->debug("channel $c->{id}: close_read");
270  
271 ## XXX: have to check for socket ($c->{socket}) and either
272 ## do shutdown or close of file descriptor.
273 }
274  
275 sub send_eof {
276 my $c = shift;
277 $c->{ssh}->debug("channel $c->{id}: send eof");
278 if ($c->{istate} == CHAN_INPUT_WAIT_DRAIN) {
279 my $packet = $c->{ssh}->packet_start(SSH2_MSG_CHANNEL_EOF);
280 $packet->put_int32($c->{remote_id});
281 $packet->send;
282 }
283 else {
284 warn "channel $c->{id}: internal error: cannot send eof for istate $c->{istate}";
285 }
286 }
287  
288 sub register_handler {
289 my $c = shift;
290 my($type, $sub, @extra) = @_;
291 $c->{handlers}{$type} = { code => $sub, extra => \@extra };
292 }
293  
294 sub drop_handler { delete $_[0]->{handlers}{$_[1]} }
295  
296 1;
297 __END__