require "Netlist.pm";
Netlist->import();
my $Netlist = Netlist->new($db, $admin, \%conf);
use Socket;




#**********************************************************
#
#**********************************************************
sub netlist_ip_calc {

  my $mask = 0b0000000000000000000000000000001;

  if (! $FORM{MASK} && $FORM{MASK_BITS}) {
    $FORM{MASK} = int2ip( 4294967296 - sprintf("%d", $mask << (32  - $FORM{MASK_BITS})) );
   }

  $Netlist->{MASK_SEL}=$html->form_select('MASK', 
                                          { 
 	                                          SELECTED    => $FORM{MASK},
 	                                          SEL_ARRAY   => [ '',
 	                                          '255.0.0.0', 	            

 	                                          '255.128.0.0',
 	                                          '255.192.0.0',
 	                                          '255.224.0.0',
 	                                          '255.240.0.0',
 	                                          '255.248.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',
 	                                          '255.255.248.0',
 	                                          '255.255.254.0',

                                            '255.255.255.0',
 	                                          '255.255.255.128',
 	                                          '255.255.255.192',
 	                                          '255.255.255.224',
 	                                          '255.255.255.240',
 	                                          '255.255.255.248',
 	                                          '255.255.255.252',
 	                                          '255.255.255.254',
 	                                          '255.255.255.255',
                                            ],
 	                                        });


  my @SUBNET_NUMBER = ();
  my @HOSTS_NUMBER  = ();
  my @MASK_BITS     = ();
  
  for(my $i=0; $i<=16; $i++) {
  	push @SUBNET_NUMBER, ($mask << $i);
  	push @HOSTS_NUMBER, sprintf("%d", 254 / ($mask << $i) - 1);
  	push @MASK_BITS, 32-$i;

#  	
#  	print sprintf("$i - Subnets: %d / Hosts: %d<br>",  $mask << $i, 254 / ($mask << $i) - 1 );
   }


  $Netlist->{SUBNET_NUMBER_SEL}=$html->form_select('SUBNET_NUMBER', 
                                          { 
 	                                          SELECTED    => $FORM{SUBNET_NUMBER},
 	                                          SEL_ARRAY   => \@SUBNET_NUMBER,
 	                                        });

  $Netlist->{HOSTS_NUMBER_SEL}=$html->form_select('HOSTS_NUMBER', 
                                          { 
 	                                          SELECTED    => $FORM{HOSTS_NUMBER},
 	                                          SEL_ARRAY   => \@HOSTS_NUMBER,
 	                                        });

  $Netlist->{MASK_BITS_SEL}=$html->form_select('MASK_BITS', 
                                          { 
 	                                          SELECTED    => $FORM{MASK_BITS},
 	                                          SEL_ARRAY   => \@MASK_BITS,
 	                                        });

  $html->tpl_show(_include('netlist_ip_calc', 'Netlist'), { %$Netlist, %FORM }); 

if ($FORM{SHOW}) {
	my $table = $html->table( { width   => '100%',
                              caption => "IP Calc",
                              border  => 1,
                              title   => [SUBNET, MASK, $_SIZE, $_HOSTS, 'BRODCAST' ],
                              qs      => $pages_qs,
                           });
  
  my $ip_prefix = "$FORM{IP_D1}\.$FORM{IP_D2}\.$FORM{IP_D3}";
  my $int = ip2int("$ip_prefix\.0");
 
  if ($FORM{MASK_BITS}) {
     
   }
 
  if ($FORM{MASK} =~ /\.0$/ && $FORM{MASK} !~ /255\.0$/) {
  	$ip_prefix = "$FORM{IP_D1}\.$FORM{IP_D2}\.0";
  	$int = ip2int("$ip_prefix\.0\.0");
   }
  elsif ($FORM{MASK} =~ /\.0\.0$/ && $FORM{MASK} !~ /\.255\.255\.0$/) {
  	$ip_prefix = "$FORM{IP_D1}\.0\.0";
  	$int = ip2int("$ip_prefix\.0\.0\.0");
   }

  
  my $addr_count = ip2int('255.255.255.255') - ip2int("$FORM{MASK}") - 1;
  my $count = 2;
  
  for(my $i=0; $i<256 / ($addr_count + 2); $i++) {
  	my $sufix_ip = ($addr_count + 2) * $i;
        $int = ip2int("$ip_prefix".'.'. $sufix_ip);
  	$table->addrow("$ip_prefix".'.'. $sufix_ip,  
  	                "$FORM{MASK}",
  	                $addr_count,
  	                "$_FROM: $ip_prefix.". (($addr_count + 2) * $i + 1) ." $_TO: ". int2ip($int +  $addr_count),
  	                int2ip($int + $addr_count + 1)
  	               );
  	
   }

  
  


	print $table->show();
}

}

