SNPP/Server.pm

filter: Statementstmt 34.7% 92 / 265 Branchbran 14.3% 21 / 146 Conditioncond 6.0% 4 / 66 MC/DCmcdc 2.7% 1 / 37 Subroutinesub 51.8% 14 / 27 Podpod 91.6% 11 / 12 totaltotal 25.8% 143 / 553 CC 97 SCAR 73.0
CC
97
Cov
25%
handle_client
3054.2
new
46.5
write_to_pipe
42.0
CRAP 1475.6
↑ Summary SNPP.pm »
1 package Net::SNPP::Server;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Net::Cmd;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use vars qw( @ISA $counter );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 @ISA = qw( IO::Handle Net::Cmd );
11 3 $counter = 0;
12  
13 =head1 NAME
14  
15 Net::SNPP::Server
16  
17 =head1 DESCRIPTION
18  
19 An object interface for creating SNPP servers. Almost everything you
20 need to create your very own SNPP server is here in this module.
21 There is a callback() method that can replace default function with
22 your own.
23 them. Any SNPP command can be overridden or new/custom ones can be
24 created using custom_command(). To disable commands you just don't
25 want to deal with, use disable_command().
26  
27 =head1 SYNOPSIS
28  
29 There may be a synopsis here someday ...
30  
31 =head1 METHODS
32  
33 =over 4
34  
35 =item new()
36  
37 Create a Net::SNPP::Server object listening on a port. By default, it only
38 listens on the localhost (127.0.0.1) - specify MultiHomed to listen on all
39 addresses or LocalAddr to listen on only one.
40  
41 my $svr = Net::SNPP::Server->new(
42 Port => port to listen on
43 BindTo => interface address to bind to
44 MultiHomed => listen on all interfaces if true (and BindTo is unset)
45 Listen => how many simultaneous connections to handle (SOMAXCONN)
46 # the following two options are only used by handle_client()
47 MaxErrors => maximum number of errors before disconnecting client
48 Timeout => timeout while waiting for data (uses SIGARLM)
49 );
50  
51 =cut
52  
53 3 ▶sub new {
Subroutine new: called CC=14 SCAR=38.4
Pod documented
54 3 my( $class, %args ) = @_;
55 3 my $self = {};
56  
57 # set defaults for basic parameters
58 3 ▶ if ( !exists($args{Listen}) ) { $args{Listen} = SOMAXCONN }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"Listen"}) 3 0 3
59 0 ▶ if ( !exists($args{Port}) ) { $args{Port} = 444 }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"Port"}) 0 3 3
60  
61 # choose either a unix domain socket or an inet socket
62 3 ▶ if ( !exists($args{UnixSocket}) ) { $args{Domain} = AF_INET }
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args{'UnixSocket'}) { } 3 0 3
63 0 else { $args{Domain} = PF_UNIX }
64  
65 # by default, bind only to the loopback interface
66 # i.e. MultiHomed and BindTo were not specified
67 3 ▶ if ( !exists($args{MultiHomed}) && !exists($args{BindTo}) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $args{'MultiHomed'} || exists $args{'BindTo'}) { } 3 0 3
elsif (exists $args{'BindTo'}) { } 0 0 0
Condition33%1/3
exists $args{'MultiHomed'} || exists $args{'BindTo'}
l!l&&r!l&&!r
003
MC/DC0%0/2
exists $args{'MultiHomed'} || exists $args{'BindTo'}
exists $args{'MultiHomed'} exists $args{'BindTo'}
Truth table33%1/3
A || B
A= exists $args{'MultiHomed'}B= exists $args{'BindTo'}
ABresult
1X1
011
000
68 3 $args{BindTo} = INADDR_LOOPBACK;
69 }
70 # if a bind address is passed in, bind to it
71 elsif ( exists($args{BindTo}) ) {
72 0 $args{BindTo} = inet_aton( $args{BindTo} );
73 }
74 # bind to all interfaces if MultiHomed is defined
75 # and BindTo is not
76 else {
77 0 $args{BindTo} = INADDR_ANY;
78 }
79  
80 # these two values are only used by the handle_client method
81 3 $self->{'MaxErrors'} = delete($args{MaxErrors});
82 3 $self->{'Timeout'} = delete($args{Timeout});
83  
84 # create the socket by hand instead of IO::Socket::INET to
85 # make manipulation a little easier within this module
86 3 $self->{sock} = IO::Handle->new();
87 3 ▶ socket( $self->{sock}, $args{Domain}, SOCK_STREAM, getprotobyname('tcp') )
Branch: 1 branch
BranchTrueFalseTotal
CORE::socket $self->{"sock"}, $args{"Domain"}, 1, getprotobyname "tcp" or croak("couldn't create socket: $!") 3 0 3
88 || croak "couldn't create socket: $!";
89 3 setsockopt( $self->{sock}, SOL_SOCKET, SO_REUSEADDR, 1 );
90  
91 3 ▶ if ( $args{Domain} == PF_UNIX ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'Domain'} == 1) { } 0 3 3
92 0 ▶ if ( -e $args{UnixSocket} ) { unlink( $args{UnixSocket} ) }
Branch: 1 branch
BranchTrueFalseTotal
if (-e $args{"UnixSocket"}) 0 0 0
93 $self->{sockaddr} = sockaddr_un( $args{UnixSocket} )
94 0 ▶ || croak "couldn't get socket address: $!";
Condition0%0/2
sockaddr_un($args{'UnixSocket'}) || croak("couldn't get socket address: $!")
l!l
00
MC/DC0%0/1
sockaddr_un($args{'UnixSocket'}) || croak("couldn't get socket address: $!")
sockaddr_un($args{'UnixSocket'})
Truth table0%0/2
A
A= sockaddr_un($args{'UnixSocket'})
Aresult
11
00
95 }
96 else {
97 $self->{sockaddr} = sockaddr_in( $args{Port}, $args{BindTo} )
98 3 ▶ || croak "couldn't get socket address: $!";
Condition50%1/2
sockaddr_in($args{'Port'}, $args{'BindTo'}) || croak("couldn't get socket address: $!")
l!l
30
MC/DC0%0/1
sockaddr_in($args{'Port'}, $args{'BindTo'}) || croak("couldn't get socket address: $!")
sockaddr_in($args{'Port'}, $args{'BindTo'})
Truth table50%1/2
A
A= sockaddr_in($args{'Port'}, $args{'BindTo'})
Aresult
11
00
99 }
100  
101 bind( $self->{sock}, $self->{sockaddr} )
102 3 ▶ || croak "could not bind socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
bind $self->{"sock"}, $self->{"sockaddr"} or croak("could not bind socket: $!") 3 0 3
103  
104 listen( $self->{sock}, $args{Listen} )
105 3 ▶ || croak "could not listen on socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
listen $self->{"sock"}, $args{"Listen"} or croak("could not listen on socket: $!") 3 0 3
106  
107 # set default callbacks
108 $self->{CB} = {
109 0 ▶ process_page => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
110 0 my( $pgr, $page, $results ) = @_;
111 0 push( @$results, [ $pgr, $page ] );
112 },
113 0 ▶ validate_pager_id => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
114 0 ▶ return undef if ( $_[0] =~ /\D/ || length($_[0]) < 7 );
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /\D/ or length $_[0] < 7 0 0 0
Condition0%0/3
$_[0] =~ /\D/ or length $_[0] < 7
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0] =~ /\D/ or length $_[0] < 7
$_[0] =~ /\D/ length $_[0] < 7
Truth table0%0/3
A or B
A= $_[0] =~ /\D/B= length $_[0] < 7
ABresult
1X1
011
000
115 0 return $_[0];
116 },
117 0 ▶ validate_pager_pin => sub { $_[1] || 1 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] or 1 0 0 0
118 0 ▶ write_log => sub { print STDERR "@_\n" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
119 0 ▶ create_id_and_pin => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 0 srand(); # re-seed the pseudrandom number generator
121 0 return( time().$counter, int(rand(1000000000)) );
122 }
123 3 };
124  
125 # initialize disabled and custom commands hashrefs
126 3 $self->{disabled} = {};
127 3 $self->{custom} = {};
128
129 3 return bless( $self, $class );
130 }
131  
132 =item client()
133  
134 Calls accept() for you and returns a client handle. This method
135 will block if there is no waiting client. The handle returned
136 is a subclass of IO::Handle, so all IO::Handle methods should work.
137 my $client = $server->client();
138  
139 =cut
140  
141 2 ▶sub client {
Subroutine client: called CC=1 SCAR=0.0
Pod documented
142 2 my $handle = IO::Handle->new();
143 2 accept( $handle, $_[0]->{sock} );
144 2 return bless($handle, ref($_[0]));
145 }
146  
147 =item ip()
148  
149 Return the IP address associated with a client handle.
150 printf "connection from %s", $client->ip();
151  
152 =cut
153  
154 0 ▶sub ip {
Subroutine ip: not called CC=2 SCAR=17.9
Pod documented
155 0 my $remote_client = getpeername($_[0]);
156 0 ▶ return 'xxx.xxx.xxx.xxx' if ( !defined($remote_client) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $remote_client 0 0 0
157 0 my($port,$iaddr) = unpack_sockaddr_in($remote_client);
158 0 return inet_ntoa($iaddr);
159 }
160  
161 =item socket()
162  
163 Returns the raw socket handle. This mainly exists for use with select() or
164 IO::Select.
165 my $select = IO::Select->new();
166 $select->add( $server->socket() );
167  
168 =cut
169  
170 0 ▶sub socket { $_[0]->{sock}; }
Subroutine socket: not called CC=1 SCAR=6.9
Pod documented
171  
172 =item connected()
173  
174 For use with a client handle. True if server socket is still alive.
175  
176 =cut
177  
178 0 ▶sub connected { $_[0]->opened() && getpeername($_[0]) }
Subroutine connected: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->opened and getpeername $_[0] 0 0 0
179  
180 =item shutdown()
181  
182 Shuts down the server socket.
183 $server->shutdown(2);
184  
185 =cut
186  
187 0 ▶sub shutdown { shutdown($_[0],$_[1] || 2) }
Subroutine shutdown: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_[1] || 2
l!l
00
MC/DC0%0/1
$_[1] || 2
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
188  
189 =item callback()
190  
191 Insert a callback into Server.pm.
192 $server->callback( 'process_page', \&my_function );
193 $server->callback( 'validate_pager_id', \&my_function );
194 $server->callback( 'validate_pager_pin', \&my_function );
195 $server->callback( 'write_log', \&my_function );
196 $server->callback( 'create_id_and_pin', \&my_function );
197  
198 =over 2
199  
200 =item process_page( $PAGER_ID, \%PAGE, \@RESULTS )
201  
202 $PAGER_ID = [
203 0 => retval of validate_pager_id
204 1 => retval of validate_pager_pin
205 ]
206 $PAGE = {
207 mess => $,
208 responses => [],
209 }
210  
211 =item validate_pager_id( PAGER_ID )
212  
213 The return value of this callback will be saved as the pager id
214 that is passed to the process_page callback as the first list
215 element of the first argument.
216  
217 =item validate_pager_pin( VALIDATED_PAGER_ID, PIN )
218  
219 The value returned by this callback will be saved as the second
220 list element in the first argument to process_page.
221 The PAGER_ID input to this callback is the output from the
222 validate_pager_id callback.
223  
224 NOTE: If you really care about the PIN, you must use this callback. The default callback will return 1 if the pin is not set.
225  
226 =item write_log
227  
228 First argument is a Unix syslog level, such as "warning" or "info."
229 The rest of the arguments are the message. Return value is ignored.
230  
231 =item create_id_and_pin
232  
233 Create an ID and PIN for a 2way message.
234  
235 =back
236  
237 =cut
238  
239 5 ▶sub callback ($ $ $) {
Subroutine callback: called CC=3 SCAR=12.5
Pod documented
240 0 croak "first argument callback() to must be one of: ", join(', ', keys(%{$_[0]->{CB}}))
241 5 ▶ if ( !exists($_[0]->{CB}{$_[1]}) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_[0]{"CB"}{$_[1]} 0 5 5
242 5 ▶ croak "second argument callback() to must be a CODE ref"
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[2] ne "CODE" 0 5 5
243 if ( ref($_[2]) ne 'CODE' );
244 5 $_[0]->{CB}{$_[1]} = $_[2];
245 }
246  
247 =item custom_command()
248  
249 Create a custom command or override a default command in handle_client().
250 The command name must be 4 letters or numbers. The second argument is a coderef
251 that should return a text command, i.e. "250 OK" and some "defined" value to continue the
252 client loop. +++If no value is set, the client will be disconnected after
253 executing your command.+++ If you need MSTA or KTAG, this
254 is the hook you need to implement them.
255  
256 The subroutine will be passed the command arguments, split on whitespace.
257  
258 sub my_MSTA_sub {
259 my( $id, $password ) = @_;
260 # ...
261 return "250 OK", 1;
262 }
263 $server->custom_command( "MSTA", \&my_MSTA_sub );
264  
265 =cut
266  
267 0 ▶sub custom_command ($ $ $) {
Subroutine custom_command: not called CC=3 SCAR=24.8
Pod documented
268 0 ▶ croak "first argument to custom_command must be exactly 4 characters"
Branch: 1 branch
BranchTrueFalseTotal
if length $_[1] != 4 0 0 0
269 if ( length($_[1]) != 4 );
270 0 ▶ croak "second argument to custom_command must be a coderef"
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[2] ne "CODE" 0 0 0
271 if ( ref($_[2]) ne 'CODE' );
272 0 $_[0]->{custom}{uc($_[1])} = $_[2];
273 }
274  
275 =item disable_command()
276  
277 Specify a command to disable in the server. This is useful, for instance,
278 if you don't want to support level 3 commands.
279 $server->disable_command( "2WAY", "550 2WAY not supported here" );
280  
281 The second argument is an optional custom error message. The default is:
282 "500 Command Not Implemented, Try Again"
283  
284 =cut
285  
286 0 ▶sub disable_command {
Subroutine disable_command: not called CC=2 SCAR=17.9
Pod documented
287 # shorten & uppercase it so it matches in handle_client
288 0 my $cmd = unpack('A4',uc($_[1]));
289  
290 0 ▶ if ( defined($_[2]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[2]) { } 0 0 0
291 0 $_[0]->{disabled}{$cmd} = $_[2];
292 }
293 else {
294 0 $_[0]->{disabled}{$cmd} = "500 Command Not Implemented, Try Again";
295 }
296 }
297  
298 =item handle_client()
299  
300 Takes the result of $server->client() and takes care of parsing
301 the user input. This should be quite close to being rfc1861
302 compliant. If you specified Timeout to be something other
303 than 0 in new(), SIGARLM will be used to set a timeout. If you
304 use this, make sure to take signals into account when writing your
305 code. fork()'ing before calling handle_client is a good way
306 to avoid interrupting code that shouldn't be interrupted.
307  
308 =cut
309  
310 1 ▶sub handle_client ($ $) {
Subroutine handle_client: called CC=59 SCAR=80.2
Pod documented
311 1 my( $self, $client ) = @_;
312 1 my $page = {}; # store the stuff the user gives us in this hash
313 1 my @pgrs = (); # store the list of pagers
314 # each pager is an array ref [ $pager_id, $pin ]
315 1 my @retvals = (); # build up a list of return values
316 1 my $errors = 0; # count the errors for maximum errors
317 1 my $timeout = 0;
318 1 local(%SIG);
319  
320 # enable timeouts if user requested passed Timeout to new()
321 1 ▶ if ( $self->{'Timeout'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Timeout"}) 0 1 1
322 0 ▶ $SIG{ALRM} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
323 0 $self->{CB}{write_log}->( 'debug', "client timeout" );
324 0 $client->command( "421 Timeout, Goodbye" );
325 0 $client->shutdown(2);
326 0 $timeout = 1;
327 0 };
328 0 alarm( $self->{'Timeout'} );
329 }
330  
331 # let the client know we're ready for them
332 1 $client->command( "220 SNPP Gateway Ready" );
333  
334 0 $self->{CB}{write_log}->( 'debug', "client connected" );
335  
336 # loop until timeout or client quits
337 0 ▶ while ( $timeout == 0 && (my $input = $client->getline()) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout == 0 and my $input = $client->getline) 0 0 0
Condition0%0/3
$timeout == 0 and my $input = $client->getline
!ll&&!rl&&r
000
MC/DC0%0/2
$timeout == 0 and my $input = $client->getline
$timeout == 0 my $input = $client->getline
Truth table0%0/3
A and B
A= $timeout == 0B= my $input = $client->getline
ABresult
0X0
100
111
338 # clean \n\r's out of input, then split it up by whitespace
339 0 $input =~ s/[\r\n]+//gs;
340 0 my @cmd = split( /\s+/, $input );
341  
342 # uppercase and truncate the command shifted from @cmd to 4 characters
343 0 my $user_cmd = unpack('A4',uc(shift(@cmd)));
344 0 ▶ if ( length($user_cmd) != 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $user_cmd != 4) 0 0 0
345 # FIXME: put in correct full text from RFC document
346 0 $client->command( "550 Error, Invalid Command" );
347 }
348  
349 0 $self->{CB}{write_log}->( 'debug', "processing command '$user_cmd @cmd'" );
350
351 # //////////////////////////////////////////////////////////////////// #
352 # BEGIN COMMANDS PARSING #
353 # \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ #
354  
355 ########################################################################
356 # user disabled commands --------------------------------------------- #
357 0 ▶ if ( exists($self->{disabled}{$user_cmd}) ) {
Branch: 14 branches
BranchTrueFalseTotal
if (exists $self->{'disabled'}{$user_cmd}) { } 0 0 0
elsif (exists $self->{'custom'}{$user_cmd}) { } 0 0 0
elsif ($user_cmd eq 'PAGE') { } 0 0 0
elsif ($user_cmd =~ /(MESS|SUBJ)/) { } 0 0 0
elsif ($user_cmd eq 'RESE') { } 0 0 0
elsif ($user_cmd eq 'SEND') { } 0 0 0
elsif ($user_cmd eq 'QUIT') { } 0 0 0
elsif ($user_cmd eq 'HELP') { } 0 0 0
elsif ($user_cmd eq 'DATA') { } 0 0 0
elsif ($user_cmd eq 'ALER') { } 0 0 0
elsif ($user_cmd eq 'HOLD') { } 0 0 0
elsif ($user_cmd eq '2WAY') { } 0 0 0
elsif ($user_cmd eq 'PING') { } 0 0 0
elsif ($user_cmd eq 'MCRE') { } 0 0 0
358 0 $errors++;
359 0 $client->command( $self->{disabled}{$user_cmd} );
360 }
361 ########################################################################
362 # user custom commands ----------------------------------------------- #
363 elsif ( exists($self->{custom}{$user_cmd}) ) {
364 0 my ($cmdtxt,$cont) = $self->{custom}{$user_cmd}->( @cmd );
365 0 $client->command( $cmdtxt );
366 0 ▶ last if ( !$cont );
Branch: 1 branch
BranchTrueFalseTotal
unless $cont 0 0 0
367 }
368 ########################################################################
369 # 4.3 Level 1 Commands #################################################
370 ########################################################################
371 # 4.3.1 PAGEr <Pager ID> --------------------------------------------- #
372 # 4.5.2 PAGEr <PagerID> [Password/PIN] ------------------------------- #
373 elsif ( $user_cmd eq 'PAGE' ) {
374 0 my $valid_pgr_id = $self->{CB}{validate_pager_id}->($cmd[0]);
375 0 my $valid_pin = $self->{CB}{validate_pager_pin}->($valid_pgr_id,$cmd[1]);
376 0 ▶ if ( $valid_pgr_id && $valid_pin ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valid_pgr_id and $valid_pin) { } 0 0 0
Condition0%0/3
$valid_pgr_id and $valid_pin
!ll&&!rl&&r
000
MC/DC0%0/2
$valid_pgr_id and $valid_pin
$valid_pgr_id $valid_pin
Truth table0%0/3
A and B
A= $valid_pgr_idB= $valid_pin
ABresult
0X0
100
111
377 0 push( @pgrs, [$valid_pgr_id,$valid_pin] );
378 0 $client->command( "250 Pager ID Accepted" );
379 }
380 else {
381 0 $errors++;
382 0 $client->command( "550 Error, Invalid Pager ID" );
383 }
384 }
385  
386 ########################################################################
387 # 4.3.2 MESSage <Alpha or Numeric Message> --------------------------- #
388 # 4.5.8 SUBJect <MessageSubject> ------------------------------------- #
389 elsif ( $user_cmd =~ /(MESS|SUBJ)/ ) {
390 0 my $key = $1;
391 0 ▶ if ( $key && $key eq 'MESS' && defined($page->{mess}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key and $key eq "MESS" and defined $page->{"mess"}) 0 0 0
Condition0%0/6
$key and $key eq "MESS" and defined $page->{"mess"}
!ll&&!rl&&r
000
$key and $key eq "MESS"
!ll&&!rl&&r
000
MC/DC0%0/3
$key and $key eq "MESS" and defined $page->{"mess"}
$key $key eq "MESS" defined $page->{"mess"}
Truth table0%0/4
A and B and C
A= $keyB= $key eq "MESS"C= defined $page->{"mess"}
ABCresult
0XX0
10X0
1100
1111
392 0 $errors++;
393 0 $client->command( "503 ERROR, Message Already Entered" );
394 0 next;
395 }
396 0 ▶ if ( !defined($cmd[0]) || $cmd[0] eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cmd[0] or $cmd[0] eq "") 0 0 0
Condition0%0/3
not defined $cmd[0] or $cmd[0] eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $cmd[0] or $cmd[0] eq ""
not defined $cmd[0] $cmd[0] eq ""
Truth table0%0/3
not(A) or B
A= not defined $cmd[0]B= $cmd[0] eq ""
ABresult
1X1
011
000
397 0 $errors++;
398 0 $client->command( "550 ERROR, Invalid Message" );
399 0 next;
400 }
401 0 $page->{lc($key)} = join(' ', @cmd);
402 0 $client->command( "250 Message OK" );
403 }
404  
405 ########################################################################
406 # 4.3.3 RESEt -------------------------------------------------------- #
407 elsif ( $user_cmd eq 'RESE' ) {
408 0 $page = {};
409 0 @pgrs = ();
410 0 $client->command( "250 RESET OK" );
411 }
412  
413 ########################################################################
414 # 4.3.4 SEND --------------------------------------------------------- #
415 elsif ( $user_cmd eq 'SEND' ) {
416 0 ▶ if ( @pgrs == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pgrs == 0) 0 0 0
417 0 $errors++;
418 0 $client->command( "503 Error, Pager ID needed" );
419 0 next;
420 }
421 0 ▶ if ( !exists($page->{mess}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $page->{"mess"}) 0 0 0
422 0 $errors++;
423 0 $client->command( "503 Error, Pager ID or Message Incomplete" );
424 0 next;
425 }
426  
427 0 my $res = undef;
428 0 ▶ for ( my $i=0; $i<@pgrs; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @pgrs) 0 0 0
429 0 ▶ if ( !exists($page->{alert}) ) { $page->{alert} = 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $page->{"alert"}) 0 0 0
430 0 ▶ if ( !exists($page->{hold}) ) { $page->{hold} = 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $page->{"hold"}) 0 0 0
431  
432 # call the callback subroutine with the data
433 # the default callback just pushes the data onto @retvals
434 0 $res = $self->{CB}{process_page}->( $pgrs[$i], $page, \@retvals );
435 }
436 0 ▶ if ( $res && exists($page->{twoway}) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res and exists $page->{'twoway'}) { } 0 0 0
elsif ($res) { } 0 0 0
Condition0%0/3
$res and exists $page->{"twoway"}
!ll&&!rl&&r
000
MC/DC0%0/2
$res and exists $page->{"twoway"}
$res exists $page->{"twoway"}
Truth table0%0/3
A and B
A= $resB= exists $page->{"twoway"}
ABresult
0X0
100
111
437 # this callback generates the two numbers for identifying a page
438 0 my @tags = $self->{CB}{create_id_and_pin}->( \@pgrs, $page );
439 0 $client->command( "960 @tags OK, Message QUEUED for Delivery" );
440 }
441 elsif ( $res ) {
442 0 $client->command( "250 Message Sent Successfully" );
443 }
444 else {
445 0 $client->command( "554 Error, failed" );
446 0 next;
447 }
448 # RESEt
449 0 @pgrs = ();
450 0 $page = {};
451 }
452  
453 ########################################################################
454 elsif ( $user_cmd eq 'QUIT' ) {
455 0 $client->command( "221 OK, Goodbye" );
456 0 last;
457 }
458  
459 ########################################################################
460 # 4.3.6 HELP (optional) ---------------------------------------------- #
461 elsif ( $user_cmd eq 'HELP' ) {
462 {
463 0 ▶ no warnings; # so we can use <DATA>
Subroutine BEGIN: called CC=1 SCAR=0.2
464 0 ▶ while (<DATA>) { $client->command( $_ ) }
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DATA)) 0 0 0
465 0 $client->command( "250 End of Help Information" );
466 }
467 }
468  
469 ########################################################################
470 ## 4.4 Level 2 - Minimum Extensions ####################################
471 ########################################################################
472 # 4.4.1 DATA --------------------------------------------------------- #
473 elsif ( $user_cmd eq 'DATA' ) {
474 0 $client->command( "354 Begin Input; End with <CRLF>'.'<CRLF>" );
475 0 my $buffer = join( '', @{ $client->read_until_dot() } );
476 0 ▶ if ( !defined($buffer) || !length($buffer) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $buffer && length $buffer) { } 0 0 0
Condition0%0/3
defined $buffer && length $buffer
!ll&&!rl&&r
000
MC/DC0%0/2
defined $buffer && length $buffer
defined $buffer length $buffer
Truth table0%0/3
A && B
A= defined $bufferB= length $buffer
ABresult
0X0
100
111
477 0 $errors++;
478 0 $client->command( "550 Error, Blank Message" );
479 }
480 else {
481 0 $buffer =~ s/[\r\n]+/\n/gs;
482 0 $page->{mess} = $buffer;
483 0 $client->command( "250 Message OK" );
484 }
485 }
486  
487 ########################################################################
488 ## 4.5 Level 2 - Optional Extensions ###################################
489 ########################################################################
490 # 4.5.4 ALERt <AlertOverride> ---------------------------------------- #
491 elsif ( $user_cmd eq 'ALER' ) {
492 0 ▶ if ( defined($cmd[0]) && ($cmd[0] == 1 || $cmd[0] == 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cmd[0] and $cmd[0] == 1 || $cmd[0] == 0) { } 0 0 0
Condition0%0/6
defined $cmd[0] and $cmd[0] == 1 || $cmd[0] == 0
!ll&&!rl&&r
000
$cmd[0] == 1 or $cmd[0] == 0
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $cmd[0] and $cmd[0] == 1 || $cmd[0] == 0
defined $cmd[0] $cmd[0] == 1 $cmd[0] == 0
Truth table0%0/4
A and B or C
A= defined $cmd[0]B= $cmd[0] == 1C= $cmd[0] == 0
ABCresult
0XX0
1000
11X1
1011
493 0 $page->{alert} = $cmd[0];
494 0 $client->command( "250 OK, Alert Override Accepted" );
495 }
496 else {
497 0 $errors++;
498 0 $client->command( "550 Error, Invalid Alert Parameter" );
499 }
500 }
501  
502 ########################################################################
503 # 4.5.6 HOLDuntil <YYMMDDHHMMSS> [+/-GMTdifference] ------------------ #
504 # non-rfc <YYYYMMDDMMSS> to accept 4-digit years is also accepted ---- #
505 elsif ( $user_cmd eq 'HOLD' ) {
506 0 ▶ if ( defined($cmd[0]) && $cmd[0] !~ /[^0-9]/
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cmd[0] and not $cmd[0] =~ /[^0-9]/ and length $cmd[0] == 12 || length $cmd[0] == 14) { } 0 0 0
Condition0%0/9
defined $cmd[0] and not $cmd[0] =~ /[^0-9]/ and length $cmd[0] == 12 || length $cmd[0] == 14
!ll&&!rl&&r
000
defined $cmd[0] and not $cmd[0] =~ /[^0-9]/
!ll&&!rl&&r
000
length $cmd[0] == 12 or length $cmd[0] == 14
l!l&&r!l&&!r
000
MC/DC0%0/4
defined $cmd[0] and not $cmd[0] =~ /[^0-9]/ and length $cmd[0] == 12 || length $cmd[0] == 14
defined $cmd[0] not $cmd[0] =~ /[^0-9]/ length $cmd[0] == 12 length $cmd[0] == 14
Truth table0%0/5
A and not(B) and C or D
A= defined $cmd[0]B= not $cmd[0] =~ /[^0-9]/C= length $cmd[0] == 12D= length $cmd[0] == 14
ABCDresult
0XXX0
10XX0
11000
111X1
11011
507 && (length($cmd[0]) == 12 || length($cmd[0]) == 14) ) {
508 0 $page->{hold} = $cmd[0];
509 0 ▶ if ( $cmd[1] =~ /([+-]\d+)/ ) { $page->{hold_gmt_diff} = $1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd[1] =~ /([+-]\d+)/) 0 0 0
510 0 $client->command( "250 Delayed Messaging Selected" );
511 }
512 else {
513 0 $errors++;
514 0 $client->command( "550 Error, Invalid Delivery Date/Time" );
515 }
516 }
517  
518 ########################################################################
519 ## 4.6 Level 3 - Two-Way Extensions ####################################
520 ########################################################################
521 # 4.6.1 2WAY --------------------------------------------------------- #
522 elsif ( $user_cmd eq '2WAY' ) {
523 0 ▶ if ( exists($page->{mess}) || @pgrs > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $page->{"mess"} or @pgrs > 0) 0 0 0
Condition0%0/3
exists $page->{"mess"} or @pgrs > 0
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $page->{"mess"} or @pgrs > 0
exists $page->{"mess"} @pgrs > 0
Truth table0%0/3
A or B
A= exists $page->{"mess"}B= @pgrs > 0
ABresult
1X1
011
000
524 0 $errors++;
525 0 $client->command( "550 Error, Standard Transaction Already Underway, use RESEt" );
526 0 next;
527 }
528 0 $page->{twoway} = 1;
529 0 $client->command( "250 OK, Beginning 2-Way Transaction" );
530 }
531  
532 ########################################################################
533 # 4.6.2 PING <PagerID | Alias> --------------------------------------- #
534 # FIXME: what the heck should this do by default?
535 elsif ( $user_cmd eq 'PING' ) {
536 0 $client->command( "250 OK, Cannot access device status" );
537 }
538  
539 ########################################################################
540 # 4.6.7 MCREsponse <2-byte_Code> Response_Text (not implemented) ----- #
541 elsif ( $user_cmd eq 'MCRE' ) {
542 0 ▶ if ( !exists($page->{twoway}) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $page->{'twoway'}) { } 0 0 0
elsif (not $cmd[0] =~ /[^0-9]/ and length $cmd[0] < 3 and length $cmd[1] >= 1 and length $cmd[1] < 16) { } 0 0 0
543 0 $errors++;
544 0 $client->command( "550 MCResponses Not Enabled" );
545 }
546 ▶ elsif ( $cmd[0] !~ /[^0-9]/ && length($cmd[0]) < 3 &&
Condition0%0/9
not $cmd[0] =~ /[^0-9]/ and length $cmd[0] < 3 and length $cmd[1] >= 1 and length $cmd[1] < 16
!ll&&!rl&&r
000
not $cmd[0] =~ /[^0-9]/ and length $cmd[0] < 3 and length $cmd[1] >= 1
!ll&&!rl&&r
000
not $cmd[0] =~ /[^0-9]/ and length $cmd[0] < 3
!ll&&!rl&&r
000
MC/DC0%0/4
not $cmd[0] =~ /[^0-9]/ and length $cmd[0] < 3 and length $cmd[1] >= 1 and length $cmd[1] < 16
not $cmd[0] =~ /[^0-9]/ length $cmd[0] < 3 length $cmd[1] >= 1 length $cmd[1] < 16
Truth table0%0/5
not(A) and B and C and D
A= not $cmd[0] =~ /[^0-9]/B= length $cmd[0] < 3C= length $cmd[1] >= 1D= length $cmd[1] < 16
ABCDresult
0XXX0
10XX0
110X0
11100
11111
547 length($cmd[1]) >= 1 && length($cmd[1]) < 16 ) {
548 0 ▶ if ( exists($page->{responses}{$cmd[0]}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $page->{"responses"}{$cmd[0]}) 0 0 0
549 0 $client->command( "502 Error! Would Duplicate Previously Entered MCResponse" );
550 0 next;
551 }
552 0 $page->{responses}{shift @cmd} = join(' ',@cmd);
553 0 $client->command( "250 Response Added to Transaction" );
554 }
555 else {
556 0 $errors++;
557 0 $client->command( "554 Error, failed" );
558 }
559 }
560 ########################################################################
561 # UNKNOWN/UNDEFINED COMMANDS ----------------------------------------- #
562 # -------------------------------------------------------------------- #
563 # 4.5.1 LOGIn <loginid> [password] (not implemented) ----------------- #
564 # 4.5.3 LEVEl <ServiceLevel> (not implemented) ----------------- #
565 # 4.5.5 COVErage <AlternateArea> (not implemented) ----------------- #
566 # 4.5.7 CALLerid <CallerID> (not implemented) ----------------- #
567 # 4.6.3 EXPTag <hours> (not implemented) ----------------- #
568 # 4.6.5 ACKRead <0|1> (not implemented) ----------------- #
569 # 4.6.6 RTYPe <Reply_Type_Code> (not implemented) ----------------- #
570 # MSTA --------------------------------------------------------------- #
571 # KTAG <Message_Tag> <Pass_Code> (not implemented) ----------------- #
572 ########################################################################
573 else {
574 0 $errors++;
575 0 $client->command( "500 Command Not Implemented, Try Again" );
576 }
577 # //////////////////////////////////////////////////////////////////// #
578 # END COMMANDS PARSING #
579 # \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ #
580  
581 # check the number of errors
582 0 ▶ if ( $self->{MaxErrors} && $errors >= $self->{MaxErrors} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"MaxErrors"} and $errors >= $self->{"MaxErrors"}) 0 0 0
Condition0%0/3
$self->{"MaxErrors"} and $errors >= $self->{"MaxErrors"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"MaxErrors"} and $errors >= $self->{"MaxErrors"}
$self->{"MaxErrors"} $errors >= $self->{"MaxErrors"}
Truth table0%0/3
A and B
A= $self->{"MaxErrors"}B= $errors >= $self->{"MaxErrors"}
ABresult
0X0
100
111
583 0 $client->command( "421 Too Many Errors, Goodbye (terminate connection)" );
584 0 last;
585 }
586 # reset the alarm on input
587 0 ▶ if ( $self->{Timeout} ) { alarm(0); alarm( $self->{Timeout} ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Timeout"}) 0 0 0
588 } # while()
589  
590 # turn off the alarm
591 0 ▶ if ( $self->{Timeout} ) { alarm(0); }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Timeout"}) 0 0 0
592  
593 # disconnect if we're still connected
594 0 ▶ if ( $client->connected() ) { $client->shutdown(2) }
Branch: 1 branch
BranchTrueFalseTotal
if ($client->connected) 0 0 0
595  
596 0 return @retvals;
597 }
598  
599 =item forked_server()
600  
601 Creates a server in a forked process. The return value is
602 an array (or arrayref depending on context) containing a read-only pipe and
603 the pid of the new process. Pages completed will be written to the pipe as
604 a semicolon delimited array.
605 my($pipe,$pid) = $server->forked_server();
606 my $line = $pipe->getline();
607 chomp( $line );
608 my( $pgr, $pgr, %pagedata ) = split( /;/, $line );
609  
610 =cut
611  
612 # when testing, pass in an integer argument to limit the number of clients
613 # the server will process before exiting
614 3 ▶sub forked_server {
Subroutine forked_server: called CC=10 SCAR=31.6
Pod documented
615 3 my( $self, $count_arg ) = @_;
616 3 my $count = -1;
617 3 ▶ if ( $count_arg ) { $count = $count_arg }
Branch: 1 branch
BranchTrueFalseTotal
if ($count_arg) 3 0 3
618 3 my @pids = (); # pids to merge before exit
619  
620 # create a pipe for communication from child back to this process
621 3 our( $rp, $wp ) = ( IO::Handle->new(), IO::Handle->new() );
622 3 ▶ pipe( $rp, $wp )
Branch: 1 branch
BranchTrueFalseTotal
pipe $Net::SNPP::Server::rp, $Net::SNPP::Server::wp or die "could not create READ/WRITE pipes" 3 0 3
623 || die "could not create READ/WRITE pipes";
624 3 $wp->autoflush(1);
625  
626 # declare our callback subroutine for process_page
627 # has it's own ugly serialization that should probably be replaced
628 # with Storable or Dumper
629 0 ▶ sub write_to_pipe {
Subroutine write_to_pipe: not called CC=6 SCAR=37.4
Pod undocumented
630 0 my( $pgr, $page, $results ) = @_;
631 0 my( @parts, @resps ) = ();
632 0 ▶ if ( my $href = delete($page->{responses}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $href = delete $page->{"responses"}) 0 0 0
633 0 ▶ while ( my($k,$v) = each(%$href) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$href) 0 0 0
634 0 $v =~ s/;/\%semicolon%/g;
635 0 $k = "responses[$k]";
636 0 push( @resps, $k, $v );
637 }
638 }
639 0 ▶ while ( my($k,$v) = each(%$page) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$page) 0 0 0
640 0 ▶ if ( !defined($v) ) { $v = '' }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v) 0 0 0
641 0 push( @parts, $k, $v );
642 }
643 0 ▶ if ( !defined($pgr->[1]) ) { $pgr->[1] = '1' }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pgr->[1]) 0 0 0
644 0 my $out = join( ';', @$pgr, @parts, @resps );
645 0 $out =~ s/[\r\n]+//gs; # make sure there aren't any unexpected newlines
646  
647 # send the page semicolon delimited down the pipe
648 0 flock( $wp, LOCK_EX );
649 0 $wp->print( "$out\n" );
650 0 flock( $wp, LOCK_UN );
651 }
652  
653 # fork a child process to act as a server
654 3 my $pid = fork();
655 3 ▶ if ( $pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 1 2 3
656 1 $wp->close();
657 1 ▶ return wantarray ? ($rp,$pid) : [$rp,$pid];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
658 }
659 else {
660 2 $rp->close();
661 # replace the page callback with our own subroutine
662 2 $self->callback( 'process_page', \&write_to_pipe );
663 2 ▶ while ( !$count_arg || $count > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $count_arg or $count > 0) 2 1 3
Condition66%2/3
not $count_arg or $count > 0
l!l&&r!l&&!r
021
MC/DC50%1/2
not $count_arg or $count > 0
not $count_arg $count > 0
Truth table66%2/3
not(A) or B
A= not $count_argB= $count > 0
ABresult
1X1
011
000
664  
665 # attempt reap child processes on every loop
666 2 ▶ for ( my $i=0; $i<@pids; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @pids) 0 2 2
667 0 my $pid = waitpid( $pids[$i], 0 );
668 0 ▶ if ( $pid < 1 ) { splice( @pids, $i, 1 ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($pid < 1) 0 0 0
669 }
670  
671 # get a client socket handle
672 2 my $client = $self->client();
673  
674 2 $count--;
675  
676 # fork again so we can handle simultaneous connections
677 2 my $pid = fork();
678  
679 # parent process goes back to top of loop
680 2 ▶ if ( $pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 1 1 2
681 1 push( @pids, $pid );
682 1 next;
683 }
684
685 1 $self->handle_client( $client );
686 0 exit 0;
687 }
688 1 $wp->close();
689 1 exit 0;
690 }
691 }
692  
693 =back
694  
695 =head1 AUTHOR
696  
697 Al Tobey <tobeya@tobert.org>
698  
699 Some ideas from Sendpage::SNPPServer
700 Kees Cook <cook@cpoint.net> http://outflux.net/
701  
702 =head1 TODO
703  
704 Add more hooks for callbacks
705  
706 Implement the following level 2 and level 3 commands
707  
708 4.5.1 LOGIn <loginid> [password]
709 4.5.3 LEVEl <ServiceLevel>
710 4.5.5 COVErage <AlternateArea>
711 4.5.7 CALLerid <CallerID>
712 4.6.3 EXPTag <hours>
713 4.6.5 ACKRead <0|1>
714 4.6.6 RTYPe <Reply_Type_Code>
715  
716 =head1 SEE ALSO
717  
718 Net::Cmd Socket
719  
720 =cut
721  
722 3 1;
723  
724 # FIXME: update this from the RFC
725 __DATA__
↑ Summary SNPP.pm »