| 1 | package Net::Connection::Linux_ss; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Net::Connection; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Proc::ProcessTable; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | require Exporter; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 5 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 11 | 5 | our @EXPORT = qw(ss_to_nc_objects); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Net::Connection::Linux_ss - Creates Net::Connection objects using ss on Linux. | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 VERSION | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | Version 0.0.1 | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =cut | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 5 | our $VERSION = '0.0.1'; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | use Net::Connection::Linux_ss; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | my @objects; | ||||||||||||||||||||||||||||
| 30 | eval{ @objects=&ss_to_nc_objects; }; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # this time don't resolve ports or ptrs | ||||||||||||||||||||||||||||
| 33 | my $args={ | ||||||||||||||||||||||||||||
| 34 | ports=>0, | ||||||||||||||||||||||||||||
| 35 | ptrs=>0, | ||||||||||||||||||||||||||||
| 36 | }; | ||||||||||||||||||||||||||||
| 37 | eval{ @objects=&ss_to_nc_objects( $args ); }; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head1 REQUIREMENTS | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | This parses the output of 'ss -p4an' and 'ss -p6an', the ss from iproute2. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | Process information beyond what ss itself reports is gathered from | ||||||||||||||||||||||||||||
| 44 | L<Proc::ProcessTable> and /proc. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =head1 SUBROUTINES | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head2 ss_to_nc_objects | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | This calls 'ss -p4an 2> /dev/null' and 'ss -p6an 2> /dev/null', parses their | ||||||||||||||||||||||||||||
| 51 | output, and returns an array of L<Net::Connection> objects. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | Upon error, this will die. | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | A single socket may be held open by more than one process, which ss reports | ||||||||||||||||||||||||||||
| 56 | as several entries in the process column of the one line. One object is | ||||||||||||||||||||||||||||
| 57 | returned per process in that column, mirroring what lsof would print, so | ||||||||||||||||||||||||||||
| 58 | those sockets appear more than once in the returned array. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =head3 args hash | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head4 ports | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | Attempt to resolve the port names. | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | Defaults to 1. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | This value is a Perl boolean. | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =head4 ptrs | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | Attempt to resolve the PTRs. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | Defaults to 1. | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | This value is a Perl boolean. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head4 uid_resolve | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Attempt to resolve the UID to a username. | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | The UID itself is not something ss reports, so it is taken from the owner | ||||||||||||||||||||||||||||
| 83 | of /proc/<pid>, meaning it is only available when this module is the one | ||||||||||||||||||||||||||||
| 84 | calling ss. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | Defaults to 1. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | This value is a Perl boolean. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =head4 proc_info | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | Use L<Proc::ProcessTable> and /proc to fill in additional info. | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | This is what fills in proc, wchan, pctcpu, pctmem, and pid_start. Without | ||||||||||||||||||||||||||||
| 95 | it, proc is still set, but to the process name ss prints, which is | ||||||||||||||||||||||||||||
| 96 | truncated to fifteen characters. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | This is incompatible with the string options. | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | This defaults to true if neither of the string options is specified. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | This value is a Perl boolean. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head4 ipv4 | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | Call 'ss -p4an' and include the IPv4 sockets. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Defaults to 1. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | This value is a Perl boolean. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head4 ipv6 | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | Call 'ss -p6an' and include the IPv6 sockets. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | Defaults to 1. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | This value is a Perl boolean. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head4 ipv4_string | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | If this is specified, it parses the string as the output of 'ss -p4an' | ||||||||||||||||||||||||||||
| 123 | instead of calling ss. | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =head4 ipv6_string | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | If this is specified, it parses the string as the output of 'ss -p6an' | ||||||||||||||||||||||||||||
| 128 | instead of calling ss. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | If either of the string options is specified, ss is not called at all, so | ||||||||||||||||||||||||||||
| 131 | specifying just one of the two parses just that one. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | If running this on anything other than Linux without passing one of these, | ||||||||||||||||||||||||||||
| 134 | it will die. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head4 zombie_skip | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | This skips sockets that are not associated with any process, which is what | ||||||||||||||||||||||||||||
| 139 | the ones with an empty process column are. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | When these are not skipped, the resulting objects have the PID and UID left | ||||||||||||||||||||||||||||
| 142 | undefined, as L<Net::Connection> requires those two to be numeric when they | ||||||||||||||||||||||||||||
| 143 | are defined. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | This defaults to 1. | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | The value taken is a Perl boolean. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =cut | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 19 | ▶ | sub ss_to_nc_objects { | ||||||||||||||||||||||||||
|
Subroutine
ss_to_nc_objects: called CC=18 SCAR=31.8
Pod
documented
| |||||||||||||||||||||||||||||
| 152 | 19 | my %func_args; | |||||||||||||||||||||||||||
| 153 | 19 | ▶ | if ( defined( $_[0] ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 19 | %func_args = %{ $_[0] }; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 19 | ▶ | my $strings_given = ( defined( $func_args{ipv4_string} ) || defined( $func_args{ipv6_string} ) ); | ||||||||||||||||||||||||||
|
Condition100%3/3
defined $func_args{'ipv4_string'} || defined $func_args{'ipv6_string'}
MC/DC100%2/2
defined $func_args{'ipv4_string'} || defined $func_args{'ipv6_string'}
defined $func_args{'ipv4_string'}
defined $func_args{'ipv6_string'}
Truth table100%3/3
A || B
A= defined $func_args{'ipv4_string'}B= defined $func_args{'ipv6_string'}
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | # | ||||||||||||||||||||||||||||
| 160 | # set the defaults for the various args | ||||||||||||||||||||||||||||
| 161 | # | ||||||||||||||||||||||||||||
| 162 | 19 | ▶ | if ( !defined( $func_args{proc_info} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # if a string is set, default to false | ||||||||||||||||||||||||||||
| 165 | 16 | ▶ | if ($strings_given) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 15 | $func_args{proc_info} = 0; | |||||||||||||||||||||||||||
| 167 | } else { | ||||||||||||||||||||||||||||
| 168 | 1 | $func_args{proc_info} = 1; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | 19 | foreach my $arg_name (qw( ports ptrs uid_resolve zombie_skip ipv4 ipv6 )) { | |||||||||||||||||||||||||||
| 172 | 114 | ▶ | if ( !defined( $func_args{$arg_name} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 77 | $func_args{$arg_name} = 1; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | # each item is the raw ss output paired with the IP version it came from | ||||||||||||||||||||||||||||
| 178 | 19 | my @ss_outputs; | |||||||||||||||||||||||||||
| 179 | 19 | ▶ | if ($strings_given) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 18 | ▶ | if ( $func_args{proc_info} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 2 | die('The ipv4_string and ipv6_string args are mutually exclusive with proc_info'); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 16 | ▶ | if ( defined( $func_args{ipv4_string} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 15 | push( @ss_outputs, [ $func_args{ipv4_string}, 4 ] ); | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | 16 | ▶ | if ( defined( $func_args{ipv6_string} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 3 | push( @ss_outputs, [ $func_args{ipv6_string}, 6 ] ); | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | } else { | ||||||||||||||||||||||||||||
| 191 | 1 | ▶ | if ( !$func_args{ipv4} && !$func_args{ipv6} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$func_args{"ipv4"} or $func_args{"ipv6"}
MC/DC0%0/2
$func_args{"ipv4"} or $func_args{"ipv6"}
$func_args{"ipv4"}
$func_args{"ipv6"}
Truth table33%1/3
A or B
A= $func_args{"ipv4"}B= $func_args{"ipv6"}
| |||||||||||||||||||||||||||||
| 192 | 1 | die('Both the ipv4 and ipv6 args are false, leaving nothing for ss to be called for'); | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 0 | ▶ | if ( $^O !~ /linux/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 0 | die('According to $^O, this is not Linux and this is specifically written for the ss from iproute2'); | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 0 | ▶ | if ( $func_args{ipv4} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 0 | push( @ss_outputs, [ &_run_ss(4), 4 ] ); | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | 0 | ▶ | if ( $func_args{ipv6} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 0 | push( @ss_outputs, [ &_run_ss(6), 6 ] ); | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | } ## end else [ if ($strings_given) ] | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | # fetched once and then reused for each socket the PID in question owns | ||||||||||||||||||||||||||||
| 208 | 16 | my $proc_info = {}; | |||||||||||||||||||||||||||
| 209 | 16 | ▶ | if ( $func_args{proc_info} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 0 | $proc_info = &_proc_table_info; | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 16 | my $parse_state = { | |||||||||||||||||||||||||||
| 214 | func_args => \%func_args, | ||||||||||||||||||||||||||||
| 215 | proc_info => $proc_info, | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | # the PIDs only mean anything on this machine, so /proc is worth | ||||||||||||||||||||||||||||
| 218 | # poking at only when this module is the one that called ss | ||||||||||||||||||||||||||||
| 219 | live => !$strings_given, | ||||||||||||||||||||||||||||
| 220 | uid_cache => {}, | ||||||||||||||||||||||||||||
| 221 | }; | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 16 | my @nc_objects; | |||||||||||||||||||||||||||
| 224 | 16 | foreach my $ss_output (@ss_outputs) { | |||||||||||||||||||||||||||
| 225 | 18 | push( @nc_objects, &_parse_ss_output( $ss_output->[0], $ss_output->[1], $parse_state ) ); | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 16 | return @nc_objects; | |||||||||||||||||||||||||||
| 229 | } ## end sub ss_to_nc_objects | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | # | ||||||||||||||||||||||||||||
| 232 | # Calls ss for the specified IP version and returns the raw output. | ||||||||||||||||||||||||||||
| 233 | # | ||||||||||||||||||||||||||||
| 234 | 0 | ▶ | sub _run_ss { | ||||||||||||||||||||||||||
|
Subroutine
_run_ss: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 235 | 0 | my $ip_version = $_[0]; | |||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 0 | my $ss_raw = `ss -p${ip_version}an 2> /dev/null`; | |||||||||||||||||||||||||||
| 238 | 0 | ▶ | if ( $? != 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | 0 | die( 'Calling "ss -p' . $ip_version . 'an" failed with a exit code of ' . ( $? >> 8 ) ); | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 0 | return $ss_raw; | |||||||||||||||||||||||||||
| 243 | } ## end sub _run_ss | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | # | ||||||||||||||||||||||||||||
| 246 | # Parses the output of a single ss call into Net::Connection objects. | ||||||||||||||||||||||||||||
| 247 | # | ||||||||||||||||||||||||||||
| 248 | 18 | ▶ | sub _parse_ss_output { | ||||||||||||||||||||||||||
|
Subroutine
_parse_ss_output: called CC=19 SCAR=32.3
| |||||||||||||||||||||||||||||
| 249 | 18 | my $ss_raw = $_[0]; | |||||||||||||||||||||||||||
| 250 | 18 | my $ip_version = $_[1]; | |||||||||||||||||||||||||||
| 251 | 18 | my $parse_state = $_[2]; | |||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 18 | my %func_args = %{ $parse_state->{func_args} }; | |||||||||||||||||||||||||||
| 254 | 18 | my $proc_info = $parse_state->{proc_info}; | |||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 18 | my @nc_objects; | |||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 18 | foreach my $line ( split( /\n/, $ss_raw ) ) { | |||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | # skip the header and any blank lines | ||||||||||||||||||||||||||||
| 261 | 231 | ▶ | if ( ( $line =~ /^Netid/ ) || ( $line !~ /\S/ ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$line =~ /^Netid/ or not $line =~ /\S/
MC/DC50%1/2
$line =~ /^Netid/ or not $line =~ /\S/
$line =~ /^Netid/
not $line =~ /\S/
Truth table66%2/3
A or not(B)
A= $line =~ /^Netid/B= not $line =~ /\S/
| |||||||||||||||||||||||||||||
| 262 | 15 | next; | |||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | # the process column is left as one chunk as the process names in it | ||||||||||||||||||||||||||||
| 266 | # may contain whitespace | ||||||||||||||||||||||||||||
| 267 | 216 | my ( $netid, $conn_state, $recvq, $sendq, $local, $peer, $process_column ) = split( /[\ \t]+/, $line, 7 ); | |||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | # anything lacking the peer column is not parsable | ||||||||||||||||||||||||||||
| 270 | 216 | ▶ | if ( !defined($peer) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | 1 | next; | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 215 | my ( $local_host, $local_port ) = &_split_host_port($local); | |||||||||||||||||||||||||||
| 275 | 215 | my ( $peer_host, $peer_port ) = &_split_host_port($peer); | |||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | # ss only puts the address family in the netid for the protocols that | ||||||||||||||||||||||||||||
| 278 | # are version specific, so icmp6 stays icmp6 instead of becoming icmp66 | ||||||||||||||||||||||||||||
| 279 | 215 | my $proto = $netid; | |||||||||||||||||||||||||||
| 280 | 215 | ▶ | if ( substr( $proto, -1 ) ne $ip_version ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 281 | 213 | $proto = $proto . $ip_version; | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | # the basic args shared by every process holding this socket open | ||||||||||||||||||||||||||||
| 285 | my $socket_args = { | ||||||||||||||||||||||||||||
| 286 | local_host => $local_host, | ||||||||||||||||||||||||||||
| 287 | local_port => $local_port, | ||||||||||||||||||||||||||||
| 288 | foreign_host => $peer_host, | ||||||||||||||||||||||||||||
| 289 | foreign_port => $peer_port, | ||||||||||||||||||||||||||||
| 290 | proto => $proto, | ||||||||||||||||||||||||||||
| 291 | state => $conn_state, | ||||||||||||||||||||||||||||
| 292 | ports => $func_args{ports}, | ||||||||||||||||||||||||||||
| 293 | ptrs => $func_args{ptrs}, | ||||||||||||||||||||||||||||
| 294 | 215 | }; | |||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | # Net::Connection requires these to be numeric when they are defined | ||||||||||||||||||||||||||||
| 297 | 215 | ▶ | if ( defined($recvq) && ( $recvq =~ /^[0-9]+$/ ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $recvq and $recvq =~ /^[0-9]+$/
MC/DC0%0/2
defined $recvq and $recvq =~ /^[0-9]+$/
defined $recvq
$recvq =~ /^[0-9]+$/
Truth table33%1/3
A and B
A= defined $recvqB= $recvq =~ /^[0-9]+$/
| |||||||||||||||||||||||||||||
| 298 | 215 | $socket_args->{recvq} = $recvq; | |||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | 215 | ▶ | if ( defined($sendq) && ( $sendq =~ /^[0-9]+$/ ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $sendq and $sendq =~ /^[0-9]+$/
MC/DC0%0/2
defined $sendq and $sendq =~ /^[0-9]+$/
defined $sendq
$sendq =~ /^[0-9]+$/
Truth table33%1/3
A and B
A= defined $sendqB= $sendq =~ /^[0-9]+$/
| |||||||||||||||||||||||||||||
| 301 | 215 | $socket_args->{sendq} = $sendq; | |||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 215 | my @processes = &_parse_process_column($process_column); | |||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | # a socket with an empty process column is not held open by anything | ||||||||||||||||||||||||||||
| 307 | 215 | ▶ | if ( !@processes ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | 4 | ▶ | if ( $func_args{zombie_skip} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 309 | 3 | next; | |||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 1 | push( @nc_objects, Net::Connection->new($socket_args) ); | |||||||||||||||||||||||||||
| 313 | 1 | next; | |||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 211 | foreach my $process (@processes) { | |||||||||||||||||||||||||||
| 317 | 245 | my $args = { %{$socket_args} }; | |||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | 245 | $args->{pid} = $process->{pid}; | |||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | # ss truncates the process name to fifteen characters, so this is | ||||||||||||||||||||||||||||
| 322 | # only a fallback for when the process table is not being used | ||||||||||||||||||||||||||||
| 323 | 245 | $args->{proc} = $process->{name}; | |||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 245 | my $uid; | |||||||||||||||||||||||||||
| 326 | 245 | ▶ | if ( defined( $proc_info->{ $process->{pid} } ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | 0 | $uid = $proc_info->{ $process->{pid} }{uid}; | |||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 0 | foreach my $field (qw( proc wchan pctcpu pctmem pid_start )) { | |||||||||||||||||||||||||||
| 330 | 0 | ▶ | if ( defined( $proc_info->{ $process->{pid} }{$field} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 331 | 0 | $args->{$field} = $proc_info->{ $process->{pid} }{$field}; | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 245 | ▶ | if ( !defined($uid) && $parse_state->{live} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $uid and $parse_state->{"live"}
MC/DC0%0/2
not defined $uid and $parse_state->{"live"}
not defined $uid
$parse_state->{"live"}
Truth table33%1/3
not(A) and B
A= not defined $uidB= $parse_state->{"live"}
| |||||||||||||||||||||||||||||
| 337 | 0 | $uid = &_uid_for_pid( $process->{pid}, $parse_state->{uid_cache} ); | |||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | # Net::Connection dies when asked to resolve without a UID or | ||||||||||||||||||||||||||||
| 341 | # username to work from, so only ask when there is a UID | ||||||||||||||||||||||||||||
| 342 | 245 | ▶ | if ( defined($uid) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | 0 | $args->{uid} = $uid; | |||||||||||||||||||||||||||
| 344 | 0 | $args->{uid_resolve} = $func_args{uid_resolve}; | |||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | 245 | push( @nc_objects, Net::Connection->new($args) ); | |||||||||||||||||||||||||||
| 348 | } ## end foreach my $process (@processes) | ||||||||||||||||||||||||||||
| 349 | } ## end foreach my $line ( split( /\n/, $ss_raw ) ) | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 18 | return @nc_objects; | |||||||||||||||||||||||||||
| 352 | } ## end sub _parse_ss_output | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | # | ||||||||||||||||||||||||||||
| 355 | # Splits a 'address:port' column from ss into the two, returning them as a | ||||||||||||||||||||||||||||
| 356 | # two item array. | ||||||||||||||||||||||||||||
| 357 | # | ||||||||||||||||||||||||||||
| 358 | 438 | ▶ | sub _split_host_port { | ||||||||||||||||||||||||||
|
Subroutine
_split_host_port: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 359 | 438 | my $address_port = $_[0]; | |||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | # everything past the last colon is the port, which is what keeps the | ||||||||||||||||||||||||||||
| 362 | # colons in a IPv6 address from being mistaken for the separator | ||||||||||||||||||||||||||||
| 363 | 438 | my $separator = rindex( $address_port, ':' ); | |||||||||||||||||||||||||||
| 364 | 438 | ▶ | if ( $separator < 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 365 | 0 | return ( $address_port, '*' ); | |||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 438 | my $host = substr( $address_port, 0, $separator ); | |||||||||||||||||||||||||||
| 369 | 438 | my $port = substr( $address_port, $separator + 1 ); | |||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | # ss wraps IPv6 addresses in square brackets, with any scope ID left | ||||||||||||||||||||||||||||
| 372 | # outside of them, so '[fe80::1]%eno3' becomes 'fe80::1%eno3' | ||||||||||||||||||||||||||||
| 373 | 438 | $host =~ s/[\[\]]//g; | |||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 438 | return ( $host, $port ); | |||||||||||||||||||||||||||
| 376 | } ## end sub _split_host_port | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | # | ||||||||||||||||||||||||||||
| 379 | # Pulls the processes out of the process column, which reads like the below, | ||||||||||||||||||||||||||||
| 380 | # and returns them as an array of hash refs. | ||||||||||||||||||||||||||||
| 381 | # | ||||||||||||||||||||||||||||
| 382 | # users:(("rpcbind",pid=915,fd=4),("systemd",pid=1,fd=244)) | ||||||||||||||||||||||||||||
| 383 | # | ||||||||||||||||||||||||||||
| 384 | 220 | ▶ | sub _parse_process_column { | ||||||||||||||||||||||||||
|
Subroutine
_parse_process_column: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 385 | 220 | my $process_column = $_[0]; | |||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | 220 | my @processes; | |||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | 220 | ▶ | if ( !defined($process_column) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 390 | 5 | return @processes; | |||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | 215 | ▶ | while ( $process_column =~ /\(\"(.*?)\",pid=([0-9]+)(?:,fd=([0-9]+))?/g ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 394 | 248 | push( @processes, { name => $1, pid => $2, fd => $3 } ); | |||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | 215 | return @processes; | |||||||||||||||||||||||||||
| 398 | } ## end sub _parse_process_column | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | # | ||||||||||||||||||||||||||||
| 401 | # Returns a hash ref, keyed on PID, of the bits that Net::Connection wants | ||||||||||||||||||||||||||||
| 402 | # filled in that ss does not report. | ||||||||||||||||||||||||||||
| 403 | # | ||||||||||||||||||||||||||||
| 404 | 0 | ▶ | sub _proc_table_info { | ||||||||||||||||||||||||||
|
Subroutine
_proc_table_info: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 405 | 0 | my $proc_info = {}; | |||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | 0 | foreach my $proc_entry ( @{ Proc::ProcessTable->new->table } ) { | |||||||||||||||||||||||||||
| 408 | 0 | my %info = ( uid => $proc_entry->{uid} ); | |||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | 0 | ▶ | if ( !defined( $proc_entry->{cmndline} ) || ( $proc_entry->{cmndline} eq '' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $proc_entry->{"cmndline"} or $proc_entry->{"cmndline"} eq ""
MC/DC0%0/2
not defined $proc_entry->{"cmndline"} or $proc_entry->{"cmndline"} eq ""
not defined $proc_entry->{"cmndline"}
$proc_entry->{"cmndline"} eq ""
Truth table0%0/3
not(A) or B
A= not defined $proc_entry->{"cmndline"}B= $proc_entry->{"cmndline"} eq ""
| |||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | # kernel proc | ||||||||||||||||||||||||||||
| 413 | 0 | $info{proc} = '[' . $proc_entry->{fname} . ']'; | |||||||||||||||||||||||||||
| 414 | } else { | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | # non-kernel proc | ||||||||||||||||||||||||||||
| 417 | 0 | $info{proc} = $proc_entry->{cmndline}; | |||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | 0 | $info{pid_start} = $proc_entry->{start}; | |||||||||||||||||||||||||||
| 421 | 0 | $info{wchan} = &_wchan_for_pid( $proc_entry->{pid} ); | |||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | 0 | eval { $info{pctcpu} = $proc_entry->pctcpu; }; | |||||||||||||||||||||||||||
| 424 | 0 | eval { $info{pctmem} = $proc_entry->pctmem; }; | |||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | 0 | $proc_info->{ $proc_entry->{pid} } = \%info; | |||||||||||||||||||||||||||
| 427 | } ## end foreach my $proc_entry ( @{ Proc::ProcessTable->...}) | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | 0 | return $proc_info; | |||||||||||||||||||||||||||
| 430 | } ## end sub _proc_table_info | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | # | ||||||||||||||||||||||||||||
| 433 | # Proc::ProcessTable hands back the raw address for wchan on Linux, so the | ||||||||||||||||||||||||||||
| 434 | # symbol name has to come from /proc, which is not readable for every proc. | ||||||||||||||||||||||||||||
| 435 | # | ||||||||||||||||||||||||||||
| 436 | 0 | ▶ | sub _wchan_for_pid { | ||||||||||||||||||||||||||
|
Subroutine
_wchan_for_pid: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 437 | 0 | my $pid = $_[0]; | |||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | 0 | my $wchan; | |||||||||||||||||||||||||||
| 440 | 0 | ▶ | if ( open( my $wchan_fh, '<', '/proc/' . $pid . '/wchan' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 441 | 0 | $wchan = readline($wchan_fh); | |||||||||||||||||||||||||||
| 442 | 0 | close($wchan_fh); | |||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | 0 | ▶ | if ( defined($wchan) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 445 | 0 | chomp($wchan); | |||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | 0 | return $wchan; | |||||||||||||||||||||||||||
| 450 | } ## end sub _wchan_for_pid | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | # | ||||||||||||||||||||||||||||
| 453 | # ss does not report the UID, but /proc/<pid> is owned by the user the proc | ||||||||||||||||||||||||||||
| 454 | # is running as. The cache keeps the repeat sockets of a PID to one stat. | ||||||||||||||||||||||||||||
| 455 | # | ||||||||||||||||||||||||||||
| 456 | 0 | ▶ | sub _uid_for_pid { | ||||||||||||||||||||||||||
|
Subroutine
_uid_for_pid: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 457 | 0 | my $pid = $_[0]; | |||||||||||||||||||||||||||
| 458 | 0 | my $uid_cache = $_[1]; | |||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | 0 | ▶ | if ( !exists( $uid_cache->{$pid} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 461 | 0 | $uid_cache->{$pid} = ( stat( '/proc/' . $pid ) )[4]; | |||||||||||||||||||||||||||
| 462 | } | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | 0 | return $uid_cache->{$pid}; | |||||||||||||||||||||||||||
| 465 | } ## end sub _uid_for_pid | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >> | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | =head1 BUGS | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | Please report any bugs or feature requests to C<bug-net-connection-linux_ss at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 474 | the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Linux_ss>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 475 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | perldoc Net::Connection::Linux_ss | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | You can also look for information at: | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | =over 4 | ||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||
| 491 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Linux_ss> | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | L<https://cpanratings.perl.org/d/Net-Connection-Linux_ss> | ||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | L<https://metacpan.org/release/Net-Connection-Linux_ss> | ||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | =back | ||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | This software is Copyright (c) 2026 by Zane C. Bowers-Hadley. | ||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | The GNU Lesser General Public License, Version 2.1, February 1999 | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | =cut | ||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | 5 | 1; # End of Net::Connection::Linux_ss | |||||||||||||||||||||||||||