code:
;****************************************************************************
;
;SCRIPT/FUNCTION : MOAN()
;
;ACTION : Depending on the input, returns IP-addresses, Subnets,
; NetworkIDs or Yes-/No-is_in_subnet
;
;AUTHOR : Jooel Nieminen and Howard Bullock
;
;CONTRIBUTORS : Patrick Rutten (MightyR1@hotmail.com)
; - making the code readable
;
;VERSION : 1.0
; - 2003-06-05 Initial Release
;
;SYNTAX : MOAN($IP, $NetworkID, $SubnetMask)
;
;PARAMETERS :
;
;RETURNS : Input: IP, NetworkID, SubnetMask
; Output: 1 If the IP is a member of a network defined
; by NetworkID AND SubnetMask, otherwise 0
;
; Input: IP, NetworkID
; Output: Array of all potential SubnetMasks
; (CIDR notation) OR empty string If no match
;
; Input: IP, SubnetMask
; Output: NetworkID OR empty string If no match
;
; Input: NetworkID, SubnetMask
; Output: Array of all potential IP addresses OR
; empty string If no match
;
;REMARKS : This UDF is a result of a KiXGolf Tournament hosted by
; Sealeopard. Follow the links below to see how the game
; was played.
;
; Assumption: Network/broadcast addresses are NOT
; available as regular IP addresses though RFC3021
; allows /31 networks (two IP addresses where the
; network/broadcast addresses are used as regular
; IP addresses)
;
; At least two input parameters must be provided.
;
; If the output consists of an array of IP addresses,
; then the IP addresses must be sorted In ascending order.
;
; The subnet mask $SubnetMask can be either the traditional
; four-octets OR the alternate CIDR number,
; e.g. 255.255.255.0 AND 24.
;
; The parameters can contain up to two spaces between the
; decimal points AND the number, e.g. 192.168.0.10 AND
; 192.168. 0. 10 should be treated the same.
;
;DEPENDENCIES :
;
;EXAMPLE(S) :
; [Example 1]
; IP=192.168.1.2
; NetworkID=192.168.1.0
; SubnetMask=255.255.255.0
; MOAN=1
;
; [Example 2]
; IP=192.168.0.1
; NetworkID=192.168.1.0
; SubnetMask=255.255.255.0
; MOAN=0
;
; [Example 3]
; IP=
; NetworkID=10.10.5.4
; SubnetMask=255.255.255.252
; MOAN=10.10.5.5,10.10.5.6
;
; [Example 4]
; IP=192.10.128.55
; NetworkID=
; SubnetMask=27
; MOAN=192.10.128.32
;
; [Example 5]
; IP=10.10.10.2
; NetworkID=
; SubnetMask=255.255.255.254
; MOAN=
;
; [Example 6]
; IP=10.10.10.2
; NetworkID=10.10.10.0
; SubnetMask=
; MOAN=/23,/24,/25,/26,/27,/28,/29,/30
;
; [Example 7]
; IP=192.168.99.55
; NetworkID=192.168.99.55
; SubnetMask=
; MOAN=
;
;KNOWN ISSUES :
;
;KIXTART VER : 4.2x
;
;KIXTART BBS : KiXGolf: MOAN(), Mother Of All Networks -->
; - http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=14;t=000720
;
; KixGolf: MOAN(), Part II -->
; - http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic;f=14;t=000748
;
;****************************************************************************
;
;!
Function MOAN($IP,$NetworkID,$SubnetMask)

;declaring variables
Dim $cidrarray,$dot,$dummy,$subnetarray,$count,$help,$maskarray[3],$count2
Dim $first,$second,$third,$fourth

$cidrarray=0,128,192,224,240,248,252,254
$dot="."

;build SubnetMask; starting from bit 8 through 31
For $count=8 to 31

$dummy=$count
$help=1

;determine each octet
For $count2=0 to 3


;if bitnumber/8 > 0, the octet is 255 else the mod must be determined
;255.255.248.0=11111111.11111111.11111000.00000000
$maskarray[$count2]=IIf($dummy/8,255,$cidrarray[$dummy mod 8])

;8 must be substracted if one "moves" to the next octed
$dummy=($dummy>8)*($dummy-8)

;& the IP, NetworkID and the SubnetMask octets to see if the are "compatible"
If $IP & $NetworkID
$help=$help & Split($NetworkID,$dot)[$count2]=(0+Split($IP,$dot)[$count2] & $maskarray[$count2])
EndIf
Next

;if all variables are "compatible", add the bit number to the subnet string in CIDR notation
If $help
$subnetMask=$SubnetMask+" /"+$count
EndIf

;if current bit=current subnetmask then convert the SubnetMask to
;normal notation, e.g. 255.255.248.0
If $count=$SubnetMask
$SubnetMask=Join($maskarray,$dot)
EndIf

Next
;end Built subnet mask

$MOAN=$SubnetMask

;if the SubnetMask contains a point, calculate IP-addresses, NetworkIDs
;or Yes-/No-is_in_subnet
If InStr($SubnetMask,$dot)

$dummy=Split($SubnetMask+$dot+$NetworkID,$dot)
$MOAN=""

;if IP is provided, determine NetworkIDs
If $IP

;calculate the NetworkID
For $count=0 to 3
$MOAN=$MOAN+$dot+(0+$dummy[$count] & 0+Split($IP,$dot)[$count])
Next

;if IP=NetworkID there is no subnet, thus returning empty
If $dot+Join(Split($IP),"")=$MOAN
$MOAN=""
EndIf

;if NetworkID is provided:
; - return .0 if the calculated NetworkID <> provided NetworkID,
; IP is not in subnet
; - return .1 if the calculated NetworkID = provided NetworkID
; IP is in subnet
If $NetworkID
$MOAN=$dot+Join(Split($NetworkID),"")=$MOAN
EndIf

;determine array of IP numbers
Else

;first octet
For $first=$dummy[4] to 255 + $dummy[4] - $dummy[0]

;second octet
For $second=$dummy[5] to 255 + $dummy[5] - $dummy[1]

;Third octet
For $third=$dummy[6] to 255 + $dummy[6] - $dummy[2]

;Fourth octet
For $fourth=1+$dummy[7] to 254 + $dummy[7] - $dummy[3]

$MOAN = $MOAN+" "+$first+$dot+$second+$dot+$third+$dot+$fourth
Next

Next

Next

Next

EndIf

EndIf

;return the result
$dummy=$MOAN,Split(SubStr($MOAN,2))
$MOAN=$dummy[Len($MOAN)>1]

EndFunction
;!
;!
; end MOAN

this I mean.
for proper explanation see the difference between the winning code and the previous ones.
it's actually too messy with the comments to see where what looping do but looking the golf-codes you see immediately the difference [Wink]

will explain properly, if you still can't see it [Razz]
_________________________
!

download KiXnet