| 1 | package LinkLocal::IPv4::Interface::ARP; | ||
| 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 | use Moose; | ||
| 23 | use Moose::Util::TypeConstraints; | ||
| 24 | use LinkLocal::IPv4::Interface::Types; | ||
| 25 | |||
| 26 | use Net::Frame::Layer::ETH qw(:consts); | ||
| 27 | use Net::Frame::Simple; | ||
| 28 | use Net::Frame::Dump::Online; | ||
| 29 | use Net::Frame::Device; | ||
| 30 | use Net::Write::Layer2; | ||
| 31 | use Net::Netmask; | ||
| 32 | |||
| 33 | |||
| 34 | # ========== | ||
| 35 | # = packet = | ||
| 36 | # ========== | ||
| 37 | has 'packet' => ( | ||
| 38 | is => 'ro', | ||
| 39 | isa => 'ArpPacket', | ||
| 40 | handles => { | ||
| 41 | pack_arp => 'pack', | ||
| 42 | unpack_arp => 'unpack', | ||
| 43 | get_key => 'getKey', | ||
| 44 | get_key_reverse => 'getKeyReverse', | ||
| 45 | is_match => 'match', | ||
| 46 | print_arp => 'print', | ||
| 47 | dump_arp => 'dump', | ||
| 48 | }, | ||
| 49 | coerce => 1, | ||
| 50 | ); | ||
| 51 | |||
| 52 | |||
| 53 | sub send { | ||
| 54 | my $this = shift; | ||
| 55 | # TODO Handle send, packet object will be passed into a "sender machine" | ||
| 56 | } | ||
| 57 | |||
| 58 | no Moose; | ||
| 59 | |||
| 60 | __PACKAGE__->meta->make_immutable; | ||
| 61 | 1; | ||
| 62 | |||
| 63 | =head1 NAME | ||
| 64 | |||
| 65 | LinkLocal::IPv4::Interface::ARP - An ARP packet wrapper, this class allows for the easy construction | ||
| 66 | of either link-local Probe or Announce ARP Requests. | ||
| 67 | |||
| 68 | =head1 SYNOPSIS | ||
| 69 | |||
| 70 | use LinkLocal::IPv4::Interface::ARP; | ||
| 71 | |||
| 72 | $arp = LinkLocal::IPv4::Interface::ARP->new(); | ||
| 73 | |||
| 74 | # Initialize a hash reference for each type of ARP message required. | ||
| 75 | my $set_probe = { srcIp => '000.000.000.000', dstIp => '169.254.100.100' }; | ||
| 76 | my $set_announce = { srcIp => '169.254.100.100', dstIp => '169.254.100.100' }; | ||
| 77 | |||
| 78 | # Setup an ARP Probe packet, testing if link-local address 169.254.100.100 is in use | ||
| 79 | $arp->packet( $set_probe ); | ||
| 80 | |||
| 81 | # Setup an ARP Announce packet, claiming link-local address 169.254.100.100 on network | ||
| 82 | $arp->packet( $set_announce ); | ||
| 83 | |||
| 84 | # Also valid, if no srcIp hash key, assumes probe and zeroes sender address | ||
| 85 | my $set_probe = { dstIp => '169.254.100.100' }; | ||
| 86 | |||
| 87 | =head1 DESCRIPTION | ||
| 88 | |||
| 89 | This small ARP packet wrapper class plays a critical role in the dynamic configuration of IPv4 | ||
| 90 | link-local addresses. When a new address is pseudo-randomly determined, ARP Probe messages are | ||
| 91 | sent out over the interface which is being configured with a link-local address. The ARP packets | ||
| 92 | are of type ARP Request, have the source address zeroed out and the target address (dstIp) set | ||
| 93 | to the address which has been selected and is being probed. Three of these ARP Probes are sent | ||
| 94 | at randomly spaced intervals and if no conflict is detected during the process, the implementation | ||
| 95 | will claim the address for the interface within the unmanaged network. | ||
| 96 | |||
| 97 | After an address has been claimed, the implementation requires that three ARP Announce packets | ||
| 98 | be broadcast on the network. These packets have both sender and target addresses set to the | ||
| 99 | address which has been claimed by the implementation. This is done to clean up any stale ARP | ||
| 100 | cache's on the local-link. | ||
| 101 | |||
| 102 | =head2 ATTRIBUTES | ||
| 103 | |||
| 104 | =over 4 | ||
| 105 | |||
| 106 | =item C<packet> | ||
| 107 | |||
| 108 | This attribute holds the custom Moose type ArpPacket which in essence provides and allows for | ||
| 109 | the Moose class wrapper around the C<Net::Frame::Layer::ARP> object type. The packet attribute | ||
| 110 | exposes a number of mappings via the Moose attribute handler construct which allow for direct | ||
| 111 | mapping between them and the object reference type without forcing the additional layer of | ||
| 112 | indirection upon client code. | ||
| 113 | |||
| 114 | =item C<raw> | ||
| 115 | |||
| 116 | This attribute is inherited from the original C<Net::Frame::Layer> class and it holds the raw | ||
| 117 | layer as has just been pulled from the network or has been packed for network transmission (see | ||
| 118 | the dump_arp method below). | ||
| 119 | |||
| 120 | =back | ||
| 121 | |||
| 122 | =head2 METHODS | ||
| 123 | |||
| 124 | =over 4 | ||
| 125 | |||
| 126 | =item C<packet()> | ||
| 127 | |||
| 128 | The packet() method provides the most basic means of setting the target and sender addresses | ||
| 129 | to the desired value, depending upon the type of ARP packet being constructed. packet() takes | ||
| 130 | both a blessed or non-blessed hash reference, so the return type from object creation can be | ||
| 131 | passed to this method as long as this blessed reference isa C<Net::Frame::Layer::ARP> object. | ||
| 132 | |||
| 133 | =item C<pack_arp()> | ||
| 134 | |||
| 135 | Packs all attributes into a raw format suitable for injection into the network. Returns the | ||
| 136 | raw packed string on success, undef otherwise. Result is stored into raw attribute. See | ||
| 137 | L<Net::Frame::Layer> for further details. | ||
| 138 | |||
| 139 | =item C<unpack_arp()> | ||
| 140 | |||
| 141 | Unpacks raw data from network and stores attributes into the object. Returns $this on success, | ||
| 142 | undef otherwise. | ||
| 143 | |||
| 144 | =item C<get_key()> | ||
| 145 | |||
| 146 | =item C<get_key_reverse()> | ||
| 147 | |||
| 148 | These two methods are basically used to increase speed when using the recv method from | ||
| 149 | C<Net::Frame::Simple> via the packet object attribute. Usually, you use them when you need | ||
| 150 | to write an is_match() method. | ||
| 151 | |||
| 152 | =item C<is_match()> | ||
| 153 | |||
| 154 | This method is mostly used internally. You pass a C<LinkLocal::IPv4::Interface::ARP> packet | ||
| 155 | attribute as a parameter, and it returns true if this is a response corresponds with the | ||
| 156 | request, otherwise false. | ||
| 157 | |||
| 158 | =item C<print_arp()> | ||
| 159 | |||
| 160 | Prints a human readable string representation of the packet and its underlying state. | ||
| 161 | |||
| 162 | =item C<dump_arp()> | ||
| 163 | |||
| 164 | Prints a hexadecimal formatted respresentation of the packet, exactly how this would appear on | ||
| 165 | the network. | ||
| 166 | |||
| 167 | =back | ||
| 168 | |||
| 169 | B<Please Note>: While this class does not directly override or map all of the underlying attributes | ||
| 170 | and methods which are present in the base C<Net::Frame::Layer> type from which the Moose | ||
| 171 | class is derived, one could, with an extra level of indirection, access the underlying | ||
| 172 | attributes and methods from that base type. Clients of this module are encouraged to either conform | ||
| 173 | to the current interface or to extend it via Moose's inheritance mechanism if they wish to access | ||
| 174 | additional underlying functionality in this way. Please see the relevant POD for further details. | ||
| 175 | |||
| 176 | =head1 SEE ALSO | ||
| 177 | |||
| 178 | Refer to RFC-3927, "Dynamic Configuration of IPv4 Link-Local Adresses", the complete | ||
| 179 | text of which can be found in the top level of the package archive. | ||
| 180 | |||
| 181 | L<perl>, L<Net::Frame::Layer>, L<Net::Frame::Layer::ARP>, L<Moose> | ||
| 182 | |||
| 183 | This project is also hosted on github at: | ||
| 184 | git@github.com:raymroz/LinkLocal--IPv4.git | ||
| 185 | |||
| 186 | =head1 BUGS | ||
| 187 | |||
| 188 | What's a bug??? | ||
| 189 | |||
| 190 | =head1 AUTHOR | ||
| 191 | |||
| 192 | Ray Mroz, E<lt>mroz@cpan.orgE<gt> | ||
| 193 | |||
| 194 | =head1 COPYRIGHT AND LICENSE | ||
| 195 | |||
| 196 | Copyright (C) 2010 Ray Mroz | ||
| 197 | |||
| 198 | This program is free software: you can redistribute it and/or modify | ||
| 199 | it under the terms of the GNU General Public License as published by | ||
| 200 | the Free Software Foundation, either version 3 of the License, or | ||
| 201 | (at your option) any later version. | ||
| 202 | |||
| 203 | This program is distributed in the hope that it will be useful, | ||
| 204 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 205 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 206 | GNU General Public License for more details. | ||
| 207 | |||
| 208 | You should have received a copy of the GNU General Public License | ||
| 209 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 210 | |||
| 211 | |||
| 212 | =cut |