Giter Site home page Giter Site logo

markrogoyski / ipv4-subnet-calculator-php Goto Github PK

View Code? Open in Web Editor NEW
161.0 11.0 44.0 120 KB

Network calculator for subnet mask and other classless (CIDR) network information.

License: MIT License

PHP 99.48% Makefile 0.52%
ipv4 ipv4-address ipv4-calculator ipv4-subnetting ipv4-network cidr cidr-subnet ip-address ip network

ipv4-subnet-calculator-php's People

Contributors

jfillmor avatar markrogoyski avatar rafihm avatar tomothumb 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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  avatar  avatar  avatar  avatar  avatar

ipv4-subnet-calculator-php's Issues

Extra

Your SubnetCalculator works as is (thanks) but your class creates two properties for network size. You have property declaration for $network. Then, instead of using $network property you create another $network_size property in constructor. The $network_size property is then used throughout your class. $network and $network_size property represents the same value. I changed $network to $network_size in the copy I'm using.

What code do i need for only 1 IP address (with 255.255.255.255 subnet mask)

Hello

I use your product to prevent my Website traffic counter for counting traffic for some IP ranges:

	$range[$idx ++] = "xxx.yyy.0.0/18";
    $range[$idx ++] = "xxx.yyy.32.0/19";
    $range[$idx ++] = "xxx.yyy.96.0/19";
    $range[$idx ++] = "xxx.yyy.128.0/18";
    $range[$idx ++] = "xxx.yyy.128.0/18";
    $range[$idx ++] = "xxx.yyy.192.0/18";
    $range[$idx ++] = "xxx.yyy.128.0/18";
    $range[$idx ++] = "xxx.yyy.228.0/22";
    $range[$idx ++] = "xxx.yyy.224.0/19";
    $range[$idx ++] = "xxx.yyy.48.0/20";
    $range[$idx ++] = "xxx.yyy.208.0/21";
    $range[$idx ++] = "xxx.yyy.120.0/21";
    $range[$idx ++] = "xxx.yyy.112.0/20";
    $range[$idx ++] = "xxx.yyy.108.0/22";
    $range[$idx ++] = "xxx.yyy.108.0/22";
    $range[$idx ++] = "xxx.yyy.121.0/24";

    // One fix (static) IP address
    $range[$idx ++] = "xxx.yyy.150.136/255.255.255.255";

But the last line does not work. Are entries for 1 IP addesss like x.x.x.x/255.255.255.255 not possible with your product?

I get that error:


Fatal error: Uncaught UnexpectedValueException: Network size 55 not valid. in C:\Apache24\htdocs\testing\ipv4-subnet-calculator\src\SubnetCalculator.php:705 Stack trace: #0 C:\Apache24\htdocs\testing\ipv4-subnet-calculator\src\SubnetCalculator.php(57): IPv4\SubnetCalculator->validateInputs('xxx.yyy.150.136', '55') #1 C:\Apache24\htdocs\testing\IPTest.php(62): IPv4\SubnetCalculator->__construct('xxx.yyy.150.136', '55') #2 C:\Apache24\htdocs\testing\IPTest.php(83): IPv4\SubnetCalculatorFactory::createInstance('xxx.yyy.150.136', '55') #3 C:\Apache24\htdocs\testing\IPTest.php(96): IPv4\checkIPAddressIsInRange() #4 C:\Apache24\htdocs\testing\IPTestService.php(10): require_once('C:\\Apache24\\htd...') #5 {main} thrown in C:\Apache24\htdocs\testing\ipv4-subnet-calculator\src\SubnetCalculator.php on line 705

Thank you for your Feedback.

Best regards,
Jan

Get HostMin and HostMax

Thanks for great work, it works perfectly.
Whats your thoughts on adding HostMin and HostMax range that can be assigned.

For IP: 192.168.112.203/23

HostMin:   192.168.112.1
HostMax:   192.168.113.254

Example: http://jodies.de/ipcalc?host=192.168.112.203&mask1=23

May be we can add these functions as well.

$sub->getHostsRange();
$sub->getHostMin();
$sub->getHostMax();

List All IPs

Hello. Thanks for the library.
Can we have method for listing all IP in subnet?
Something like:

$sub = new IPv4\SubnetCalculator( '50.50.50.50', 20 );
list($start, $end) = $sub->getIPAddressRange();
$array = [];

