When profiling we are more interested in real-world performance than memory
[metze/wireshark/wip.git] / tools / make-services.pl
1 #!/usr/bin/perl -w
2 # create the services file from
3 # http://www.iana.org/assignments/enterprise-numbers
4 #
5 # $Id$
6 #
7 # Wireshark - Network traffic analyzer
8 # By Gerald Combs <gerald@wireshark.org>
9 # Copyright 2004 Gerald Combs
10 #
11 # This program is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU General Public License
13 # as published by the Free Software Foundation; either version 2
14 # of the License, or (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 use strict;
25 use English;
26
27 my $svc_file = "services";
28 my $in = shift;
29 my $min_size = 2000000; # Size was 2654612 on 2011-08-31
30 my @exclude_pats = qw(
31         ^spr-itunes
32         ^spl-itunes
33         ^shilp
34 );
35 my $iana_port_url = "http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.txt";
36
37 # As of August 2011, the page linked from http://www.iana.org/protocols/
38 # is XML. Perhaps we should parse that instead.
39 $in = $iana_port_url unless(defined $in);
40
41 my $revision = '$Revision$';
42 if ($revision !~ /[0-9]/ ) { $revision = "unknown"; }
43
44 my $body = "";
45
46 if($in =~ m/^http:/i) {
47         eval "require LWP::UserAgent;";
48         die "LWP isn't installed. It is part of the standard Perl module libwww." if $@;
49
50         my $agent    = LWP::UserAgent->new;
51         $agent->env_proxy;
52         $agent->agent("Wireshark make-services.pl/$revision");
53
54         warn "starting to fetch $in ...\n";
55
56         my $request  = HTTP::Request->new(GET => $in);
57
58
59         if (-f $svc_file) {
60                 my $mtime;
61                 (undef,undef,undef,undef,undef,undef,undef,$min_size,undef,$mtime,undef,undef,undef) = stat($svc_file);
62                 $request->if_modified_since( $mtime );
63         }
64
65         my $result   = $agent->request($request);
66
67         if ($result->code eq 200) {
68                 warn "done fetching $in\n";
69                 my @in_lines = split /\n/, $result->content;
70                 my $prefix = "";
71                 my $exclude_match;
72                 my $line;
73                 my $pat;
74                 foreach $line (@in_lines) {
75                         $prefix = "# ";
76                         $exclude_match = 0;
77
78                         if ($line =~ /^(\S+)\s+(\d+)\s+(tcp|udp|sctp|dccp)\s+(\S.*)/) {
79                                 $line = "$1     $2/$3   # $4";
80
81                                 foreach $pat (@exclude_pats) {
82                                         if ($line =~ $pat) {
83                                                 $exclude_match = 1;
84                                                 last;
85                                         }
86                                 }
87
88                                 if ($exclude_match) {
89                                         $body .= "# Excluded by $PROGRAM_NAME\n";
90                                 } else {
91                                         $prefix = "";
92                                 }
93                         }
94
95                         $line =~ s/^\s+|\s+$//g;
96
97                         $body .= $prefix . $line . "\n";
98                 }
99         } elsif ($result->code eq 304) {
100                 warn "$svc_file was up-to-date\n";
101                 exit 0;
102         } else {
103                 die "request for $in failed with result code:" . $result->code;
104         }
105
106 } else {
107   open IN, "< $in";
108   $body = <IN>;
109   close IN;
110 }
111
112 if (length($body) < $min_size * 0.9) {
113         die "$in doesn't have enough data\n";
114 }
115
116 open OUT, "> $svc_file";
117
118 print OUT <<"_HEADER";
119 # This is a local copy of the IANA port-numbers file.
120 #
121 # \$Id\$
122 #
123 # Wireshark uses it to resolve port numbers into human readable
124 # service names, e.g. TCP port 80 -> http.
125 #
126 # It is subject to copyright and being used with IANA's permission:
127 # http://www.wireshark.org/lists/wireshark-dev/200708/msg00160.html
128 #
129 # The original file can be found at:
130 # $iana_port_url
131 #
132 $body
133 _HEADER
134
135 close OUT;