« blib/lib/Monitoring/Sneck/Checks/file_mode.pm
↑ Summary
blib/lib/Monitoring/Sneck/Checks/hosts.pm »
| 1 | package Monitoring::Sneck::Checks::listening; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 15 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 15 | ▶ | use parent 'Monitoring::Sneck::Checks::base'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use Socket qw(AF_INET AF_INET6 inet_ntop); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | Monitoring::Sneck::Checks::listening - checks what ports are being listened on | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | Version 0.0.1 | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 15 | our $VERSION = '0.0.1'; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | # Where sockstat is on FreeBSD. Set by tests. | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 15 | our $SOCKSTAT = '/usr/bin/sockstat'; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | ssh_listening|perl:listening -p 22 | ||||||||||||||||||||||||||||||||||||||||||
| 27 | web_listening|perl:listening -p tcp/80 -p tcp/443 -p udp/443 | ||||||||||||||||||||||||||||||||||||||||||
| 28 | !only_expected||perl:listening -p 22 -p 25 -p udp/123 -o -L | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | Checks each port given is being listened on, and with --only that | ||||||||||||||||||||||||||||||||||||||||||
| 33 | nothing else is. Either is CRITICAL. | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | Ports are given as [protocol/][address:]port, such as 22, tcp/22, | ||||||||||||||||||||||||||||||||||||||||||
| 36 | udp/53, 127.0.0.1:6379, or tcp6/[::1]:25. tcp and udp match either IP | ||||||||||||||||||||||||||||||||||||||||||
| 37 | version, while tcp4, tcp6, udp4, and udp6 match only that one. No | ||||||||||||||||||||||||||||||||||||||||||
| 38 | address matches any, while a address must match exactly, a wildcard | ||||||||||||||||||||||||||||||||||||||||||
| 39 | listener, 0.0.0.0 or ::, counting for any address. For UDP, a bound | ||||||||||||||||||||||||||||||||||||||||||
| 40 | socket not connected to anything counts as listening. That includes | ||||||||||||||||||||||||||||||||||||||||||
| 41 | clients using unconnected sockets, such as for mDNS, so --only with UDP | ||||||||||||||||||||||||||||||||||||||||||
| 42 | may need --ignore for those. | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | On Linux, sockets are read from /proc/net. On FreeBSD, sockstat -46lqn | ||||||||||||||||||||||||||||||||||||||||||
| 45 | is run. Other OSes are UNKNOWN. | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | =head1 OPTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | - -p, --port :: A port that must be listened on. May be given more | ||||||||||||||||||||||||||||||||||||||||||
| 50 | than once. | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | - -o, --only :: Nothing but --port may be listened on. | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | - -L, --ignore-loopback :: For --only, leave out what only listens on | ||||||||||||||||||||||||||||||||||||||||||
| 55 | loopback addresses. | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | - -i, --ignore :: For --only, a port that may be listened on, given | ||||||||||||||||||||||||||||||||||||||||||
| 58 | the same way as --port. May be given more than once. | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | One of --port or --only must be given. | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 117 | ▶ | sub option_spec { return ( 'port|p=s@', 'only|o', 'ignore-loopback|L', 'ignore|i=s@' ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
option_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 117 | ▶ | sub validate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 117 | my ( $self, $options ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 117 | my @problems; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 117 | foreach my $name ( 'port', 'ignore' ) { | |||||||||||||||||||||||||||||||||||||||||
| 71 | 234 | ▶ | foreach my $port ( @{ $options->{$name} // [] } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{$name} // []
MC/DC100%1/1
$options->{$name} // []
$options->{$name}
Truth table100%2/2
A
A= $options->{$name}
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 286 | ▶ | push( @problems, '--' . $name . ' "' . $port . '" is not [protocol/][address:]port' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | if !defined( _wanted($port) ); | ||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 117 | ▶ | push( @problems, 'nothing to check, give --port or --only' ) if !defined( $options->{port} ) && !$options->{only}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $options->{"port"} or $options->{"only"}
MC/DC50%1/2
defined $options->{"port"} or $options->{"only"}
defined $options->{"port"}
$options->{"only"}
Truth table66%2/3
A or B
A= defined $options->{"port"}B= $options->{"only"}
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 117 | return @problems; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } ## end sub validate | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | =head2 check | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | Does the check. | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 78 | ▶ | sub check { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=15 SCAR=27.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 78 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 78 | my @listeners = $self->listeners; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 77 | ▶ | my @wanted = map { _wanted($_) } @{ $self->opt('port') // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->opt("port") // []
MC/DC0%0/1
$self->opt("port") // []
$self->opt("port")
Truth table50%1/2
A
A= $self->opt("port")
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 77 | ▶ | my @ignored = map { _wanted($_) } @{ $self->opt('ignore') // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->opt("ignore") // []
MC/DC100%1/1
$self->opt("ignore") // []
$self->opt("ignore")
Truth table100%2/2
A
A= $self->opt("ignore")
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 77 | my @problems; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 77 | foreach my $want (@wanted) { | |||||||||||||||||||||||||||||||||||||||||
| 97 | 243 | ▶ | push( @problems, $want->{text} . ' is not listened on' ) if !grep { _matches( $want, $_ ) } @listeners; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 77 | ▶ | if ( $self->opt('only') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 51 | my %extra; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 51 | foreach my $listener (@listeners) { | |||||||||||||||||||||||||||||||||||||||||
| 103 | 243 | ▶ | next if grep { _matches( $_, $listener ) } @wanted, @ignored; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 104 | ▶ | next if $self->opt('ignore-loopback') && $listener->{address} =~ /\A(?:127\.|::1\z|::ffff:127\.)/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->opt("ignore-loopback") and $listener->{"address"} =~ /\A(?:127\.|::1\z|::ffff:127\.)/u
MC/DC100%2/2
$self->opt("ignore-loopback") and $listener->{"address"} =~ /\A(?:127\.|::1\z|::ffff:127\.)/u
$self->opt("ignore-loopback")
$listener->{"address"} =~ /\A(?:127\.|::1\z|::ffff:127\.)/u
Truth table100%3/3
A and B
A= $self->opt("ignore-loopback")B= $listener->{"address"} =~ /\A(?:127\.|::1\z|::ffff:127\.)/u
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 52 | $extra{ $listener->{protocol} . '/' . _join_address( $listener->{address}, $listener->{port} ) } | |||||||||||||||||||||||||||||||||||||||||
| 106 | = $listener; | ||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | my @extra = sort { | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 51 | $extra{$a}{protocol} cmp $extra{$b}{protocol} | |||||||||||||||||||||||||||||||||||||||||
| 110 | || $extra{$a}{port} <=> $extra{$b}{port} | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 39 | ▶ | || $a cmp $b | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
$extra{$a}{'protocol'} cmp $extra{$b}{'protocol'} || $extra{$a}{'port'} <=> $extra{$b}{'port'} || $a cmp $b
$extra{$a}{"protocol"} cmp $extra{$b}{"protocol"} or $extra{$a}{"port"} <=> $extra{$b}{"port"}
MC/DC0%0/3
$extra{$a}{'protocol'} cmp $extra{$b}{'protocol'} || $extra{$a}{'port'} <=> $extra{$b}{'port'} || $a cmp $b
$extra{$a}{"protocol"} cmp $extra{$b}{"protocol"}
$extra{$a}{"port"} <=> $extra{$b}{"port"}
$a cmp $b
Truth table25%1/4
A or B || C
A= $extra{$a}{"protocol"} cmp $extra{$b}{"protocol"}B= $extra{$a}{"port"} <=> $extra{$b}{"port"}C= $a cmp $b
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | } keys(%extra); | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 51 | ▶ | push( @problems, 'also listening on ' . join( ', ', @extra ) ) if defined( $extra[0] ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | } ## end if ( $self->opt('only') ) | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 77 | my $message = scalar(@listeners) . ' listening'; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 77 | ▶ | $message = $message . ', ' . join( ', ', @problems ) if defined( $problems[0] ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 77 | ▶ | return $self->finish( defined( $problems[0] ) ? $self->CRITICAL : $self->OK, $message ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | } ## end sub check | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | =head2 listeners | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | Returns what is listening, as hash refs of protocol, tcp4, tcp6, udp4, | ||||||||||||||||||||||||||||||||||||||||||
| 125 | or udp6, address, and port. Dies if it can not be found. | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 78 | ▶ | sub listeners { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
listeners: called CC=19 SCAR=30.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 78 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 78 | ▶ | if ( $self->os eq 'linux' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 65 | my @listeners; | |||||||||||||||||||||||||||||||||||||||||
| 134 | 65 | foreach my $protocol ( 'tcp', 'tcp6', 'udp', 'udp6' ) { | |||||||||||||||||||||||||||||||||||||||||
| 135 | 260 | ▶ | my $table = eval { $self->proc_file( 'net/' . $protocol ) } // next; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
eval {
do {
$self->proc_file('net/' . $protocol)
}
} // (next)
MC/DC0%0/1
eval {
do {
$self->proc_file('net/' . $protocol)
}
} // (next)
eval {
do {
$self->proc_file('net/' . $protocol)
}
}
Truth table50%1/2
A
A= eval {
do {
$self->proc_file('net/' . $protocol)
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 260 | ▶ | my $family = $protocol =~ /6\z/ ? 6 : 4; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | # TCP_LISTEN is 0A, and a unconnected UDP socket is TCP_CLOSE, 07 | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 260 | ▶ | my $listening = $protocol =~ /\Atcp/ ? '0A' : '07'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 260 | foreach my $line ( split( /\n/, $table ) ) { | |||||||||||||||||||||||||||||||||||||||||
| 141 | 650 | my ( $local, $state ) = ( split( /\s+/, $line =~ s/\A\s+//r ) )[ 1, 3 ]; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 650 | ▶ | next if !defined($state) || $state ne $listening || $local !~ /\A([0-9A-F]+):([0-9A-F]{4})\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
not defined $state or $state ne $listening or not $local =~ /\A([0-9A-F]+):([0-9A-F]{4})\z/u
not defined $state or $state ne $listening
MC/DC33%1/3
not defined $state or $state ne $listening or not $local =~ /\A([0-9A-F]+):([0-9A-F]{4})\z/u
not defined $state
$state ne $listening
not $local =~ /\A([0-9A-F]+):([0-9A-F]{4})\z/u
Truth table50%2/4
not(A) or B or not(C)
A= not defined $stateB= $state ne $listeningC= not $local =~ /\A([0-9A-F]+):([0-9A-F]{4})\z/u
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 325 | my ( $hex, $port ) = ( $1, hex($2) ); | |||||||||||||||||||||||||||||||||||||||||
| 144 | 325 | push( | |||||||||||||||||||||||||||||||||||||||||
| 145 | @listeners, | ||||||||||||||||||||||||||||||||||||||||||
| 146 | { | ||||||||||||||||||||||||||||||||||||||||||
| 147 | protocol => ( $protocol =~ s/\d\z//r ) . $family, | ||||||||||||||||||||||||||||||||||||||||||
| 148 | address => _hex_address( $hex, $family ), | ||||||||||||||||||||||||||||||||||||||||||
| 149 | port => $port | ||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 152 | } ## end foreach my $line ( split( /\n/, $table ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 153 | } ## end foreach my $protocol ( 'tcp', 'tcp6', 'udp', 'udp6') | ||||||||||||||||||||||||||||||||||||||||||
| 154 | 65 | return @listeners; | |||||||||||||||||||||||||||||||||||||||||
| 155 | } ## end if ( $self->os eq 'linux' ) | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 13 | ▶ | if ( $self->os eq 'freebsd' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 13 | my ( $exit, $output ) = $self->run_command( $SOCKSTAT, '-46lqn' ); | |||||||||||||||||||||||||||||||||||||||||
| 158 | 12 | ▶ | die( 'sockstat exited ' . $exit . "\n" ) if $exit; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | # USER COMMAND PID FD PROTO LOCAL FOREIGN, with tcp46 and the like for both versions | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 12 | my @listeners; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 12 | foreach my $line ( split( /\n/, $output ) ) { | |||||||||||||||||||||||||||||||||||||||||
| 163 | 36 | my ( $protocol, $local ) = ( split( /\s+/, $line ) )[ 4, 5 ]; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 36 | ▶ | next if !defined($local); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 36 | ▶ | my ( $kind, $families ) = $protocol =~ /\A(tcp|udp)(4|6|46)\z/ or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 36 | ▶ | my ( $address, $port ) = $local =~ /\A(.*):([0-9]+)\z/ or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 36 | foreach my $family ( split( //, $families ) ) { | |||||||||||||||||||||||||||||||||||||||||
| 168 | 48 | ▶ | my $at = $address eq '*' ? ( $family eq '6' ? '::' : '0.0.0.0' ) : $address; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 48 | push( @listeners, { protocol => $kind . $family, address => $at, port => $port + 0 } ); | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | } ## end foreach my $line ( split( /\n/, $output ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 12 | return @listeners; | |||||||||||||||||||||||||||||||||||||||||
| 173 | } ## end if ( $self->os eq 'freebsd' ) | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | die( 'not supported on ' . $self->os . "\n" ); | |||||||||||||||||||||||||||||||||||||||||
| 176 | } ## end sub listeners | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | # Parses a --port. | ||||||||||||||||||||||||||||||||||||||||||
| 179 | # | ||||||||||||||||||||||||||||||||||||||||||
| 180 | # Args... | ||||||||||||||||||||||||||||||||||||||||||
| 181 | # | ||||||||||||||||||||||||||||||||||||||||||
| 182 | # - text :: The --port, such as 'udp/53' or 'tcp6/[::1]:25'. | ||||||||||||||||||||||||||||||||||||||||||
| 183 | # | ||||||||||||||||||||||||||||||||||||||||||
| 184 | # Returns a hash ref of text, protocol, tcp, udp, or one of those with 4 | ||||||||||||||||||||||||||||||||||||||||||
| 185 | # or 6, address, undef for any, and port, or undef if it is not valid. | ||||||||||||||||||||||||||||||||||||||||||
| 186 | # | ||||||||||||||||||||||||||||||||||||||||||
| 187 | # Example... | ||||||||||||||||||||||||||||||||||||||||||
| 188 | # | ||||||||||||||||||||||||||||||||||||||||||
| 189 | # _wanted('127.0.0.1:6379'); | ||||||||||||||||||||||||||||||||||||||||||
| 190 | # # { text => '127.0.0.1:6379', protocol => 'tcp', address => '127.0.0.1', port => 6379 } | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 542 | ▶ | sub _wanted { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_wanted: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | 542 | my ($text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | 542 | ▶ | return undef if $text !~ m{\A(?:(tcp|udp)([46]?)/)?(?:\[?([0-9A-Fa-f.:]+?)\]?:)?([0-9]{1,5})\z}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 529 | ▶ | my ( $kind, $family, $address, $port ) = ( $1 // 'tcp', $2, $3, $4 ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$1 // "tcp"
MC/DC100%1/1
$1 // "tcp"
$1
Truth table100%2/2
A
A= $1
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 529 | ▶ | return undef if $port < 1 || $port > 65535; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$port < 1 or $port > 65535
MC/DC50%1/2
$port < 1 or $port > 65535
$port < 1
$port > 65535
Truth table66%2/3
A or B
A= $port < 1B= $port > 65535
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 199 | 516 | ▶ | text => $text, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$family // ""
MC/DC100%1/1
$family // ""
$family
Truth table100%2/2
A
A= $family
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | protocol => $kind . ( $family // '' ), | ||||||||||||||||||||||||||||||||||||||||||
| 201 | address => defined($address) ? lc($address) : undef, | ||||||||||||||||||||||||||||||||||||||||||
| 202 | port => $port + 0 | ||||||||||||||||||||||||||||||||||||||||||
| 203 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 204 | } ## end sub _wanted | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | # Checks if a listener is what a --port wants. | ||||||||||||||||||||||||||||||||||||||||||
| 207 | # | ||||||||||||||||||||||||||||||||||||||||||
| 208 | # Args... | ||||||||||||||||||||||||||||||||||||||||||
| 209 | # | ||||||||||||||||||||||||||||||||||||||||||
| 210 | # - want :: From _wanted. | ||||||||||||||||||||||||||||||||||||||||||
| 211 | # | ||||||||||||||||||||||||||||||||||||||||||
| 212 | # - listener :: From listeners. | ||||||||||||||||||||||||||||||||||||||||||
| 213 | # | ||||||||||||||||||||||||||||||||||||||||||
| 214 | # Returns 1 if it matches, else 0. | ||||||||||||||||||||||||||||||||||||||||||
| 215 | # | ||||||||||||||||||||||||||||||||||||||||||
| 216 | # Example... | ||||||||||||||||||||||||||||||||||||||||||
| 217 | # | ||||||||||||||||||||||||||||||||||||||||||
| 218 | # _matches( _wanted('tcp/22'), { protocol => 'tcp6', address => '::', port => 22 } ); # 1 | ||||||||||||||||||||||||||||||||||||||||||
| 219 | 1814 | ▶ | sub _matches { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_matches: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 1814 | my ( $want, $listener ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | 1814 | ▶ | return 0 if $want->{port} != $listener->{port}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 430 | ▶ | return 0 if index( $listener->{protocol}, $want->{protocol} ) != 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 356 | ▶ | return 1 if !defined( $want->{address} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 76 | ▶ | return 1 if $listener->{address} eq '0.0.0.0' || $listener->{address} eq '::'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$listener->{"address"} eq "0.0.0.0" or $listener->{"address"} eq "::"
MC/DC50%1/2
$listener->{"address"} eq "0.0.0.0" or $listener->{"address"} eq "::"
$listener->{"address"} eq "0.0.0.0"
$listener->{"address"} eq "::"
Truth table66%2/3
A or B
A= $listener->{"address"} eq "0.0.0.0"B= $listener->{"address"} eq "::"
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | 63 | ▶ | return lc( $listener->{address} ) eq $want->{address} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | } ## end sub _matches | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | # Turns a address from /proc/net into text. Linux prints each 32 bit | ||||||||||||||||||||||||||||||||||||||||||
| 231 | # word of it as a number in the host's byte order, so packing each back | ||||||||||||||||||||||||||||||||||||||||||
| 232 | # that way gives the bytes as they were, in network order. | ||||||||||||||||||||||||||||||||||||||||||
| 233 | # | ||||||||||||||||||||||||||||||||||||||||||
| 234 | # Args... | ||||||||||||||||||||||||||||||||||||||||||
| 235 | # | ||||||||||||||||||||||||||||||||||||||||||
| 236 | # - hex :: The address as hex, 8 characters for IPv4 or 32 for IPv6. | ||||||||||||||||||||||||||||||||||||||||||
| 237 | # | ||||||||||||||||||||||||||||||||||||||||||
| 238 | # - family :: 4 or 6. | ||||||||||||||||||||||||||||||||||||||||||
| 239 | # | ||||||||||||||||||||||||||||||||||||||||||
| 240 | # Returns the address, such as 127.0.0.1 or ::1. | ||||||||||||||||||||||||||||||||||||||||||
| 241 | # | ||||||||||||||||||||||||||||||||||||||||||
| 242 | # Example... | ||||||||||||||||||||||||||||||||||||||||||
| 243 | # | ||||||||||||||||||||||||||||||||||||||||||
| 244 | # _hex_address( '0100007F', 4 ); # '127.0.0.1', on a little endian host | ||||||||||||||||||||||||||||||||||||||||||
| 245 | 325 | ▶ | sub _hex_address { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_hex_address: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 325 | my ( $hex, $family ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | 325 | my $packed = join( '', map { pack( 'L', hex($_) ) } $hex =~ /(.{8})/g ); | |||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | 325 | ▶ | return inet_ntop( $family == 6 ? AF_INET6 : AF_INET, $packed ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | # Joins a address and port, putting [] around IPv6 addresses. | ||||||||||||||||||||||||||||||||||||||||||
| 254 | # | ||||||||||||||||||||||||||||||||||||||||||
| 255 | # Args... | ||||||||||||||||||||||||||||||||||||||||||
| 256 | # | ||||||||||||||||||||||||||||||||||||||||||
| 257 | # - address :: The address. | ||||||||||||||||||||||||||||||||||||||||||
| 258 | # | ||||||||||||||||||||||||||||||||||||||||||
| 259 | # - port :: The port. | ||||||||||||||||||||||||||||||||||||||||||
| 260 | # | ||||||||||||||||||||||||||||||||||||||||||
| 261 | # Returns them joined. | ||||||||||||||||||||||||||||||||||||||||||
| 262 | # | ||||||||||||||||||||||||||||||||||||||||||
| 263 | # Example... | ||||||||||||||||||||||||||||||||||||||||||
| 264 | # | ||||||||||||||||||||||||||||||||||||||||||
| 265 | # _join_address( '::1', 25 ); # '[::1]:25' | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 52 | ▶ | sub _join_address { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_join_address: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | 52 | my ( $address, $port ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | 52 | ▶ | return ( $address =~ /:/ ? '[' . $address . ']' : $address ) . ':' . $port; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | 15 | 1; | |||||||||||||||||||||||||||||||||||||||||