Cffunction / cfargument pass unsigned int32

I am working on creating a function that translates unsigned int32 to ipv4 address, and all this works fine until I check the top half of the range.

<cffunction name="Int32ToIPv4" returntype="string" output="false" access="public" hint="returns IPv4 address for int32 number">
    <cfargument name="nInt32" type="numeric" required="yes" hint="int32 to convert to ipv4 address">

    <cfreturn
        bitAnd((arguments.nInt32 / 16777216), 255) & "."
        & bitAnd((arguments.nInt32 / 65536), 255) & "."
        & bitAnd((arguments.nInt32 / 256), 255) & "."
        & bitAnd(arguments.nInt32, 255)
    >
</cffunction>

ColdFusion seems to interpret type = "numeric" as a 32-bit signed integer .

This example works:

Int32ToIPv4(nInt32 = 1181772947) = #Int32ToIPv4(nInt32 = 1181772947)# (expected value = 70.112.108.147)<br>

This example fails:

Int32ToIPv4(nInt32 = 3401190660) = #Int32ToIPv4(nInt32 = 3401190660)# (expected value = 202.186.13.4)<br>

Error message: "Cannot convert the value 3.40119066E9 to an integer because it cannot fit inside an integer."

Should I pass this number as a string and convert it to Java unsigned int before using it?

At some point, I want to do something similar with ipv6 addresses (unsigned 128-bit integer). Any recommendations for this type of data would be appreciated.

+4
2

bitAnd() 32- , cfargument.

, , (3) . - , bitAnd(), , INT. , , , .

 bitAnd(  (3401190660 / 16777216), 255 )  // Actual => 202.7267611032, 255
 bitAnd(  (3401190660 / 65536), 255 ) // Actual => 51898.05084233, 255
 bitAnd(  (3401190660 / 256), 255 )  // Actual =>13285901.0156, 255
 bitAnd(3401190660, 255) // Too big!

CF 32- , , ( ) CF. , Java 8+, , Integer.parseUnsignedInt(String).

, . , IP4 IP6, java, . , Guava InetAdress:

// IP4
input = 3401190660;
uint = createObject("java", "java.lang.Integer").parseUnsignedInt(input);
inet = createObject("Java", "com.google.common.net.InetAddresses");
result = inet.fromInteger(uint).getHostAddress();
+3

Java-, 32- int . , @Leigh .

<cffunction name="UInt32ToIPv4" returntype="string" output="no" access="public" hint="returns IPv4 address for uint32 number">
    <cfargument name="nUInt32" type="numeric" required="yes" hint="uint32 to convert to ipv4 address">

    <cfset local['JavaLangInteger'] = CreateObject("java", "java.lang.Integer")>
    <cfset local['JavaMathBigInteger'] = CreateObject("java", "java.math.BigInteger")>
    <cfset local['JavaNetInetAddress'] = CreateObject("java", "java.net.InetAddress")>

    <cfif arguments.nUInt32 EQ 0>
        <cfreturn "">
    </cfif>

    <cftry>
        <cfset local['vcIPv4'] = local.JavaNetInetAddress.getByAddress(local.JavaMathBigInteger.valueOf(local.JavaLangInteger.parseUnsignedInt(arguments.nUInt32)).toByteArray())>
        <cfreturn reReplace(local.vcIPv4, "[^0-9\.]", "", "all")>
        <cfcatch type="any">
            <cfreturn "">
        </cfcatch>
    </cftry>
</cffunction>

, , , java-.

ipv6 (unsigned 128-bit int to/from ipv6). , . , / .

+2

Source: https://habr.com/ru/post/1673848/


All Articles