IPCALC
[ class tree: IPCALC ] [ index: IPCALC ] [ all elements ]

Source for file IPCALC.php

Documentation is available at IPCALC.php

  1. <?php
  2. /**
  3.  * Project: IPCALCLogic :: The internal API about IPCALC class
  4.  * File:    IPCALC/IPCALC.php
  5.  *
  6.  * This class is subpackage of IPCALC class, and support various
  7.  * api for IPCALC class
  8.  *
  9.  * @category    Networking
  10.  * @package     IPCALC
  11.  * @subpackage  IPCALCLogic
  12.  * @author      JoungKyun.Kim <http://oops.org>
  13.  * @copyright   (c) 2018, JoungKyun.Kim
  14.  * @license     LGPL
  15.  * @filesource
  16.  */
  17.  
  18. /**
  19.  * This class is subpackage of IPCALC class, and support various
  20.  * api for IPCALC class
  21.  *
  22.  * @package IPCALC
  23.  */
  24. {
  25.     // {{{ (long) IPCALCLogic::signed_casting ($v)
  26.     /**
  27.      * Convert unsigned long to signed long
  28.      *
  29.      * @access    private
  30.      * @return    long    signed long value
  31.      * @param    long    unsigned long value
  32.      */
  33.     private static function signed_casting (&$v{
  34.         if $v 0x7fffffff )
  35.             $v -= 0xffffffff 1;
  36.     }
  37.     // }}}
  38.  
  39.     // {{{ (long) IPCALCLogic::not_operand ($v)
  40.     /**
  41.      * Support ~(NOT) operand
  42.      *
  43.      * @access    private
  44.      * @return    long 
  45.      * @param    logn    decimical value
  46.      */
  47.     private static function not_operand ($v{
  48.         $v decbin ($v);
  49.         $l strlen ($v);
  50.         $z '';
  51.  
  52.         for $i=0$i<$l$i++ )
  53.             $z .= $v[$i1;
  54.  
  55.         return $z bindec ($zfalse;
  56.     }
  57.     // }}}
  58.  
  59.     // {{{ (long) IPCALCLogic::ip2long ($ip)
  60.     /**
  61.      * Return unsigned proper address about given dotted ipv4 address
  62.      *
  63.      * ip2long API of PHP is returnd signed long value, but this api
  64.      * is returned unsigned long value.
  65.      *
  66.      * @access    public
  67.      * @return    long    proper address of long type
  68.      * @param    string    dotted ipv4 address
  69.      */
  70.     static function ip2long ($ip{
  71.         return sprintf ('%lu'ip2long ($ip));
  72.     }
  73.     // }}}
  74.  
  75.     // {{{ (boolean) IPCALCLogic::valid_ipv4_addr ($ip)
  76.     /**
  77.      * Check given adddress is valid or not
  78.      *
  79.      * @access    public
  80.      * @return    boolean 
  81.      * @param    string    dotted ipv4 address
  82.      */
  83.     static function valid_ipv4_addr ($ip{
  84.         $ip preg_replace ('/\s/'''$ip);
  85.  
  86.         if $p )
  87.             return false;
  88.  
  89.         return (long2ip (ip2long ($ip)) == $iptrue false;
  90.         /*
  91.         $p = self::ip2long ($ip);
  92.         if ( ! $p || ! preg_match ('/[.]/', $ip) )
  93.             return false;
  94.  
  95.         if ( (int) $p < 16777216 )
  96.             return false;
  97.  
  98.         return true;
  99.          */
  100.     }
  101.     // }}}
  102.  
  103.     // {{{ (long) IPCALCLogic::prefix2long ($prefix)
  104.     /**
  105.      * Convert network prefix to long type network mask
  106.      *
  107.      * @access    public
  108.      * @return    long    long type network maks or false
  109.      * @param    int        decimical network prefix
  110.      */
  111.     static function prefix2long ($prefix{
  112.         if is_numeric ($prefix) )
  113.             return false;
  114.  
  115.         if (int) $prefix 32 || (int) $prefix )
  116.             return false;
  117.  
  118.         $l 32;
  119.         $shift 30;
  120.         $v 0;
  121.  
  122.         $prefix 32 - (int) $prefix;
  123.  
  124.         while $l $prefix {
  125.             if $shift )
  126.                 $v += 1;
  127.             else
  128.                 $v += (<< $shift);
  129.  
  130.             $shift--;
  131.             $l--;
  132.         }
  133.  
  134.         return $v;
  135.  
  136.     }
  137.     // }}}
  138.  
  139.     // {{{ (int) IPCALCLogic::long2prefix ($mask)
  140.     /**
  141.      * Convert long type network mask to decimical network prefix
  142.      *
  143.      * @access  public
  144.      * @return  int        Decimical netowrk prefix
  145.      * @param   long    long type network mask
  146.      */
  147.     static function long2prefix ($mask{
  148.         $count 32;
  149.         self::signed_casting ($mask);
  150.  
  151.         for $i 0$i 32$i++{
  152.             if !((int) $mask ((<< $i1)) )
  153.                 $count--;
  154.         }
  155.         return $count;
  156.     }
  157.     // }}}
  158.  
  159.     // {{{ (long) IPCALCLogic::network ($ip, $mask)
  160.     /**
  161.      * Get long type network address for given ip address
  162.      * and network mask
  163.      *
  164.      * @access    public
  165.      * @return    long 
  166.      * @param    string    dotted ipv4 address or long proper address
  167.      * @param    string    dotted network mask or network prefix
  168.      */
  169.     static function network ($ip$mask{
  170.         if is_numeric ($ip) )
  171.             $ip ip2long ($ip);
  172.         self::signed_casting ($ip);
  173.  
  174.         if is_numeric ($mask) )
  175.             $mask ip2long ($mask);
  176.         self::signed_casting ($mask);
  177.  
  178.         if (int) $mask >= && (int) $mask 33 )
  179.             $mask self::prefix2long ($mask);
  180.  
  181.         return (int) $ip (int) $mask;
  182.     }
  183.     // }}}
  184.  
  185.     // {{{ (long) IPCALCLogic::broadcast ($ip, $mask)
  186.     /**
  187.      * Get long type broadcast address for given ip address
  188.      * and network mask
  189.      *
  190.      * @access    public
  191.      * @return    long 
  192.      * @param    string    dotted ipv4 address or long proper address
  193.      * @param    string    dotted network mask or network prefix
  194.      */
  195.     static function broadcast ($ip$mask{
  196.         if is_numeric ($ip) )
  197.             $ip ip2long ($ip);
  198.         self::signed_casting ($ip);
  199.  
  200.         if is_numeric ($mask) )
  201.             $mask ip2long ($mask);
  202.         self::signed_casting ($mask);
  203.  
  204.         if (int) $mask >= && (int) $mask 33 )
  205.             $mask self::prefix2long ((int) $mask);
  206.  
  207.         $r ((int) $ip (int) $maskself::not_operand ($mask);
  208.  
  209.         return $r;
  210.     }
  211.     // }}}
  212.  
  213.     // {{{ (int) IPCALCLogic::guess_prefix ($start, $end)
  214.     /**
  215.      * Get decimical network prefix about given start and end ip address
  216.      *
  217.      * @access    public
  218.      * @return    int        Decimical network prefix
  219.      * @param    string    Dotted ipv4 address or long proper address
  220.      * @param    string    Dotted ipv4 address or long proper address
  221.      */
  222.     static function guess_prefix ($start$end{
  223.         $prefix 0;
  224.         if is_numeric ($start) )
  225.             $start ip2long ($start);
  226.         self::signed_casting ($start);
  227.  
  228.         if is_numeric ($end) )
  229.             $end   ip2long ($end);
  230.         self::signed_casting ($end);
  231.  
  232.         if $end $start {
  233.             $n $start;
  234.             $start $end;
  235.             $end $n;
  236.         }
  237.  
  238.         $n $end $start;
  239.         if (int) $n === )
  240.             return 32;
  241.  
  242.         while ($n 0x80000000) ) {
  243.             $n <<= 1;
  244.             $prefix++;
  245.         }
  246.  
  247.         $n 0;
  248.         while (int) $end > (int) $n {
  249.             if $n )
  250.                 $prefix--;
  251.  
  252.             $n sprintf ('%u'self::broadcast ($start$prefix));
  253.         }
  254.  
  255.         return $prefix;
  256.     }
  257.     // }}}
  258.  
  259.     // {{{ (long) IPCALCLogic::guess_netmask ($start, $end)
  260.     /**
  261.      * Get long type network mask about given start and end ip address
  262.      *
  263.      * @access    public
  264.      * @return    string    Dotted IPv4 address
  265.      * @param    string    Dotted ipv4 address or long proper address
  266.      * @param    string     Dotted ipv4 address or long proper address
  267.      */
  268.     static function guess_netmask ($start$end{
  269.         $r self::prefix2long (self::guess_prefix ($start$end));
  270.         return long2ip ($r);
  271.     }
  272.     // }}}
  273. }
  274.  
  275. /*
  276.  * Local variables:
  277.  * tab-width: 4
  278.  * c-basic-offset: 4
  279.  * End:
  280.  * vim600: noet sw=4 ts=4 fdm=marker
  281.  * vim<600: noet sw=4 ts=4
  282.  */
  283. ?>

Documentation generated on Tue, 14 May 2019 02:00:08 +0900 by phpDocumentor 1.4.4