[7766] | 1 | <?php
|
---|
| 2 | /*
|
---|
| 3 | * Project: NodeMap2.0
|
---|
| 4 | * File: Network.class.php
|
---|
| 5 | * Purpose: Contains a network, calculates everything like broadcast IP, netmask etc.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | class Network {
|
---|
| 9 | public $netmaskNumber; // Netmask number
|
---|
[7768] | 10 | public $netmaskBinary; // Binary value of netmask address
|
---|
[7766] | 11 | public $netmaskDecimal; // Decimal value of netmask address
|
---|
[7768] | 12 | public $wildcardBinary; // Binary value of wildcard address
|
---|
[7766] | 13 | public $wildcardDecimal; // Decimal value of wildcard address
|
---|
[7768] | 14 | public $networkBinary; // Binary value of network address
|
---|
[7766] | 15 | public $networkDecimal; // Decimal value of network address
|
---|
[7768] | 16 | public $broadcastBinary; // Binary value of broadcast address
|
---|
[7766] | 17 | public $broadcastDecimal; // Decimal value of broadcast address
|
---|
[7768] | 18 | public $hostminBinary; // Binary value of hostmin address
|
---|
[7766] | 19 | public $hostminDecimal; // Decimal value of hostmin address
|
---|
[7768] | 20 | public $hostmaxBinary; // Binary value of hostmax address
|
---|
[7766] | 21 | public $hostmaxDecimal; // Decimal value of hostmax address
|
---|
| 22 | public $numberHosts; // Number of possible hosts in this network
|
---|
| 23 |
|
---|
| 24 | /*
|
---|
| 25 | * Function: __construct (constructor)
|
---|
| 26 | * Description: Creating a new Network object
|
---|
| 27 | * Parameters: string $ip_address, int $ip_netmask
|
---|
| 28 | * Returns: -
|
---|
| 29 | */
|
---|
| 30 | public function __construct($ip_address, $ip_netmask) {
|
---|
[7768] | 31 | // Binary value of given IP address
|
---|
| 32 | $binary = $this->binIP($ip_address);
|
---|
[7766] | 33 |
|
---|
| 34 | // Let's store the IP netmask number
|
---|
| 35 | $this->netmaskNumber = $ip_netmask;
|
---|
| 36 |
|
---|
| 37 | // Netmask address
|
---|
[7768] | 38 | $this->netmaskBinary = str_repeat('1', $ip_netmask) . str_repeat('0', 32 - $ip_netmask);
|
---|
| 39 | $this->netmaskDecimal = $this->decIP($this->netmaskBinary);
|
---|
[7766] | 40 |
|
---|
| 41 | // Wildcard address
|
---|
[7768] | 42 | $this->wildcardBinary = str_repeat('0', $ip_netmask) . str_repeat('1', 32 - $ip_netmask);
|
---|
| 43 | $this->wildcardDecimal = $this->decIP($this->wildcardBinary);
|
---|
[7766] | 44 |
|
---|
| 45 | // Network address
|
---|
[7768] | 46 | $this->networkBinary = substr($binary, 0, $ip_netmask) . str_repeat('0', 32 - $ip_netmask);
|
---|
| 47 | $this->networkDecimal = $this->decIP($this->networkBinary);
|
---|
[7766] | 48 |
|
---|
| 49 | // Broadcast address
|
---|
[7768] | 50 | $this->broadcastBinary = substr($binary, 0, $ip_netmask) . str_repeat('1', 32 - $ip_netmask);
|
---|
| 51 | $this->broadcastDecimal = $this->decIP($this->broadcastBinary);
|
---|
[7766] | 52 |
|
---|
| 53 | // HostMin address
|
---|
[7768] | 54 | $this->hostminBinary = substr($binary, 0, $ip_netmask) . str_repeat('0', 31 - $ip_netmask) . '1';
|
---|
| 55 | $this->hostminDecimal = $this->decIP($this->hostminBinary);
|
---|
[7766] | 56 |
|
---|
| 57 | // HostMax address
|
---|
[7768] | 58 | $this->hostmaxBinary = substr($binary, 0, $ip_netmask) . str_repeat('1', 31 - $ip_netmask) . '0';
|
---|
| 59 | $this->hostmaxDecimal = $this->decIP($this->hostmaxBinary);
|
---|
[7766] | 60 |
|
---|
| 61 | // Numbers of hosts
|
---|
[7768] | 62 | $this->numberHosts = bindec(substr($this->broadcastBinary, $ip_netmask)) - bindec(substr($this->networkBinary, $ip_netmask)) - 1;
|
---|
[7766] | 63 | }
|
---|
| 64 |
|
---|
[7768] | 65 | /*
|
---|
| 66 | * Function: binIP
|
---|
| 67 | * Description: Converts a IP address like 192.168.0.1 to it's binary value
|
---|
| 68 | * Parameters: string $address
|
---|
| 69 | * Returns: string $address_binary
|
---|
| 70 | */
|
---|
[7766] | 71 | function binIP($address) {
|
---|
| 72 | $address_array = explode('.', $address);
|
---|
[7768] | 73 | $address_binary = '';
|
---|
[7766] | 74 |
|
---|
| 75 | for ($i = 0; $i < 4; $i++) {
|
---|
[7768] | 76 | // Create binary value
|
---|
| 77 | $binary = decbin($address_array[$i]);
|
---|
[7766] | 78 |
|
---|
| 79 | // Trim value to the length
|
---|
[7768] | 80 | $binary = trim($binary, 8);
|
---|
[7766] | 81 |
|
---|
| 82 | // Add zeros at the left to fit to $size
|
---|
[7768] | 83 | $binary = str_repeat('0', 8 - strlen($binary)) . $binary;
|
---|
[7766] | 84 |
|
---|
[7768] | 85 | $address_binary .= $binary;
|
---|
[7766] | 86 | }
|
---|
| 87 |
|
---|
[7768] | 88 | return $address_binary;
|
---|
[7766] | 89 | }
|
---|
| 90 |
|
---|
[7768] | 91 | /*
|
---|
| 92 | * Function: decIP
|
---|
| 93 | * Description: Converts a binary IP address to it's decimal value like 192.168.0.1
|
---|
| 94 | * Parameters: string $binary
|
---|
| 95 | * Returns: string $address
|
---|
| 96 | */
|
---|
| 97 | function decIP($binary) {
|
---|
[7766] | 98 | $address_array = array();
|
---|
| 99 |
|
---|
| 100 | for ($i = 0; $i < 4; $i++) {
|
---|
[7768] | 101 | $block = substr($binary, $i * 8, 8) . '<br/>';
|
---|
[7766] | 102 | $address_array[] = bindec(intval($block));
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[7768] | 105 | $address = implode('.', $address_array);
|
---|
| 106 |
|
---|
| 107 | return $address;
|
---|
[7766] | 108 | }
|
---|
| 109 |
|
---|
[7768] | 110 | /*
|
---|
| 111 | * Function: compare
|
---|
| 112 | * Description: Checks if to networks are the same using network IP address
|
---|
| 113 | * Parameters: Network $otherNetwork
|
---|
| 114 | * Returns: true if the networks are the same, otherwise false
|
---|
| 115 | */
|
---|
| 116 | function compare(Network $otherNetwork) {
|
---|
| 117 | $check = false;
|
---|
| 118 |
|
---|
| 119 | // Compare the network address
|
---|
| 120 | if ($this->networkDecimal == $otherNetwork->networkDecimal) {
|
---|
| 121 | $check = true;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | return $check;
|
---|
[7766] | 125 | }
|
---|
[7768] | 126 |
|
---|
| 127 | /*
|
---|
| 128 | * Function: inNetwork
|
---|
| 129 | * Description: Checks if the given IP is in the IP range of the current network
|
---|
| 130 | * Parameters: string $ipDecimal
|
---|
| 131 | * Returns: true if the IP address is in the IP range, otherwise false
|
---|
| 132 | */
|
---|
| 133 | function inNetwork($ipDecimal) {
|
---|
| 134 | $check = true;
|
---|
| 135 |
|
---|
| 136 | // Check if given IP is bigger than our HostMin IP Address
|
---|
| 137 | if (ip2long($ipDecimal) < ip2long($this->hostminDecimal)) {
|
---|
| 138 | $check = false;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | // Check if given IP is smaller than our HostMax IP Address
|
---|
| 142 | if (ip2long($ipDecimal) > ip2long($this->hostmaxDecimal)) {
|
---|
| 143 | $check = false;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | return $check;
|
---|
| 147 | }
|
---|
[7766] | 148 | }
|
---|
| 149 | ?>
|
---|