$range = range(ip2long($start), ip2long($end));
foreach ($range as $rng){
    $array[] = long2ip($rng);
}
return $array;

If you like I can make PR for this or you can add it by yourself.

Additional methods

Also would be nice to have:

  • check if IP is in this subnet
  • reverse lookup: get subnet by IP range (given: 50.0.0.0 โ€” 50.255.255.255, get: 50.50.50.50/8)

Wrong min_host when using single ip

If the object is created with a subnetmask of 255.255.255.255 or /32, the min host isn't calculated correctly.

$subnet = new IPv4\SubnetCalculator(192.168.178.23, 32);
$subnet->printSubnetReport();

returns

192.168.178.23/32            Quads      Hex                           Binary
------------------ --------------- -------- --------------------------------
IP Address:         192.168.178.23 C0A8B217 11000000101010001011001000010111
Subnet Mask:       255.255.255.255 FFFFFFFF 11111111111111111111111111111111
Network Portion:    192.168.178.23 C0A8B217 11000000101010001011001000010111
Host Portion:              0.0.0.0 00000000 00000000000000000000000000000000

Number of IP Addresses:      1
Number of Addressable Hosts: 1
IP Address Range:            192.168.178.23 - 192.168.178.23
Broadcast Address:           192.168.178.23
Min Host:                    192.168.178.24
Max Host:                    192.168.178.23

This also happens with both getMinHost() and getAddressableHostRange().

PHP 8.1 compatibility

PHP Deprecated: Return type of IPv4\SubnetCalculator::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

php 5.3 support

Hello,

in your composer you show support to PHP 5.3 but in this version it isn't possible to define arrays like this

$array = []

Ipv6

it would be good if the whole thing would also work with ipv6.

Use case.

Hello, I am developing an ISP management platform and am interested in using your library if that's OK with you. My use case :

  1. Define bras
  2. Define subnets (each subnet has bras and use case - whether network or clients)
  3. generate smaller nested subnets into the main subnet. - For instance, a /22 representing a tower can be broken into two /23, or a couple dozen /30's.
  4. each of the client subnets defined in 3 above also has a vlan attached to it, so whene generating subnets, we define a starting subnet and then as it generates the subnet, it has a vlan attached to this, and a "status" flag which states if the subnet is utilised or available.

When customer is provisioned service,
a) subnet flag is changed to assigned
b) API writes into mikrotik, creating the subnet on the bridge, and assigning the gateway IP to the subnet, and a simple queue is created for the vlan, for the package assigned to the client.
c) installation instructions are then generated for the technician, who simply logs to client end devices, and inserts the statis /30 IP and vlan for the client to get service.

I intend to use your script for two things:

a) creating subnets given a master subnet. e.g. create four /24's from one/22 on the fly.
b) looping through each client subnet and getting the network, gateway, client and broadcast IP's in an array that I can then use to populate my db and write to my routers.

Please let me knowif it is OK to use it, and how you would recommend implementing efficiently

Request: calculate n-th subnet

Hi,

Would it be possible to add to the code an n- subnet value.
For example the bc, hostmin, hostmax, etc of the 3th subnet for 123.78.88.90/12 ?
Maybe feeding it as an argument to the script?

Thnx.
Jay

PHP 8.1 compatibility

During inheritance of JsonSerializable: Uncaught ErrorException: Return type of IPv4\SubnetCalculator::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\Re
turnTypeWillChange] attribute should be used to temporarily suppress the notice in markrogoyski/ipv4-subnet-calculator/src/SubnetCalculator.php:549

I did not have try to track this or fix it yet, but I am adding it here just for reference. Seems that SubnetCalculator is broken in 8.1 (but not 8.0).

Feature Request

Hello,

would it be possible to integrate arpa from an IP as a function?

error method validateInputs

replace

    /**
     * Validate IP address and network.
     * @param string $ip IP address in dotted quads format.
     * @param int $network_size Network.
     * @throws Exception ip or network size not valid
     */
    private function validateInputs( $ip, $network_size ) {
        if (!filter_var( $ip, FILTER_VALIDATE_IP )) {
            throw new Exception("IP address $ip not valid.");
        }
        if ( ( $network_size < 1 ) || ( $network_size > 32 ) ) {
            throw new Exception("Network size $network_size not valid.");
        }
    }

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.