| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Net::IMP::Remote::DualvarMapping; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 15 | use strict; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 120 |  | 
| 4 | 2 |  |  | 2 |  | 11 | use warnings; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 72 |  | 
| 5 | 2 |  |  | 2 |  | 11 | use Net::IMP::Remote::Protocol; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 221 |  | 
| 6 | 2 |  |  | 2 |  | 12 | use Net::IMP qw(:DEFAULT :log IMP_DATA_TYPES ); | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 576 |  | 
| 7 | 2 |  |  | 2 |  | 15 | use Net::IMP::Debug; | 
|  | 2 |  |  |  |  | 59 |  | 
|  | 2 |  |  |  |  | 13223 |  | 
| 8 | 2 |  |  | 2 |  | 358 | use Exporter 'import'; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 3310 |  | 
| 9 |  |  |  |  |  |  | our @EXPORT = qw(rpc_i2d rpc_d2i); | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | # data type mapping int -> dualvar | 
| 12 |  |  |  |  |  |  | # basic data types are added, we check for more additional types in | 
| 13 |  |  |  |  |  |  | # IMPRPC_GET_INTERFACE and IMPRPC_SET_INTERFACE | 
| 14 |  |  |  |  |  |  | my %dt_i2d = ( | 
| 15 |  |  |  |  |  |  | IMP_DATA_STREAM+0 => IMP_DATA_STREAM, | 
| 16 |  |  |  |  |  |  | IMP_DATA_PACKET+0 => IMP_DATA_PACKET, | 
| 17 |  |  |  |  |  |  | ); | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | sub _dt_i2d { | 
| 20 | 0 |  |  | 0 |  | 0 | my $i = shift; | 
| 21 | 0 |  |  |  |  | 0 | my $v = $dt_i2d{$i}; | 
| 22 | 0 | 0 |  |  |  | 0 | return $v if defined $v; | 
| 23 | 0 |  |  |  |  | 0 | for ( IMP_DATA_TYPES() ) { | 
| 24 | 0 | 0 |  |  |  | 0 | exists $dt_i2d{ $_+0 } and next; | 
| 25 | 0 |  |  |  |  | 0 | $dt_i2d{ $_+0 } = $_; | 
| 26 |  |  |  |  |  |  | } | 
| 27 | 0 |  |  |  |  | 0 | $v = $dt_i2d{$i}; | 
| 28 | 0 | 0 |  |  |  | 0 | return $v if defined $v; | 
| 29 | 0 |  |  |  |  | 0 | die "cannot map $i to known data type"; | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | # return type mapping int -> dualvar | 
| 33 |  |  |  |  |  |  | my %rt_i2d = map { ( $_+0 => $_ ) } ( | 
| 34 |  |  |  |  |  |  | IMP_PASS, | 
| 35 |  |  |  |  |  |  | IMP_PASS_PATTERN, | 
| 36 |  |  |  |  |  |  | IMP_PREPASS, | 
| 37 |  |  |  |  |  |  | IMP_DENY, | 
| 38 |  |  |  |  |  |  | IMP_DROP, | 
| 39 |  |  |  |  |  |  | IMP_FATAL, | 
| 40 |  |  |  |  |  |  | IMP_TOSENDER, | 
| 41 |  |  |  |  |  |  | IMP_REPLACE, | 
| 42 |  |  |  |  |  |  | IMP_PAUSE, | 
| 43 |  |  |  |  |  |  | IMP_CONTINUE, | 
| 44 |  |  |  |  |  |  | IMP_LOG, | 
| 45 |  |  |  |  |  |  | IMP_PORT_OPEN, | 
| 46 |  |  |  |  |  |  | IMP_PORT_CLOSE, | 
| 47 |  |  |  |  |  |  | IMP_ACCTFIELD, | 
| 48 |  |  |  |  |  |  | ); | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | # log level mapping int -> dualvar | 
| 51 |  |  |  |  |  |  | my %ll_i2d = map { ( $_+0 => $_ ) } ( | 
| 52 |  |  |  |  |  |  | IMP_LOG_DEBUG, | 
| 53 |  |  |  |  |  |  | IMP_LOG_INFO, | 
| 54 |  |  |  |  |  |  | IMP_LOG_NOTICE, | 
| 55 |  |  |  |  |  |  | IMP_LOG_WARNING, | 
| 56 |  |  |  |  |  |  | IMP_LOG_ERR, | 
| 57 |  |  |  |  |  |  | IMP_LOG_CRIT, | 
| 58 |  |  |  |  |  |  | IMP_LOG_ALERT, | 
| 59 |  |  |  |  |  |  | IMP_LOG_EMERG, | 
| 60 |  |  |  |  |  |  | ); | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | # op mapping int -> dualvar | 
| 63 |  |  |  |  |  |  | my %op_i2d = map { ( $_+0 => $_ ) } ( | 
| 64 |  |  |  |  |  |  | IMPRPC_GET_INTERFACE, | 
| 65 |  |  |  |  |  |  | IMPRPC_SET_INTERFACE, | 
| 66 |  |  |  |  |  |  | IMPRPC_NEW_ANALYZER, | 
| 67 |  |  |  |  |  |  | IMPRPC_DEL_ANALYZER, | 
| 68 |  |  |  |  |  |  | IMPRPC_DATA, | 
| 69 |  |  |  |  |  |  | IMPRPC_SET_VERSION, | 
| 70 |  |  |  |  |  |  | IMPRPC_EXCEPTION, | 
| 71 |  |  |  |  |  |  | IMPRPC_INTERFACE, | 
| 72 |  |  |  |  |  |  | IMPRPC_RESULT, | 
| 73 |  |  |  |  |  |  | ); | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | my %args_d2i = ( | 
| 76 |  |  |  |  |  |  | IMPRPC_GET_INTERFACE+0 => sub { | 
| 77 |  |  |  |  |  |  | # @_ -> list< data_type_id, list > provider_ifs | 
| 78 |  |  |  |  |  |  | my @rv; | 
| 79 |  |  |  |  |  |  | for my $if (@_) { | 
| 80 |  |  |  |  |  |  | my ($dtype,$rtypes) = @$if; | 
| 81 |  |  |  |  |  |  | if ( defined $dtype ) { | 
| 82 |  |  |  |  |  |  | $dt_i2d{ $dtype+0 } ||= $dtype; | 
| 83 |  |  |  |  |  |  | $dtype += 0 | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  | if ( $rtypes ) { | 
| 86 |  |  |  |  |  |  | push @rv, [ $dtype , [ map { $_+0 } @$rtypes ]]; | 
| 87 |  |  |  |  |  |  | } else { | 
| 88 |  |  |  |  |  |  | push @rv, [ $dtype ] | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  | return @rv; | 
| 92 |  |  |  |  |  |  | }, | 
| 93 |  |  |  |  |  |  | IMPRPC_SET_INTERFACE+0 => sub { | 
| 94 |  |  |  |  |  |  | # @_ ->  > provider_if | 
| 95 |  |  |  |  |  |  | my ($dtype,$rtypes) = @{$_[0]}; | 
| 96 |  |  |  |  |  |  | my @rt = map { $_+0 } @$rtypes; | 
| 97 |  |  |  |  |  |  | if ( ! defined $dtype ) { | 
| 98 |  |  |  |  |  |  | return [ undef , \@rt ] | 
| 99 |  |  |  |  |  |  | } else { | 
| 100 |  |  |  |  |  |  | $dt_i2d{ $dtype+0 } ||= $dtype; | 
| 101 |  |  |  |  |  |  | return [ $dtype+0 , \@rt ] | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  | }, | 
| 104 |  |  |  |  |  |  | IMPRPC_DATA+0 => sub { | 
| 105 |  |  |  |  |  |  | # @_ -> analyzer_id, dir, offset, data_type_id, char data[] | 
| 106 |  |  |  |  |  |  | return (@_[0,1,2],$_[3]+0,$_[4]); | 
| 107 |  |  |  |  |  |  | }, | 
| 108 |  |  |  |  |  |  | IMPRPC_RESULT+0 => sub { | 
| 109 |  |  |  |  |  |  | # @_ -> analyzer_id, result_type_id, ... | 
| 110 |  |  |  |  |  |  | my ($id,$rtype) = @_; | 
| 111 |  |  |  |  |  |  | if ( $rtype == IMP_LOG ) { | 
| 112 |  |  |  |  |  |  | # id,type - dir,offset,len,level,msg | 
| 113 |  |  |  |  |  |  | return ($id,$rtype+0,@_[2,3,4],$_[5]+0,$_[6]); | 
| 114 |  |  |  |  |  |  | } else { | 
| 115 |  |  |  |  |  |  | return ($id,$rtype+0,@_[2..$#_]); | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  | }, | 
| 118 |  |  |  |  |  |  | ); | 
| 119 |  |  |  |  |  |  | $args_d2i{ IMPRPC_INTERFACE+0 } = $args_d2i{ IMPRPC_GET_INTERFACE+0 }; | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | my %args_i2d = ( | 
| 123 |  |  |  |  |  |  | IMPRPC_GET_INTERFACE+0 => sub { | 
| 124 |  |  |  |  |  |  | # @_ -> list< data_type_id, list > provider_ifs | 
| 125 |  |  |  |  |  |  | my @rv; | 
| 126 |  |  |  |  |  |  | for my $if (@_) { | 
| 127 |  |  |  |  |  |  | my ($dtype,$rtypes) = @$if; | 
| 128 |  |  |  |  |  |  | $dtype = $dt_i2d{$dtype} || _dt_i2d($dtype) if defined $dtype; | 
| 129 |  |  |  |  |  |  | if ( $rtypes ) { | 
| 130 |  |  |  |  |  |  | push @rv, [ $dtype, [ map { $rt_i2d{$_} } @$rtypes ]]; | 
| 131 |  |  |  |  |  |  | } else { | 
| 132 |  |  |  |  |  |  | push @rv, [ $dtype ] | 
| 133 |  |  |  |  |  |  | } | 
| 134 |  |  |  |  |  |  | } | 
| 135 |  |  |  |  |  |  | return @rv; | 
| 136 |  |  |  |  |  |  | }, | 
| 137 |  |  |  |  |  |  | IMPRPC_SET_INTERFACE+0 => sub { | 
| 138 |  |  |  |  |  |  | # @_ ->  > provider_if | 
| 139 |  |  |  |  |  |  | my ($dtype,$rtypes) = @{$_[0]}; | 
| 140 |  |  |  |  |  |  | my @rt = map { defined($_) ? $rt_i2d{$_} :undef } @$rtypes; | 
| 141 |  |  |  |  |  |  | $dtype = $dt_i2d{$dtype} || _dt_i2d($dtype) if defined $dtype; | 
| 142 |  |  |  |  |  |  | return [ $dtype,\@rt ]; | 
| 143 |  |  |  |  |  |  | }, | 
| 144 |  |  |  |  |  |  | IMPRPC_DATA+0 => sub { | 
| 145 |  |  |  |  |  |  | # @_ -> analyzer_id, dir, offset, data_type_id, char data[] | 
| 146 |  |  |  |  |  |  | return (@_[0,1,2],$dt_i2d{$_[3]} || _dt_i2d($_[3]),$_[4]); | 
| 147 |  |  |  |  |  |  | }, | 
| 148 |  |  |  |  |  |  | IMPRPC_RESULT+0 => sub { | 
| 149 |  |  |  |  |  |  | # @_ -> analyzer_id, result_type_id, ... | 
| 150 |  |  |  |  |  |  | my ($id,$rtype) = @_; | 
| 151 |  |  |  |  |  |  | if ( $rtype == IMP_LOG ) { | 
| 152 |  |  |  |  |  |  | # id,type - dir,offset,len,level,msg | 
| 153 |  |  |  |  |  |  | return ($id,$rt_i2d{$rtype},@_[2,3,4],$ll_i2d{$_[5]},$_[6]); | 
| 154 |  |  |  |  |  |  | } else { | 
| 155 |  |  |  |  |  |  | return ($id,$rt_i2d{$rtype},@_[2..$#_]); | 
| 156 |  |  |  |  |  |  | } | 
| 157 |  |  |  |  |  |  | }, | 
| 158 |  |  |  |  |  |  | ); | 
| 159 |  |  |  |  |  |  | $args_i2d{ IMPRPC_INTERFACE+0 } = $args_i2d{ IMPRPC_GET_INTERFACE+0 }; | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | sub rpc_i2d { | 
| 162 | 22 |  |  | 22 | 0 | 39 | my ($op,@args) = @{$_[0]}; | 
|  | 22 |  |  |  |  | 99 |  | 
| 163 | 22 |  |  |  |  | 66 | $op = $op_i2d{$op}; | 
| 164 | 22 | 100 |  |  |  | 104 | my $sub = $args_i2d{$op+0} or return [$op,@args]; | 
| 165 |  |  |  |  |  |  | #$DEBUG && debug("calling args_i2d for $op"); | 
| 166 | 16 |  |  |  |  | 52 | return [ $op, $sub->(@args) ]; | 
| 167 |  |  |  |  |  |  | } | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | sub rpc_d2i { | 
| 170 | 24 |  |  | 24 | 0 | 35 | my ($op,@args) = @{$_[0]}; | 
|  | 24 |  |  |  |  | 77 |  | 
| 171 | 24 | 100 |  |  |  | 1739 | my $sub = $args_d2i{$op+0} or return [ $op+0,@args ]; | 
| 172 |  |  |  |  |  |  | #$DEBUG && debug("calling args_d2i for $op"); | 
| 173 | 16 |  |  |  |  | 385 | return [ $op+0,$sub->(@args) ]; | 
| 174 |  |  |  |  |  |  | } | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | 1; |