Lookup/Ipoption.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Lookup/Iplocation.pm ↑ Summary Lookup/Oui.pm »
1 #
2 # $Id$
3 #
4 # lookup::ipoption Brik
5 #
6 package Metabrik::Lookup::Ipoption;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik::File::Csv);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable ip option iana) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 datadir => [ qw(datadir) ],
20 input => [ qw(input) ],
21 _load => [ qw(INTERNAL) ],
22 },
23 attributes_default => {
24 separator => ',',
25 input => 'ip-parameters-1.csv',
26 },
27 commands => {
28 update => [ qw(output|OPTIONAL) ],
29 load => [ qw(input|OPTIONAL) ],
30 from_dec => [ qw(dec_number) ],
31 from_hex => [ qw(hex_number) ],
32 from_string => [ qw(ip_option_string) ],
33 },
34 require_modules => {
35 'Metabrik::Client::Www' => [ ],
36 'Metabrik::File::Text' => [ ],
37 },
38 };
39 }
40  
41 sub update {
42 my $self = shift;
43 my ($output) = @_;
44  
45 my $url = 'http://www.iana.org/assignments/ip-parameters/ip-parameters-1.csv';
46 my ($file) = $self->input;
47  
48 my $datadir = $self->datadir;
49 $output ||= $datadir.'/'.$file;
50  
51 my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
52 my $files = $cw->mirror($url, $file, $datadir) or return;
53 if (@$files == 0) { # Nothing new
54 return $output;
55 }
56  
57 # We have to rewrite the CSV file, cause some entries are multiline.
58 my $ft = Metabrik::File::Text->new_from_brik_init($self) or return;
59 $ft->overwrite(1);
60 $ft->append(0);
61 my $text = $ft->read($output) or return;
62  
63 # Some lines are split on multi-lines, we put into a single line
64 # for each record.
65 my @new = split(/\r\n/, $text);
66 for (@new) {
67 s/\n/ /g;
68 }
69  
70 $ft->write(\@new, $output);
71  
72 return $output;
73 }
74  
75 sub load {
76 my $self = shift;
77 my ($input) = @_;
78  
79 $input ||= $self->datadir.'/'.$self->input;
80 $self->brik_help_run_file_not_found('load', $input) or return;
81  
82 my $data = $self->read($input) or return;
83  
84 # Copy,Class,Number,Value,Name,Reference
85 # 0,0,0,0,EOOL - End of Options List,[RFC791][Jon_Postel]
86  
87 return $self->_load($data);
88 }
89  
90 sub from_dec {
91 my $self = shift;
92 my ($dec) = @_;
93  
94 $self->brik_help_run_undef_arg('from_dec', $dec) or return;
95  
96 my $data = $self->_load || $self->load;
97 if (! defined($data)) {
98 return $self->log->error("from_dec: load failed");
99 }
100  
101 for my $this (@$data) {
102 if ($this->{'Value'} == $dec) {
103 return $this->{'Name'};
104 }
105 }
106  
107 # No match
108 return 'undef';
109 }
110  
111 sub from_hex {
112 my $self = shift;
113 my ($hex) = @_;
114  
115 $self->brik_help_run_undef_arg('from_hex', $hex) or return;
116  
117 my $dec = hex($hex);
118  
119 return $self->from_dec($dec);
120 }
121  
122 sub from_string {
123 my $self = shift;
124 my ($string) = @_;
125  
126 $self->brik_help_run_undef_arg('from_string', $string) or return;
127  
128 my $data = $self->_load || $self->load;
129 if (! defined($data)) {
130 return $self->log->error("from_string: load failed");
131 }
132  
133 # Copy,Class,Number,Value,Name,Reference
134 # 0,0,0,0,EOOL - End of Options List,[RFC791][Jon_Postel]
135  
136 my @match = ();
137 for my $this (@$data) {
138 next unless length($this->{'Name'});
139 my $name = $this->{'Name'};
140 if ($name =~ /$string/i) {
141 $self->log->verbose("from_string: match with [$name]");
142 push @match, $this->{'Value'};
143 }
144 }
145  
146 return \@match;
147 }
148  
149 1;
150  
151 __END__
« Lookup/Iplocation.pm ↑ Summary Lookup/Oui.pm »