make-services.pl 3.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#!/usr/bin/perl -w
# create the services file from
# http://www.iana.org/assignments/enterprise-numbers
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
# Copyright 2004 Gerald Combs
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# 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 26

my $svc_file = "services";
my $in = shift;
27
my $min_size = 2000000; # Size was 2654612 on 2011-08-31
28 29 30 31 32
my @exclude_pats = qw(
	^spr-itunes
	^spl-itunes
	^shilp
);
33
my $iana_port_url = "http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.txt";
34

35 36 37
# As of August 2011, the page linked from http://www.iana.org/protocols/
# is XML. Perhaps we should parse that instead.
$in = $iana_port_url unless(defined $in);
38 39 40 41 42 43 44 45

my $body = "";

if($in =~ m/^http:/i) {
	eval "require LWP::UserAgent;";
	die "LWP isn't installed. It is part of the standard Perl module libwww." if $@;

	my $agent    = LWP::UserAgent->new;
46
	$agent->env_proxy;
47
	$agent->agent("Wireshark make-services.pl");
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

	warn "starting to fetch $in ...\n";

	my $request  = HTTP::Request->new(GET => $in);


	if (-f $svc_file) {
		my $mtime;
		(undef,undef,undef,undef,undef,undef,undef,$min_size,undef,$mtime,undef,undef,undef) = stat($svc_file);
		$request->if_modified_since( $mtime );
	}

	my $result   = $agent->request($request);

	if ($result->code eq 200) {
		warn "done fetching $in\n";
64 65 66 67 68 69
		my @in_lines = split /\n/, $result->content;
		my $prefix = "";
		my $exclude_match;
		my $line;
		my $pat;
		foreach $line (@in_lines) {
70
			$prefix = "# ";
71
			$exclude_match = 0;
72 73 74 75 76 77 78 79 80

			if ($line =~ /^(\S+)\s+(\d+)\s+(tcp|udp|sctp|dccp)\s+(\S.*)/) {
				$line = "$1	$2/$3	# $4";

				foreach $pat (@exclude_pats) {
					if ($line =~ $pat) {
						$exclude_match = 1;
						last;
					}
81
				}
82 83

				if ($exclude_match) {
84
					$body .= "# Excluded by $PROGRAM_NAME\n";
85 86
				} else {
					$prefix = "";
87 88
				}
			}
89 90 91

			$line =~ s/^\s+|\s+$//g;

92 93
			$body .= $prefix . $line . "\n";
		}
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	} elsif ($result->code eq 304) {
		warn "$svc_file was up-to-date\n";
		exit 0;
	} else {
		die "request for $in failed with result code:" . $result->code;
	}

} else {
  open IN, "< $in";
  $body = <IN>;
  close IN;
}

if (length($body) < $min_size * 0.9) {
	die "$in doesn't have enough data\n";
}

open OUT, "> $svc_file";

113
print OUT <<"_HEADER";
114 115 116 117 118 119 120 121 122
# This is a local copy of the IANA port-numbers file.
#
# Wireshark uses it to resolve port numbers into human readable
# service names, e.g. TCP port 80 -> http.
#
# It is subject to copyright and being used with IANA's permission:
# http://www.wireshark.org/lists/wireshark-dev/200708/msg00160.html
#
# The original file can be found at:
123
# $iana_port_url
124 125
#
$body
126
_HEADER
127 128

close OUT;