arc_of_descent has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks

I would like comments from the community about a module I would like to implement.

Name: Net::IP::Correct

Description: Breakdown an incorrect range of IP addresses into valid IP Ranges

Rationale: This module will exist as a subclass of Net::IP. It would basically accept a range of IP addresses (Net::IP object or a literal IP range) , and if the range is incorrect, it would break it down into seperate correct IP ranges. Each IP range returned would be a Net::IP object.

I think Net::IP::Correct is a good enough namespace, although there is also Net::IP::Range::Correct, which is more meaningful. I'm open to suggestions.

For example: given the range 192.168.1.0 - 192.168.1.5, this module would break this range down into:


Update:
Thanks to the suggestion from Smylers, I propose to name this module as Net::IP::Range::Split
Update:
Thanks to the info from merlyn and OverlordQ, Net::IP's find_prefixes does get the work done. I don't think we require Net::IP::Range::Split :-(

Replies are listed 'Best First'.
Re: RFC: CPAN module Net::IP::Correct
by merlyn (Sage) on Jun 29, 2005 at 12:00 UTC

      I guess I should be using the word block instead of range. 192.168.1.0 - 192.168.1.5 is fundamentally an incorrect IP block which if used in many utilities (iptables for example; on Linux), would raise an error (or not, depending on the utility). But the fact remains, that it is an incorrect IP block.

      The aim of this module is to take an IP range, and split the range into correct IP blocks starting from the largest possible, to the smallest block possible.

      So if we have a range like 192.168.1.0 - 192.168.2.130 for example, this module would split this range into the following valid IP blocks:

      • 192.168.1.0/255.255.255.0 (192.168.1.0 - 192.168.1.255)
      • 192.168.2.0/255.255.255.128 (192.168.2.0 - 192.168.2.127)
      • 192.168.2.128/255.255.255.254 (192.168.2.128 - 192.168.2.129)
      • 192.168.2.130/255.255.255.255 (192.168.2.130)
      I hope I'm clear enough :-)
        Net::IP Already does this: Quick-N-Dirty copy of a Oneliner I got aliased:
        use Net::IP; $i=new Net::IP("$ARGV[0]-$ARGV[1]"); @l=$i->find_prefixes(); foreach(@l){ ($i,$m)=split("/"); $i=new Net::IP($i); print$i->short()."/$m\n"; }
        Pass it: 192.168.1.0 192.168.2.130
        Output:
        192.168.1.0/24 192.168.2.0/25 192.168.2.128/31 192.168.2.130/32
        Yes it requires a 'bit' of coding, and IMO would be better suited to go into Net::IP instead of a new module.

        BIG HUGE DISCLAIMER (for Dio): THIS CODE SUCKS AND PROBABLY WILL BREAK

Re: RFC: CPAN module Net::IP::Correct
by Smylers (Pilgrim) on Jun 29, 2005 at 13:13 UTC

    I think having "correct" as part of the name is pretty meaningless — nobody purposefully creates a Cpan module that's incorrect.

    How about just Net::IP::Range? Or Net::IP::Range::Split? Definitely "range" needs to be in there somewhere since that's what the module deals with; merely calling it Net::IP::Correct makes it sound like it's an alternative to Net::IP but that is "correct" (and thereby implies that Net::IP is in some way incorrect).

    Smylers

Re: RFC: CPAN module Net::IP::Correct
by ambrus (Abbot) on Jun 29, 2005 at 12:41 UTC

    For example: given the range 192.168.1.0 - 192.168.1.5, this module would break this range down into:

    192.168.1.0/255.255.255.252
    192.168.1.4/255.255.255.255
    192.168.1.5/255.255.255.255

    Shouldn't that be instead just

    192.168.1.0/255.255.255.252
    192.168.1.4/255.255.255.254

Re: RFC: CPAN module Net::IP::Correct
by fmerges (Chaplain) on Jun 29, 2005 at 11:54 UTC

    Hi

    At this time, I don't find any use of this module, please can you put an example-use. Thanks.

    In my opinion I would choose Net::IP::Range::Check or Correct as you said, and add the module the functionality of not only give you a valid range, also to check the range you would supply them. Only ideas ;-)

    Regards.

    Update: ok, now I understand what is the purpose and from where comes the name. Split sound good, since SUBNET(ting) is a method where you split a network in subnets. I'm thinking about the interface it'll have. Weird!

    I'm looking for a module-idea for contributing to CPAN too. Hehe ;-)

      update: complete misread the question
Re: RFC: CPAN module Net::IP::Correct
by del (Pilgrim) on Jun 29, 2005 at 16:37 UTC
    Consider this a vote of encouragement.

    I would certainly welcome and appreciate such a module. Minimizing the number of routing entries required in configurations, describing address spaces to other parties, or even network documentation come to mind as useful applications of what you propose.

    (How about Net::IP::Range::Minimize?)

      Thanks for your comments and suggestions!

      But isn't splitting an IP range into its constituent valid IP blocks actually maximising the number of routing entries? (assuming this exercise is for a route table).

      I do think Net::IP::Range::Split (thanks to Smylers again) is appropriate for what this module will try to achive. i.e. split an IP range into its constituent valid IP blocks. The caller could use arguments to indicate whether she wants IP blocks smallest first or largest first.

      Module implementation aside, I'm now confused as how to go ahead with CPAN. I did send a request to register a namespace using PAUSE, but I was informed by various good folks that its not a needed step. Do I need a certain number of votes here on Perlmonks to get the go ahead?

      All help will be much appreciated!

        But isn't splitting an IP range into its constituent valid IP blocks actually maximising the number of routing entries? (assuming this exercise is for a route table).

        I was actually thinking in terms of minimal and usable. Your initial example included the range 192.168.1.0 - 192.168.1.5. That range cannot be expressed as a single entity using CIDR or network masks, as you allude to, so it can't be used as a routing entry.

        One approach I frequently see is this:

        192.168.0.0 255.255.255.0
        192.168.1.0 255.255.255.0
        192.168.2.0 255.255.255.0
        192.168.3.0 255.255.255.0
        192.168.4.0 255.255.255.0
        192.168.5.0 255.255.255.0
        

        With your proposed module, I was imagining that same range could be programmatically rewritten as this:

        192.168.0.0 255.255.252.0  ( /22 )
        192.168.4.0 255.255.254.0  ( /23 )
        

        In these scenarios, I see this as a way of reducing the number of entries in a routing table.

        Module implementation aside, I'm now confused as how to go ahead with CPAN. I did send a request to register a namespace using PAUSE, but I was informed by various good folks that its not a needed step. Do I need a certain number of votes here on Perlmonks to get the go ahead?

        A monk vote shouldn't be needed. I think you were on the right track with PAUSE, as described here.

        Module implementation aside, I'm now confused as how to go ahead with CPAN. I did send a request to register a namespace using PAUSE, but I was informed by various good folks that its not a needed step. Do I need a certain number of votes here on Perlmonks to get the go ahead?
        Please don't, until you explain how it isn't actually the same as Net::IP::ip_range_to_prefix. We don't need a whole module to do what's already done as a subroutine in another module, unless there's something very broken about that implementation.

        Then again, if you just want to go pollute the CPAN, I'm really not the one to cast stones. {grin}

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.