** (process:23326): WARNING **: Field 'Reserved bit(s)' does not have an
[obnox/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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24 use strict;
25 use English;
26
27 my $svc_file = "services";
28 my $in = shift;
29 my $min_size = 800000; # Size was 833397 on 2010-10-04
30 my @exclude_pats = qw(
31         ^spr-itunes
32         ^spl-itunes
33         ^shilp
34 );
35
36 $in = "http://www.iana.org/assignments/port-numbers" unless(defined $in);
37
38 my $body = "";
39
40 if($in =~ m/^http:/i) {
41         eval "require LWP::UserAgent;";
42         die "LWP isn't installed. It is part of the standard Perl module libwww." if $@;
43
44         my $agent    = LWP::UserAgent->new;
45
46         warn "starting to fetch $in ...\n";
47
48         my $request  = HTTP::Request->new(GET => $in);
49
50
51         if (-f $svc_file) {
52                 my $mtime;
53                 (undef,undef,undef,undef,undef,undef,undef,$min_size,undef,$mtime,undef,undef,undef) = stat($svc_file);
54                 $request->if_modified_since( $mtime );
55         }
56
57         my $result   = $agent->request($request);
58
59         if ($result->code eq 200) {
60                 warn "done fetching $in\n";
61                 my @in_lines = split /\n/, $result->content;
62                 my $prefix = "";
63                 my $exclude_match;
64                 my $line;
65                 my $pat;
66                 foreach $line (@in_lines) {
67                         chomp($line);
68                         $exclude_match = 0;
69                         foreach $pat (@exclude_pats) {
70                                 if ($line =~ $pat) {
71                                         $exclude_match = 1;
72                                         last;
73                                 }
74                         }
75                         if ($exclude_match) {
76                                 if ($prefix eq "") {
77                                         $body .= "# Excluded by $PROGRAM_NAME\n";
78                                 }
79                                 $prefix = "# ";
80                         } else {
81                                 $prefix = "";
82                         }
83                         
84                         $body .= $prefix . $line . "\n";
85                 }
86         } elsif ($result->code eq 304) {
87                 warn "$svc_file was up-to-date\n";
88                 exit 0;
89         } else {
90                 die "request for $in failed with result code:" . $result->code;
91         }
92
93 } else {
94   open IN, "< $in";
95   $body = <IN>;
96   close IN;
97 }
98
99 if (length($body) < $min_size * 0.9) {
100         die "$in doesn't have enough data\n";
101 }
102
103 open OUT, "> $svc_file";
104
105 print OUT <<"_SMINMPEC";
106 # This is a local copy of the IANA port-numbers file.
107 #
108 # \$Id\$
109 #
110 # Wireshark uses it to resolve port numbers into human readable
111 # service names, e.g. TCP port 80 -> http.
112 #
113 # It is subject to copyright and being used with IANA's permission:
114 # http://www.wireshark.org/lists/wireshark-dev/200708/msg00160.html
115 #
116 # The original file can be found at:
117 # http://www.iana.org/assignments/port-numbers
118 #
119 $body
120 _SMINMPEC
121
122 close OUT;