| 1 | package LinkLocal::IPv4::Interface::Types; | ||
| 2 | |||
| 3 | 1 | our $VERSION = '0.19_01'; | |
| 4 | |||
| 5 | 1 | 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 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | 1 | ▶ | use Moose::Util::TypeConstraints; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 24 | 1 | ▶ | use Regexp::Common qw/ net pattern /; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 25 | 1 | ▶ | use Net::Frame::Layer::ARP qw/:consts/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 26 | 1 | ▶ | use IO::Interface::Simple; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | |||
| 28 | 1 | subtype 'ArpPacket' | |
| 29 | => as class_type('Net::Frame::Layer::ARP'); | ||
| 30 | |||
| 31 | coerce 'ArpPacket' | ||
| 32 | => from 'HashRef' | ||
| 33 | 0 | ▶ | => via { Net::Frame::Layer::ARP->new( %{$_} ) }; |
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||
| 34 | |||
| 35 | 1 | subtype 'LinkLocalInterface' | |
| 36 | => as class_type('IO::Interface::Simple'); | ||
| 37 | |||
| 38 | coerce 'LinkLocalInterface' | ||
| 39 | => from 'Str' | ||
| 40 | 0 | ▶ | => via { IO::Interface::Simple->new($_) }; |
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||
| 41 | |||
| 42 | subtype 'IpAddress' | ||
| 43 | => as 'Str' | ||
| 44 | 0 | ▶ | => where { /^$RE{net}{IPv4}/ } |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 45 | 0 | ▶ | => message { "$_: Invalid IPv4 address format." }; |
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||
| 46 | |||
| 47 | subtype 'LinkLocalAddress' | ||
| 48 | => as 'IpAddress' | ||
| 49 | 0 | ▶ | => where { /^$RE{net}{linklocal}/ } |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 50 | 0 | ▶ | => message { "$_: Invalid IPv4 Link-Local Address" }; |
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||
| 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 | 1 | my %LLoctet = ( | |
| 60 | dec => q{(?k:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})}, | ||
| 61 | ); | ||
| 62 | |||
| 63 | 1 | my $LLprefix = '169.254'; | |
| 64 | 1 | my $IPsep = '[.]'; | |
| 65 | |||
| 66 | 1 | pattern( | |
| 67 | name => [qw (net linklocal)], | ||
| 68 | create => "($LLprefix$IPsep$LLoctet{dec}$IPsep$LLoctet{dec})", | ||
| 69 | ); | ||
| 70 | |||
| 71 | 1 | ▶ | no Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 72 | |||
| 73 | 1 | __PACKAGE__->meta->make_immutable; | |
| 74 | 1 | 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 | ||