markrogoyski / ipv4-subnet-calculator-php

Network calculator for subnet mask and other classless (CIDR) network information.
MIT License
168 stars 42 forks source link

Wrong min_host when using single ip #9

Closed hendrikbl closed 6 years ago

hendrikbl commented 6 years ago

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().

markrogoyski commented 6 years ago

Hi @hendrikbl, What version are you using? Do you see this behavior with the latest version, 2.1.0 (https://github.com/markrogoyski/ipv4-subnet-calculator-php/releases/tag/v2.1.0)? Thanks, Mark

hendrikbl commented 6 years ago

@markrogoyski Yes, I'm using the latest 2.1.0 with the following in my composer.json

rogoyski/ipv4-subnet-calculator": "^2.1"
markrogoyski commented 6 years ago

Hi @hendrikbl, I am not able to reproduce your issue.

Here is the output I get:

$ php -a
Interactive mode enabled

php > require_once __DIR__ . '/SubnetCalculator.php';
php >
php > $subnet = new IPv4\SubnetCalculator('192.168.178.23', 32);
php >
php > echo $subnet->getMinHost();
192.168.178.23
php > print_r($subnet->getAddressableHostRange());
Array
(
    [0] => 192.168.178.23
    [1] => 192.168.178.23
)
php >
php > $subnet->printSubnetReport();
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.23
Max Host:                    192.168.178.23
php >

Please verify that you are in fact using the latest version.

Also, check that your composer require has the correct package name and try doing a composer update and then try it out again. Let me know if you still see the issue.

{
  "require": {
      "markrogoyski/ipv4-subnet-calculator": "2.*"
  }
}

Thanks, Mark

hendrikbl commented 6 years ago

Okay, this is weird... My mask (32) was a string. Funny thing is, everything works fine whith this, except the min host. Changing it to int now fixed the problem.

Thank you for looking into it :)