my $broadcast_address = inet_ntoa( $ip_address_binary | ~$netmask_binary );
return $broadcast_address;
}
+
+sub ip2dec
+{
+ my $ip_num;
+ my $ip=$_[0];
+ if ( $ip =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/ ) {
+ $ip_num = (($1*256**3) + ($2*256**2) + ($3*256) + $4);
+ } else {
+ $ip_num = -1;
+ }
+ $ip_num = (($1*256**3) + ($2*256**2) + ($3*256) + $4);
+ return($ip_num);
+}
+
+sub dec2ip
+{
+ my $ip;
+ my $ip_num=$_[0];
+ my $o1=$ip_num%256;
+ $ip_num=int($ip_num/256);
+ my $o2=$ip_num%256;
+ $ip_num=int($ip_num/256);
+ my $o3=$ip_num%256;
+ $ip_num=int($ip_num/256);
+ my $o4=$ip_num%256;
+ $ip="$o4.$o3.$o2.$o1";
+ return ($ip);
+}
+
sub getnextip
{
- my ($byte1,$byte2,$byte3,$byte4) = split (/\./,$_[0]);
- my $step=$_[1];
- for (my $x=1;$x<=$step;$x++){
- $byte4++;
- if($byte4==255){ $byte4=0;$byte3++;}
- if($byte3==255){$byte3=0;$byte2++;}
- if ($byte2==255){$byte2=0;$byte1++}
-
- }
- return "$byte1.$byte2.$byte3.$byte4";
+ my $decip=&ip2dec($_[0]);
+ $decip=$decip+4;
+ return &dec2ip($decip);
}
+
sub getlastip
{
- my ($byte1,$byte2,$byte3,$byte4) = split (/\./,$_[0]);
- my $step=$_[1];
- for (my $x=$step;$x>=1;$x--){
- $byte4--;
- if($byte4==0){ $byte4=255;$byte3--;}
- if($byte3==0){$byte3=255;$byte2--;}
- if ($byte2==0){$byte2=255;$byte1--}
- }
- return "$byte1.$byte2.$byte3.$byte4";
+ my $decip=&ip2dec($_[0]);
+ $decip--;
+ return &dec2ip($decip);
}
sub validipandmask
if ($ccdip=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ &&(($1>0 && $1<=255 && $2>=0 && $2<=255 && $3>=0 && $3<=255 && $4<=255 ))) {
#Subnet in decimal and valid?
if ($ccdsubnet=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ &&(($1<=255 && $2<=$1 && $3<=$2 && $4<=$3 ))) {
- for (my $i=8;$i<=30;$i++){
+ for (my $i=8;$i<=32;$i++){
if (&General::cidrtosub($i) eq $ccdsubnet){
return 1;
}
}
#Subnet already in binary format?
- }elsif ($ccdsubnet=~/^(\d{1,2})$/ && (($1<=30 && $1>=8))){
+ }elsif ($ccdsubnet=~/^(\d{1,2})$/ && (($1<=32 && $1>=8))){
return 1;
}else{
return 0;
return 0;
}
+sub checksubnets
+{
+
+ my %ccdconfhash=();
+ my @ccdconf=();
+ my $ccdname=$_[0];
+ my $ccdnet=$_[1];
+ my $errormessage;
+ my ($ip,$cidr)=split(/\//,$ccdnet);
+ $cidr=&iporsubtocidr($cidr);
+
+
+ #get OVPN-Subnet (dynamic range)
+ my %ovpnconf=();
+ &readhash("${General::swroot}/ovpn/settings", \%ovpnconf);
+ my ($ovpnip,$ovpncidr)= split (/\//,$ovpnconf{'DOVPN_SUBNET'});
+ $ovpncidr=&iporsubtocidr($ovpncidr);
+
+ #check if we try to use same network as ovpn server
+ if ("$ip/$cidr" eq "$ovpnip/$ovpncidr") {
+ $errormessage=$errormessage.$Lang::tr{'ccd err isovpnnet'}."<br>";
+ return $errormessage;
+ }
+
+ #check if we use a network-name/subnet that already exists
+ &readhasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
+ foreach my $key (keys %ccdconfhash) {
+ @ccdconf=split(/\//,$ccdconfhash{$key}[1]);
+ if ($ccdname eq $ccdconfhash{$key}[0])
+ {
+ $errormessage=$errormessage.$Lang::tr{'ccd err nameexist'}."<br>";
+ return $errormessage;
+ }
+ my ($newip,$newsub) = split(/\//,$ccdnet);
+ if (&IpInSubnet($newip,$ccdconf[0],&iporsubtodec($ccdconf[1])))
+ {
+ $errormessage=$errormessage.$Lang::tr{'ccd err issubnet'}."<br>";
+ return $errormessage;
+ }
+
+ }
+ #check if we use a name which is already used by ovpn
+
+
+
+
+
+ #check if we use a ipsec right network which is already defined
+ my %ipsecconf=();
+ &General::readhasharray("${General::swroot}/vpn/config", \%ipsecconf);
+ foreach my $key (keys %ipsecconf){
+ if ($ipsecconf{$key}[11] ne ''){
+ #$errormessage="DRIN!";
+ #return $errormessage;
+
+ my ($ipsecip,$ipsecsub) = split (/\//, $ipsecconf{$key}[11]);
+ $ipsecsub=&iporsubtodec($ipsecsub);
+
+ if ( &IpInSubnet ($ip,$ipsecip,$ipsecsub) ){
+ $errormessage=$Lang::tr{'ccd err isipsecnet'}." Name: $ipsecconf{$key}[2]";
+ return $errormessage;
+ }
+ }
+ }
+
+
+ #check if we use one of ipfire's networks (green,orange,blue)
+ my %ownnet=();
+ &readhash("${General::swroot}/ethernet/settings", \%ownnet);
+ if (($ownnet{'GREEN_NETADDRESS'} ne '' && $ownnet{'GREEN_NETADDRESS'} ne '0.0.0.0') && &IpInSubnet($ownnet{'GREEN_NETADDRESS'},$ip,&iporsubtodec($cidr))){ $errormessage=$Lang::tr{'ccd err green'};return $errormessage;}
+ if (($ownnet{'ORANGE_NETADDRESS'} ne '' && $ownnet{'ORANGE_NETADDRESS'} ne '0.0.0.0') && &IpInSubnet($ownnet{'ORANGE_NETADDRESS'},$ip,&iporsubtodec($cidr))){ $errormessage=$Lang::tr{'ccd err orange'};return $errormessage;}
+ if (($ownnet{'BLUE_NETADDRESS'} ne '' && $ownnet{'BLUE_NETADDRESS'} ne '0.0.0.0') && &IpInSubnet($ownnet{'BLUE_NETADDRESS'},$ip,&iporsubtodec($cidr))){ $errormessage=$Lang::tr{'ccd err blue'};return $errormessage;}
+ if (($ownnet{'RED_NETADDRESS'} ne '' && $ownnet{'RED_NETADDRESS'} ne '0.0.0.0') && &IpInSubnet($ownnet{'RED_NETADDRESS'},$ip,&iporsubtodec($cidr))){ $errormessage=$Lang::tr{'ccd err red'};return $errormessage;}
+
+
+
+}
+
+
sub validport
{
$_ = $_[0];