File Coverage

blib/lib/LinkLocal/IPv4/Interface/Types.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 24 24 100.0


line stmt bran cond sub pod time code
1             package LinkLocal::IPv4::Interface::Types;
2              
3             our $VERSION = '0.19_01';
4              
5             require 5.010_000;
6              
7             # Copyright (C) 2010 Raymond Mroz
8             #
9             # This program is free software: you can redistribute it and/or modify
10             # it under the terms of the GNU General Public License as published by
11             # the Free Software Foundation, either version 3 of the License, or
12             # (at your option) any later version.
13             #
14             # This program is distributed in the hope that it will be useful,
15             # but WITHOUT ANY WARRANTY; without even the implied warranty of
16             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17             # GNU General Public License for more details.
18             #
19             # You should have received a copy of the GNU General Public License
20             # along with this program. If not, see <http://www.gnu.org/licenses/>.
21              
22 1     1   11 use Moose;
  1         1  
  1         8  
23 1     1   6807 use Moose::Util::TypeConstraints;
  1         2  
  1         10  
24 1     1   6056 use Regexp::Common qw/ net pattern /;
  1         3111  
  1         6  
25 1     1   4372 use Net::Frame::Layer::ARP qw/:consts/;
  1         113174  
  1         205  
26 1     1   1123 use IO::Interface::Simple;
  1         56202  
  1         360  
27              
28             subtype 'ArpPacket'
29             => as class_type('Net::Frame::Layer::ARP');
30              
31             coerce 'ArpPacket'
32             => from 'HashRef'
33             => via { Net::Frame::Layer::ARP->new( %{$_} ) };
34              
35             subtype 'LinkLocalInterface'
36             => as class_type('IO::Interface::Simple');
37              
38             coerce 'LinkLocalInterface'
39             => from 'Str'
40             => via { IO::Interface::Simple->new($_) };
41              
42             subtype 'IpAddress'
43             => as 'Str'
44             => where { /^$RE{net}{IPv4}/ }
45             => message { "$_: Invalid IPv4 address format." };
46              
47             subtype 'LinkLocalAddress'
48             => as 'IpAddress'
49             => where { /^$RE{net}{linklocal}/ }
50             => message { "$_: Invalid IPv4 Link-Local Address" };
51              
52             # Custom Regexp::Common extension for link-local address pattern
53              
54             #====================================================================
55             # FIXME:2010-12-01: Investigate why this custom extension to
56             # the Regexp::Common module is not matching on the last two octets
57             #====================================================================
58              
59             my %LLoctet = (
60             dec => q{(?k:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})},
61             );
62              
63             my $LLprefix = '169.254';
64             my $IPsep = '[.]';
65              
66             pattern(
67             name => [qw (net linklocal)],
68             create => "($LLprefix$IPsep$LLoctet{dec}$IPsep$LLoctet{dec})",
69             );
70              
71 1     1   14 no Moose;
  1         1  
  1         22  
72              
73             __PACKAGE__->meta->make_immutable;
74             1;
75              
76             =head1 NAME
77              
78             LinkLocal::IPv4::Interface::Types - A collection of shared Moose subtype definitions and coercions.
79              
80             =head1 SYNOPSIS
81              
82             use Moose;
83             use LinkLocal::IPv4::Interface::Types;
84              
85             has 'ip_address' => (
86             is => 'ro',
87             isa => 'IpAddress',
88             writer => 'set_ip_address',
89             );
90              
91             has 'packet' => (
92             is => 'ro',
93             isa => 'ArpPacket',
94             writer => 'set_packet',
95             coerce => 1,
96             );
97            
98             sub set_attribs {
99             my $this = shift;
100             my ($ip_address, $net_settings_hashref) = @_;
101            
102             # Set up the attributes
103             $this->set_ip_address($ip_address);
104             $this->set_packet( $net_settings_hashref );
105             }
106              
107             =head1 DESCRIPTION
108              
109             This Moose-based wrapper object provides a collection of shared types, compliant with and dependant
110             upon the Moose type and constraint system. All type coercions which are defined and used are also present
111             here. Client code only need C<use> this file and it has access to all custom types in a consistent system
112             wide way. By collecting and presenting these defined types in one place, it guarantees easy and consistent
113             access to them on a project wide scope (see L<Moose::Manual::BestPractices>).
114              
115             =head2 SUBTYPES
116              
117             =over 4
118              
119             =item C<ArpPacket>
120              
121             From base type C<Object>, C<isa> C<Net::Frame::Layer::ARP>. ArpPacket provides the basis for both
122             ARP Probes and Announce messages, both of which are required in the implementation of
123             RFC-3927.
124              
125             =item C<LinkLocalInterface>
126              
127             From base type C<Object>, C<isa> C<IO::Interface::Simple>. LinkLocalInterface is a custom object
128             wrapper of a hardware network interface on the system being configured for dynamic link-local
129             addressing. It provides an entry point to this auto-ip framework.
130              
131             =item C<IpAddress>
132              
133             From base type C<Str>, uses C<Regexp::Common> to provide a type constraint for IPv4 dotted-decimal
134             notation addresses.
135              
136             =item C<LinkLocalAddress>
137              
138             From base type C<IpAddress>, this type provides for a type constraint for IPv4 dotted-decimal
139             notation addresses as is specified in RFC-3927; The prefix C<169.254/16> is reserved by IANA for
140             the exclusive use of link-local address allocation (noting that the first 256 and last 256
141             addresses in the C<169.254/16> prefix are reserved for future use and B<MUST NOT> be selected by
142             a host using this dynamic configuration mechanism).
143              
144             =back
145              
146             =head2 COERCIONS
147              
148             =over 4
149              
150             =item C<ArpPacket>
151              
152             Type coercion of a C<HashRef> into an C<ArpPacket> type via a C<Net::Frame::Layer::ARP>
153             object type.
154              
155             =item C<LinkLocalInterface>
156              
157             Type coercion of a C<Str> type representing a network device name into an object of
158             type C<IO::Interface::Simple>.
159              
160             =back
161              
162             =head1 SEE ALSO
163              
164             Refer to RFC-3927, I<Dynamic Configuration of IPv4 Link-Local Adresses>, the complete
165             text of which can be found in the top level of the package archive.
166              
167             L<perl>, L<Net::Frame::Layer::ARP>, L<IO::Interface::Simple>, L<Regexp::Common>, L<Moose>
168              
169             This project is also hosted on github at:
170             git@github.com:raymroz/LinkLocal--IPv4.git
171              
172             =head1 BUGS
173              
174             What's a bug???
175              
176             =head1 AUTHOR
177              
178             Ray Mroz, E<lt>mroz@cpan.orgE<gt>
179              
180             =head1 COPYRIGHT AND LICENSE
181              
182             Copyright (C) 2010 Ray Mroz
183              
184             This program is free software: you can redistribute it and/or modify
185             it under the terms of the GNU General Public License as published by
186             the Free Software Foundation, either version 3 of the License, or
187             (at your option) any later version.
188              
189             This program is distributed in the hope that it will be useful,
190             but WITHOUT ANY WARRANTY; without even the implied warranty of
191             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
192             GNU General Public License for more details.
193              
194             You should have received a copy of the GNU General Public License
195             along with this program. If not, see <http://www.gnu.org/licenses/>.
196              
197              
198             =cut