line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# $Id$ |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# lookup::service Brik |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
package Metabrik::Lookup::Service; |
7
|
1
|
|
|
1
|
|
775
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
29
|
|
8
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
27
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
5
|
use base qw(Metabrik::File::Csv); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
890
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub brik_properties { |
13
|
|
|
|
|
|
|
return { |
14
|
0
|
|
|
0
|
1
|
|
revision => '$Revision$', |
15
|
|
|
|
|
|
|
tags => [ qw(unstable iana) ], |
16
|
|
|
|
|
|
|
author => 'GomoR ', |
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
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
44
|
0
|
|
|
|
|
|
my ($output) = @_; |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
my $url = 'http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.csv'; |
47
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
my $input = $self->input; |
49
|
0
|
|
|
|
|
|
my $datadir = $self->datadir; |
50
|
0
|
|
0
|
|
|
|
$output ||= $input; |
51
|
|
|
|
|
|
|
|
52
|
0
|
0
|
|
|
|
|
my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return; |
53
|
0
|
0
|
|
|
|
|
my $files = $cw->mirror($url, "$output.gz", $datadir) or return; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# If files were modified, we uncompress and save |
56
|
0
|
0
|
|
|
|
|
if (@$files > 0) { |
57
|
0
|
0
|
|
|
|
|
my $fc = Metabrik::File::Compress->new_from_brik_init($self) or return; |
58
|
0
|
0
|
|
|
|
|
$fc->uncompress($datadir."/$output.gz", $output, $datadir) or return; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# We have to rewrite the CSV file, cause some entries are multiline. |
61
|
0
|
0
|
|
|
|
|
my $ft = Metabrik::File::Text->new_from_brik_init($self) or return; |
62
|
0
|
|
|
|
|
|
$ft->overwrite(1); |
63
|
0
|
|
|
|
|
|
$ft->append(0); |
64
|
0
|
0
|
|
|
|
|
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
|
0
|
|
|
|
|
|
my @new = split(/\r\n/, $text); |
69
|
0
|
|
|
|
|
|
for (@new) { |
70
|
0
|
|
|
|
|
|
s/\n/ /g; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
$ft->write(\@new, $datadir.'/'.$output); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
return $datadir.'/'.$output; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub load { |
80
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
81
|
0
|
|
|
|
|
|
my ($input) = @_; |
82
|
|
|
|
|
|
|
|
83
|
0
|
|
0
|
|
|
|
$input ||= $self->datadir.'/'.$self->input; |
84
|
0
|
0
|
|
|
|
|
$self->brik_help_run_file_not_found('load', $input) or return; |
85
|
|
|
|
|
|
|
|
86
|
0
|
0
|
|
|
|
|
my $data = $self->read($input) or return; |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
return $self->_load($data); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub from_dec { |
92
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
93
|
0
|
|
|
|
|
|
my ($dec) = @_; |
94
|
|
|
|
|
|
|
|
95
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('from_dec', $dec) or return; |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
0
|
|
|
|
my $data = $self->_load || $self->load; |
98
|
0
|
0
|
|
|
|
|
if (! defined($data)) { |
99
|
0
|
|
|
|
|
|
return $self->log->error("from_dec: load failed"); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
for my $this (@$data) { |
103
|
0
|
0
|
|
|
|
|
if ($this->{'Port Number'} == $dec) { |
104
|
0
|
|
|
|
|
|
return $this->{'Service Name'}; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# No match |
109
|
0
|
|
|
|
|
|
return 'undef'; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub from_hex { |
113
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
114
|
0
|
|
|
|
|
|
my ($hex) = @_; |
115
|
|
|
|
|
|
|
|
116
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('from_hex', $hex) or return; |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
my $dec = hex($hex); |
119
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
return $self->from_dec($dec); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub from_string { |
124
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
125
|
0
|
|
|
|
|
|
my ($string) = @_; |
126
|
|
|
|
|
|
|
|
127
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('from_string', $string) or return; |
128
|
|
|
|
|
|
|
|
129
|
0
|
|
0
|
|
|
|
my $data = $self->_load || $self->load; |
130
|
0
|
0
|
|
|
|
|
if (! defined($data)) { |
131
|
0
|
|
|
|
|
|
return $self->log->error("from_string: load failed"); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
0
|
|
|
|
|
|
my @match = (); |
135
|
0
|
|
|
|
|
|
for my $this (@$data) { |
136
|
0
|
0
|
|
|
|
|
next unless length($this->{'Port Number'}); |
137
|
0
|
|
|
|
|
|
my $service = $this->{'Service Name'}; |
138
|
0
|
0
|
|
|
|
|
if ($service =~ /$string/i) { |
139
|
0
|
|
|
|
|
|
$self->log->verbose("from_string: match with [$service]"); |
140
|
0
|
|
|
|
|
|
push @match, $this->{'Port Number'}.'/'.$this->{'Transport Protocol'}; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
0
|
|
|
|
|
|
return \@match; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
1; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
__END__ |