Module:IP/doc: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
start Subnet class docs
(add section links)
(start Subnet class docs)
Line 181:
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>
 
== Subnet ==
 
The Subnet class is used to work with [[subnetwork]]s of IPv4 or IPv6 addresses. To create a new Subnet object:
 
<source lang="lua">
local subnet = Subnet.new(cidrString)
</source>
 
<var>cidrString</var> must be a valid IPv4 or IPv6 [[CIDR]] string.
 
Subnet objects can be compared for equality:
 
<source lang="lua">
Subnet.new('1.2.3.0/24') == Subnet.new('1.2.3.0/24') -- true
Subnet.new('1.2.3.0/24') == Subnet.new('1.2.3.0/25') -- false
Subnet.new('1.2.3.0/24') == Subnet.new('1.2.0.0/16') -- true
Subnet.new('2001:db8::ff00:12:0/112') == Subnet.new('2001:db8::ff00:12:0/112') -- true
Subnet.new('2001:db8:0:0:0:0:0:0/112') == Subnet.new('2001:db8::/112') -- true
</source>
 
You can use tostring on them (this is equivalent to [[#getCIDR|getCIDR]]):
<source lang="lua">
tostring(Subnet.new('1.2.3.0/24')) -- "1.2.3.0/24"
tostring(Subnet.new('2001:db8::ff00:12:0/112')) -- "2001:db8::ff00:12:0/112"
tostring(Subnet.new('2001:db8:0:0:0:0:0:0/112')) -- "2001:db8::/112"
</source>
 
You can also concatenate them:
<source lang="lua">
Subnet.new('1.2.3.0/24') .. ' foo' -- "1.2.3.0/24 foo"
Subnet.new('1.2.3.0/24') .. Subnet.new('4.5.6.0/24') -- "1.2.3.0/244.5.6.0/24"
</source>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu