line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::UCP::Client; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
36277
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
43
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
1412
|
use Net::UCP; |
|
1
|
|
|
|
|
66149
|
|
|
1
|
|
|
|
|
54
|
|
7
|
1
|
|
|
1
|
|
8
|
use IO::Socket; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
8
|
1
|
|
|
1
|
|
686
|
use IO::Select; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
36
|
|
9
|
1
|
|
|
1
|
|
5
|
use Time::HiRes qw(usleep setitimer ITIMER_REAL); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
9
|
|
10
|
1
|
|
|
1
|
|
2590
|
use Encode; |
|
1
|
|
|
|
|
15352
|
|
|
1
|
|
|
|
|
158
|
|
11
|
|
|
|
|
|
|
#use Data::Dumper; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
require Exporter; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
16
|
|
|
|
|
|
|
our @EXPORT = qw(NONOTIFY NOTIFY BYPASS NOBYPASS); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
1
|
|
9
|
use constant NONOTIFY => 0; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
64
|
|
21
|
1
|
|
|
1
|
|
4
|
use constant NOTIFY => 1; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
36
|
|
22
|
1
|
|
|
1
|
|
5
|
use constant NOBYPASS => 0; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
45
|
|
23
|
1
|
|
|
1
|
|
4
|
use constant BYPASS => 1; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
24
|
|
|
|
|
|
|
|
25
|
1
|
|
|
1
|
|
5
|
use vars qw($childpid); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1756
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub new { |
28
|
0
|
|
0
|
0
|
0
|
|
my $class = shift || die "Missing class"; |
29
|
|
|
|
|
|
|
|
30
|
0
|
0
|
|
|
|
|
my $args = @_ == 1 ? shift : {@_}; |
31
|
0
|
|
|
|
|
|
my $self = bless {server => { %$args }}, $class; |
32
|
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
$self->{server}->{alert_wait} = 0; |
34
|
0
|
|
|
|
|
|
$self->{ucp_stuff} = new Net::UCP(FAKE => 1); |
35
|
0
|
|
|
|
|
|
return $self; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub run { |
39
|
0
|
0
|
|
0
|
0
|
|
my $self = ref($_[0]) ? shift() : shift->new(@_); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
#print Dumper(\@_); |
42
|
|
|
|
|
|
|
|
43
|
0
|
0
|
|
|
|
|
if ($self->establish_smsc_connection()) { |
44
|
|
|
|
|
|
|
|
45
|
0
|
0
|
0
|
|
|
|
if ($self->{server}->{user} && $self->{server}->{password}) { |
46
|
0
|
0
|
|
|
|
|
$self->login() ? $self->_run_loop() : die "Unable to login"; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
0
|
0
|
0
|
|
|
|
$self->_run_loop() if (defined $self->{server}->{bypass_auth} && $self->{server}->{bypass_auth}); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
} else { |
52
|
0
|
|
|
|
|
|
die "Unable to establish connection with $self->{server}->{smsc_host} on $self->{server}->{smsc_port} [$!]"; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub establish_smsc_connection { |
58
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
#print Dumper($self); |
61
|
|
|
|
|
|
|
|
62
|
0
|
0
|
|
|
|
|
$self->{server}->{socket} = IO::Socket::INET->new( |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
63
|
|
|
|
|
|
|
LocalAddr => (defined $self->{server}->{src_addr}) ? $self->{server}->{src_addr} : "", |
64
|
|
|
|
|
|
|
LocalPort => (defined $self->{server}->{src_port}) ? $self->{server}->{src_port} : 0, |
65
|
|
|
|
|
|
|
PeerAddr => $self->{server}->{smsc_host}, |
66
|
|
|
|
|
|
|
PeerPort => $self->{server}->{smsc_port}, |
67
|
|
|
|
|
|
|
Proto => 'tcp', |
68
|
|
|
|
|
|
|
Reuse => 1, |
69
|
|
|
|
|
|
|
) or return(0); |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
return 1; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub login { |
75
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
my $ucp_login = $self->{ucp_stuff}->make_message(op => '60', |
78
|
|
|
|
|
|
|
operation => 1, |
79
|
|
|
|
|
|
|
styp => 1, |
80
|
|
|
|
|
|
|
oadc => $self->{server}->{user}, |
81
|
|
|
|
|
|
|
pwd => $self->{server}->{password}, |
82
|
|
|
|
|
|
|
vers => '0100', |
83
|
|
|
|
|
|
|
); |
84
|
|
|
|
|
|
|
|
85
|
0
|
0
|
|
|
|
|
if (defined $ucp_login) { |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
$self->{ucp_stuff}->{SOCK} = $self->{server}->{socket}; |
88
|
0
|
|
|
|
|
|
my ($ack, $e_num, $resp) = $self->{ucp_stuff}->transmit_msg($ucp_login, $self->{server}->{timeout}, 1); |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
$self->log("UCP Client login() : ACK = $ack, Error Number = $e_num, Message = $resp"); |
91
|
0
|
0
|
|
|
|
|
return 0 if ($e_num) |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
} else { |
94
|
0
|
|
|
|
|
|
return 0; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
return 1; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub log { |
101
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
102
|
0
|
|
|
|
|
|
my $msg = shift; |
103
|
|
|
|
|
|
|
|
104
|
0
|
0
|
|
|
|
|
if (defined ($self->{server}->{log_file})) { |
105
|
0
|
0
|
|
|
|
|
if (open(LOG, ">>$self->{server}->{log_file}")) { |
106
|
0
|
|
|
|
|
|
print LOG $msg . "\n"; |
107
|
|
|
|
|
|
|
} else { |
108
|
0
|
|
|
|
|
|
warn "[Unable to open log file $!]"; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
} else { |
111
|
0
|
0
|
0
|
|
|
|
print $msg . "\n" if defined($self->{server}->{debug}) && $self->{server}->{debug}; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
return; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub _run_loop { |
118
|
0
|
|
|
0
|
|
|
my $self = shift; |
119
|
0
|
|
|
|
|
|
my $pid; |
120
|
|
|
|
|
|
|
|
121
|
0
|
0
|
|
|
|
|
if ($pid = fork) { |
|
|
0
|
|
|
|
|
|
122
|
0
|
|
|
|
|
|
$self->log("UCP Client : Forking child listener [$pid]"); |
123
|
0
|
|
|
|
|
|
$self->set_signal(); |
124
|
0
|
|
|
|
|
|
$childpid = $pid; |
125
|
|
|
|
|
|
|
} elsif (defined($pid)) { |
126
|
0
|
|
|
|
|
|
$self->start_listener(); |
127
|
|
|
|
|
|
|
} else { |
128
|
0
|
|
|
|
|
|
$self->log("Unable to start listener"); |
129
|
0
|
|
|
|
|
|
die "Unable to start listener [$!]"; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
for (;;) { usleep(500_000); } |
|
0
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
return; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub set_signal { |
137
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
138
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
$SIG{CHLD} = \&child_die; |
140
|
0
|
|
|
|
|
|
$SIG{INT} = \&must_die; |
141
|
0
|
|
|
|
|
|
$SIG{HUP} = \&must_die; |
142
|
0
|
|
|
|
|
|
$SIG{TERM} = \&must_die; |
143
|
|
|
|
|
|
|
|
144
|
0
|
0
|
0
|
|
|
|
if (exists $self->{server}->{send_hook_time} && $self->{server}->{send_hook_time} =~ m/\d+/) { |
145
|
|
|
|
|
|
|
#print "Set signal\n"; |
146
|
|
|
|
|
|
|
$SIG{ALRM} = sub { |
147
|
0
|
|
|
0
|
|
|
setitimer(ITIMER_REAL, 0, 0); |
148
|
0
|
|
|
|
|
|
$self->{server}->{alert_wait} += $self->{server}->{send_hook_time}; |
149
|
0
|
0
|
|
|
|
|
if ($self->{server}->{alert_time} <= $self->{server}->{alert_wait}) { |
150
|
0
|
|
|
|
|
|
$self->{server}->{alert_wait} = 0; #init alert time counter :: TODO :: |
151
|
0
|
|
|
|
|
|
$self->send_alert("0123456789", "0539"); #you will see an error in ucp response, but it will work as a keep alive :) |
152
|
|
|
|
|
|
|
} |
153
|
0
|
|
|
|
|
|
$self->{server}->{send_hook}(); |
154
|
0
|
|
|
|
|
|
setitimer(ITIMER_REAL, $self->{server}->{send_hook_time}, 0); |
155
|
0
|
|
|
|
|
|
}; |
156
|
0
|
|
|
|
|
|
setitimer(ITIMER_REAL, $self->{server}->{send_hook_time}, 0); |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
0
|
|
|
|
|
|
return; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub child_die { |
163
|
0
|
|
|
0
|
0
|
|
my $waitedpid = wait; |
164
|
0
|
|
|
|
|
|
$SIG{CHLD} = \&child_die; |
165
|
0
|
|
|
|
|
|
return; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
sub must_die { |
169
|
0
|
0
|
|
0
|
0
|
|
kill 9, $childpid if ($childpid); |
170
|
0
|
|
|
|
|
|
exit; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
sub start_listener { |
174
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
175
|
|
|
|
|
|
|
|
176
|
0
|
|
|
|
|
|
my (@ready, $fh); |
177
|
0
|
|
|
|
|
|
my $s = new IO::Select($self->{server}->{socket}); |
178
|
|
|
|
|
|
|
|
179
|
0
|
|
|
|
|
|
while(@ready = $s->can_read) { |
180
|
0
|
|
|
|
|
|
foreach $fh (@ready) { #we have only one handler but it could be useful in the future |
181
|
0
|
|
|
|
|
|
my ($buffer, $response); |
182
|
|
|
|
|
|
|
|
183
|
0
|
|
|
|
|
|
$buffer = ''; |
184
|
0
|
|
|
|
|
|
$response = ''; |
185
|
|
|
|
|
|
|
|
186
|
0
|
|
|
|
|
|
do { |
187
|
0
|
|
|
|
|
|
read($self->{server}->{socket}, $buffer, 1); |
188
|
0
|
|
|
|
|
|
$response .= $buffer; |
189
|
|
|
|
|
|
|
} until ($buffer eq $self->{ucp_stuff}->{OBJ_EMI_COMMON}->ETX); |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
# print "RESP : " . $response . "\n"; |
192
|
|
|
|
|
|
|
|
193
|
0
|
|
|
|
|
|
$self->{ucp_stuff}->remove_ucp_enclosure(\$response); |
194
|
0
|
|
|
|
|
|
my $ref_mes = $self->{ucp_stuff}->parse_message($response); |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
# print "RESP 2 : " . $response . "\n"; |
197
|
|
|
|
|
|
|
# print Dumper($ref_mes); |
198
|
|
|
|
|
|
|
|
199
|
0
|
0
|
|
|
|
|
if ($self->control_checksum($ref_mes)) { |
200
|
0
|
|
|
|
|
|
$self->log("Checksum control : Checksum OK for $response"); |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
0
|
0
|
|
|
|
|
if ($ref_mes->{ot} eq "01") { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
204
|
0
|
|
|
|
|
|
$self->{server}->{op_01}($ref_mes); |
205
|
|
|
|
|
|
|
} elsif ($ref_mes->{ot} eq "02") { |
206
|
0
|
|
|
|
|
|
$self->{server}->{op_02}($ref_mes) |
207
|
|
|
|
|
|
|
} elsif ($ref_mes->{ot} eq "31") { |
208
|
0
|
|
|
|
|
|
$self->{server}->{op_31}($ref_mes); |
209
|
|
|
|
|
|
|
} elsif ($ref_mes->{ot} eq "51") { |
210
|
0
|
|
|
|
|
|
$self->{server}->{op_51}($ref_mes); |
211
|
|
|
|
|
|
|
} elsif ($ref_mes->{ot} eq "52") { |
212
|
0
|
|
|
|
|
|
$self->{server}->{op_52}($ref_mes); |
213
|
|
|
|
|
|
|
} elsif ($ref_mes->{ot} eq "53") { |
214
|
0
|
|
|
|
|
|
$self->{server}->{op_53}($ref_mes); |
215
|
|
|
|
|
|
|
} else { |
216
|
0
|
|
|
|
|
|
$self->log("No hook for this operation NUM : [$ref_mes->{op}] Contact the author and tell him to add something in this module..."); |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
sub control_checksum { |
224
|
0
|
|
|
0
|
0
|
|
shift; |
225
|
0
|
|
|
|
|
|
my $mes = shift; |
226
|
0
|
0
|
|
|
|
|
($mes->{my_checksum} eq $mes->{checksum}) ? return 1 : return 0; |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
sub send_alert { |
230
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
231
|
0
|
|
|
|
|
|
my ($adc, $pid) = @_; |
232
|
|
|
|
|
|
|
|
233
|
0
|
0
|
|
|
|
|
my $ucp_alert = $self->{ucp_stuff}->make_31(operation => 1, |
|
|
0
|
|
|
|
|
|
234
|
|
|
|
|
|
|
adc => defined($adc) ? $adc : "", #<-- you need to pass it |
235
|
|
|
|
|
|
|
pid => defined($pid) ? $pid : "0539", |
236
|
|
|
|
|
|
|
); |
237
|
|
|
|
|
|
|
|
238
|
0
|
|
|
|
|
|
my ($ack, $e_num, $resp) = $self->{ucp_stuff}->transmit_msg($ucp_alert, $self->{server}->{timeout}, 0); |
239
|
0
|
|
|
|
|
|
return; |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
sub send_sms { |
243
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
244
|
0
|
|
|
|
|
|
my ($phone_number, $from, $text, $delivery_notification) = @_; |
245
|
|
|
|
|
|
|
|
246
|
0
|
0
|
|
|
|
|
if (defined $self->{ucp_stuff} ) { |
247
|
|
|
|
|
|
|
|
248
|
0
|
|
|
|
|
|
my $ucp_string = $self->{ucp_stuff}->make_message( |
249
|
|
|
|
|
|
|
op => '51', |
250
|
|
|
|
|
|
|
operation => 1, |
251
|
|
|
|
|
|
|
adc => $phone_number, |
252
|
|
|
|
|
|
|
oadc => $from, |
253
|
|
|
|
|
|
|
mt => 3, |
254
|
|
|
|
|
|
|
amsg => $text, |
255
|
|
|
|
|
|
|
mcls => 1, |
256
|
|
|
|
|
|
|
otoa => "5039", |
257
|
|
|
|
|
|
|
); |
258
|
0
|
0
|
|
|
|
|
if (defined($ucp_string)) { |
259
|
|
|
|
|
|
|
|
260
|
0
|
|
|
|
|
|
print $ucp_string . "\n"; |
261
|
|
|
|
|
|
|
|
262
|
0
|
|
|
|
|
|
$self->{ucp_stuff}->transmit_msg($ucp_string, $self->{server}->{timeout}, 0); |
263
|
0
|
|
|
|
|
|
$self->log("UCP Client op51 send_sms(): $phone_number, $from, $text"); |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
} else { |
266
|
0
|
|
|
|
|
|
$self->log("Error while making UCP String OP 51 $phone_number $from $text"); |
267
|
|
|
|
|
|
|
} |
268
|
|
|
|
|
|
|
|
269
|
0
|
|
|
|
|
|
return 1; |
270
|
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
|
272
|
0
|
|
|
|
|
|
return 0; |
273
|
|
|
|
|
|
|
} |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
#Overwritable methods |
276
|
|
|
|
|
|
|
#sub op_01 {}; |
277
|
|
|
|
|
|
|
#sub op_31 {}; |
278
|
|
|
|
|
|
|
#sub op_51 {}; |
279
|
|
|
|
|
|
|
#sub op_52 {}; |
280
|
|
|
|
|
|
|
#sub op_53 {}; |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
1; |
283
|
|
|
|
|
|
|
__END__ |