Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Last active November 19, 2024 20:05
Show Gist options
  • Save JamoCA/0e638da6927d341ed61e411f5789b4ec to your computer and use it in GitHub Desktop.
Save JamoCA/0e638da6927d341ed61e411f5789b4ec to your computer and use it in GitHub Desktop.
Using IPAddress Java Library with ColdFusion to Standardize IPv4 & IPv6 Addresses / cfml
<cfscript>
/* ipAddressStringToBinary & ipAddressBinaryToString
2024-11-19
Requires IPAddress java library from https://github.com/seancfoley/IPAddress
Author: James Moberg http://sunstarmedia.com @sunstarmedia
GIST: https://gist.github.com/JamoCA/0e638da6927d341ed61e411f5789b4ec
Blog: https://dev.to/gamesover/using-ipaddress-java-library-with-coldfusion-to-standardize-ipv4-ipv6-addresses-bcn
X/Twitter: https://x.com/gamesover/status/1858964208043651497
LinkedIn: https://www.linkedin.com/posts/jamesmoberg_coldfusion-cfml-activity-7264730444338737152-e8ce
*/
public binary function ipAddressStringToBinary(required string ipString) hint="Converts IPV4 and IPV6 string representations to 4 byte and 16 bytes binary values respectively." {
try {
return createobject("java", "inet.ipaddr.IPAddressString").init(javacast("string", trim(arguments.ipString))).getAddress().getBytes();
} catch (any error) {
return createobject("java", "inet.ipaddr.IPAddressString").init("0").getAddress().getBytes();
}
}
public string function ipAddressBinaryToString(required any ipBinary, boolean normalized=true) hint="Converts 4 bytes values to IPV4 and 16 byte values to IPV6 string representations. Note that this function does not shorten addresses." {
if(!isvalid("binary", arguments.ipBinary)) return "";
try {
local.ip = createobject("java", "inet.ipaddr.IPAddressNetwork$IPAddressGenerator").from(arguments.ipBinary);
if (arguments.normalized){
return local.ip.toNormalizedString();
} else {
return local.ip.toString();
}
} catch (any error){
return "";
}
}
</cfscript>
<cfscript>
/* Tests for ipAddressStringToBinary & ipAddressBinaryToString
2024-11-19
GIST: https://gist.github.com/JamoCA/0e638da6927d341ed61e411f5789b4ec
*/
tests = [
"0000:0000:0000:0000:0000:0000:0000:0001"
,"0001:0002:0003:0040:0500:0600:7000:0089"
,"2001:0db8:85a3:08d3:1319:0000:0370:7348"
,"2001:db8:85a3:8d3:1319::370:7348"
,"2001:db8:85a3:8d3:1319:0000:370:7348"
,"192.168.1.72"
,"192.88.99.15"
,"10.0.15.25"
,"127.0.0.1"
,"127...1"
,""
,"abc"
];
</cfscript>
<cfoutput>
<table border="1" cellspacing="0">
<thead>
<tr>
<th>IP</th>
<th>Valid</th>
<th>isIPAddress</th>
<th>isLoopback</th>
<th>isIPv4</th>
<th>isIPv6</th>
<th>isMixedIPv6</th>
<th>toNormalizedString</th>
<th>toBinary</th>
<th>toString</th>
</tr>
</thead>
<tbody>
<cfloop array="#tests#" index="test">
<cfset ip = ipAddressString.init(test)>
<cfset b = ipAddressStringToBinary(ip)>
<cfset n = ip.toNormalizedString()>
<cfset i = isvalid("binary", b) ? ipAddressBinaryToString(b) : "">
<tr>
<td>#test#</td>
<td>#ip.isValid()#</td>
<td>#ip.isIPAddress()#</td>
<td>#ip.isLoopback()#</td>
<td>#ip.isIPv4()#</td>
<td>#ip.isIPv6()#</td>
<td>#ip.isMixedIPv6()#</td>
<td<cfif test neq n> style="background-color:yellow;"</cfif>>#n#</td>
<td>#b.toString()#</td>
<td<cfif i neq n> style="background-color:red;"</cfif>>#i#</td>
</tr>
</cfloop>
</tbody>
</table>
</cfoutput>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment