blib/lib/Net/RCON.pm

filter: Statementstmt 32.5% 14 / 43 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 44.4% 4 / 9 Podpod 100% 2 / 2 totaltotal 26.6% 20 / 75 CC 8 SCAR 25.6
CC
8
Cov
23%
_recv_rcon
30.0
new
12.0
send
6.0
CRAP 12.9
↑ Summary
1 package Net::RCON;
2 1 $Net::RCON::VERSION = '1.0';
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 SERVERDATA_RESPONSE_VALUE => 0,
10 SERVERDATA_AUTH_RESPONSE => 2,
11 SERVERDATA_EXECCOMMAND => 2,
12 SERVERDATA_AUTH => 3
13 1 };
14  
15 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
16 0 my ( $class, $args ) = @_;
17  
18 my $self = {
19 host => $args->{host},
20 port => $args->{port},
21 password => $args->{password},
22 0 id => 0
23 };
24  
25 $self->{sock} = IO::Socket::INET->new(
26 PeerHost => $self->{host},
27 PeerPort => $self->{port},
28 0 ▶ Proto => 'tcp'
Branch: 1 branch
BranchTrueFalseTotal
$self->{"sock"} = "IO::Socket::INET"->new("PeerHost", $self->{"host"}, "PeerPort", $self->{"port"}, "Proto", "tcp") or die "Unable to connect to the RCON server: $!\n" 0 0 0
29 ) or die "Unable to connect to the RCON server: $!\n";
30  
31 0 my $object = bless ( $self, $class );
32  
33 # Send a SERVERDATA_AUTH packet
34 0 $object->_send_rcon( SERVERDATA_AUTH, $self->{password} );
35  
36 # Check is SERVERDATA_AUTH_RESPONSE came back as valid
37 0 ▶ if( !$self->_recv_rcon( SERVERDATA_AUTH_RESPONSE ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_recv_rcon(2)) 0 0 0
38 0 warn "Authentication to the RCON server failed";
39 0 return undef;
40 }
41  
42 0 return $object;
43 }
44  
45 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
46 0 my ( $self ) = @_;
47  
48 0 $self->{sock}->close();
49 }
50  
51 0 ▶sub send {
Subroutine send: not called CC=2 SCAR=17.9
Pod documented
52 0 my ( $self, $command ) = @_;
53  
54 0 $self->_send_rcon( SERVERDATA_EXECCOMMAND, $command );
55 0 my $response = $self->_recv_rcon( SERVERDATA_RESPONSE_VALUE );
56  
57 0 ▶ if( $response ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) { } 0 0 0
58 0 return $response;
59 } else {
60 0 warn "RCON command $command failed. Unexpected response from the RCON server";
61 0 return undef;
62 }
63 }
64  
65 0 ▶sub _send_rcon {
Subroutine _send_rcon: not called CC=1 SCAR=6.9
66 0 my ( $self, $type, $message ) = @_;
67  
68 # Build RCON packet
69 0 my $data = pack( "VV", 1, $type ) . $message . pack("xx");
70  
71 # Prepend packet size
72 0 $data = pack( "V", length( $data ) ).$data;
73  
74 0 return $self->{sock}->send( $data );
75 }
76  
77 0 ▶sub _recv_rcon {
Subroutine _recv_rcon: not called CC=5 SCAR=34.0
78 0 my ( $self, $type ) = @_;
79  
80 # Get response
81 0 my $response = "";
82 0 $self->{sock}->recv( $response, 4096 );
83  
84 # Unpack response packet
85 0 my ( $size, $response_id, $response_type, $response_body ) = unpack( "VVVa*", $response );
86  
87 # Make sure the response id is what we sent
88 0 ▶ if( $response_id == 1 && $response_type == $type && $size >= 10 && $size <= 4096 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response_id == 1 and $response_type == $type and $size >= 10 and $size <= 4096) { } 0 0 0
Condition0%0/9
$response_id == 1 and $response_type == $type and $size >= 10 and $size <= 4096
!ll&&!rl&&r
000
$response_id == 1 and $response_type == $type and $size >= 10
!ll&&!rl&&r
000
$response_id == 1 and $response_type == $type
!ll&&!rl&&r
000
MC/DC0%0/4
$response_id == 1 and $response_type == $type and $size >= 10 and $size <= 4096
$response_id == 1 $response_type == $type $size >= 10 $size <= 4096
Truth table0%0/5
A and B and C and D
A= $response_id == 1B= $response_type == $typeC= $size >= 10D= $size <= 4096
ABCDresult
0XXX0
10XX0
110X0
11100
11111
89 0 return $response_body;
90 } else {
91 0 return undef;
92 }
93 }
94 1 1;
95  
96 # ABSTRACT: This module provides an interface to an RCON (remote console) server. RCON is typically implemented by game servers to allow servers admins to control their game servers without direct access to the machine the server is running on. This module is based on the spec detailed here, https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
97  
98 __END__
↑ Summary