| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # lookup::protocol Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Lookup::Protocol; | ||
| 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 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 => 'protocol-numbers-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(protocol_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/protocol-numbers/protocol-numbers-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) | ||
| 62 | or return $self->log->error("update: read failed"); | ||
| 63 | |||
| 64 | # Some lines are split on multi-lines, we put into a single line | ||
| 65 | # for each record. | ||
| 66 | my @new = split(/\r\n/, $text); | ||
| 67 | for (@new) { | ||
| 68 | s/\n/ /g; | ||
| 69 | } | ||
| 70 | |||
| 71 | $ft->write(\@new, $output); | ||
| 72 | |||
| 73 | return $output; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub load { | ||
| 77 | my $self = shift; | ||
| 78 | my ($input) = @_; | ||
| 79 | |||
| 80 | $input ||= $self->datadir.'/'.$self->input; | ||
| 81 | $self->brik_help_run_file_not_found('load', $input) or return; | ||
| 82 | |||
| 83 | my $data = $self->read($input) or return; | ||
| 84 | |||
| 85 | return $self->_load($data); | ||
| 86 | } | ||
| 87 | |||
| 88 | sub from_dec { | ||
| 89 | my $self = shift; | ||
| 90 | my ($dec) = @_; | ||
| 91 | |||
| 92 | $self->brik_help_run_undef_arg('from_dec', $dec) or return; | ||
| 93 | |||
| 94 | my $data = $self->_load || $self->load; | ||
| 95 | if (! defined($data)) { | ||
| 96 | return $self->log->error("from_dec: load failed"); | ||
| 97 | } | ||
| 98 | |||
| 99 | for my $this (@$data) { | ||
| 100 | if ($this->{Decimal} == $dec) { | ||
| 101 | return $this->{Keyword}; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | # No match | ||
| 106 | return 'undef'; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub from_hex { | ||
| 110 | my $self = shift; | ||
| 111 | my ($hex) = @_; | ||
| 112 | |||
| 113 | $self->brik_help_run_undef_arg('from_hex', $hex) or return; | ||
| 114 | |||
| 115 | my $dec = hex($hex); | ||
| 116 | |||
| 117 | return $self->from_dec($dec); | ||
| 118 | } | ||
| 119 | |||
| 120 | sub from_string { | ||
| 121 | my $self = shift; | ||
| 122 | my ($string) = @_; | ||
| 123 | |||
| 124 | $self->brik_help_run_undef_arg('from_string', $string) or return; | ||
| 125 | |||
| 126 | my $data = $self->_load || $self->load; | ||
| 127 | if (! defined($data)) { | ||
| 128 | return $self->log->error("from_string: load failed"); | ||
| 129 | } | ||
| 130 | |||
| 131 | my @match = (); | ||
| 132 | for my $this (@$data) { | ||
| 133 | if ($this->{Keyword} =~ /$string/i) { | ||
| 134 | $self->log->verbose("from_string: match with [".$this->{Keyword}."]"); | ||
| 135 | push @match, $this->{Decimal}; | ||
| 136 | } | ||
| 137 | elsif ($this->{Protocol} =~ /$string/i) { | ||
| 138 | $self->log->verbose("from_string: match with [".$this->{Protocol}."]"); | ||
| 139 | push @match, $this->{Decimal}; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | return \@match; | ||
| 144 | } | ||
| 145 | |||
| 146 | 1; | ||
| 147 | |||
| 148 | __END__ | ||