Changeset 247 for trunk


Ignore:
Timestamp:
Sep 2, 2009, 12:39:06 PM (15 years ago)
Author:
george
Message:
  • Opraveno: Skript pro import online stavu IP adres routeboard netwatch.
  • Upraveno: Funkce pro přepočet CIDR podsítí na rozsah adres.
  • Přidáno: Funkce pro kontrolu platnosti zadané IP adresy v IP rozashu.
Location:
trunk
Files:
2 added
4 deleted
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/global.php

    r237 r247  
    351351}
    352352
     353function IPv4ToInt32($IP)
     354{
     355  $Parts = explode('.', $IP);
     356  return(($Parts[0] << 24) | ($Parts[1] << 16) | ($Parts[2] << 8) | $Parts[3]);
     357}
     358
     359function Int32ToIPv4($Value)
     360{
     361  return(implode('.', array(($Value >> 24) & 255, ($Value >> 16) & 255, ($Value >> 8) & 255, ($Value & 255))));
     362}
     363
    353364function CIDRToAddressRange($Subnet, $Mask)
    354365{
    355   $SubnetParts = explode('.', $Subnet);
    356   $SubnetBinary = ($SubnetParts[0] << 24) | ($SubnetParts[1] << 16) | ($SubnetParts[2] << 8) | $SubnetParts[3];
    357   $SubnetTopBinary = $SubnetBinary + (1 << (32 - $Mask)) - 1;
    358   $SubnetTopParts = array(($SubnetTopBinary >> 24) & 255, ($SubnetTopBinary >> 16) & 255, ($SubnetTopBinary >> 8) & 255, ($SubnetTopBinary & 255));
    359   $SubnetTop = implode('.', $SubnetTopParts);
    360   return(array('From' => $Subnet, 'To' => $SubnetTop));
     366  $SubnetBinary = IPv4ToInt32($Subnet);
     367  $Hostmask = (1 << (32 - $Mask)) - 1;
     368  $Netmask = 0xffffffff ^ $Hostmask;
     369  $From = $SubnetBinary & $Netmask;
     370  $To = $From + $Hostmask;
     371  return(array('From' => Int32ToIPv4($From), 'To' => Int32ToIPv4($To)));
     372}
     373
     374function IsAddressInSubnet($Address, $Subnet, $Mask)
     375{
     376  $AddressBinary = IPv4ToInt32($Address);
     377  $SubnetBinary = IPv4ToInt32($Subnet);
     378  $Netmask = 0xffffffff ^ ((1 << (32 - $Mask)) - 1);
     379  if(($AddressBinary & $Netmask) == ($SubnetBinary & $Netmask)) return(true);
     380    else return(false);
    361381}
    362382
Note: See TracChangeset for help on using the changeset viewer.