1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
5 <title>Reference</title>
6 <link rel="stylesheet" href="../luadoc.css" type="text/css" />
7 <!--meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/-->
14 <div id="product_logo"></div>
15 <div id="product_name"><big><b></b></big></div>
16 <div id="product_description"></div>
17 </div> <!-- id="product" -->
27 <li><a href="../index.html">Index</a></li>
38 <a href="../modules/luci.dispatcher.html">luci.dispatcher</a>
42 <a href="../modules/luci.http.protocol.html">luci.http.protocol</a>
46 <a href="../modules/luci.http.protocol.conditionals.html">luci.http.protocol.conditionals</a>
50 <a href="../modules/luci.http.protocol.date.html">luci.http.protocol.date</a>
54 <a href="../modules/luci.http.protocol.mime.html">luci.http.protocol.mime</a>
58 <a href="../modules/luci.i18n.html">luci.i18n</a>
61 <li><strong>luci.ip</strong></li>
64 <a href="../modules/luci.ip.cidr.html">luci.ip.cidr</a>
68 <a href="../modules/luci.jsonc.html">luci.jsonc</a>
72 <a href="../modules/luci.jsonc.parser.html">luci.jsonc.parser</a>
76 <a href="../modules/luci.sys.init.html">luci.sys.init</a>
80 <a href="../modules/luci.sys.iptparser.html">luci.sys.iptparser</a>
84 <a href="../modules/luci.sys.net.html">luci.sys.net</a>
88 <a href="../modules/luci.sys.process.html">luci.sys.process</a>
92 <a href="../modules/luci.sys.user.html">luci.sys.user</a>
96 <a href="../modules/luci.sys.wifi.html">luci.sys.wifi</a>
100 <a href="../modules/nixio.html">nixio</a>
104 <a href="../modules/nixio.CHANGELOG.html">nixio.CHANGELOG</a>
108 <a href="../modules/nixio.CryptoHash.html">nixio.CryptoHash</a>
112 <a href="../modules/nixio.File.html">nixio.File</a>
116 <a href="../modules/nixio.README.html">nixio.README</a>
120 <a href="../modules/nixio.Socket.html">nixio.Socket</a>
124 <a href="../modules/nixio.TLSContext.html">nixio.TLSContext</a>
128 <a href="../modules/nixio.TLSSocket.html">nixio.TLSSocket</a>
132 <a href="../modules/nixio.UnifiedIO.html">nixio.UnifiedIO</a>
136 <a href="../modules/nixio.bin.html">nixio.bin</a>
140 <a href="../modules/nixio.bit.html">nixio.bit</a>
144 <a href="../modules/nixio.crypto.html">nixio.crypto</a>
148 <a href="../modules/nixio.fs.html">nixio.fs</a>
163 </div><!-- id="navigation" -->
167 <h1>Class <code>luci.ip</code></h1>
170 LuCI IP calculation and netlink access library.</p>
179 <table class="function_list">
182 <td class="name" nowrap><a href="#new">new</a> (address, netmask)</td>
185 Construct a new luci.ip.cidr instance and autodetect the address family.</td>
189 <td class="name" nowrap><a href="#IPv4">IPv4</a> (address, netmask)</td>
192 Construct a new IPv4 luci.ip.cidr instance.</td>
196 <td class="name" nowrap><a href="#IPv6">IPv6</a> (address, netmask)</td>
199 Construct a new IPv6 luci.ip.cidr instance.</td>
203 <td class="name" nowrap><a href="#route">route</a> (address)</td>
206 Determine the route leading to the given destination.</td>
210 <td class="name" nowrap><a href="#routes">routes</a> (filter, callback)</td>
213 Fetch all routes, optionally matching the given criteria.</td>
217 <td class="name" nowrap><a href="#neighbors">neighbors</a> (filter, callback)</td>
220 Fetches entries from the IPv4 ARP and IPv6 neighbour kernel table </td>
224 <td class="name" nowrap><a href="#link">link</a> (device)</td>
227 Fetch basic device information </td>
241 <h2><a name="functions"></a>Functions</h2>
242 <dl class="function">
246 <dt><a name="new"></a><strong>new</strong> (address, netmask)</dt>
250 Construct a new luci.ip.cidr instance and autodetect the address family.
251 Throws an error if the given strings do not represent a valid address or
252 if the given optional netmask is of a different family.
259 address: String containing a valid IPv4 or IPv6 address, optionally
260 with prefix size (CIDR notation) or netmask separated by slash.
264 netmask: String containing a valid IPv4 or IPv6 netmask or number
265 containing a prefix size in bits (<code>0..32</code> for IPv4,
266 <code>0..128</code> for IPv6). Overrides mask embedded in the first argument
267 if specified. (optional)
276 <pre>addr = luci.ip.new("10.24.0.1/24")
277 addr = luci.ip.new("10.24.0.1/255.255.255.0")
278 addr = luci.ip.new("10.24.0.1", "255.255.255.0") -- separate netmask
279 addr = luci.ip.new("10.24.0.1/24", 16) -- override netmask
281 addr6 = luci.ip.new("fe80::221:63ff:fe75:aa17/64")
282 addr6 = luci.ip.new("fe80::221:63ff:fe75:aa17/ffff:ffff:ffff:ffff::")
283 addr6 = luci.ip.new("fe80::221:63ff:fe75:aa17", "ffff:ffff:ffff:ffff::")
284 addr6 = luci.ip.new("fe80::221:63ff:fe75:aa17/64", 128) -- override netmask</pre>
288 <h3>Return value:</h3>
289 A <code>luci.ip.cidr</code> object representing the given
312 <dt><a name="IPv4"></a><strong>IPv4</strong> (address, netmask)</dt>
316 Construct a new IPv4 luci.ip.cidr instance.
317 Throws an error if the given string does not represent a valid IPv4 address or
318 if the given optional netmask is of a different family.
325 address: String containing a valid IPv4, optionally with prefix size
326 (CIDR notation) or netmask separated by slash.
330 netmask: String containing a valid IPv4 netmask or number
331 containing a prefix size between <code>0</code> and <code>32</code> bit.
332 Overrides mask embedded in the first argument if specified. (optional)
341 <pre>addr = luci.ip.new("10.24.0.1/24")
342 addr = luci.ip.new("10.24.0.1/255.255.255.0")
343 addr = luci.ip.new("10.24.0.1", "255.255.255.0") -- separate netmask
344 addr = luci.ip.new("10.24.0.1/24", 16) -- override netmask</pre>
348 <h3>Return value:</h3>
349 A <code>luci.ip.cidr</code> object representing the given IPv4 range.
367 <dt><a name="IPv6"></a><strong>IPv6</strong> (address, netmask)</dt>
371 Construct a new IPv6 luci.ip.cidr instance.
372 Throws an error if the given string does not represent a valid IPv6 address or
373 if the given optional netmask is of a different family.
380 address: String containing a valid IPv6, optionally with prefix size
381 (CIDR notation) or netmask separated by slash.
385 netmask: String containing a valid IPv4 netmask or number
386 containing a prefix size between <code>0</code> and <code>128</code> bit.
387 Overrides mask embedded in the first argument if specified. (optional)
396 <pre>addr6 = luci.ip.new("fe80::221:63ff:fe75:aa17/64")
397 addr6 = luci.ip.new("fe80::221:63ff:fe75:aa17/ffff:ffff:ffff:ffff::")
398 addr6 = luci.ip.new("fe80::221:63ff:fe75:aa17", "ffff:ffff:ffff:ffff::")
399 addr6 = luci.ip.new("fe80::221:63ff:fe75:aa17/64", 128) -- override netmask</pre>
403 <h3>Return value:</h3>
404 A <code>luci.ip.cidr</code> object representing the given IPv6 range.
422 <dt><a name="route"></a><strong>route</strong> (address)</dt>
426 Determine the route leading to the given destination.
433 address: A <code>luci.ip.cidr</code> instance or a string containing
434 a valid IPv4 or IPv6 range as specified by <code>luci.ip.new()</code>.
444 <li>Find default gateway by getting route to Google's public NS server
445 <pre>rt = luci.ip.route("8.8.8.8")
447 print("gateway is", rt.gw)
449 <li>Determine IPv6 upstream interface <pre>rt = luci.ip.route("2001::/7")
451 print("ipv6 upstream device is", rt.dev)
457 <h3>Return value:</h3>
458 <p>Table containing the fields described below.</p>
459 <table id="routetable">
460 <tr><th>Field</th><th>Description</th></tr>
461 <tr><td><code>type</code><td>
462 <p>Route type with one of the following numeric values:</p>
465 <td><code>1</code></td>
466 <td><code>RTN_UNICAST</code> - Gateway or direct route</td>
469 <td><code>2</code></td>
470 <td><code>RTN_LOCAL</code> - Accept locally</td>
473 <td><code>3</code></td>
474 <td><code>RTN_BROADCAST</code> -
475 Accept locally as broadcast send as broadcast</td>
478 <td><code>4</code></td>
479 <td><code>RTN_ANYCAST</code> -
480 Accept locally as broadcast but send as unicast</td>
483 <td><code>5</code></td>
484 <td><code>RTN_MULTICAST</code> - Multicast route</td>
489 <td><code>family</code></td>
490 <td>Number containing the route family, <code>4</code> for IPv4 or
491 <code>6</code> for IPv6</td>
494 <td><code>dest</code></td>
495 <td>Destination <code>luci.ip.cidr</code> instance</td>
498 <td><code>gw</code></td>
499 <td>Gateway <code>luci.ip.cidr</code> instance (optional)</td>
502 <td><code>from</code></td>
503 <td>Source address <code>luci.ip.cidr</code> instance (optional)</td>
506 <td><code>src</code></td>
507 <td>Preferred source <code>luci.ip.cidr</code> instance (optional)</td>
510 <td><code>dev</code></td>
511 <td>String containing the name of the outgoing interface</td>
514 <td><code>iif</code></td>
515 <td>String containing the name of the incoming interface (optional)</td>
518 <td><code>table</code></td>
519 <td>Number of the associated routing table (<code>0..65535</code>)</td>
522 <td><code>proto</code></td>
523 <td>Number of the associated routing protocol</td>
526 <td><code>scope</code></td>
527 <td>Number describing the scope of the route, most commonly
528 <code>0</code> for global or <code>253</code> for on-link</td>
531 <td><code>metric</code></td>
532 <td>Number describing the route metric (optional)</td>
535 <td><code>expires</code></td>
536 <td>Number of seconds the prefix is valid (IPv6 only, optional)</td>
539 <td><code>error</code></td>
540 <td>Route destination error code (optional)</td>
549 <li><a href="#routes">
560 <dt><a name="routes"></a><strong>routes</strong> (filter, callback)</dt>
564 Fetch all routes, optionally matching the given criteria.
571 filter: <p>Table containing one or more of the possible filter
572 critera described below (optional)</p><table>
573 <tr><th>Field</th><th>Description</th></tr>
574 <tr><td><code>family</code></td><td>
575 Number describing the address family to return - <code>4</code> selects
576 IPv4 routes, <code>6</code> IPv6 ones. Any other value selects both.
578 <tr><td><code>iif</code></td><td>
579 String containing the incoming route interface to match.
581 <tr><td><code>oif</code></td><td>
582 String containing the outgoing route interface to match.
584 <tr><td><code>type</code></td><td>
585 Numeric type to match, e.g. <code>1</code> for unicast.
587 <tr><td><code>scope</code></td><td>
588 Numeric scope to match, e.g. <code>253</code> for onlink.
590 <tr><td><code>proto</code></td><td>
591 Numeric protocol to match, e.g. <code>2</code> for boot.
593 <tr><td><code>table</code></td><td>
594 Numeric routing table to match (<code>0..65535</code>).
596 <tr><td><code>gw</code></td><td>
597 String containing the gateway address to match. Can be in any notation
598 specified by <code>luci.ip.new()</code>. Prefix matching is performed when
599 comparing the routes, e.g. "192.168.1.0/24" would select routes with gateway
600 addresses <code>192.168.1.1 .. 192.168.1.255</code>.
602 <tr><td><code>dest</code></td><td>
603 String containing the destination to match. Prefix matching is performed.
605 <tr><td><code>from</code></td><td>
606 String containing the source address to match. Prefix matching is performed.
608 <tr><td><code>src</code></td><td>
609 String containing the preferred source address to match.
610 Prefix matching is performed.
612 <tr><td><code>dest_exact</code></td><td>
613 String containing the destination to match. Exact matching is performed,
614 e.g. <code>dest = "0.0.0.0/0"</code> would match <em>any</em> IPv4 route
615 while <code>dest_exact = "0.0.0.0/0"</code> will <em>only</em> match the
618 <tr><td><code>from_exact</code></td><td>
619 String containing the source address to match. Exact matching is performed.
625 callback: <p>Callback function to invoke for each found route
626 instead of returning one table of route objects (optional)</p>
636 <li>Find all IPv4 default routes:
637 <pre>luci.ip.routes({ dest_exact = "0.0.0.0/0" }, function(rt)
638 print(rt.type, rt.gw, rt.dev)
640 <li>Find all global IPv6 prefixes on the current system:
641 <pre>luci.ip.routes({ from = "2001::/7" }, function(rt)
644 <li>Fetch all IPv4 routes:
645 <pre>routes = luci.ip.routes({ family = 4 })
646 for _, rt in ipairs(routes) do
647 print(rt.dest, rt.gw, rt.dev)
653 <h3>Return value:</h3>
654 If no callback function is provided, a table of routes
655 <a href="#routetable">as specified by <code>luci.ip.route()</code></a>
656 is returned. If a callback function is given, it is invoked for each route
657 and nothing is returned.
664 <li><a href="#route">
675 <dt><a name="neighbors"></a><strong>neighbors</strong> (filter, callback)</dt>
679 Fetches entries from the IPv4 ARP and IPv6 neighbour kernel table
686 filter: <p>Table containing one or more of the possible filter
687 critera described below (optional)</p><table>
688 <tr><th>Field</th><th>Description</th></tr>
689 <tr><td><code>family</code></td><td>
690 Number describing the address family to return - <code>4</code> selects
691 IPv4 ARP, <code>6</code> select IPv6 neighbour entries. Any other value
694 <tr><td><code>dev</code></td><td>
695 String containing the associated interface to match.
697 <tr><td><code>dest</code></td><td>
698 String containing the associated address to match. Can be in any notation
699 specified by <code>luci.ip.new()</code>. Prefix matching is performed when
700 comparing the addresses, e.g. "192.168.1.0/24" would select ARP entries
701 for <code>192.168.1.1 .. 192.168.1.255</code>.
703 <tr><td><code>mac</code></td><td>
704 String containing MAC address to match.
710 callback: <p>Callback function to invoke for each found neighbour
711 entry instead of returning one table of neighbour entries (optional)</p>
721 <li>Find all ARP neighbours in the LAN:
722 <pre>luci.ip.neighbors({ dest = "192.168.0.0/16" }, function(n)
725 <li>Find all active IPv6 addresses of host with given MAC:
726 <pre>luci.ip.neighbors({ family = 6, mac = "00:21:63:75:aa:17" },
734 <h3>Return value:</h3>
735 If no callback function is provided, a table of neighbour entries
736 is returned. If a callback function is given, it is invoked for each entry
737 and nothing is returned.
739 A neighbour entry is a table containing the following fields:
742 <tr><th>Field</th><th>Description</th></tr>
744 <td><code>family</code></td>
745 <td>Number containing the neighbour entry family, <code>4</code> for IPv4
746 ARP or <code>6</code> for IPv6 NDP</td>
749 <td><code>dev</code></td>
750 <td>String containing the associated device of the neighbour entry</td>
753 <td><code>dest</code></td>
754 <td>IP address <code>luci.ip.cidr</code> instance</td>
757 <td><code>mac</code></td>
758 <td>String containing the associated MAC address</td>
761 <td><code>router</code></td>
762 <td>Boolean "true" if the neighbour entry is a router (IPv6, optional)</td>
765 <td><code>proxy</code></td>
766 <td>Boolean "true" if this is a proxy entry (optional)</td>
769 <td><code>incomplete</code></td>
770 <td>Boolean "true" if the entry is in incomplete state (optional)</td>
773 <td><code>reachable</code></td>
774 <td>Boolean "true" if the entry is in reachable state (optional)</td>
777 <td><code>stale</code></td>
778 <td>Boolean "true" if the entry is stale (optional)</td>
781 <td><code>delay</code></td>
782 <td>Boolean "true" if the entry is delayed (optional)</td>
785 <td><code>probe</code></td>
786 <td>Boolean "true" if the entry is in probe state (optional)</td>
789 <td><code>failed</code></td>
790 <td>Boolean "true" if the entry is in failed state (optional)</td>
793 <td><code>noarp</code></td>
794 <td>Boolean "true" if the entry is not caused by NDP or
798 <td><code>permanent</code></td>
799 <td>Boolean "true" if the entry was statically configured from
800 userspace (optional)</td>
811 <dt><a name="link"></a><strong>link</strong> (device)</dt>
815 Fetch basic device information
822 device: String containing the network device to query
832 <li>Test whether device br-lan exists:
833 <pre>print(luci.ip.link("br-lan").name ~= nil)
835 <li>Query MAC address of eth0:
836 <pre>print(luci.ip.link("eth0").mac)
842 <h3>Return value:</h3>
843 If the given interface is found, a table containing the fields
844 described below is returned, else an empty table.
847 <tr><th>Field</th><th>Description</th></tr>
849 <td><code>up</code></td>
850 <td>Boolean indicating whether the device is in IFF_RUNNING state</td>
853 <td><code>type</code></td>
854 <td>Numeric value indicating the type of the device, e.g. <code>1</code>
858 <td><code>name</code></td>
859 <td>String containing the name of the device</td>
862 <td><code>master</code></td>
863 <td>If queried device is a bridge port, string containing the name of
864 parent bridge device (optional)</td>
867 <td><code>mtu</code></td>
868 <td>Number containing the current MTU of the device</td>
871 <td><code>qlen</code></td>
872 <td>Number containing the TX queue length of the device</td>
875 <td><code>mac</code></td>
876 <td>String containing the link local address of the device in
877 dotted hex notation</td>
892 </div> <!-- id="content" -->
894 </div> <!-- id="main" -->
897 <p><a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" height="31" width="88" /></a></p>
898 </div> <!-- id="about" -->
900 </div> <!-- id="container" -->