From 1784620bb965628f05fac6659b8bca7e9d821ea9 Mon Sep 17 00:00:00 2001 From: hjp Date: Wed, 1 Dec 2004 17:12:04 +0000 Subject: [PATCH] Checked in minnet. Doesn't really have anything to do with DNS, but who cares? --- dns/GNUmakefile | 1 + dns/minnet | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100755 dns/minnet diff --git a/dns/GNUmakefile b/dns/GNUmakefile index 8997acc..9ab40c2 100644 --- a/dns/GNUmakefile +++ b/dns/GNUmakefile @@ -5,6 +5,7 @@ install: \ $(BINDIR)/gethostbyname \ $(BINDIR)/axfr \ $(BINDIR)/fqdn \ + $(BINDIR)/minnet \ clean: diff --git a/dns/minnet b/dns/minnet new file mode 100755 index 0000000..5d71255 --- /dev/null +++ b/dns/minnet @@ -0,0 +1,48 @@ +#!/usr/bin/perl +use warnings; +use strict; + +my $min = 0xFFFF_FFFF; +my $max = 0x0000_0000; + +for (@ARGV) { + if (/(\d+)\.(\d+)\.(\d+)\.(\d+)/) { + my $adr = $1 * 0x100_0000 + $2 * 0x1_0000 + $3 * 0x100 + $4; + $max = $adr if ($adr > $max); + $min = $adr if ($adr < $min); + } +} + +my $diff = $min ^ $max; + +# printf "min = %08x max = %08x diff = %08x\n", $min, $max, $diff; + +my $netmaskbits = 32; +my $netmask = 0xFFFF_FFFF; + +while ($diff > 0) { + $netmaskbits--; + $diff >>= 1; + $netmask <<= 1; +} + +my $net = $min & $netmask; +my $bcast = $min | ~$netmask; + +printf "net = %s/%d (%s/%s) bcast = %s\n", + dottedquad($net), + $netmaskbits, + dottedquad($net), + dottedquad($netmask), + dottedquad($bcast); + +sub dottedquad { + my ($adr) = @_; + return sprintf "%d.%d.%d.%d", + ($adr >> 24) & 0xFF, + ($adr >> 16) & 0xFF, + ($adr >> 8) & 0xFF, + ($adr >> 0) & 0xFF; +} + +exit(0);