Module:IP/doc: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
add IPAddress methods
(start work on the docs)
 
(add IPAddress methods)
Line 55:
IPAddress.new('1.2.3.4') .. ' foo' -- "1.2.3.4 foo"
IPAddress.new('1.2.3.4') .. IPAddress.new('5.6.7.8') -- "1.2.3.45.6.7.8"
</source>
 
IPAddress objects have several methods, outlined below.
 
=== getIP ===
 
<source lang="lua">
ipAddress:getIP()
</source>
 
Returns a string representation of the IP address. IPv6 addresses are abbreviated if possible.
 
'''Examples'''
 
<source lang="lua">
IPAddress.new('1.2.3.4'):getIP() -- "1.2.3.4"
IPAddress.new('2001:db8::ff00:12:3456'):getIP() -- "2001:db8::ff00:12:3456"
IPAddress.new('2001:db8:0:0:0:0:0:0'):getIP() -- "2001:db8::"
</source>
 
=== getVersion ===
 
<source lang="lua">
ipAddress:getVersion()
</source>
 
Returns the version of the IP protocol being used. This is "IPv4" for IPv4 addresses, and "IPv6" for IPv6 addresses.
 
'''Examples'''
 
<source lang="lua">
IPAddress.new('1.2.3.4'):getVersion() -- "IPv4"
IPAddress.new('2001:db8::ff00:12:3456'):getVersion() -- "IPv6"
</source>
 
=== isIPv4 ===
 
<source lang="lua">
ipAddress:isIPv4()
</source>
 
Returns true if the IP address is an IPv4 address, and false otherwise.
 
'''Examples'''
 
<source lang="lua">
IPAddress.new('1.2.3.4'):isIPv4() -- true
IPAddress.new('2001:db8::ff00:12:3456'):isIPv4() -- false
</source>
 
=== isIPv6 ===
 
<source lang="lua">
ipAddress:isIPv6()
</source>
 
Returns true if the IP address is an IPv6 address, and false otherwise.
 
'''Examples'''
 
<source lang="lua">
IPAddress.new('1.2.3.4'):isIPv6() -- false
IPAddress.new('2001:db8::ff00:12:3456'):isIPv6() -- true
</source>
 
=== isInSubnet ===
 
<source lang="lua">
ipAddress:isInSubnet(subnet)
</source>
 
Returns true if the IP address is in the subnet <var>subnet</var>, and false otherwise. <var>subnet</var> may be a [[#Subnet|Subnet object]] or a [[CIDR]] string.
 
'''Examples'''
 
<source lang="lua">
IPAddress.new('1.2.3.4'):isInSubnet('1.2.3.0/24') -- true
IPAddress.new('1.2.3.4'):isInSubnet('1.2.4.0/24') -- false
IPAddress.new('1.2.3.4'):isInSubnet(Subnet.new('1.2.3.0/24')) -- true
IPAddress.new('2001:db8::ff00:12:3456'):isInSubnet('2001:db8:ff00:12:0/112') -- true
</source>
 
=== getSubnet ===
 
<source lang="lua">
ipAddress:getSubnet(bitLength)
</source>
 
Returns a Subnet object for the subnet with a bit length of <var>bitLength</var> which contains the current IP. The <var>bitLength</var> parameter must be an integer between 0 and 32 for IPv4 addresses, or an integer between 0 and 128 for IPv6 addresses.
 
'''Examples'''
 
<source lang="lua">
IPAddress.new('1.2.3.4'):getSubnet(24) -- Equivalent to Subnet.new('1.2.3.0/24')
</source>
 
=== getNextIP ===
 
<source lang="lua">
ipAddress:getNextIP()
</source>
 
Returns a new IPAddress object equivalent to the current IP address incremented by one. The IPv4 address "255.255.255.255" rolls around to "0.0.0.0", and the IPv6 address "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" rolls around to "::".
 
'''Examples'''
 
<source lang="lua">
IPAddress.new('1.2.3.4'):getNextIP() -- Equivalent to IPAddress.new('1.2.3.5')
IPAddress.new('2001:db8::ff00:12:3456'):getNextIP() -- Equivalent to IPAddress.new(''2001:db8::ff00:12:3457')
IPAddress.new('255.255.255.255'):getNextIP() -- Equivalent to IPAddress.new('0.0.0.0')
</source>
 
=== getPreviousIP ===
 
<source lang="lua">
ipAddress:getPreviousIP()
</source>
 
Returns a new IPAddress object equivalent to the current IP address decremented by one. The IPv4 address "0.0.0.0" rolls around to "255.255.255.255", and the IPv6 address "::" rolls around to "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff".
 
'''Examples'''
 
<source lang="lua">
IPAddress.new('1.2.3.4'):getPreviousIP() -- Equivalent to IPAddress.new('1.2.3.3')
IPAddress.new('2001:db8::ff00:12:3456'):getPreviousIP() -- Equivalent to IPAddress.new(''2001:db8::ff00:12:3455')
IPAddress.new('0.0.0.0'):getPreviousIP() -- Equivalent to IPAddress.new('255.255.255.255')
</source>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu