Interface/Cache.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 98 CC 2 SCAR 9.6
CC
2
Cov
0%
get_last_ip
6.0
_build_cache
2.0
cache_this_ip
2.0
CRAP 2.6
1 package LinkLocal::IPv4::Interface::Cache;
2  
3 our $VERSION = '0.19_01';
4  
5 require 5.010_000;
6  
7 # Copyright (C) 2010 Raymond Mroz, Tony Li Xu
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 LinkLocal::IPv4::Interface::Types;
23 use Moose;
24 use MooseX::Params::Validate;
25 use IO::File;
26 use Try::Tiny;
27  
28 use feature ':5.10';
29  
30 # Attributes
31 has '_cache' => (
32 is => 'bare',
33 isa => 'IO::File',
34 builder => '_build_cache',
35 lazy => 1,
36 reader => 'get_cache',
37 handles => {
38 _slurp_file => 'getlines',
39 _record_ip => 'printf',
40 _refresh_cache => 'seek',
41 },
42 init_arg => undef,
43 );
44  
45 sub _build_cache {
46 my $filename; # Cache file name
47  
48 try {
49 # Determine the file name based on OS
50 given ($^O) {
51 when ( m/linux/ || m/bsd/ ) { # Linux || (Free|Open|Net)BSD
52 $filename = "/var/cache/link-local/link-local.tab";
53 }
54 when (m/darwin/) { # Mac OS X
55 $filename = "/Library/Caches/org.cpan.cache.link-local";
56 }
57 when (m/solaris/) { # SunOS
58 $filename = "/etc/link-local/link-local.tab";
59 }
60 when (m/MSWin32/) { # Windows
61 $filename = "%ALLUSERSPROFILE%\\link-local\\link-local.tab";
62 }
63 default {
64 die "Unsupported OS type";
65 }
66 }
67  
68 # Create IO::File object and return
69 return new IO::File( $filename, O_RDWR | O_CREAT );
70 }
71 catch {
72 die "Error while opening cache file: $!";
73 };
74 }
75  
76 sub get_last_ip {
77 my $this = shift;
78 my ( @buffer, %cache ) = ();
79  
80 # Validate parameters tyeps
81 my ($given_ifc) = pos_validated_list( \@_, { isa => 'Str' } );
82  
83 # Slurp the contents of buffer into a file
84 try {
85 $this->_refresh_cache( 0, 0 );
86 @buffer = $this->_slurp_file();
87 }
88 catch {
89 die "Error while slurping cache file: $!\n";
90 };
91  
92 my $size = @buffer;
93 if ( $size > 0 ) {
94  
95 # Build a hash from if/ip current cache
96 foreach my $line (@buffer) {
97 chomp($line);
98 my ( $if, $ip ) = split( '\t', $line );
99 $cache{$if} = $ip;
100 }
101 return $cache{$given_ifc};
102 }
103 else { # Empty buffer
104 return undef;
105 }
106 }
107  
108 sub cache_this_ip {
109 my $this = shift;
110  
111 # Validate parameters tyeps
112 my ( $if, $ip ) =
113 pos_validated_list( \@_, { isa => 'Str' }, { isa => 'LinkLocalAddress' } );
114  
115 # Get cached IPs if exists
116 my %cache = $this->_get_hash_from_cache();
117  
118 # Adding IP to hash, creating or overwriting entry as need be.
119 $cache{$if} = $ip;
120  
121 # Re-position the file-handle to the beginning of the file
122 $this->_refresh_cache( 0, 0 );
123 foreach my $key ( sort ( keys(%cache) ) ) {
124 $this->_record_ip( "%s\t%s\n", $key, $cache{$key} );
125 }
126 }
127  
128 sub _get_hash_from_cache {
129 my $this = shift;
130 my ( @buffer, %cache ) = ();
131  
132 # Slurp the contents of buffer into a file
133 try {
134 $this->_refresh_cache( 0, 0 );
135 @buffer = $this->_slurp_file();
136 }
137 catch {
138 die "Error while slurping cache file: $!\n";
139 };
140  
141 # Build a hash from if/ip current cache
142 foreach my $line (@buffer) {
143 chomp($line);
144 my ( $if, $ip ) = split( '\t', $line );
145 $cache{$if} = $ip;
146 }
147  
148 return %cache;
149 }
150  
151 no Moose;
152  
153 __PACKAGE__->meta->make_immutable;
154 1;
155  
156 =head1 NAME
157  
158 LinkLocal::IPv4::Interface::Cache - IPv4 link-local address caching object.
159  
160 =head1 SYNOPSIS
161  
162 use LinkLocal::IPv4::Interface::Cache;
163  
164 # Create a cache file object
165 my $cache = LinkLocal::IPv4::Interface::Cache->new();
166
167 # Get the last IP cached for a given interface
168 my $last_address = $cache->get_last_ip('eth0');
169
170 # Update the cache when an interface has successfully
171 # configured a link-local address
172 $cache->cache_this_ip( 'eth0', '169.254.100.150' );
173  
174 =head1 DESCRIPTION
175  
176 Implementations of the IPv4 link-local protocol stack will always cache the most recent successfully
177 configured IPv4 link-local addresses in cases where the host has some form of persistant storage. In
178 doing so, the next time that implementation begins the address selection process it will read from its
179 address cache first and attempt to use the last successfully configured address it had previously
180 used on that interface. This has two benefits; the first is that address collision and conflict are
181 less likely during the address selection phase and second is that host interfaces will try and first
182 use link-local addresses which have been successfully configured on them in the past which provides a
183 degree of continuity and address stability in the otherwise unmanaged environment.
184  
185 This caching object wraps an L<IO::File>, the path to which is determined by the OS on which it is
186 run. At present Linux, Solaris, OS X, Windows and the BSD derivatives are supported. Each cache maintains
187 an IPv4 link-local address for each interface which has been successfully configured with one in the past.
188  
189 =head2 ATTRIBUTES
190  
191 =over 4
192  
193 =head3 Public Attributes
194  
195 =item C<N/A>
196  
197 =head3 Private Attributes
198  
199 =item C<_cache>
200  
201 The private _cache attribute is an L<IO::File> object which represents the IPv4 link-local address
202 cache for the current implementation. According to the specification, all past successfully configured
203 link-local addresses are cached on an interface by interface basis.
204  
205 =back
206  
207 =head2 METHODS
208  
209 =over 4
210  
211 =head3 Constructors
212  
213 =item C<new>
214  
215 This constructor takes no arguments and returns an instance of a link-local address cache file
216 object which contains all of the most recent successfully configured link-local addresses, by
217 interface, on the system.
218  
219 =head3 Public Methods
220  
221 =item C<get_last_ip>
222  
223 get_last_ip() is a public method which takes one argument, a network interface device name
224 (such as 'eth0' for example) and it returns the last successfully configured IPv4 link-local address
225 which was configured on that interface or undef if this interface has never been dynamically
226 configured with a link-local address. L<MooseX::Params::Validate> is in effect on all arguments.
227  
228 =item C<cache_this_ip>
229  
230 cache_this_ip() is a public method which takes two arguments, a network interface device name
231 (such as 'eth0' for example) and the link-local IPv4 address in dotted-decimal format which is
232 to be cached for that interface as the currently configured IPv4 link-local address but only after
233 it has been successfully probed and configured as per RFC-3927. L<MooseX::Params::Validate> is in
234 effect on all arguments.
235  
236 =head3 Private Methods
237  
238 =item C<_get_hash_from_cache>
239  
240 _get_hash_from_cache() is a private method which builds a hash from the current link-local cache
241 file containing the most recent successfully configured IPv4 link-local addresses. The hash is
242 structured in an interface => address key/value format. For example:
243
244 eth0 => 169.254.150.120
245 eth1 => 169.254.134.288
246  
247 =item C<_build_cache>
248  
249 This builder method handles the proper initialization of the private L<_cache> attribute, which is
250 iself an L<IO::File> object. It detects the OS on which the implementation is being run and from
251 that, configures the application cache to use the proper location for its environment according
252 to current system type.
253  
254 =item C<_slurp_file>
255  
256 _slurp_file() is a method delegate on the getlines() method of an L<IO::Handle> object via L<IO::File>.
257 When called, _slurp_file() returns a list containing the contents of the cache file
258  
259 =item C<_record_ip>
260  
261 _record_ip() is a method delegate on the printf() method of an L<IO::Handle> object via L<IO::File>.
262 This delegate is used to update the cache with the new interface/address mapping.
263  
264 =item C<_refresh_cache>
265  
266 _refresh_cache() is a method delegate on the seek() method of an L<IO::Seekable> object via L<IO::File>.
267 The _refresh_cache() delegate is used to reset the file pointer so that the stream is at the beginning of
268 the file. This ensures that the next read from the file will have its stream set to the start of the
269 file.
270  
271 =back
272  
273 =head1 SEE ALSO
274  
275 Refer to RFC-3927, I<Dynamic Configuration of IPv4 Link-Local Adresses>, the complete
276 text of which can be found in the top level of the package archive.
277  
278 L<perl>, L<LinkLocal::IPv4::Interface>, L<MooseX::Params::Validate>, L<IO::File>,
279 L<Try::Tiny>, L<Moose>
280  
281 This project is also hosted on github at:
282 git@github.com:raymroz/LinkLocal--IPv4.git
283  
284 =head1 BUGS
285  
286 What's a bug???
287  
288 =head1 AUTHOR
289  
290 Tony Li Xu, E<lt>tonylixu@gmail.comE<gt>
291  
292 =head1 COPYRIGHT AND LICENSE
293  
294 Copyright (C) 2010 Ray Mroz, Tony Li Xu
295  
296 This program is free software: you can redistribute it and/or modify
297 it under the terms of the GNU General Public License as published by
298 the Free Software Foundation, either version 3 of the License, or
299 (at your option) any later version.
300  
301 This program is distributed in the hope that it will be useful,
302 but WITHOUT ANY WARRANTY; without even the implied warranty of
303 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 GNU General Public License for more details.
305  
306 You should have received a copy of the GNU General Public License
307 along with this program. If not, see <http://www.gnu.org/licenses/>.
308  
309  
310 =cut