#**********************************************************
#
#**********************************************************
sub netlist_list {
  $Netlist->{ACTION}='add';
  $Netlist->{ACTION_LNG}=$_ADD;

  if ($FORM{ping}) {
 	  $FORM{ping} = int2ip($FORM{ping});
 	  if ($FORM{ping} =~ /^(\d){1,3}\.(\d){1,3}\.(\d){1,3}\.(\d){1,3}$/) {
      my $res = `$PING -c 5 $FORM{ping}`;
      $html->message('info', $_INFO,  "Ping  $FORM{ping}\nResult:\n". $html->per($res));
     }
    else {
      $html->message('err', $_ERROR,  "_WRONG_DATA");
     }
   }
  elsif($FORM{add}) {
    $Netlist->ip_add({ %FORM });
    if(! $Netlist->{errno}) {
      $html->message('info', $_INFO, "$_ADDED");
     }
    undef %FORM;
  }
  elsif($FORM{change}) {
    $FORM{DATE}=$DATE;
    $Netlist->ip_change({ %FORM });
    if(! $Netlist->{errno}) {
      $html->message('info', $_INFO, "$_CHANGED"); 
     }
    undef %FORM;
  }
 	elsif(defined($FORM{chg})) {
    $Netlist->ip_info($FORM{chg});
    if(! $Netlist->{errno}) {
      $html->message('info', $_INFO, "$_CHANGING");  	
     }
    $Netlist->{ACTION}='change';
    $Netlist->{ACTION_LNG}=$_CHANGE;
  }
  elsif(defined($FORM{del}) && defined($FORM{is_js_confirmed}) ) {
    $Netlist->ip_del($FORM{del});
      if(! $Netlist->{errno}) {
    	$html->message('info', $_INFO, "$_DELETED"); 
     }
  }

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

  my $groups = $Netlist->groups_list();

  $Netlist->{GROUP_SEL}=$html->form_select('GID', 
                                          { 
 	                                          SELECTED          => $Netlist->{GID},
 	                                          SEL_MULTI_ARRAY   => [@$groups],
 	                                          MULTI_ARRAY_KEY   => 3,
 	                                          MULTI_ARRAY_VALUE => 0,
 	                                          NO_ID             => 1
 	                                        });


  $Netlist->{STATE_SEL}=$html->form_select('STATUS', 
                                          { 
 	                                          SELECTED    => $Netlist->{STATUS},
 	                                          SEL_ARRAY   => \@status,
 	                                          ARRAY_NUM_ID => 1
 	                                        });


$Netlist->{GROUP_SEL}=$html->form_select('GID', 
                                          { 
 	                                          SELECTED          => $Netlist->{GID} || $FORM{GID},
 	                                          SEL_MULTI_ARRAY   => [['', $_ALL], @$groups],
 	                                          MULTI_ARRAY_KEY   => 3,
 	                                          MULTI_ARRAY_VALUE => 0,
 	                                          NO_ID             => 1,
 	                                          MAIN_MENU         => get_function_index('netlist_groups'),
 	                                        });

  $Netlist->{STATE_SEL}=$html->form_select('STATUS', 
                                          { 
 	                                          SELECTED    => $Netlist->{STATUS},
 	                                          SEL_ARRAY   => [@status],
 	                                          ARRAY_NUM_ID => 1
 	                                        });


$html->tpl_show(_include('netlist_ip_search', 'Netlist'), { %$Netlist, %FORM }); 

$pages_qs .= "&GID=$FORM{GID}" if ($FORM{GID});

my $list = $Netlist->ip_list( { %LIST_PARAMS, %FORM } );


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


my $table = $html->table( { width      => '100%',
                            caption    => "$_LIST IP",
                            border     => 1,
                            title      => ['IP', 'NETMASK', 'HOSTNAME', $_DESCRIBE, $_GROUP, $_STATE,  $_CHANGE, '-', '-', '-'],
                            cols_align => ['right', 'right', 'left', 'left', 'left', 'center', 'right', 
                             'center:noprint', 'center:noprint', 'center:noprint'],
                            qs         => $pages_qs,
                            pages      => $Netlist->{TOTAL},
                            ID         => 'NETLIST_LIST',
                            EXPORT     => "$_EXPORT XML:&xml=1",
                           });



  foreach my $line (@$list) {
     my $delete = $html->button($_DEL, "index=$index&del=$line->[0]", { MESSAGE => "$_DEL $line->[0]?", CLASS => 'del' }); 
     my $change = $html->button($_INFO, "index=$index&chg=$line->[0]", { CLASS => 'show' });

     $table->addrow(
     $line->[7].' '.$html->form_input('IDS', "$line->[0]", { TYPE => 'checkbox', EX_PARAMS => 'class="noprint"' } ), 
     $line->[1], 
     $line->[2], 
     $line->[3], 
     $line->[4], 
     $status[$line->[5]], 

     $line->[6], 
     $html->button("P", "index=$index&ping=$line->[0]$pages_qs", { TITLE => 'Ping', BUTTON => 1 }),
     $change,
     $delete
     );
   }
 


my $table2 = $html->table( { width       => '100%',
                             border      => 1,
      	                     class       => 'noprint',
                             title_plain => [
                                             "$_GROUP: ". $Netlist->{GROUP_SEL},
  	                                         "$_STATUS: ". $Netlist->{STATE_SEL},
                     	                       "RESOLVE: ". $html->form_input('RESOLV', '1', { TYPE => 'checkbox' } ),
                     	                       $html->form_input('change', $_CHANGE, { TYPE => 'SUBMIT' } )
                     	                     ]
                           });

  if ($FORM{EXPORT_CONTENT}) {
  	print $table->show();
   }

  print $html->form_main({ CONTENT => $table->show(). $table2->show(),
	                         HIDDEN  => { index  =>  "$index" },
                         });


  $table = $html->table({ width       => '100%',
                          cols_align  => ['left', 'right'],    
                          rows        => [ ["$_TOTAL:", $Netlist->{TOTAL} ]]
                        });

  print $table->show();

  $html->tpl_show(_include('netlist_ip', 'Netlist'), $Netlist); 
}


