dreamworkAPI
Ctrl K
GitHub
classDocs / IPv6

IPv6

classsharedmenusource
Object → IPv6

An object that represents an Internet Protocol v6 address as eight 16-bit groups.

Groups are stored in the array part of the object, address[ 1 ] is the most significant group.

Supports tostring (compressed RFC 5952 form), comparison operators (==, <, <=), the length operator (#address is always 8) and unary minus (-address is the bitwise NOT of the address).

Constructor

IPv6(group1: integer?, group2: integer?, group3: integer?, group4: integer?, group5: integer?, group6: integer?, group7: integer?, group8: integer?): IPv6

Fields

NameTypeDescription
[ 1 ]integerThe first group. <0-65535>
[ 2 ]integerThe second group. <0-65535>
[ 3 ]integerThe third group. <0-65535>
[ 4 ]integerThe fourth group. <0-65535>
[ 5 ]integerThe fifth group. <0-65535>
[ 6 ]integerThe sixth group. <0-65535>
[ 7 ]integerThe seventh group. <0-65535>
[ 8 ]integerThe eighth group. <0-65535>

Static functions

fromIPv4Creates an IPv4-mapped IPv6 address (::ffff:a.b.c.d) from an IPv4 address.
fromPrefixReturns the network mask address for a given CIDR prefix length.
is6to4Returns whether the address is a 6to4 address (2002::/16, RFC 3056).
isBenchmarkingReturns whether the address is reserved for network benchmarking (2001:2::/48, RFC 5180).
isDiscardReturns whether the address is a discard-only address (100::/64, RFC 6666).
isDocumentationReturns whether the address is reserved for documentation and examples (2001:db8::/32, RFC 3849).
isLinkLocalReturns whether the address is a link-local unicast address (fe80::/10, RFC 4291).
isMulticastReturns whether the address is a multicast address (ff00::/8, RFC 4291).
isSiteLocalReturns whether the address is a site-local unicast address (fec0::/10).
isTeredoReturns whether the address is a Teredo address (2001::/32, RFC 4380).
isUniqueLocalReturns whether the address is a unique-local address (fc00::/7, RFC 4193).
parseParses an IPv6 address string with an optional CIDR prefix length, e.g. 2001:db8::1/32.

#IPv6.fromIPv4

sharedmenunodiscardsource
function IPv6.fromIPv4(ipv4_address: IPv4): IPv6

Creates an IPv4-mapped IPv6 address (::ffff:a.b.c.d) from an IPv4 address.

lua
print( IPv6.fromIPv4( 0x7F000001 ) ) -- ::ffff:7f00:1
Parameters
NameTypeDescription
ipv4_addressIPv4The IPv4 address.
Returns
TypeNameDescription
IPv6ipv6_addressThe IPv4-mapped IPv6 address.

#IPv6.fromPrefix

sharedmenunodiscardsource
function IPv6.fromPrefix(prefix: integer): IPv6

Returns the network mask address for a given CIDR prefix length.

lua
print( IPv6.fromPrefix( 64 ) ) -- ffff:ffff:ffff:ffff::
Parameters
NameTypeDescription
prefixintegerThe CIDR prefix length. <0-128>, throws an error if out of range.
Returns
TypeNameDescription
IPv6mask_addressThe network mask address.

#IPv6.is6to4

sharedmenusource
function IPv6.is6to4(): boolean

Returns whether the address is a 6to4 address (2002::/16, RFC 3056).

Returns
TypeNameDescription
boolean

#IPv6.isBenchmarking

sharedmenusource
function IPv6.isBenchmarking(): boolean

Returns whether the address is reserved for network benchmarking (2001:2::/48, RFC 5180).

Returns
TypeNameDescription
boolean

#IPv6.isDiscard

sharedmenusource
function IPv6.isDiscard(): boolean

Returns whether the address is a discard-only address (100::/64, RFC 6666).

Returns
TypeNameDescription
boolean

#IPv6.isDocumentation

sharedmenusource
function IPv6.isDocumentation(): boolean

Returns whether the address is reserved for documentation and examples (2001:db8::/32, RFC 3849).

Returns
TypeNameDescription
boolean

#IPv6.isLinkLocal

sharedmenusource
function IPv6.isLinkLocal(): boolean

Returns whether the address is a link-local unicast address (fe80::/10, RFC 4291).

Returns
TypeNameDescription
boolean

#IPv6.isMulticast

sharedmenusource
function IPv6.isMulticast(): boolean

Returns whether the address is a multicast address (ff00::/8, RFC 4291).

Returns
TypeNameDescription
boolean

#IPv6.isSiteLocal

sharedmenusource
function IPv6.isSiteLocal(): boolean

Returns whether the address is a site-local unicast address (fec0::/10).

Site-local addresses are deprecated by RFC 3879, but still recognized.

Returns
TypeNameDescription
boolean

#IPv6.isTeredo

sharedmenusource
function IPv6.isTeredo(): boolean

Returns whether the address is a Teredo address (2001::/32, RFC 4380).

Returns
TypeNameDescription
boolean

#IPv6.isUniqueLocal

sharedmenusource
function IPv6.isUniqueLocal(): boolean

Returns whether the address is a unique-local address (fc00::/7, RFC 4193).

Returns
TypeNameDescription
boolean

#IPv6.parse

sharedmenunodiscardsource
function IPv6.parse(ipv6_str: string, start_position?: integer, end_position?: integer): IPv6, integer

Parses an IPv6 address string with an optional CIDR prefix length, e.g. 2001:db8::1/32.

Handles the :: zero-groups compression.

Embedded IPv4 notation (::ffff:1.2.3.4) and zone indices (fe80::1%eth0) are not supported.

Throws an error if a group is empty, contains invalid characters or more than 4 hex digits, the address has a wrong number of groups or more than one ::, or the prefix length is not an integer in range 0-128.

Parameters
NameTypeDescription
ipv6_strstringThe IPv6 address string to parse.
start_positionoptionalintegerThe start position in the ipv6 string.
end_positionoptionalintegerThe end position in the ipv6 string.
Returns
TypeNameDescription
IPv6addressThe parsed IP address.
integerprefix_lengthThe CIDR prefix length. <0-128>, 128 if not specified.

Methods

cidrReturns the network address and the last address of the network for a given CIDR prefix length.
groupsReturns the groups of the address, from the most significant to the least significant.
inRangeReturns whether the address is within a given network.
isGlobalUnicastReturns whether the address is a global unicast address.
isIPv4CompatibleReturns whether the address is an IPv4-compatible IPv6 address (::a.b.c.d).
isIPv4MappedReturns whether the address is an IPv4-mapped IPv6 address (::ffff:a.b.c.d, RFC 4291).
isLoopbackReturns whether the address is the loopback address (::1).
isPrivateReturns whether the address belongs to a private or local-use range.
isPublicReturns whether the address is globally routable, i.e. a global unicast address that is not reserved.
isReservedReturns whether the address belongs to a special-purpose range.
isUnspecifiedReturns whether the address is the unspecified address (::).
reversePointerReturns the reverse DNS pointer (ip6.arpa) of the address.
toIPv4Extracts the embedded IPv4 address from an IPv4-mapped or IPv4-compatible address.
toStringReturns the string representation of the address in compressed RFC 5952 form.

#IPv6:cidr

sharedmenunodiscardsource
function IPv6:cidr(prefix_length: integer): IPv6, IPv6

Returns the network address and the last address of the network for a given CIDR prefix length.

IPv6 has no broadcast, the "broadcast" address is just the last address of the network.

lua
local address, prefix_length = IPv6.parse( "2001:db8::1/32" )
local network, broadcast = address:cidr( prefix_length ) -- 2001:db8::, 2001:db8:ffff:ffff:ffff:ffff:ffff:ffff
Parameters
NameTypeDescription
prefix_lengthintegerThe CIDR prefix length. <0-128>, throws an error if out of range.
Returns
TypeNameDescription
IPv6network_addressThe network (first) address.
IPv6broadcast_addressThe last address of the network.

#IPv6:groups

sharedmenunodiscardsource
function IPv6:groups(): integer, integer, integer, integer, integer, integer, integer, integer

Returns the groups of the address, from the most significant to the least significant.

Returns
TypeNameDescription
integergroup1The first group. <0-65535>
integergroup2The second group. <0-65535>
integergroup3The third group. <0-65535>
integergroup4The fourth group. <0-65535>
integergroup5The fifth group. <0-65535>
integergroup6The sixth group. <0-65535>
integergroup7The seventh group. <0-65535>
integergroup8The eighth group. <0-65535>

#IPv6:inRange

sharedmenunodiscardsource
function IPv6:inRange(network: IPv6, broadcast: IPv6): boolean

Returns whether the address is within a given network.

lua
local network, broadcast = IPv6.parse( "2001:db8::" ):cidr( 32 )
IPv6.parse( "2001:db8::1" ):inRange( network, broadcast ) -- true
Parameters
NameTypeDescription
networkIPv6The network (first) address.
broadcastIPv6The last address of the network.
Returns
TypeNameDescription
booleanin_rangetrue if the address is within the network, false otherwise.

#IPv6:isGlobalUnicast

sharedmenunodiscardsource
function IPv6:isGlobalUnicast(): boolean

Returns whether the address is a global unicast address.

Any address that is not multicast, unspecified, loopback, link-local, site-local or unique-local.

Returns
TypeNameDescription
booleanis_global_unicasttrue if the address is a global unicast address, false otherwise.

#IPv6:isIPv4Compatible

sharedmenunodiscardsource
function IPv6:isIPv4Compatible(): boolean

Returns whether the address is an IPv4-compatible IPv6 address (::a.b.c.d).

IPv4-compatible addresses are deprecated by RFC 4291.

The unspecified (::) and loopback (::1) addresses are not considered IPv4-compatible.

Returns
TypeNameDescription
booleanis_ipv4_compatibletrue if the address is an IPv4-compatible address, false otherwise.

#IPv6:isIPv4Mapped

sharedmenunodiscardsource
function IPv6:isIPv4Mapped(): boolean

Returns whether the address is an IPv4-mapped IPv6 address (::ffff:a.b.c.d, RFC 4291).

Returns
TypeNameDescription
booleanis_ipv4_mappedtrue if the address is an IPv4-mapped address, false otherwise.

#IPv6:isLoopback

sharedmenunodiscardsource
function IPv6:isLoopback(): boolean

Returns whether the address is the loopback address (::1).

Returns
TypeNameDescription
booleanis_loopbacktrue if the address is the loopback address, false otherwise.

#IPv6:isPrivate

sharedmenunodiscardsource
function IPv6:isPrivate(): boolean

Returns whether the address belongs to a private or local-use range.

Includes the unspecified, loopback, link-local, site-local, unique-local and discard-only addresses.

Returns
TypeNameDescription
booleanis_privatetrue if the address is private, false otherwise.

#IPv6:isPublic

sharedmenunodiscardsource
function IPv6:isPublic(): boolean

Returns whether the address is globally routable, i.e. a global unicast address that is not reserved.

Returns
TypeNameDescription
booleanis_publictrue if the address is public, false otherwise.

#IPv6:isReserved

sharedmenunodiscardsource
function IPv6:isReserved(): boolean

Returns whether the address belongs to a special-purpose range.

Includes the documentation, benchmarking, IPv4-mapped, IPv4-compatible, 6to4 and Teredo addresses.

Returns
TypeNameDescription
booleanis_reservedtrue if the address is reserved, false otherwise.

#IPv6:isUnspecified

sharedmenunodiscardsource
function IPv6:isUnspecified(): boolean

Returns whether the address is the unspecified address (::).

Returns
TypeNameDescription
booleanis_unspecifiedtrue if the address is the unspecified address, false otherwise.

#IPv6:reversePointer

sharedmenunodiscardsource
function IPv6:reversePointer(): string

Returns the reverse DNS pointer (ip6.arpa) of the address.

lua
IPv6.parse( "2001:db8::1" ):reversePointer()
-- "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa"
Returns
TypeNameDescription
stringptrThe reverse DNS pointer of the address.

#IPv6:toIPv4

sharedmenunodiscardsource
function IPv6:toIPv4(): IPv4

Extracts the embedded IPv4 address from an IPv4-mapped or IPv4-compatible address.

The last two groups are always returned as an IPv4 address, even if the address is not IPv4-mapped or IPv4-compatible, so validate the address before calling this method.

lua
local address = IPv6.parse( "::ffff:7f00:1" )
if address:isIPv4Mapped() then
    print( ipv4.toString( address:toIPv4() ) ) -- 127.0.0.1
end
Returns
TypeNameDescription
IPv4ipv4_addressThe embedded IPv4 address.
See also
  • IPv6.isIPv4Mapped
  • IPv6.isIPv4Compatible

#IPv6:toString

sharedmenunodiscardsource
function IPv6:toString(): string

Returns the string representation of the address in compressed RFC 5952 form.

Same as tostring( address ).

lua
IPv6( 0x2001, 0xdb8, 0, 0, 0, 0, 0, 1 ):toString() -- "2001:db8::1"
Returns
TypeNameDescription
stringstrThe string representation of the address.
Generated from the dreamwork meta branch.