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.


In reply to Dotless IP address problem by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.