Giter VIP home page Giter VIP logo

aggregator's Introduction

Join the chat at https://gitter.im/CIDRAM/Lobby PHP >= 5.4.0 License: GPL v2 PRs Welcome

Aggregator.

A stand-alone class implementation of the IPv4+IPv6 IP+CIDR aggregator from CIDRAM.


How to install:

Installing the Aggregator is exceptionally easy. You can download the necessary files directly from the src directory, and then copy them to any projects that need it, or, if you'd prefer, you can install it using Composer:

composer require cidram/aggregator

Note: The code in this class is based upon code in the CIDRAM package, but the two are NOT dependent on each other.

After you've downloaded the file, to allow your projects to use the class, PSR-4 autoloading is preferred (particularly if you're using a large number of different, unrelated classes). If you're installing the class via Composer, all you have to do is require_once 'vendor/autoload.php'; and everything will be taken care of. Alternatively, if you're installing it manually (or without Composer), and don't want to use a PSR-4 autoloader, you can simply require or include the class into your projects (which may be much easier in many cases) by including the respective statement to point to the class file in the relevant PHP files.


How to use:

The simplest way to use Aggregator is to create a new instance of the class and enter some data to be aggregated as a parameter to the aggregate method. The aggregate method will return an aggregate of the entered data.

Example:

<?php
use \CIDRAM\Aggregator\Aggregator;

$Aggregator = new Aggregator();
$Output = $Aggregator->aggregate($Input);

Or, if the file helpers.php is loaded, this function will be available:

$Output = aggregate($Input);

Note: The function aggregate will be available if you installed the package via composer, but otherwise, you'll need to include the file src/helpers.php.

In the case of the above example, if this is entered as $Input:

127.0.0.1 Some arbitrary single IPs from here
127.0.0.2
127.0.0.3
1::
1::1
1:2:3:4::
1:2:3:4::1
1:2:3:4::2
1:2:3:4::3
2002::1
127.0.0.4
127.0.0.5
257.0.0.999 Some arbitrary INVALID single IPs from here
555.666.777.888
2002:abcd:efgh::1
10.0.0.0/9 Some arbitrary CIDRs from here
10.128.0.0/9
10.192.0.0/10
11.128.0.0/10
11.192.0.0/10
12.0.0.0/9
12.128.0.0/9
13.0.0.0/9
13.128.0.0/9
192.168.0.0/8 Some arbitrary INVALID CIDRs from here
192.168.0.0/9
192.168.0.0/10
192.168.192.0/10
192.169.0.0/10
192.169.64.0/10
1.2.3.4/255.255.255.254 Some arbitrary netmasks from here
2.3.4.5/255.255.255.255
99.99.99.99/255.255.255.255
99.10.10.0/255.255.255.0
99.10.11.0/255.255.255.0
99.8.0.0/255.252.0.0
11.11.11.11/11.11.11.11 Some arbitrary INVALID netmasks from here
255.255.255.254/1.2.3.4
6.7.8.9/255.255.255.254
88.88.88.88/255.255.254.255
Foobar Some garbage data from here
ASDFQWER!@#$
>>HelloWorld<<
SDFSDFSDF
QWEQWEQWE

$Output will be expected to contain this:

1.2.3.4/31
2.3.4.5/32
10.0.0.0/8
11.128.0.0/9
12.0.0.0/7
99.8.0.0/14
99.99.99.99/32
127.0.0.1/32
127.0.0.2/31
127.0.0.4/31
1::/127
1:2:3:4::/126
2002::1/128

Data is newline-delimited and each line represents one item to be aggregated. Aggregator handles IPv4+IPv6 seamlessly, attempts to clean up each item (i.e., remove invalid and superfluous data in order to reduce to a valid IP, CIDR, or netmask), attempts to aggregate the resultant cleaned up data (unreadable and invalid data is rejected), and then returns the resultant aggregated data.

It is possible to obtain more information about each aggregation operation if desired. If "Results" is set to true (it is false by default), then "NumberEntered" (the total number of lines entered when an operation begins), "NumberRejected" (the number of lines or items "rejected", i.e., perceived as invalid, or unreadable; note that this number will also also duplicate items, due to that duplicates are stripped along with invalid and superfluous data prior to aggregation), "NumberAccepted" (the number of lines or items accepted for aggregation; i.e., NumberAccepted = NumberEntered - NumberRejected), "NumberMerged" (the total number of items aggregated or merged), and "NumberReturned" (the total number of items returned at the end of an operation) will be populated during operation accordingly. These values can be retrieved after each operation from the class instance or object:

<?php
use \CIDRAM\Aggregator\Aggregator;

$Aggregator = new Aggregator();
$Aggregator->Results = true;
$Output = $Aggregator->aggregate($Input);
echo $Output;
echo "\n\n";
echo $Aggregator->NumberEntered . "\n";
echo $Aggregator->NumberRejected . "\n";
echo $Aggregator->NumberAccepted . "\n";
echo $Aggregator->NumberMerged . "\n";
echo $Aggregator->NumberReturned . "\n";

