Module:IP/doc: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
m
27 revisions imported from wikipedia:Module:IP/doc
(Cleanup TfD tag detritus)
m (27 revisions imported from wikipedia:Module:IP/doc)
 
(One intermediate revision by one other user not shown)
Line 5:
== Loading the library ==
 
<sourcesyntaxhighlight lang="lua">
local IP = require('Module:IP')
local IPAddress = IP.IPAddress
local Subnet = IP.Subnet
</syntaxhighlight>
</source>
 
== IPAddress ==
Line 15:
The IPAddress class is used to work with single IP addresses. To create a new IPAddress object:
 
<sourcesyntaxhighlight lang="lua">
local ipAddress = IPAddress.new(ipString)
</syntaxhighlight>
</source>
 
The ipString variable can be a valid IPv4 or IPv6 address.
Line 23:
Examples:
 
<sourcesyntaxhighlight lang="lua">
local ipv4Address = IPAddress.new('1.2.3.4')
local ipv6Address = IPAddress.new('2001:db8::ff00:12:3456')
</syntaxhighlight>
</source>
 
IPAddress objects can be compared with relational operators:
 
<sourcesyntaxhighlight lang="lua">
-- Equality
IPAddress.new('1.2.3.4') == IPAddress.new('1.2.3.4') -- true
Line 40:
IPAddress.new('1.2.3.4') <= IPAddress.new('1.2.3.5') -- true
IPAddress.new('1.2.3.4') <= IPAddress.new('1.2.3.4') -- true
</syntaxhighlight>
</source>
 
