blib/lib/IO/Interface/Simple.pm

filter: Statementstmt 75.6% 56 / 74 Branchbran 90.0% 9 / 10 Conditioncond 46.1% 6 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 63.3% 19 / 30 Podpod 87.5% 21 / 24 totaltotal 69.8% 111 / 159 CC 11 SCAR 7.3
CC
11
Cov
73%
sock
4.2
interfaces
3.1
new
3.1
CRAP 2.1
« blib/lib/IO/Interface.pm ↑ Summary Interface.xs »
1 package IO::Interface::Simple;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use IO::Interface;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 use overload '""' => \&as_string,
7 eq => '_eq_',
8 1 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # class variable
11 1 my $socket;
12  
13 # class methods
14 sub interfaces {
15 1 ▶ my $class = shift;
Subroutine interfaces: called CC=3 SCAR=11.3
Pod documented
16 1 my $s = $class->sock;
17 1 ▶ return sort {($a->index||0) <=> ($b->index||0) } map {$class->new($_)} $s->if_list;
Condition50%2/4
$a->index || 0
l!l
10
$b->index || 0
l!l
10
MC/DC0%0/1
$a->index || 0
$a->index
MC/DC0%0/1
$b->index || 0
$b->index
Truth table50%1/2
A
A= $a->index
Aresult
11
00
Truth table50%1/2
A
A= $b->index
Aresult
11
00
18 }
19  
20 sub new {
21 5 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.2
Pod documented
22 5 my $if_name = shift;
23 5 my $s = $class->sock;
24 5 ▶ return unless defined $s->if_mtu($if_name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s->if_mtu($if_name) 1 4 5
25 4 ▶ return bless {s => $s,
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
26 name => $if_name},ref $class || $class;
27 }
28  
29 sub new_from_address {
30 2 ▶ my $class = shift;
Subroutine new_from_address: called CC=2 SCAR=6.9
Pod documented
31 2 my $addr = shift;
32 2 my $s = $class->sock;
33 2 ▶ my $name = $s->addr_to_interface($addr) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $name = $s->addr_to_interface($addr) or return 1 1 2
34 1 return $class->new($name);
35 }
36  
37 sub new_from_index {
38 2 ▶ my $class = shift;
Subroutine new_from_index: called CC=2 SCAR=6.9
Pod documented
39 2 my $index = shift;
40 2 my $s = $class->sock;
41 2 ▶ my $name = $s->if_indextoname($index) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $name = $s->if_indextoname($index) or return 1 1 2
42 1 return $class->new($name);
43 }
44  
45 sub sock {
46 19 ▶ my $self = shift;
Subroutine sock: called CC=4 SCAR=14.5
Pod undocumented
47 19 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 9 10 19
48 9 ▶ return $self->{s} ||= $socket;
Condition33%1/3
$self->{"s"} ||= $socket
l!l&&r!l&&!r
900
MC/DC0%0/2
$self->{"s"} ||= $socket
$self->{"s"} $socket
Truth table33%1/3
A ||= B
A= $self->{"s"}B= $socket
ABresult
1X1
011
000
49 } else {
50 10 ▶ return $socket ||= IO::Socket::INET->new(Proto=>'udp');
Condition66%2/3
$socket ||= "IO::Socket::INET"->new("Proto", "udp")
l!l&&r!l&&!r
910
MC/DC0%0/2
$socket ||= "IO::Socket::INET"->new("Proto", "udp")
$socket "IO::Socket::INET"->new("Proto", "udp")
Truth table66%2/3
A ||= B
A= $socketB= "IO::Socket::INET"->new("Proto", "udp")
ABresult
1X1
011
000
51 }
52 }
53  
54 sub _eq_ {
55 4 ▶ return shift->name eq shift;
Subroutine _eq_: called CC=1 SCAR=0.0
56 }
57  
58 sub as_string {
59 1 ▶ shift->name;
Subroutine as_string: called CC=1 SCAR=0.0
Pod undocumented
60 }
61  
62 sub name {
63 14 ▶ shift->{name};
Subroutine name: called CC=1 SCAR=0.0
Pod documented
64 }
65  
66 sub address {
67 1 ▶ my $self = shift;
Subroutine address: called CC=1 SCAR=0.0
Pod documented
68 1 $self->sock->if_addr($self->name,@_);
69 }
70  
71 sub broadcast {
72 0 ▶ my $self = shift;
Subroutine broadcast: not called CC=1 SCAR=6.9
Pod documented
73 0 $self->sock->if_broadcast($self->name,@_);
74 }
75  
76 sub netmask {
77 1 ▶ my $self = shift;
Subroutine netmask: called CC=1 SCAR=0.0
Pod documented
78 1 $self->sock->if_netmask($self->name,@_);
79 }
80  
81 sub dstaddr {
82 0 ▶ my $self = shift;
Subroutine dstaddr: not called CC=1 SCAR=6.9
Pod undocumented
83 0 $self->sock->if_dstaddr($self->name,@_);
84 }
85  
86 sub hwaddr {
87 0 ▶ my $self = shift;
Subroutine hwaddr: not called CC=1 SCAR=6.9
Pod documented
88 0 $self->sock->if_hwaddr($self->name,@_);
89 }
90  
91 sub flags {
92 4 ▶ my $self = shift;
Subroutine flags: called CC=1 SCAR=0.0
Pod documented
93 4 $self->sock->if_flags($self->name,@_);
94 }
95  
96 sub mtu {
97 0 ▶ my $self = shift;
Subroutine mtu: not called CC=1 SCAR=6.9
Pod documented
98 0 $self->sock->if_mtu($self->name,@_);
99 }
100  
101 sub metric {
102 0 ▶ my $self = shift;
Subroutine metric: not called CC=1 SCAR=6.9
Pod documented
103 0 $self->sock->if_metric($self->name,@_);
104 }
105  
106 sub index {
107 3 ▶ my $self = shift;
Subroutine index: called CC=1 SCAR=0.0
Pod documented
108 3 return $self->sock->if_index($self->name);
109 }
110  
111 2 ▶sub is_running { shift->_gettestflag(IO::Interface::IFF_RUNNING(),@_) }
Subroutine is_running: called CC=1 SCAR=0.0
Pod documented
112 0 ▶sub is_broadcast { shift->_gettestflag(IO::Interface::IFF_BROADCAST(),@_) }
Subroutine is_broadcast: not called CC=1 SCAR=6.9
Pod documented
113 0 ▶sub is_pt2pt { shift->_gettestflag(IO::Interface::IFF_POINTOPOINT(),@_) }
Subroutine is_pt2pt: not called CC=1 SCAR=6.9
Pod documented
114 2 ▶sub is_loopback { shift->_gettestflag(IO::Interface::IFF_LOOPBACK(),@_) }
Subroutine is_loopback: called CC=1 SCAR=0.0
Pod documented
115 0 ▶sub is_promiscuous { shift->_gettestflag(IO::Interface::IFF_PROMISC(),@_) }
Subroutine is_promiscuous: not called CC=1 SCAR=6.9
Pod documented
116 0 ▶sub is_multicast { shift->_gettestflag(IO::Interface::IFF_MULTICAST(),@_) }
Subroutine is_multicast: not called CC=1 SCAR=6.9
Pod documented
117 0 ▶sub is_notrailers { shift->_gettestflag(IO::Interface::IFF_NOTRAILERS(),@_) }
Subroutine is_notrailers: not called CC=1 SCAR=6.9
Pod documented
118 0 ▶sub is_noarp { shift->_gettestflag(IO::Interface::IFF_NOARP(),@_) }
Subroutine is_noarp: not called CC=1 SCAR=6.9
Pod documented
119  
120 sub _gettestflag {
121 4 ▶ my $self = shift;
Subroutine _gettestflag: called CC=2 SCAR=7.6
122 4 my $bitmask = shift;
123 4 my $flags = $self->flags;
124 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 4 4
125 0 $flags |= $bitmask;
126 0 $self->flags($flags);
127 } else {
128 4 return ($flags & $bitmask) != 0;
129 }
130 }
131  
132 1 1;
133  
134 =head1 NAME
135  
136 IO::Interface::Simple - Perl extension for access to network card configuration information
137  
138 =head1 SYNOPSIS
139  
140 use IO::Interface::Simple;
141  
142 my $if1 = IO::Interface::Simple->new('eth0');
143 my $if2 = IO::Interface::Simple->new_from_address('127.0.0.1');
144 my $if3 = IO::Interface::Simple->new_from_index(1);
145  
146 my @interfaces = IO::Interface::Simple->interfaces;
147  
148 for my $if (@interfaces) {
149 print "interface = $if\n";
150 print "addr = ",$if->address,"\n",
151 "broadcast = ",$if->broadcast,"\n",
152 "netmask = ",$if->netmask,"\n",
153 "dstaddr = ",$if->dstaddr,"\n",
154 "hwaddr = ",$if->hwaddr,"\n",
155 "mtu = ",$if->mtu,"\n",
156 "metric = ",$if->metric,"\n",
157 "index = ",$if->index,"\n";
158  
159 print "is running\n" if $if->is_running;
160 print "is broadcast\n" if $if->is_broadcast;
161 print "is p-to-p\n" if $if->is_pt2pt;
162 print "is loopback\n" if $if->is_loopback;
163 print "is promiscuous\n" if $if->is_promiscuous;
164 print "is multicast\n" if $if->is_multicast;
165 print "is notrailers\n" if $if->is_notrailers;
166 print "is noarp\n" if $if->is_noarp;
167 }
168  
169  
170 =head1 DESCRIPTION
171  
172 IO::Interface::Simple allows you to interrogate and change network
173 interfaces. It has overlapping functionality with Net::Interface, but
174 might compile and run on more platforms.
175  
176 =head2 Class Methods
177  
178 =over 4
179  
180 =item $interface = IO::Interface::Simple->new('eth0')
181  
182 Given an interface name, new() creates an interface object.
183  
184 =item @iflist = IO::Interface::Simple->interfaces;
185  
186 Returns a list of active interface objects.
187  
188 =item $interface = IO::Interface::Simple->new_from_address('192.168.0.1')
189  
190 Returns the interface object corresponding to the given address.
191  
192 =item $interface = IO::Interface::Simple->new_from_index(2)
193  
194 Returns the interface object corresponding to the given numeric
195 index. This is only supported on BSD-ish platforms.
196  
197 =back
198  
199 =head2 Object Methods
200  
201 =over 4
202  
203 =item $name = $interface->name
204  
205 Get the name of the interface. The interface object is also overloaded
206 so that if you use it in a string context it is the same as calling
207 name().
208  
209 =item $index = $interface->index
210  
211 Get the index of the interface. This is only supported on BSD-like
212 platforms.
213  
214 =item $addr = $interface->address([$newaddr])
215  
216 Get or set the interface's address.
217  
218  
219 =item $addr = $interface->broadcast([$newaddr])
220  
221 Get or set the interface's broadcast address.
222  
223 =item $addr = $interface->netmask([$newmask])
224  
225 Get or set the interface's netmask.
226  
227 =item $addr = $interface->hwaddr([$newaddr])
228  
229 Get or set the interface's hardware address.
230  
231 =item $addr = $interface->mtu([$newmtu])
232  
233 Get or set the interface's MTU.
234  
235 =item $addr = $interface->metric([$newmetric])
236  
237 Get or set the interface's metric.
238  
239 =item $flags = $interface->flags([$newflags])
240  
241 Get or set the interface's flags. These can be ANDed with the IFF
242 constants exported by IO::Interface or Net::Interface in order to
243 interrogate the state and capabilities of the interface. However, it
244 is probably more convenient to use the broken-out methods listed
245 below.
246  
247 =item $flag = $interface->is_running([$newflag])
248  
249 =item $flag = $interface->is_broadcast([$newflag])
250  
251 =item $flag = $interface->is_pt2pt([$newflag])
252  
253 =item $flag = $interface->is_loopback([$newflag])
254  
255 =item $flag = $interface->is_promiscuous([$newflag])
256  
257 =item $flag = $interface->is_multicast([$newflag])
258  
259 =item $flag = $interface->is_notrailers([$newflag])
260  
261 =item $flag = $interface->is_noarp([$newflag])
262  
263 Get or set the corresponding configuration parameters. Note that the
264 operating system may not let you set some of these.
265  
266 =back
267  
268 =head1 AUTHOR
269  
270 Lincoln D. Stein <lincoln.stein@gmail.com>
271 Copyright 2001-2014, Lincoln D. Stein.
272  
273 This library is distributed under the Perl Artistic License
274 2.0. Please see LICENSE for more information.
275  
276 =head1 SUPPORT
277  
278 For feature requests, bug reports and code contributions, please use
279 the GitHub repository at
280 https://github.com/lstein/LibIO-Interface-Perl
281  
282 =head1 SEE ALSO
283  
284 L<perl>, L<IO::Socket>, L<IO::Multicast>), L<IO::Interface>, L<Net::Interface>
285  
286 =cut
287  
« blib/lib/IO/Interface.pm ↑ Summary Interface.xs »