ipv4
Provides utility functions for working with Internet Protocol v4 addresses as integers.
Addresses are plain unsigned 32-bit integers (IPv4 ), so they can be compared, sorted and used as table keys directly.
Networks are described by a prefix length (0-32) or by a pair of network and broadcast addresses, which can be obtained with ipv4.cidr .
Classes & types Fields Name Type Description isLinkLocalReturns whether an IP address is a link-local address (169.254.0.0/16, RFC 3927 ). isLoopbackReturns whether an IP address is a loopback address (127.0.0.0/8, RFC 1122 ). isMulticastReturns whether an IP address is a multicast address (224.0.0.0/4, RFC 5771 ). isReservedReturns whether an IP address is reserved for future use (240.0.0.0/4, RFC 1112 ). isSharedReturns whether an IP address is in the shared address space used by carrier-grade NAT (100.64.0.0/10, RFC 6598 ). isBenchmarkingReturns whether an IP address is reserved for network benchmarking (198.18.0.0/15, RFC 2544 ).
Functions ipv4.cdirReturns the network address and broadcast address for a given IPv4 address and CIDR prefix length. ipv4.cidrReturns the network address and broadcast address for a given IPv4 address and CIDR prefix length. ipv4.classReturns the classful network class of the given IP address. ipv4.countReturns the number of addresses in a network. ipv4.fromOctetsBuilds an IPv4 address from octets. ipv4.fromPrefixReturns the subnet mask address for a given CIDR prefix length. ipv4.inRangeReturns whether an IP address is within a given network. ipv4.isBroadcastReturns whether an IP address is the limited broadcast address (255.255.255.255). ipv4.isDocumentationReturns whether an IP address is reserved for documentation and examples (192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0… ipv4.isPrivateReturns whether the given IP address is private, i.e. not globally reachable. ipv4.isPublicReturns whether the given IP address is public, i.e. not private. ipv4.isUnspecifiedReturns whether an IP address is the unspecified address (0.0.0.0). ipv4.maskReturns the subnet mask address of a network, e.g. 255.255.255.0 for a /24 network. ipv4.octetsReturns the octets of an IPv4 address, from the most significant to the least significant. ipv4.parseParses an IPv4 address string with an optional CIDR prefix length, e.g. 192.168.0.1/24. ipv4.rangeReturns the first and last usable host addresses of a network. ipv4.reversePointerReturns the reverse DNS pointer (in-addr.arpa) of an IPv4 address. ipv4.toStringReturns the string representation of an IPv4 address in dotted-decimal notation. ipv4.wildcardReturns the wildcard (inverted subnet mask) address of a network, e.g. 0.0.0.255 for a /24 network.
function ipv4.cdir (ip_address : IPv4 , prefix_length : integer ): IPv4 , IPv4
Deprecated. Use ipv4.cidr instead, this is a misspelled alias kept for compatibility.
Returns the network address and broadcast address for a given IPv4 address and CIDR prefix length.
Parameters
Name Type Description ip_addressIPv4 The IP address. prefix_lengthinteger The CIDR prefix length. <0-32>
Returns
Type Name Description IPv4 network_addressThe network (first) address. IPv4 broadcast_addressThe broadcast (last) address.
function ipv4.cidr (ip_address : IPv4 , prefix_length : integer ): IPv4 , IPv4
Returns the network address and broadcast address for a given IPv4 address and CIDR prefix length.
lua Copy
ipv4.cidr ( ipv4.parse ( "192.168.1.42/24" ) ) -- 192.168.1.0, 192.168.1.255
Parameters
Name Type Description ip_addressIPv4 The IP address. prefix_lengthinteger The CIDR prefix length. <0-32>, throws an error if out of range.
Returns
Type Name Description IPv4 network_addressThe network (first) address. IPv4 broadcast_addressThe broadcast (last) address.
function ipv4.class (ip_address : IPv4 ): IPv4.Class
Returns the classful network class of the given IP address.
A: 0.0.0.0 - 127.255.255.255B: 128.0.0.0 - 191.255.255.255C: 192.0.0.0 - 223.255.255.255D: 224.0.0.0 - 239.255.255.255 (multicast)E: 240.0.0.0 - 255.255.255.255 (reserved)
Parameters
Name Type Description ip_addressIPv4 The IP address.
Returns
Type Name Description IPv4.Class classThe class of the IP address.
function ipv4.count (network_address : IPv4 , broadcast_address : IPv4 ): integer , integer
Returns the number of addresses in a network.
Parameters
Name Type Description network_addressIPv4 The network address. broadcast_addressIPv4 The broadcast address.
Returns
Type Name Description integer total_countThe total number of addresses in the network. integer usable_countThe number of usable host addresses in the network, excluding the network and broadcast addresses.
function ipv4.fromOctets (a : integer , b : integer , c : integer , d : integer ): IPv4
Builds an IPv4 address from octets.
Octets out of the 0-255 range are clamped.
lua Copy
ipv4.fromOctets ( 127 , 0 , 0 , 1 ) -- 0x7F000001
Parameters
Name Type Description ainteger The first octet. <0-255> binteger The second octet. <0-255> cinteger The third octet. <0-255> dinteger The fourth octet. <0-255>
Returns
Type Name Description IPv4 ip_addressThe IP address.
function ipv4.fromPrefix (prefix_length : integer ): IPv4
Returns the subnet mask address for a given CIDR prefix length.
Prefix lengths out of the 0-32 range are clamped.
lua Copy
ipv4.fromPrefix ( 24 ) -- 0xFFFFFF00 (255.255.255.0)
Parameters
Name Type Description prefix_lengthinteger The CIDR prefix length. <0-32>
Returns
Type Name Description IPv4 mask_addressThe subnet mask address.
function ipv4.inRange (ip_address : IPv4 , network_address : IPv4 , broadcast_address : IPv4 ): boolean
Returns whether an IP address is within a given network.
Parameters
Name Type Description ip_addressIPv4 The IP address. network_addressIPv4 The network address. broadcast_addressIPv4 The broadcast address.
Returns
Type Name Description boolean in_rangetrue if the IP address is within the network, false otherwise.
function ipv4.isBroadcast (ip_address : IPv4 ): boolean
Returns whether an IP address is the limited broadcast address (255.255.255.255).
Parameters
Name Type Description ip_addressIPv4 The IP address to check.
Returns
Type Name Description boolean is_broadcasttrue if the IP address is the limited broadcast address, false otherwise.
function ipv4.isDocumentation (ip_address : IPv4 ): boolean
Returns whether an IP address is reserved for documentation and examples (192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24, RFC 5737 ).
Parameters
Name Type Description ip_addressIPv4 The IP address to check.
Returns
Type Name Description boolean is_documentationtrue if the IP address is a documentation address, false otherwise.
function ipv4.isPrivate (ip_address : IPv4 ): boolean
Returns whether the given IP address is private, i.e. not globally reachable.
Follows the IANA IPv4 Special-Purpose Address Registry , the same way as Python's ipaddress.IPv4Address.is_private.
The shared address space (100.64.0.0/10) is not considered private.
Parameters
Name Type Description ip_addressIPv4 The IP address.
Returns
Type Name Description boolean is_privatetrue if the IP address is private, false otherwise.
function ipv4.isPublic (ip_address : IPv4 ): boolean
Returns whether the given IP address is public, i.e. not private.
Note that the shared address space (100.64.0.0/10) is considered public by this function.
Parameters
Name Type Description ip_addressIPv4 The IP address.
Returns
Type Name Description boolean is_publictrue if the IP address is public, false otherwise.
function ipv4.isUnspecified (ip_address : IPv4 ): boolean
Returns whether an IP address is the unspecified address (0.0.0.0).
Parameters
Name Type Description ip_addressIPv4 The IP address to check.
Returns
Type Name Description boolean is_unspecifiedtrue if the IP address is the unspecified address, false otherwise.
function ipv4.mask (network_address : IPv4 , broadcast_address : IPv4 ): IPv4
Returns the subnet mask address of a network, e.g. 255.255.255.0 for a /24 network.
Parameters
Name Type Description network_addressIPv4 The network address. broadcast_addressIPv4 The broadcast address.
Returns
Type Name Description IPv4 mask_addressThe subnet mask address.
function ipv4.octets (ip_address : IPv4 ): integer , integer , integer , integer
Returns the octets of an IPv4 address, from the most significant to the least significant.
lua Copy
ipv4.octets ( 0x7F000001 ) -- 127, 0, 0, 1
Parameters
Name Type Description ip_addressIPv4 The IP address.
Returns
Type Name Description integer aThe first octet. <0-255> integer bThe second octet. <0-255> integer cThe third octet. <0-255> integer dThe fourth octet. <0-255>
function ipv4.parse (ipv4_str : string , start_position? : integer , end_position? : integer ): IPv4 , integer
Parses an IPv4 address string with an optional CIDR prefix length, e.g. 192.168.0.1/24.
Each octet supports:
Hexadecimal : prefixed with 0x or 0X (e.g., 0x7F), a bare 0x is 0.Octal : prefixed with 0 followed by octal digits (e.g., 0177).Decimal : standard base-10 digits (e.g., 127).Addresses with fewer than 4 octets are accepted, the last given octet becomes the last octet of the address, e.g. 127.1 is parsed as 127.0.0.1.
The prefix length must be decimal digits only.
Throws an error if an octet is empty, contains invalid characters, is out of range or there are more than 4 octets, or if the prefix length is empty, contains invalid characters or is greater than 32.
Parameters
Name Type Description ipv4_strstring The IPv4 address string to parse. start_positionoptional integer The start position in the ipv4 string. end_positionoptional integer The end position in the ipv4 string.
Returns
Type Name Description IPv4 ip_addressThe parsed IP address. integer prefix_lengthThe CIDR prefix length. <0-32>, 32 if not specified.
function ipv4.range (network_address : IPv4 , broadcast_address : IPv4 ): IPv4 , IPv4
Returns the first and last usable host addresses of a network.
The network and broadcast addresses are excluded, so the result is meaningless for /31 and /32 networks.
Parameters
Name Type Description network_addressIPv4 The network address. broadcast_addressIPv4 The broadcast address.
Returns
Type Name Description IPv4 firstThe first usable address in the network. IPv4 lastThe last usable address in the network.
function ipv4.reversePointer (ip_address : IPv4 ): string
Returns the reverse DNS pointer (in-addr.arpa) of an IPv4 address.
lua Copy
ipv4.reversePointer ( 0x7F000001 ) -- "1.0.0.127.in-addr.arpa"
Parameters
Name Type Description ip_addressIPv4 The IP address.
Returns
Type Name Description string ptrThe reverse DNS pointer of the IP address.
function ipv4.toString (ip_address : IPv4 ): string
Returns the string representation of an IPv4 address in dotted-decimal notation.
lua Copy
ipv4.toString ( 0x7F000001 ) -- "127.0.0.1"
Parameters
Name Type Description ip_addressIPv4 The IP address.
Returns
Type Name Description string strThe string representation of the IP address.
function ipv4.wildcard (network_address : IPv4 , broadcast_address : IPv4 ): IPv4
Returns the wildcard (inverted subnet mask) address of a network, e.g. 0.0.0.255 for a /24 network.
Parameters
Name Type Description network_addressIPv4 The network address. broadcast_addressIPv4 The broadcast address.
Returns
Type Name Description IPv4 wildcard_addressThe wildcard address.