Date: Sun, 17 Nov 2002 08:16:20 +0000 From: Matthew Seaman <m.seaman@infracaninophile.co.uk> To: budsz <budsz@kumprang.or.id> Cc: freebsd-questions@freebsd.org Subject: Re: question on IP alias/broadcast Message-ID: <20021117081620.GA19147@happy-idiot-talk.infracaninophi> In-Reply-To: <20021117070457.GB45577@kumprang.or.id> 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>
next in thread | previous in thread | raw e-mail | index | archive | help
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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20021117081620.GA19147>