Lookup/Service.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/Protocol.pm ↑ Summary Lookup/Tcpoption.pm »
1 #
2 # $Id$
3 #
4 # lookup::service Brik
5 #
6 package Metabrik::Lookup::Service;
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 => 'service-names-port-numbers.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(service_string) ],
33 },
34 require_modules => {
35 'Metabrik::Client::Www' => [ ],
36 'Metabrik::File::Compress' => [ ],
37 'Metabrik::File::Text' => [ ],
38 },
39 };
40 }
41  
42 sub update {
43 my $self = shift;
44 my ($output) = @_;
45  
46 my $url = 'http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.csv';
47  
48 my $input = $self->input;
49 my $datadir = $self->datadir;
50 $output ||= $input;
51  
52 my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
53 my $files = $cw->mirror($url, "$output.gz", $datadir) or return;
54  
55 # If files were modified, we uncompress and save
56 if (@$files > 0) {
57 my $fc = Metabrik::File::Compress->new_from_brik_init($self) or return;
58 $fc->uncompress($datadir."/$output.gz", $output, $datadir) or return;
59  
60 # We have to rewrite the CSV file, cause some entries are multiline.
61 my $ft = Metabrik::File::Text->new_from_brik_init($self) or return;
62 $ft->overwrite(1);
63 $ft->append(0);
64 my $text = $ft->read($datadir.'/'.$output) or return;
65  
66 # Some lines are split on multi-lines, we put into a single line
67 # for each record.
68 my @new = split(/\r\n/, $text);
69 for (@new) {
70 s/\n/ /g;
71 }
72  
73 $ft->write(\@new, $datadir.'/'.$output);
74 }
75  
76 return $datadir.'/'.$output;
77 }
78  
79 sub load {
80 my $self = shift;
81 my ($input) = @_;
82  
83 $input ||= $self->datadir.'/'.$self->input;
84 $self->brik_help_run_file_not_found('load', $input) or return;
85  
86 my $data = $self->read($input) or return;
87  
88 return $self->_load($data);
89 }
90  
91 sub from_dec {
92 my $self = shift;
93 my ($dec) = @_;
94  
95 $self->brik_help_run_undef_arg('from_dec', $dec) or return;
96  
97 my $data = $self->_load || $self->load;
98 if (! defined($data)) {
99 return $self->log->error("from_dec: load failed");
100 }
101  
102 for my $this (@$data) {
103 if ($this->{'Port Number'} == $dec) {
104 return $this->{'Service Name'};
105 }
106 }
107  
108 # No match
109 return 'undef';
110 }
111  
112 sub from_hex {
113 my $self = shift;
114 my ($hex) = @_;
115  
116 $self->brik_help_run_undef_arg('from_hex', $hex) or return;
117  
118 my $dec = hex($hex);
119  
120 return $self->from_dec($dec);
121 }
122  
123 sub from_string {
124 my $self = shift;
125 my ($string) = @_;
126  
127 $self->brik_help_run_undef_arg('from_string', $string) or return;
128  
129 my $data = $self->_load || $self->load;
130 if (! defined($data)) {
131 return $self->log->error("from_string: load failed");
132 }
133  
134 my @match = ();
135 for my $this (@$data) {
136 next unless length($this->{'Port Number'});
137 my $service = $this->{'Service Name'};
138 if ($service =~ /$string/i) {
139 $self->log->verbose("from_string: match with [$service]");
140 push @match, $this->{'Port Number'}.'/'.$this->{'Transport Protocol'};
141 }
142 }
143  
144 return \@match;
145 }
146  
147 1;
148  
149 __END__
« Lookup/Protocol.pm ↑ Summary Lookup/Tcpoption.pm »