| 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 | 4 | require 5.004; | |||||||||||||||||||||||||||
| 18 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 4 | require Storable; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | package RPC::PlServer::Comm; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 4 | $RPC::PlServer::Comm::VERSION = '0.1003'; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | ############################################################################ | ||||||||||||||||||||||||||||
| 30 | # | ||||||||||||||||||||||||||||
| 31 | # Name: new (Class method) | ||||||||||||||||||||||||||||
| 32 | # | ||||||||||||||||||||||||||||
| 33 | # Purpose: Constructor | ||||||||||||||||||||||||||||
| 34 | # | ||||||||||||||||||||||||||||
| 35 | # Inputs: $class - This class | ||||||||||||||||||||||||||||
| 36 | # $attr - Hash ref of attributes | ||||||||||||||||||||||||||||
| 37 | # | ||||||||||||||||||||||||||||
| 38 | # Result: Server object for success, error message otherwise | ||||||||||||||||||||||||||||
| 39 | # | ||||||||||||||||||||||||||||
| 40 | ############################################################################ | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 5 | ▶ | sub new ($) { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=22.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | 5 | my($class, $attr) = @_; | |||||||||||||||||||||||||||
| 44 | 5 | my $self = {}; | |||||||||||||||||||||||||||
| 45 | 5 | ▶ | bless($self, (ref($class) || $class)); | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 5 | ▶ | if (my $comp = $attr->{'compression'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 2 | ▶ | if ($comp eq 'off') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 49 | 0 | $self->{'compression'} = undef; | |||||||||||||||||||||||||||
| 50 | } elsif ($comp eq 'gzip') { | ||||||||||||||||||||||||||||
| 51 | 2 | require Compress::Zlib; | |||||||||||||||||||||||||||
| 52 | 2 | $self->{'compression'} = 'gzip'; | |||||||||||||||||||||||||||
| 53 | } else { | ||||||||||||||||||||||||||||
| 54 | 0 | die "Unknown compression type ($comp), use 'off' or 'gzip'"; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 5 | ▶ | if (my $cipher = $attr->{'cipher'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | $self->{'cipher'} = $cipher; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 5 | ▶ | if (my $maxmessage = $attr->{'maxmessage'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | $self->{'maxmessage'} = $maxmessage; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 5 | $self; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | ############################################################################ | ||||||||||||||||||||||||||||
| 69 | # | ||||||||||||||||||||||||||||
| 70 | # Name: Write | ||||||||||||||||||||||||||||
| 71 | # | ||||||||||||||||||||||||||||
| 72 | # Purpose: Writing to a PlRPC socket; used by both the client (when | ||||||||||||||||||||||||||||
| 73 | # sending a method name and arguments) and the server (for | ||||||||||||||||||||||||||||
| 74 | # sending the result list). Communication occurrs in packets. | ||||||||||||||||||||||||||||
| 75 | # Each packet is preceeded by 4 bytes with the true packet | ||||||||||||||||||||||||||||
| 76 | # size. If encryption happens, then the packet is padded with | ||||||||||||||||||||||||||||
| 77 | # NUL bytes to a multiple of blocksize bytes. However, the | ||||||||||||||||||||||||||||
| 78 | # stored size remains unchanged. | ||||||||||||||||||||||||||||
| 79 | # | ||||||||||||||||||||||||||||
| 80 | # Inputs: $self - Instance of RPC::PlServer or RPC::PlClient | ||||||||||||||||||||||||||||
| 81 | # $socket - The socket to write to | ||||||||||||||||||||||||||||
| 82 | # $args - Reference to array of arguments being sent | ||||||||||||||||||||||||||||
| 83 | # | ||||||||||||||||||||||||||||
| 84 | # Result: Nothing; dies in case of errors. | ||||||||||||||||||||||||||||
| 85 | # | ||||||||||||||||||||||||||||
| 86 | ############################################################################ | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 42 | ▶ | sub Write ($$$) { | ||||||||||||||||||||||||||
|
Subroutine
Write: called CC=7 SCAR=27.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 89 | 42 | my($self, $socket, $msg) = @_; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 42 | my $encodedMsg = Storable::nfreeze($msg); | |||||||||||||||||||||||||||
| 92 | $encodedMsg = Compress::Zlib::compress($encodedMsg) | ||||||||||||||||||||||||||||
| 93 | 42 | ▶ | if ($self->{'compression'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 42 | my($encodedSize) = length($encodedMsg); | |||||||||||||||||||||||||||
| 96 | 42 | ▶ | if (my $cipher = $self->{'cipher'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | my $size = $cipher->blocksize; | |||||||||||||||||||||||||||
| 98 | 0 | ▶ | if (my $addSize = length($encodedMsg) % $size) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | $encodedMsg .= chr(0) x ($size - $addSize); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 0 | $msg = ''; | |||||||||||||||||||||||||||
| 102 | 0 | ▶ | for (my $i = 0; $i < length($encodedMsg); $i += $size) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | $msg .= $cipher->encrypt(substr($encodedMsg, $i, $size)); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 0 | $encodedMsg = $msg; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 42 | local $\; | |||||||||||||||||||||||||||
| 109 | 42 | ▶ | if (!$socket->print(pack("N", $encodedSize), $encodedMsg) || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$socket->print(pack('N', $encodedSize), $encodedMsg) && $socket->flush
MC/DC0%0/2
$socket->print(pack('N', $encodedSize), $encodedMsg) && $socket->flush
$socket->print(pack('N', $encodedSize), $encodedMsg)
$socket->flush
Truth table33%1/3
A && B
A= $socket->print(pack('N', $encodedSize), $encodedMsg)B= $socket->flush
| |||||||||||||||||||||||||||||
| 110 | !$socket->flush()) { | ||||||||||||||||||||||||||||
| 111 | 0 | die "Error while writing socket: $!"; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | ############################################################################ | ||||||||||||||||||||||||||||
| 117 | # | ||||||||||||||||||||||||||||
| 118 | # Name: Read | ||||||||||||||||||||||||||||
| 119 | # | ||||||||||||||||||||||||||||
| 120 | # Purpose: Reading from a PlRPC socket; used by both the client (when | ||||||||||||||||||||||||||||
| 121 | # receiving a result list) and the server (for receiving the | ||||||||||||||||||||||||||||
| 122 | # method name and arguments). Counterpart of Write, see | ||||||||||||||||||||||||||||
| 123 | # above for specs. | ||||||||||||||||||||||||||||
| 124 | # | ||||||||||||||||||||||||||||
| 125 | # Inputs: $self - Instance of RPC::PlServer or RPC::PlClient | ||||||||||||||||||||||||||||
| 126 | # $socket - The socket to read from | ||||||||||||||||||||||||||||
| 127 | # | ||||||||||||||||||||||||||||
| 128 | # Result: Array ref to result list; dies in case of errors. | ||||||||||||||||||||||||||||
| 129 | # | ||||||||||||||||||||||||||||
| 130 | ############################################################################ | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 42 | ▶ | sub Read($$) { | ||||||||||||||||||||||||||
|
Subroutine
Read: called CC=14 SCAR=34.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 133 | 42 | my($self, $socket) = @_; | |||||||||||||||||||||||||||
| 134 | 42 | my $result; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 42 | my($encodedSize, $readSize, $blockSize); | |||||||||||||||||||||||||||
| 137 | 42 | $readSize = 4; | |||||||||||||||||||||||||||
| 138 | 42 | $encodedSize = ''; | |||||||||||||||||||||||||||
| 139 | 42 | ▶ | while ($readSize > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 42 | my $result = $socket->read($encodedSize, $readSize, | |||||||||||||||||||||||||||
| 141 | length($encodedSize)); | ||||||||||||||||||||||||||||
| 142 | 42 | ▶ | if (!$result) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | return undef if defined($result); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | die "Error while reading socket: $!"; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 42 | $readSize -= $result; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 42 | $encodedSize = unpack("N", $encodedSize); | |||||||||||||||||||||||||||
| 149 | 42 | my $max = $self->getMaxMessage(); | |||||||||||||||||||||||||||
| 150 | 42 | ▶ | die "Maximum message size of $max exceeded, use option 'maxmessage' to" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$max and $encodedSize > $max
MC/DC0%0/2
$max and $encodedSize > $max
$max
$encodedSize > $max
Truth table33%1/3
A and B
A= $maxB= $encodedSize > $max
| |||||||||||||||||||||||||||||
| 151 | . " increase" if $max && $encodedSize > $max; | ||||||||||||||||||||||||||||
| 152 | 42 | $readSize = $encodedSize; | |||||||||||||||||||||||||||
| 153 | 42 | ▶ | if ($self->{'cipher'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | $blockSize = $self->{'cipher'}->blocksize; | |||||||||||||||||||||||||||
| 155 | 0 | ▶ | if (my $addSize = ($encodedSize % $blockSize)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | $readSize += ($blockSize - $addSize); | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | 42 | my $msg = ''; | |||||||||||||||||||||||||||
| 160 | 42 | my $rs = $readSize; | |||||||||||||||||||||||||||
| 161 | 42 | ▶ | while ($rs > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 42 | my $result = $socket->read($msg, $rs, length($msg)); | |||||||||||||||||||||||||||
| 163 | 42 | ▶ | if (!$result) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | ▶ | die "Unexpected EOF" if defined $result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 0 | die "Error while reading socket: $!"; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | 42 | $rs -= $result; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | 42 | ▶ | if ($self->{'cipher'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 0 | my $cipher = $self->{'cipher'}; | |||||||||||||||||||||||||||
| 171 | 0 | my $encodedMsg = $msg; | |||||||||||||||||||||||||||
| 172 | 0 | $msg = ''; | |||||||||||||||||||||||||||
| 173 | 0 | ▶ | for (my $i = 0; $i < $readSize; $i += $blockSize) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | $msg .= $cipher->decrypt(substr($encodedMsg, $i, $blockSize)); | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | 0 | $msg = substr($msg, 0, $encodedSize); | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | 42 | ▶ | $msg = Compress::Zlib::uncompress($msg) if ($self->{'compression'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 42 | Storable::thaw($msg); | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | ############################################################################ | ||||||||||||||||||||||||||||
| 184 | # | ||||||||||||||||||||||||||||
| 185 | # Name: Init | ||||||||||||||||||||||||||||
| 186 | # | ||||||||||||||||||||||||||||
| 187 | # Purpose: Initialize an object for using RPC::PlServer::Comm methods | ||||||||||||||||||||||||||||
| 188 | # | ||||||||||||||||||||||||||||
| 189 | # Input: $self - Instance | ||||||||||||||||||||||||||||
| 190 | # | ||||||||||||||||||||||||||||
| 191 | # Returns: The instance in case of success, dies in case of trouble. | ||||||||||||||||||||||||||||
| 192 | # | ||||||||||||||||||||||||||||
| 193 | ############################################################################ | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | ############################################################################ | ||||||||||||||||||||||||||||
| 196 | # | ||||||||||||||||||||||||||||
| 197 | # Name: getMaxMessage | ||||||||||||||||||||||||||||
| 198 | # | ||||||||||||||||||||||||||||
| 199 | # Purpose: Returns the maximum size of a message | ||||||||||||||||||||||||||||
| 200 | # | ||||||||||||||||||||||||||||
| 201 | # Inputs: None | ||||||||||||||||||||||||||||
| 202 | # | ||||||||||||||||||||||||||||
| 203 | # Returns: Maximum message size or 65536, if none specified | ||||||||||||||||||||||||||||
| 204 | # | ||||||||||||||||||||||||||||
| 205 | ############################################################################ | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 0 | ▶ | sub getMaxMessage() { | ||||||||||||||||||||||||||
|
Subroutine
getMaxMessage: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 208 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 209 | return defined($self->{'maxmessage'}) ? | ||||||||||||||||||||||||||||
| 210 | 0 | ▶ | $self->{'maxmessage'} : 65536; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 4 | 1; | |||||||||||||||||||||||||||