source: trunk/src/inc/Network.class.php@ 7834

Last change on this file since 7834 was 7773, checked in by Pieter Naber, 15 years ago

Created interlinks!
Fixed LogHandler to output at the end (so we have a valid KML file)

File size: 5.4 KB
Line 
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
8class Network {
9 public $netmaskNumber; // Netmask number
10 public $netmaskBinary; // Binary value of netmask address
11 public $netmaskDecimal; // Decimal value of netmask address
12 public $wildcardBinary; // Binary value of wildcard address
13 public $wildcardDecimal; // Decimal value of wildcard address
14 public $networkBinary; // Binary value of network address
15 public $networkDecimal; // Decimal value of network address
16 public $broadcastBinary; // Binary value of broadcast address
17 public $broadcastDecimal; // Decimal value of broadcast address
18 public $hostminBinary; // Binary value of hostmin address
19 public $hostminDecimal; // Decimal value of hostmin address
20 public $hostmaxBinary; // Binary value of hostmax address
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) {
31 // Binary value of given IP address
32 $binary = $this->binIP($ip_address);
33
34 // Let's store the IP netmask number
35 $this->netmaskNumber = $ip_netmask;
36
37 // Netmask address
38 $this->netmaskBinary = str_repeat('1', $ip_netmask) . str_repeat('0', 32 - $ip_netmask);
39 $this->netmaskDecimal = $this->decIP($this->netmaskBinary);
40
41 // Wildcard address
42 $this->wildcardBinary = str_repeat('0', $ip_netmask) . str_repeat('1', 32 - $ip_netmask);
43 $this->wildcardDecimal = $this->decIP($this->wildcardBinary);
44
45 // Network address
46 $this->networkBinary = substr($binary, 0, $ip_netmask) . str_repeat('0', 32 - $ip_netmask);
47 $this->networkDecimal = $this->decIP($this->networkBinary);
48
49 // Broadcast address
50 $this->broadcastBinary = substr($binary, 0, $ip_netmask) . str_repeat('1', 32 - $ip_netmask);
51 $this->broadcastDecimal = $this->decIP($this->broadcastBinary);
52
53 // HostMin address
54 $this->hostminBinary = substr($binary, 0, $ip_netmask) . str_repeat('0', 31 - $ip_netmask) . '1';
55 $this->hostminDecimal = $this->decIP($this->hostminBinary);
56
57 // HostMax address
58 $this->hostmaxBinary = substr($binary, 0, $ip_netmask) . str_repeat('1', 31 - $ip_netmask) . '0';
59 $this->hostmaxDecimal = $this->decIP($this->hostmaxBinary);
60
61 // Numbers of hosts
62 $this->numberHosts = bindec(substr($this->broadcastBinary, $ip_netmask)) - bindec(substr($this->networkBinary, $ip_netmask)) - 1;
63 }
64
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 */
71 function binIP($address) {
72 $address_array = explode('.', $address);
73 $address_binary = '';
74
75 for ($i = 0; $i < 4; $i++) {
76 // Create binary value
77 $binary = decbin($address_array[$i]);
78
79 // Trim value to the length
80 $binary = trim($binary, 8);
81
82 // Add zeros at the left to fit to $size
83 $binary = str_repeat('0', 8 - strlen($binary)) . $binary;
84
85 $address_binary .= $binary;
86 }
87
88 return $address_binary;
89 }
90
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) {
98 $address_array = array();
99
100 for ($i = 0; $i < 4; $i++) {
101 $block = substr($binary, $i * 8, 8) . '<br/>';
102 $address_array[] = bindec(intval($block));
103 }
104
105 $address = implode('.', $address_array);
106
107 return $address;
108 }
109
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;
125 }
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 }
148
149 /*
150 * Function: toString
151 * Description: Converts the data of the network to a string
152 * Parameters: -
153 * Returns: string with all data
154 */
155 function toString() {
156 return '
157 Netmask: ' . $this->netmaskDecimal . ' / ' . $this->netmaskBinary . '<br/>
158 Wildcard: ' . $this->wildcardDecimal . ' / ' . $this->wildcardBinary . '<br/>
159 Network: ' . $this->networkDecimal . ' / ' . $this->networkBinary . '<br/>
160 Broadcast: ' . $this->broadcastDecimal . ' / ' . $this->broadcastBinary . '<br/>
161 HostMin: ' . $this->hostminDecimal . ' / ' . $this->hostminBinary . '<br/>
162 HostMax: ' . $this->hostmaxDecimal . ' / ' . $this->hostmaxBinary . '<br/>
163 Hosts/Net: ' . $this->numberHosts . '<br/>';
164 }
165}
166?>
Note: See TracBrowser for help on using the repository browser.