From owner-freebsd-questions Sun Nov 17 0:16:42 2002 Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id BFECF37B401 for ; Sun, 17 Nov 2002 00:16:39 -0800 (PST) Received: from smtp.infracaninophile.co.uk (ns0.infracaninophile.co.uk [81.2.69.218]) by mx1.FreeBSD.org (Postfix) with ESMTP id 8CA3343E75 for ; Sun, 17 Nov 2002 00:16:37 -0800 (PST) (envelope-from m.seaman@infracaninophile.co.uk) Received: from happy-idiot-talk.infracaninophile.co.uk (localhost.infracaninophile.co.uk [IPv6:::1]) by smtp.infracaninophile.co.uk (8.12.6/8.12.6) with ESMTP id gAH8GSx2019402; Sun, 17 Nov 2002 08:16:28 GMT (envelope-from matthew@happy-idiot-talk.infracaninophile.co.uk) Received: (from matthew@localhost) by happy-idiot-talk.infracaninophile.co.uk (8.12.6/8.12.6/Submit) id gAH8GKGX019401; Sun, 17 Nov 2002 08:16:20 GMT Date: Sun, 17 Nov 2002 08:16:20 +0000 From: Matthew Seaman To: budsz Cc: freebsd-questions@freebsd.org Subject: Re: question on IP alias/broadcast Message-ID: <20021117081620.GA19147@happy-idiot-talk.infracaninophi> Mail-Followup-To: Matthew Seaman , budsz , freebsd-questions@freebsd.org References: <058f01c28d76$22296230$020aa8c0@morpheous> <5.2.0.9.2.20021116082511.00b26508@molson.wixb.com> <20021116162134.GB12726@happy-idiot-talk.infracaninophi> <20021117070457.GB45577@kumprang.or.id> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20021117070457.GB45577@kumprang.or.id> User-Agent: Mutt/1.5.1i X-Spam-Status: No, hits=-3.3 required=5.0 tests=IN_REP_TO,QUOTED_EMAIL_TEXT,REFERENCES,SPAM_PHRASE_02_03, TO_LOCALPART_EQ_REAL,USER_AGENT,USER_AGENT_MUTT version=2.43 Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Sun, Nov 17, 2002 at 02:04:57PM +0700, budsz wrote: > On Sat, Nov 16, 2002 at 04:21:34PM +0000, Matthew Seaman wrote: > >expressed as a hexadecimal or even decimal integer. thus: > > > > 192.168.100.1 is the same as 0xc0a86401 or 3232261121 > > ^^^^^^^^^^ > >and > > > > 0xffffff00 is the same as 255.255.255.0 or 4294967040 > ^^^^^^^^^^ > > Sorry sir, How we calculate that number (Decimal Interger)?, I hope > explaination step by step? Simple enough: This perl snippet will convert a dotted quad address into an integer: # in the spirit of inet_aton(3) convert an address given as a dotted # quad into an integer sub inet_atoi ($) { my $ipaddr = shift; my @bytes; @bytes = ( $ipaddr =~ /^(25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9][0-9]|[0-9])\. (25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9][0-9]|[0-9])\. (25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9][0-9]|[0-9])\. (25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9][0-9]|[0-9])$/x ); $ipaddr = 0; for (@bytes) { $ipaddr <<= 8; $ipaddr += $_; } return $ipaddr; } There's a standard version of inet_aton() provided by the Socket module (perldoc Socket), but that returns a packed string rather than an integer value. These three will print it out an integer in whatever appropriate format: # in the spirit of inet_ntoa(3) convert a network address expressed as # an integer into the typical dotted-quad representation. sub inet_itoa ($) { my $ipaddr = shift; my @bytes; for ( 1 .. 4 ) { unshift @bytes, $ipaddr & 0xff; $ipaddr >>= 8; } return sprintf "%u.%u.%u.%u", @bytes; } # Display address as hex string sub inet_itox ($) { return sprintf "%#x", $_[0]; } # Display address as decimal integer sub inet_itod ($) { return sprintf "%u", $_[0]; } If you're not into perl programming, I wrote a little CGI or PHP network calculator which you can slap into a website: http://www.infracaninophile.co.uk/nwc/networkcalc-1.0.tar.bz2 Cheers, Matthew -- Dr Matthew J Seaman MA, D.Phil. 26 The Paddocks Savill Way Marlow Tel: +44 1628 476614 Bucks., SL7 1TH UK To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message