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

filter: Statementstmt 0.0% 0 / 144 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 26 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 253 SCAR 58.3
CC
18
Cov
0%
any_open_channels
12.0
prepare_channels
12.0
process_input_packets
12.0
CRAP 342.0
1 package Net::SSH::Perl::ChannelMgr;
2 use strict;
3 use warnings;
4  
5 use Net::SSH::Perl::Channel;
6 use Net::SSH::Perl::Packet;
7 use Net::SSH::Perl::Constants qw( :msg2 :channels );
8  
9 use Carp qw( croak );
10 use Scalar::Util qw(weaken);
11  
12 sub new {
13 my $class = shift;
14 my $ssh = shift;
15 my $cmgr = bless { ssh => $ssh, @_ }, $class;
16 weaken $cmgr->{ssh};
17 $cmgr->init;
18 $cmgr;
19 }
20  
21 sub init {
22 my $cmgr = shift;
23 $cmgr->{channels} = [];
24 $cmgr->{handlers} = {
25 SSH2_MSG_CHANNEL_CLOSE() => \&input_oclose,
26 SSH2_MSG_CHANNEL_DATA() => \&input_data,
27 SSH2_MSG_CHANNEL_EOF() => \&input_eof,
28 SSH2_MSG_CHANNEL_EXTENDED_DATA() => \&input_extended_data,
29 SSH2_MSG_CHANNEL_OPEN_CONFIRMATION() => \&input_open_confirmation,
30 SSH2_MSG_CHANNEL_OPEN_FAILURE() => \&input_open_failure,
31 SSH2_MSG_CHANNEL_REQUEST() => \&input_channel_request,
32 SSH2_MSG_CHANNEL_WINDOW_ADJUST() => \&input_window_adjust,
33 SSH2_MSG_KEXINIT() => \&input_kexinit,
34 };
35 }
36  
37 sub new_channel {
38 my $cmgr = shift;
39 my $c = Net::SSH::Perl::Channel->new($cmgr->{ssh}, $cmgr, @_);
40 push @{ $cmgr->{channels} }, $c;
41 $c;
42 }
43  
44 sub remove {
45 my $cmgr = shift;
46 my($id) = @_;
47 $cmgr->{channels}->[$id] = undef;
48 }
49  
50 sub new_channel_id {
51 my $cmgr = shift;
52 $cmgr->{_channel_id} ||= 0;
53 $cmgr->{_channel_id}++;
54 }
55  
56 sub any_open_channels {
57 my $cmgr = shift;
58 for my $c (@{ $cmgr->{channels} }) {
59 next unless defined $c;
60 return 1 if
61 $c->{type} == SSH_CHANNEL_OPENING ||
62 $c->{type} == SSH_CHANNEL_OPEN ||
63 $c->{type} == SSH_CHANNEL_INPUT_DRAINING ||
64 $c->{type} == SSH_CHANNEL_OUTPUT_DRAINING;
65 }
66 }
67  
68 sub prepare_channels {
69 my $cmgr = shift;
70 for my $c (@{ $cmgr->{channels} }) {
71 next unless defined $c;
72 $c->prepare_for_select(@_);
73 if ($c->delete_if_full_closed) {
74 $cmgr->remove($c->{id});
75 }
76 }
77 }
78  
79 sub process_input_packets {
80 my $cmgr = shift;
81 for my $c (@{ $cmgr->{channels} }) {
82 next unless defined $c;
83 $c->process_buffers(@_);
84 $c->check_window;
85 if ($c->delete_if_full_closed) {
86 $cmgr->remove($c->{id});
87 }
88 }
89 }
90  
91 sub process_output_packets {
92 my $cmgr = shift;
93 for my $c (@{ $cmgr->{channels} }) {
94 next unless defined $c;
95 $c->process_outgoing;
96 }
97 }
98  
99 sub _get_channel_from_packet {
100 my($cmgr, $packet, $what) = @_;
101 my $id = $packet->get_int32;
102 my $c = $cmgr->{channels}->[$id];
103 croak "Received $what for nonexistent channel $id"
104 unless $c;
105 $c;
106 }
107  
108 sub input_oclose {
109 my $cmgr = shift;
110 my($packet) = @_;
111 my $c = $cmgr->_get_channel_from_packet($packet, 'oclose');
112 $c->rcvd_oclose;
113 }
114  
115 sub input_data {
116 my $cmgr = shift;
117 my($packet) = @_;
118 my $c = $cmgr->_get_channel_from_packet($packet, 'data');
119 return unless $c->{type} == SSH_CHANNEL_OPEN;
120 my $data = $packet->get_str;
121 $c->{local_window} -= length $data;
122 $c->{output}->append($data);
123 }
124  
125 sub input_eof {
126 my $cmgr = shift;
127 my($packet) = @_;
128 my $c = $cmgr->_get_channel_from_packet($packet, 'ieof');
129 $c->rcvd_ieof;
130 }
131  
132 sub input_extended_data {
133 my $cmgr = shift;
134 my($packet) = @_;
135 my $c = $cmgr->_get_channel_from_packet($packet, 'extended_data');
136 return unless $c->{type} == SSH_CHANNEL_OPEN;
137 my $code = $packet->get_int32;
138 my $data = $packet->get_str;
139 $c->{extended}->append($data);
140 }
141  
142 sub input_open_confirmation {
143 my $cmgr = shift;
144 my($packet) = @_;
145 my $id = $packet->get_int32;
146 my $c = $cmgr->{channels}->[$id];
147 croak "Received open confirmation for non-opening channel $id"
148 unless $c && $c->{type} == SSH_CHANNEL_OPENING;
149 $c->{remote_id} = $packet->get_int32;
150 $c->{type} = SSH_CHANNEL_OPEN;
151 $c->{remote_window} = $packet->get_int32;
152 $c->{remote_maxpacket} = $packet->get_int32;
153 if (my $sub = $c->{handlers}{$packet->type}{code}) {
154 $sub->($c, $packet);
155 }
156 $cmgr->{ssh}->debug("channel $id: open confirm rwindow $c->{remote_window} rmax $c->{remote_maxpacket}");
157 }
158  
159 sub input_open_failure {
160 my $cmgr = shift;
161 my($packet) = @_;
162 my $id = $packet->get_int32;
163 my $c = $cmgr->{channels}->[$id];
164 croak "Received open failure for non-opening channel $id"
165 unless $c && $c->{type} == SSH_CHANNEL_OPENING;
166 my $reason = $packet->get_int32;
167 my $msg = $packet->get_str;
168 my $lang = $packet->get_str;
169 $cmgr->{ssh}->debug("Channel open failure: $id: reason $reason: $msg");
170 $cmgr->remove($id);
171 }
172  
173 sub input_channel_request {
174 my $cmgr = shift;
175 my($packet) = @_;
176 my $id = $packet->get_int32;
177 my $c = $cmgr->{channels}->[$id];
178 croak "Received request for non-open channel $id"
179 unless $c && $c->{type} == SSH_CHANNEL_OPEN ||
180 $c->{type} == SSH_CHANNEL_LARVAL;
181 if (my $sub = $c->{handlers}{$packet->type}{code}) {
182 $sub->($c, $packet);
183 }
184 }
185  
186 sub input_window_adjust {
187 my $cmgr = shift;
188 my($packet) = @_;
189 my $id = $packet->get_int32;
190 my $c = $cmgr->{channels}->[$id];
191 croak "Received window adjust for non-open channel $id"
192 unless $c && $c->{type} == SSH_CHANNEL_OPEN;
193 $c->{remote_window} += $packet->get_int32;
194 if (my $sub = $c->{handlers}{$packet->type}{code}) {
195 $sub->($c, $packet);
196 }
197 }
198  
199 sub input_kexinit {
200 my $cmgr = shift;
201 my($packet) = @_;
202  
203 my $kex = Net::SSH::Perl::Kex->new($cmgr->{ssh});
204 $kex->exchange($packet);
205 $cmgr->{ssh}->debug("Re-key complete.");
206 }
207  
208 sub register_handler {
209 my $cmgr = shift;
210 my($type, $code) = @_;
211 $cmgr->{handlers}->{ $type } = $code;
212 }
213  
214 sub handlers { $_[0]->{handlers} }
215  
216 1;
217 __END__