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

    Not sure what the problem is, but you can simplify dotless_to_ip() significantly ...

    sub dotless_to_ip { return inet_ntoa( inet_aton( shift ) ); }

        --k.


Re: Dotless IP address problem
by dragonchild (Archbishop) on Oct 19, 2001 at 00:18 UTC
    The problem is that you're using the fact that integers "flip over" the sign barrier at high numbers. However, there's one more negative number than positive number (due to the fact that 2's complement is a negative vs. non-negative representation system).

    The solution is to add a $dotless++ before the loop to decode it. That gives the right answer.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: Dotless IP address problem
by clintp (Curate) on Oct 19, 2001 at 00:38 UTC
    Just following the formulas on the web page:
    sub ip_to_dotless { return 0+ eval " 0x" . sprintf("%02x%02x%02x%02x", split(/\./, + shift)); } sub dotless_to_ip { my $hex=reverse sprintf("%x", shift); my @ip; for(0..3) { push(@ip, 0+ eval "0x" . reverse substr($hex, 0, 2, "" +)); } join('.', reverse @ip); }
    This seems to work nicely.
      A slight twist which works for your two examples anyway.
      sub dotless_to_ip { #use integer; my $dotless = shift; my $index = 3; my @ip = (); while ( @ip < 4 ) { my $temp = 256 ** $index--; push @ip, int($dotless / $temp) ; $dotless -= $ip[ -1 ] * $temp; } return join '.', @ip; }
      I guess one could attack the problem using binary shifts (>>) and bitwise and's as well.
Fore!
by Fletch (Bishop) on Oct 19, 2001 at 07:18 UTC
    $ perl -le 'print join(".",unpack("C4",pack("N",shift)))' 3232235777 192.168.1.1 $ perl -le 'print unpack("N",pack("C4",split(/\./,shift)))' 192.168.1. +1 3232235777
      Nice, but too many parens....
      % perl -le 'print join".",unpack"C4",pack"N",pop' 3232235777 192.168.1.1 % perl -le 'print unpack"N",pack"C4",split/\./,pop' 192.168.1.1 + 3232235777
      Update: 34 chars each... (though split\D seems to only work in 5.503 not 5.6.0)
      % perl -le 'print unpack N,pack C4,split\D,pop' 192.168.1.1 + 3221225472 % perl -le 'print join".",unpack C4,pack N,pop' 3232235777 192.168.1.1

      -Blake

Re: Dotless IP address problem
by clintp (Curate) on Oct 19, 2001 at 00:17 UTC
    Looks like you've got some sign problems there. 127.0.0.1 doesn't convert properly and looks suspiciously like overflow.
      Actually, anything under 128.x.x.x doesn't convert properly. (It either doesn't convert out properly or convert back properly. Haven't figured out which.)

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.