Ignore:
Timestamp:
Dec 21, 2009, 10:48:41 AM (15 years ago)
Author:
george
Message:
  • Upraveno: Nově přepracován systém generování značkovacích pravidel mangle firewallu. Pro snížení počtu pravidel procházených při značkování packetu, jsou adresy rozdělovány do podskupin. Díky tomu je snížen rozptyl mezi nejmenším a nejvyšším početem procházených pravidel.
  • Upraveno: Přepracovány funkce pro práci s IP adresami typu IPv4. Přehledněji zpracováno jako třída.
  • Přidáno: Tabulka v databázi pro persistentní přiřazení Id čísla podsítě pro generování pravidel mangle firewallu.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/system/generators/common.php

    r266 r267  
    11<?php
     2
     3include_once('../../network_address.php');
    24
    35function GetMarkByComment($Comment)
     
    1719}
    1820
     21function GetSubgroupByRange($AddressRange)
     22{
     23  global $Database;
     24 
     25  $DbResult = $Database->query('SELECT `Id` FROM `NetworkMangleSubgroup` WHERE `AddressRange`="'.$AddressRange.'"');
     26  if($DbResult->num_rows > 0)
     27  {
     28    $DbRow = $DbResult->fetch_assoc();
     29    return($DbRow['Id']);
     30  } else
     31  {
     32    $DbResult = $Database->query('INSERT INTO `NetworkMangleSubgroup` (`AddressRange`) VALUES ("'.$AddressRange.'")');
     33    return($Database->insert_id);
     34  }
     35}
     36
     37function InsertToAddressTree(&$Tree, $Address, $Name, $InterSubnets = false)
     38{
     39  global $Config;
     40 
     41  $Found = false;
     42  foreach($Tree['Items'] as $Index => $Node)
     43  {
     44    if($Node['Address']->Contain($Address))
     45    {
     46      InsertToAddressTree($Tree['Items'][$Index], $Address, $Name, true);
     47      $Found = true;
     48    }
     49  }
     50  if($Found == false)
     51  {
     52    if($InterSubnets and ($Tree['Address']->Prefix < $Config['MainRouter']['MangleRuleSubgroupMinPrefix']) and
     53    ($Address->Prefix > ($Tree['Address']->Prefix + 1)))
     54    {
     55      $NewAddress = new NetworkAddressIPv4();
     56      $NewAddress->Address = $Address->Address;
     57      $NewAddress->ChangePrefix($Tree['Address']->Prefix + 1);
     58      //echo('InsertToTree('.$NewAddress->AddressToString().'/'.$NewAddress->Prefix.')'."\n");
     59      $Tree['Items'][] = array('Address' => $NewAddress, 'Name' => $Name, 'Items' => array());
     60      InsertToAddressTree($Tree['Items'][count($Tree['Items']) - 1], $Address, $Name, true);
     61    } else
     62    {
     63     
     64      $NewNode = array('Address' => $Address, 'Name' => $Name, 'Items' => array());
     65     
     66      // Should be existed items placed under new node?
     67      $Found = false;
     68      foreach($Tree['Items'] as $Index => $Node)
     69      {
     70        if(($Node['Address']->Address == $NewNode['Address']->Address) and
     71        ($Node['Address']->Prefix == $NewNode['Address']->Prefix)) $Found = true;       
     72       
     73        //echo($Index.',');
     74        if($Address->Contain($Node['Address']))
     75        {
     76          $NewNode['Items'][] = $Node;
     77          unset($Tree['Items'][$Index]);
     78        }
     79      }
     80      if($Found == false) $Tree['Items'][] = $NewNode;     
     81    }
     82  }
     83}
     84
     85function ShowSubnetNode($Node, $Indent = 0)
     86{
     87  echo(str_repeat('  ', $Indent).$Node['Address']->AddressToString().'/'.$Node['Address']->Prefix.' '.$Node['Name']."\n");
     88  foreach($Node['Items'] as $Index => $Item)
     89  {
     90    ShowSubnetNode($Item, $Indent + 1);
     91  } 
     92}
     93
     94/*
     95function Test()
     96{
     97  $SubnetTree = array('Address' => new NetworkAddressIPv4(), 'Items' => array());
     98
     99  $NewAddress = new NetworkAddressIPv4();
     100  $NewAddress->AddressFromString('10.145.64.0');
     101  $NewAddress->Prefix = 24;
     102  InsertToAddressTree($SubnetTree, $NewAddress);
     103  $NewAddress = new NetworkAddressIPv4();
     104  $NewAddress->AddressFromString('10.145.64.0');
     105  $NewAddress->Prefix = 29;
     106  InsertToAddressTree($SubnetTree, $NewAddress);
     107  $NewAddress = new NetworkAddressIPv4();
     108  $NewAddress->AddressFromString('10.145.65.0');
     109  $NewAddress->Prefix = 24;
     110  InsertToAddressTree($SubnetTree, $NewAddress);
     111  $NewAddress = new NetworkAddressIPv4();
     112  $NewAddress->AddressFromString('10.145.65.156');
     113  $NewAddress->Prefix = 32;
     114  InsertToAddressTree($SubnetTree, $NewAddress);
     115  $NewAddress = new NetworkAddressIPv4();
     116  $NewAddress->AddressFromString('10.145.64.0');
     117  $NewAddress->Prefix = 20;
     118  InsertToAddressTree($SubnetTree, $NewAddress);
     119
     120
     121  ShowSubnetNode($SubnetTree);
     122  die();
     123}
     124*/
     125
    19126?>
Note: See TracChangeset for help on using the changeset viewer.