DEV Community

Cover image for Testing if IP is within a CIDR Range
James Moberg
James Moberg

Posted on

Testing if IP is within a CIDR Range

I recently wrote a blog entry about using a third-party Java library to standardize IPv4 & IPv6 IP addresses using ColdFusion.

The Apache Commons Java library (that is built into ColdFusion) was previously the solution that we used to determine if an IPv4 address is within a CIDR Range... but that was when all our IP-related data was strictly IPv4. Now that we're leveraging a cloud WAF provider that provides us with a true remote IP address (some are IPv6), we need to consider this when logging or applying access rules so we're not just blindly logging our gateway's IP address. (NOTE: We're now in the process of migrating to a third WAF/CDN provider as the first two companies that we partnered with declared chapter 11. SMH)

For more info, Ben Nadal has an article from 2018 entitled "Checking To See If An IP v4 Address Is In A CIDR Range Using ColdFusion And SubnetUtils".

If you wanted to test whether an IPv6 IP is within an IPv6 CIDR range, it can't be done natively with ColdFusion (to my knowledge) and requires using a third-party like the jgonian commons ip math java library.

For more info, Ben Nadal has an article from 2018 entitled "Using Commons IP Math To Check If An IP Address Exists In An IPv4 Or IPv6 CIDR Range In ColdFusion".

I started using the seancfoley IPAddress Java library because:

  • it validates IPv4/IPv6 IP addresses
  • it converts an IP string to binary object (for database storage & better indexed queries)
  • it converts a binary object to an IP string
  • it normalizes the IPv6 string (since the IPv6 syntax can assume different abbreviated formats)
  • it tests if an IPv4/IPv6 address is within a CIDR range
  • it's used by Akamai, Amazon, Atlassian, ATT, Cisco, Citibank, Comcast, Disney, Evernote, Fortinet, Google, Hulu, IBM, ICANN, Microsoft, MongoDB, Netflix, NIST, Oracle, Spotify... and more... including my ColdFusion applications!

We're only performing the above functions (so far), but the library supports many functions with lots of java code examples demonstrating conversion, subnetting, matching/comparing and creating/parsing functionality.

Here's a CFML proof-of-concept code that will accept a CIDR range and identify whether an IPv4 or IPv6 address is within that range. Enjoy!

Source Code

https://gist.github.com/JamoCA/6cee1fae80e25a83be13a840621d1b9d

<cfscript>
/* isIpInCIDRRange (Determine if the given address range contains the given IP address. Supports IPv4 & IPv6.)
2024-12-05
Requires IPAddress java library from https://github.com/seancfoley/IPAddress
Author: James Moberg http://sunstarmedia.com @sunstarmedia
GIST: https://gist.github.com/JamoCA/6cee1fae80e25a83be13a840621d1b9d
Blog: https://dev.to/gamesover/testing-if-ip-is-within-a-cidr-range-29jb
X/Twitter: https://x.com/gamesover/status/1864714591646945491
LinkedIn: https://www.linkedin.com/posts/jamesmoberg_heres-how-we-are-testing-if-an-ip-address-activity-7270480668407537665-XUlP
*/
public boolean function isIpInCIDRRange(required string ipRange, required string ipAddress) output=false hint="I determine if the given IP address range contains the given IP address. Supports IPv4 & IPv6." {
try {
local.network = createobject("java", "inet.ipaddr.IPAddressString").init(arguments.ipRange).getAddress();
local.ip = createobject("java", "inet.ipaddr.IPAddressString").init(arguments.ipAddress).getAddress();
return javacast("boolean", local.network.contains(local.ip));
} catch (any error){}
return javacast("boolean", false);
}
</cfscript>

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay