| 1 | # Ser::BinRPC.pm | ||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||
| 3 | # Copyright (c) 2010 Tomas Mandys <tomas.mandys@2p.cz>. All rights reserved. | ||||||||||||||||||||||||||||||
| 4 | # This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||||
| 5 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | package Ser::BinRPC; | ||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | #no strict "refs"; | ||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Socket; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use IO::Socket; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||
| 16 | require Exporter; | ||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||
| 18 | our @ISA = qw(Exporter); | ||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||
| 20 | # Items to export into callers namespace by default. Note: do not export | ||||||||||||||||||||||||||||||
| 21 | # names by default without a very good reason. Use EXPORT_OK instead. | ||||||||||||||||||||||||||||||
| 22 | # Do not simply export all your public functions/methods/constants. | ||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||
| 24 | # This allows declaration use Ser::BinRPC ':all'; | ||||||||||||||||||||||||||||||
| 25 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||||||||||||||||||||||||||||||
| 26 | # will save memory. | ||||||||||||||||||||||||||||||
| 27 | our %EXPORT_TAGS = ( 'all' => [ qw( | ||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||
| 29 | ) ] ); | ||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||
| 31 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||
| 33 | our @EXPORT = qw( | ||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||
| 37 | our $VERSION = '0.01'; | ||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||
| 39 | require XSLoader; | ||||||||||||||||||||||||||||||
| 40 | XSLoader::load('Ser::BinRPC', $VERSION); | ||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||
| 42 | my %data_type = ( | ||||||||||||||||||||||||||||||
| 43 | INT => 0, | ||||||||||||||||||||||||||||||
| 44 | STR => 1, # 0 term, for easier parsing | ||||||||||||||||||||||||||||||
| 45 | DOUBLE => 2, | ||||||||||||||||||||||||||||||
| 46 | STRUCT => 3, | ||||||||||||||||||||||||||||||
| 47 | ARRAY => 4, | ||||||||||||||||||||||||||||||
| 48 | AVP => 5, # allowed only in structs | ||||||||||||||||||||||||||||||
| 49 | BYTES => 6 # like STR, but not 0 term | ||||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||
| 52 | my %message_type = ( | ||||||||||||||||||||||||||||||
| 53 | REQ => 0, | ||||||||||||||||||||||||||||||
| 54 | REPL => 1, | ||||||||||||||||||||||||||||||
| 55 | FAULT => 3 | ||||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||
| 58 | my %sock_type_by_proto = ( | ||||||||||||||||||||||||||||||
| 59 | udp => SOCK_DGRAM, | ||||||||||||||||||||||||||||||
| 60 | tcp => SOCK_STREAM | ||||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||
| 63 | # ------------------------ module subroutines ------------------------ | ||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | sub binrpc_get_int_len($) { | ||||||||||||||||||||||||||||
|
Subroutine
binrpc_get_int_len: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||
| 66 | 0 | my ($i) = @_; | |||||||||||||||||||||||||||||
| 67 | 0 | my $size; | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | for ($size=4; $size && (($i & (0xff<<24))==0); $i<<=8, $size--) {}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$size and ($i & 4278190080) == 0
MC/DC0%0/2
$size and ($i & 4278190080) == 0
$size
($i & 4278190080) == 0
Truth table0%0/3
A and B
A= $sizeB= ($i & 4278190080) == 0
| |||||||||||||||||||||||||||||||
| 69 | 0 | return $size; | |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub binrpc_write_int(\@$) { | ||||||||||||||||||||||||||||
|
Subroutine
binrpc_write_int: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||
| 73 | 0 | my ($arr, $i) = @_; | |||||||||||||||||||||||||||||
| 74 | 0 | my $size; | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | for ($size=4; $size && (($i & (0xff<<24))==0); $i<<=8, $size--) {}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$size and ($i & 4278190080) == 0
MC/DC0%0/2
$size and ($i & 4278190080) == 0
$size
($i & 4278190080) == 0
Truth table0%0/3
A and B
A= $sizeB= ($i & 4278190080) == 0
| |||||||||||||||||||||||||||||||
| 76 | 0 | my $n; | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | for ($n=$size; $n; $n--){ | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 78 | 0 | push(@$arr, (($i>>24) & 0xFF)); | |||||||||||||||||||||||||||||
| 79 | 0 | $i<<=8; | |||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||
| 81 | 0 | return $size; | |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | sub binrpc_read_int(\@$\$) { | ||||||||||||||||||||||||||||
|
Subroutine
binrpc_read_int: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||
| 85 | 0 | my ($arr, $len, $i) = @_; | |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | if ($len > scalar(@$arr)) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 88 | 0 | return 0; | |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||
| 90 | 0 | $$i = 0; | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | for (; $len>0; $len--) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 92 | 0 | $$i <<= 8; | |||||||||||||||||||||||||||||
| 93 | 0 | $$i |= shift(@$arr); | |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||
| 95 | 0 | return 1; | |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub binrpc_add_str(\@$) { | ||||||||||||||||||||||||||||
|
Subroutine
binrpc_add_str: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||
| 99 | 0 | my ($arr, $s) = @_; | |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||
| 101 | 0 | my $l = length($s)+1; | |||||||||||||||||||||||||||||
| 102 | 0 | my $size; | |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if ($l < 8) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 104 | 0 | push(@$arr, ($l << 4) | $data_type{STR}); | |||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||
| 106 | else { | ||||||||||||||||||||||||||||||
| 107 | 0 | push(@$arr, (binrpc_get_int_len($l) << 4) | 0x80 | $data_type{STR}); | |||||||||||||||||||||||||||||
| 108 | 0 | binrpc_write_int(@$arr, $l); | |||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||
| 110 | 0 | for my $i (0 .. length($s)-1) { | |||||||||||||||||||||||||||||
| 111 | 0 | push(@$arr, unpack('C', substr($s, $i, 1))); | |||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||
| 113 | 0 | push(@$arr, 0); | |||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub binrpc_add_int(\@$) { | ||||||||||||||||||||||||||||
|
Subroutine
binrpc_add_int: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 117 | 0 | my ($arr, $x) = @_; | |||||||||||||||||||||||||||||
| 118 | 0 | push(@$arr, (binrpc_get_int_len($x) << 4) | $data_type{INT}); | |||||||||||||||||||||||||||||
| 119 | 0 | binrpc_write_int(@$arr, $x); | |||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | sub binrpc_add_double(\@$) { | ||||||||||||||||||||||||||||
|
Subroutine
binrpc_add_double: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 123 | 0 | my ($arr, $x) = @_; | |||||||||||||||||||||||||||||
| 124 | 0 | $x = int($x*1000); | |||||||||||||||||||||||||||||
| 125 | 0 | push(@$arr, binrpc_get_int_len($x) <<4 | $data_type{DOUBLE}); | |||||||||||||||||||||||||||||
| 126 | 0 | binrpc_write_int(@$arr, $x); | |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||
| 129 | sub binrpc_read_record(\@$\$\$\$); # forward declaration to introduce prototype for recursive usage | ||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | sub binrpc_read_record(\@$\$\$\$) { | ||||||||||||||||||||||||||||
|
Subroutine
binrpc_read_record: not called CC=35 SCAR=71.4
| |||||||||||||||||||||||||||||||
| 132 | 0 | my ($arr, $nesting, $val_type, $val, $name) = @_; | |||||||||||||||||||||||||||||
| 133 | 0 | my $end_tag = 0; | |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | if (scalar(@$arr) == 0) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 136 | 0 | return 0; | |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||
| 138 | 0 | my $len = shift(@$arr); | |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | if (defined $$val_type && (($len & 0x0F) != $$val_type)) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $$val_type and ($len & 15) != $$val_type
MC/DC0%0/2
defined $$val_type and ($len & 15) != $$val_type
defined $$val_type
($len & 15) != $$val_type
Truth table0%0/3
A and B
A= defined $$val_typeB= ($len & 15) != $$val_type
| |||||||||||||||||||||||||||||||
| 140 | 0 | return 0; | |||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||
| 142 | 0 | $$val_type = $len & 0x0F; | |||||||||||||||||||||||||||||
| 143 | 0 | $len >>= 4; | |||||||||||||||||||||||||||||
| 144 | 0 | ▶ | if ($len & 0x08) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 145 | 0 | $end_tag=1; # possible end mark for array or structs | |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | unless (binrpc_read_int(@$arr, $len & 0x07, $len)) { # we have to read len bytes and use them as the new len */ | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 147 | 0 | return 0; | |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | if ($len > scalar(@$arr)) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 151 | 0 | return 0; | |||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | if ($nesting eq 'S') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | if ($$val_type == $data_type{STRUCT} ) { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if ($end_tag) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 156 | 0 | undef $$val_type; # end of struct | |||||||||||||||||||||||||||||
| 157 | 0 | return 1; | |||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||
| 159 | else { | ||||||||||||||||||||||||||||||
| 160 | 0 | return 0; | |||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||
| 162 | } elsif ($$val_type == $data_type{AVP} ) { | ||||||||||||||||||||||||||||||
| 163 | 0 | $$name = ''; | |||||||||||||||||||||||||||||
| 164 | 0 | for my $i (0 .. $len-2) { | |||||||||||||||||||||||||||||
| 165 | 0 | $$name .= pack('C', shift @$arr); | |||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||
| 167 | 0 | shift @$arr; # zero term | |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | if (scalar(@$arr) == 0) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 169 | 0 | return 0; | |||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||
| 172 | 0 | $$val_type = $$arr[0] & 0x0F; | |||||||||||||||||||||||||||||
| 173 | 0 | ▶ | if ($$val_type == $data_type{AVP} || $$val_type == $data_type{ARRAY}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$$val_type == $data_type{"AVP"} or $$val_type == $data_type{"ARRAY"}
MC/DC0%0/2
$$val_type == $data_type{"AVP"} or $$val_type == $data_type{"ARRAY"}
$$val_type == $data_type{"AVP"}
$$val_type == $data_type{"ARRAY"}
Truth table0%0/3
A or B
A= $$val_type == $data_type{"AVP"}B= $$val_type == $data_type{"ARRAY"}
| |||||||||||||||||||||||||||||||
| 174 | 0 | return 0; | |||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||
| 176 | 0 | my ($dummy); | |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | unless (binrpc_read_record(@$arr, '', $$val_type, $$val, $dummy)) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 178 | 0 | return 0; | |||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||
| 180 | 0 | return 1; | |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||
| 182 | } else { | ||||||||||||||||||||||||||||||
| 183 | 0 | return 0; | |||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||
| 186 | else { | ||||||||||||||||||||||||||||||
| 187 | 0 | ▶ | if ($$val_type == $data_type{INT} ) { | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | unless (binrpc_read_int(@$arr, $len, $$val)) { # we have to read len bytes and use them as the new len */ | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 189 | 0 | return 0; | |||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||
| 192 | } elsif ($$val_type == $data_type{DOUBLE} ) { | ||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | unless (binrpc_read_int(@$arr, $len, $$val)) { # we have to read len bytes and use them as the new len */ | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 194 | 0 | return 0; | |||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||
| 196 | 0 | $$val = ($$val*1.00)/1000; | |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||
| 198 | } elsif ($$val_type == $data_type{STR} ) { | ||||||||||||||||||||||||||||||
| 199 | 0 | $$val = ''; | |||||||||||||||||||||||||||||
| 200 | 0 | for my $i (0 .. $len-2) { | |||||||||||||||||||||||||||||
| 201 | 0 | $$val .= pack('C', shift @$arr); | |||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||
| 203 | 0 | shift @$arr; # zero term | |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||
| 205 | } elsif ($$val_type == $data_type{BYTES} ) { | ||||||||||||||||||||||||||||||
| 206 | 0 | $$val = ''; | |||||||||||||||||||||||||||||
| 207 | 0 | for my $i (0 .. $len-1) { | |||||||||||||||||||||||||||||
| 208 | 0 | $$val .= pack('C', shift @$arr); | |||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||
| 211 | } elsif ($$val_type == $data_type{STRUCT} ) { | ||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | if ($end_tag) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 213 | 0 | return 0; | |||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||
| 215 | 0 | my %s = (); | |||||||||||||||||||||||||||||
| 216 | 0 | my ($val_type2, $val2, $name2); | |||||||||||||||||||||||||||||
| 217 | 0 | ▶ | do { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 218 | 0 | undef $val_type2; | |||||||||||||||||||||||||||||
| 219 | 0 | ▶ | unless (binrpc_read_record(@$arr, 'S', $val_type2, $val2, $name2)) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 220 | 0 | return 0; | |||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||
| 222 | 0 | ▶ | if (defined $val_type2) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 223 | 0 | $s{$name2} = $val2; | |||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||
| 225 | } while (defined $val_type2); | ||||||||||||||||||||||||||||||
| 226 | 0 | $$val = \%s; | |||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||
| 228 | } elsif ($$val_type == $data_type{ARRAY} ) { | ||||||||||||||||||||||||||||||
| 229 | 0 | ▶ | if ($end_tag) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 230 | 0 | ▶ | if ($nesting eq 'A') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 231 | 0 | undef $$val_type; | |||||||||||||||||||||||||||||
| 232 | 0 | return 1; | |||||||||||||||||||||||||||||
| 233 | } else { | ||||||||||||||||||||||||||||||
| 234 | 0 | return 0; | |||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||
| 237 | 0 | my @a2 = (); | |||||||||||||||||||||||||||||
| 238 | 0 | my ($val_type2, $val2, $name2); | |||||||||||||||||||||||||||||
| 239 | 0 | ▶ | do { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 240 | 0 | undef $val_type2; | |||||||||||||||||||||||||||||
| 241 | 0 | ▶ | unless (binrpc_read_record(@$arr, 'A', $val_type2, $val2, $name2)) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 242 | 0 | return 0; | |||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||
| 244 | 0 | ▶ | if (defined $val_type2) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 245 | 0 | push (@a2, $val2); | |||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||
| 247 | } while (defined $val_type2); | ||||||||||||||||||||||||||||||
| 248 | 0 | $$val = \@a2; | |||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||
| 250 | } else { | ||||||||||||||||||||||||||||||
| 251 | 0 | return 0; | |||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||
| 254 | 0 | return 1; | |||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||
| 257 | # ----------------------------- begin of object ------------------------------------ | ||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||
| 259 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 260 | 2 | my $class = shift; | |||||||||||||||||||||||||||||
| 261 | 2 | my $self = { | |||||||||||||||||||||||||||||
| 262 | verbose=>0, | ||||||||||||||||||||||||||||||
| 263 | errs=>'', | ||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||
| 265 | sock_domain=>PF_UNIX, | ||||||||||||||||||||||||||||||
| 266 | sock_type=>SOCK_STREAM, | ||||||||||||||||||||||||||||||
| 267 | unix_sock=>'/tmp/ser_ctl', | ||||||||||||||||||||||||||||||
| 268 | remote_host=>'localhost', | ||||||||||||||||||||||||||||||
| 269 | remote_port=>2049, | ||||||||||||||||||||||||||||||
| 270 | proto=>getprotobyname('udp') | ||||||||||||||||||||||||||||||
| 271 | }; | ||||||||||||||||||||||||||||||
| 272 | 2 | return bless($self, $class); | |||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||
| 275 | 7 | ▶ | sub parse_connection_string($$) { | ||||||||||||||||||||||||||||
|
Subroutine
parse_connection_string: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||
| 276 | 7 | my ( $self, $s ) = @_; | |||||||||||||||||||||||||||||
| 277 | 7 | $self->dbg("parse_connection_string($s)"); | |||||||||||||||||||||||||||||
| 278 | 7 | my @flds = split(/:/, $s); | |||||||||||||||||||||||||||||
| 279 | 7 | ▶ | if ($flds[0] eq 'unix') { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 280 | 2 | $self->{sock_domain} = PF_UNIX; | |||||||||||||||||||||||||||||
| 281 | 2 | ▶ | $self->{unix_sock} = $flds[1] if $flds[1]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 282 | 2 | $self->{sock_type} = SOCK_STREAM; | |||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||
| 284 | ▶ | elsif ($flds[0] eq 'udp' || $flds[0] eq 'tcp') { | |||||||||||||||||||||||||||||
|
Condition66%2/3
$flds[0] eq "udp" or $flds[0] eq "tcp"
MC/DC50%1/2
$flds[0] eq "udp" or $flds[0] eq "tcp"
$flds[0] eq "udp"
$flds[0] eq "tcp"
Truth table66%2/3
A or B
A= $flds[0] eq "udp"B= $flds[0] eq "tcp"
| |||||||||||||||||||||||||||||||
| 285 | 4 | my $type; | |||||||||||||||||||||||||||||
| 286 | 4 | $self->{sock_domain} = PF_INET; | |||||||||||||||||||||||||||||
| 287 | 4 | ▶ | $self->{remote_host} = $flds[1] if $flds[1]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 288 | 4 | ▶ | $self->{remote_port} = $flds[2] if $flds[2]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 289 | 4 | $self->{proto} = (getprotobyname($flds[0]))[2]; | |||||||||||||||||||||||||||||
| 290 | 4 | $self->{sock_type} = $sock_type_by_proto{$flds[0]}; | |||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||
| 292 | else { | ||||||||||||||||||||||||||||||
| 293 | 1 | $self->err("Bad protocol in \'$s\'\n"); | |||||||||||||||||||||||||||||
| 294 | 1 | return 0; | |||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||
| 296 | 6 | return 1; | |||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||
| 299 | 9 | ▶ | sub dbg($$) { | ||||||||||||||||||||||||||||
|
Subroutine
dbg: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||||
| 300 | 9 | my ( $self, $s ) = @_; | |||||||||||||||||||||||||||||
| 301 | 9 | ▶ | if ($self->{verbose}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 302 | 0 | print STDERR "DBG: $s\n"; | |||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||
| 307 | 0 | ▶ | sub dbg_dump_arr ($$\@) { # method prototypes has no effect ! | ||||||||||||||||||||||||||||
|
Subroutine
dbg_dump_arr: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||
| 308 | 0 | my ($self, $name, $arr) = @_; | |||||||||||||||||||||||||||||
| 309 | 0 | ▶ | if ($self->{verbose}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 310 | 0 | $self->dbg(sprintf("$name (length: %d):", scalar(@$arr))); | |||||||||||||||||||||||||||||
| 311 | 0 | my $s1 = ''; | |||||||||||||||||||||||||||||
| 312 | 0 | my $s2 = ''; | |||||||||||||||||||||||||||||
| 313 | 0 | my $i = 16; | |||||||||||||||||||||||||||||
| 314 | 0 | for my $j (0 .. $#$arr) { | |||||||||||||||||||||||||||||
| 315 | 0 | $b = $$arr[$j]; | |||||||||||||||||||||||||||||
| 316 | 0 | $s1 .= sprintf('%0.2x ', $b); | |||||||||||||||||||||||||||||
| 317 | 0 | ▶ | if (($b >= 0x20) and ($b<0x80)) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$b >= 32 and $b < 128
MC/DC0%0/2
$b >= 32 and $b < 128
$b >= 32
$b < 128
Truth table0%0/3
A and B
A= $b >= 32B= $b < 128
| |||||||||||||||||||||||||||||||
| 318 | 0 | $s2 .= pack('C', $b); | |||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||
| 320 | else { | ||||||||||||||||||||||||||||||
| 321 | 0 | $s2 .= '.'; | |||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||
| 323 | 0 | $i--; | |||||||||||||||||||||||||||||
| 324 | 0 | ▶ | if (($i & 0x0F) == 0) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 325 | 0 | $self->dbg("$s1 $s2"); | |||||||||||||||||||||||||||||
| 326 | 0 | $s1 = ''; | |||||||||||||||||||||||||||||
| 327 | 0 | $s2 = ''; | |||||||||||||||||||||||||||||
| 328 | 0 | $i = 16; | |||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||
| 331 | 0 | ▶ | if ($i & 0x0F) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 332 | 0 | ▶ | while ($i > 0) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 333 | 0 | $s1 .= ' '; | |||||||||||||||||||||||||||||
| 334 | 0 | $i--; | |||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||
| 336 | 0 | $self->dbg("$s1 $s2"); | |||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||
| 338 | 0 | $self->dbg("END"); | |||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||
| 342 | 2 | ▶ | sub err($$) { | ||||||||||||||||||||||||||||
|
Subroutine
err: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 343 | 2 | my ($self, $s) = @_; | |||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||
| 345 | 2 | my @stack = caller(1); | |||||||||||||||||||||||||||||
| 346 | 2 | $self->{errs} = "$stack[3]: $s"; | |||||||||||||||||||||||||||||
| 347 | 2 | $self->dbg("ERROR: $self->{errs}"); | |||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||
| 350 | 1 | ▶ | sub open($) { | ||||||||||||||||||||||||||||
|
Subroutine
open: called CC=7 SCAR=31.3
| |||||||||||||||||||||||||||||||
| 351 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 352 | 1 | my $sock; | |||||||||||||||||||||||||||||
| 353 | 1 | ▶ | if (defined $self->{socket}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 354 | 0 | $self->err("Socket is already opened"); | |||||||||||||||||||||||||||||
| 355 | 0 | return 0; | |||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||
| 357 | 1 | ▶ | if ($self->{sock_domain} == PF_UNIX) { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 358 | $sock = IO::Socket::UNIX->new( | ||||||||||||||||||||||||||||||
| 359 | Type=>$self->{sock_type}, | ||||||||||||||||||||||||||||||
| 360 | Peer=>$self->{unix_sock} | ||||||||||||||||||||||||||||||
| 361 | #Local=> | ||||||||||||||||||||||||||||||
| 362 | #Listem=> | ||||||||||||||||||||||||||||||
| 363 | 1 | ); | |||||||||||||||||||||||||||||
| 364 | 1 | ▶ | unless ($sock) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 365 | 1 | $self->err("socket: $!"); | |||||||||||||||||||||||||||||
| 366 | 1 | return 0; | |||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||
| 368 | # unless (connect(SOCKET, sockaddr_un($conn_params->{'file'}))) { | ||||||||||||||||||||||||||||||
| 369 | # err(%$conn, "connect: $!"); | ||||||||||||||||||||||||||||||
| 370 | # close SOCKET; | ||||||||||||||||||||||||||||||
| 371 | # return 0; | ||||||||||||||||||||||||||||||
| 372 | # } | ||||||||||||||||||||||||||||||
| 373 | 0 | $self->{socket} = $sock; | |||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||
| 375 | elsif($self->{sock_domain} == PF_INET) { | ||||||||||||||||||||||||||||||
| 376 | 0 | my $iaddr = inet_aton($self->{remote_host}); | |||||||||||||||||||||||||||||
| 377 | 0 | ▶ | unless ($iaddr) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 378 | 0 | $self->err("no destination address for \'$self->{remote_host}\'"); | |||||||||||||||||||||||||||||
| 379 | 0 | return 0; | |||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||
| 381 | $sock = IO::Socket::INET->new( | ||||||||||||||||||||||||||||||
| 382 | PeerAddr=>$self->{remote_host}, | ||||||||||||||||||||||||||||||
| 383 | PeerPort=>$self->{remote_port}, | ||||||||||||||||||||||||||||||
| 384 | Proto=>$self->{proto}, | ||||||||||||||||||||||||||||||
| 385 | Type=>$self->{sock_type} | ||||||||||||||||||||||||||||||
| 386 | 0 | ); | |||||||||||||||||||||||||||||
| 387 | 0 | ▶ | unless ($sock) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 388 | 0 | $self->err("socket: $!"); | |||||||||||||||||||||||||||||
| 389 | 0 | return 0; | |||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||||
| 391 | # unless (connect(SOCKET, sockaddr_in($conn_params->{'port'}, $iaddr))) { | ||||||||||||||||||||||||||||||
| 392 | # err(%$conn, "connect: $!"); | ||||||||||||||||||||||||||||||
| 393 | # $sock->close; | ||||||||||||||||||||||||||||||
| 394 | # return 0; | ||||||||||||||||||||||||||||||
| 395 | # } | ||||||||||||||||||||||||||||||
| 396 | 0 | $self->{socket} = $sock; | |||||||||||||||||||||||||||||
| 397 | } else { | ||||||||||||||||||||||||||||||
| 398 | 0 | $self->err("Unknown domain"); | |||||||||||||||||||||||||||||
| 399 | 0 | return 0; | |||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||||
| 401 | 0 | return 1; | |||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||
| 404 | 2 | ▶ | sub close($) { | ||||||||||||||||||||||||||||
|
Subroutine
close: called CC=3 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 405 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 406 | 2 | ▶ | if (! defined $self->{socket}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 407 | 2 | return 1; | |||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||||
| 409 | 0 | my $sock = $self->{socket}; | |||||||||||||||||||||||||||||
| 410 | 0 | ▶ | unless ($sock->close) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 411 | 0 | $self->err("close: $!"); | |||||||||||||||||||||||||||||
| 412 | 0 | return 0; | |||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||
| 414 | 0 | undef $self->{socket}; | |||||||||||||||||||||||||||||
| 415 | 0 | return 1; | |||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||
| 418 | 2 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 419 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 420 | 2 | $self->close(); | |||||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||
| 423 | sub command($$\@\@) { # method prototypes has no effect ! | ||||||||||||||||||||||||||||||
| 424 | my ($self, $cmd, $params, $result) = @_; | ||||||||||||||||||||||||||||||
| 425 | my $magic = 0xA; | ||||||||||||||||||||||||||||||
| 426 | my $version = 1; | ||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||
| 428 | if (!defined @$params) { | ||||||||||||||||||||||||||||||
| 429 | my @a = (); | ||||||||||||||||||||||||||||||
| 430 | $params = \@a; | ||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||
| 432 | $self->dbg("command: $cmd(@$params)"); | ||||||||||||||||||||||||||||||
| 433 | unless ($cmd) { | ||||||||||||||||||||||||||||||
| 434 | $self->err("Command not specified"); | ||||||||||||||||||||||||||||||
| 435 | return 0; | ||||||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||||
| 437 | unless (defined $self->{socket}) { | ||||||||||||||||||||||||||||||
| 438 | $self->err("Socket is not opened"); | ||||||||||||||||||||||||||||||
| 439 | return 0; | ||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||
| 441 | # prepare body | ||||||||||||||||||||||||||||||
| 442 | my @body = (); | ||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||
| 444 | binrpc_add_str(@body, $cmd); | ||||||||||||||||||||||||||||||
| 445 | foreach (@$params) { | ||||||||||||||||||||||||||||||
| 446 | my $item = $_; | ||||||||||||||||||||||||||||||
| 447 | my $data_type; | ||||||||||||||||||||||||||||||
| 448 | if ($item =~ /^\d+$/) { | ||||||||||||||||||||||||||||||
| 449 | binrpc_add_int(@body, $item); | ||||||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||||
| 451 | elsif ($item =~ /^\d+\.\d+$/) { | ||||||||||||||||||||||||||||||
| 452 | binrpc_add_double(@body, $item); | ||||||||||||||||||||||||||||||
| 453 | } else { | ||||||||||||||||||||||||||||||
| 454 | if ($item =~ /^s:/ ) { | ||||||||||||||||||||||||||||||
| 455 | $item = substr($item, 2); | ||||||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||||
| 457 | binrpc_add_str(@body, $item); | ||||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||
| 462 | my $cookie = int(rand(0xFFFFFFFF)); | ||||||||||||||||||||||||||||||
| 463 | my $body_len = $#body+1; | ||||||||||||||||||||||||||||||
| 464 | my $type = $message_type{REQ}; | ||||||||||||||||||||||||||||||
| 465 | if ($body_len > 0xFFFFFFFF) { | ||||||||||||||||||||||||||||||
| 466 | $self->err("Body length exceeded"); | ||||||||||||||||||||||||||||||
| 467 | return -1; | ||||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||||
| 469 | my $len_len = binrpc_get_int_len($body_len); | ||||||||||||||||||||||||||||||
| 470 | my $c_len = binrpc_get_int_len($cookie); | ||||||||||||||||||||||||||||||
| 471 | if ($len_len==0) { | ||||||||||||||||||||||||||||||
| 472 | $len_len=1; # we can't have 0 len | ||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||
| 474 | if ($c_len==0) { | ||||||||||||||||||||||||||||||
| 475 | $c_len=1; # we can't have 0 len | ||||||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||||
| 477 | my @hdr = (); | ||||||||||||||||||||||||||||||
| 478 | push(@hdr, ($magic << 4) | $version); | ||||||||||||||||||||||||||||||
| 479 | push(@hdr, ($type<<4)|(($len_len-1)<<2)|($c_len-1)); | ||||||||||||||||||||||||||||||
| 480 | #$self->dbg(sprintf("Cookie: %x, len_len=$len_len, c_len=$c_len, body_len=$body_len", $cookie)); | ||||||||||||||||||||||||||||||
| 481 | for (; $len_len>0; $len_len--){ | ||||||||||||||||||||||||||||||
| 482 | push(@hdr, ($body_len>>(($len_len-1)*8)) & 0xFF); | ||||||||||||||||||||||||||||||
| 483 | } | ||||||||||||||||||||||||||||||
| 484 | for (; $c_len>0; $c_len--){ | ||||||||||||||||||||||||||||||
| 485 | push(@hdr, ($cookie>>(($c_len-1)*8)) & 0xFF); | ||||||||||||||||||||||||||||||
| 486 | } | ||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||
| 488 | $self->dbg_dump_arr('header', \@hdr); | ||||||||||||||||||||||||||||||
| 489 | $self->dbg_dump_arr('body', \@body); | ||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||
| 491 | # flush read buffer | ||||||||||||||||||||||||||||||
| 492 | my $sock = $self->{'socket'}; | ||||||||||||||||||||||||||||||
| 493 | $sock->flush; | ||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||
| 495 | # send request | ||||||||||||||||||||||||||||||
| 496 | my $buf = ''; | ||||||||||||||||||||||||||||||
| 497 | for my $i (0 .. $#hdr) { | ||||||||||||||||||||||||||||||
| 498 | $buf .= pack('C', $hdr[$i]); | ||||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||||
| 500 | for my $i (0 .. $#body) { | ||||||||||||||||||||||||||||||
| 501 | $buf .= pack('C', $body[$i]); | ||||||||||||||||||||||||||||||
| 502 | } | ||||||||||||||||||||||||||||||
| 503 | unless ($sock->write($buf, length($buf))) { | ||||||||||||||||||||||||||||||
| 504 | $self->err("Send error: $!"); | ||||||||||||||||||||||||||||||
| 505 | return 0; | ||||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||
| 508 | # read header | ||||||||||||||||||||||||||||||
| 509 | unless ($sock->read($buf, 2)) { | ||||||||||||||||||||||||||||||
| 510 | $self->err("Header recv error: $!"); | ||||||||||||||||||||||||||||||
| 511 | return 0; | ||||||||||||||||||||||||||||||
| 512 | } | ||||||||||||||||||||||||||||||
| 513 | # validate | ||||||||||||||||||||||||||||||
| 514 | my @arr = unpack('C*', $buf); | ||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||
| 516 | $self->dbg_dump_arr('magic', \@arr); | ||||||||||||||||||||||||||||||
| 517 | if (($arr[0] >> 4) != $magic) { | ||||||||||||||||||||||||||||||
| 518 | $self->err("Bad magic"); | ||||||||||||||||||||||||||||||
| 519 | return 0; | ||||||||||||||||||||||||||||||
| 520 | } | ||||||||||||||||||||||||||||||
| 521 | if (($arr[0] & 0x0F) != $version) { | ||||||||||||||||||||||||||||||
| 522 | $self->err("Bad version"); | ||||||||||||||||||||||||||||||
| 523 | return 0; | ||||||||||||||||||||||||||||||
| 524 | } | ||||||||||||||||||||||||||||||
| 525 | $type = $arr[1] >> 4; | ||||||||||||||||||||||||||||||
| 526 | $len_len = (($arr[1] >> 2) & 0x03)+1; | ||||||||||||||||||||||||||||||
| 527 | $c_len = ($arr[1] & 0x03)+1; | ||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||
| 529 | unless ($sock->read($buf, $len_len+$c_len)) { | ||||||||||||||||||||||||||||||
| 530 | $self->err("Header length recv error: $!"); | ||||||||||||||||||||||||||||||
| 531 | return 0; | ||||||||||||||||||||||||||||||
| 532 | } | ||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||
| 534 | # read body len & cookie | ||||||||||||||||||||||||||||||
| 535 | my $cookie2; | ||||||||||||||||||||||||||||||
| 536 | @arr = unpack('C*', $buf); | ||||||||||||||||||||||||||||||
| 537 | $self->dbg_dump_arr('cookie', \@arr); | ||||||||||||||||||||||||||||||
| 538 | binrpc_read_int(@arr, $len_len, $body_len); | ||||||||||||||||||||||||||||||
| 539 | binrpc_read_int(@arr, $c_len, $cookie2); | ||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||
| 541 | if ($cookie != $cookie2) { | ||||||||||||||||||||||||||||||
| 542 | $self->err("Bad cookie ($cookie!=$cookie2"); | ||||||||||||||||||||||||||||||
| 543 | return 0; | ||||||||||||||||||||||||||||||
| 544 | } | ||||||||||||||||||||||||||||||
| 545 | # read body | ||||||||||||||||||||||||||||||
| 546 | unless ($sock->read($buf, $body_len)) { | ||||||||||||||||||||||||||||||
| 547 | $self->err("Body recv error: $!"); | ||||||||||||||||||||||||||||||
| 548 | return 0; | ||||||||||||||||||||||||||||||
| 549 | } | ||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||
| 551 | # parse result | ||||||||||||||||||||||||||||||
| 552 | @arr = unpack('C*', $buf); | ||||||||||||||||||||||||||||||
| 553 | $self->dbg_dump_arr('result', \@arr); | ||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||
| 555 | @$result = (); | ||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||
| 557 | while (scalar(@arr)) { | ||||||||||||||||||||||||||||||
| 558 | my ($val_type, $val, $dummy); | ||||||||||||||||||||||||||||||
| 559 | undef $val_type; # any type | ||||||||||||||||||||||||||||||
| 560 | unless (binrpc_read_record(@arr, '', $val_type, $val, $dummy)) { | ||||||||||||||||||||||||||||||
| 561 | $self->err("Parsing result error"); | ||||||||||||||||||||||||||||||
| 562 | return 0; | ||||||||||||||||||||||||||||||
| 563 | } | ||||||||||||||||||||||||||||||
| 564 | push(@$result, $val); | ||||||||||||||||||||||||||||||
| 565 | } | ||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||
| 567 | if ($type == $message_type{REPL}) { | ||||||||||||||||||||||||||||||
| 568 | return 1; | ||||||||||||||||||||||||||||||
| 569 | } | ||||||||||||||||||||||||||||||
| 570 | elsif ($type == $message_type{FAULT}) { | ||||||||||||||||||||||||||||||
| 571 | return -1; | ||||||||||||||||||||||||||||||
| 572 | } | ||||||||||||||||||||||||||||||
| 573 | else { | ||||||||||||||||||||||||||||||
| 574 | $self->err("Bad reply type ($type)"); | ||||||||||||||||||||||||||||||
| 575 | return 0; | ||||||||||||||||||||||||||||||
| 576 | } | ||||||||||||||||||||||||||||||
| 577 | } | ||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||
| 579 | sub print_result($$\@$) { | ||||||||||||||||||||||||||||||
| 580 | my ($self, $stream, $result, $indent) = @_; | ||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||
| 582 | $indent = '' unless (defined $indent); | ||||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||||
| 584 | if (ref($result) eq 'ARRAY') { | ||||||||||||||||||||||||||||||
| 585 | for my $i (0 .. $#$result) { | ||||||||||||||||||||||||||||||
| 586 | if (ref($$result[$i]) eq 'ARRAY') { | ||||||||||||||||||||||||||||||
| 587 | printf $stream "%s(\n", $indent; | ||||||||||||||||||||||||||||||
| 588 | $self->print_result($stream, $$result[$i], "$indent "); | ||||||||||||||||||||||||||||||
| 589 | printf $stream "%s)\n", $indent; | ||||||||||||||||||||||||||||||
| 590 | } elsif (ref($$result[$i]) eq 'HASH') { | ||||||||||||||||||||||||||||||
| 591 | printf $stream "%s\{\n", $indent; | ||||||||||||||||||||||||||||||
| 592 | $self->print_result($stream, $$result[$i], "$indent "); | ||||||||||||||||||||||||||||||
| 593 | printf $stream "%s}\n", $indent; | ||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||
| 595 | } else { | ||||||||||||||||||||||||||||||
| 596 | printf $stream "%s%s\n", $indent, $$result[$i]; | ||||||||||||||||||||||||||||||
| 597 | } | ||||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||||
| 600 | } elsif (ref($result) eq 'HASH') { | ||||||||||||||||||||||||||||||
| 601 | foreach my $k (keys %$result) { | ||||||||||||||||||||||||||||||
| 602 | printf $stream "%s%s: %s\n", $indent, $k, $result->{$k}; | ||||||||||||||||||||||||||||||
| 603 | } | ||||||||||||||||||||||||||||||
| 604 | } else { | ||||||||||||||||||||||||||||||
| 605 | printf $stream "%s%s\n", $indent, $$result; | ||||||||||||||||||||||||||||||
| 606 | } | ||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||
| 608 | } | ||||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||
| 611 | # ----------------------------- end of object ------------------------------------ | ||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||
| 613 | # Preloaded methods go here. | ||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||
| 615 | 1; | ||||||||||||||||||||||||||||||
| 616 | __END__ | ||||||||||||||||||||||||||||||