IP address functions

By Gummo on Apr 05, 2011

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(
This returns whether the given text was an IP address.

$cmpIPAddress(Address one,Address two)
example: $cmpIPAddress(,
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(,
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(
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(,
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(,
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


Sign in to comment.
jaytea   -  Apr 07, 2011

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

Gummo   -  Apr 06, 2011

What is your basis for calling it invalid other than the fact that it's never used?
I looked at the IP standard as well as IP address allocation and nothing mentioned
The only mention(s) of it were to say that an IP address is x.x.x.x where x is between 0 and 255.

Dani_l11   -  Apr 06, 2011

@Gummo I knew my regex was invalid, thats why i removed it, however, it doens't change the fact of being an invalid IP. Just add if ($calc($replace($1-4,$chr(32),+)) == 0) return $false

Gummo   -  Apr 06, 2011

With 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 is never used for anything).
I would personally only change that if is considered invalid by the IP standard.

napa182   -  Apr 06, 2011

One of the main reasons that i like this snippet is no use of regexs. People tend to over use them now adays when they don't need to be used.
again nice work Gummo.

Jethro   -  Apr 06, 2011

Regex learning is a never-ending journey. It continues to evolve and new features being added to read up upon. It's a complex grammatical language that will ultimately rack our brains during the learning process. :p

Gummo   -  Apr 06, 2011

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.

Dani_l11   -  Apr 06, 2011

Sorry i ment would return $true, while i still think that is an invalid IP address (atleast, this address if often showed when you're not connected)?

Gummo   -  Apr 06, 2011

No.. Why would it?

Dani_l11   -  Apr 06, 2011

$isipaddress( would return $true?

Gummo   -  Apr 05, 2011

Thanks napa. :)

napa182   -  Apr 05, 2011

nice work Gummo I like it ;x 8/10 +like

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.