Generally, it is better to create a new class instance for each aggregation operation. However, if you want to recycle an old instance, and want to continue to retrieve these values after each operation, you can reset these values to their initial state between operations by using the "resetNumbers" method:

$Aggregator->resetNumbers();

Regardless of whether "Results" is true or false, after each aggregation operation, "ProcessingTime" will be available, in case you want to know how much was consumed during the operation. This could be useful both for debugging and for general vanity purposes.

Example:

<?php
use \CIDRAM\Aggregator\Aggregator;

$Aggregator = new Aggregator();
$Output = $Aggregator->aggregate($Input);
echo $Aggregator->ProcessingTime . "\n";

Additionally, "ExpandIPv4" and "ExpandIPv6" public methods are provided with the class, and they function in exactly the same way their CIDRAM package closure counterparts. Calling either of these with an IPv4 or IPv6 IP address respectively will return an array containing the potential factors for the given IP address. The potential factors are all possible subnets (or CIDRs) that the given IP address is a member of. When a valid IP address is supplied, "ExpandIPv4" and "ExpandIPv6" and should return an array with 32 and 128 elements respectively.

If you want Aggregator to return results as netmasks instead of CIDRs, you can instantiate the object with a parameter value of 1, like this:

<?php
use \CIDRAM\Aggregator\Aggregator;

$Aggregator = new Aggregator(1);
$Output = $Aggregator->aggregate($Input);

In the case of the example input mentioned earlier, the output should look something like this:

1.2.3.4/255.255.255.254
2.3.4.5/255.255.255.255
10.0.0.0/255.0.0.0
11.128.0.0/255.128.0.0
12.0.0.0/254.0.0.0
99.8.0.0/255.252.0.0
99.99.99.99/255.255.255.255
127.0.0.1/255.255.255.255
127.0.0.2/255.255.255.254
127.0.0.4/255.255.255.254
1::/ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe:0
1:2:3:4::/ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffc:0
2002::1/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:0

Other information:

Licensing:

Licensed as GNU General Public License version 2.0 (GPLv2).

For support:

Please use the issues page of this repository.


Last Updated: 18 May 2022 (2022.05.18).

aggregator's People

Contributors

maikuolan avatar masnathan avatar tarlepp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

aggregator's Issues

Support for netmasks.

Possible future feature: Adding support for netmasks to the Aggregator (currently it just supports IPs and CIDRs).

This has been requested by several different people now, both privately and publicly, so I'm pretty sure this is something that others would find useful. Most recently requested via PHPClasses.org:

I think it's a good idea, but it's currently a low-priority thing for me (got too many other things to do for too many other projects already). I've got a reasonably good idea of how I could go about doing it though, if and when it happens. Definitely not an impossible thing to do.

If anyone else wanted to give it a whirl, they're welcome to it. Otherwise, I should be able to make a start on it at some point next month or thereafter, probably.

Creating this issue for reference and labeling as "attention required".

NumberEntered missing

Hello,

When running results with Aggregator it appears that NumberEntered is always 0.

When I simply hack it up with the following I have returned numbers:

    private function stripInvalidCharactersAndSort(&$In)
    {
        $In = explode("\n", strtolower(trim(str_replace("\r", '', $In))));
        $InCount = count($In);
        $this->NumberEntered = $InCount;
        if (isset($this->callbacks['newParse']) && is_callable($this->callbacks['newParse'])) {
            $this->callbacks['newParse']($InCount);
        }
        unset($InCount);

Not sure if this is the best place or not, or if better before in the previous function on the intake?

Thanks!

Performance issue on stripInvalidRangesAndSubs ?

Hi there,

I'm wondering if there's any known issue with performance when trying to feed in 100k prefixes? When I enable some extra output on the classes/functions I find that it seems to drag along at stripInvalidRangesAndSubs($this->Output), specifically the $Line area I think.

Any idea?

Feature: passing an array to $input

Hi again, very handy stuff you have, thanks!

I'm wondering if it would be possible to have the $input figure out if it's a simple pass of plain data or an array. To give you some context we have a program that writes out a flat file with a series of prefixes of varying lengths, both v4/v6. Right now when we pass the data we do it this way:

 if (($addresses = file($routefile)) == FALSE)
  {
    status(STATUS_WARNING, "Unable to open route file (might be empty), skipping.");
    return FALSE;
  }

  // First normalize the routes, no array.
  $addresses_normal = implode("", $addresses);

  // Process the aggregation
  $addresses_agg = aggregate($addresses_normal);

  // Return them in an array
  return explode(" ", $addresses_agg);

I'm wondering if it's possible to add the ability to detect if an array is being passed and do the magic within the Input?

Hacktoberfest 2017

Hacktoberfest by Digital Ocean will be running again this year through the month of October 2017. If anyone sees any room for improvement to the codebase, has ideas, suggestions, bug-fixes, or whatever else, if you want to win a free Hacktoberfest t-shirt and some stickers, consider signing up for this year and sending some pull requests to this repository. :-)

For more details: https://hacktoberfest.digitalocean.com/#details

Issue will remain open until the end of October.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.