Jebemci picku materinu to je z tych lahsich
fu fu este teraz to celkom nechapem ale sa k tomu blizim

nuz napriklad nam provider povie 
ze mame subnetmask

255.255.255.240

a ip adresu 

64.26.135.212

nuz tak najprv ideme zistit s tej zubnetmasky ze kolko je hostov a kolko sieti
1-ky su siete a 0 hosty

255 je 8 jednotiek a 240 je 11110000 (da sa si to vyratat a da sa pouzit ghex)
je to normalny prevod z desiatkovej sus do dvojkovej a naopak cize mame

11111111.11111111.11111111.11110000 i je zrejme ze jeden ceckovy subnetik mame 
rozdeleny na styri siete

cize mame 0-255 moznych adries cize 256 potencionalnych IP rozdelime 4
vychadza nam 256/4 = 64

cize ideme

0   - 63      prva siet
64  - 127     druha siet
128 - 191     tretia siet
192 - 255     stvrta siet

vtipne teraz este treba vediet ze ta spodna adresa je network address a vrchna broadcast
cize prva siet
0 network
1-62 mozne masiny
63 broadcact
druha siet
64 network
65-126 mozne masiny
127 broadcast atd

cize ked sme mali adresu 64.26.135.212 a subnetmask 255.255.255.240
tak je to v stvrtej sieti a network address je
255-64 + 1 = 192
a broadcast je to posledne cize v tomto pripade jednoduche 255
IP Address          64.26.135.212
IP Netmask          255.255.255.240
Broadcast address   64.26.135.255
Network address     64.26.135.192

brano
probably B class network
IP adresa 24.114.46.3
Subnetmask 255.255.252.0
cize podla spodnej logiky
256 - 252 = 4
40, 44, 48
cize 
sme snadwichovani
24.114.44
24.114.48

cize sietova adresa je
24.114.44.0
a broadcast je
24.114.47.255

skvele ipcalc potvrdil moje vypocty
prikladam ho na koniec suboru


1.2.10 IP Calculations

Let's say you have an IP Address 24.136.35.51 and a netmask 255.255.248.0. Then you have size 8 ( 256-248 = 8, or 3 bits) in the third byte. You can
infer that you are within one of the many size 8 subnets in the third byte. They will have addresses that are multiples of 8, so your job is to find which
multiples of 8 are the ones that sandwich your IP address. 

Multiples of 8 are: 0, 8, 16, 24, 32, 40, 48, .... So your IP is sandwiched by 24.136.32 and 24.136.40. The first number tells you your network address -
here, it is 24.136.32.0. Subtract 1 from the other address to obtain your broadcast - here it will be 24.136.39.255. 

Things are a little simpler if your netmask looks like 255.255.0.0. In this case, there is no partitioning within any of the 4 bytes. So if your IP address
was, say, 24.114.55.190, then your network address and broadcast would just be 24.114.0.0 and 24.114.255.255 respectively. 




ip-calc

#!/usr/bin/perl -w

# 2/2000 [email protected]
#
# 0.14    Release
# 0.14.1  Allow netmasks given as dotted quads
# 0.15    Colorize Classbits, Mark new bits in network

use strict;

my $version = "0.15";


my $private = "(Private Internet RFC 1918)";

my @privmin = qw (10.0.0.0        172.16.0.0      192.168.0.0);
my @privmax = qw (10.255.255.255  172.31.255.255  192.168.255.255);

my $allhosts;
my $mark_newbits = 0;

my $qcolor = "\033[34m"; # dotted quads, blue
my $ncolor = "\033[m";   # normal, black
my $bcolor = "\033[33m"; # binary, yellow
my $mcolor = "\033[31m"; # netmask, red
my $ccolor = "\033[35m"; # classbits, magenta
my $dcolor = "\033[32m"; # newbits, green



foreach (@privmin) {
    $_ = &bintoint(&dqtobin("$_"));
}

foreach (@privmax) {
    $_ = &bintoint(&dqtobin("$_"));
}


if (! defined ($ARGV[0])) {
    &usage;
}


if (defined ($ARGV[0]) && $ARGV[0] eq "-n") {
    shift @ARGV;
    $qcolor = '';
    $ncolor = '';
    $bcolor = '';
    $mcolor = '';
    $ccolor = '';
    $dcolor = '';
}

if (defined ($ARGV[0]) && $ARGV[0] eq "-h") {
    shift @ARGV;
    $qcolor = '' ;
    $ncolor = '';
    $bcolor = '';
    $mcolor = '';
    $ccolor = '';
    $dcolor = '';
    $private = "(Private Internet)";
    print "\n";
}


my $host  = "192.168.0.1";
my $mask  = "24";
my $mask2 = '24';

