Changeset 870 for trunk/Packages/Common
- Timestamp:
- Apr 3, 2020, 12:30:49 AM (5 years ago)
- Location:
- trunk/Packages/Common
- Files:
-
- 2 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/Common.php
r869 r870 21 21 include_once(dirname(__FILE__).'/Process.php'); 22 22 include_once(dirname(__FILE__).'/Generics.php'); 23 include_once(dirname(__FILE__).'/BigInt.php'); 24 include_once(dirname(__FILE__).'/Int128.php'); 23 25 24 26 class PackageCommon -
trunk/Packages/Common/NetworkAddress.php
r746 r870 1 1 <?php 2 3 define('IPV4_BIT_WIDTH', 32); 2 4 3 5 class NetworkAddressIPv4 … … 14 16 function GetNetMask() 15 17 { 16 return( 0xffffffff ^ ((1 << (32- $this->Prefix)) - 1));18 return(((1 << IPV4_BIT_WIDTH) - 1) ^ ((1 << (IPV4_BIT_WIDTH - $this->Prefix)) - 1)); 17 19 } 18 20 … … 32 34 $From = new NetworkAddressIPv4(); 33 35 $From->Address = $this->Address; 34 $From->Prefix = 32;36 $From->Prefix = IPV4_BIT_WIDTH; 35 37 $HostMask = 0xffffffff ^ $this->GetNetMask(); 36 38 $To = new NetworkAddressIPv4(); 37 39 $To->Address = $From->Address + $HostMask; 38 $To->Prefix = 32;40 $To->Prefix = IPV4_BIT_WIDTH; 39 41 return(array('From' => $From, 'To' => $To)); 40 42 } … … 43 45 { 44 46 $this->Prefix = $NewPrefix; 45 if($this->Prefix > 32) $this->Prefix = 32;47 if($this->Prefix > IPV4_BIT_WIDTH) $this->Prefix = IPV4_BIT_WIDTH; 46 48 if($this->Prefix < 0) $this->Prefix = 0; 47 49 $this->Address = $this->Address & $this->GetNetMask(); … … 53 55 if(($this->Prefix < $Address->Prefix) and (($Address->Address & $UpperNetmask) == ($this->Address & $UpperNetmask))) $Result = true; 54 56 else $Result = false; 55 //echo($Address->AddressToString().'/'.$Address->Prefix.' in '.$this->AddressToString().'/'.$this->Prefix.' '.$Result."\n");56 57 return($Result); 57 58 } 58 59 } 60 61 define('IPV6_BIT_WIDTH', 128); 59 62 60 63 class NetworkAddressIPv6 … … 69 72 } 70 73 74 function GetNetMask() 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 71 80 function AddressToString() 72 81 { … … 77 86 { 78 87 $this->Address = inet_pton($Value); 88 } 89 90 function ChangePrefix($NewPrefix) 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()); 79 96 } 80 97 … … 107 124 } 108 125 126 function Contain($Address) 127 { 128 $UpperNetmask = $this->GetNetMask(); 129 if(($this->Prefix < $Address->Prefix) and ((Int128Equal(Int128And($Address->Address, $UpperNetmask), Int128And($this->Address, $UpperNetmask))))) $Result = true; 130 else $Result = false; 131 return($Result); 132 } 109 133 }
Note:
See TracChangeset
for help on using the changeset viewer.