[28433] in Source-Commits

home help back first fref pref prev next nref lref last post

netparams commit: Initial checkin of netparams

daemon@ATHENA.MIT.EDU (Jonathan D Reed)
Wed Sep 17 12:57:48 2014

Date: Wed, 17 Sep 2014 12:57:40 -0400
From: Jonathan D Reed <jdreed@mit.edu>
Message-Id: <201409171657.s8HGverP016236@drugstore.mit.edu>
To: source-commits@mit.edu

https://github.com/mit-athena/netparams/commit/634c80f4712f6365c7b524da3c542c7f354a3dba
commit 634c80f4712f6365c7b524da3c542c7f354a3dba
Author: Jonathan Reed <jdreed@mit.edu>
Date:   Wed Sep 17 12:56:38 2014 -0400

    Initial checkin of netparams
    
    Move netparams from the installer to an actual package, which can
    server as the canonical location.  Update netparams to look in
    more modern locations for the default masks file and the local masks
    file.  Clean up some stray whitespace in masks file.

 debian/changelog     |    5 +
 debian/compat        |    1 +
 debian/control       |   14 +++
 debian/copyright     |   20 ++++
 debian/install       |    2 +
 debian/rules         |   10 ++
 debian/source/format |    1 +
 masks                |   90 +++++++++++++++++
 netparams            |  266 ++++++++++++++++++++++++++++++++++++++++++++++++++
 9 files changed, 409 insertions(+), 0 deletions(-)

