1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
#ver 0.03 (C)Cyr
use strict;
use warnings;
use diagnostics;
use IO::Socket;
use Convert::ASN1;
#use Data::Dumper;
use Carp;
use POSIX;
my $CSTAapdu;
my $socket;
sub decode_msg_header {
my $bin = shift;
my $len = unpack( 'n', $bin );
return $len;
}
sub encode_msg_header {
my $len = shift;
die "Message larger than allowed!" unless ( $len <= 240 );
my $bin = pack( 'n', $len );
return $bin;
}
sub convert_to_hex {
my $pdu = $_[0];
my $hexdata = unpack( 'H*', $pdu );
$hexdata =~ tr/a-z/A-Z/;
$hexdata =~ s/(..)/$1 /g;
$hexdata =~ s/ $//g;
return $hexdata;
}
sub send_pdu {
my $pdu = $_[0];
my $header = encode_msg_header( length($pdu) );
$socket->write($header);
$socket->write($pdu);
# my $hexdata = convert_to_hex($pdu);
# print("SENT: [$hexdata]\n");
}
sub csta_connect {
my %args = %{ $_[0] };
open_csta_socket( $args{host}, $args{port} );
# A-ASSOCIATE Request
my $pdu =
"602380020780A10706052B0C00815ABE14281206072B0C00821D8148A007A0050303000800";
$pdu = pack( 'H*', $pdu );
send_pdu($pdu);
# A-ASSOCIATE Result
$pdu = receive_stuff();
my $hexdata = convert_to_hex($pdu);
if ( $hexdata =~ /A2 03 02 01 01/ ) {
print "rejected-permanent\n";
exit 0;
}
#SystemStatus Request
$pdu = receive_stuff();
my $out = $CSTAapdu->decode($pdu);
my $invokeID = $out->{svcRequest}->{invokeID};
SystemStatusResult($invokeID);
}
sub open_csta_socket {
my $host = shift;
my $port = shift;
$socket = new IO::Socket::INET(
PeerAddr => $host,
PeerPort => $port,
Blocking => 1,
Proto => 'tcp'
) || die "Error creating socket: $!\n";
$socket->autoflush(1);
print("opened a connection to $host on port $port\n");
}
sub receive_stuff {
my $header = '';
my $pdu = '';
my $nbytes = $socket->sysread( $header, 2 );
if ( $nbytes == 1 ) { # фрагмент пакета
my $header2;
my $nbytes2 = $socket->sysread( $header2, 1 );
$header = $header . $header2;
$nbytes = 2;
}
croak "Didn't receive the specified amount of data (2 bytes)!\n" . chr(7)
unless ( $nbytes == 2 );
my $len = decode_msg_header($header);
$nbytes = $socket->sysread( $pdu, $len );
if ( $nbytes < $len ) { # фрагмент пакета
my $pdu2;
my $nbytes2 = $socket->sysread( $pdu2, $len - $nbytes );
$pdu = $pdu . $pdu2;
$nbytes = $nbytes + $nbytes2;
}
croak "Didn't receive the specified amount of data ($len bytes)!\n" . chr(7)
unless ( $nbytes == $len );
# my $hexdata = convert_to_hex($pdu);
# print("RECEIVED:[$hexdata]\n");
return $pdu;
}
sub SystemStatusResult {
my $invokeID = shift;
my $pdu = $CSTAapdu->encode(
{
svcResult => {
invokeID => $invokeID,
result => { serviceID => 211, serviceResult => { noData => 1 } }
}
}
);
send_pdu($pdu); # send SystemStatus Result
}
sub makeCall {
my $dialingNumber = shift;
my $pdu = $CSTAapdu->encode(
{
svcRequest => {
invokeID => 9,
serviceID => 10,
serviceArgs => {
callingDevice => {
deviceIdentifier => {
dialingNumber =>
$dialingNumber # номер абонента
}
},
calledDirectoryNumber => {
deviceIdentifier => {
dialingNumber =>
178 # номер ICD автоинформатора
}
}
}
}
}
);
send_pdu($pdu);
$pdu = receive_stuff();
#Convert::ASN1::asn_dump($pdu);
my $out = $CSTAapdu->decode($pdu);
#$Data::Dumper::Indent=1;
#$Data::Dumper::Pair=' ';
#$Data::Dumper::Quotekeys=0;
#$Data::Dumper::Varname='CSTAapdu';
#print Dumper($out);
if ( defined $out->{svcResult}->{result}->{serviceResult}->{callingDevice} )
{
print $out->{svcResult}->{result}->{serviceResult}->{callingDevice}
->{both}->{deviceID}->{staticID}->{deviceIdentifier}->{dialingNumber},
"\n";
}
elsif ( defined $out->{svcError}->{parameter} ) {
#my $error_parameter = (keys %{$out->{svcError}->{parameter}})[0];
#print $error_parameter,"\n";
if ( defined $out->{svcError}->{parameter}->{operation} ) {
my $OperationErrors = $out->{svcError}->{parameter}->{operation};
if ( $OperationErrors == 12 ) {
print $dialingNumber, " - invalidDeviceID\n";
}
elsif ( $OperationErrors == 6 ) {
print $dialingNumber, " - invalidCalledDeviceID\n";
}
elsif ( $OperationErrors == 31 ) {
print $dialingNumber, " - invalidParameterValue\n";
}
elsif ( $OperationErrors == 2 ) {
print $dialingNumber, " - requestIncompatibleWithObject\n";
}
else {
print "OperationErrors:", $OperationErrors, "\n";
}
}
elsif ( defined $out->{svcError}->{parameter}->{stateIncompatibility} )
{
my $StateIncompatibilityErrors =
$out->{svcError}->{parameter}->{stateIncompatibility};
if ( $StateIncompatibilityErrors == 2 ) {
print $dialingNumber, " - invalidObjectState\n";
}
else {
print "StateIncompatibilityErrors:",
$StateIncompatibilityErrors;
}
}
elsif (
defined $out->{svcError}->{parameter}
->{systemResourceAvailability} )
{
my $SystemResourceAvailabilityErrors =
$out->{svcError}->{parameter}->{systemResourceAvailability};
if ( $SystemResourceAvailabilityErrors == 15 ) {
print $dialingNumber, " - deviceOutOfService\n";
}
else {
print "SystemResourceAvailabilityErrors:",
$SystemResourceAvailabilityErrors;
}
}
else {
print Dumper($out);
}
}
else {
print Dumper($out);
}
}
# parse ASN.1 desciptions
my $asn = Convert::ASN1->new;
#$asn->configure(tagdefault=>'EXPLICIT');
$asn->prepare(<<ASN1) or die "prepare: ", $asn->error;
CSTAapdu ::= CHOICE {
svcRequest ROIVapdu,
svcResult RORSapdu,
svcError ROERapdu
-- svcReject RORJapdu
}
ROIVapdu ::= [1] IMPLICIT SEQUENCE {
invokeID INTEGER,
serviceID INTEGER,
serviceArgs ANY DEFINED BY serviceID
}
RORSapdu ::= [2] IMPLICIT SEQUENCE {
invokeID INTEGER,
result SEQUENCE {
serviceID INTEGER,
serviceResult ANY DEFINED BY serviceID OPTIONAL
}
}
ROERapdu ::= [3] IMPLICIT SEQUENCE {
invokeID INTEGER,
code INTEGER, -- local:1
parameter UniversalFailure
}
UniversalFailure ::= CHOICE
{ operation [0] EXPLICIT OperationErrors,
stateIncompatibility [2] EXPLICIT StateIncompatibilityErrors,
systemResourceAvailability [3] EXPLICIT SystemResourceAvailabilityErrors,
unspecified [7] UnspecifiedErrors
}
OperationErrors ::= ENUMERATED
{
invalidDeviceID (12),
invalidCalledDeviceID (6),
invalidParameterValue (31),
requestIncompatibleWithObject (2)
}
StateIncompatibilityErrors ::= ENUMERATED
{
invalidObjectState (2)
}
SystemResourceAvailabilityErrors ::= ENUMERATED
{
deviceOutOfService (15)
}
UnspecifiedErrors ::= NULL
--makeCall OPERATION ::=
--{ ARGUMENT MakeCallArgument
-- RESULT MakeCallResult
-- ERRORS {universalFailure}
-- CODE local: 10
--}
MakeCallArgument ::= SEQUENCE
{ callingDevice DeviceID,
calledDirectoryNumber DeviceID
}
MakeCallResult ::= SEQUENCE
{ callingDevice ConnectionID}
ConnectionID ::= [APPLICATION 11] CHOICE
{
deviceID [1] LocalDeviceID,
both SEQUENCE
{ callID [0] IMPLICIT CallID,
deviceID [1] LocalDeviceID
}
}
CallID ::= OCTET STRING
LocalDeviceID ::= CHOICE
{ staticID DeviceID}
DeviceID ::= SEQUENCE
{ deviceIdentifier CHOICE
{ dialingNumber [0] IMPLICIT NumberDigits,
deviceNumber [1] IMPLICIT DeviceNumber,
other [6] IMPLICIT OtherPlan
}
}
OtherPlan ::= OCTET STRING
NumberDigits ::= IA5String
DeviceNumber ::= INTEGER
systemStatus ::= CHOICE
{ ARGUMENT SystemStatusArg,
RESULT SystemStatusRes
-- ERRORS {universalFailure}
-- CODE local: 211
}
SystemStatusArg ::= SEQUENCE
{ systemStatus SystemStatus}
SystemStatusRes ::= CHOICE
{ noData NULL}
SystemStatus ::= ENUMERATED
{ normal (2),
messageLost (3),
overloadReached (6)
}
ASN1
my %serviceArgs = ( #51=> 'EscapeArgument',
10 => 'MakeCallArgument',
211 => 'SystemStatusArg'
);
foreach ( keys %serviceArgs ) {
$asn->registertype( 'serviceArgs', $_, $asn->find( $serviceArgs{$_} ) );
}
my %serviceResults = ( #51=> 'EscapeResult',
10 => 'MakeCallResult',
211 => 'SystemStatusRes'
);
foreach ( keys %serviceResults ) {
$asn->registertype( 'serviceResult', $_,
$asn->find( $serviceResults{$_} ) );
}
$CSTAapdu = $asn->find('CSTAapdu');
csta_connect( { 'host' => '192.168.0.66', 'port' => 33333 } );
print "makeCall:\n";
my @devices = ( 106, 111, 155, 156 ); # список номеров
my $i = 0;
for (@devices) {
makeCall($_);
if ( $i++ % 2 ) { # 2 - количество каналов SVM
sleep 3 # задержка на сообщение
}
}
|