<?php
##
## this file name is 'class.ktispnet.php'
##
## KT-IPV4 : NETMASK - ISP
##
## ktispnet object
##
## [author]
##  - Chilbong Kim, <san2(at)linuxchannel.net>
##  - http://linuxchannel.net/
##
## [changes]
##  - 2005.07.07 : update
##  - 2005.06.19 : new build
##
## [references]
##  - http://www.krnic.or.kr/jsp/business/management/isCurrentIpv4.jsp // 2016
##  - http://irms.nic.or.kr/servlet/member.view.ISPAllocation
##  - http://ip.nida.or.kr/membership_03.htm
##  - http://irms.nic.or.kr/servlet/member.view.ISPInformation?ENG_SNAME=KORNET
##  - http://irms.nic.or.kr/servlet/member.view.ISPInformation?ENG_SNAME=PUBNET
##
## [usage]
##
## [example]
##   $get = ktispnet::search($_SERVER['REMOTE_ADDR']); // return array
##
class ktispnet
{
  
//var $_NETS = array();
  //var $_ISPS = array();
  //var $_preload = FALSE;

  ## init
  ##
  //function &_ktispnet($ipa=0)
  //{
    //$this->_NETS = ktispnet::netmask();
    //$this->_ISPS = ktispnet::ispmap();
    //$this->_preload = TRUE;

    //return array(ktispnet::netmask($ipa),ktispnet::ispmap());
  //}

  ## main core
  ##
  
function &search($ipaddr)
  {
    list(
$ipa) = explode('.',$ipaddr);
    
//list($_NET,$_ISPS) = ktispnet::_ktispnet($ipa);

    
$_NET ktispnet::netmask($ipa);

    if(!
$_NET) return;

    
$_ISPS ktispnet::ispmap();
    
$ipb ktispnet::inet_atob($ipaddr);
    
$size sizeof($_NET);

    for(
$i=0$i<$size$i++)
    {
        
$mask ktispnet::inet_atob($_NET[$i]);
        
$network bindec($mask $ipb);
        
//$nn = ktispnet::inet_ntoa($network); // debug

        
if($_ISP $_ISPS["$network"]) // require double quota
        
{
            
$fnet ktispnet::inet_atob($_ISP[2]) & $ipb;
            
$fnet ktispnet::inet_ntoa(bindec($fnet));

            
//echo "$_NET[$i] == $mask ==> $network($nn) $fnet\n"; // debug

            
if($fnet == $_ISP[0]) // real network
            
{
                return 
$_ISP// array
                
break;
            }
        }
    }

    return;
  }

  function &
inet_aton($ipaddr)
  {
    return 
sprintf('%u',ip2long($ipaddr));
  }

  function &
inet_ntoa($base)
  {
    return 
long2ip($base);
  }

  function &
inet_ntob($base)
  {
    return 
sprintf('%032b',$base);
  }

  function &
inet_bton($bits)
  {
    return 
bindec($bits);
  }

  function &
inet_atob($ipaddr)
  {
    return 
sprintf('%032b',ip2long($ipaddr));
  }

  function &
inet_btoa($bits)
  {
    return 
long2ip(sprintf('%u',$bits));
  }

  function &
netmask($ipa=0)
  {
    
$_NET[59] = array('255.224.0.0');
    
$_NET[61] = array('255.248.0.0','255.252.0.0','255.254.0.0');
    
$_NET[125] = array('255.224.0.0');
    
$_NET[128] = array('255.255.0.0');
    
$_NET[147] = array('255.255.0.0');
    
$_NET[168] = array('255.254.0.0','255.255.0.0');
    
$_NET[203] = array('255.255.128.0','255.255.192.0','255.255.224.0','255.255.240.0','255.255.252.0');
    
$_NET[210] = array('255.254.0.0','255.255.0.0','255.255.128.0','255.255.192.0','255.255.224.0','255.255.240.0');
    
$_NET[211] = array('255.248.0.0','255.252.0.0','255.254.0.0','255.255.0.0','255.255.128.0','255.255.192.0','255.255.224.0','255.255.240.0');
    
$_NET[218] = array('255.240.0.0');
    
$_NET[220] = array('255.240.0.0','255.248.0.0','255.252.0.0','255.254.0.0');
    
$_NET[221] = array('255.240.0.0','255.248.0.0','255.255.0.0');
    
$_NET[222] = array('255.240.0.0','255.248.0.0','255.254.0.0','255.255.0.0');

    if(
$ipa) return $_NET[$ipa];
    else return 
$_NET// total, array
  
}

