lib/Net/Tools/Socket/Err.pm

filter: Statementstmt 100% 49 / 49 Branchbran 90.0% 18 / 20 Conditioncond 80.0% 4 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 94.4% 85 / 90 CC 10 SCAR 12.4
CC
10
Cov
96%
sock_err_name
6.0
sock_err
4.0
sock_err_sys
2.0
CRAP 3.4
« lib/Net/Tools/Socket/Protocols.pm ↑ Summary blib/lib/Net/Tools.pm »
1 ##############################################################################
2 #
3 # Net::Tools::Socket::Err common socket, protocols and pair error codes
4 # Copyright (c) 2026 Vladi Belperchinov-Shabanski "Cade"
5 # <cade@noxrun.com>
6 # http://cade.noxrun.com/
7 #
8 # GPL
9 #
10 ##############################################################################
11 #
12 # one error state shared by all Net::Tools::Socket* modules:
13 #
14 # $SOCK_ERR last error code: negative SOCK_E_* constant, or positive
15 # errno value if a system call failed, undef if not set
16 # (no error). errno values are always positive, so they
17 # never collide with SOCK_E_* codes.
18 # $SOCK_ERRSTR last error text, undef if not set
19 # sock_err_name() name of $SOCK_ERR: 'SOCK_E_EOF', 'SOCK_E_TIMEOUT', etc.
20 # or errno name: 'EPIPE', 'EBADF', etc.
21 # %SOCK_ERRSTR SOCK_E_* code => text map, locked (read only)
22 #
23 # socket functions clear the state on entry and set it on error:
24 #
25 # sock_err_clear();
26 # ...
27 # return sock_err( SOCK_E_EOF, undef ) if ...;
28 # return sock_err( SOCK_E_PROTO, undef, "bad type" ) if ...; # extra text
29 # return sock_err_sys( undef ) unless defined sysread( ... );
30 #
31 # the optional third argument of sock_err() is extra text appended to
32 # $SOCK_ERRSTR after ": " (SOCK_E_* codes only, not errno values).
33 #
34 # the state describes only the last call, read it right after the call.
35 #
36 ##############################################################################
37 package Net::Tools::Socket::Err;
38 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 4 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 4 ▶use Hash::Util qw( lock_hash );
Subroutine BEGIN: called CC=1 SCAR=0.0
41 4 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 4 our $VERSION = '1.60';
44  
45 4 our @ISA = qw( Exporter );
46 4 our @EXPORT = qw(
47 $SOCK_ERR
48 $SOCK_ERRSTR
49 %SOCK_ERRSTR
50  
51 sock_err
52 sock_err_sys
53 sock_err_clear
54 sock_err_name
55  
56 SOCK_E_AGAIN
57 SOCK_E_EOF
58 SOCK_E_TOOBIG
59 SOCK_E_EMPTY
60 SOCK_E_MSGLEN
61 SOCK_E_SHORT
62 SOCK_E_TIMEOUT
63 SOCK_E_PROTO
64 SOCK_E_SYS
65 SOCK_E_INVAL
66 );
67  
68 use constant
69 4 ▶ {
Subroutine BEGIN: called CC=1 SCAR=0.0
70 4 SOCK_E_AGAIN => -1, # nothing to read or peer's queue full, retry
71 SOCK_E_EOF => -2, # peer closed
72 SOCK_E_TOOBIG => -3, # message larger than allowed
73 SOCK_E_EMPTY => -4, # empty or undef message
74 SOCK_E_MSGLEN => -5, # invalid or unreadable message length
75 SOCK_E_SHORT => -6, # short read or write below requested count
76 SOCK_E_TIMEOUT => -7, # timed out
77 SOCK_E_PROTO => -8, # unknown or forbidden protocol, invalid data
78 SOCK_E_SYS => -9, # system call failed but $! is not set
79 SOCK_E_INVAL => -10, # invalid argument or data (wide characters, etc.)
80 4 };
81  
82 4 our $SOCK_ERR;
83 4 our $SOCK_ERRSTR;
84  
85 4 our %SOCK_ERRSTR = (
86 SOCK_E_AGAIN() => 'try again',
87 SOCK_E_EOF() => 'end of file, peer closed',
88 SOCK_E_TOOBIG() => 'message too big',
89 SOCK_E_EMPTY() => 'empty message',
90 SOCK_E_MSGLEN() => 'invalid message length',
91 SOCK_E_SHORT() => 'short read or write below requested count',
92 SOCK_E_TIMEOUT() => 'timeout',
93 SOCK_E_PROTO() => 'protocol error',
94 SOCK_E_SYS() => 'system error, unknown errno',
95 SOCK_E_INVAL() => 'invalid argument',
96 );
97  
98 4 lock_hash( %SOCK_ERRSTR );
99  
100 # constant names, returned by sock_err_name()
101 4 my %SOCK_ERRNAME = (
102 SOCK_E_AGAIN() => 'SOCK_E_AGAIN',
103 SOCK_E_EOF() => 'SOCK_E_EOF',
104 SOCK_E_TOOBIG() => 'SOCK_E_TOOBIG',
105 SOCK_E_EMPTY() => 'SOCK_E_EMPTY',
106 SOCK_E_MSGLEN() => 'SOCK_E_MSGLEN',
107 SOCK_E_SHORT() => 'SOCK_E_SHORT',
108 SOCK_E_TIMEOUT() => 'SOCK_E_TIMEOUT',
109 SOCK_E_PROTO() => 'SOCK_E_PROTO',
110 SOCK_E_SYS() => 'SOCK_E_SYS',
111 SOCK_E_INVAL() => 'SOCK_E_INVAL',
112 );
113  
114 # errno value => name ('EPIPE', etc.), first name in sort order wins for
115 # aliases (EAGAIN/EWOULDBLOCK)
116 4 my %ERRNO_NAME;
117 4 for my $name ( sort @Errno::EXPORT_OK )
118 {
119 536 ▶ my $sub = Errno->can( $name ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $sub = "Errno"->can($name) or next 536 0 536
120 536 my $val = eval { $sub->() };
121 536 ▶ $ERRNO_NAME{ $val } //= $name if $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 536 0 536
Condition66%2/3
$ERRNO_NAME{$val} //= $name
l!l&&r!l&&!r
125240
MC/DC0%0/2
$ERRNO_NAME{$val} //= $name
$ERRNO_NAME{$val} $name
Truth table66%2/3
A //= B
A= $ERRNO_NAME{$val}B= $name
ABresult
1X1
011
000
122 }
123  
124 # sets error state to the given code and returns the second argument,
125 # so it can be used as: return sock_err( SOCK_E_EOF, undef );
126 # positive code is errno value, its text is taken from the system.
127 # optional third argument is extra text appended after ": " to the text of
128 # a negative (SOCK_E_*) code, ignored for errno values
129 sub sock_err
130 92 ▶{
Subroutine sock_err: called CC=4 SCAR=13.9
Pod documented
131 92 my $code = shift;
132 92 my $ret = shift;
133 92 my $xtra = shift;
134  
135 92 $SOCK_ERR = $code;
136 92 ▶ if( $code > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($code > 0) { } 4 88 92
137 {
138 4 local $! = $code;
139 4 $SOCK_ERRSTR = "$!";
140 }
141 else
142 {
143 # locked hash dies on access to unknown key, so check first
144 88 ▶ $SOCK_ERRSTR = exists $SOCK_ERRSTR{ $code } ? $SOCK_ERRSTR{ $code } : "unknown error code [$code]";
Branch: 1 branch
BranchTrueFalseTotal
exists $Net::Tools::Socket::Err::SOCK_ERRSTR{$code} ? : 86 2 88
145 88 ▶ $SOCK_ERRSTR .= ': ' . $xtra if $xtra;
Branch: 1 branch
BranchTrueFalseTotal
if $xtra 22 66 88
146 }
147  
148 92 return $ret;
149 }
150  
151 # sets error state to current errno ($!) after failed system call and returns
152 # the argument: return sock_err_sys( undef ) unless defined $res;
153 # SOCK_E_SYS if $! is not set. must be called right after the failed call.
154 sub sock_err_sys
155 4 ▶{
Subroutine sock_err_sys: called CC=2 SCAR=6.9
Pod documented
156 4 my $errno = $! + 0;
157 4 ▶ return sock_err( $errno > 0 ? $errno : SOCK_E_SYS, shift );
Branch: 1 branch
BranchTrueFalseTotal
$errno > 0 ? : 3 1 4
158 }
159  
160 # returns name ('SOCK_E_EOF', 'SOCK_E_TIMEOUT', etc.) of the given code or of
161 # the current $SOCK_ERR if no code given, errno name ('EPIPE', etc.) for
162 # positive codes, undef if not set
163 sub sock_err_name
164 19 ▶{
Subroutine sock_err_name: called CC=6 SCAR=17.9
Pod documented
165 19 ▶ my $code = @_ ? shift : $SOCK_ERR;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 14 5 19
166 19 ▶ return undef unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 1 18 19
167 18 ▶ return $ERRNO_NAME{ $code } || "ERRNO_$code" if $code > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $code > 0 5 13 18
Condition100%2/2
$ERRNO_NAME{$code} || "ERRNO_$code"
l!l
41
MC/DC100%1/1
$ERRNO_NAME{$code} || "ERRNO_$code"
$ERRNO_NAME{$code}
Truth table100%2/2
A
A= $ERRNO_NAME{$code}
Aresult
11
00
168 13 ▶ return exists $SOCK_ERRNAME{ $code } ? $SOCK_ERRNAME{ $code } : 'SOCK_E_UNKNOWN';
Branch: 1 branch
BranchTrueFalseTotal
exists $SOCK_ERRNAME{$code} ? : 12 1 13
169 }
170  
171 sub sock_err_clear
172 998 ▶{
Subroutine sock_err_clear: called CC=1 SCAR=0.0
Pod documented
173 998 $SOCK_ERR = undef;
174 998 $SOCK_ERRSTR = undef;
175 998 return 1;
176 }
177  
178 ##############################################################################
179 4 1;
180  
181  
182 =pod
183  
184 =head1 NAME
185  
186 Net::Tools::Socket::Err - error codes and error state shared by the Net::Tools::Socket modules
187  
188 =head1 SYNOPSIS
189  
190 use Net::Tools::Socket;
191 use Net::Tools::Socket::Err;
192  
193 my $data = socket_read_message( $socket, $timeout );
194 if( ! defined $data )
195 {
196 print "error: $SOCK_ERRSTR\n" if defined $SOCK_ERR;
197 reconnect() if $SOCK_ERR == SOCK_E_EOF;
198 print "name: " . sock_err_name() . "\n"; # 'SOCK_E_EOF', 'EBADF', etc.
199 }
200  
201 =head1 DESCRIPTION
202  
203 L<Net::Tools::Socket>, L<Net::Tools::Socket::Protocols> and
204 L<Net::Tools::Socket::Pair> share one error state. Every function of those
205 modules clears it on entry and sets it when it fails, so it describes the
206 last call only and must be read right after it.
207  
208 =head1 VARIABLES
209  
210 =over 4
211  
212 =item $SOCK_ERR
213  
214 The last error code: a negative SOCK_E_* constant, or the positive errno
215 value when a system call failed, undef when there is no error. errno values
216 are always positive and SOCK_E_* codes negative, so they never collide.
217  
218 =item $SOCK_ERRSTR
219  
220 The text of the last error, undef when there is no error. For errno values
221 it is the system text (C<"$!">), for SOCK_E_* codes it is taken from
222 %SOCK_ERRSTR and may be followed by ": " and details, for example the
223 rejected protocol type.
224  
225 =item %SOCK_ERRSTR
226  
227 SOCK_E_* code => text map. It is locked, reading an unknown key or changing
228 it dies.
229  
230 =back
231  
232 =head1 ERROR CODES
233  
234 SOCK_E_AGAIN nothing to read or peer's queue is full, retry
235 SOCK_E_EOF end of file, peer closed
236 SOCK_E_TOOBIG message larger than allowed
237 SOCK_E_EMPTY empty or undef message
238 SOCK_E_MSGLEN invalid or unreadable message length
239 SOCK_E_SHORT short read or write below requested count
240 SOCK_E_TIMEOUT timed out
241 SOCK_E_PROTO unknown or forbidden protocol type, or invalid data
242 SOCK_E_SYS system call failed but $! is not set
243 SOCK_E_INVAL invalid argument or data (wide characters, etc.)
244  
245 Which codes a function sets is documented with the function.
246  
247 =head1 FUNCTIONS
248  
249 These are used by the socket modules to set the state, and by callers to
250 read it.
251  
252 =head2 sock_err( $code, $ret, $xtra )
253  
254 Sets $SOCK_ERR to $code and $SOCK_ERRSTR to its text, then returns $ret, so
255 it can be used as:
256  
257 return sock_err( SOCK_E_EOF, undef ) if ...;
258  
259 A positive $code is an errno value and its text is the system one, $! is
260 not changed. An unknown negative $code gets the text
261 "unknown error code [$code]". $xtra, if given and not empty, is appended to
262 the text of a negative code after ": ", it is ignored for errno values.
263  
264 =head2 sock_err_sys( $ret )
265  
266 Sets the state from the current $! after a failed system call and returns
267 $ret. If $! is not set, $SOCK_ERR is SOCK_E_SYS. Must be called right after
268 the failed call, before anything else changes $!.
269  
270 =head2 sock_err_name( $code )
271  
272 Returns the name of $code, or of $SOCK_ERR if no $code is given:
273 'SOCK_E_EOF', 'SOCK_E_TIMEOUT', etc. for SOCK_E_* codes, 'SOCK_E_UNKNOWN'
274 for an unknown negative code, 'EPIPE', 'EBADF', etc. for errno values
275 ('ERRNO_<value>' if the value has no name), undef if not set.
276  
277 =head2 sock_err_clear()
278  
279 Clears $SOCK_ERR and $SOCK_ERRSTR to undef, returns 1.
280  
281 =head1 EXPORTS
282  
283 All variables, functions and SOCK_E_* constants are exported by default.
284  
285 =head1 SEE ALSO
286  
287 L<Net::Tools::Socket>, L<Net::Tools::Socket::Protocols>,
288 L<Net::Tools::Socket::Pair>, L<Errno>
289  
290 =head1 AUTHOR
291  
292 Vladi Belperchinov-Shabanski "Cade"
293 <cade@noxrun.com>
294 http://cade.noxrun.com/
295  
296 =head1 COPYRIGHT AND LICENSE
297  
298 Copyright (c) 2026 Vladi Belperchinov-Shabanski "Cade"
299  
300 This program is free software; you can redistribute it and/or modify it
301 under the terms of the GNU General Public License as published by the
302 Free Software Foundation; either version 2 of the License, or (at your
303 option) any later version.
304  
305 =cut
« lib/Net/Tools/Socket/Protocols.pm ↑ Summary blib/lib/Net/Tools.pm »