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

Last change on this file since 7766 was 7766, checked in by Pieter Naber, 16 years ago

Created a class Network for links. Needs work!

File size: 4.0 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 $netmaskBinair; // Binair value of netmask address
11 public $netmaskDecimal; // Decimal value of netmask address
12 public $wildcardBinair; // Binair value of wildcard address
13 public $wildcardDecimal; // Decimal value of wildcard address
14 public $networkBinair; // Binair value of network address
15 public $networkDecimal; // Decimal value of network address
16 public $broadcastBinair; // Binair value of broadcast address
17 public $broadcastDecimal; // Decimal value of broadcast address
18 public $hostminBinair; // Binair value of hostmin address
19 public $hostminDecimal; // Decimal value of hostmin address
20 public $hostmaxBinair; // Binair 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 // Binair value of given IP address
32 $binair = $this->binIP($ip_address);
33
34 // Let's store the IP netmask number
35 $this->netmaskNumber = $ip_netmask;
36
37 // Netmask address
38 $this->netmaskBinair = str_repeat('1', $ip_netmask) . str_repeat('0', 32 - $ip_netmask);
39 $this->netmaskDecimal = $this->decIP($this->netmaskBinair);
40
41 // Wildcard address
42 $this->wildcardBinair = str_repeat('0', $ip_netmask) . str_repeat('1', 32 - $ip_netmask);
43 $this->wildcardDecimal = $this->decIP($this->wildcardBinair);
44
45 // Network address
46 $this->networkBinair = substr($binair, 0, $ip_netmask) . str_repeat('0', 32 - $ip_netmask);
47 $this->networkDecimal = $this->decIP($this->networkBinair);
48
49 // Broadcast address
50 $this->broadcastBinair = substr($binair, 0, $ip_netmask) . str_repeat('1', 32 - $ip_netmask);
51 $this->broadcastDecimal = $this->decIP($this->broadcastBinair);
52
53 // HostMin address
54 $this->hostminBinair = substr($binair, 0, $ip_netmask) . str_repeat('0', 31 - $ip_netmask) . '1';
55 $this->hostminDecimal = $this->decIP($this->hostminBinair);
56
57 // HostMax address
58 $this->hostmaxBinair = substr($binair, 0, $ip_netmask) . str_repeat('1', 31 - $ip_netmask) . '0';
59 $this->hostmaxDecimal = $this->decIP($this->hostmaxBinair);
60
61 // Numbers of hosts
62 $this->numberHosts = bindec(substr($this->broadcastBinair, $ip_netmask)) - bindec(substr($this->networkBinair, $ip_netmask)) - 1;
63 }
64
65 function binIP($address) {
66 $address_array = explode('.', $address);
67 $address_binair = '';
68
69 for ($i = 0; $i < 4; $i++) {
70 // Create binair value
71 $binair = decbin($address_array[$i]);
72
73 // Trim value to the length
74 $binair = trim($binair, 8);
75
76 // Add zeros at the left to fit to $size
77 $binair = str_repeat('0', 8 - strlen($binair)) . $binair;
78
79 $address_binair .= $binair;
80 }
81
82 return $address_binair;
83 }
84
85 function decIP($binair) {
86 $address_array = array();
87
88 for ($i = 0; $i < 4; $i++) {
89 $block = substr($binair, $i * 8, 8) . '<br/>';
90 $address_array[] = bindec(intval($block));
91 }
92
93 return implode('.', $address_array);
94 }
95
96 function inNetwork($ip_address) {
97 return false;
98 }
99}
100
101$ip = '172.16.0.245';
102$network = new Network($ip, 30);
103echo '
104 Address: ' . $ip . ' / ' . Network::binIP($ip) . '<br/>
105 Netmask: ' . $network->netmaskDecimal . ' / ' . $network->netmaskBinair . '<br/>
106 Wildcard: ' . $network->wildcardDecimal . ' / ' . $network->wildcardBinair . '<br/>
107 Network: ' . $network->networkDecimal . ' / ' . $network->networkBinair . '<br/>
108 Broadcast: ' . $network->broadcastDecimal . ' / ' . $network->broadcastBinair . '<br/>
109 HostMin: ' . $network->hostminDecimal . ' / ' . $network->hostminBinair . '<br/>
110 HostMax: ' . $network->hostmaxDecimal . ' / ' . $network->hostmaxBinair . '<br/>
111 Hosts/Net: ' . $network->numberHosts . '<br/>';
112?>
Note: See TracBrowser for help on using the repository browser.