if (defined $ARGV[0]) {
    $host = $ARGV[0];
}
if (! ($host = &is_valid_dq($host)) ) {
    print "$mcolor Strange value for ADDRESS ($ARGV[0])$ncolor\n";
}



if (defined $ARGV[1]) {
    $mask = $ARGV[1];
    if (! ($mask = is_valid_netmask($mask)) ) {
	print "$mcolor Strange value for NETMASK ($ARGV[1])$ncolor\n";
    }
}


if (defined ($ARGV[2])) {
    $mask2 = $ARGV[2];
    if (! ($mask2 = is_valid_netmask($mask2)) ) {
	print "$mcolor Strange value for second NETMASK ($ARGV[2])$ncolor\n";
    }	
} else {
    $mask2 = $mask;
} 

print "\n";

print "Address:   $qcolor$host$ncolor\n";
print "Address:   " . &formatbin(&dqtobin($host),$mask,$bcolor,0) ."$ncolor\n";

my $m  = pack( "B*",("1" x $mask) . ("0" x (32 - $mask)) );

print "Netmask:   $qcolor" . &bintodq($m) . " == $mask$ncolor\n";
print "Netmask:   " . &formatbin($m,$mask,$mcolor,0) . "$ncolor\n";
print "=>\n";

my $h = dqtobin($host);

my $n = $h & $m;

&printnet($n,$mask);


if ( $mask2 == $mask ) {
    exit;
}
if ($mask2 > $mask) {
    print "Subnets\n\n";
    $mark_newbits = 1;
    &subnets;
} else {
    print "Supernet\n\n";
    &supernet;
}

sub supernet {
    $m  = pack( "B*",("1" x $mask2) . ("0" x (32 - $mask2)) );
    $n = $h & $m;
    print "Netmask:   $qcolor" . &bintodq($m) . " == $mask2$ncolor\n";
    print "Netmask:   "        . &formatbin($m,$mask2,$mcolor,0) . "$ncolor\n\n";
    &printnet($n,$mask2);
}

sub subnets {
    my $subnets = 0;
    my @oldnet;
    my $oldnet;
    my $k;
    my @nr;
    my $nextnet;
    my $l;


    $m  = pack( "B*",("1" x $mask2) . ("0" x (32 - $mask2)) );
    print "Netmask:   $qcolor" . &bintodq($m) . " == $mask2$ncolor\n";
    print "Netmask:   " . &formatbin($m,$mask2,$mcolor,0) . "$ncolor\n";
    print "\n";
    
    @oldnet = split //,unpack("B*",$n);
    for ($k = 0 ; $k < $mask ; $k++) {
	$oldnet .= $oldnet[$k];
    }
    for ($k = 0 ; $k < ( 2 ** ($mask2 - $mask)) ; $k++) {
	@nr = split //,unpack("b*",pack("L",$k));
	$nextnet = $oldnet;
	for ($l = 0; $l < ($mask2 - $mask) ; $l++) {
	    $nextnet .= $nr[$mask2 - $mask - $l - 1] ;
	}
	$n = pack "B*",$nextnet;
	&printnet($n,$mask2);
	++$subnets;
	if ($subnets >= 1000) {
	    print "... stopped at 1000 subnets ...\n";
	    last;
	}
    }

    if ( ($subnets < 1000) && ($mask2 > $mask) ){
	print "\nSubnets:   $qcolor$subnets $ncolor\n";
	print "Hosts:     $qcolor" . ($allhosts * $subnets) . "$ncolor\n";
    }
}

sub printnet {
    my ($n,$mask) = @_;
    my $nm;
    my $type;
    my $hmin;
    my $hmax; 
    my $hostn;
    my $p;
    my $i;

    
    $m  = pack( "B*",("1" x $mask) . ("0" x (32 - $mask)) );
    $nm = pack( "B*",("0" x $mask) . ("1" x (32 - $mask)) );

    $b = $n | $nm;
    
    $type = 1;
    while (unpack("B$type",$n) !~ /0/) {
	$type++;
    }
    if ($type > 5) {
	$type = '';
    } else {
	$type = "Class " . chr($type+64);
    }
    
    $hmin  = pack("B*",("0"x31) . "1") | $n;
    $hmax  = pack("B*",("0"x $mask) . ("1" x (31 - $mask)) . "0" ) | $n;
    $hostn = (2 ** (32 - $mask)) -2  ;

    $p = 0;
    for ($i=0; $i<3; $i++) {
	if ( (&bintoint($hmax) <= $privmax[$i])  && 
             (&bintoint($hmin) >= $privmin[$i]) ) {
	    $p = $i +1;
	    last;
	}
    }
    
    if ($p) {
	$p = $private;
    } else {
	$p = '';
    }


    print "Network:   $qcolor" . &bintodq($n) . "/$mask $ncolor($ccolor" .$type. "$ncolor) $p $ncolor\n";
    print "Network:   "        . &formatbin($n,$mask,$bcolor,1) . "$ncolor\n";

    print "Broadcast: $qcolor" . &bintodq($b) . "$ncolor\n";
    print "Broadcast: "        . &formatbin($b,$mask,$bcolor,0) . "$ncolor\n";


    print "HostMin:   $qcolor" . &bintodq($hmin) . "$ncolor\n";
    print "HostMin:   "        . &formatbin($hmin,$mask,$bcolor,0) . "$ncolor\n";
    
    print "HostMax:   $qcolor" . &bintodq($hmax) . "$ncolor\n";
    print "HostMax:   "        . &formatbin($hmax,$mask,$bcolor,0) . "$ncolor\n";


    print "Hosts:     $qcolor$hostn$ncolor\n\n";
    $allhosts = $hostn;
}



