blib/lib/Net/CIMD.pm

filter: Statementstmt 31.6% 45 / 142 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 39.2% 11 / 28 Podpod 58.3% 7 / 12 totaltotal 26.1% 63 / 241 CC 30 SCAR 25.9
CC
30
Cov
23%
new_connect
42.0
login
20.0
encode_packet
20.0
CRAP 13.3
↑ Summary CIMD.c »
1 # Net::CIMD.pm - CIMD over TCP, pure perl implementation
2 # Copyright (c) 2013 Badr Zarhri <badr.zarhri@gmail.com>, <badr.zarhri.ext@nsn.com>, All rights reserved.
3 # This code may be distributed under same terms as perl. NO WARRANTY.
4 # The implementation of this module is based on CIMD interface specification available in the internet.
5 # I should mention that Net::SMPP of Sampo Kellomaki <sampo@iki.fi> was of great help in implementing this protocol.
6 # Writing this module in perl makes it independant of other packages (it requires only a working perl installation).
7 # This module was tested in perl 5.8.8 (redhat EL 5), and 5.16.3 (centos 6.3).
8 # Please feel free to contact me if you've any remarks or ideas to improve this work.
9 # 23-June-2013, Created the module -- Badr
10 # 27-June-2013, fixed decode RE to handle nack --Badr
11 # 11-July-2013, added server support -- Badr
12 # 12-July-2013, added 7bit encoding functions -- Badr
13  
14 package Net::CIMD;
15  
16 1 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 require Exporter;
24  
25 1 our @ISA = qw(Exporter);
26  
27 # Items to export into callers namespace by default. Note: do not export
28 # names by default without a very good reason. Use EXPORT_OK instead.
29 # Do not simply export all your public functions/methods/constants.
30  
31 # This allows declaration use Net::CIMD ':all';
32 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
33 # will save memory.
34 1 our %EXPORT_TAGS = ( 'all' => [ qw( pack_7bit unpack_7bit
35
36 ) ] );
37  
38 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
39  
40 1 our @EXPORT = qw(
41
42 );
43  
44 1 our $VERSION = '0.01_03';
45  
46  
47 1 my $trace = 0;
48 1 my %inv_op;
49 my %inv_par;
50 1 ▶use constant Defaults => {
Subroutine BEGIN: called CC=1 SCAR=0.0
51 host => "127.0.0.1",
52 port => 9971,
53 timeout => 300,
54 queue_size => 120
55 1 };
56 # CIMD interface specification 3.5 operation codes
57 1 ▶use constant operation => {
Subroutine BEGIN: called CC=1 SCAR=0.0
58 login => "01",
59 login_resp => "51",
60 logout => "02",
61 logout_resp => "52",
62 submit => "03",
63 submit_resp => "53",
64 submit_status_report => "13",
65 submit_status_report_resp => "63",
66 enquire_message_status => "04",
67 enquire_message_status_resp => "54",
68 delivery_request => "05",
69 delivery_request_resp => "55",
70 cancel => "06",
71 cancel_resp => "56",
72 deliver_message => "20",
73 deliver_message_resp => "70",
74 deliver_status_report => "23",
75 deliver_status_report_resp => "73",
76 set_parameters => "08",
77 set_parameters_resp => "58",
78 get_parameters => "09",
79 get_parameters_resp => "59",
80 alive => "40",
81 alive_resp => "90",
82 general_error_resp => "98",
83 nack => "99"
84 1 };
85 # CIMD specification 7.2 parameters.
86 1 ▶use constant parameter => {
Subroutine BEGIN: called CC=1 SCAR=0.0
87 user_identity => "010",
88 password => "011",
89 subaddr => "012",
90 window_size => "019",
91 destination_address => "021",
92 originating_address => "023",
93 originating_IMSI => "026",
94 alphanumeric_ariginating_address => "027",
95 originated_visited_MSC_address => "028",
96 data_coding_scheme => "030",
97 user_data_header => "032",
98 user_data => "033",
99 user_data_binary => "034",
100 more_messages_to_send => "044",
101 validity_Period_Relative => "050",
102 validity_Period_Absolute => "051",
103 protocol_identifier => "052",
104 first_delivery_time_relative => "053",
105 first_delivery_time_absolute => "054",
106 reply_path => "055",
107 status_report_request => "056",
108 cancel_enabled => "058",
109 cancel_mode => "059",
110 service_centre_time_stamp => "060",
111 status_code => "061",
112 status_error_code => "062",
113 discharge_time => "063",
114 tariff_class => "064",
115 service_description => "065",
116 message_count => "066",
117 priority => "067",
118 delivery_request_mode => "068",
119 service_center_address => "069",
120 IP_address => "071",
121 get_parameter => "500",
122 SMS_center_time => "501",
123 error_code => "900",
124 error_text => "901"
125 1 };
126  
127 1 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.0
128 1 foreach( keys %{&operation})
129 {
130 26 $inv_op{operation->{$_}}=$_;
131 }
132 1 foreach( keys %{&parameter} )
133 {
134 38 $inv_par{parameter->{$_}}=$_;
135 }
136 }
137 0 ▶sub new_connect {
Subroutine new_connect: not called CC=6 SCAR=37.4
Pod undocumented
138 0 my %arg = @_;
139 my %parms=(
140 PeerAddr => exists $arg{host} ? $arg{host} : Defaults->{host},
141 PeerPort => exists $arg{port} ? $arg{port} : Defaults->{port},
142 Proto => 'tcp',
143 0 ▶ Timeout => exists $arg{timeout} ? $arg{timeout} : Defaults->{timeout} );
Branch: 3 branches
BranchTrueFalseTotal
exists $arg{'host'} ? : 0 0 0
exists $arg{'port'} ? : 0 0 0
exists $arg{'timeout'} ? : 0 0 0
144 0 ▶ my $s = IO::Socket::INET->new(%parms) # pass any extra args to constructor
Branch: 1 branch
BranchTrueFalseTotal
my $s = "IO::Socket::INET"->new(%parms) or croak("Can't connect $! $@\n") 0 0 0
145 or croak "Can't connect $! $@\n";
146 0 ▶ return undef unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 0 0
147 0 $s->autoflush(1);
148 0 return $s;
149 }
150  
151 # buffer here is for future use.
152 0 ▶sub receive_packet {
Subroutine receive_packet: not called CC=3 SCAR=24.8
Pod documented
153 0 my $me=shift;
154 0 my $temp;
155 0 ▶ while ($me->{tunnel}->sysread($temp, 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->{"tunnel"}->sysread($temp, 1)) 0 0 0
156 0 $me->{buffer} .= $temp;
157 0 ▶ last if($me->{buffer} =~ /\x09(?:..)?\x03$/);
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"buffer"} =~ /\x09(?:..)?\x03$/ 0 0 0
158 }
159 0 my $result=$me->{buffer};
160 0 $me->{buffer}="";
161 0 return $result;
162 }
163  
164 0 ▶sub send_packet {
Subroutine send_packet: not called CC=3 SCAR=24.8
Pod undocumented
165 0 my $me = shift;
166 0 my $data = shift;
167 0 $me->{seq}=sprintf("%03d", (($me->{seq}+2)%1000));
168 0 ▶ carp "sending packet :\n".hexdump($data,"\t") if $trace;
Branch: 1 branch
BranchTrueFalseTotal
if $trace 0 0 0
169 0 ▶ $me->{tunnel}->syswrite($data) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$me->{"tunnel"}->syswrite($data) or return undef 0 0 0
170 0 return 'Ok';
171 }
172  
173 0 ▶sub login {
Subroutine login: not called CC=4 SCAR=30.0
Pod documented
174 0 my $type = shift;
175 0 ▶ $type=ref($type) || $type;
Condition0%0/3
ref $type || $type
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $type || $type
ref $type $type
Truth table0%0/3
A || B
A= ref $typeB= $type
ABresult
1X1
011
000
176 0 my $tunnel = new_connect(@_);
177 0 ▶ return undef if !defined $tunnel;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tunnel 0 0 0
178 0 my $me= bless {
179 seq => "001",
180 buffer => "",
181 checksum => 0,
182 tunnel => $tunnel
183 }, $type;
184 0 my %args=@_;
185 0 $me->send_packet($me->encode_packet("login", 'user_identity',$args{user_identity},'password',$args{password}));
186 0 my $resp=$me->receive_packet();
187 0 ▶ print "received\n".Dumper($resp) if $trace;
Branch: 1 branch
BranchTrueFalseTotal
if $trace 0 0 0
188 0 return $me;
189 }
190  
191 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
192 0 my $me = shift;
193 0 my $operation=our $AUTOLOAD;
194 0 $operation =~ s/^.*::([^:]+)$/$1/;
195 0 ▶ return undef unless defined (operation->{$operation});
Branch: 1 branch
BranchTrueFalseTotal
unless defined {"alive" => 40,"alive_resp" => 90,"cancel" => "06","cancel_resp" => 56,"deliver_message" => 20,"deliver_message_resp" => 70,"deliver_status_report" => 23,"deliver_status_report_resp" => 73,"delivery_request" => "05","delivery_request_resp" => 55,"enquire_message_status" => "04","enquire_message_status_resp" => 54,"general_error_resp" => 98,"get_parameters" => "09","get_parameters_resp" => 59,"login" => "01","login_resp" => 51,"logout" => "02","logout_resp" => 52,"nack" => 99,"set_parameters" => "08","set_parameters_resp" => 58,"submit" => "03","submit_resp" => 53,"submit_status_report" => 13,"submit_status_report_resp" => 63}->{$operation} 0 0 0
196 0 $me->send_packet($me->encode_packet($operation,@_));
197 0 return "Ok";
198 }
199  
200 0 ▶sub encode_packet {
Subroutine encode_packet: not called CC=4 SCAR=30.0
Pod undocumented
201 0 my $me=shift;
202 0 my $op=shift;
203 0 my %args=@_;
204 my $res="\x02".
205 0 ▶ operation->{$op}.":".(defined $args{seq}?$args{seq}:$me->{seq})."\t";
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'seq'} ? : 0 0 0
206 0 foreach (sort { parameter->{$a} <=> parameter->{$b} } keys %args)
207 {
208 0 ▶ $res.=parameter->{$_}.":".$args{$_}."\t" if(defined parameter->{$_});
Branch: 1 branch
BranchTrueFalseTotal
if defined {"IP_address" => "071","SMS_center_time" => 501,"alphanumeric_ariginating_address" => "027","cancel_enabled" => "058","cancel_mode" => "059","data_coding_scheme" => "030","delivery_request_mode" => "068","destination_address" => "021","discharge_time" => "063","error_code" => 900,"error_text" => 901,"first_delivery_time_absolute" => "054","first_delivery_time_relative" => "053","get_parameter" => 500,"message_count" => "066","more_messages_to_send" => "044","originated_visited_MSC_address" => "028","originating_IMSI" => "026","originating_address" => "023","password" => "011","priority" => "067","protocol_identifier" => "052","reply_path" => "055","service_center_address" => "069","service_centre_time_stamp" => "060","service_description" => "065","status_code" => "061","status_error_code" => "062","status_report_request" => "056","subaddr" => "012","tariff_class" => "064","user_data" => "033","user_data_binary" => "034","user_data_header" => "032","user_identity" => "010","validity_Period_Absolute" => "051","validity_Period_Relative" => "050","window_size" => "019"}->{$_} 0 0 0
209 }
210 0 return $res.&checksum($res)."\x03";
211 #return $res."\x03";
212 }
213  
214 0 ▶sub decode_packet {
Subroutine decode_packet: not called CC=3 SCAR=24.8
Pod documented
215 0 my $me=shift;
216 0 my $data=shift;
217 0 ▶ return undef unless($data =~ /^\x02([^:]+):([^\x09]+)\x09(.*\x09)?(..)?\x03/);
Branch: 1 branch
BranchTrueFalseTotal
unless $data =~ /^\x02([^:]+):([^\x09]+)\x09(.*\x09)?(..)?\x03/ 0 0 0
218 0 my ($op, $seq)=($1, $2);
219 0 $data=$3;
220 0 my $checksum=$4;
221 0 my %parms;
222 0 ▶ $data =~ s/([^:]+):([^\x09]*)\x09/$parms{$inv_par{$1}}=$2/eg if(defined $data);
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 0 0 0
223 0 return bless {"operation", $inv_op{$op}, "sequence", $seq, %parms}, 'Net::CIMD::PDU';
224 }
225  
226 0 ▶sub checksum {
Subroutine checksum: not called CC=2 SCAR=17.9
Pod undocumented
227 0 my $hash = 0;
228 0 foreach (split //, shift) {
229 0 $hash += ord($_);
230 0 $hash &= 0xFF;
231 }
232 0 return sprintf("%02x",$hash);
233 }
234 sub read_sync()
235 0 ▶{
Subroutine read_sync: not called CC=2 SCAR=17.9
Pod documented
236 0 my $me=shift;
237 0 my $req=$me->decode_packet($me->receive_packet());
238 0 ▶ $me->send_packet($me->encode_packet($req->{"operation"}."_resp",'seq', $req->{"sequence"})) if defined (operation->{$req->{"operation"}."_resp"});
Branch: 1 branch
BranchTrueFalseTotal
if defined {"alive" => 40,"alive_resp" => 90,"cancel" => "06","cancel_resp" => 56,"deliver_message" => 20,"deliver_message_resp" => 70,"deliver_status_report" => 23,"deliver_status_report_resp" => 73,"delivery_request" => "05","delivery_request_resp" => 55,"enquire_message_status" => "04","enquire_message_status_resp" => 54,"general_error_resp" => 98,"get_parameters" => "09","get_parameters_resp" => 59,"login" => "01","login_resp" => 51,"logout" => "02","logout_resp" => 52,"nack" => 99,"set_parameters" => "08","set_parameters_resp" => 58,"submit" => "03","submit_resp" => 53,"submit_status_report" => 13,"submit_status_report_resp" => 63}->{$req->{"operation"} . '_resp'} 0 0 0
239 0 return $req;
240 }
241 sub read_async()
242 0 ▶{
Subroutine read_async: not called CC=1 SCAR=6.9
Pod documented
243 0 my $me=shift;
244 0 return $me->decode_packet($me->receive_packet());
245 }
246 sub DESTROY
247 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=6.9
248 0 my $me=shift;
249 0 $me->logout();
250 }
251  
252 # Server commands
253 0 ▶sub listen {
Subroutine listen: not called CC=4 SCAR=30.0
Pod undocumented
254 0 my $type=shift;
255 0 ▶ $type=ref($type) || $type;
Condition0%0/3
ref $type || $type
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $type || $type
ref $type $type
Truth table0%0/3
A || B
A= ref $typeB= $type
ABresult
1X1
011
000
256 0 my %arg=@_;
257 my %parms=(
258 Listen => exists $arg{queue_size} ? $arg{queue_size} : Defaults->{queue_size},
259 LocalPort => exists $arg{port} ? $arg{port} : Defaults->{port},
260 0 ▶ Proto => 'tcp',
Branch: 2 branches
BranchTrueFalseTotal
exists $arg{'queue_size'} ? : 0 0 0
exists $arg{'port'} ? : 0 0 0
261 ReuseAddr => 'true',
262 );
263 0 return Net::CIMD::Listener->new(%parms);
264 }
265  
266 0 ▶sub pack_7bit {
Subroutine pack_7bit: not called CC=1 SCAR=6.9
Pod documented
267 0 my ($s) = @_;
268 0 $s = unpack 'b*', $s;
269 0 $s =~ s/(.{7})./$1/g; # Zap the high order (8th) bits
270 0 $s= pack 'b*', $s;
271 0 $s=unpack("H*", $s);
272 0 return uc($s);
273 }
274  
275 0 ▶sub unpack_7bit {
Subroutine unpack_7bit: not called CC=2 SCAR=17.9
Pod documented
276 0 my ($s) = @_;
277 0 $s=pack("H*", $s);
278 0 $s = unpack 'b*', $s;
279 0 $s =~ s/(.{7})/${1}0/g; # Stuff in high order (8th) bits
280 0 $s = pack 'b*', $s;
281 0 ▶ chop $s if substr($s, -1, 1) eq "\x00";
Branch: 1 branch
BranchTrueFalseTotal
if substr($s, -1, 1) eq "\0" 0 0 0
282 0 return $s;
283 }
284  
285 package Net::CIMD::PDU;
286 sub new
287 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
288 0 my $class=shift;
289 0 return bless {@_}, $class;
290 }
291  
292 package Net::CIMD::Listener;
293 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
294  
295 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
296 0 my $type=shift;
297 0 ▶ $type=ref($type) || $type;
Condition0%0/3
ref $type || $type
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $type || $type
ref $type $type
Truth table0%0/3
A || B
A= ref $typeB= $type
ABresult
1X1
011
000
298 0 print "Listening : ".join(";", @_)." ...\n";
299 0 ▶ my $s = IO::Socket::INET->new(@_) or croak "Can't Listen $! $@\n";
Branch: 1 branch
BranchTrueFalseTotal
my $s = "IO::Socket::INET"->new(@_) or croak("Can't Listen $! $@\n") 0 0 0
300 0 ▶ return undef unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 0 0
301 0 $s->autoflush(1);
302 0 return bless {'listener' => $s}, $type;
303 }
304 0 ▶sub accept {
Subroutine accept: not called CC=1 SCAR=6.9
305 0 my $self=shift;
306 0 my $tunnel = $self->{'listener'}->accept();
307 0 return bless {
308 seq => "002",
309 buffer => "",
310 tunnel => $tunnel
311 }, 'Net::CIMD';
312 }
313  
314 1 1;
315 __END__
↑ Summary CIMD.c »