Ovid has asked for the wisdom of the Perl Monks concerning the following question:
I was reading about the security problems with IE and dotless IP addresses and decided to write a little conversion script for dotless ips (have I just reinvented the wheel?). My script converts to the dotless IP just fine, but when it converts back, the last octet is one lower than it should be. What did I do wrong?
#!D:/perl/bin/perl.exe -w use strict; use Socket; my $domain = shift or die "Usage: $0 [domain]\n[domain] should *not* i +nclude the protocol (e.g. http://)"; my $address = inet_ntoa( inet_aton( $domain ) ); print "Original ip: $address\n"; my $dotless = ip_to_dotless( $address ); print "Dotless ip: $dotless\n"; my $orig_address = dotless_to_ip( $dotless ); print "Original ip: $orig_address"; sub ip_to_dotless { my $ip = shift; my $index = 3; my $dotless = 0; $dotless += $_ * ( 256 ** $index-- ) foreach ( split /\./, $ip ); return $dotless; } sub dotless_to_ip { use integer; my $dotless = shift; my $index = 3; my @ip = (); while ( @ip < 4 ) { my $temp = 256 ** $index--; push @ip, $dotless / $temp; $dotless -= $ip[ -1 ] * $temp; } @ip = map { $_ + 255 } @ip; return join '.', @ip; }
You can read about dotless IPs here if you're not familiar with this.
My output is as follows:
C:\>test.pl www.yahoo.com Original ip: 216.115.102.78 Dotless ip: 3631441486 Original ip: 216.115.102.77 C:\>test.pl www.perlmonks.org Original ip: 206.170.14.76 Dotless ip: 3467251276 Original ip: 206.170.14.75
Cheers,
Ovid
Vote for paco!
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Dotless IP address problem
by Kanji (Parson) on Oct 19, 2001 at 00:04 UTC | |
|
Re: Dotless IP address problem
by dragonchild (Archbishop) on Oct 19, 2001 at 00:18 UTC | |
|
Re: Dotless IP address problem
by clintp (Curate) on Oct 19, 2001 at 00:38 UTC | |
by guha (Priest) on Oct 19, 2001 at 00:46 UTC | |
|
Fore!
by Fletch (Bishop) on Oct 19, 2001 at 07:18 UTC | |
|
Re: Dotless IP address problem
by clintp (Curate) on Oct 19, 2001 at 00:17 UTC | |
by dragonchild (Archbishop) on Oct 19, 2001 at 00:20 UTC |