1 | <?php
|
---|
2 |
|
---|
3 | class NetworkAddressIPv4
|
---|
4 | {
|
---|
5 | var $Address;
|
---|
6 | var $Prefix;
|
---|
7 |
|
---|
8 | function __construct()
|
---|
9 | {
|
---|
10 | $this->Address = 0;
|
---|
11 | $this->Prefix = 0;
|
---|
12 | }
|
---|
13 |
|
---|
14 | function GetNetMask()
|
---|
15 | {
|
---|
16 | return(0xffffffff ^ ((1 << (32 - $this->Prefix)) - 1));
|
---|
17 | }
|
---|
18 |
|
---|
19 | function AddressToString()
|
---|
20 | {
|
---|
21 | return(implode('.', array(($this->Address >> 24) & 255, ($this->Address >> 16) & 255, ($this->Address >> 8) & 255, ($this->Address & 255))));
|
---|
22 | }
|
---|
23 |
|
---|
24 | function AddressFromString($Value)
|
---|
25 | {
|
---|
26 | $Parts = explode('.', $Value);
|
---|
27 | $this->Address = ($Parts[0] << 24) | ($Parts[1] << 16) | ($Parts[2] << 8) | $Parts[3];
|
---|
28 | }
|
---|
29 |
|
---|
30 | function GetRange()
|
---|
31 | {
|
---|
32 | $From = new NetworkAddressIPv4();
|
---|
33 | $From->Address = $this->Address;
|
---|
34 | $From->Prefix = 32;
|
---|
35 | $HostMask = 0xffffffff ^ $this->GetNetMask();
|
---|
36 | $To = new NetworkAddressIPv4();
|
---|
37 | $To->Address = $From->Address + $HostMask;
|
---|
38 | $To->Prefix = 32;
|
---|
39 | return(array('From' => $From, 'To' => $To));
|
---|
40 | }
|
---|
41 |
|
---|
42 | function ChangePrefix($NewPrefix)
|
---|
43 | {
|
---|
44 | $this->Prefix = $NewPrefix;
|
---|
45 | if($this->Prefix > 32) $this->Prefix = 32;
|
---|
46 | if($this->Prefix < 0) $this->Prefix = 0;
|
---|
47 | $this->Address = $this->Address & $this->GetNetMask();
|
---|
48 | }
|
---|
49 |
|
---|
50 | function Contain($Address)
|
---|
51 | {
|
---|
52 | $UpperNetmask = $this->GetNetMask();
|
---|
53 | if(($this->Prefix < $Address->Prefix) and (($Address->Address & $UpperNetmask) == ($this->Address & $UpperNetmask))) $Result = true;
|
---|
54 | else $Result = false;
|
---|
55 | //echo($Address->AddressToString().'/'.$Address->Prefix.' in '.$this->AddressToString().'/'.$this->Prefix.' '.$Result."\n");
|
---|
56 | return($Result);
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | class NetworkAddressIPv6
|
---|
61 | {
|
---|
62 | var $Address;
|
---|
63 | var $Prefix;
|
---|
64 |
|
---|
65 | function __construct()
|
---|
66 | {
|
---|
67 | $this->Address = 0;
|
---|
68 | $this->Prefix = 0;
|
---|
69 | }
|
---|
70 |
|
---|
71 | function AddressToString()
|
---|
72 | {
|
---|
73 | return(inet_ntop($this->Address));
|
---|
74 | }
|
---|
75 |
|
---|
76 | function AddressFromString($Value)
|
---|
77 | {
|
---|
78 | $this->Address = inet_pton($Value);
|
---|
79 | }
|
---|
80 |
|
---|
81 | function GetOctets()
|
---|
82 | {
|
---|
83 | $Result = array();
|
---|
84 | $Data = array_reverse(unpack('C*', $this->Address));
|
---|
85 | foreach($Data as $Item)
|
---|
86 | {
|
---|
87 |
|
---|
88 | $Result[] = dechex($Item & 15);
|
---|
89 | $Result[] = dechex(($Item >> 4) & 15);
|
---|
90 | }
|
---|
91 | return($Result);
|
---|
92 | }
|
---|
93 |
|
---|
94 | function EncodeMAC($MAC)
|
---|
95 | {
|
---|
96 | $MAC = explode(':', $MAC);
|
---|
97 | $Data = unpack('C*', $this->Address);
|
---|
98 | $Data[9] = hexdec($MAC[0]) ^ 0x02;
|
---|
99 | $Data[10] = hexdec($MAC[1]);
|
---|
100 | $Data[11] = hexdec($MAC[2]);
|
---|
101 | $Data[12] = 0xff;
|
---|
102 | $Data[13] = 0xfe;
|
---|
103 | $Data[14] = hexdec($MAC[3]);
|
---|
104 | $Data[15] = hexdec($MAC[4]);
|
---|
105 | $Data[16] = hexdec($MAC[5]);
|
---|
106 | $this->Address = pack_array('C*', $Data);
|
---|
107 | }
|
---|
108 |
|
---|
109 | }
|
---|