How do the rest of you test Public vs Private IP addresses?
Posted: Tue Sep 06, 2011 1:32 am
This is a quickly put together utility thing to test for public vs private IP addresses I needed to come up with in a hurry earlier today for a temporary use:
Just wondering how others are testing programatically for public vs private IP addresses?
Code: Select all
; private IPv4 IP ranges
; 10.0.0.0 - 10.255.255.255 Class A
; 172.16.0.0 - 172.31.255.255 Class B
; 192.168.0.0 - 192.168.255.255 Class C
; IPv6 private local prefixs are
; fe80: And FC00::/7
Procedure.s TestForPrivatePublicIP(ip_in$)
Protected ipmid.l, priv10.l, priv172.l, priv192.l, privLnkLoc.l, ip$, loopip$
ip$ = ip_in$
If FindString(ip$, "10.", 1) <> 0 And Left(ip$,2) = "10"
priv10 = #True
EndIf
If FindString(ip$, "172.", 1) <> 0 And Left(ip$,3) = "172"
For ipmid = 16 To 31
loopip$ = "." + Str(ipmid) + "."
If Mid(ip$, 4, 4) = loopip$
priv172 = #True
EndIf
Next
EndIf
If FindString(ip$, "192.", 1) <> 0 And Left(ip$,3) = "192" And Mid(ip$, 4, 5) = ".168."
priv192 = #True
EndIf
; ipv6 test for private local - per RFC 4193 - http://tools.ietf.org/html/rfc4193
If (FindString(ip$, "fe80:", 1) <> 0 Or FindString(ip$, "FC00::/7", 1) <> 0) And (Left(ip$, 5) = "fe80:" Or Left(ip$, 8) = "FC00::/7")
privLnkLoc = #True
EndIf
Debug priv10
Debug priv172
Debug priv192
Debug privLnkLoc
If priv10 = #True Or priv172 = #True Or priv192 = #True Or privLnkLoc = #True
ProcedureReturn "This is a Private IP address" ;#True
Else
ProcedureReturn "This is a Public IP address" ;#False
EndIf
EndProcedure
;Debug TestForPrivatePublicIP("fe80::c56f:23df:e8fe:626a%15") ; ipv6
Debug TestForPrivatePublicIP("192.168.255.255") ; ipv4