Functions for Working with IPv4 and IPv6 Addresses
IPv4NumToString
Takes a UInt32 number. Interprets it as an IPv4 address in big endian. Returns a string containing the corresponding IPv4 address in the format A.B.C.d (dot-separated numbers in decimal form).
Alias: INET_NTOA
.
IPv4StringToNum
The reverse function of IPv4NumToString. If the IPv4 address has an invalid format, it throws exception.
Alias: INET_ATON
.
IPv4StringToNumOrDefault(s)
Same as IPv4StringToNum
, but if the IPv4 address has an invalid format, it returns 0.
IPv4StringToNumOrNull(s)
Same as IPv4StringToNum
, but if the IPv4 address has an invalid format, it returns null.
IPv4NumToStringClassC(num)
Similar to IPv4NumToString, but using xxx instead of the last octet.
Example:
Since using 'xxx' is highly unusual, this may be changed in the future. We recommend that you do not rely on the exact format of this fragment.
IPv6NumToString(x)
Accepts a FixedString(16) value containing the IPv6 address in binary format. Returns a string containing this address in text format. IPv6-mapped IPv4 addresses are output in the format ::ffff:111.222.33.44.
Alias: INET6_NTOA
.
Examples:
IPv6StringToNum
The reverse function of IPv6NumToString. If the IPv6 address has an invalid format, it throws exception.
If the input string contains a valid IPv4 address, returns its IPv6 equivalent. HEX can be uppercase or lowercase.
Alias: INET6_ATON
.
Syntax
Argument
string
— IP address. String.
Returned value
- IPv6 address in binary format. FixedString(16).
Example
Query:
Result:
See Also
IPv6StringToNumOrDefault(s)
Same as IPv6StringToNum
, but if the IPv6 address has an invalid format, it returns 0.
IPv6StringToNumOrNull(s)
Same as IPv6StringToNum
, but if the IPv6 address has an invalid format, it returns null.
IPv4ToIPv6(x)
Takes a UInt32
number. Interprets it as an IPv4 address in big endian. Returns a FixedString(16)
value containing the IPv6 address in binary format. Examples:
cutIPv6(x, bytesToCutForIPv6, bytesToCutForIPv4)
Accepts a FixedString(16) value containing the IPv6 address in binary format. Returns a string containing the address of the specified number of bytes removed in text format. For example:
IPv4CIDRToRange(ipv4, Cidr),
Accepts an IPv4 and an UInt8 value containing the CIDR. Return a tuple with two IPv4 containing the lower range and the higher range of the subnet.
IPv6CIDRToRange(ipv6, Cidr),
Accepts an IPv6 and an UInt8 value containing the CIDR. Return a tuple with two IPv6 containing the lower range and the higher range of the subnet.
toIPv4
Like IPv4StringToNum
but takes a string form of IPv4 address and returns value of IPv4 type.
Syntax
Arguments
string
— IPv4 address. String.
Returned value
string
converted to the IPv4 address. IPv4.
Examples
Query:
Result:
Query:
Result:
toIPv4OrDefault
Same as toIPv4
, but if the IPv4 address has an invalid format, it returns 0.0.0.0
(0 IPv4), or the provided IPv4 default.
Syntax
Arguments
value
— IP address. String.default
(optional) — The value to return ifstring
has an invalid format. IPv4.
Returned value
string
converted to the current IPv4 address. String.
Example
Query:
Result:
toIPv4OrNull
Same as toIPv4
, but if the IPv4 address has an invalid format, it returns null.
Syntax
Arguments
string
— IP address. String.
Returned value
string
converted to the current IPv4 address, or null ifstring
is an invalid address. String.
Example
Query:
Result:
toIPv4OrZero
Same as toIPv4
, but if the IPv4 address has an invalid format, it returns 0.0.0.0
.
Syntax
Arguments
string
— IP address. String.
Returned value
string
converted to the current IPv4 address, or0.0.0.0
ifstring
is an invalid address. String.
Example
Query:
Result:
toIPv6
Converts a string form of IPv6 address to IPv6 type. If the IPv6 address has an invalid format, returns an empty value. Similar to IPv6StringToNum function, which converts IPv6 address to binary format.
If the input string contains a valid IPv4 address, then the IPv6 equivalent of the IPv4 address is returned.
Syntax
Argument
string
— IP address. String.
Returned value
- IP address. IPv6.
Examples
Query:
Result:
Query:
Result:
toIPv6OrDefault
Same as toIPv6
, but if the IPv6 address has an invalid format, it returns ::
(0 IPv6) or the provided IPv6 default.
Syntax
Argument
string
— IP address. String.default
(optional) — The value to return ifstring
has an invalid format. IPv6.
Returned value
- IPv6 address IPv6, otherwise
::
or the provided optional default ifstring
has an invalid format.
Example
Query:
Result:
toIPv6OrNull
Same as toIPv6
, but if the IPv6 address has an invalid format, it returns null.
Syntax
Argument
string
— IP address. String.
Returned value
- IP address. IPv6, or null if
string
is not a valid format.
Example
Query:
Result:
toIPv6OrZero
Same as toIPv6
, but if the IPv6 address has an invalid format, it returns ::
.
Syntax
Argument
string
— IP address. String.
Returned value
- IP address. IPv6, or
::
ifstring
is not a valid format.
Example
Query:
Result:
IPv6StringToNumOrDefault(s)
Same as toIPv6
, but if the IPv6 address has an invalid format, it returns 0.
IPv6StringToNumOrNull(s)
Same as toIPv6
, but if the IPv6 address has an invalid format, it returns null.
isIPv4String
Determines whether the input string is an IPv4 address or not. If string
is IPv6 address returns 0
.
Syntax
Arguments
string
— IP address. String.
Returned value
1
ifstring
is IPv4 address,0
otherwise. UInt8.
Examples
Query:
Result:
isIPv6String
Determines whether the input string is an IPv6 address or not. If string
is IPv4 address returns 0
.
Syntax
Arguments
string
— IP address. String.
Returned value
1
ifstring
is IPv6 address,0
otherwise. UInt8.
Examples
Query:
Result:
isIPAddressInRange
Determines if an IP address is contained in a network represented in the CIDR notation. Returns 1
if true, or 0
otherwise.
Syntax
This function accepts both IPv4 and IPv6 addresses (and networks) represented as strings. It returns 0
if the IP version of the address and the CIDR don't match.
Arguments
Returned value
1
or0
. UInt8.
Example
Query:
Result:
Query:
Result:
Query:
Result: