RPC/PlServer.pm

filter: Statementstmt 8.0% 11 / 137 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 13.3% 2 / 15 Podpod 0.0% 0 / 13 totaltotal 4.8% 13 / 269 CC 46 SCAR 52.9
CC
46
Cov
5%
Accept
462.0
Run
132.0
CallMethod
56.0
CRAP 197.6
↑ Summary RPC/PlServer/Comm.pm »
1 # -*- perl -*-
2 #
3 #
4 # PlRPC - Perl RPC, package for writing simple, RPC like clients and
5 # servers
6 #
7 #
8 # Copyright (c) 1997,1998 Jochen Wiedmann
9 #
10 # You may distribute under the terms of either the GNU General Public
11 # License or the Artistic License, as specified in the Perl README file.
12 #
13 # Author: Jochen Wiedmann
14 # Email: jochen.wiedmann at freenet.de
15 #
16 #
17  
18 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 require Net::Daemon;
21 1 require RPC::PlServer::Comm;
22  
23  
24 package RPC::PlServer;
25  
26 1 @RPC::PlServer::ISA = qw(Net::Daemon);
27 1 $RPC::PlServer::VERSION = '0.2020';
28  
29  
30 ############################################################################
31 #
32 # Name: Version (Class method)
33 #
34 # Purpose: Returns version string
35 #
36 # Inputs: $class - This class
37 #
38 # Result: Version string; suitable for printed by "--version"
39 #
40 ############################################################################
41  
42 0 ▶sub Version ($) {
Subroutine Version: not called CC=1 SCAR=6.9
Pod undocumented
43 0 "RPC::PlServer application, Copyright (C) 1997, 1998, Jochen Wiedmann";
44 }
45  
46  
47 ############################################################################
48 #
49 # Name: Options (Class method)
50 #
51 # Purpose: Returns a hash ref of command line options
52 #
53 # Inputs: $class - This class
54 #
55 # Result: Options array; any option is represented by a hash ref;
56 # used keys are 'template', a string suitable for describing
57 # the option to Getopt::Long::GetOptions and 'description',
58 # a string for the Usage message
59 #
60 ############################################################################
61  
62 0 ▶sub Options ($) {
Subroutine Options: not called CC=1 SCAR=6.9
Pod undocumented
63 0 my $options = shift->SUPER::Options();
64 0 $options->{'maxmessage'} =
65 { 'template' => 'maxmessage=i',
66 'description' => '--maxmessage <size> '
67 . 'Set max message size to <size> (Default 65535).'
68 };
69 0 $options->{'compression'} =
70 { 'template' => 'compression=s',
71 'description' => '--compression <type> '
72 . 'Set compression type to off (default) or gzip.'
73 };
74 0 $options;
75 }
76  
77  
78 ############################################################################
79 #
80 # Name: AcceptApplication, AcceptVersion, AcceptUser
81 # (Instance methods)
82 #
83 # Purpose: Called for authentication purposes; these three in common
84 # are replacing Net::Daemon's Accept().
85 #
86 # Inputs: $self - Server instance
87 # $app - Application name
88 # $version - Version number
89 # $user, $password - User name and password
90 #
91 # Result: TRUE, if the client has successfully authorized, FALSE
92 # otherwise. The AcceptUser method (being called as the
93 # last) may additionally return an array ref as a TRUE
94 # value: This is treated as welcome message.
95 #
96 ############################################################################
97  
98 0 ▶sub AcceptApplication ($$) {
Subroutine AcceptApplication: not called CC=1 SCAR=6.9
Pod undocumented
99 0 my $self = shift; my $app = shift;
100 0 $self->Debug("Client requests application $app");
101 0 UNIVERSAL::isa($self, $app);
102 }
103  
104 0 ▶sub AcceptVersion ($$) {
Subroutine AcceptVersion: not called CC=2 SCAR=12.6
Pod undocumented
105 0 my $self = shift; my $version = shift;
106 0 $self->Debug("Client requests version $version");
107 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
108 0 my $myversion = ${ref($self) . "::VERSION"};
109 0 ▶ ($version <= $myversion) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$version <= $myversion ? : 0 0 0
110 }
111  
112 0 ▶sub AcceptUser ($$$) {
Subroutine AcceptUser: not called CC=6 SCAR=37.4
Pod undocumented
113 0 my $self = shift; my $user = shift; my $password = shift;
114  
115 0 my $client = $self->{'client'};
116 0 ▶ return 1 unless $client->{'users'};
Branch: 1 branch
BranchTrueFalseTotal
unless $client->{"users"} 0 0 0
117 0 my $users = $client->{'users'};
118 0 foreach my $u (@$users) {
119 0 my $au;
120 0 ▶ if (ref($u)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $u) 0 0 0
121 0 $au = $u;
122 0 ▶ $u = defined($u->{'name'}) ? $u->{'name'} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $u->{'name'} ? : 0 0 0
123 }
124 0 ▶ if ($u eq $user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($u eq $user) 0 0 0
125 0 $self->{'authorized_user'} = $au;
126 0 return 1;
127 }
128 }
129 0 0;
130 }
131  
132 0 ▶sub Accept ($) {
Subroutine Accept: not called CC=21 SCAR=61.4
Pod undocumented
133 0 my $self = shift;
134 0 my $socket = $self->{'socket'};
135 0 my $comm = $self->{'comm'};
136 0 ▶ return 0 if (!$self->SUPER::Accept());
Branch: 1 branch
BranchTrueFalseTotal
unless $self->SUPER::Accept 0 0 0
137 0 my $client;
138 0 ▶ if ($client = $self->{'client'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client = $self->{"client"}) 0 0 0
139 0 ▶ if (my $cipher = $client->{'cipher'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cipher = $client->{"cipher"}) 0 0 0
140 0 $self->Debug("Host encryption: %s", $cipher);
141 0 $self->{'cipher'} = $cipher;
142 }
143 }
144  
145 0 my $msg = $comm->Read($socket);
146 0 ▶ die "Unexpected EOF from client" unless defined $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg 0 0 0
147 0 ▶ die "Login message: Expected array, got $msg" unless ref($msg) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $msg eq "ARRAY" 0 0 0
148  
149 0 ▶ my $app = $self->{'application'} = $msg->[0] || '';
Condition0%0/2
$msg->[0] || ''
l!l
00
MC/DC0%0/1
$msg->[0] || ''
$msg->[0]
Truth table0%0/2
A
A= $msg->[0]
Aresult
11
00
150 0 ▶ my $version = $self->{'version'} = $msg->[1] || 0;
Condition0%0/2
$msg->[1] || 0
l!l
00
MC/DC0%0/1
$msg->[1] || 0
$msg->[1]
Truth table0%0/2
A
A= $msg->[1]
Aresult
11
00
151 0 ▶ my $user = $self->{'user'} = $msg->[2] || '';
Condition0%0/2
$msg->[2] || ''
l!l
00
MC/DC0%0/1
$msg->[2] || ''
$msg->[2]
Truth table0%0/2
A
A= $msg->[2]
Aresult
11
00
152 0 ▶ my $password = $self->{'password'} = $msg->[3] || '';
Condition0%0/2
$msg->[3] || ''
l!l
00
MC/DC0%0/1
$msg->[3] || ''
$msg->[3]
Truth table0%0/2
A
A= $msg->[3]
Aresult
11
00
153  
154 0 $self->Debug("Client logs in: Application %s, version %s, user %s",
155 $app, $version, $user);
156  
157 0 ▶ if (!$self->AcceptApplication($app)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->AcceptApplication($app)) 0 0 0
158 0 $comm->Write($socket,
159 [0, "This is a " . ref($self) . " server, go away!"]);
160 0 return 0;
161 }
162 0 ▶ if (!$self->AcceptVersion($version)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->AcceptVersion($version)) 0 0 0
163 0 $comm->Write($socket,
164 [0, "Sorry, but I am not running version $version."]);
165 0 return 0;
166 }
167 0 my $result;
168 0 ▶ if (!($result = $self->AcceptUser($user, $password))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result = $self->AcceptUser($user, $password)) 0 0 0
169 0 $comm->Write($socket,
170 [0, "User $user is not permitted to connect."]);
171 0 return 0;
172 }
173 0 ▶ $comm->Write($socket, (ref($result) ? $result : [1, "Welcome!"]));
Branch: 1 branch
BranchTrueFalseTotal
ref $result ? : 0 0 0
174 0 ▶ if (my $au = $self->{'authorized_user'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $au = $self->{"authorized_user"}) 0 0 0
175 0 ▶ if (ref($au) && (my $cipher = $au->{'cipher'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $au and my $cipher = $au->{"cipher"}) 0 0 0
Condition0%0/3
ref $au and my $cipher = $au->{"cipher"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $au and my $cipher = $au->{"cipher"}
ref $au my $cipher = $au->{"cipher"}
Truth table0%0/3
A and B
A= ref $auB= my $cipher = $au->{"cipher"}
ABresult
0X0
100
111
176 0 $self->Debug("User encryption: %s", $cipher);
177 0 $self->{'cipher'} = $cipher;
178 }
179 }
180  
181 0 ▶ if (my $client = $self->{'client'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $client = $self->{"client"}) 0 0 0
182 0 ▶ if (my $methods = $client->{'methods'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $methods = $client->{"methods"}) 0 0 0
183 0 $self->{'methods'} = $methods;
184 }
185 }
186 0 ▶ if (my $au = $self->{'authorized_user'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $au = $self->{"authorized_user"}) 0 0 0
187 0 ▶ if (my $methods = $au->{'methods'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $methods = $au->{"methods"}) 0 0 0
188 0 $self->{'methods'} = $methods;
189 }
190 }
191  
192 0 1;
193 }
194  
195  
196 ############################################################################
197 #
198 # Name: new (Class method)
199 #
200 # Purpose: Constructor
201 #
202 # Inputs: $class - This class
203 # $attr - Hash ref of attributes
204 # $args - Array ref of command line arguments
205 #
206 # Result: Server object for success, error message otherwise
207 #
208 ############################################################################
209  
210 0 ▶sub new ($$;$) {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
211 0 my $self = shift->SUPER::new(@_);
212 0 $self->{'comm'} = RPC::PlServer::Comm->new($self);
213 0 $self;
214 }
215  
216  
217 ############################################################################
218 #
219 # Name: Run
220 #
221 # Purpose: Process client requests
222 #
223 # Inputs: $self - Server instance
224 #
225 # Returns: Nothing, dies in case of errors.
226 #
227 ############################################################################
228  
229 0 ▶sub Run ($) {
Subroutine Run: not called CC=11 SCAR=48.8
Pod undocumented
230 0 my $self = shift;
231 0 my $comm = $self->{'comm'};
232 0 my $socket = $self->{'socket'};
233  
234 0 ▶ while (!$self->Done()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->Done) 0 0 0
235 0 my $msg = $comm->Read($socket);
236 0 ▶ last unless defined($msg);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg 0 0 0
237 0 ▶ die "Expected array" unless ref($msg) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $msg eq "ARRAY" 0 0 0
238 0 my($error, $command);
239 0 ▶ if (!($command = shift @$msg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $command = shift @$msg) { } 0 0 0
240 0 $error = "Expected method name";
241 } else {
242 0 ▶ if ($self->{'methods'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"methods"}) 0 0 0
243 0 my $class = $self->{'methods'}->{ref($self)};
244 0 ▶ if (!$class || !$class->{$command}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class and $class->{$command}) 0 0 0
Condition0%0/3
$class and $class->{$command}
!ll&&!rl&&r
000
MC/DC0%0/2
$class and $class->{$command}
$class $class->{$command}
Truth table0%0/3
A and B
A= $classB= $class->{$command}
ABresult
0X0
100
111
245 0 $error = "Not permitted for method $command of class "
246 . ref($self);
247 }
248 }
249 0 ▶ if (!$error) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error) 0 0 0
250 0 $self->Debug("Client executes method $command");
251 0 my @result = eval { $self->$command(@$msg) };
252 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
253 0 $error = "Failed to execute method $command: $@";
254 } else {
255 0 $comm->Write($socket, \@result);
256 }
257 }
258 }
259 0 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
260 0 $comm->Write($socket, \$error);
261 }
262 }
263 }
264  
265  
266 ############################################################################
267 #
268 # Name: StoreHandle, NewHandle, UseHandle, DestroyHandle,
269 # CallMethod
270 #
271 # Purpose: Support functions for working with objects
272 #
273 # Inputs: $self - server instance
274 # $object - Server side object
275 # $handle - Client side handle
276 #
277 ############################################################################
278  
279 0 ▶sub StoreHandle ($$) {
Subroutine StoreHandle: not called CC=1 SCAR=6.9
Pod undocumented
280 0 my $self = shift; my $object = shift;
281 0 my $handle = "$object";
282 0 $self->{'handles'}->{$handle} = $object;
283 0 $handle;
284 }
285  
286 0 ▶sub NewHandle ($$$@) {
Subroutine NewHandle: not called CC=2 SCAR=17.9
Pod undocumented
287 0 my($self, $handle, $method, @args) = @_;
288 0 my $object = $self->CallMethod($handle, $method, @args);
289 0 ▶ die "Constructor $method didn't return a true value" unless $object;
Branch: 1 branch
BranchTrueFalseTotal
unless $object 0 0 0
290 0 $self->StoreHandle($object)
291 }
292  
293 0 ▶sub UseHandle ($$) {
Subroutine UseHandle: not called CC=2 SCAR=17.9
Pod undocumented
294 0 my $self = shift; my $handle = shift;
295 0 ▶ $self->{'handles'}->{$handle} || die "No such object: $handle";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"handles"}{$handle} or die "No such object: $handle" 0 0 0
296 }
297  
298 0 ▶sub DestroyHandle ($$) {
Subroutine DestroyHandle: not called CC=2 SCAR=17.9
Pod undocumented
299 0 my $self = shift; my $handle = shift;
300 0 ▶ (delete $self->{'handles'}->{$handle}) || die "No such object: $handle";
Branch: 1 branch
BranchTrueFalseTotal
delete $self->{"handles"}{$handle} or die "No such object: $handle" 0 0 0
301 0 ();
302 }
303  
304 0 ▶sub CallMethod ($$$@) {
Subroutine CallMethod: not called CC=7 SCAR=40.3
Pod undocumented
305 0 my($self, $handle, $method, @args) = @_;
306 0 my($ref, $object);
307  
308 0 my $call_by_instance;
309 {
310 0 my $lock = lock($Net::Daemon::RegExpLock)
311 0 ▶ if $Net::Daemon::RegExpLock && $self->{'mode'} eq 'threads';
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Daemon::RegExpLock and $self->{"mode"} eq "threads" 0 0 0
Condition0%0/3
$Net::Daemon::RegExpLock and $self->{"mode"} eq "threads"
!ll&&!rl&&r
000
MC/DC0%0/2
$Net::Daemon::RegExpLock and $self->{"mode"} eq "threads"
$Net::Daemon::RegExpLock $self->{"mode"} eq "threads"
Truth table0%0/3
A and B
A= $Net::Daemon::RegExpLockB= $self->{"mode"} eq "threads"
ABresult
0X0
100
111
312 0 $call_by_instance = ($handle =~ /=\w+\(0x/);
313 }
314 0 ▶ if ($call_by_instance) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call_by_instance) { } 0 0 0
315 # Looks like a call by instance
316 0 $object = $self->UseHandle($handle);
317 0 $ref = ref($object);
318 } else {
319 # Call by class
320 0 $ref = $object = $handle;
321 }
322  
323 0 ▶ if ($self->{'methods'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"methods"}) 0 0 0
324 0 my $class = $self->{'methods'}->{$ref};
325 0 ▶ if (!$class || !$class->{$method}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class and $class->{$method}) 0 0 0
Condition0%0/3
$class and $class->{$method}
!ll&&!rl&&r
000
MC/DC0%0/2
$class and $class->{$method}
$class $class->{$method}
Truth table0%0/3
A and B
A= $classB= $class->{$method}
ABresult
0X0
100
111
326 0 die "Not permitted for method $method of class $ref";
327 }
328 }
329  
330 0 $object->$method(@args);
331 }
332  
333  
334 1 1;
335  
336  
337 __END__
↑ Summary RPC/PlServer/Comm.pm »