RegEx pattern for reserved IP addresses

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
palinka
Senior user
Senior user
Posts: 4360
Joined: 2017-09-12 17:57

RegEx pattern for reserved IP addresses

Post by palinka » 2023-09-22 13:20

It works with IPv4 addresses only.

Code: Select all

^(0\.|10\.|127\.|169\.254|192\.0\.0\.|192\.0\.2\.|192\.88\.99|192\.168|198\.1(8|9)\.|198\.51\.100|203\.0\.113|233\.252\.0\.|100\.(6[4-9]|[789]\d|1([01]\d|2[0-7]))\.|2(2[4-9]|[345]))

User avatar
SorenR
Senior user
Senior user
Posts: 6128
Joined: 2006-08-21 15:38
Location: Denmark

Re: RegEx pattern for reserved IP addresses

Post by SorenR » 2023-09-22 16:51

https://www.iana.org/assignments/iana-i ... stry.xhtml

Check the columns "Forwardable" and "Globally Reachable". Only those marked "true" can be observed in the wild. :idea:
The column "Forwardable" show which IP addresses your Router allow or block if compliant with IANA.
SørenR.

To understand recursion, you must first understand recursion.

palinka
Senior user
Senior user
Posts: 4360
Joined: 2017-09-12 17:57

Re: RegEx pattern for reserved IP addresses

Post by palinka » 2023-09-22 17:14

I'm not going to worry about these 2 single IPs that will get caught up in the pattern. :lol: :lol:

192.0.0.9/32
192.0.0.10/32

Otherwise the list looks good (I think). I took the ranges from here: https://en.wikipedia.org/wiki/Reserved_IP_addresses

palinka
Senior user
Senior user
Posts: 4360
Joined: 2017-09-12 17:57

Re: RegEx pattern for reserved IP addresses

Post by palinka » 2023-09-22 19:22

I missed one. Here's the updated regex pattern.

Code: Select all

^(0\.|10\.|127\.|169\.254|172\.(1[6-9]|2\d|3[01])\.|192\.0\.0\.([02-8]|1[1-9]|[2-9][0-9]|1\d\d|2\d\d)|192\.0\.2\.|192\.88\.99|192\.168|198\.1(8|9)\.|198\.51\.100|203\.0\.113|233\.252\.0\.|100\.(6[4-9]|[789]\d|1([01]\d|2[0-7]))\.|2(2[4-9]|[345]))
https://regex101.com/r/XA4qg8/1

It even ignores those 2 /32 ips.

Of course, the pattern assumes a valid IPv4 address is being tested.

User avatar
SorenR
Senior user
Senior user
Posts: 6128
Joined: 2006-08-21 15:38
Location: Denmark

Re: RegEx pattern for reserved IP addresses

Post by SorenR » 2023-09-23 02:49

What on earth are you going to use it for??

AND how can you trust WIKIPEDIA over IANA?
SørenR.

To understand recursion, you must first understand recursion.

palinka
Senior user
Senior user
Posts: 4360
Joined: 2017-09-12 17:57

Re: RegEx pattern for reserved IP addresses

Post by palinka » 2023-09-23 07:51

SorenR wrote:
2023-09-23 02:49
What on earth are you going to use it for??

AND how can you trust WIKIPEDIA over IANA?
First, I updated it (the above regex101) based on the IANA page you provided. So thank you. :D And you're 100% right about wackypedia. :mrgreen:

In eventhandlers.vbs, its for skipping geoip tests - my event log is filled with "can't find IP" geoip errors and I just wanted a way to quiet those down. But I first needed it for my private geoip check website - reserved/unreachable IPs should be presented as such.

Its just some little thing that maybe a couple of tinkerers like me might find useful. :mrgreen:

User avatar
SorenR
Senior user
Senior user
Posts: 6128
Joined: 2006-08-21 15:38
Location: Denmark

Re: RegEx pattern for reserved IP addresses

Post by SorenR » 2023-09-23 17:15

palinka wrote:
2023-09-23 07:51
SorenR wrote:
2023-09-23 02:49
What on earth are you going to use it for??

AND how can you trust WIKIPEDIA over IANA?
First, I updated it (the above regex101) based on the IANA page you provided. So thank you. :D And you're 100% right about wackypedia. :mrgreen:

In eventhandlers.vbs, its for skipping geoip tests - my event log is filled with "can't find IP" geoip errors and I just wanted a way to quiet those down. But I first needed it for my private geoip check website - reserved/unreachable IPs should be presented as such.

Its just some little thing that maybe a couple of tinkerers like me might find useful. :mrgreen:
I exempt private IP's from GeoIP lookups.

Code: Select all

Function isPrivateIP(strIP)
    Dim strRegEx
    strRegEx = "(^10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)|" & _
               "(^172((\.1[6-9]{1})|(\.2[0-9]{1})|(\.3[0-1]{1}))\.[0-9]{1,3}\.[0-9]{1,3}$)|" & _
               "(^192\.168\.[0-9]{1,3}\.[0-9]{1,3}$)"
    isPrivateIP = Lookup(strRegEx, strIP)
End Function
I use two different GEOIP lookup services, one is the GeoLite database (with a ton of false lookups) and the other is an online service https://ipgeolocation.io/ that never really failed me so far. As a backup solution I also have https://ipapi.co

A failed lookup or a failed criteria is a failure and thus a denied connection. No more, no less ... No reason to make a big deal out of it. One line in the log ...
SørenR.

To understand recursion, you must first understand recursion.

Post Reply