+sub subtocidr
+{
+ #gets: Subnet in decimal (255.255.255.0)
+ #Gives: 24 (The cidr of network)
+ my ($byte1, $byte2, $byte3, $byte4) = split(/\./, $_[0].".0.0.0.0");
+ my $num = ($byte1 * 16777216) + ($byte2 * 65536) + ($byte3 * 256) + $byte4;
+ my $bin = unpack("B*", pack("N", $num));
+ my $count = ($bin =~ tr/1/1/);
+ return $count;
+}
+
+sub cidrtosub
+{
+ #gets: Cidr of network (20-30 for ccd)
+ #Konverts 30 to 255.255.255.252 e.g
+ my $cidr=$_[0];
+ my $netmask = &Net::IPv4Addr::ipv4_cidr2msk($cidr);
+ return "$netmask";
+}
+
+sub iporsubtodec
+{
+ #Gets: Ip address or subnetmask in decimal oder CIDR
+ #Gives: What it gets only in CIDR format
+ my $subnet=$_[0];
+ my $net;
+ my $mask;
+ my $full=0;
+ if ($subnet =~ /^(.*?)\/(.*?)$/) {
+ ($net,$mask) = split (/\//,$subnet);
+ $full=1;
+ return "$subnet";
+ }else{
+ $mask=$subnet;
+ }
+ #Subnet already in decimal and valid?
+ if ($mask=~/^(\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<=32;$i++){
+ if (&General::cidrtosub($i) eq $mask){
+ if ($full == 0){return $mask;}else{
+ return $net."/".$mask;
+ }
+ }
+ }
+ }
+ #Subnet in binary format?
+ if ($mask=~/^(\d{1,2})$/ && (($1<=32 && $1>=8))){
+ if($full == 0){ return &General::cidrtosub($mask);}else{
+ return $net."/".&General::cidrtosub($mask);
+ }
+ }else{
+ return 3;
+ }
+ return 3;
+}
+
+
+sub iporsubtocidr
+{
+ #gets: Ip Address or subnetmask in decimal oder CIDR
+ #Gives: What it gets only in CIDR format
+ my $subnet=$_[0];
+ my $net;
+ my $mask;
+ my $full=0;
+ if ($subnet =~ /^(.*?)\/(.*?)$/) {
+ ($net,$mask) = split (/\//,$subnet);
+ $full=1;
+ }else{
+ $mask=$subnet;
+ }
+ #Subnet in decimal and valid?
+ if ($mask=~/^(\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<=32;$i++){
+ if (&General::cidrtosub($i) eq $mask){
+ if ($full == 0){return &General::subtocidr($mask);}else{
+ return $net."/".&General::subtocidr($mask);
+ }
+ }
+ }
+ }
+ #Subnet already in binary format?
+ if ($mask=~/^(\d{1,2})$/ && (($1<=32 && $1>=8))){
+ if($full == 0){ return $mask;}else{
+ return $net."/".$mask;
+ }
+ }else{
+ return 3;
+ }
+ return 3;
+}
+
+sub getnetworkip
+{
+ #Gets: IP, CIDR (10.10.10.0-255, 24)
+ #Gives: 10.10.10.0
+ my ($ccdip,$ccdsubnet) = @_;
+ my $ip_address_binary = inet_aton( $ccdip );
+ my $netmask_binary = ~pack("N", (2**(32-$ccdsubnet))-1);
+ my $network_address = inet_ntoa( $ip_address_binary & $netmask_binary );
+ return $network_address;
+}
+
+sub getccdbc
+{
+ #Gets: IP in Form ("192.168.0.0/24")
+ #Gives: Broadcastaddress of network
+ my $ccdnet=$_;
+ my ($ccdip,$ccdsubnet) = split "/",$ccdnet;
+ my $ip_address_binary = inet_aton( $ccdip );
+ my $netmask_binary = ~pack("N", (2**(32-$ccdsubnet))-1);
+ 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 $decip=&ip2dec($_[0]);
+ $decip=$decip+4;
+ return &dec2ip($decip);
+}
+
+sub getlastip
+{
+ my $decip=&ip2dec($_[0]);
+ $decip--;
+ return &dec2ip($decip);
+}
+