That looks a whole lot harder than it has to be. For starters, I'm assuming that there's a reason you want to do this yourself, rather than just using one of the CPAN libraries, like NetAddr::IP.

If that's the case, the trick is just remembering that in the end, an IPv4 address or mask is really just a 32 bit int. Just use a little bit of shifting and splitting to roll your own inet_aton and inet_ntoa if you don't want to use the ones in Socket

sub my_inet_aton { my ($string) = @_; my ($a, $b, $c, $d) = split(/\./, $string); return ( ($a << 24) + ($b << 16) + ($c << 8) + ($d) ); } sub my_inet_ntoa { my ($ip) = @_; return ( (($ip & 0xFF000000) >> 24) . '.' . (($ip & 0xFF0000) >> 16) . '.' . (($ip & 0xFF00) >> 8) . '.' . ($ip & 0xFF) ); }

Then just use a bitwise AND between the mask and IP address to extract the subnet portion.

my $ipstring = '192.168.190.128'; my $maskstring = '255.255.255.0'; my $ip = my_inet_aton($ipstring); my $mask = my_inet_aton($maskstring); my $subnet = $ip & $mask; print "Subnet portion of ", $ipstring, "/", $maskstring, " is ", my_inet_ntoa($subnet), "\n";

The basic logic is the same for IPv6 addresses. However, a) parsing them is a little tricker with things like :: compact form, and b) they're 128 bits, so not all systems will be able to directly do bit masks on them. If there's any chance you need to handle v6 addresses, you're much better off using one of the pre-existing CPAN modules that will handles those cases for you automatically.


In reply to Re: calculate subnet help by fs
in thread calculate subnet help by Anonymous Monk

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.