diff --git a/debian/changelog b/debian/changelog
new file mode 100644
index 0000000..24732b0
--- /dev/null
+++ b/debian/changelog
@@ -0,0 +1,5 @@
+debathena-netparams (1.0) unstable; urgency=low
+
+  * Initial release
+
+ -- Jonathan Reed <jdreed@mit.edu>  Wed, 17 Sep 2014 09:27:37 -0400
diff --git a/debian/compat b/debian/compat
new file mode 100644
index 0000000..7f8f011
--- /dev/null
+++ b/debian/compat
@@ -0,0 +1 @@
+7
diff --git a/debian/control b/debian/control
new file mode 100644
index 0000000..ff29088
--- /dev/null
+++ b/debian/control
@@ -0,0 +1,14 @@
+Source: debathena-netparams
+Section: debathena/net
+Priority: optional
+Maintainer: Jonathan Reed <jdreed@mit.edu>
+Build-Depends: debhelper (>= 7.0.50~)
+Standards-Version: 3.9.4
+
+Package: debathena-netparams
+Architecture: all
+Depends: ${misc:Depends}
+Description: Generate network parameters from IP address
+ This package generats network parameters (e.g. netmask, gateway) from
+ an IP address, given a file which loosely describes the network
+ layout.  This is probably only useful to sites with a /8.
diff --git a/debian/copyright b/debian/copyright
new file mode 100644
index 0000000..137a407
--- /dev/null
+++ b/debian/copyright
@@ -0,0 +1,20 @@
+Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
+
+Files: *
+Copyright: 2014 Massachusetts Institute of Technology
+License: GPL-2+
+ This package 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 package 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, see <http://www.gnu.org/licenses/>
+ .
+ On Debian systems, the complete text of the GNU General
+ Public License version 2 can be found in "/usr/share/common-licenses/GPL-2".
diff --git a/debian/install b/debian/install
new file mode 100644
index 0000000..2f255dd
--- /dev/null
+++ b/debian/install
@@ -0,0 +1,2 @@
+netparams usr/lib/debathena-netparams
+masks usr/share/debathena-netparams
diff --git a/debian/rules b/debian/rules
new file mode 100755
index 0000000..a6efa99
--- /dev/null
+++ b/debian/rules
@@ -0,0 +1,10 @@
+#!/usr/bin/make -f
+# -*- makefile -*-
+
+%:
+	dh $@
+
+# We need to build explcitly with gzip, because in an include_command,
+# udpkg can only uncompress gzip, not xzip.  Because d-i.
+override_dh_builddeb:
+	dh_builddeb -- -Zgzip
diff --git a/debian/source/format b/debian/source/format
new file mode 100644
index 0000000..89ae9db
--- /dev/null
+++ b/debian/source/format
@@ -0,0 +1 @@
+3.0 (native)
diff --git a/masks b/masks
new file mode 100644
index 0000000..43a021f
--- /dev/null
+++ b/masks
@@ -0,0 +1,90 @@
+# $Id: masks,v 1.7 2004/03/20 11:38:17 zacheiss Exp $
+
+# This file specifies the netmasks for each network supported by the
+# Athena environment.  The format for lines in the file is:
+#
+#	address		significant-bits	netmask-bits	[gateway]
+#
+# The number of netmask bits for a given IP address is the value of
+# <netmask-bits> on the first line for which <address> matches the
+# first <significant-bits> bits of the given address.  If a gateway
+# is not given, the gateway is assumed to be one greater than the
+# network address.
+#
+# The contents of this file are maintained by Athena release
+# engineering.  If you edit this file on your workstation, it will be
+# replaced at the next update and your changes will be lost.  If you
+# have local changes to make, create or add to
+# /etc/athena/masks.local, which has the same file format as this
+# file.  Entries in /etc/athena/masks.local take precedence over
+# entries in this file.
+#
+# The shell script /etc/athena/netparams uses this file to compute the
+# network, broadcast, and gateway address for a given IP address.  See
+# the comments at the top of that file for usage information.
+
+# Most MITnet addresses begin with "18" and have a 16-bit netmask.
+# 18.7 is being used in IS machine rooms with a 24-bit netmask.
+# At least parts of 18.7.1 are being used as /30s for servers that
+# don't like to have their IP address changed.
+# Bits of LCS also have a 24-bit netmask, according to
+# wollman@lcs.mit.edu, 1/12/98.
+# 18.101 is being used for IS internal experiments, with a 28 bit netmask.
+18.7.1.0	24	30
+18.7.0.0	16	24
+18.24.0.0	16	24
+18.26.0.0	16	24
+18.101.0.0	16	28
+18.0.0.0	8	16
+
+# WHOI has a class B subnetted to 22 bits.  128.128.16 has a funny
+# gateway address.
+128.128.16.0	22	22	128.128.16.7
+128.128.0.0	16	22
+
+# More AI Lab configuration, from foley, 1/24/03
+128.52.29.0	24	24	128.52.29.10
+# AI lab configuration, from bruce@ai.mit.edu, 1/12/98
+128.52.32.0	24	24	128.52.32.10
+128.52.37.0	24	24	128.52.37.10
+128.52.38.0	24	24	128.52.38.10
+128.52.39.0	24	24	128.52.39.10
+128.52.54.0	24	24	128.52.54.10
+
+# Stata Center configuration, see http://tig.csail.mit.edu/stata-net.html
+128.30.0.0	25	25
+128.30.0.192	27	27
+128.30.0.244	30	30
+128.30.0.248	30	30
+128.30.0.252	30	30
+128.30.2.0	24	24
+128.30.3.0	24	24
+128.30.4.0	23	23
+128.30.6.0	23	23
+128.30.16.0	20	24
+128.30.32.0	22	22
+128.30.36.0	24	24
+128.30.40.0	22	22
+128.30.44.0	24	24
+128.30.48.0	22	22
+128.30.52.0	22	22
+128.30.56.0	22	22
+128.30.60.0	24	24
+128.30.64.0	22	22
+128.30.72.0	22	22
+128.30.76.0	22	22
+128.30.80.0	22	22
+128.30.84.0	24	24
+128.30.86.0	24	24
+128.30.88.0	22	22
+128.30.92.0	24	24
+128.30.96.0	22	22
+128.30.100.0	24	24
+128.30.104.0	22	22
+128.30.108.0	24	24
+128.31.0.0	24	24
+128.31.1.0	24	24
+128.31.4.0	22	22
+128.31.8.0	22	22
+128.31.16.0	24	24
+192.12.11.0	24	24
diff --git a/netparams b/netparams
new file mode 100755
index 0000000..e8c850b
--- /dev/null
+++ b/netparams
@@ -0,0 +1,266 @@
+#!/bin/sh
+# $Id: netparams.sh,v 1.2 1998/05/17 03:25:03 ghudson Exp $
+
+# Usage: netparams [-d defaultbits] [-e] [-f file] address ...
+#
+# Computes the netmask for each address and outputs the netmask,
+# network, broadcast address, and gateway address.  Options are:
+#
+#	-d defaultbits	Give the default number of bits to use if an
+#			IP address is not located on the file.  The
+#			default is 24.
+#
+#	-e		Error if an IP address is not located in the
+#			file.
+#
+#	-f file		Give the masks filename to use.  The default
+#			is the concatenation of /etc/athena/masks.local
+#			(if it exists) and /etc/athena/masks.
+#
+# The format of each line of the masks file is:
+#
+#	address		significant-bits	mask-bits	[gateway]
+#
+# The first <significant-bits> of <address> are compared against the
+# given IP address.  If they match, <mask-bits> is the number of bits
+# in the netmask, and <gateway> is the gateway address if given.  (If
+# gateway is not given, it is assumed to be one greater than the
+# network address.) The first match in the file is taken.  Blank lines
+# and lines beginning with a '#' are ignored.
+
+# This script may want to run before /usr is mounted on BSD-ish
+# systems, so it uses a very limited set of external commands (expr,
+# test, echo).  If you want to use a command not in that set, make
+# sure it lives in /bin or /sbin in BSD 4.4.
+
+# Regular expression to match things that looklike IP addresss.
+ipreg='[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$'
+
+# Usage message
+usage="$0 [-f file] [-d defaultbits] [-e] address ..."
+
+# octet <ip address> sets o1..o4 to the octets of the address.
+octets() {
+	OLDIFS="$IFS"
+	IFS=.
+	set -- $1
+	o1=$1 o2=$2 o3=$3 o4=$4
+	IFS="$OLDIFS"
+}
+
+# octetbits <n> splits <n> up into four values b1..b4 each between 0
+# and 8.  If you want the first n bits of a 32-bit value, then you
+# want the first b1..b4 bits of the corresponding octets.
+octetbits() {
+	case "$1" in
+	 0) b1=0; b2=0; b3=0; b4=0 ;;
+	 1) b1=1; b2=0; b3=0; b4=0 ;;
+	 2) b1=2; b2=0; b3=0; b4=0 ;;
+	 3) b1=3; b2=0; b3=0; b4=0 ;;
+	 4) b1=4; b2=0; b3=0; b4=0 ;;
+	 5) b1=5; b2=0; b3=0; b4=0 ;;
+	 6) b1=6; b2=0; b3=0; b4=0 ;;
+	 7) b1=7; b2=0; b3=0; b4=0 ;;
+	 8) b1=8; b2=0; b3=0; b4=0 ;;
+	 9) b1=8; b2=1; b3=0; b4=0 ;;
+	10) b1=8; b2=2; b3=0; b4=0 ;;
+	11) b1=8; b2=3; b3=0; b4=0 ;;
+	12) b1=8; b2=4; b3=0; b4=0 ;;
+	13) b1=8; b2=5; b3=0; b4=0 ;;
+	14) b1=8; b2=6; b3=0; b4=0 ;;
+	15) b1=8; b2=7; b3=0; b4=0 ;;
+	16) b1=8; b2=8; b3=0; b4=0 ;;
+	17) b1=8; b2=8; b3=1; b4=0 ;;
+	18) b1=8; b2=8; b3=2; b4=0 ;;
+	19) b1=8; b2=8; b3=3; b4=0 ;;
+	20) b1=8; b2=8; b3=4; b4=0 ;;
+	21) b1=8; b2=8; b3=5; b4=0 ;;
+	22) b1=8; b2=8; b3=6; b4=0 ;;
+	23) b1=8; b2=8; b3=7; b4=0 ;;
+	24) b1=8; b2=8; b3=8; b4=0 ;;
+	25) b1=8; b2=8; b3=8; b4=1 ;;
+	26) b1=8; b2=8; b3=8; b4=2 ;;
+	27) b1=8; b2=8; b3=8; b4=3 ;;
+	28) b1=8; b2=8; b3=8; b4=4 ;;
+	29) b1=8; b2=8; b3=8; b4=5 ;;
+	30) b1=8; b2=8; b3=8; b4=6 ;;
+	31) b1=8; b2=8; b3=8; b4=7 ;;
+	32) b1=8; b2=8; b3=8; b4=8 ;;
+	esac
+}
+
+# mask8 <o> <b> sets a8 to the first <b> bits of <o>.
+mask8() {
+	case "$2" in
+	0) a8=0 ;;
+	1) a8=`expr "$1" - "$1" % 128` ;;
+	2) a8=`expr "$1" - "$1" % 64` ;;
+	3) a8=`expr "$1" - "$1" % 32` ;;
+	4) a8=`expr "$1" - "$1" % 16` ;;
+	5) a8=`expr "$1" - "$1" % 8` ;;
+	6) a8=`expr "$1" - "$1" % 4` ;;
+	7) a8=`expr "$1" - "$1" % 2` ;;
+	8) a8=$1 ;;
+	esac
+}
+
+# fill8 <o> <b> sets a8 to <o> with the last 8-<b> bits set to 1.
+fill8() {
+	case "$2" in
+	0) a8=255 ;;
+	1) a8=`expr "$1" - "$1" % 128 + 127` ;;
+	2) a8=`expr "$1" - "$1" % 64 + 63` ;;
+	3) a8=`expr "$1" - "$1" % 32 + 31` ;;
+	4) a8=`expr "$1" - "$1" % 16 + 15` ;;
+	5) a8=`expr "$1" - "$1" % 8 + 7` ;;
+	6) a8=`expr "$1" - "$1" % 4 + 3` ;;
+	7) a8=`expr "$1" - "$1" % 2 + 1` ;;
+	8) a8=$1 ;;
+	esac
+}
+
+# mask <ip address> <n> sets a to the first <n> bits of <ip address>.
+# If a third parameter is given, add 1 to the last octet (to get the
+# gateway address).
+mask() {
+	a=""
+	octets "$1"
+	octetbits "$2"
+	mask8 "$o1" "$b1"
+	a=$a8
+	mask8 "$o2" "$b2"
+	a=$a.$a8
+	mask8 "$o3" "$b3"
+	a=$a.$a8
+	mask8 "$o4" "$b4"
+	if [ -n "$3" ]; then
+		a8=`expr "$a8" + 1`
+	fi
+	a=$a.$a8
+}
+
+# fill <ip address> <n> sets a to <ip address> with the last 32-<n> bits
+# set to 1.
+fill() {
+	a=""
+	octets "$1"
+	octetbits "$2"
+	fill8 "$o1" "$b1"
+	a=$a8
+	fill8 "$o2" "$b2"
+	a=$a.$a8
+	fill8 "$o3" "$b3"
+	a=$a.$a8
+	fill8 "$o4" "$b4"
+	a=$a.$a8
+}
+
+# getnetmask <ip address> <file> sets maskbits to the number of netmask
+# bits for <ip address> according to <file>.  Also sets gateway to the
+# gateway field on the matching line, if one was specified.
+getnetmask() {
+	exec < $2
+	maskbits=""
+	while read addr sigbits mbits gw; do
+		# Ignore blank lines and comments.
+		if [ -z "$addr" -o `expr "$addr" : '#'` -ne 0 ]; then
+			continue
+		fi
+
+		# Make sure the line is in the proper form.
+		if [ `expr "$addr" : "$ipreg"` -eq 0 \
+		     -o `expr "$sigbits" : '[0-9][0-9]*$'` -eq 0 \
+		     -o `expr "$mbits" : '[0-9][0-9]*$'` -eq 0 ]; then
+			echo "Bad line in $2: $addr $sigbits $mbits $gw" 1>&2
+			exit 1
+		fi
+		if [ -n "$gw" -a `expr "$gw" : "$ipreg"` -eq 0 ]; then
+			echo "Bad gateway in $2: $gw" 1>&2
+			exit 1
+		fi
+
+		mask "$1" "$sigbits"
+		if [ "$a" = "$addr" ]; then
+			maskbits=$mbits
+			gateway=$gw
+			break
+		fi
+	done
+}
+
+# Process arguments.
+defaultbits=24
+errornf=false
+maskfile="/usr/share/debathena-netparams/masks"
+localmaskfile="/etc/netparams/masks.local"
+while getopts d:ef: opt; do
+	case "$opt" in
+	d)
+		defaultbits=$OPTARG
+		;;
+	e)
+		errornf=true
+		;;
+	f)
+		maskfile=$OPTARG
+		# If you pass -f, local masks file is not consulted
+		localmaskfile=""
+		;;
+	\?)
+		echo "$usage"
+		exit 1
+		;;
+	esac
+done
+shift `expr $OPTIND - 1`
+
+# Make sure we have some addresses.
+if [ $# -eq 0 ]; then
+	echo "Usage: $0 ip-address ..." 1>&2
+	exit 1
+fi
+
+# Make sure the masks file exists
+if ! [ -e "$maskfile" ]; then
+	echo "$maskfile does not exist" 1>&2
+	exit 1
+fi
+
+# Make sure our addresses are in something like the proper format.
+for ip in "$@"; do
+	if [ `expr "$ip" : "$ipreg"` -eq 0 ]; then
+		echo "Bad address: $ip" 1>&2
+		echo "Usage: $0 ip-address ..." 1>&2
+		exit 1
+	fi
+done
+
+for ip in "$@"; do
+	if [ -n "$localmaskfile" ] && [ -e "$localmaskfile" ]; then
+		getnetmask "$ip" "$localmaskfile"
+	fi
+	if [ -z "$maskbits" ]; then
+		getnetmask "$ip" "$maskfile"
+	fi
+	if [ -z "$maskbits" ]; then
+		# Error out if we were told to, or guess.
+		if [ "$errornf" = true ]; then
+			echo "$ip not matched." 1>&2
+			exit 1
+		fi
+		maskbits=$defaultbits
+	fi
+
+	mask 255.255.255.255 "$maskbits"
+	netmask=$a
+	mask "$ip" "$maskbits"
+	network=$a
+	fill "$ip" "$maskbits"
+	broadcast=$a
+	if [ -z "$gateway" ]; then
+		mask "$ip" "$maskbits" 1
+		gateway=$a
+	fi
+
+	echo "$netmask $network $broadcast $gateway $maskbits"
+done

home help back first fref pref prev next nref lref last post