luci.ip.cidr.html 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html>
  4. <head>
  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"/-->
  8. </head>
  9. <body>
  10. <div id="container">
  11. <div id="product">
  12. <div id="product_logo"></div>
  13. <div id="product_name"><big><b></b></big></div>
  14. <div id="product_description"></div>
  15. </div> <!-- id="product" -->
  16. <div id="main">
  17. <div id="navigation">
  18. <h1>LuaDoc</h1>
  19. <ul>
  20. <li><a href="../index.html">Index</a></li>
  21. </ul>
  22. <!-- Module list -->
  23. <h1>Modules</h1>
  24. <ul>
  25. <li>
  26. <a href="../modules/luci.dispatcher.html">luci.dispatcher</a>
  27. </li>
  28. <li>
  29. <a href="../modules/luci.http.html">luci.http</a>
  30. </li>
  31. <li>
  32. <a href="../modules/luci.http.conditionals.html">luci.http.conditionals</a>
  33. </li>
  34. <li>
  35. <a href="../modules/luci.http.date.html">luci.http.date</a>
  36. </li>
  37. <li>
  38. <a href="../modules/luci.http.mime.html">luci.http.mime</a>
  39. </li>
  40. <li>
  41. <a href="../modules/luci.i18n.html">luci.i18n</a>
  42. </li>
  43. <li>
  44. <a href="../modules/luci.ip.html">luci.ip</a>
  45. </li>
  46. <li><strong>luci.ip.cidr</strong></li>
  47. <li>
  48. <a href="../modules/luci.json.html">luci.json</a>
  49. </li>
  50. <li>
  51. <a href="../modules/luci.jsonc.html">luci.jsonc</a>
  52. </li>
  53. <li>
  54. <a href="../modules/luci.jsonc.parser.html">luci.jsonc.parser</a>
  55. </li>
  56. <li>
  57. <a href="../modules/luci.model.ipkg.html">luci.model.ipkg</a>
  58. </li>
  59. <li>
  60. <a href="../modules/luci.model.uci.html">luci.model.uci</a>
  61. </li>
  62. <li>
  63. <a href="../modules/luci.rpcc.html">luci.rpcc</a>
  64. </li>
  65. <li>
  66. <a href="../modules/luci.rpcc.ruci.html">luci.rpcc.ruci</a>
  67. </li>
  68. <li>
  69. <a href="../modules/luci.sys.html">luci.sys</a>
  70. </li>
  71. <li>
  72. <a href="../modules/luci.sys.init.html">luci.sys.init</a>
  73. </li>
  74. <li>
  75. <a href="../modules/luci.sys.iptparser.html">luci.sys.iptparser</a>
  76. </li>
  77. <li>
  78. <a href="../modules/luci.sys.net.html">luci.sys.net</a>
  79. </li>
  80. <li>
  81. <a href="../modules/luci.sys.process.html">luci.sys.process</a>
  82. </li>
  83. <li>
  84. <a href="../modules/luci.sys.user.html">luci.sys.user</a>
  85. </li>
  86. <li>
  87. <a href="../modules/luci.sys.wifi.html">luci.sys.wifi</a>
  88. </li>
  89. <li>
  90. <a href="../modules/luci.util.html">luci.util</a>
  91. </li>
  92. <li>
  93. <a href="../modules/luci.xml.html">luci.xml</a>
  94. </li>
  95. <li>
  96. <a href="../modules/nixio.html">nixio</a>
  97. </li>
  98. <li>
  99. <a href="../modules/nixio.CHANGELOG.html">nixio.CHANGELOG</a>
  100. </li>
  101. <li>
  102. <a href="../modules/nixio.CryptoHash.html">nixio.CryptoHash</a>
  103. </li>
  104. <li>
  105. <a href="../modules/nixio.File.html">nixio.File</a>
  106. </li>
  107. <li>
  108. <a href="../modules/nixio.README.html">nixio.README</a>
  109. </li>
  110. <li>
  111. <a href="../modules/nixio.Socket.html">nixio.Socket</a>
  112. </li>
  113. <li>
  114. <a href="../modules/nixio.TLSContext.html">nixio.TLSContext</a>
  115. </li>
  116. <li>
  117. <a href="../modules/nixio.TLSSocket.html">nixio.TLSSocket</a>
  118. </li>
  119. <li>
  120. <a href="../modules/nixio.UnifiedIO.html">nixio.UnifiedIO</a>
  121. </li>
  122. <li>
  123. <a href="../modules/nixio.bin.html">nixio.bin</a>
  124. </li>
  125. <li>
  126. <a href="../modules/nixio.bit.html">nixio.bit</a>
  127. </li>
  128. <li>
  129. <a href="../modules/nixio.crypto.html">nixio.crypto</a>
  130. </li>
  131. <li>
  132. <a href="../modules/nixio.fs.html">nixio.fs</a>
  133. </li>
  134. </ul>
  135. <!-- File list -->
  136. </div><!-- id="navigation" -->
  137. <div id="content">
  138. <h1>Object Instance <code>luci.ip.cidr</code></h1>
  139. <p>
  140. IP CIDR Object.
  141. Represents an IPv4 or IPv6 address range.</p>
  142. <h2>Functions</h2>
  143. <table class="function_list">
  144. <tr>
  145. <td class="name" nowrap><a href="#cidr.is4">cidr:is4</a>&nbsp;()</td>
  146. <td class="summary">
  147. Checks whether the CIDR instance is an IPv4 address range
  148. </td>
  149. </tr>
  150. <tr>
  151. <td class="name" nowrap><a href="#cidr.is4rfc1918">cidr:is4rfc1918</a>&nbsp;()</td>
  152. <td class="summary">
  153. Checks whether the CIDR instance is within the private RFC1918 address space
  154. </td>
  155. </tr>
  156. <tr>
  157. <td class="name" nowrap><a href="#cidr.is4linklocal">cidr:is4linklocal</a>&nbsp;()</td>
  158. <td class="summary">
  159. Checks whether the CIDR instance is an IPv4 link local (Zeroconf) address
  160. </td>
  161. </tr>
  162. <tr>
  163. <td class="name" nowrap><a href="#cidr.is6">cidr:is6</a>&nbsp;()</td>
  164. <td class="summary">
  165. Checks whether the CIDR instance is an IPv6 address range
  166. </td>
  167. </tr>
  168. <tr>
  169. <td class="name" nowrap><a href="#cidr.is6linklocal">cidr:is6linklocal</a>&nbsp;()</td>
  170. <td class="summary">
  171. Checks whether the CIDR instance is an IPv6 link local address
  172. </td>
  173. </tr>
  174. <tr>
  175. <td class="name" nowrap><a href="#cidr.is6mapped4">cidr:is6mapped4</a>&nbsp;()</td>
  176. <td class="summary">
  177. Checks whether the CIDR instance is an IPv6 mapped IPv4 address
  178. </td>
  179. </tr>
  180. <tr>
  181. <td class="name" nowrap><a href="#cidr.ismac">cidr:ismac</a>&nbsp;()</td>
  182. <td class="summary">
  183. Checks whether the CIDR instance is an ethernet MAC address range
  184. </td>
  185. </tr>
  186. <tr>
  187. <td class="name" nowrap><a href="#cidr.ismaclocal">cidr:ismaclocal</a>&nbsp;()</td>
  188. <td class="summary">
  189. Checks whether the CIDR instance is a locally administered (LAA) MAC address
  190. </td>
  191. </tr>
  192. <tr>
  193. <td class="name" nowrap><a href="#cidr.ismacmcast">cidr:ismacmcast</a>&nbsp;()</td>
  194. <td class="summary">
  195. Checks whether the CIDR instance is a multicast MAC address
  196. </td>
  197. </tr>
  198. <tr>
  199. <td class="name" nowrap><a href="#cidr.lower">cidr:lower</a>&nbsp;(addr)</td>
  200. <td class="summary">
  201. Checks whether this CIDR instance is lower than the given argument.</td>
  202. </tr>
  203. <tr>
  204. <td class="name" nowrap><a href="#cidr.higher">cidr:higher</a>&nbsp;(addr)</td>
  205. <td class="summary">
  206. Checks whether this CIDR instance is higher than the given argument.</td>
  207. </tr>
  208. <tr>
  209. <td class="name" nowrap><a href="#cidr.equal">cidr:equal</a>&nbsp;(addr)</td>
  210. <td class="summary">
  211. Checks whether this CIDR instance is equal to the given argument.</td>
  212. </tr>
  213. <tr>
  214. <td class="name" nowrap><a href="#cidr.prefix">cidr:prefix</a>&nbsp;(mask)</td>
  215. <td class="summary">
  216. Get or set prefix size of CIDR instance.</td>
  217. </tr>
  218. <tr>
  219. <td class="name" nowrap><a href="#cidr.network">cidr:network</a>&nbsp;(mask)</td>
  220. <td class="summary">
  221. Derive network address of CIDR instance.</td>
  222. </tr>
  223. <tr>
  224. <td class="name" nowrap><a href="#cidr.host">cidr:host</a>&nbsp;()</td>
  225. <td class="summary">
  226. Derive host address of CIDR instance.</td>
  227. </tr>
  228. <tr>
  229. <td class="name" nowrap><a href="#cidr.mask">cidr:mask</a>&nbsp;(mask)</td>
  230. <td class="summary">
  231. Derive netmask of CIDR instance.</td>
  232. </tr>
  233. <tr>
  234. <td class="name" nowrap><a href="#cidr.broadcast">cidr:broadcast</a>&nbsp;(mask)</td>
  235. <td class="summary">
  236. Derive broadcast address of CIDR instance.</td>
  237. </tr>
  238. <tr>
  239. <td class="name" nowrap><a href="#cidr.mapped4">cidr:mapped4</a>&nbsp;()</td>
  240. <td class="summary">
  241. Derive mapped IPv4 address of CIDR instance.</td>
  242. </tr>
  243. <tr>
  244. <td class="name" nowrap><a href="#cidr.tomac">cidr:tomac</a>&nbsp;()</td>
  245. <td class="summary">
  246. Derive MAC address of IPv6 link local CIDR instance.</td>
  247. </tr>
  248. <tr>
  249. <td class="name" nowrap><a href="#cidr.tolinklocal">cidr:tolinklocal</a>&nbsp;()</td>
  250. <td class="summary">
  251. Derive IPv6 link local address from MAC address CIDR instance.</td>
  252. </tr>
  253. <tr>
  254. <td class="name" nowrap><a href="#cidr.contains">cidr:contains</a>&nbsp;(addr)</td>
  255. <td class="summary">
  256. Test whether CIDR contains given range.</td>
  257. </tr>
  258. <tr>
  259. <td class="name" nowrap><a href="#cidr.add">cidr:add</a>&nbsp;(amount, inplace)</td>
  260. <td class="summary">
  261. Add given amount to CIDR instance.</td>
  262. </tr>
  263. <tr>
  264. <td class="name" nowrap><a href="#cidr.sub">cidr:sub</a>&nbsp;(amount, inplace)</td>
  265. <td class="summary">
  266. Subtract given amount from CIDR instance.</td>
  267. </tr>
  268. <tr>
  269. <td class="name" nowrap><a href="#cidr.minhost">cidr:minhost</a>&nbsp;()</td>
  270. <td class="summary">
  271. Calculate the lowest possible host address within this CIDR instance.</td>
  272. </tr>
  273. <tr>
  274. <td class="name" nowrap><a href="#cidr.maxhost">cidr:maxhost</a>&nbsp;()</td>
  275. <td class="summary">
  276. Calculate the highest possible host address within this CIDR instance.</td>
  277. </tr>
  278. <tr>
  279. <td class="name" nowrap><a href="#cidr.string">cidr:string</a>&nbsp;()</td>
  280. <td class="summary">
  281. Convert CIDR instance into string representation.</td>
  282. </tr>
  283. </table>
  284. <br/>
  285. <br/>
  286. <h2><a name="functions"></a>Functions</h2>
  287. <dl class="function">
  288. <dt><a name="cidr.is4"></a><strong>cidr:is4</strong>&nbsp;()</dt>
  289. <dd>
  290. Checks whether the CIDR instance is an IPv4 address range
  291. <h3>Return value:</h3>
  292. <code>true</code> if the CIDR is an IPv4 range, else <code>false</code>
  293. <h3>See also:</h3>
  294. <ul>
  295. <li><a href="#cidr.is6">
  296. cidr:is6
  297. </a>
  298. <li><a href="#cidr.ismac">
  299. cidr:ismac
  300. </a>
  301. </ul>
  302. </dd>
  303. <dt><a name="cidr.is4rfc1918"></a><strong>cidr:is4rfc1918</strong>&nbsp;()</dt>
  304. <dd>
  305. Checks whether the CIDR instance is within the private RFC1918 address space
  306. <h3>Usage:</h3>
  307. <pre>local addr = luci.ip.new("192.168.45.2/24")
  308. if addr:is4rfc1918() then
  309. print("Is a private address")
  310. end</pre>
  311. <h3>Return value:</h3>
  312. <code>true</code> if the entire range of this CIDR lies within one of
  313. the ranges <code>10.0.0.0-10.255.255.255</code>,
  314. <code>172.16.0.0-172.31.0.0</code> or
  315. <code>192.168.0.0-192.168.255.255</code>, else <code>false</code>.
  316. </dd>
  317. <dt><a name="cidr.is4linklocal"></a><strong>cidr:is4linklocal</strong>&nbsp;()</dt>
  318. <dd>
  319. Checks whether the CIDR instance is an IPv4 link local (Zeroconf) address
  320. <h3>Usage:</h3>
  321. <pre>local addr = luci.ip.new("169.254.34.125")
  322. if addr:is4linklocal() then
  323. print("Is a zeroconf address")
  324. end</pre>
  325. <h3>Return value:</h3>
  326. <code>true</code> if the entire range of this CIDR lies within the range
  327. the range <code>169.254.0.0-169.254.255.255</code>, else <code>false</code>.
  328. </dd>
  329. <dt><a name="cidr.is6"></a><strong>cidr:is6</strong>&nbsp;()</dt>
  330. <dd>
  331. Checks whether the CIDR instance is an IPv6 address range
  332. <h3>Return value:</h3>
  333. <code>true</code> if the CIDR is an IPv6 range, else <code>false</code>
  334. <h3>See also:</h3>
  335. <ul>
  336. <li><a href="#cidr.is4">
  337. cidr:is4
  338. </a>
  339. <li><a href="#cidr.ismac">
  340. cidr:ismac
  341. </a>
  342. </ul>
  343. </dd>
  344. <dt><a name="cidr.is6linklocal"></a><strong>cidr:is6linklocal</strong>&nbsp;()</dt>
  345. <dd>
  346. Checks whether the CIDR instance is an IPv6 link local address
  347. <h3>Usage:</h3>
  348. <pre>local addr = luci.ip.new("fe92:53a:3216:af01:221:63ff:fe75:aa17/64")
  349. if addr:is6linklocal() then
  350. print("Is a linklocal address")
  351. end</pre>
  352. <h3>Return value:</h3>
  353. <code>true</code> if the entire range of this CIDR lies within the range
  354. the <code>fe80::/10</code> range, else <code>false</code>.
  355. </dd>
  356. <dt><a name="cidr.is6mapped4"></a><strong>cidr:is6mapped4</strong>&nbsp;()</dt>
  357. <dd>
  358. Checks whether the CIDR instance is an IPv6 mapped IPv4 address
  359. <h3>Usage:</h3>
  360. <pre>local addr = luci.ip.new("::ffff:192.168.1.1")
  361. if addr:is6mapped4() then
  362. print("Is a mapped IPv4 address")
  363. end</pre>
  364. <h3>Return value:</h3>
  365. <code>true</code> if the address is an IPv6 mapped IPv4 address in the
  366. form <code>::ffff:1.2.3.4</code>.
  367. </dd>
  368. <dt><a name="cidr.ismac"></a><strong>cidr:ismac</strong>&nbsp;()</dt>
  369. <dd>
  370. Checks whether the CIDR instance is an ethernet MAC address range
  371. <h3>Return value:</h3>
  372. <code>true</code> if the CIDR is a MAC address range, else <code>false</code>
  373. <h3>See also:</h3>
  374. <ul>
  375. <li><a href="#cidr.is4">
  376. cidr:is4
  377. </a>
  378. <li><a href="#cidr.is6">
  379. cidr:is6
  380. </a>
  381. </ul>
  382. </dd>
  383. <dt><a name="cidr.ismaclocal"></a><strong>cidr:ismaclocal</strong>&nbsp;()</dt>
  384. <dd>
  385. Checks whether the CIDR instance is a locally administered (LAA) MAC address
  386. <h3>Usage:</h3>
  387. <pre>local mac = luci.ip.new("02:C0:FF:EE:00:01")
  388. if mac:ismaclocal() then
  389. print("Is an LAA MAC address")
  390. end</pre>
  391. <h3>Return value:</h3>
  392. <code>true</code> if the MAC address sets the locally administered bit.
  393. </dd>
  394. <dt><a name="cidr.ismacmcast"></a><strong>cidr:ismacmcast</strong>&nbsp;()</dt>
  395. <dd>
  396. Checks whether the CIDR instance is a multicast MAC address
  397. <h3>Usage:</h3>
  398. <pre>local mac = luci.ip.new("01:00:5E:7F:00:10")
  399. if addr:ismacmcast() then
  400. print("Is a multicast MAC address")
  401. end</pre>
  402. <h3>Return value:</h3>
  403. <code>true</code> if the MAC address sets the multicast bit.
  404. </dd>
  405. <dt><a name="cidr.lower"></a><strong>cidr:lower</strong>&nbsp;(addr)</dt>
  406. <dd>
  407. Checks whether this CIDR instance is lower than the given argument.
  408. The comparisation follows these rules:
  409. <ul><li>An IPv4 address is always lower than an IPv6 address and IPv6 addresses
  410. are considered lower than MAC addresses</li>
  411. <li>Prefix sizes are ignored</li></ul>
  412. <h3>Parameters</h3>
  413. <ul>
  414. <li>
  415. addr: A <code>luci.ip.cidr</code> instance or a string convertible by
  416. <code>luci.ip.new()</code> to compare against.
  417. </li>
  418. </ul>
  419. <h3>Usage:</h3>
  420. <pre>local addr = luci.ip.new("192.168.1.1")
  421. print(addr:lower(addr)) -- false
  422. print(addr:lower("10.10.10.10/24")) -- false
  423. print(addr:lower(luci.ip.new("::1"))) -- true
  424. print(addr:lower(luci.ip.new("192.168.200.1"))) -- true
  425. print(addr:lower(luci.ip.new("00:14:22:01:23:45"))) -- true</pre>
  426. <h3>Return value:</h3>
  427. <code>true</code> if this CIDR is lower than the given address,
  428. else <code>false</code>.
  429. <h3>See also:</h3>
  430. <ul>
  431. <li><a href="#cidr.higher">
  432. cidr:higher
  433. </a>
  434. <li><a href="#cidr.equal">
  435. cidr:equal
  436. </a>
  437. </ul>
  438. </dd>
  439. <dt><a name="cidr.higher"></a><strong>cidr:higher</strong>&nbsp;(addr)</dt>
  440. <dd>
  441. Checks whether this CIDR instance is higher than the given argument.
  442. The comparisation follows these rules:
  443. <ul><li>An IPv4 address is always lower than an IPv6 address and IPv6 addresses
  444. are considered lower than MAC addresses</li>
  445. <li>Prefix sizes are ignored</li></ul>
  446. <h3>Parameters</h3>
  447. <ul>
  448. <li>
  449. addr: A <code>luci.ip.cidr</code> instance or a string convertible by
  450. <code>luci.ip.new()</code> to compare against.
  451. </li>
  452. </ul>
  453. <h3>Usage:</h3>
  454. <pre>local addr = luci.ip.new("192.168.1.1")
  455. print(addr:higher(addr)) -- false
  456. print(addr:higher("10.10.10.10/24")) -- true
  457. print(addr:higher(luci.ip.new("::1"))) -- false
  458. print(addr:higher(luci.ip.new("192.168.200.1"))) -- false
  459. print(addr:higher(luci.ip.new("00:14:22:01:23:45"))) -- false</pre>
  460. <h3>Return value:</h3>
  461. <code>true</code> if this CIDR is higher than the given address,
  462. else <code>false</code>.
  463. <h3>See also:</h3>
  464. <ul>
  465. <li><a href="#cidr.lower">
  466. cidr:lower
  467. </a>
  468. <li><a href="#cidr.equal">
  469. cidr:equal
  470. </a>
  471. </ul>
  472. </dd>
  473. <dt><a name="cidr.equal"></a><strong>cidr:equal</strong>&nbsp;(addr)</dt>
  474. <dd>
  475. Checks whether this CIDR instance is equal to the given argument.
  476. <h3>Parameters</h3>
  477. <ul>
  478. <li>
  479. addr: A <code>luci.ip.cidr</code> instance or a string convertible by
  480. <code>luci.ip.new()</code> to compare against.
  481. </li>
  482. </ul>
  483. <h3>Usage:</h3>
  484. <pre>local addr = luci.ip.new("192.168.1.1")
  485. print(addr:equal(addr)) -- true
  486. print(addr:equal("192.168.1.1")) -- true
  487. print(addr:equal(luci.ip.new("::1"))) -- false
  488. local addr6 = luci.ip.new("::1")
  489. print(addr6:equal("0:0:0:0:0:0:0:1/64")) -- true
  490. print(addr6:equal(luci.ip.new("fe80::221:63ff:fe75:aa17"))) -- false
  491. local mac = luci.ip.new("00:14:22:01:23:45")
  492. print(mac:equal("0:14:22:1:23:45")) -- true
  493. print(mac:equal(luci.ip.new("01:23:45:67:89:AB")) -- false</pre>
  494. <h3>Return value:</h3>
  495. <code>true</code> if this CIDR is equal to the given address,
  496. else <code>false</code>.
  497. <h3>See also:</h3>
  498. <ul>
  499. <li><a href="#cidr.lower">
  500. cidr:lower
  501. </a>
  502. <li><a href="#cidr.higher">
  503. cidr:higher
  504. </a>
  505. </ul>
  506. </dd>
  507. <dt><a name="cidr.prefix"></a><strong>cidr:prefix</strong>&nbsp;(mask)</dt>
  508. <dd>
  509. Get or set prefix size of CIDR instance.
  510. If the optional mask parameter is given, the prefix size of this CIDR is altered
  511. else the current prefix size is returned.
  512. <h3>Parameters</h3>
  513. <ul>
  514. <li>
  515. mask: Either a number containing the number of bits (<code>0..32</code>
  516. for IPv4, <code>0..128</code> for IPv6 or <code>0..48</code> for MAC addresses) or a string
  517. containing a valid netmask (optional)
  518. </li>
  519. </ul>
  520. <h3>Usage:</h3>
  521. <pre>local range = luci.ip.new("192.168.1.1/255.255.255.0")
  522. print(range:prefix()) -- 24
  523. range:prefix(16)
  524. print(range:prefix()) -- 16
  525. range:prefix("255.255.255.255")
  526. print(range:prefix()) -- 32</pre>
  527. <h3>Return value:</h3>
  528. Bit count of the current prefix size
  529. </dd>
  530. <dt><a name="cidr.network"></a><strong>cidr:network</strong>&nbsp;(mask)</dt>
  531. <dd>
  532. Derive network address of CIDR instance.
  533. Returns a new CIDR instance representing the network address of this instance
  534. with all host parts masked out. The used prefix size can be overridden by the
  535. optional mask parameter.
  536. <h3>Parameters</h3>
  537. <ul>
  538. <li>
  539. mask: Either a number containing the number of bits (<code>0..32</code>
  540. for IPv4, <code>0..128</code> for IPv6 or <code>0..48</code> for MAC addresses) or a string
  541. containing a valid netmask (optional)
  542. </li>
  543. </ul>
  544. <h3>Usage:</h3>
  545. <pre>local range = luci.ip.new("192.168.62.243/255.255.0.0")
  546. print(range:network()) -- "192.168.0.0"
  547. print(range:network(24)) -- "192.168.62.0"
  548. print(range:network("255.255.255.0")) -- "192.168.62.0"
  549. local range6 = luci.ip.new("fd9b:62b3:9cc5:0:221:63ff:fe75:aa17/64")
  550. print(range6:network()) -- "fd9b:62b3:9cc5::"</pre>
  551. <h3>Return value:</h3>
  552. CIDR instance representing the network address
  553. </dd>
  554. <dt><a name="cidr.host"></a><strong>cidr:host</strong>&nbsp;()</dt>
  555. <dd>
  556. Derive host address of CIDR instance.
  557. This function essentially constructs a copy of this CIDR with the prefix size
  558. set to <code>32</code> for IPv4, <code>128</code> for IPv6 or <code>48</code> for MAC addresses.
  559. <h3>Usage:</h3>
  560. <pre>local range = luci.ip.new("172.19.37.45/16")
  561. print(range) -- "172.19.37.45/16"
  562. print(range:host()) -- "172.19.37.45"</pre>
  563. <h3>Return value:</h3>
  564. CIDR instance representing the host address
  565. </dd>
  566. <dt><a name="cidr.mask"></a><strong>cidr:mask</strong>&nbsp;(mask)</dt>
  567. <dd>
  568. Derive netmask of CIDR instance.
  569. Constructs a CIDR instance representing the netmask of this instance. The used
  570. prefix size can be overridden by the optional mask parameter.
  571. <h3>Parameters</h3>
  572. <ul>
  573. <li>
  574. mask: Either a number containing the number of bits (<code>0..32</code>
  575. for IPv4, <code>0..128</code> for IPv6 or <code>0..48</code> for MAC addresses) or a string
  576. containing a valid netmask (optional)
  577. </li>
  578. </ul>
  579. <h3>Usage:</h3>
  580. <pre>local range = luci.ip.new("172.19.37.45/16")
  581. print(range:mask()) -- "255.255.0.0"
  582. print(range:mask(24)) -- "255.255.255.0"
  583. print(range:mask("255.0.0.0")) -- "255.0.0.0"</pre>
  584. <h3>Return value:</h3>
  585. CIDR instance representing the netmask
  586. </dd>
  587. <dt><a name="cidr.broadcast"></a><strong>cidr:broadcast</strong>&nbsp;(mask)</dt>
  588. <dd>
  589. Derive broadcast address of CIDR instance.
  590. Constructs a CIDR instance representing the broadcast address of this instance.
  591. The used prefix size can be overridden by the optional mask parameter.
  592. This function has no effect on IPv6 or MAC address instances, it will return
  593. nothing in this case.
  594. <h3>Parameters</h3>
  595. <ul>
  596. <li>
  597. mask: Either a number containing the number of bits (<code>0..32</code> for IPv4) or
  598. a string containing a valid netmask (optional)
  599. </li>
  600. </ul>
  601. <h3>Usage:</h3>
  602. <pre>local range = luci.ip.new("172.19.37.45/16")
  603. print(range:broadcast()) -- "172.19.255.255"
  604. print(range:broadcast(24)) -- "172.19.37.255"
  605. print(range:broadcast("255.0.0.0")) -- "172.255.255.255"</pre>
  606. <h3>Return value:</h3>
  607. Return a new CIDR instance representing the broadcast address if this
  608. instance is an IPv4 range, else return nothing.
  609. </dd>
  610. <dt><a name="cidr.mapped4"></a><strong>cidr:mapped4</strong>&nbsp;()</dt>
  611. <dd>
  612. Derive mapped IPv4 address of CIDR instance.
  613. Constructs a CIDR instance representing the IPv4 address of the IPv6 mapped
  614. IPv4 address in this instance.
  615. This function has no effect on IPv4 instances, MAC address instances or IPv6
  616. instances which are not a mapped address, it will return nothing in this case.
  617. <h3>Usage:</h3>
  618. <pre>local addr = luci.ip.new("::ffff:172.16.19.1")
  619. print(addr:mapped4()) -- "172.16.19.1"</pre>
  620. <h3>Return value:</h3>
  621. Return a new CIDR instance representing the IPv4 address if this
  622. instance is an IPv6 mapped IPv4 address, else return nothing.
  623. </dd>
  624. <dt><a name="cidr.tomac"></a><strong>cidr:tomac</strong>&nbsp;()</dt>
  625. <dd>
  626. Derive MAC address of IPv6 link local CIDR instance.
  627. Constructs a CIDR instance representing the MAC address contained in the IPv6
  628. link local address of this instance.
  629. This function has no effect on IPv4 instances, MAC address instances or IPv6
  630. instances which are not a link local address, it will return nothing in this
  631. case.
  632. <h3>Usage:</h3>
  633. <pre>local addr = luci.ip.new("fe80::6666:b3ff:fe47:e1b9")
  634. print(addr:tomac()) -- "64:66:B3:47:E1:B9"</pre>
  635. <h3>Return value:</h3>
  636. Return a new CIDR instance representing the MAC address if this
  637. instance is an IPv6 link local address, else return nothing.
  638. </dd>
  639. <dt><a name="cidr.tolinklocal"></a><strong>cidr:tolinklocal</strong>&nbsp;()</dt>
  640. <dd>
  641. Derive IPv6 link local address from MAC address CIDR instance.
  642. Constructs a CIDR instance representing the IPv6 link local address of the
  643. MAC address represented by this instance.
  644. This function has no effect on IPv4 instances or IPv6 instances, it will return
  645. nothing in this case.
  646. <h3>Usage:</h3>
  647. <pre>local mac = luci.ip.new("64:66:B3:47:E1:B9")
  648. print(mac:tolinklocal()) -- "fe80::6666:b3ff:fe47:e1b9"</pre>
  649. <h3>Return value:</h3>
  650. Return a new CIDR instance representing the IPv6 link local address.
  651. </dd>
  652. <dt><a name="cidr.contains"></a><strong>cidr:contains</strong>&nbsp;(addr)</dt>
  653. <dd>
  654. Test whether CIDR contains given range.
  655. <h3>Parameters</h3>
  656. <ul>
  657. <li>
  658. addr: A <code>luci.ip.cidr</code> instance or a string convertible by
  659. <code>luci.ip.new()</code> to test.
  660. </li>
  661. </ul>
  662. <h3>Usage:</h3>
  663. <pre>local range = luci.ip.new("10.24.0.0/255.255.0.0")
  664. print(range:contains("10.24.5.1")) -- true
  665. print(range:contains("::1")) -- false
  666. print(range:contains("10.0.0.0/8")) -- false
  667. local range6 = luci.ip.new("fe80::/10")
  668. print(range6:contains("fe80::221:63f:fe75:aa17/64")) -- true
  669. print(range6:contains("fd9b:6b3:c5:0:221:63f:fe75:aa17/64")) -- false
  670. local intel_macs = luci.ip.MAC("C0:B6:F9:00:00:00/24")
  671. print(intel_macs:contains("C0:B6:F9:A3:C:11")) -- true
  672. print(intel_macs:contains("64:66:B3:47:E1:B9")) -- false</pre>
  673. <h3>Return value:</h3>
  674. <code>true</code> if this instance fully contains the given address else
  675. <code>false</code>.
  676. </dd>
  677. <dt><a name="cidr.add"></a><strong>cidr:add</strong>&nbsp;(amount, inplace)</dt>
  678. <dd>
  679. Add given amount to CIDR instance. If the result would overflow the maximum
  680. address space, the result is set to the highest possible address.
  681. <h3>Parameters</h3>
  682. <ul>
  683. <li>
  684. amount: A numeric value between 0 and 0xFFFFFFFF, a
  685. <code>luci.ip.cidr</code> instance or a string convertible by
  686. <code>luci.ip.new()</code>.
  687. </li>
  688. <li>
  689. inplace: If <code>true</code>, modify this instance instead of returning
  690. a new derived CIDR instance.
  691. </li>
  692. </ul>
  693. <h3>Usage:</h3>
  694. <pre>local addr = luci.ip.new("192.168.1.1/24")
  695. print(addr:add(250)) -- "192.168.1.251/24"
  696. print(addr:add("0.0.99.0")) -- "192.168.100.1/24"
  697. addr:add(256, true) -- true
  698. print(addr) -- "192.168.2.1/24
  699. addr:add("255.0.0.0", true) -- false (overflow)
  700. print(addr) -- "255.255.255.255/24
  701. local addr6 = luci.ip.new("fe80::221:63f:fe75:aa17/64")
  702. print(addr6:add(256)) -- "fe80::221:63f:fe75:ab17/64"
  703. print(addr6:add("::ffff:0")) -- "fe80::221:640:fe74:aa17/64"
  704. addr6:add(256, true) -- true
  705. print(addr6) -- "fe80::221:63f:fe75:ab17/64
  706. addr6:add("ffff::", true) -- false (overflow)
  707. print(addr6) -- "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/64"
  708. local mac = luci.ip.new("00:14:22:01:23:45")
  709. print(mac:add(256)) -- "00:14:22:01:24:45"
  710. print(mac:add("0:0:0:0:FF:0") -- "00:14:22:02:22:45"
  711. mac:add(256, true) -- true
  712. print(mac) -- "00:14:22:01:24:45"
  713. mac:add("FF:FF:0:0:0:0", true) -- false (overflow)
  714. print(mac) -- "FF:FF:FF:FF:FF:FF"</pre>
  715. <h3>Return value:</h3>
  716. <ul>
  717. <li>When adding inplace: Return <code>true</code> if the addition succeeded
  718. or <code>false</code> when the addition overflowed.</li>
  719. <li>When deriving new CIDR: Return new instance representing the value of
  720. this instance plus the added amount or the highest possible address if
  721. the addition overflowed the available address space.</li></ul>
  722. </dd>
  723. <dt><a name="cidr.sub"></a><strong>cidr:sub</strong>&nbsp;(amount, inplace)</dt>
  724. <dd>
  725. Subtract given amount from CIDR instance. If the result would under, the lowest
  726. possible address is returned.
  727. <h3>Parameters</h3>
  728. <ul>
  729. <li>
  730. amount: A numeric value between 0 and 0xFFFFFFFF, a
  731. <code>luci.ip.cidr</code> instance or a string convertible by
  732. <code>luci.ip.new()</code>.
  733. </li>
  734. <li>
  735. inplace: If <code>true</code>, modify this instance instead of returning
  736. a new derived CIDR instance.
  737. </li>
  738. </ul>
  739. <h3>Usage:</h3>
  740. <pre>local addr = luci.ip.new("192.168.1.1/24")
  741. print(addr:sub(256)) -- "192.168.0.1/24"
  742. print(addr:sub("0.168.0.0")) -- "192.0.1.1/24"
  743. addr:sub(256, true) -- true
  744. print(addr) -- "192.168.0.1/24
  745. addr:sub("255.0.0.0", true) -- false (underflow)
  746. print(addr) -- "0.0.0.0/24
  747. local addr6 = luci.ip.new("fe80::221:63f:fe75:aa17/64")
  748. print(addr6:sub(256)) -- "fe80::221:63f:fe75:a917/64"
  749. print(addr6:sub("::ffff:0")) -- "fe80::221:63e:fe76:aa17/64"
  750. addr:sub(256, true) -- true
  751. print(addr) -- "fe80::221:63f:fe75:a917/64"
  752. addr:sub("ffff::", true) -- false (underflow)
  753. print(addr) -- "::/64"
  754. local mac = luci.ip.new("00:14:22:01:23:45")
  755. print(mac:sub(256)) -- "00:14:22:01:22:45"
  756. print(mac:sub("0:0:0:0:FF:0") -- "00:14:22:00:24:45"
  757. mac:sub(256, true) -- true
  758. print(mac) -- "00:14:22:01:22:45"
  759. mac:sub("FF:FF:0:0:0:0", true) -- false (overflow)
  760. print(mac) -- "00:00:00:00:00:00"</pre>
  761. <h3>Return value:</h3>
  762. <ul>
  763. <li>When subtracting inplace: Return <code>true</code> if the subtraction
  764. succeeded or <code>false</code> when the subtraction underflowed.</li>
  765. <li>When deriving new CIDR: Return new instance representing the value of
  766. this instance minus the subtracted amount or the lowest address if
  767. the subtraction underflowed.</li></ul>
  768. </dd>
  769. <dt><a name="cidr.minhost"></a><strong>cidr:minhost</strong>&nbsp;()</dt>
  770. <dd>
  771. Calculate the lowest possible host address within this CIDR instance.
  772. <h3>Usage:</h3>
  773. <pre>local addr = luci.ip.new("192.168.123.56/24")
  774. print(addr:minhost()) -- "192.168.123.1"
  775. local addr6 = luci.ip.new("fd9b:62b3:9cc5:0:221:63ff:fe75:aa17/64")
  776. print(addr6:minhost()) -- "fd9b:62b3:9cc5::1"
  777. local mac = luci.ip.new("00:14:22:01:22:45/32")
  778. print(mac:minhost()) -- "00:14:22:01:00:01"</pre>
  779. <h3>Return value:</h3>
  780. Returns a new CIDR instance representing the lowest host address
  781. within this range.
  782. </dd>
  783. <dt><a name="cidr.maxhost"></a><strong>cidr:maxhost</strong>&nbsp;()</dt>
  784. <dd>
  785. Calculate the highest possible host address within this CIDR instance.
  786. <h3>Usage:</h3>
  787. <pre>local addr = luci.ip.new("192.168.123.56/24")
  788. print(addr:maxhost()) -- "192.168.123.254" (.255 is broadcast)
  789. local addr6 = luci.ip.new("fd9b:62b3:9cc5:0:221:63ff:fe75:aa17/64")
  790. print(addr6:maxhost()) -- "fd9b:62b3:9cc5:0:ffff:ffff:ffff:ffff"
  791. local mac = luci.ip.new("00:14:22:01:22:45/32")
  792. print(mac:maxhost()) -- "00:14:22:01:FF:FF"</pre>
  793. <h3>Return value:</h3>
  794. Returns a new CIDR instance representing the highest host address
  795. within this range.
  796. </dd>
  797. <dt><a name="cidr.string"></a><strong>cidr:string</strong>&nbsp;()</dt>
  798. <dd>
  799. Convert CIDR instance into string representation.
  800. If the prefix size of instance is less than 32 for IPv4, 128 for IPv6 or 48 for
  801. MACs, the address is returned in the form "address/prefix" otherwise just
  802. "address".
  803. It is usually not required to call this function directly as CIDR objects
  804. define it as __tostring function in the associated metatable.
  805. <h3>Return value:</h3>
  806. Returns a string representing the range or address of this CIDR instance
  807. </dd>
  808. </dl>
  809. </div> <!-- id="content" -->
  810. </div> <!-- id="main" -->
  811. <div id="about">
  812. <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>
  813. </div> <!-- id="about" -->
  814. </div> <!-- id="container" -->
  815. </body>
  816. </html>