Subject: standards/11532: a script to keep /etc/protocols up-to-date
To: None <gnats-bugs@gnats.netbsd.org>
From: None <woods@weird.com>
List: netbsd-bugs
Date: 11/20/2000 13:55:15
>Number:         11532
>Category:       standards
>Synopsis:       a script to keep /etc/protocols up-to-date
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    standards-manager
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Mon Nov 20 13:55:00 PST 2000
>Closed-Date:
>Last-Modified:
>Originator:     Greg A. Woods
>Release:        NetBSD-any
>Organization:
Planix, Inc.; Toronto, Ontario; Canada
>Environment:


>Description:

	Here's a little script to keep /etc/protocols up-to-date
	without too much hassle.

>How-To-Repeat:

	wonder why some programs don't always find a name for a
	registered IP protocol number and then try comparing
	/etc/protocols with the canonical source of such information

>Fix:

	Arrange that the following script be installed in /usr/sbin
	and that the most current version form the source tree be used
	to create the new /etc/protocols file at minimum prior to each
	new release.

	This script can probably be kept up-to-date easier than
	manually hacking through the protocol-numbers file, and once
	updated to match the latest exceptions in the IANA file it
	always produces mechanically reproducible and accurate output
	generated directly from the canonical source db.

	Maybe someday the existance of this script will make its way
	back to IANA and they'll repair their input data so that such
	a fancy hand-tuned parser isn't necessary.

#! /bin/sh
#
#ident "@(#)$NetBSD$"
#
#	update-protocols
#
# retrieve the official protocol-numbers from IANA and transform
# it into a unix-style /etc/protocols file on stdout
#
# Note that the IANA file is kinda messy and needs some fancy fixing up!
#
# This script last updated for IANA input hacking on 2000/11/19.
#
# Written 2000/11/19 by Greg A. Woods  <woods@planix.com>
#
# Copyright is hereby assigned to the Public Domain

SOURCE_URL="ftp://ftp.iana.org/in-notes/iana/assignments/protocol-numbers"

cat <<EOF
#	\$NetBSD\$
#
# Internet (IP) protocols -- see protocols(5)
#
# Updated on $(date)
# using $0
# from $SOURCE_URL
#
# name        P# alias(es)    # comment
#----------- --- ------------ ---------
#
EOF

ftp -o - $SOURCE_URL |								\
	expand |								\
	sed -e '1,/^---/d'							\
	    -e '/^REF/,$d'							\
	    -e 's/^   //'							\
	    -e '/^[ 	]*$/d' |						\
	sed -e '/^117/s/Interactive/  Interactive/'				\
	    -e '/^124/s/ISIS over IPv4            /ISIS-IPv4   ISIS over IPv4/' \
	    -e '/^131/s/Private/     Private/'					\
	    -e '/^132/s/Stream/     Stream/'					\
	    -e '/^133/s/Fibre/     Fibre/' |					\
	sed -e 's/^\(....... \)\(........... [ ]*\)/\1:\2:/' |			\
	sort -n |								\
	awk -F: '
function print_entry(number, name, comment)
{
	# traditionally the all-lower version of the official name is used
	# as the official name on Unix and we keep the real official name
	# in preserved case as an alias
	#
	printf("%-12s %3d %-12s # %s\n", tolower(name), number, name, comment);
}
BEGIN {
	unassigned = 134;	# set this to the first unassigned number
	last = 0;
}
{
	# clean up field data
	#
	gsub(/ /, "", $1);
	gsub(/ /, "", $2);
	gsub(/[ ]*$/, "", $3);

	# ignore multi-line comments
	#
	if ($1 == "" && $2 == "") {
		next;
	}

	# fill in the unassigned range
	#
	if ($1 == sprintf("%s-254", unassigned)) {
		for (i = unassigned; i <= 254; i++) {
			$2 = toupper(sprintf("unasgnd-%d", i));
			print_entry(i, $2, $3);
		}
		last = 254;
		next;
	}

	# type co-ersion
	#
	$1 = $1 + 0;

	# extraneous error checking
	#
	if ($1 > 255) {
		printf("#");
		$3 = sprintf("ILLEGAL NUMBER! - %s", $3);
	}

	# fill in any missing numbers
	#
	while ((last + 1) < $1) {
		last = last + 1;
		fakename = toupper(sprintf("unknown-%d", last));
		print_entry(last, fakename, "missing entry");
	}

	# "generic/private" protocols have no official name
	#
	if ($2 == "") {
		$2 = toupper(sprintf("priv-%d", $1));
	}
	if ($1 == 255) {
		$2 = "RSVD-255";
	}
	print_entry($1, $2, $3);

	last = $1 + 0;
}'
>Release-Note:
>Audit-Trail:
>Unformatted: