#!/usr/bin/perl
# Dhcphosts
# Special thanx for dreamer_538
#require "Abills/nas.pl";

use Dhcphosts;
my $Dhcphosts = Dhcphosts->new($db, $admin, \%conf);

use Abills::Base;
use Socket;

#use Spreadsheet::WriteExcel::Simple;
#use Switch;


#*******************************************************************
# 
#*******************************************************************
sub dhcphosts_periodic {
	
	
}


#*******************************************************************
# 
#*******************************************************************
sub dhcphosts_mac_block_show {
   my ($attr) = @_;
   my $debug = $attr->{DEBUG} || 0;
   my $ethers_content = "# $DATE $TIME\n";
   
   $LIST_PARAMS{STATUS}=0 if (! $conf{DHCPHOSTS_IPGUARD_TYPE} || $conf{DHCPHOSTS_IPGUARD_TYPE} ne 'ipsentinel');
   my @pair_status = ('ACCEPT', 'DENY');
   
   my $list = $Dhcphosts->hosts_list({ %LIST_PARAMS, 
   	                                   PAGE_ROWS  => 1000000 });
   
	 foreach my $line (@$list) {
	    next if ($line->[6] eq '');
	    my $ip = ($conf{DHCPHOSTS_IPGUARD_FORMAT} && $conf{DHCPHOSTS_IPGUARD_FORMAT} eq 'MAC') ? '0.0.0.0' : $line->[2]; 
      if ($conf{DHCPHOSTS_IPGUARD_TYPE} && $conf{DHCPHOSTS_IPGUARD_TYPE} eq 'ipsentinel') {
	      $ethers_content .= "$ip $line->[6]  $pair_status[$line->[10]] \n";
	     }
	    else {
	    	$ethers_content .= "$line->[6] $ip # $line->[1]\n";
	     }
    };

	return  $ethers_content;
}

#*******************************************************************
# 
#*******************************************************************
sub dhcphosts_mac_block_make {
	my ($attr) = shift;

	if (! $attr->{ETHER_CONTENT}) {
	  $attr->{ETHER_CONTENT} = dhcphosts_mac_block_show();			
	 }

	my $debug = $attr->{DEBUG} || 0;
	my $debug_output = "$attr->{ETHER_CONTENT}\n" if ($debug > 1);
	$DEBUG .= $debug_output;
	
	if ($debug > 2) {
		
		return 1;
	 }
	
	open(FILE, ">$var_dir/ipguard") || return "Can't open '$var_dir/ipguard' $!";
	  print FILE "$attr->{ETHER_CONTENT}";
	close(FILE);

  $DEBUG .= $debug_output;
  
	return 1;
}

#*******************************************************************
# 
#*******************************************************************
sub dhcphosts_mac_block {
  
  
  if ($FORM{MAKE}) {
  	my $ret = dhcphosts_mac_block_make($FORM{ETHER_CONTENT});
  	if ($ret eq '1') {
  	  print $html->message('info', $_INFO, "$_CHANGED '$var_dir/ipguard'");
  	 }
    else {
    	print $html->message('err', $_ERROR, "$ret");
     }
   }
	
	my $ethers_content = dhcphosts_mac_block_show();	
  $html->tpl_show(_include('dhcphosts_ipguard', 'Dhcphosts'), { ETHER_CONTENT => "$ethers_content",
	 	                                                            FILE          => "$var_dir/ipguard"
	 	                                                           });
	 
}

#*******************************************************************
# Show dhcpd.leases information
#*******************************************************************
sub dhcphosts_monitor {

 if (! $conf{DHCPHOSTS_LEASES}) {
  	print $html->message('err', $_ERROR, "$_NOT_EXIST ". '$conf{DHCPHOSTS_LEASES}');
  	return 0;
  }
 
 if ($FORM{ping}) {
  if ($FORM{ping} =~ /^(\d){1,3}\.(\d){1,3}\.(\d){1,3}\.(\d){1,3}$/) {
    my $res = `/sbin/ping -c 5 $FORM{ping}`;
    $html->message('info', $_INFO,  "Ping  $FORM{ping}<br>Result:<br><pre>$res</pre>");
   }
  else {
    $html->message('err', $_ERROR,  "$ERR_WRONG_DATA");
   }
 }

 
 #Get info from dhcpd.leased 
 my $dhcp_hash = leaseparse($conf{DHCPHOSTS_LEASES});
 #Get user info from DB

 my $hosts_list = $Dhcphosts->hosts_list({ IPS       => join(", ",  keys %$dhcp_hash), 
 	                                         PAGE_ROWS => 100000 
 	                                       });
 
 my $table = $html->table({ width      => '100%',
                     			  caption    => "$_HOSTS",
				                    title      => ["$_USER", 'IP', "$_START", "MAC", "$_HOSTS",  "abandoned","-","-", '-'],
				                    cols_align => ['left', 'right', 'right', 'right', 'left', 'center:noprint', 'center:noprint','center:noprint', 'center:noprint' ]
                          });

 $table->{rowcolor}=$_COLORS[0];
 $table->{extra}="colspan='6'";
 $table->addrow("$_TOTAL: $Dhcphosts->{TOTAL}");
 $table->{extra}=undef;
 $table->{rowcolor}=undef;


 foreach my $line (@$hosts_list) {
      $table->addrow(
                     $html->button("$line->[1]", "index=15&UID=$line->[12]"), 
                     $line->[2],
                     $dhcp_hash->{$line->[2]}{starts},
                     $dhcp_hash->{$line->[2]}{hardware},
                     $dhcp_hash->{$line->[2]}{'client-hostname'},
                     $dhcp_hash->{$line->[2]}{abandoned},
                     "(". $html->button('P', "index=$index&ping=$line->[2]", { TITLE => 'ping' }) .")",
                     '',
                     ''
                     );

     delete $dhcp_hash->{$line->[2]};
  }
 
 my @unregistred = sort keys %$dhcp_hash;
 $table->{rowcolor}=$_COLORS[0];
 $table->{extra}="colspan='6'";
 $table->addrow("$_NOT_EXIST $_TOTAL: ". ($#unregistred + 1));
 $table->{extra}=undef;
 $table->{rowcolor}=undef;

 foreach my $ip ( @unregistred ) {
      $table->addrow(
                     '', 
                     $ip,
                     $dhcp_hash->{$ip}{starts},
                     $dhcp_hash->{$ip}{hardware},
                     $dhcp_hash->{$ip}{'client-hostname'},
                     $dhcp_hash->{$ip}{abandoned},
                     "(". $html->button('P', "index=$index&ping=$ip", { TITLE => 'ping' }) .")",
                     '',
                     ''
                     );
  }
	
 print $table->show();	

}


#*******************************************************************
# Parse dhcpd.leases
#*******************************************************************
sub leaseparse {
   my $logfile = shift;
   my ( %list, $ip );

   if (-f $logfile ) {
   	return \%list; 
   }

   open (FILE, $logfile) or print "Can't read file '$logfile' $!";
   
   
   while (<FILE>) {
      next if /^#|^$/;

      if (/^lease (\d+\.\d+\.\d+\.\d+)/) {
         $ip = $1; 
         $list{$ip}{ip}=sprintf("%-17s", $ip);
         $ips_hash{$ip}=$list{$ip}{ip};
       }
      elsif (/^\s*client-hostname "(.*)";/) {
      	 $list{$ip}{'client-hostname'}=sprintf("%-17s", $1);
       }

      /^\s*hardware ethernet (.*);/ && ( $list{$ip}{hardware}=sprintf("%-19s", $1) );
      /^\s*starts \d (.*);/ && (  $list{$ip}{starts}=sprintf("%-21s", $1) );
      /^\s*(abandoned).*/ && (    $list{$ip}{abandoned}=sprintf("%-19s", $1) );
   }

   close FILE;

   return \%list;
}



#*******************************************************************
# 
# 
#*******************************************************************
sub dhcphosts_networks {


  $Dhcphosts->{ACTION}='add';
  $Dhcphosts->{ACTION_LNG}=$_ADD;

  if ($FORM{NAME} and $FORM{NAME} !~ /^[0-9a-zA-Z_\-]+$/) {
  	print $html->message('err', $_ERROR, "$_HOSTS_NETWORKS_NAME - '$FORM{NAME}' $ERR_WRONG_DATA");
  	return 0;
   }
  
  if ($FORM{config}) {
  	dhcphosts_config({ NETWORKS => $FORM{IDS} });
  	
  	return 0;
   }
  elsif ($FORM{add}) {
    $FORM{ROUTERS}='0.0.0.0' if (! $FORM{ROUTERS}) ;
    $Dhcphosts->network_add({ %FORM });
    if (! $Dhcphosts->{errno}) {
	    $html->message('info', $_ADDED, "$_HOSTS_NETWORKS_NAME [$FORM{NAME}] ");
	   }
	 }
  elsif ($FORM{change}) {
	  $FORM{MASK} = ip2int($FORM{MASK});
	  $FORM{NETWORK} = ip2int($FORM{NETWORK});
    $FORM{ROUTERS} = ip2int($FORM{ROUTERS});

	  $Dhcphosts->network_change({ %FORM });
    
    if (! $Dhcphosts->{errno}) {
	    $html->message('info', $_CHANGED, "$_CHANGED [$FORM{ID}] $FORM{NAME} ");
	   }
	 }
  elsif ($FORM{chg}) {
	  $Dhcphosts->network_info( $FORM{chg} );

    $Dhcphosts->{ACTION}='change';
    $Dhcphosts->{ACTION_LNG}=$_CHANGE;

    if (! $Dhcphosts->{errstr}) {
	    $html->message('info', $_CHANGE, "$_CHANGE [$FORM{chg}] ");
	   }
   }
  elsif ($FORM{del} && $FORM{is_js_confirmed}) {
   	$Dhcphosts->network_del($FORM{del});
    if (! $Dhcphosts->{errstr}) {
	    $html->message('info', $_DELETED, "$_DELETED [$FORM{del}] ");
	   }
   }

  if ($Dhcphosts->{errno}) {
    $html->message('err', $_ERROR, "[$Dhcphosts->{errno}] $err_strs{$Dhcphosts->{errno}}");
  }


   $html->tpl_show(_include('dhcphosts_network', 'Dhcphosts'), $Dhcphosts);
   my $list = $Dhcphosts->networks_list({ %LIST_PARAMS });

   my $table = $html->table( { width      => '100%',
				caption    => "$_HOSTS_NETWORKS",
				title      => ['#', "$_HOSTS_NETWORKS_NAME", "$_HOSTS_NETWORKS_NET", "$_HOSTS_NETWORKS_COORDINATOR", 
				  "$_HOSTS_NETWORKS_COORDINATOR_PHONE","-","-", '-'],
				cols_align => ['left', 'left', 'left', 'left', 'left', 'center:noprint', 'center:noprint','center:noprint',
				 'center:noprint' ],
				qs         => $pages_qs,
				pages      => $Dhcphosts->{TOTAL}
    });

    foreach my $line (@$list) {
      $table->addrow(
	    $line->[0]. $html->form_input('IDS', "$line->[0]", { TYPE => 'checkbox', }), 
	    $line->[1],
	    $line->[2]."/". $line->[3],
	    $line->[4],
	    $line->[5],
	    $html->button($_DEL, "index=$index&del=$line->[0]", { MESSAGE => "$_DEL $line->[0]?" }),
	    $html->button($_CHANGE, "index=$index&chg=$line->[0]"),
	    $html->button("Routes", "index=".($index+1)."&NET_ID=$line->[0]") 	    	    
     );
    };


   print $html->form_main({ CONTENT => $table->show(),
	                          HIDDEN  => { index  => $index },
                            SUBMIT  => { config => 'dhcp.conf' }
                          });

  
  $table = $html->table( { width      => '100%',
                           cols_align => ['right', 'right'],
                           rows       => [ [ "$_TOTAL:", "<b>$Dhcphosts->{TOTAL}</b>" ] ]
                       } );
  print $table->show();
};



#*******************************************************************
# 
# 
#*******************************************************************
sub dhcphosts_hosts {
  
  
  if (! $FORM{UID}) {
    $Dhcphosts->{NETWORKS_SEL} = dhcphosts_network_sel();
    form_search({ SEARCH_FORM => $html->tpl_show(_include('dhcphosts_search', 'Dhcphosts'), 
  	                                  { %$Dhcphosts, %FORM }, 
  	                                  { notprint => 1 })  
	            });
   }
  
  my $list = $Dhcphosts->hosts_list({ %LIST_PARAMS });
  my $table = $html->table({ width      => '100%',
				                     caption    => "$_HOSTS_USER",
				                     title      => ['#', "$_USER",  "IP", "$_HOSTS_HOSTNAME","$_HOSTS_NETWORKS_NET", "$_HOSTS_MAC", "$_STATUS", "$_HOSTS_BLOCKTIME", "$_HOSTS_BLOCKED","-","-"],
				                     cols_align => ['left', 'left', 'left','left', 'left', 'left','center:noprint','center:noprint'],
				                     qs         => $pages_qs,
				                     pages      => $Dhcphosts->{TOTAL}
                           });

  my $i=4;
  foreach my $line (@$list) {

   	$status=$line->[10]+$line->[8];
	  if ($status == 0)    { $table->{rowcolor}=$_COLORS[2]; }
	  elsif($status == 1 ) { $table->{rowcolor}=$_COLORS[6]; }
	  elsif($status == 2 ) { $table->{rowcolor}=$_COLORS[1]; }

	  if (($line->[11]==1) && $status) { $table->{rowcolor}=$_COLORS[0]; };

	  #$table->{rowcolor}=$_COLORS[$i];
    $table->addrow(
	    $line->[0], 
	    $html->button($line->[1], "index=15&UID=$line->[12]"),
	    $line->[2],
	    $line->[3],
	    $line->[5].": ".$line->[4],
	    $line->[6],
	    $line->[10]."/".$line->[8],
	    $line->[9],
	    $line->[7],
	    $html->button($_CHANGE, "index=$index&chg=$line->[0]&UID=$line->[12]"),
	    $html->button($_DEL, "index=$index&del=$line->[0]&UID=$line->[12]", { MESSAGE => "$_DEL $line->[0]?" })
     );
	   $i++;
   };
  print $table->show();	

  $table = $html->table( { width      => '100%',
                           cols_align => ['right', 'right'],
                           rows       => [ [ "$_TOTAL:", "<b>$Dhcphosts->{TOTAL}</b>" ] ]
                       } );
  print $table->show();
}


#**********************************************************
#
#**********************************************************
sub dhcphosts_routes {

  $Dhcphosts->{ACTION}='add';
  $Dhcphosts->{ACTION_LNG}=$_ADD;
  


  if ($FORM{add}) {
    $Dhcphosts->route_add({ %FORM });
    if (! $Dhcphosts->{errno}) {
	    $html->message('info', $_ADDED, "$_ADDED [$FORM{SRC}] ");
	   }
	 }
  elsif ($FORM{change}) {
	  $FORM{SRC}     = ip2int($FORM{SRC});
	  $FORM{MASK} = ip2int($FORM{MASK});
	  $FORM{ROUTER}  = ip2int($FORM{ROUTER});

	  $Dhcphosts->route_change({ %FORM });
    
    if (! $Dhcphosts->{errno}) {
	    $html->message('info', $_CHANGED, "$_CHANGED [$FORM{ID}] $FORM{SRC} ");
	   }
	 }
  elsif ($FORM{chg}) {
	  $Dhcphosts->route_info( $FORM{chg} );

    $Dhcphosts->{ACTION}='change';
    $Dhcphosts->{ACTION_LNG}=$_CHANGE;

    if (! $Dhcphosts->{errstr}) {
	    $html->message('info', $_CHANGE, "$_CHANGE [$FORM{chg}] ");
	   }
   }
  elsif ($FORM{del} && $FORM{is_js_confirmed}) {
   	$Dhcphosts->route_del($FORM{del});
    if (! $Dhcphosts->{errstr}) {
	    $html->message('info', $_DELETED, "$_DELETED [$FORM{del}] ");
	   }
   }

  if ($Dhcphosts->{errno}) {
    $html->message('err', $_ERROR, "[$Dhcphosts->{errno}] $err_strs{$Dhcphosts->{errno}}");
  }

 
    $html->tpl_show(_include('dhcphosts_routes', 'Dhcphosts'), $Dhcphosts);


    $LIST_PARAMS{NET_ID} = $FORM{NET_ID};
    my $list = $Dhcphosts->routes_list({ %LIST_PARAMS });

    my $table = $html->table( { width      => '100%',
				caption    => "Routes",
				title      => ['#', "$_HOSTS_NETWORKS_NAME", "$_HOSTS_NETWORKS_NET", "NETMASK","$_HOSTS_ROUTER" ,"-","-"],
				cols_align => ['left', 'left', 'right','right', 'right', 'center:noprint','center:noprint'],
				qs         => $pages_qs,
				pages      => $Dhcphosts->{TOTAL}
    });


    foreach my $line (@$list) {
      $table->addrow(
	    $line->[0], 
	    $line->[1].":".$line->[5],
	    $line->[2],
	    $line->[3],
	    $line->[4],
	    $html->button($_DEL, "index=$index&del=$line->[0]&NET_ID=".$FORM{NET_ID}, { MESSAGE => "$_DEL $line->[0]?" }),
	    $html->button($_CHANGE, "index=$index&chg=$line->[0]&NET_ID=".$FORM{NET_ID})
      );
    };

   print $table->show();

  $table = $html->table( { width      => '100%',
                           cols_align => ['right', 'right'],
                           rows       => [ [ "$_TOTAL:", "<b>$Dhcphosts->{TOTAL}</b>" ] ]
                       } );
  print $table->show();

};

#**********************************************************
#
#**********************************************************
sub dhcphosts_user {
  $Dhcphosts->{ACTION}='add';
  $Dhcphosts->{ACTION_LNG}=$_ADD;


  if ($FORM{add}) {
    $Dhcphosts->host_add({ %FORM });
    if (! $Dhcphosts->{errno}) {
	    $html->message('info', $_ADDED, "$_ADDED [$FORM{HOSTNAME}] ");
      dhcphosts_config({ NETWORKS => $FORM{NETWORK}, reconfig => 1 });
	   }
	 }
  elsif ($FORM{change}) {
	  $Dhcphosts->host_change({ %FORM });
    
    if (! $Dhcphosts->{errno}) {
	    $html->message('info', $_CHANGED, "$_CHANGED [$FORM{ID}] $FORM{NAME} ");
	    dhcphosts_config({ NETWORKS => $FORM{NETWORK}, reconfig => 1 });
	   }
	 }
  elsif ($FORM{chg}) {
	  $Dhcphosts->host_info( $FORM{chg} );

    $FORM{NETWORK}=$Dhcphosts->{NETWORK};
    $Dhcphosts->{ACTION}='change';
    $Dhcphosts->{ACTION_LNG}=$_CHANGE;

    if (! $Dhcphosts->{errstr}) {
	    $html->message('info', $_CHANGE, "$_CHANGE [$FORM{chg}] ");
	   }
   }
  elsif ($FORM{del} && $FORM{is_js_confirmed}) {
   	$Dhcphosts->host_del({ ID => $FORM{del} });
    if (! $Dhcphosts->{errstr}) {
	    $html->message('info', $_DELETED, "$_DELETED [$FORM{del}] ");
	   }
   }
  
  if ($Dhcphosts->{errno}) {
    $html->message('err', $_ERROR, "[$Dhcphosts->{errno}] $err_strs{$Dhcphosts->{errno}}");
  }

   
  
  $Dhcphosts->{NETWORKS_SEL} = dhcphosts_network_sel();



  $Dhcphosts->{STATUS_SEL} = $html->form_select('STATUS',
			                                   {   SELECTED => $FORM{STATUS},
		                                         SEL_HASH => { 0   => "Not Set",
                                                           -1  => $_DISABLE,
                                                           1   => "$_ENABLE" } 
                                          });

  $Dhcphosts->{DISABLE}=' checked' if($Dhcphosts->{DISABLE});

  $html->tpl_show(_include('dhcphosts_host', 'Dhcphosts'), { %{ $Dhcphosts->host_defaults() },  %$Dhcphosts }); 
  dhcphosts_hosts();

};


#**********************************************************
#
#**********************************************************
sub dhcphosts_network_sel {
	
  my $list=$Dhcphosts->networks_list();
  if ($Hosts->{errno}) {
   	print $html->message('err', $_ERROR, "[$Hosts->{errno}] $err_strs{$Hosts->{errno}}");
   	return 0;
   }

  my %networks = ();
  foreach my $line (@$list) {
    $networks{$line->[0]}=$line->[1]."(".$line->[2]."/".$line->[3].")";
   }; 

  return $html->form_select('NETWORK', 	
                                    {
    	                               SELECTED => $FORM{NETWORK},
		                                 SEL_HASH => \%networks 
		                                }
		                        );
}

#**********************************************************
#
#**********************************************************
sub dhcphosts_config {
  my ($attr) = @_;

  if (! $attr->{NETWORKS}) {
  	$html->message('err', $_ERROR, "$ERR_WRONG_DATA");
  	return 0;
   }

  my %INFO = ();
  my @networks = split(/, /, $attr->{NETWORKS});

foreach my $NET_ID (@networks) {
  my %curnet = ();

  $Dhcphosts->network_info($NET_ID);

  $INFO{DNS}=$Dhcphosts->{DNS};
  $INFO{DOMAINNAME}="\"$Dhcphosts->{DOMAINNAME}\"";    
  $INFO{ROUTERS}="option routers $Dhcphosts->{ROUTERS};" if ($Dhcphosts->{ROUTERS} ne '0.0.0.0');
  $INFO{DATETIME}="$DATE $TIME";
  
  $INFO{NETWORK_NAME}=$Dhcphosts->{NAME};
  $INFO{BLOCK_NETWORK}=$Dhcphosts->{BLOCK_NETWORK};
  $INFO{BLOCK_MASK}=$Dhcphosts->{BLOCK_MASK};
  $INFO{NETWORK}=$Dhcphosts->{NETWORK};
  $INFO{NETWORK_MASK}=$Dhcphosts->{MASK};
  

  my $list = $Dhcphosts->routes_list({ NET_ID => $NET_ID });  
  
  if ($Dhcphosts->{TOTAL} > 0) {
    my $routes="";
 
    foreach my $line (@$list) {
      #($src,$mask,$router)=$sth->fetchrow_array()){
      my $src = $line->[2];
      my $mask = $line->[3]; 
      my $router = $line->[4];
      #print "// $src,$mask,$router\n";
      
      my @ip=split(/\./,$src);
      my @ip2=split(/\./,$router);    
      $mask = mask2bitlen($mask);
      $routes.=$mask;
        
      for(my $i=0;$i<$mask/8;$i++) {
     	  $routes.=", $ip[$i]";
       };
      $routes.=", ".join(", ",@ip2).",\n";
     }

    chop $routes;
    chop $routes;
    $routes.=";";
  
    $INFO{NET_ROUTES}="option ms-classless-static-routes $routes";
   }


print $INFO{NET_ROUTES};

  $INFO->{NETWORK}='';
  $list = $Dhcphosts->hosts_list({ NETWORK => $NET_ID, STATUS => 0, PAGE_ROWS => 100000 });
  
  foreach my $line (@$list) {
    $INFO{HOSTS} .= $html->tpl_show(_include('dhcphosts_dhcp_conf_host', 'Dhcphosts',), { 
    	  MAC      => $line->[6],
        IP       => $line->[2],
        ROUTERS  => ($Dhcphosts->{ROUTERS} ne '0.0.0.0') ? $Dhcphosts->{ROUTERS} : convert_ip("0.0.0.1", '', $Dhcphosts),
        LOGIN    => $line->[1],
        HOSTNAME => $line->[3]
    	}, { notprint => 1 });
   }
  
}



if ($attr->{reconfig} || $FORM{reconfig} ) {
	$conf{DHCPHOSTS_CONFIG} = "/usr/local/etc/dhcpd.conf" if (! $conf{DHCPHOSTS_CONFIG}) ;

  my $tpl = $html->tpl_show(_include('dhcphosts_dhcp_conf_main', 'Dhcphosts'), \%INFO, { notprint => 'yes' });	 
	open(FILE, ">$conf{DHCPHOSTS_CONFIG}") || print "Can't open file '$conf{DHCPHOSTS_CONFIG}' $!";
	   print FILE $tpl;
  close(FILE);
  
  dhcphosts_reconfigure({ DEBUG => $FORM{DEBUG} });
  
  print $html->message('info', $_INFO, "DHCP $_RECONFIGURE '$conf{DHCPHOSTS_CONFIG}'");
 }
else {
  print "<textarea cols=90 rows=20>";
  $html->tpl_show(_include('dhcphosts_dhcp_conf_main', 'Dhcphosts'), \%INFO);
	print "</textarea>";
	print $html->form_main(	{  HIDDEN  => { index  => $index,
		                                     IDS    => $FORM{IDS},
		                                     config => 'dhcp.conf' },
                            SUBMIT  => { reconfig => $_RECONFIGURE }
                         } );
	
}
	
}


#**********************************************************
# Get bit count from net mask
#**********************************************************
sub dhcphosts_reconfigure {
  my ($attr) = @_;

  my $debug = $attr->{DEBUG} || 0;

  if (! $conf{DHCPHOSTS_RECONFIGURE}) {
  	print $html->message('err', $_ERROR, "Can't find reconfiguration command ". '"$conf{DHCPHOSTS_RECONFIGURE}"');
  	return 0;
   }
 
  my $res = `$conf{DHCPHOSTS_RECONFIGURE}`;
  print $res if ($debug > 1);

  return 0;
}


#**********************************************************
#
#**********************************************************
sub convert_ip {
    my ($cid, $blocked, $attr)=@_;

    if (! $Dhcphosts->{NETWORK}) {
      $Dhcphosts->network_info($net_id);
     }

    my @ip = split(/\./, $cid);
    my @ip2 = ();
    if ($blocked) { 
    	@ip2=split(/\./,  $Dhcphosts->{BLOCK_NETWORK}); 
     }
    else {
    	@ip2=split(/\./, $Dhcphosts->{NETWORK});
     }

    return "$ip2[0].$ip2[1].$ip2[2].$ip[3]";
}

#**********************************************************
# Get bit count from net mask
#**********************************************************
sub mask2bitlen {
  my ($netmask) = @_;

  my @mask = split(/\./, $netmask);
  my $bitlen = 0;
  foreach my $line (@mask) {
    if ($line == 255) {
      $bitlen += 8;
     }
    else {
      last;
     }
   }
  return $bitlen;
}





1
