I decided to share three small functions I made for IP addresses the other day in a few minutes.
Their use is as follows:
$isIPAddress(Possible IP address here)
example: $isIPAddress(1.2.3.4)
This returns whether the given text was an IP address.
$cmpIPAddress(Address one,Address two)
example: $cmpIPAddress(1.2.3.4,2.3.4.5)
This returns a comparison value for two IP addresses similar to strcmp() for string comparison in C.
-1 means the first is less than the second.
0 means they are equal.
1 means the first is greater than the second.
$isinIPRange(IP address range,IP address to check)
example: $isinIPRange(0.0.0.0-255.255.255.255,1.2.3.4)
This returns whether the IP address exists within the given range, in address1-address2 format.
The function will accept arguments where address2>address1.
I don't currently have a use for these, but perhaps you will or perhaps you can find something for me to improve on them.
Either way, enjoy. :)
; Returns whether a given IP address is valid
; example: $isIPAddress(1.2.3.4)
isIPAddress {
tokenize 46 $1
if ($1 isnum 0-255 && $2 isnum 0-255 && $3 isnum 0-255 && $4 isnum 0-255 && $5 == $null) return $true
else return $false
}
; Returns a comparison value for the first IP address to the second (-1 less than, 0 equal, 1 greater than)
; example: $cmpIPAddress(1.2.3.4,2.3.4.5)
cmpIPAddress {
if ($isIPAddress($1) && $isIPAddress($2)) {
var %x 1, %result 0
while (%x <= 4 && !%result) {
if ($gettok($1,%x,46) < $gettok($2,%x,46)) %result = -1
elseif ($v1 > $v2) %result = 1
inc %x
}
return %result
}
else echo -s ERROR: $cmpIPAddress invalid parameters ( $+ $1- $+ )
}
; Returns whether a given IP address falls within the given range
; example: $isinIPRange(0.0.0.0-255.255.255.255,1.2.3.4)
isinIPRange {
var %low $gettok($1,1,45), %high $gettok($1,2,45), %ip $2, %result $false
if ($isIPAddress(%low) && $isIPAddress(%high) && $isIPAddress(%ip)) {
; Make sure low is actually the lowest (and vice versa)
if ($cmpIPAddress(%low,%high) > 0) {
var %temp = %low
%low = %high
%high = %temp
}
; Compare
if ($cmpIPAddress(%ip,%low) >= 0) {
if ($cmpIPAddress(%ip,%high) <= 0) %result = $true
}
}
else echo -s ERROR: $isinIPRange invalid parameters ( $+ $1- $+ )
return %result
}
did you intentionally avoid using $longip()? :P with it, you can map valid IPv4 addresses onto their integer counterparts, making them much easier to compare. it also implicitly validates them: an integer return value implies a valid IPv4 address. mIRC 7.1 also introduced a new $iptype() identifier which returns 'ipv4' if the input is a valid such address.
careful with /tokenize though: $isipaddress(1...1...1...1) = $true. also, due to using isnum, $isipaddress(+1.-0.+00000002.+3) = $true. making these kinds of things bulletproof is often a pain ;P
With 0.0.0.0 alone, you must decide whether your IP address check is based on whether the values are valid (all octets between 0 and 255) or whether the IP address is considered valid (since 0.0.0.0 is never used for anything).
I would personally only change that if 0.0.0.0 is considered invalid by the IP standard.
Your regex is completely invalid.
It matches zero or more number characters (3 times), then zero or more number characters again.
It will be true if you supply a blank string or any reasonable amount of random numbers.
The regular expression for accurately matching an IP address is far more complex.