sub formatbin {
    my ($bin,$actual_mask,$color,$mark_classbits) = @_;
    my @dq;
    my $dq;
    my @dq2;
    my $is_classbit = 1;
    my $bit;
    my $i;
    my $j;
    my $oldmask;
    my $newmask;

    if ($mask2 > $mask) {
	$oldmask = $mask;
	$newmask = $mask2;
	
    } else {
	$oldmask = $mask2;
	$newmask = $mask;
    }

    @dq = split //,unpack("B*",$bin);
    if ($mark_classbits) {
	$dq = $ccolor;
    }	else {
	$dq = $color;
    }
    for ($j = 0; $j < 4 ; $j++) {
	for ($i = 0; $i < 8; $i++) {
	    if (! defined ($bit = $dq[$i+($j*8)]) ) {
		$bit = '0';
	    }

	    if ( $mark_newbits &&((($j*8) + $i + 1) == ($oldmask + 1)) ) {
		$dq .= "$dcolor";
	    }


	    $dq .= $bit;
	    if ( ($mark_classbits && 
		  $is_classbit && $bit == 0)) {
		$dq .= $color;
		$is_classbit = 0;
	    }
	    if ( (($j*8) + $i + 1) == $actual_mask ) {
		$dq .= " ";
	    }

	    if ( $mark_newbits &&((($j*8) + $i + 1) == $newmask) ) {
		$dq .= "$color";
	    }

	}
	push @dq2, $dq;
	$dq = '';
    }
    return (join ".",@dq2) . $ncolor;
}

sub dqtobin {
        my @dq;
	my $q;
	my $i;
	my $bin;

	foreach $q (split /\./,$_[0]) {
		push @dq,$q;
	}
	for ($i = 0; $i < 4 ; $i++) {
		if (! defined $dq[$i]) {
			push @dq,0;
		}
	}
	$bin    = pack("CCCC",@dq);      # 4 unsigned chars
	return $bin;
}

sub bintodq {
	my $dq = join ".",unpack("CCCC",$_[0]);
	return $dq;
}

sub bintoint {
	return unpack("N",$_[0]);
}


sub is_valid_dq {
	my $value = $_[0];
	my $test = $value;
 	my $i;
	my $corrected;
	$test =~ s/\.//g;
	if ($test !~ /^\d+$/) {
		return 0;
	}
	my @value = split /\./, $value, 4;
	for ($i = 0; $i<4; $i++) {
		if (! defined ($value[$i]) ) {
			$value[$i] = 0;
		}
		if ( ($value[$i] !~ /^\d+$/) ||
		     ($value[$i] < 0) || 
                     ($value[$i] > 255) ) 
                {
			return 0;
		}
	}
	$corrected = join ".", @value;
	return $corrected;
}

sub is_valid_netmask {
	my $mask = $_[0];
	if ($mask =~ /^\d+$/) {
		if ( ($mask > 32) || ($mask < 1) ) {
			return 0;
		}
	} else {
		if (! ($mask = &is_valid_dq($mask)) ) {
			return 0;
		}
		$mask = dqtocidr($mask);
	}
	return $mask;

}


sub dqtocidr {
	my $dq = $_[0];
	$b = &dqtobin($dq);
	my $cidr = 1;
	while (unpack("B$cidr",$b) !~ /0/) {
		$cidr++;
		last if ($cidr == 33);
	}
	$cidr--;
	return $cidr;
	
}

sub usage {
    print << "EOF";
Usage: ipcalc [-n|-h] 
[NETMASK] Calculates network parameters given by ADDRESS an NETMASK and displays them as dotted quads and binary values. If a second NETMASK is provided the resulting super- or subnets of a transition to the new netmask are displayed. ADDRESS can be the ip-address in the network of interest or part of the network prefix. -n Don't display ANSI color codes -h Display results as HTML Example: ipcalc 192.168.0.1 24 EOF exit; }