Module:IP/doc: Difference between revisions

Content deleted Content added
start Subnet class docs
document Subnet methods
Line 214:
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>
 
Subnet objects have several methods, outlined below.
 
=== getPrefix ===
 
<source lang="lua">
subnet:getPrefix()
</source>
 
Returns an IPAddress object for the lowest IP address in the subnet.
 
Examples:
 
<source lang="lua">
Subnet.new('1.2.3.0/24'):getPrefix() -- Equivalent to IPAddress.new('1.2.3.0')
Subnet.new('2001:db8::ff00:12:0/112'):getPrefix() -- Equivalent to IPAddress.new('2001:db8::ff00:12:0')
</source>
 
=== getHighestIP ===
 
<source lang="lua">
subnet:getHighestIP()
</source>
 
Returns an IPAddress object for the highest IP address in the subnet.
 
Examples:
 
<source lang="lua">
Subnet.new('1.2.3.0/24'):getHighestIP() -- Equivalent to IPAddress.new('1.2.3.255')
Subnet.new('2001:db8::ff00:12:0/112'):getHighestIP() -- Equivalent to IPAddress.new('2001:db8::ff00:12:ffff')
</source>
 
=== getBitLength ===
 
<source lang="lua">
subnet:getBitLength()
</source>
 
Returns the bit length of the subnet. This is an integer between 0 and 32 for IPv4 addresses, or an integer between 0 and 128 for IPv6 addresses.
 
Examples:
 
<source lang="lua">
Subnet.new('1.2.3.0/24'):getBitLength() -- 24
Subnet.new('2001:db8::ff00:12:0/112'):getBitLength() -- 112
</source>
 
=== getCIDR ===
 
<source lang="lua">
subnet:getCIDR()
</source>
 
Returns a [[CIDR]] string representation of the subnet.
 
Examples:
 
<source lang="lua">
Subnet.new('1.2.3.0/24'):getCIDR() -- "1.2.3.0/24"
Subnet.new('2001:db8::ff00:12:0/112'):getCIDR() -- "2001:db8::ff00:12:0/112"
Subnet.new('2001:db8:0:0:0:0:0:0/112'):getCIDR() -- "2001:db8::/112"
</source>
 
=== getVersion ===
 
<source lang="lua">
subnet: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">
Subnet.new('1.2.3.0/24'):getVersion() -- "IPv4"
Subnet.new('2001:db8::ff00:12:0/112'):getVersion() -- "IPv6"
</source>
 
=== isIPv4 ===
 
<source lang="lua">
subnet:isIPv4()
</source>
 
Returns true if the subnet is using IPv4, and false otherwise.
 
Examples:
 
<source lang="lua">
Subnet.new('1.2.3.0/24'):isIPv4() -- true
Subnet.new('2001:db8::ff00:12:0/112'):isIPv4() -- false
</source>
 
=== isIPv6 ===
 
<source lang="lua">
subnet:isIPv6()
</source>
 
Returns true if the subnet is using IPv6, and false otherwise.
 
Examples:
 
<source lang="lua">
Subnet.new('1.2.3.0/24'):isIPv6() -- false
Subnet.new('2001:db8::ff00:12:0/112'):isIPv6() -- true
</source>
 
=== containsIP ===
 
<source lang="lua">
subnet:containsIP(ip)
</source>
 
Returns true if the subnet contains the IP address <var>ip</var>, and false otherwise. <var>ip</var> can be an IP address string, or an IPAddress object.
 
Examples:
 
<source lang="lua">
Subnet.new('1.2.3.0/24'):containsIP('1.2.3.4') -- true
Subnet.new('1.2.3.0/24'):containsIP('1.2.4.4') -- false
Subnet.new('1.2.3.0/24'):containsIP(IPAddress.new('1.2.3.4')) -- true
Subnet.new('2001:db8::ff00:12:0/112'):containsIP('2001:db8::ff00:12:3456') -- true
</source>
 
=== overlapsSubnet ===
 
<source lang="lua">
subnet:overlapsSubnet(subnet)
</source>
 
Returns true if the current subnet overlaps with <var>subnet</var>, and false otherwise. <var>subnet</var> can be a CIDR string or a subnet object.
 
Examples:
 
<source lang="lua">
Subnet.new('1.2.3.0/24'):overlapsSubnet('1.2.0.0/16') -- true
Subnet.new('1.2.3.0/24'):overlapsSubnet('1.2.4.0/16') -- false
Subnet.new('1.2.3.0/24'):overlapsSubnet(Subnet.new('1.2.0.0/16')) -- true
Subnet.new('2001:db8::ff00:12:0/112'):overlapsSubnet('2001:db8::ff00:0:0/96') -- true
</source>
 
=== walk ===
 
<source lang="lua">
subnet:walk()
</source>
 
The walk method iterates over all of the IPAddress objects in the subnet.
 
Examples:
 
<source lang="lua">
for ipAddress in Subnet.new('192.168.0.0/30'):walk() do
mw.log(tostring(ipAddress))
end
-- 192.168.0.0
-- 192.168.0.1
-- 192.168.0.2
-- 192.168.0.3
</source>