/*_____________________________________________________________________________ |/ / CIDR add-on by David "Saturn" van Moolenbroek | Version 0.2, released 25-06-2005 -- support on #help.script/Quakenet | Use/modify however you want, but please keep my name in it. Thank you! | | Keywords: CIDR masking, IP blocks, binary operations | | This script allows you to display the properties of CIDR-masked IP blocks. | If you don't know what CIDR masking is, this script is not for you; it's | simply a tool to those who do know what it is. | | The script defines the following commands/identifiers: | | /cidr [-ms] [ip|mask] | | This command displays the properties of the specified IP block, in the | form , / or . In the last case, the script | will calculate the smallest CIDR mask that contains both IP addresses. | The command will display the start IP, end IP, IP netmask and the number | of IP addresses in the block. | | If the -m switch is specified, then the command takes two IPs as | parameters, and it will dump a list of CIDR addresses that cover the | exact range between the given two IPs, including these IPs themselves. | | If the -s switch is specified, the script will use a short, one-line | notation, regardless of whether the -m switch is also present. | | $cidr(ip,mask) | | Properties: start, end, mask, size, ip, bits | | This identifier, which also accepts input in the form /, will | just return the block in CIDR notation (IP/mask) by default, if it | considers it valid; with one of the properties, specific properties of | the IP block are returned: respectively the start IP of the block, the | end IP of the block, the IP mask for the block, and the size of the | block (the number of IPs in the block). The properties ip and bits will | return the given IP number and the CIDR mask (0-32) for the block, | respectively. | | $cidr(ip,ip) | | This identifier works just like $cidr(,), but it will first | calculate the smallest mask that contains both specified IP addresses. | | $cidr(ip,ip,N) | | This identifier works just like $cidr(,), but it will first | calculate the Nth block of the set of blocks that cover the whole given | IP-range (inclusive) exactly; except when N is 0, in which case the | identifier will return the number of blocks that cover the IP-range. | | Simple examples | | /cidr -s 172.18.235.94/12 | /cidr 10.3.151.241 10.250.10.1 | /cidr -m 192.168.25.2 192.168.25.125 \ _\_____________________________________________________________________________ */ alias -l cidr.start return $longip($and($1,$calc(2^32-2^(32-$2)))) alias -l cidr.end return $longip($or($1,$calc(2^(32-$2)-1))) alias -l cidr.mask return $longip($calc(2^32-2^(32-$1))) alias -l cidr.size return $calc(2^(32-$1)) alias -l cidr.scd return $calc(32-$len($iif($base($calc($xor($1,$2)),10,2),$v1))) alias -l cidr.sub { var %m = $calc($len($iif($base($xor($1,$2),10,2),$v1)) - 1) if ($calc((2 ^ %m) - ($1 % (2 ^ %m)))) { var %i = $v1, %b = $base(%i,10,2,32), %n = $pos(%b,1,0) if (($3) && ($3 <= %n)) { var %p = $pos(%b,1,$calc(%n - $3 + 1)) return $calc($1 + $and(%i,$calc(2^(32-%p)-1))) %p } tokenize 32 $calc($1 + %i) $2 $3 %n } if ($calc($2 % (2 ^ %m))) { var %i = $v1, %b = $base(%i,10,2,32), %n = $pos(%b,1,0) if (($3) && ($calc($3 - $4) <= %n)) { var %p = $pos(%b,1,$v1) return $calc($1 + $and(%i,$calc(2^32-2^(33-%p)))) %p } if (!$3) return $calc($4 + %n) } if (!$3) return $4 } alias cidr { if ($isid) { if ($0 == 3) { var %a = $longip($1), %b = $longip($2) if ((%a !isnum) || (%b !isnum) || (%a > %b) || ($3 !isnum)) return tokenize 32 $cidr.sub(%a,$calc(%b + 1),$3,0) if ($0 < 2) return $1 } elseif ($0 == 2) { var %a = $longip($1), %b = $longip($2) if (%a !isnum) return if (%b isnum) tokenize 32 %a $cidr.scd(%a,%b) elseif ($2 isnum 0-32) tokenize 32 %a $2 else return } elseif (($0 == 1) && (/ isin $2)) { tokenize 47 $1 var %a = $longip($1) if (($longip($1) !isnum) || ($2 !isnum 0-32)) return tokenize 32 %a $2 } else return ; $1 = longip, $2 = mask if ($prop == start) return $cidr.start($1,$2) elseif ($prop == end) return $cidr.end($1,$2) elseif ($prop == mask) return $cidr.mask($2) elseif ($prop == size) return $cidr.size($2) elseif ($prop == ip) return $longip($1) elseif ($prop == bits) return $2 else return $+($cidr.start($1,$2),/,$2) } var %opt if (-* iswm $1) { %opt = $1 tokenize 32 $2- } if (m isincs %opt) { if ($0 < 2) { echo $color(info) -aqt * /cidr: insufficient parameters return } var %a = $longip($1), %b = $longip($2), %i = 1 if ((%a !isnum) || (%b !isnum) || (%a > %b)) { echo $color(info) -aqt * /cidr: invalid parameters return } echo -aq - if (s isincs %opt) var %s = $+($1 - $2,:,$color(info2)) else echo $color(info) -aqt * CIDR masks for IP range $1 - $2 inc %b while ($cidr.sub(%a,%b,%i) != $null) { tokenize 32 $v1 if (s isincs %opt) %s = %s $+($longip($1),/,$2) else echo $color(info2) -aqt %i $+ : $+($longip($1),/,$2) inc %i } if (s isincs %opt) echo $color(info) -aqt * %s echo -aq - return } if ((/ isin $1) && ($gettok($1,2-,47) isnum)) tokenize 47 $1 var %a = $longip($1), %b = $longip($2) if ($0 != 2) { echo $color(info) -aqt * /cidr: insufficient parameters return } if ((%a !isnum) || (($2 !isnum 0-32) && (%b !isnum))) { echo $color(info) -aqt * /cidr: invalid parameters return } var %m = $iif($2 isnum,$2,$cidr.scd(%a,%b)) if (s isincs %opt) { echo $color(info) -aqt * $+($1,/,%m,:,$color(info2)) $& $cidr.start(%a,%m) - $cidr.end(%a,%m) ( $+ $cidr.mask(%m) $+ ), $+ $& $color(info2) $cidr.size(%m) IPs } else { echo -aq - echo $color(info) -aqt * IP block $+($1,/,%m) echo $color(info2) -aqt Start: $cidr.start(%a,%m) echo $color(info2) -aqt End: $cidr.end(%a,%m) echo $color(info2) -aqt Mask: $cidr.mask(%m) echo $color(info2) -aqt Size: $cidr.size(%m) IPs echo -aq - } }