blib/lib/Net/UNIX.pm

filter: Statementstmt 75.5% 136 / 180 Branchbran 61.7% 58 / 94 Conditioncond 42.6% 29 / 68 MC/DCmcdc 16.6% 6 / 36 Subroutinesub 82.1% 23 / 28 Podpod 80.0% 8 / 10 totaltotal 62.5% 260 / 416 CC 72 SCAR 30.4
CC
72
Cov
65%
_init
43.9
bind
31.1
unpack_sockaddr_un
20.0
CRAP 20.8
« blib/lib/Net/Gen.pm ↑ Summary blib/lib/Net/UDP.pm »
1 # Copyright 1995,2002 Spider Boardman.
2 # All rights reserved.
3 #
4 # Automatic licensing for this software is available. This software
5 # can be copied and used under the terms of the GNU Public License,
6 # version 1 or (at your option) any later version, or under the
7 # terms of the Artistic license. Both of these can be found with
8 # the Perl distribution, which this software is intended to augment.
9 #
10 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
11 # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
12 # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13  
14 # rcsid: "@(#) $Id: UNIX.dat,v 1.22 2002/03/30 10:11:08 spider Exp $"
15  
16 package Net::UNIX;
17 1 ▶use 5.004_04; # new minimum Perl version for this package
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 #use Carp;
21 0 ▶sub carp { require Carp; goto &Carp::carp; }
Subroutine carp: not called CC=1 SCAR=6.9
Pod undocumented
22 0 ▶sub croak { require Carp; goto &Carp::croak; }
Subroutine croak: not called CC=1 SCAR=6.9
Pod undocumented
23 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS *AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.4
26 1 $VERSION = '1.0';
27 0 ▶ eval "sub Version { __PACKAGE__ . ' v$VERSION' }";
Subroutine Version: not called CC=1 SCAR=1.2
Pod documented
28 }
29  
30 1 ▶use AutoLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 #use Exporter ();
32 1 ▶use Net::Gen 1.0 qw(/pack_sockaddr$/ :sockvals :families);
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 @ISA = 'Net::Gen';
36  
37 # Items to export into callers namespace by default.
38 # (Move infrequently used names to @EXPORT_OK below.)
39  
40 1 @EXPORT = qw(
41 );
42  
43 1 @EXPORT_OK = qw(
44 pack_sockaddr_un
45 unpack_sockaddr_un
46 );
47  
48 1 %EXPORT_TAGS = (
49 routines => [qw(pack_sockaddr_un unpack_sockaddr_un)],
50 ALL => [@EXPORT, @EXPORT_OK],
51 );
52 1 *AUTOLOAD = \$Net::Gen::AUTOLOAD;
53 }
54  
55 ;# sub AUTOLOAD inherited from Net::Gen
56  
57 ;# since 5.003_96 will break simple subroutines with inherited autoload, cheat
58 sub AUTOLOAD
59 1 ▶{
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
60 #$Net::Gen::AUTOLOAD = $AUTOLOAD;
61 1 goto &Net::Gen::AUTOLOAD;
62 }
63  
64  
65 # Preloaded methods go here. Autoload methods go after __END__, and are
66 # processed by the autosplit program.
67  
68 ;# No additional sockopts for UNIX-domain sockets (?)
69  
70 1 my $sun_path_len =
71 length(Socket::unpack_sockaddr_un(Socket::pack_sockaddr_un('')));
72  
73 #& _canonpath($path) : returns NUL-padded $path for sun_addr
74 sub _canonpath ($)
75 10 ▶{
Subroutine _canonpath: called CC=4 SCAR=14.1
76 10 my $path = shift;
77 10 my $ix;
78 # extend to proper length
79 10 $ix = index($path, "\0");
80 10 ▶ if ($ix >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ix >= 0) { } 5 5 10
81 5 ▶ substr($path,$ix) = "\0" x ($sun_path_len - $ix)
Branch: 1 branch
BranchTrueFalseTotal
if $ix < $sun_path_len 5 0 5
82 if $ix < $sun_path_len;
83 }
84 else {
85 5 $ix = length($path);
86 5 ▶ if ($ix < $sun_path_len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ix < $sun_path_len) { } 5 0 5
87 5 $path .= "\0" x ($sun_path_len - $ix);
88 }
89 else {
90 0 $path .= "\0";
91 }
92 }
93 10 $path;
94 }
95  
96 #& pack_sockaddr_un([$family,] $path) : $packed_addr
97 sub pack_sockaddr_un ($;$)
98 5 ▶{
Subroutine pack_sockaddr_un: called CC=2 SCAR=7.1
Pod documented
99 5 my(@args) = @_;
100 5 ▶ unshift(@args, AF_UNIX) if @args == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @args == 1 0 5 5
101 5 pack_sockaddr($args[0], _canonpath($args[1]));
102 }
103  
104 #& unpack_sockaddr_un($sockaddr_un) : [$fam,] $path
105 sub unpack_sockaddr_un ($)
106 0 ▶{
Subroutine unpack_sockaddr_un: not called CC=4 SCAR=30.0
Pod documented
107 0 my $addr = shift;
108 0 my ($fam,$path) = unpack_sockaddr($addr);
109 0 my $nul = index($path, "\0");
110 0 ▶ if ($nul >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nul >= 0) 0 0 0
111 0 substr($path, $nul) = '';
112 }
113 0 ▶ $fam ||= AF_UNIX;
Condition0%0/2
$fam ||= 1
l!l
00
MC/DC0%0/1
$fam ||= 1
$fam
Truth table0%0/2
A
A= $fam
Aresult
11
00
114 0 ▶ wantarray ? ($fam, $path) : $path;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
115 }
116  
117 1 my $debug = 0;
118  
119 #& _debug($this, [$newval]) : oldval
120 sub _debug
121 142 ▶{
Subroutine _debug: called CC=3 SCAR=11.0
122 1 ▶ use attrs qw(locked);
Subroutine BEGIN: called CC=1 SCAR=0.0
123 142 my ($this,$newval) = @_;
124 142 ▶ return $this->debug($newval) if ref $this;
Branch: 1 branch
BranchTrueFalseTotal
if ref $this 124 18 142
125 18 my $prev = $debug;
126 18 ▶ $debug = 0+$newval if defined $newval;
Branch: 1 branch
BranchTrueFalseTotal
if defined $newval 0 18 18
127 18 $prev;
128 }
129  
130  
131 1 my %keyhandlers = (thispath => \&_setbindpath,
132 destpath => \&_setconnpath,
133 unbuffered_IO => \&_setbuf_unbuf,
134 unbuffered_io => \&_setbuf_unbuf,
135 );
136  
137 1 my @Keys = qw(unbuffered_input unbuffered_output);
138  
139 1 my %Keys; # for storing the registrations
140  
141  
142 #& new($class, [\%params]) : {$obj | undef}
143 sub new
144 5 ▶{
Subroutine new: called CC=11 SCAR=26.7
Pod documented
145 1 ▶ use attrs qw(locked);
Subroutine BEGIN: called CC=1 SCAR=0.0
146 5 my $whoami = $_[0]->_trace(\@_,1);
147 5 my($class,@Args,$self) = @_;
148 5 $self = $class->SUPER::new(@Args);
149 5 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 1 4 5
150 5 ▶ ($self || $class)->_trace(\@_,2,", self" .
Branch: 1 branch
BranchTrueFalseTotal
defined $self ? : 5 0 5
Condition33%1/3
$self || $class
l!l&&r!l&&!r
500
MC/DC0%0/2
$self || $class
$self $class
Truth table33%1/3
A || B
A= $selfB= $class
ABresult
1X1
011
000
151 (defined $self ? "=$self" : " undefined") .
152 " after sub-new");
153 5 ▶ if ($self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self) 5 0 5
154 # register our keys and their handlers
155 5 ▶ if (%Keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (%Keys) { } 4 1 5
156 4 $ {*$self}{Keys} = { %Keys };
157 }
158 else {
159 1 ▶ $self->register_param_keys(\@Keys) if @Keys;
Branch: 1 branch
BranchTrueFalseTotal
if @Keys 1 0 1
160 1 $self->register_param_handlers(\%keyhandlers);
161 1 %Keys = %{ $ {*$self}{Keys} };
162 }
163 # register our socket options
164 # none for AF_UNIX?
165 # set our expected parameters
166 5 $self->setparams({PF => PF_UNIX, AF => AF_UNIX,
167 unbuffered_output => 0,
168 unbuffered_input => 0,
169 type => SOCK_DGRAM},
170 -1);
171 5 ▶ if ($class eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq __PACKAGE__) 2 3 5
172 2 ▶ unless ($self->init(@Args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->init(@Args)) 0 2 2
173 0 local $!; # protect errno
174 0 undef $self; # from the side-effects of this
175 0 undef $self; # another statement needed for unwinding
176 }
177 }
178 }
179 5 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 5 5
180 0 ▶ if ($self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self) { } 0 0 0
181 0 print STDERR __PACKAGE__ . "::new returning self=$self\n";
182 }
183 else {
184 0 print STDERR __PACKAGE__ . "::new returning undef\n";
185 }
186 }
187 5 $self;
188 }
189  
190 #& _setbindpath($self, 'thispath', $path) : {'' | "carp string"}
191 sub _setbindpath
192 3 ▶{
Subroutine _setbindpath: called CC=3 SCAR=13.0
193 3 my($self,$what,$path) = @_;
194 3 my $ix;
195 3 ▶ if (!defined($path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 0 3 3
196 # removing, so cooperate
197 0 delete $ {*$self}{Parms}{srcaddrlist};
198 0 return '';
199 }
200 # canonicalize the path to be of the right length, if possible
201 3 $path = _canonpath($path);
202 3 $ix = index($path, "\0"); # check for NUL-termination
203 3 ▶ if (!$ix) { # empty path is not a bind
Branch: 1 branch
BranchTrueFalseTotal
if (not $ix) { } 0 3 3
204 0 delete $ {*$self}{Parms}{srcaddrlist};
205 0 $_[2] = undef;
206 }
207 else {
208 3 $ {*$self}{Parms}{srcaddrlist} =
209 3 [pack_sockaddr_un($self->getparam('AF',AF_UNIX,1), $path)];
210 }
211 3 '';
212 }
213  
214 #& _setconnpath($self, 'destpath', $path) : {'' | "carp string"}
215 sub _setconnpath
216 2 ▶{
Subroutine _setconnpath: called CC=3 SCAR=12.2
217 2 my($self,$what,$path) = @_;
218 2 my $ix;
219 2 ▶ if (!defined($path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 0 2 2
220 # removing, so cooperate
221 0 delete $ {*$self}{Parms}{dstaddrlist};
222 0 return '';
223 }
224 # canonicalize the path to be of the right length, if possible
225 2 $path = _canonpath($path);
226 2 $ix = index($path, "\0"); # check for NUL-termination
227 2 ▶ if (!$ix) { # empty path?
Branch: 1 branch
BranchTrueFalseTotal
if (not $ix) { } 0 2 2
228 0 "$what parameter has no path: $path";
229 }
230 else { # just try it here
231 2 $ {*$self}{Parms}{dstaddrlist} =
232 2 [pack_sockaddr_un($self->getparam('AF',AF_UNIX,1), $path)];
233 2 '';
234 }
235 }
236  
237 #& _init($self, whatpath[, $path][, \%params]) : {$self | undef}
238 sub _init
239 5 ▶{
Subroutine _init: called CC=28 SCAR=37.8
240 1 ▶ use attrs qw(locked method);
Subroutine BEGIN: called CC=1 SCAR=0.0
241 5 my ($self,$what,@args,$path,$parms) = @_;
242 5 ▶ if (@args == 1 or @args == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 or @args == 2) 4 1 5
Condition100%3/3
@args == 1 or @args == 2
l!l&&r!l&&!r
311
MC/DC100%2/2
@args == 1 or @args == 2
@args == 1 @args == 2
Truth table100%3/3
A or B
A= @args == 1B= @args == 2
ABresult
1X1
011
000
243 4 $parms = $args[-1];
244 4 ▶ $parms = undef
Branch: 1 branch
BranchTrueFalseTotal
unless $parms and ref $parms eq "HASH" 2 2 4
Condition66%2/3
$parms and ref $parms eq "HASH"
!ll&&!rl&&r
022
MC/DC50%1/2
$parms and ref $parms eq "HASH"
$parms ref $parms eq "HASH"
Truth table66%2/3
A and B
A= $parmsB= ref $parms eq "HASH"
ABresult
0X0
100
111
245 unless $parms and ref($parms) eq 'HASH';
246 4 $path = $args[0];
247 4 ▶ $path = undef
Branch: 1 branch
BranchTrueFalseTotal
if defined $path and ref $path 1 3 4
Condition66%2/3
defined $path and ref $path
!ll&&!rl&&r
031
MC/DC50%1/2
defined $path and ref $path
defined $path ref $path
Truth table66%2/3
A and B
A= defined $pathB= ref $path
ABresult
0X0
100
111
248 if defined($path) and ref($path);
249 }
250 5 ▶ croak("Invalid call to " . __PACKAGE__ . "::init(@_)")
Branch: 1 branch
BranchTrueFalseTotal
if @args == 2 and not $parms or @args > 2 or not $what 0 5 5
Condition44%4/9
@args == 2 and not $parms or @args > 2 or not $what
l!l&&r!l&&!r
005
@args == 2 and not $parms or @args > 2
l!l&&r!l&&!r
005
@args == 2 and not $parms
!ll&&!rl&&r
410
MC/DC0%0/4
@args == 2 and not $parms or @args > 2 or not $what
@args == 2 not $parms @args > 2 not $what
Truth table28%2/7
A and not(B) or C or not(D)
A= @args == 2B= not $parmsC= @args > 2D= not $what
ABCDresult
11XX1
0X1X1
101X1
0X011
10011
0X000
10000
251 if @args == 2 and !$parms or @args > 2 or !$what;
252 5 ▶ $parms ||= {};
Condition100%2/2
$parms ||= {}
l!l
23
MC/DC100%1/1
$parms ||= {}
$parms
Truth table100%2/2
A
A= $parms
Aresult
11
00
253 5 ▶ $$parms{$what} = $path if defined $path;
Branch: 1 branch
BranchTrueFalseTotal
if defined $path 3 2 5
254 5 ▶ return undef unless $self->SUPER::init($parms);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->SUPER::init($parms) 0 5 5
255 5 ▶ if (scalar %$parms) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar %$parms) 4 1 5
256 4 ▶ return undef unless $self->setparams($parms);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->setparams($parms) 0 4 4
257 }
258 5 ▶ $self->setparams({netgen_fakeconnect=>1},-1) if
Branch: 1 branch
BranchTrueFalseTotal
if $self->getparam("type") == 2 3 2 5
259 $self->getparam('type') == SOCK_DGRAM;
260 5 ▶ if ($self->getparams([qw(srcaddr srcaddrlist dstaddr dstaddrlist)],1) >0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getparams(["srcaddr", "srcaddrlist", "dstaddr", "dstaddrlist"], 1) > 0) 3 2 5
261 3 ▶ return undef unless $self->isopen or $self->open;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isopen or $self->open 0 3 3
Condition33%1/3
$self->isopen or $self->open
l!l&&r!l&&!r
030
MC/DC0%0/2
$self->isopen or $self->open
$self->isopen $self->open
Truth table33%1/3
A or B
A= $self->isopenB= $self->open
ABresult
1X1
011
000
262 3 ▶ if ($self->getparams([qw(srcaddr srcaddrlist)],1) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getparams(["srcaddr", "srcaddrlist"], 1) > 0) 2 1 3
263 2 ▶ return undef unless $self->isbound or $self->bind;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isbound or $self->bind 0 2 2
Condition33%1/3
$self->isbound or $self->bind
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->isbound or $self->bind
$self->isbound $self->bind
Truth table33%1/3
A or B
A= $self->isboundB= $self->bind
ABresult
1X1
011
000
264 }
265 3 ▶ if ($self->getparams([qw(dstaddr dstaddrlist)],1) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getparams(["dstaddr", "dstaddrlist"], 1) > 0) 1 2 3
266 1 ▶ return undef unless $self->isconnected or $self->connect or
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isconnected or $self->connect or $self->isconnecting and not $self->blocking 0 1 1
Condition22%2/9
$self->isconnected or $self->connect or $self->isconnecting and not $self->blocking
l!l&&r!l&&!r
100
$self->isconnected or $self->connect
l!l&&r!l&&!r
010
$self->isconnecting and not $self->blocking
!ll&&!rl&&r
000
MC/DC0%0/4
$self->isconnected or $self->connect or $self->isconnecting and not $self->blocking
$self->isconnected $self->connect $self->isconnecting not $self->blocking
Truth table20%1/5
A or B or C and not(D)
A= $self->isconnectedB= $self->connectC= $self->isconnectingD= not $self->blocking
ABCDresult
1XXX1
01XX1
00111
000X0
00100
267 $self->isconnecting and !$self->blocking;
268 }
269 }
270 5 $self;
271 }
272  
273 #& init($self [, $destpath][, \%params]) : {$self | undef}
274 sub init
275 2 ▶{
Subroutine init: called CC=1 SCAR=0.0
Pod documented
276 2 my ($self,@args) = @_;
277 2 $self->_init('destpath',@args);
278 }
279  
280 #& connect($self [, $destpath] [, \%newparams]) : boolean
281 sub connect
282 2 ▶{
Subroutine connect: called CC=11 SCAR=27.4
Pod documented
283 1 ▶ use attrs qw(locked method);
Subroutine BEGIN: called CC=1 SCAR=0.0
284 2 my($self,$path,$parms) = @_;
285 2 ▶ if (@_ > 3 or @_ == 3 and (!ref($parms) or ref($path))) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 3 or @_ == 3 and !ref($parms) || ref $path) 0 2 2
Condition44%4/9
@_ > 3 or @_ == 3 and !ref($parms) || ref $path
l!l&&r!l&&!r
002
@_ == 3 and !ref($parms) || ref $path
!ll&&!rl&&r
110
not ref $parms or ref $path
l!l&&r!l&&!r
001
MC/DC0%0/4
@_ > 3 or @_ == 3 and !ref($parms) || ref $path
@_ > 3 @_ == 3 not ref $parms ref $path
Truth table40%2/5
A or B and not(C) or D
A= @_ > 3B= @_ == 3C= not ref $parmsD= ref $path
ABCDresult
1XXX1
011X1
01011
00XX0
01000
286 0 croak("Invalid arguments to " . __PACKAGE__ . "::connect(@_), called");
287 }
288 2 ▶ if (@_ == 2 and ref $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2 and ref $path) { } 0 2 2
Condition33%1/3
@_ == 2 and ref $path
!ll&&!rl&&r
200
MC/DC0%0/2
@_ == 2 and ref $path
@_ == 2 ref $path
Truth table33%1/3
A and B
A= @_ == 2B= ref $path
ABresult
0X0
100
111
289 0 $parms = $path;
290 0 undef $path;
291 }
292 else {
293 2 ▶ $parms ||= {};
Condition100%2/2
$parms ||= {}
l!l
11
MC/DC100%1/1
$parms ||= {}
$parms
Truth table100%2/2
A
A= $parms
Aresult
11
00
294 }
295 2 ▶ if (defined $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path) 1 1 2
296 1 $$parms{destpath} = $path;
297 }
298 2 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 1 2
299 1 ▶ return unless $self->setparams($parms);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->setparams($parms) 0 1 1
300 }
301 2 $self->SUPER::connect;
302 }
303  
304 #& format_addr({$class|$obj} , $sockaddr) : $string
305 sub format_addr
306 0 ▶{
Subroutine format_addr: not called CC=2 SCAR=17.9
Pod documented
307 0 my ($this,$addr) = @_;
308 0 my ($fam,$sdata) = unpack_sockaddr($addr);
309 0 ▶ if ($fam == AF_UNIX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fam == 1) { } 0 0 0
310 0 $sdata = unpack_sockaddr_un($addr);
311 }
312 else {
313 0 $sdata = $this->SUPER::format_addr($addr);
314 }
315 0 $sdata;
316 }
317  
318 1 1;
319  
320  
321 # autoloaded methods go after the END token (& pod) below
322  
323 __END__
« blib/lib/Net/Gen.pm ↑ Summary blib/lib/Net/UDP.pm »