Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Last active December 5, 2024 16:55
Show Gist options
  • Save JamoCA/6cee1fae80e25a83be13a840621d1b9d to your computer and use it in GitHub Desktop.
Save JamoCA/6cee1fae80e25a83be13a840621d1b9d to your computer and use it in GitHub Desktop.
Using IPAddress Java Library with ColdFusion to determine if a given address range contains a given IP address. Supports IPv4 & IPv6. cfml
<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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment