File Coverage

blib/lib/IO/Interface/Simple.pm
Criterion Covered Total %
statement 54 72 75.0
branch 9 10 90.0
condition 6 13 46.1
subroutine 19 30 63.3
pod 21 24 87.5
total 109 149 73.1


line stmt bran cond sub pod time code
1             package IO::Interface::Simple;
2 1     1   73423 use strict;
  1         3  
  1         41  
3 1     1   933 use IO::Socket;
  1         35842  
  1         5  
4 1     1   1173 use IO::Interface;
  1         4  
  1         271  
5              
6 1         11 use overload '""' => \&as_string,
7             eq => '_eq_',
8 1     1   3356 fallback => 1;
  1         2089  
9              
10             # class variable
11             my $socket;
12              
13             # class methods
14             sub interfaces {
15 1     1 1 2 my $class = shift;
16 1         4 my $s = $class->sock;
17 1   50     5 return sort {($a->index||0) <=> ($b->index||0) } map {$class->new($_)} $s->if_list;
  1   50     5  
  2         6  
18             }
19              
20             sub new {
21 5     5 1 21 my $class = shift;
22 5         8 my $if_name = shift;
23 5         13 my $s = $class->sock;
24 5 100       537 return unless defined $s->if_mtu($if_name);
25 4   33     42 return bless {s => $s,
26             name => $if_name},ref $class || $class;
27             }
28              
29             sub new_from_address {
30 2     2 1 4 my $class = shift;
31 2         5 my $addr = shift;
32 2         5 my $s = $class->sock;
33 2 100       16 my $name = $s->addr_to_interface($addr) or return;
34 1         4 return $class->new($name);
35             }
36              
37             sub new_from_index {
38 2     2 1 252 my $class = shift;
39 2         3 my $index = shift;
40 2         6 my $s = $class->sock;
41 2 100       135 my $name = $s->if_indextoname($index) or return;
42 1         4 return $class->new($name);
43             }
44              
45             sub sock {
46 19     19 0 23 my $self = shift;
47 19 100       41 if (ref $self) {
48 9   33     106 return $self->{s} ||= $socket;
49             } else {
50 10   66     57 return $socket ||= IO::Socket::INET->new(Proto=>'udp');
51             }
52             }
53              
54             sub _eq_ {
55 4     4   22 return shift->name eq shift;
56             }
57              
58             sub as_string {
59 1     1 0 23 shift->name;
60             }
61              
62             sub name {
63 14     14 1 173 shift->{name};
64             }
65              
66             sub address {
67 1     1 1 250 my $self = shift;
68 1         3 $self->sock->if_addr($self->name,@_);
69             }
70              
71             sub broadcast {
72 0     0 1 0 my $self = shift;
73 0         0 $self->sock->if_broadcast($self->name,@_);
74             }
75              
76             sub netmask {
77 1     1 1 3 my $self = shift;
78 1         4 $self->sock->if_netmask($self->name,@_);
79             }
80              
81             sub dstaddr {
82 0     0 0 0 my $self = shift;
83 0         0 $self->sock->if_dstaddr($self->name,@_);
84             }
85              
86             sub hwaddr {
87 0     0 1 0 my $self = shift;
88 0         0 $self->sock->if_hwaddr($self->name,@_);
89             }
90              
91             sub flags {
92 4     4 1 5 my $self = shift;
93 4         8 $self->sock->if_flags($self->name,@_);
94             }
95              
96             sub mtu {
97 0     0 1 0 my $self = shift;
98 0         0 $self->sock->if_mtu($self->name,@_);
99             }
100              
101             sub metric {
102 0     0 1 0 my $self = shift;
103 0         0 $self->sock->if_metric($self->name,@_);
104             }
105              
106             sub index {
107 3     3 1 5 my $self = shift;
108 3         7 return $self->sock->if_index($self->name);
109             }
110              
111 2     2 1 345 sub is_running { shift->_gettestflag(IO::Interface::IFF_RUNNING(),@_) }
112 0     0 1 0 sub is_broadcast { shift->_gettestflag(IO::Interface::IFF_BROADCAST(),@_) }
113 0     0 1 0 sub is_pt2pt { shift->_gettestflag(IO::Interface::IFF_POINTOPOINT(),@_) }
114 2     2 1 15 sub is_loopback { shift->_gettestflag(IO::Interface::IFF_LOOPBACK(),@_) }
115 0     0 1 0 sub is_promiscuous { shift->_gettestflag(IO::Interface::IFF_PROMISC(),@_) }
116 0     0 1 0 sub is_multicast { shift->_gettestflag(IO::Interface::IFF_MULTICAST(),@_) }
117 0     0 1 0 sub is_notrailers { shift->_gettestflag(IO::Interface::IFF_NOTRAILERS(),@_) }
118 0     0 1 0 sub is_noarp { shift->_gettestflag(IO::Interface::IFF_NOARP(),@_) }
119              
120             sub _gettestflag {
121 4     4   5 my $self = shift;
122 4         5 my $bitmask = shift;
123 4         10 my $flags = $self->flags;
124 4 50       10 if (@_) {
125 0         0 $flags |= $bitmask;
126 0         0 $self->flags($flags);
127             } else {
128 4         14 return ($flags & $bitmask) != 0;
129             }
130             }
131              
132             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 Stein Elstein@cshl.orgE
271              
272             This module is distributed under the same license as Perl itself.
273              
274             =head1 SEE ALSO
275              
276             L, L, L), L, L
277              
278             =cut
279