source: trunk/Packages/Common/NetworkAddress.php

Last change on this file was 888, checked in by chronos, 2 years ago
  • Modified: Updated Common package to latest version.
  • Modified: Fixes related to PHP 8.x.
File size: 3.4 KB
Line 
1<?php
2
3define('IPV4_BIT_WIDTH', 32);
4
5class NetworkAddressIPv4
6{
7 public int $Address;
8 public int $Prefix;
9
10 function __construct()
11 {
12 $this->Address = 0;
13 $this->Prefix = 0;
14 }
15
16 function GetNetMask(): int
17 {
18 return ((1 << IPV4_BIT_WIDTH) - 1) ^ ((1 << (IPV4_BIT_WIDTH - $this->Prefix)) - 1);
19 }
20
21 function AddressToString(): string
22 {
23 return implode('.', array(($this->Address >> 24) & 255, ($this->Address >> 16) & 255, ($this->Address >> 8) & 255, ($this->Address & 255)));
24 }
25
26 function AddressFromString(string $Value): void
27 {
28 $Parts = explode('.', $Value);
29 $this->Address = ($Parts[0] << 24) | ($Parts[1] << 16) | ($Parts[2] << 8) | $Parts[3];
30 }
31
32 function GetRange(): array
33 {
34 $From = new NetworkAddressIPv4();
35 $From->Address = $this->Address;
36 $From->Prefix = IPV4_BIT_WIDTH;
37 $HostMask = 0xffffffff ^ $this->GetNetMask();
38 $To = new NetworkAddressIPv4();
39 $To->Address = $From->Address + $HostMask;
40 $To->Prefix = IPV4_BIT_WIDTH;
41 return array('From' => $From, 'To' => $To);
42 }
43
44 function ChangePrefix(int $NewPrefix): void
45 {
46 $this->Prefix = $NewPrefix;
47 if ($this->Prefix > IPV4_BIT_WIDTH) $this->Prefix = IPV4_BIT_WIDTH;
48 if ($this->Prefix < 0) $this->Prefix = 0;
49 $this->Address = $this->Address & $this->GetNetMask();
50 }
51
52 function Contain(NetworkAddressIPv4 $Address): bool
53 {
54 $UpperNetmask = $this->GetNetMask();
55 if (($this->Prefix < $Address->Prefix) and (($Address->Address & $UpperNetmask) == ($this->Address & $UpperNetmask))) $Result = true;
56 else $Result = false;
57 return $Result;
58 }
59}
60
61define('IPV6_BIT_WIDTH', 128);
62
63class NetworkAddressIPv6
64{
65 public string $Address;
66 public int $Prefix;
67
68 function __construct()
69 {
70 $this->Address = 0;
71 $this->Prefix = 0;
72 }
73
74 function GetNetMask(): string
75 {
76 return Int128Xor(Int128Sub(Int128Shl(IntToInt128(1), IntToInt128(IPV6_BIT_WIDTH)), IntToInt128(1)),
77 Int128Sub(Int128Shl(IntToInt128(1), IntToInt128(IPV6_BIT_WIDTH - $this->Prefix)), IntToInt128(1)));
78 }
79
80 function AddressToString(): string
81 {
82 return inet_ntop($this->Address);
83 }
84
85 function AddressFromString(string $Value)
86 {
87 $this->Address = inet_pton($Value);
88 }
89
90 function ChangePrefix(int $NewPrefix): void
91 {
92 $this->Prefix = $NewPrefix;
93 if ($this->Prefix > IPV6_BIT_WIDTH) $this->Prefix = IPV6_BIT_WIDTH;
94 if ($this->Prefix < 0) $this->Prefix = 0;
95 $this->Address = Int128And($this->Address, $this->GetNetMask());
96 }
97
98 function GetOctets(): array
99 {
100 $Result = array();
101 $Data = array_reverse(unpack('C*', $this->Address));
102 foreach ($Data as $Item)
103 {
104 $Result[] = dechex($Item & 15);
105 $Result[] = dechex(($Item >> 4) & 15);
106 }
107 return $Result;
108 }
109
110 function EncodeMAC(string $MAC): void
111 {
112 $MAC = explode(':', $MAC);
113 $Data = unpack('C*', $this->Address);
114 $Data[9] = hexdec($MAC[0]) ^ 0x02;
115 $Data[10] = hexdec($MAC[1]);
116 $Data[11] = hexdec($MAC[2]);
117 $Data[12] = 0xff;
118 $Data[13] = 0xfe;
119 $Data[14] = hexdec($MAC[3]);
120 $Data[15] = hexdec($MAC[4]);
121 $Data[16] = hexdec($MAC[5]);
122 $this->Address = pack_array('C*', $Data);
123 }
124
125 function Contain(NetworkAddressIPv6 $Address): bool
126 {
127 $UpperNetmask = $this->GetNetMask();
128 if (($this->Prefix < $Address->Prefix) and ((Int128Equal(Int128And($Address->Address, $UpperNetmask), Int128And($this->Address, $UpperNetmask))))) $Result = true;
129 else $Result = false;
130 return $Result;
131 }
132}
Note: See TracBrowser for help on using the repository browser.