  function &
ispmap($nnetwork=0)
  {
    
$_ISP['989855744'] = array('59.0.0.0','59.31.255.255','255.224.0.0','한국통신','KORNET');
    
$_ISP['1028128768'] = array('61.72.0.0','61.79.255.255','255.248.0.0','한국통신','KORNET');
    
$_ISP['1028653056'] = array('61.80.0.0','61.83.255.255','255.252.0.0','한국통신','KORNET');
    
$_ISP['1028915200'] = array('61.84.0.0','61.85.255.255','255.254.0.0','한국통신','KORNET');
    
$_ISP['2105540608'] = array('125.128.0.0','125.159.255.255','255.224.0.0','한국통신','KORNET');
    
$_ISP['2156265472'] = array('128.134.0.0','128.134.255.255','255.255.0.0','한국통신','KORNET');
    
$_ISP['2466643968'] = array('147.6.0.0','147.6.255.255','255.255.0.0','한국통신','KORNET');
    
$_ISP['2826829824'] = array('168.126.0.0','168.126.255.255','255.255.0.0','한국통신','KORNET');
    
$_ISP['2834825216'] = array('168.248.0.0','168.249.255.255','255.254.0.0','한국통신','KORNET');
    
$_ISP['3420717056'] = array('203.228.0.0','203.228.127.255','255.255.128.0','한국통신','KORNET');
    
$_ISP['3420815360'] = array('203.229.128.0','203.229.255.255','255.255.128.0','한국통신','KORNET');
    
$_ISP['3420889088'] = array('203.230.160.0','203.230.191.255','255.255.224.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3420979200'] = array('203.232.0.0','203.232.127.255','255.255.128.0','한국통신','KORNET');
    
$_ISP['3421134848'] = array('203.234.96.0','203.234.111.255','255.255.240.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3421143040'] = array('203.234.128.0','203.234.255.255','255.255.128.0','한국통신','KORNET');
    
$_ISP['3421252608'] = array('203.236.44.0','203.236.47.255','255.255.252.0','한국통신','KORNET');
    
$_ISP['3421253632'] = array('203.236.48.0','203.236.63.255','255.255.240.0','한국통신','KORNET');
    
$_ISP['3421257728'] = array('203.236.64.0','203.236.127.255','255.255.192.0','한국통신','KORNET');
    
$_ISP['3421634560'] = array('203.242.0.0','203.242.31.255','255.255.224.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3421700096'] = array('203.243.0.0','203.243.63.255','255.255.192.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3422109696'] = array('203.249.64.0','203.249.95.255','255.255.224.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3422224384'] = array('203.251.0.0','203.251.127.255','255.255.128.0','한국통신','KORNET');
    
$_ISP['3422257152'] = array('203.251.128.0','203.251.191.255','255.255.192.0','한국통신','KORNET');
    
$_ISP['3422445568'] = array('203.254.96.0','203.254.127.255','255.255.224.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3529113600'] = array('210.90.0.0','210.90.255.255','255.255.0.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3529179136'] = array('210.91.0.0','210.91.255.255','255.255.0.0','한국통신','KORNET');
    
$_ISP['3529244672'] = array('210.92.0.0','210.92.63.255','255.255.192.0','한국통신','KORNET');
    
$_ISP['3529441280'] = array('210.95.0.0','210.95.255.255','255.255.0.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3529506816'] = array('210.96.0.0','210.96.127.255','255.255.128.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3529572352'] = array('210.97.0.0','210.97.127.255','255.255.128.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3529703424'] = array('210.99.0.0','210.99.255.255','255.255.0.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3529801728'] = array('210.100.128.0','210.100.255.255','255.255.128.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3529850880'] = array('210.101.64.0','210.101.127.255','255.255.192.0','한국통신','KORNET');
    
$_ISP['3529965568'] = array('210.103.0.0','210.103.127.255','255.255.128.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3530031104'] = array('210.104.0.0','210.104.255.255','255.255.0.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3530096640'] = array('210.105.0.0','210.105.255.255','255.255.0.0','한국통신','KORNET');
    
$_ISP['3530186752'] = array('210.106.96.0','210.106.127.255','255.255.224.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3530194944'] = array('210.106.128.0','210.106.191.255','255.255.192.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3530620928'] = array('210.113.0.0','210.113.255.255','255.255.0.0','한국통신','KORNET');
    
$_ISP['3531075584'] = array('210.119.240.0','210.119.255.255','255.255.240.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3531177984'] = array('210.121.128.0','210.121.255.255','255.255.128.0','한국통신','KORNET');
    
$_ISP['3531276288'] = array('210.123.0.0','210.123.255.255','255.255.0.0','한국통신','KORNET');
    
$_ISP['3531472896'] = array('210.126.0.0','210.126.127.255','255.255.128.0','한국통신','KORNET');
    
$_ISP['3534880768'] = array('210.178.0.0','210.179.255.255','255.254.0.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3535208448'] = array('210.183.0.0','210.183.255.255','255.255.0.0','한국통신','KORNET');
    
$_ISP['3536584704'] = array('210.204.0.0','210.204.255.255','255.255.0.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3537436672'] = array('210.217.0.0','210.217.127.255','255.255.128.0','한국통신','KORNET');
    
$_ISP['3537764352'] = array('210.222.0.0','210.223.255.255','255.254.0.0','한국통신','KORNET');
    
$_ISP['3542188032'] = array('211.33.128.0','211.33.255.255','255.255.128.0','한국통신','KORNET');
    
$_ISP['3542220800'] = array('211.34.0.0','211.34.255.255','255.255.0.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3542319104'] = array('211.35.128.0','211.35.255.255','255.255.128.0','한국통신','KORNET');
    
$_ISP['3542482944'] = array('211.38.0.0','211.38.255.255','255.255.0.0','한국통신','KORNET');
    
$_ISP['3542564864'] = array('211.39.64.0','211.39.95.255','255.255.224.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3542581248'] = array('211.39.128.0','211.39.159.255','255.255.224.0','한국통신','KORNET');
    
$_ISP['3542794240'] = array('211.42.192.0','211.42.223.255','255.255.224.0','한국통신','KORNET');
    
$_ISP['3542810624'] = array('211.43.0.0','211.43.127.255','255.255.128.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3542843392'] = array('211.43.128.0','211.43.143.255','255.255.240.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3543007232'] = array('211.46.0.0','211.46.255.255','255.255.0.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3543138304'] = array('211.48.0.0','211.48.255.255','255.255.0.0','한국통신','KORNET');
    
$_ISP['3543334912'] = array('211.51.0.0','211.51.255.255','255.255.0.0','한국통신','KORNET');
    
$_ISP['3543531520'] = array('211.54.0.0','211.55.255.255','255.254.0.0','한국통신','KORNET');
    
$_ISP['3543728128'] = array('211.57.0.0','211.57.255.255','255.255.0.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3544154112'] = array('211.63.128.0','211.63.255.255','255.255.128.0','한국통신','KORNET');
    
$_ISP['3546808320'] = array('211.104.0.0','211.107.255.255','255.252.0.0','한국통신','KORNET');
    
$_ISP['3547463680'] = array('211.114.0.0','211.114.255.255','255.255.0.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3547561984'] = array('211.115.128.0','211.115.191.255','255.255.192.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3551920128'] = array('211.182.0.0','211.182.255.255','255.255.0.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3552051200'] = array('211.184.0.0','211.185.255.255','255.254.0.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3552575488'] = array('211.192.0.0','211.199.255.255','255.248.0.0','한국통신','KORNET');
    
$_ISP['3554148352'] = array('211.216.0.0','211.223.255.255','255.248.0.0','한국통신','KORNET');
    
$_ISP['3554672640'] = array('211.224.0.0','211.231.255.255','255.248.0.0','한국통신','KORNET');
    
$_ISP['3555475456'] = array('211.236.64.0','211.236.127.255','255.255.192.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3556245504'] = array('211.248.0.0','211.248.255.255','255.255.0.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3556376576'] = array('211.250.0.0','211.251.255.255','255.254.0.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3556507648'] = array('211.252.0.0','211.253.255.255','255.254.0.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3556720640'] = array('211.255.64.0','211.255.127.255','255.255.192.0','한국통신-광대역통합망','PUBNET');
    
$_ISP['3666870272'] = array('218.144.0.0','218.159.255.255','255.240.0.0','한국통신','KORNET');
    
$_ISP['3695575040'] = array('220.70.0.0','220.71.255.255','255.254.0.0','한국통신','KORNET');
    
$_ISP['3695706112'] = array('220.72.0.0','220.79.255.255','255.248.0.0','한국통신','KORNET');
    
$_ISP['3696230400'] = array('220.80.0.0','220.95.255.255','255.240.0.0','한국통신','KORNET');
    
$_ISP['3698589696'] = array('220.116.0.0','220.119.255.255','255.252.0.0','한국통신','KORNET');
    
$_ISP['3698851840'] = array('220.120.0.0','220.127.255.255','255.248.0.0','한국통신','KORNET');
    
$_ISP['3717201920'] = array('221.144.0.0','221.159.255.255','255.240.0.0','한국통신','KORNET');
    
$_ISP['3718250496'] = array('221.160.0.0','221.167.255.255','255.248.0.0','한국통신','KORNET');
    
$_ISP['3718774784'] = array('221.168.0.0','221.168.255.255','255.255.0.0','한국통신','KORNET');
    
$_ISP['3730833408'] = array('222.96.0.0','222.111.255.255','255.240.0.0','한국통신','KORNET');
    
$_ISP['3731881984'] = array('222.112.0.0','222.119.255.255','255.248.0.0','한국통신','KORNET');
    
$_ISP['3732406272'] = array('222.120.0.0','222.121.255.255','255.254.0.0','한국통신','KORNET');
    
$_ISP['3732537344'] = array('222.122.0.0','222.122.255.255','255.255.0.0','한국통신','KORNET');

    if(
$nnetwork) return $_ISP["$nnetwork"];
    else return 
$_ISP// total, array
  
}
// end of class
?>