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