Web Connection User Discussions
Trying to use REGEX to discover if an ip address is public or private. Trying to beef up Request.GetIpAddress()
Gravatar is a globally recognized avatar based on your email address. Trying to use REGEX to discover if an ip address is public or private. Trying to beef up Request.GetIpAddress()
  Michael B
  All
  Feb 25, 2017 @ 09:48am

Howdy folks, I discovered today that WWWC discovers only the non routable version of my ip address (10.1.10.123) instead of (96.90.xx.xx). It does not do this on all machines, but it is doing it on mine. My local setup includes having a dedicated ip from Comcast. If I go to myipaddress.com I will get the 'real' 96.90.xx.xx that I mention above. I can even use httpget() to myipaddress.com and it will return 96.90.xx.xx but I cannot get it when developing locally. This gave me an idea that might include using RegEx to compare the ip address reported to see if it is public or private.

This led me to an article that included this Perl formatted regex.

^([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(?<!172\.(16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31))(?<!127)(?<!^10)(?<!^0)\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(?<!192\.168)(?<!172\.(16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31))\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(?<!\.0$)(?<!\.255$)$

I tried to get this to work in vfp without success.

Any regex experts out there with a suggestion?

Gravatar is a globally recognized avatar based on your email address. re: Trying to use REGEX to discover if an ip address is public or private. Trying to beef up Request.GetIpAddress()
  Randy Pearson
  Michael B
  Feb 25, 2017 @ 01:28pm

Hi Michael,

In case it helps, I use this UDF to decide if an IP (v4) address is local:

function IsPrivateIp(tcIP)
  return m.tcIp = "::1" or ;
    LIKE([127*], m.tcIp) or ; && LOCALHOST
    LIKE([192.168.*], m.tcIp) or ; 
    LIKE([172.16.*], m.tcIp) or ; 
    LIKE([10.*], m.tcIp) 
endfunc && IsPrivateIP

Gravatar is a globally recognized avatar based on your email address. re: Trying to use REGEX to discover if an ip address is public or private. Trying to beef up Request.GetIpAddress()
  Michael B
  Michael B
  Feb 26, 2017 @ 01:01pm

Thanks Randy. I thought of doing something like that, but I got concerned that it would be too hacky. Take a look at this article - https://tools.ietf.org/html/rfc3330 - it seems there are more candidates.

Gravatar is a globally recognized avatar based on your email address. re: Trying to use REGEX to discover if an ip address is public or private. Trying to beef up Request.GetIpAddress()
  Rick Strahl
  Michael B
  Feb 26, 2017 @ 02:27pm

Go for the simple solution first - always! Then do some metrics and see if it gets close to what you set out to accomplish. This might not catch everything, but it'll likely cut most and the few edge cases left may not be an issue.

+++ Rick ---

© 1996-2017