Changeset 870 for trunk/Packages/Common


Ignore:
Timestamp:
Apr 3, 2020, 12:30:49 AM (5 years ago)
Author:
chronos
Message:
  • Added: IPv6 mangle firewall configuration.
Location:
trunk/Packages/Common
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/Common.php

    r869 r870  
    2121include_once(dirname(__FILE__).'/Process.php');
    2222include_once(dirname(__FILE__).'/Generics.php');
     23include_once(dirname(__FILE__).'/BigInt.php');
     24include_once(dirname(__FILE__).'/Int128.php');
    2325
    2426class PackageCommon
  • trunk/Packages/Common/NetworkAddress.php

    r746 r870  
    11<?php
     2
     3define('IPV4_BIT_WIDTH', 32);
    24
    35class NetworkAddressIPv4
     
    1416  function GetNetMask()
    1517  {
    16     return(0xffffffff ^ ((1 << (32 - $this->Prefix)) - 1));
     18    return(((1 << IPV4_BIT_WIDTH) - 1) ^ ((1 << (IPV4_BIT_WIDTH - $this->Prefix)) - 1));
    1719  }
    1820
     
    3234    $From = new NetworkAddressIPv4();
    3335    $From->Address = $this->Address;
    34     $From->Prefix = 32;
     36    $From->Prefix = IPV4_BIT_WIDTH;
    3537    $HostMask = 0xffffffff ^ $this->GetNetMask();
    3638    $To = new NetworkAddressIPv4();
    3739    $To->Address = $From->Address + $HostMask;
    38     $To->Prefix = 32;
     40    $To->Prefix = IPV4_BIT_WIDTH;
    3941    return(array('From' => $From, 'To' => $To));
    4042  }
     
    4345  {
    4446    $this->Prefix = $NewPrefix;
    45     if($this->Prefix > 32) $this->Prefix = 32;
     47    if($this->Prefix > IPV4_BIT_WIDTH) $this->Prefix = IPV4_BIT_WIDTH;
    4648    if($this->Prefix < 0) $this->Prefix = 0;
    4749    $this->Address = $this->Address & $this->GetNetMask();
     
    5355    if(($this->Prefix < $Address->Prefix) and (($Address->Address & $UpperNetmask) == ($this->Address & $UpperNetmask))) $Result = true;
    5456      else $Result = false;
    55     //echo($Address->AddressToString().'/'.$Address->Prefix.' in '.$this->AddressToString().'/'.$this->Prefix.' '.$Result."\n");
    5657    return($Result);
    5758  }
    5859}
     60
     61define('IPV6_BIT_WIDTH', 128);
    5962
    6063class NetworkAddressIPv6
     
    6972  }
    7073
     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
    7180  function AddressToString()
    7281  {
     
    7786  {
    7887    $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());
    7996  }
    8097
     
    107124  }
    108125
     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  }
    109133}
Note: See TracChangeset for help on using the changeset viewer.