#**********************************************************
#
#**********************************************************
sub netlist_groups {


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

  if($FORM{add}) {
    $Netlist->group_add({ %FORM });
    my $GID=$Netlist->{GID};

    if(! $Netlist->{errno}) {
      $html->message('info', $_INFO, "$_ADDED");
     }
    
      if ($FORM{IP}) {
      	if($FORM{IP} !~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/) {
      	  $html->message('err', $_ERROR, "$ERR_WRONG_DATA [IP]");
    	    return 0;
    	   }
      	if($FORM{NETMASK} !~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/) {
      	  $html->message('err', $_ERROR, "$ERR_WRONG_DATA [NETMASK]");
    	    return 0;
    	   }
        else {
      	  my $ip_num = ip2int($FORM{IP}); #) {
      	  my $ip_count = unpack("N", pack("C4", split( /\./, "255.255.255.255"))) - unpack("N", pack("C4", split( /\./, "$FORM{NETMASK}")));



       	  for(my $i=0; $i<$ip_count; $i++){
      		  my $ip = int2ip($ip_num + $i);
      		  $Netlist->ip_add({IP       => $ip,
      		                    NETMASK  => $FORM{NETMASK},
      		                    HOSTNAME => lookupaddress($ip),
                              GID      => $GID });

            if ($Netlist->{errno}) {
               $html->message('err', $_ERROR, "[$Netlist->{errno}] $err_strs{$Netlist->{errno}}");	
              return 0
             }
       	   }
        }
       }
   }
  elsif($FORM{change}) {
    $Netlist->group_change({ %FORM });
    if(! $Netlist->{errno}) {
      $html->message('info', $_INFO, "$_CHANGED"); 
     }
   }
 	elsif(defined($FORM{chg})) {
    $Netlist->group_info($FORM{chg});
    if(! $Netlist->{errno}) {
      $html->message('info', $_INFO, "$_CHANGING");  	
     }
    $Netlist->{ACTION}='change';
    $Netlist->{ACTION_LNG}=$_CHANGE;
   }
  elsif(defined($FORM{del}) && defined($FORM{is_js_confirmed}) ) {
    $Netlist->group_del($FORM{del});
    if(! $Netlist->{errno}) {
    	$html->message('info', $_INFO, "$_DELETED"); 
     }
   }

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


$Netlist->{NETMASK}='255.255.255.0';
$html->tpl_show(_include('netlist_group', 'Netlist'), $Netlist);


my $list = $Netlist->groups_list({ %LIST_PARAMS });
my $table = $html->table( { width   => '100%',
                            caption => "$_GROUPS",
                            border  => 1,
                            title   => [$_NAME, $_COMMENTS, 'IP', '-', '-'],
                            cols_align => ['left', 'left', 'right', 'center:noprint', 'center:noprint'],
                            qs      => $pages_qs,
                            pages   => $Netlist->{TOTAL}
                           });



  foreach my $line (@$list) {
     my $delete = $html->button($_DEL, "index=$index&del=$line->[3]", { MESSAGE => "$_DEL $line->[0]?", CLASS => 'del' }); 
     my $change = $html->button($_INFO, "index=$index&chg=$line->[3]", { CLASS => 'show' });

     $table->addrow($line->[0], 
     $line->[1], 
     $html->button($line->[2], "index=". ($index-1). "&GID=$line->[3]"),
     $change,
     $delete
     );
   }
 

  
  print $table->show(); 


  $table = $html->table({ width       => '100%',
                          cols_align  => ['left', 'right'],    
                          rows        => [ ["$_TOTAL:", $Netlist->{TOTAL} ]]
                      });

  print $table->show();

  return 0;
}


#**********************************************************
#
#**********************************************************
sub lookupaddress {
  my($hostname,$server) = @_;

  my $iaddr = inet_aton($hostname); # or whatever address
  my $name  = gethostbyaddr($iaddr, AF_INET);

return $name;
}


sub netlist_new {
my $table = $html->table( { width   => '100%',
                            caption => "$_GROUPS",
                            border  => 1,
                            title   => [$_NAME, $_COMMENTS, 'IP', '-', '-'],
                            qs      => $pages_qs,
                            pages   => $Netlist->{TOTAL}
                           });

	print $table->show();
}

1

