Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP (Non-Forum Related)
 ASP i.p. address range function
 New Topic  Topic Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

Podge
Support Moderator

Ireland
3776 Posts

Posted - 23 March 2005 :  12:58:43  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
I'm trying to write a function which will return true if a given address is within a specific range and false otherwise but I'm not having much success.

The range will be semi permanent and will never contain wildcards e.g.

127.0.64.1 - 127.0.79.254

Any ideas? I coundn't find an asp example on the net.


Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.

redbrad0
Advanced Member

USA
3725 Posts

Posted - 23 March 2005 :  22:32:35  Show Profile  Visit redbrad0's Homepage  Send redbrad0 an AOL message
Well I am just talking here...

You would first have to split it up into 4 sections (each range). Then just grab each number and see if the number is between the two by a loop. It might not be the most effective way but sounds like it would work.

Brad
Oklahoma City Online Entertainment Guide
Oklahoma Event Tickets
Go to Top of Page

Podge
Support Moderator

Ireland
3776 Posts

Posted - 24 March 2005 :  04:03:53  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
I figured out the maths and created a function. I'll post it here later when I clean it up a bit.

Initially I thought it would be simple enough. How wrong I was.

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page

Podge
Support Moderator

Ireland
3776 Posts

Posted - 24 March 2005 :  07:21:10  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message

Function chkIPRange (startIP, endIP, ip)
chkIPRange = false
myArray1 = split(startIP, ".")
myArray2 = split(endIP, ".")
myArray3 = split(ip, ".")

'myArray1 to Binary
	for n = 0 to 3
		myInt= CInt(myArray1(n))
		binmyArray1 = binMyArray1 & dectobin(myInt)
	next
	document.write binmyArray1 & " - " & bintodec(binmyArray1) & "<br>"

'myArray2 to Binary
	for n = 0 to 3
		myInt= CInt(myArray2(n))
		binmyArray2 = binMyArray2 & dectobin(myInt)
	next
	document.write binmyArray2 & " - " & bintodec(binmyArray2) & "<br>"

'myArray3 to Binary
	for n = 0 to 3
		myInt= CInt(myArray3(n))
		binmyArray3 = binMyArray3 & dectobin(myInt)
	next
	document.write binmyArray3 & " - " & bintodec(binmyArray3) & "<br>"

	if (bintodec(binmyArray1) <= bintodec(binmyArray3)) and (bintodec(binmyArray3) <= bintodec(binmyArray2)) then
		chkIPRange = true
	else 
		chkIPRange = false
	end if
End Function

Function DecToBin(dIn)
   DecToBin = ""
   While dIn >= 1
     if dIn Mod 2 then
     DecToBin = "1" & DecToBin
     else 
     DecToBin = "0" & DecToBin
     end if
     dIn = dIn \ 2
   Wend
   if len(DecToBin) < 8 then
	for i = 1 to 8 - len(DecToBin)
		DecToBin = "0" & DecToBin
	next
	end if 
End Function

Function BinToDec(sIn)
   Dim x
   BinToDec = 0
   For x = 1 To Len(sIn)
     BinToDec = BinToDec + (CInt(Mid(sIn, x, 1)) * (2 ^ (Len(sIn) - x)))
   Next
End Function


Probably could be cleaned up a bit more.

How it works;

Each of the four numbers in the i.p. address must be converted to binary (8 digits)
and concatenated e.g.

127.0.0.12 = 01111111.00000000.00000000.00001100
01111111.00000000.00000000.00001100 ends up as 01111111000000000000000000001100 and is then converted to decimal so
01111111000000000000000000001100 = 2130706444

Once each i.p. address is converted to decimal its easy to compare them.

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page

mios
Junior Member

United Kingdom
101 Posts

Posted - 24 March 2005 :  09:07:01  Show Profile  Send mios an ICQ Message
It's easier if you convert the ip address to a long value and use this to compare:

Function Dot2LongIP (ByVal DottedIP)
Dim i, pos
Dim PrevPos, num
If DottedIP = "" Then
    Dot2LongIP = 0
Else
    For i = 1 To 4
        pos = InStr(PrevPos + 1, DottedIP, ".", 1)
        If i = 4 Then 
            pos = Len(DottedIP) + 1
        End If
        num = Int(Mid(DottedIP, PrevPos + 1, pos - PrevPos - 1))
        PrevPos = pos
        Dot2LongIP = ((num Mod 256) * (256 ^ (4 - i))) + Dot2LongIP
    Next
End If
End Function 

If IPAddress > Dot2LongIP("127.0.64.1") AND IPAddress < Dot2LongIP("127.0.79.254") then .......
Go to Top of Page

Podge
Support Moderator

Ireland
3776 Posts

Posted - 24 March 2005 :  11:07:02  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
Cool.

Can you give me an example of what the output from Dot2LongIP("127.0.64.1") is ?


Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page

Podge
Support Moderator

Ireland
3776 Posts

Posted - 24 March 2005 :  11:13:05  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
Actually. No need.

Its a lot simpler.

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.3 seconds. Powered By: Snitz Forums 2000 Version 3.4.07