You can use tostring on them (this is equivalent to using [[#getIP|getIP]]):
 
<sourcesyntaxhighlight lang="lua">
tostring(IPAddress.new('1.2.3.4')) -- "1.2.3.4"
tostring(IPAddress.new('2001:db8::ff00:12:3456')) -- "2001:db8::ff00:12:3456"
Line 50:
-- Expanded IPv6 addresses are abbreviated:
tostring(IPAddress.new('2001:db8:0:0:0:0:0:0')) -- "2001:db8::"
</syntaxhighlight>
</source>
 
You can also concatenate them:
 
<sourcesyntaxhighlight lang="lua">
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"
</syntaxhighlight>
</source>
 
IPAddress objects have several methods, outlined below.
Line 63:
=== getIP ===
 
<sourcesyntaxhighlight lang="lua">
ipAddress:getIP()
</syntaxhighlight>
</source>
 
Returns a string representation of the IP address. IPv6 addresses are abbreviated if possible.
Line 71:
Examples:
 
<sourcesyntaxhighlight 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::"
</syntaxhighlight>
</source>
 
=== getVersion ===
 
<sourcesyntaxhighlight lang="lua">
ipAddress:getVersion()
</syntaxhighlight>
</source>
 
Returns the version of the IP protocol being used. This is "IPv4" for IPv4 addresses, and "IPv6" for IPv6 addresses.
Line 87:
Examples:
 
<sourcesyntaxhighlight lang="lua">
IPAddress.new('1.2.3.4'):getVersion() -- "IPv4"
IPAddress.new('2001:db8::ff00:12:3456'):getVersion() -- "IPv6"
</syntaxhighlight>
</source>
 
=== isIPv4 ===
 
<sourcesyntaxhighlight lang="lua">
ipAddress:isIPv4()
</syntaxhighlight>
</source>
 
Returns true if the IP address is an IPv4 address, and false otherwise.
Line 102:
Examples:
 
<sourcesyntaxhighlight lang="lua">
IPAddress.new('1.2.3.4'):isIPv4() -- true
IPAddress.new('2001:db8::ff00:12:3456'):isIPv4() -- false
</syntaxhighlight>
</source>
 
=== isIPv6 ===
 
<sourcesyntaxhighlight lang="lua">
ipAddress:isIPv6()
</syntaxhighlight>
</source>
 
Returns true if the IP address is an IPv6 address, and false otherwise.
Line 117:
Examples:
 
<sourcesyntaxhighlight lang="lua">
IPAddress.new('1.2.3.4'):isIPv6() -- false
IPAddress.new('2001:db8::ff00:12:3456'):isIPv6() -- true
</syntaxhighlight>
</source>
 
=== isInSubnet ===
 
<sourcesyntaxhighlight lang="lua">
ipAddress:isInSubnet(subnet)
</syntaxhighlight>
</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.
Line 132:
Examples:
 
<sourcesyntaxhighlight 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
</syntaxhighlight>
</source>
 
=== getSubnet ===
 
<sourcesyntaxhighlight lang="lua">
ipAddress:getSubnet(bitLength)
</syntaxhighlight>
</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.
Line 149:
Examples:
 
<sourcesyntaxhighlight lang="lua">
IPAddress.new('1.2.3.4'):getSubnet(24) -- Equivalent to Subnet.new('1.2.3.0/24')
</syntaxhighlight>
</source>
 
=== getNextIP ===
 
<sourcesyntaxhighlight lang="lua">
ipAddress:getNextIP()
</syntaxhighlight>
</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 "::".
Line 163:
Examples:
 
<sourcesyntaxhighlight 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')
</syntaxhighlight>
</source>
 
=== getPreviousIP ===
 
<sourcesyntaxhighlight lang="lua">
ipAddress:getPreviousIP()
</syntaxhighlight>
</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".
Line 179:
Examples:
 
<sourcesyntaxhighlight 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')
</syntaxhighlight>
</source>
 
== Subnet ==
Line 189:
The Subnet class is used to work with [[subnetwork]]s of IPv4 or IPv6 addresses. To create a new Subnet object:
 
<sourcesyntaxhighlight lang="lua">
local subnet = Subnet.new(cidrString)
</syntaxhighlight>
</source>
 
<var>cidrString</var> must be a valid IPv4 or IPv6 [[CIDR]] string.
Line 197:
Subnet objects can be compared for equality:
 
<sourcesyntaxhighlight 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
Line 203:
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
</syntaxhighlight>
</source>
 
You can use tostring on them (this is equivalent to [[#getCIDR|getCIDR]]):
<sourcesyntaxhighlight 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"
</syntaxhighlight>
</source>
 
You can also concatenate them:
<sourcesyntaxhighlight 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"
</syntaxhighlight>
</source>
 
Subnet objects have several methods, outlined below.
Line 222:
=== getPrefix ===
 
<sourcesyntaxhighlight lang="lua">
subnet:getPrefix()
</syntaxhighlight>
</source>
 
Returns an IPAddress object for the lowest IP address in the subnet.
Line 230:
Examples:
 
<sourcesyntaxhighlight 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')
</syntaxhighlight>
</source>
 
=== getHighestIP ===
 
<sourcesyntaxhighlight lang="lua">
subnet:getHighestIP()
</syntaxhighlight>
</source>
 
Returns an IPAddress object for the highest IP address in the subnet.
Line 245:
Examples:
 
<sourcesyntaxhighlight 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')
</syntaxhighlight>
</source>
 
=== getBitLength ===
 
<sourcesyntaxhighlight lang="lua">
subnet:getBitLength()
</syntaxhighlight>
</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.
Line 260:
Examples:
 
<sourcesyntaxhighlight lang="lua">
Subnet.new('1.2.3.0/24'):getBitLength() -- 24
Subnet.new('2001:db8::ff00:12:0/112'):getBitLength() -- 112
</syntaxhighlight>
</source>
 
=== getCIDR ===
 
<sourcesyntaxhighlight lang="lua">
subnet:getCIDR()
</syntaxhighlight>
</source>
 
Returns a [[CIDR]] string representation of the subnet.
Line 275:
Examples:
 
<sourcesyntaxhighlight 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"
</syntaxhighlight>
</source>
 
=== getVersion ===
 
<sourcesyntaxhighlight lang="lua">
subnet:getVersion()
</syntaxhighlight>
</source>
 
Returns the version of the IP protocol being used. This is "IPv4" for IPv4 addresses, and "IPv6" for IPv6 addresses.
Line 291:
Examples:
 
<sourcesyntaxhighlight lang="lua">
Subnet.new('1.2.3.0/24'):getVersion() -- "IPv4"
Subnet.new('2001:db8::ff00:12:0/112'):getVersion() -- "IPv6"
</syntaxhighlight>
</source>
 
=== isIPv4 ===
 
<sourcesyntaxhighlight lang="lua">
subnet:isIPv4()
</syntaxhighlight>
</source>
 
Returns true if the subnet is using IPv4, and false otherwise.
Line 306:
Examples:
 
<sourcesyntaxhighlight lang="lua">
Subnet.new('1.2.3.0/24'):isIPv4() -- true
Subnet.new('2001:db8::ff00:12:0/112'):isIPv4() -- false
</syntaxhighlight>
</source>
 
=== isIPv6 ===
 
<sourcesyntaxhighlight lang="lua">
subnet:isIPv6()
</syntaxhighlight>
</source>
 
Returns true if the subnet is using IPv6, and false otherwise.
Line 321:
Examples:
 
<sourcesyntaxhighlight lang="lua">
Subnet.new('1.2.3.0/24'):isIPv6() -- false
Subnet.new('2001:db8::ff00:12:0/112'):isIPv6() -- true
</syntaxhighlight>
</source>
 
=== containsIP ===
 
<sourcesyntaxhighlight lang="lua">
subnet:containsIP(ip)
</syntaxhighlight>
</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.
Line 336:
Examples:
 
<sourcesyntaxhighlight 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
</syntaxhighlight>
</source>
 
=== overlapsSubnet ===
 
<sourcesyntaxhighlight lang="lua">
subnet:overlapsSubnet(subnet)
</syntaxhighlight>
</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.
Line 353:
Examples:
 
<sourcesyntaxhighlight 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.12.0/22') -- 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
</syntaxhighlight>
</source>
 
=== walk ===
 
<sourcesyntaxhighlight lang="lua">
subnet:walk()
</syntaxhighlight>
</source>
 
The walk method iterates over all of the IPAddress objects in the subnet.
Line 370:
Examples:
 
<sourcesyntaxhighlight lang="lua">
for ipAddress in Subnet.new('192.168.0.0/30'):walk() do
mw.log(tostring(ipAddress))
Line 378:
-- 192.168.0.2
-- 192.168.0.3
</syntaxhighlight>
</source>
 
== IPv4Collection ==
Line 384:
The IPv4Collection class is used to work with several different IPv4 addresses and IPv4 subnets. To create a new IPv4Collection object:
 
<sourcesyntaxhighlight lang="lua">
local collection = IPv4Collection.new()
</syntaxhighlight>
</source>
 
IPv4Collection objects have several methods, outlined below.
Line 392:
=== getVersion ===
 
<sourcesyntaxhighlight lang="lua">
collection:getVersion()
</syntaxhighlight>
</source>
 
Returns the string "IPv4".
Line 400:
=== addIP ===
 
<sourcesyntaxhighlight lang="lua">
collection:addIP(ip)
</syntaxhighlight>
</source>
 
Adds an IP to the collection. The IP can be either a string or an [[#IPAddress|IPAddress]] object.
Line 408:
Examples:
 
<sourcesyntaxhighlight lang="lua">
collection:addIP('1.2.3.4')
collection:addIP(IPAddress.new('1.2.3.4'))
</syntaxhighlight>
</source>
 
This method is chainable:
 
<sourcesyntaxhighlight lang="lua">
collection:addIP('1.2.3.4'):addIP('5.6.7.8')
</syntaxhighlight>
</source>
 
=== addSubnet ===
 
<sourcesyntaxhighlight lang="lua">
collection:addSubnet(subnet)
</syntaxhighlight>
</source>
 
Adds a subnet to the collection. The subnet can be either a [[CIDR]] string or a [[#Subnet|Subnet]] object.
Line 429:
Examples:
 
<sourcesyntaxhighlight lang="lua">
collection:addSubnet('1.2.3.0/24')
collection:addSubnet(Subnet.new('1.2.3.0/24'))
</syntaxhighlight>
</source>
 
This method is chainable:
 
<sourcesyntaxhighlight lang="lua">
collection:addSubnet('1.2.0.0/24'):addSubnet('1.2.1.0/24')
</syntaxhighlight>
</source>
 
=== addFromString ===
 
<sourcesyntaxhighlight lang="lua">
collection:addFromString(str)
</syntaxhighlight>
</source>
 
Extracts any IPv4 addresses and IPv4 CIDR subnets from <var>str</var> and adds them to the collection. Any text that is not an IPv4 address or CIDR subnet is ignored.
Line 450:
Examples:
 
<sourcesyntaxhighlight lang="lua">
collection:addFromString('Add some IPs and subnets: 1.2.3.4 1.2.3.5 2001:0::f foo 1.2.4.0/24')
</syntaxhighlight>
</source>
 
This method is chainable:
 
<sourcesyntaxhighlight lang="lua">
collection:addFromString('foo 1.2.3.4'):addFromString('bar 5.6.7.8')
</syntaxhighlight>
</source>
 
=== containsIP ===
 
<sourcesyntaxhighlight lang="lua">
collection:containsIP(ip)
</syntaxhighlight>
</source>
 
Returns true if the collection contains the specified IP; otherwise returns false. The <var>ip</var> parameter can be a string or an [[#IPAddress|IPAddress]] object.
Line 470:
Examples:
 
<sourcesyntaxhighlight lang="lua">
collection:containsIP('1.2.3.4')
collection:containsIP(IPAddress.new('1.2.3.4'))
</syntaxhighlight>
</source>
 
=== getRanges ===
 
<sourcesyntaxhighlight lang="lua">
collection:getRanges()
</syntaxhighlight>
</source>
 
Returns a sorted array of IP pairs equivalent to the collection. Each IP pair is an array representing a contiguous range of IP addresses from pair[1] to pair[2] inclusive. pair[1] and pair[2] are [[#IPAddress|IPAddress]] objects.
Line 485:
Examples:
 
<sourcesyntaxhighlight lang="lua">
collection:addSubnet('1.2.0.0/24')
collection:addSubnet('1.2.1.0/24')
Line 501:
-- },
-- }
</syntaxhighlight>
</source>
 
=== overlapsSubnet ===
 
<sourcesyntaxhighlight lang="lua">
collection:overlapsSubnet(subnet)
</syntaxhighlight>
</source>
 
Returns true, obj if <var>subnet</var> overlaps this collection, where obj is the first [[#IPAddress|IPAddress]] or [[#Subnet|Subnet]] object overlapping the subnet. Otherwise, returns false. <var>subnet</var> can be a CIDR string or a [[#Subnet|Subnet]] object.
Line 513:
Examples:
 
<sourcesyntaxhighlight lang="lua">
collection:addIP('1.2.3.4')
collection:overlapsSubnet('1.2.3.0/24') -- true, IPAddress.new('1.2.3.4')
collection:overlapsSubnet('1.2.4.0/24') -- false
</syntaxhighlight>
</source>
 
== IPv6Collection ==
Line 525:
To create a new IPv6Collection object:
 
<sourcesyntaxhighlight lang="lua">
local collection = IPv6Collection.new()
</syntaxhighlight>
</source>
 
<includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox | |
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu