XDR/CallReply.pm

filter: Statementstmt 91.6% 176 / 192 Branchbran 73.1% 79 / 108 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 22 / 22 Podpod 0.0% 0 / 4 totaltotal 85.6% 292 / 341 CC 58 SCAR 21.8
CC
58
Cov
85%
define
17.0
struct
14.4
dispatch
9.8
CRAP 8.9
↑ Summary XDR/RPC/Reply.pm »
1 # CallRep.pm - XDR RPC protocol helper functions
2 # Copyright (C) 2000 Mountain View Data, Inc.
3 # Written by Gordon Matzigkeit <gord@fig.org>, 2000-12-15
4 #
5 # This file is part of Perl XDR.
6 #
7 # Perl XDR is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # Perl XDR is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 # USA
21  
22 package XDR::CallReply;
23 # [guilt]
24 # [maint
25 # File: CallRep.pm
26 # Summary: XDR protocol helper functions
27 # Package: Perl XDR
28 # Owner: Mountain View Data, Inc.
29 # Years: 2000
30 # Author: Gordon Matzigkeit
31 # Contact: <gord@fig.org>
32 # Date: 2000-12-15
33 # License: GPL]
34 # [clemency]
35  
36 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39  
40 sub new
41 1 ▶{
Subroutine new: called CC=3 SCAR=11.4
Pod undocumented
42 1 my ($type, $prog, $vers) = @_;
43 1 ▶ $vers = 0 if (! $vers);
Branch: 1 branch
BranchTrueFalseTotal
unless $vers 0 1 1
44 1 ▶ $prog = 0 if (! $prog);
Branch: 1 branch
BranchTrueFalseTotal
unless $prog 0 1 1
45 1 bless { PROGRAM => $prog, VERSION => $vers, types => {} }, $type;
46 }
47  
48  
49 # Define a new type name.
50 sub typedef
51 2 ▶{
Subroutine typedef: called CC=2 SCAR=6.9
Pod undocumented
52 2 my ($self, $type, $name, @args) = @_;
53 2 ▶ if ($type eq 'struct')
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'struct') { } 1 1 2
54 {
55 1 $self->{TYPES}->{$name} = [@args];
56 }
57 else
58 {
59 1 $self->{TYPES}->{$name} = $type;
60 }
61 }
62  
63  
64 # Encode a structure.
65 sub struct
66 18 ▶{
Subroutine struct: called CC=14 SCAR=26.7
Pod undocumented
67 18 my ($self, $type, $arg) = @_;
68 18 my $types = $self->{TYPES};
69 18 my $ret = '';
70  
71 18 my $tname = $type;
72 18 ▶ while (! ref ($type) && defined $self->{TYPES}->{$type})
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $type and defined $self->{"TYPES"}{$type}) 12 18 30
Condition100%3/3
not ref $type and defined $self->{"TYPES"}{$type}
!ll&&!rl&&r
61212
MC/DC100%2/2
not ref $type and defined $self->{"TYPES"}{$type}
not ref $type defined $self->{"TYPES"}{$type}
Truth table100%3/3
not(A) and B
A= not ref $typeB= defined $self->{"TYPES"}{$type}
ABresult
0X0
100
111
73 {
74 12 $tname = $type;
75 12 $type = $self->{TYPES}->{$type};
76 }
77 18 ▶ if (UNIVERSAL::isa ($arg, 'XDR::Decode'))
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($arg, 'XDR::Decode')) { } 9 9 18
elsif (ref $type) { } 3 6 9
78 {
79 # We're decoding.
80 9 ▶ if (ref $type)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type) { } 3 6 9
81 {
82 3 my $i;
83 3 $ret = [];
84 # FIXME: Why is $type getting an undef pushed on it?
85 3 ▶ for ($i = 0; $i < @$type; $i ++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$type) 6 3 9
86 {
87 6 my ($subtype) = $type->[$i];
88 6 ▶ ($subtype) = split (/\s+/, $subtype) if (! ref $subtype);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $subtype 6 0 6
89 6 push @$ret, $self->struct ($subtype, $arg);
90 }
91 }
92 else
93 {
94 6 $ret = eval "\$arg->$type ()";
95 6 ▶ confess $@ if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
96 }
97 }
98 elsif (ref $type)
99 {
100 # We're encoding a reference.
101 3 ▶ confess "\`$arg' is not an array reference" if (ref $arg ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $arg ne "ARRAY" 0 3 3
102 3 ▶ if (scalar (@$type) != scalar (@$arg))
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$type != scalar @$arg) 0 3 3
103 {
104 0 warn "Received ", scalar (@$arg) + 1, " arguments for struct ",
105 $tname, ", not ", scalar (@$type) + 1, "\n";
106 }
107 3 my $i;
108 # FIXME: Why is $type getting an undef pushed on it?
109 3 ▶ for ($i = 0; $i < @$type; $i ++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$type) 6 3 9
110 {
111 6 my ($subtype) = $type->[$i];
112 6 ▶ ($subtype) = split (/\s+/, $subtype) if (! ref $subtype);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $subtype 6 0 6
113 6 $ret .= $self->struct ($subtype, $arg->[$i]);
114 }
115 }
116 else
117 {
118 # Encoding a scalar.
119 6 my ($sub) = eval "XDR::Encode::$type (\$arg)";
120 6 ▶ confess $@ if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
121 6 $ret .= $sub;
122 }
123 18 return $ret;
124 }
125  
126  
127 # Define an RPC.
128 sub define
129 4 ▶{
Subroutine define: called CC=16 SCAR=28.3
Pod undocumented
130 4 my ($self, $proc, $rets, $name, @args) = @_;
131 4 $self->{$proc} = [ $rets, $name, @args ];
132  
133 # Automatically determine the package name.
134 4 my ($pkg) = caller;
135 4 my $need_struct = 0;
136  
137 # Determine the types and build up a prototype.
138 4 my ($proto, $arg, $nargs, $i);
139 4 $nargs = 0;
140 4 ▶ for ($i = 0; $i <= $#args; $i ++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#args) 6 4 10
141 {
142 6 my ($type, $name) = split (/ /, $args[$i]);
143 6 $proto .= "\$";
144 6 ▶ $arg .= ' . ' if ($i != 0);
Branch: 1 branch
BranchTrueFalseTotal
if $i != 0 2 4 6
145 6 my $tname = $type;
146 6 ▶ while (! ref $type && defined $self->{TYPES}->{$type})
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $type and defined $self->{"TYPES"}{$type}) 3 6 9
Condition100%3/3
not ref $type and defined $self->{"TYPES"}{$type}
!ll&&!rl&&r
243
MC/DC100%2/2
not ref $type and defined $self->{"TYPES"}{$type}
not ref $type defined $self->{"TYPES"}{$type}
Truth table100%3/3
not(A) and B
A= not ref $typeB= defined $self->{"TYPES"}{$type}
ABresult
0X0
100
111
147 {
148 3 $tname = $type;
149 3 $type = $self->{TYPES}->{$type};
150 }
151 6 ▶ if ($type ne 'void')
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "void") 5 1 6
152 {
153 5 ▶ if (ref $type)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type) { } 2 3 5
154 {
155 2 $arg .= "\$_xdr_callreply->struct ('$tname', \$_[$nargs])";
156 }
157 else
158 {
159 3 $arg .= "XDR::Encode::$type (\$_[$nargs])";
160 }
161 5 $nargs ++;
162 }
163 }
164  
165 4 ▶ $arg = "''" if ($nargs == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $nargs == 0 1 3 4
166  
167 4 my ($type) = split (/ /, $rets);
168 4 my $tname = $type;
169 4 ▶ while (! ref $type && defined $self->{TYPES}->{$type})
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $type and defined $self->{"TYPES"}{$type}) 1 4 5
Condition100%3/3
not ref $type and defined $self->{"TYPES"}{$type}
!ll&&!rl&&r
131
MC/DC100%2/2
not ref $type and defined $self->{"TYPES"}{$type}
not ref $type defined $self->{"TYPES"}{$type}
Truth table100%3/3
not(A) and B
A= not ref $typeB= defined $self->{"TYPES"}{$type}
ABresult
0X0
100
111
170 {
171 1 $tname = $type;
172 1 $type = $self->{TYPES}->{$type};
173 }
174  
175 4 my ($res, $nres);
176 4 $nres = 0;
177 4 ▶ if ($type ne 'void')
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "void") 2 2 4
178 {
179 2 ▶ if (ref $type)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type) { } 1 1 2
180 {
181 1 $res = "\$_xdr_callreply->struct ('$tname', \$_[0])";
182 }
183 else
184 {
185 1 $res = "XDR::Encode::$type (\$_[0])";
186 }
187 2 $nres ++;
188 }
189 4 ▶ $res = "''" if ($nres == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $nres == 0 2 2 4
190  
191 4 my ($stub) = "package $pkg;\n";
192 4 ▶ if (! $pkg->can ('call'))
Branch: 1 branch
BranchTrueFalseTotal
unless ($pkg->can("call")) 1 3 4
193 {
194 1 $stub .= "
195 # FIXME: It would be nice to close \$self within this eval, but
196 # perl documentation implies that it is impossible.
197 use vars qw(\$_xdr_callreply);
198 \$_xdr_callreply = \$self;
199  
200 use Carp;
201  
202 # Return a call packet generator.
203 sub call
204 {
205 my (\$type) = \@_;
206 return bless [ 0, 0 ], \$type;
207 }
208  
209  
210 # Return a reply packet generator.
211 sub reply
212 {
213 my (\$type) = \@_;
214 return bless [ 1 ], \$type;
215 }
216  
217  
218 # Return a new hook database.
219 sub hookdb
220 {
221 my (\$type) = \@_;
222 return bless [ \$_xdr_callreply, {}, {} ], \$type;
223 }
224  
225  
226 # Set up a hook for the given callrep.
227 use XDR ':vers';
228 use XDR::RPC;
229 sub hook
230 {
231 my (\$slf, \$proto, \$hook, \$xid) = \@_;
232 if (defined \$xid)
233 {
234 # We're binding a reply packet.
235 \$xid = XDR::RPC->decode (\$xid)->xid
236 if (\$xid !~ /^\d+\$/);
237 \$slf->[1]->{\$xid} = [\$hook, \@\$proto];
238 }
239 else
240 {
241 # We have a call packet.
242 \$slf->[2]->{\&RPCVERS}->{\$proto->[0]}->{\$proto->[1]}->{\$proto->[2]} =
243 [ \$hook, \@\$proto ];
244 }
245 }";
246 }
247  
248 4 ▶ if (! $pkg->can ('dispatch'))
Branch: 1 branch
BranchTrueFalseTotal
unless ($pkg->can("dispatch")) 1 3 4
249 {
250 1 $stub .= "
251  
252 use XDR ':all';
253 use XDR::RPC;
254 use XDR::Encode ':all';
255  
256 # Invoke the hook for a given RPC.
257 sub dispatch
258 {
259 my (\$slf, \$rpc, \@args) = \@_;
260  
261 # Implicitly convert buffers to RPC objects.
262 \$rpc = XDR::RPC->decode (\$rpc)
263 if (! UNIVERSAL::isa (\$rpc, 'XDR::RPC'));
264  
265 my (\$bad, \@proto, \$func);
266 if (\$rpc->can ('rpcvers'))
267 {
268 # Call packet.
269 my (\$binding) = \$slf->[2];
270 my \$t = \$binding->{\$rpc->rpcvers};
271 if (! defined \$t)
272 {
273 # Bad version.
274 my (\@vsns, \$low, \$high) = sort keys %\$binding;
275 \$low = \$vsns[0];
276 \$high = \$vsns[\$\#vsns - 1];
277 return reply_packet (\$rpc->xid, MSG_DENIED, RPC_MISMATCH,
278 unsigned (\$low) . unsigned (\$high));
279 }
280  
281 \$t = \$t->{\$rpc->prog};
282 if (! defined \$t)
283 {
284 # Bad program.
285 return reply_packet (\$rpc->xid, MSG_ACCEPTED, PROG_UNAVAIL);
286 }
287  
288 my (\$prog) = \$t;
289 \$t = \$t->{\$rpc->vers};
290 if (! defined \$t)
291 {
292 # Bad version.
293 my (\@vsns, \$low, \$high) = sort keys \%{\$prog};
294 \$low = \$vsns[0];
295 \$high = \$vsns[\$\#vsns - 1];
296 return reply_packet (\$rpc->xid, MSG_ACCEPTED, PROG_MISMATCH,
297 unsigned (\$low) . unsigned (\$high));
298 }
299  
300 \$t = \$t->{\$rpc->proc};
301 if (! defined \$t)
302 {
303 # Bad procedure.
304 return reply_packet (\$rpc->xid, MSG_ACCEPTED, PROC_UNAVAIL);
305 }
306  
307 my (\$hook, \$progt, \$vers, \$proc,
308 \$ret, \$name, \@pto)
309 = @\$t;
310  
311 # Invoke the reply hook with the correct arguments.
312 \@proto = \@pto;
313 \$bad = reply_packet (\$rpc->xid, MSG_ACCEPTED, GARBAGE_ARGS);
314 \$func = \$hook;
315 }
316 else
317 {
318 # Reply packet.
319 my (\$hook, \$prog, \$vers, \$proc, \$ret) =
320 \@{\$slf->[1]->{\$rpc->xid}};
321  
322 # Not waiting for that reply xid.
323 return \$bad if (! defined \$hook);
324  
325 # Reply hooks are one-shot.
326 delete \$slf->[1]->{\$rpc->xid};
327  
328 \$bad = 1;
329 \@proto = (\$ret);
330 \$func = \$hook;
331 }
332  
333 # Call the hook.
334 push \@args, eval '\$rpc->args (\$_xdr_callreply, \@proto)';
335 return \$bad if \$@;
336  
337 return &\$func (\$rpc, \@args);
338 }
339 ";
340 }
341  
342 # Actually define the stub.
343 4 $stub .= "
344  
345  
346 sub $name # ($proto)
347 {
348 my (\$slf) = shift;
349 my \$callrep = \$slf->[0];
350 if (\$callrep == 0)
351 {
352 # Return the call packet.
353 carp '$pkg->call->$name received ', \$#_ + 1,
354 \" arguments instead of $nargs\\n\"
355 if (\$#_ != $nargs - 1);
356 call_packet (\$slf->[1] ++, $proc, $arg,
357 \$_xdr_callreply->{VERSION},
358 \$_xdr_callreply->{PROGRAM});
359 }
360 elsif (\$callrep == 1)
361 {
362 # Return the reply arguments.
363 carp '$pkg->reply->$name received ', \$#_ + 1,
364 \" results instead of $nres\\n\"
365 if (\$#_ != $nres - 1);
366 $res;
367 }
368 else
369 {
370 # Return the callrep specification.
371 [\$callrep->{PROGRAM}, \$callrep->{VERSION}, $proc,
372 \@{\$callrep->{$proc}}];
373 }
374 }";
375  
376 # warn "FIXME!\n", $stub;
377 0 ▶ eval $stub;
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine ADDCAT: called CC=5 SCAR=16.6
Subroutine DATA: called CC=5 SCAR=16.6
Subroutine NULL: called CC=5 SCAR=16.6
Subroutine PRODUCE: called CC=5 SCAR=16.6
Subroutine call: called CC=1 SCAR=0.1
Subroutine dispatch: called CC=9 SCAR=22.9
Subroutine hook: called CC=3 SCAR=11.3
Subroutine hookdb: called CC=1 SCAR=0.1
Subroutine reply: called CC=1 SCAR=0.1
Branch: 26 branches
BranchTrueFalseTotal
if ($callrep == 0) { } 1 3 4
elsif ($callrep == 1) { } 1 2 3
if $#_ != 1 0 1 1
if $#_ != 0 0 1 1
if ($callrep == 0) { } 1 1 2
elsif ($callrep == 1) { } 0 1 1
if $#_ != 0 0 1 1
if $#_ != -1 0 0 0
if ($callrep == 0) { } 1 1 2
elsif ($callrep == 1) { } 0 1 1
if $#_ != -1 0 1 1
if $#_ != -1 0 0 0
if ($callrep == 0) { } 1 3 4
elsif ($callrep == 1) { } 1 2 3
if $#_ != 1 0 1 1
if $#_ != 0 0 1 1
unless UNIVERSAL::isa($rpc, "XDR::RPC") 6 0 6
if ($rpc->can('rpcvers')) { } 4 2 6
unless (defined $t) 0 4 4
unless (defined $t) 0 4 4
unless (defined $t) 0 4 4
unless (defined $t) 0 4 4
unless defined $hook 0 2 2
if $@ 0 6 6
if (defined $xid) { } 2 4 6
unless $xid =~ /^d+$/ 2 0 2
378 4 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
379 }
380  
381 1 1;
↑ Summary XDR/RPC/Reply.pm »