1
0

luci.ip.cidr.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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.protocol.html">luci.http.protocol</a>
  33. </li>
  34. <li>
  35. <a href="../modules/luci.http.protocol.conditionals.html">luci.http.protocol.conditionals</a>
  36. </li>
  37. <li>
  38. <a href="../modules/luci.http.protocol.date.html">luci.http.protocol.date</a>
  39. </li>
  40. <li>
  41. <a href="../modules/luci.http.protocol.mime.html">luci.http.protocol.mime</a>
  42. </li>
  43. <li>
  44. <a href="../modules/luci.i18n.html">luci.i18n</a>
  45. </li>
  46. <li>
  47. <a href="../modules/luci.ip.html">luci.ip</a>
  48. </li>
  49. <li><strong>luci.ip.cidr</strong></li>
  50. <li>
  51. <a href="../modules/luci.json.html">luci.json</a>
  52. </li>
  53. <li>
  54. <a href="../modules/luci.jsonc.html">luci.jsonc</a>
  55. </li>
  56. <li>
  57. <a href="../modules/luci.jsonc.parser.html">luci.jsonc.parser</a>
  58. </li>
  59. <li>
  60. <a href="../modules/luci.model.ipkg.html">luci.model.ipkg</a>
  61. </li>
  62. <li>
  63. <a href="../modules/luci.model.uci.html">luci.model.uci</a>
  64. </li>
  65. <li>
  66. <a href="../modules/luci.rpcc.html">luci.rpcc</a>
  67. </li>
  68. <li>
  69. <a href="../modules/luci.rpcc.ruci.html">luci.rpcc.ruci</a>
  70. </li>
  71. <li>
  72. <a href="../modules/luci.sys.html">luci.sys</a>
  73. </li>
  74. <li>
  75. <a href="../modules/luci.sys.init.html">luci.sys.init</a>
  76. </li>
  77. <li>
  78. <a href="../modules/luci.sys.iptparser.html">luci.sys.iptparser</a>
  79. </li>
  80. <li>
  81. <a href="../modules/luci.sys.net.html">luci.sys.net</a>
  82. </li>
  83. <li>
  84. <a href="../modules/luci.sys.process.html">luci.sys.process</a>
  85. </li>
  86. <li>
  87. <a href="../modules/luci.sys.user.html">luci.sys.user</a>
  88. </li>
  89. <li>
  90. <a href="../modules/luci.sys.wifi.html">luci.sys.wifi</a>
  91. </li>
  92. <li>
  93. <a href="../modules/luci.util.html">luci.util</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.lower">cidr:lower</a>&nbsp;(addr)</td>
  182. <td class="summary">
  183. Checks whether this CIDR instance is lower than the given argument.</td>
  184. </tr>
  185. <tr>
  186. <td class="name" nowrap><a href="#cidr.higher">cidr:higher</a>&nbsp;(addr)</td>
  187. <td class="summary">
  188. Checks whether this CIDR instance is higher than the given argument.</td>
  189. </tr>
  190. <tr>
  191. <td class="name" nowrap><a href="#cidr.equal">cidr:equal</a>&nbsp;(addr)</td>
  192. <td class="summary">
  193. Checks whether this CIDR instance is equal to the given argument.</td>
  194. </tr>
  195. <tr>
  196. <td class="name" nowrap><a href="#cidr.prefix">cidr:prefix</a>&nbsp;(mask)</td>
  197. <td class="summary">
  198. Get or set prefix size of CIDR instance.</td>
  199. </tr>
  200. <tr>
  201. <td class="name" nowrap><a href="#cidr.network">cidr:network</a>&nbsp;(mask)</td>
  202. <td class="summary">
  203. Derive network address of CIDR instance.</td>
  204. </tr>
  205. <tr>
  206. <td class="name" nowrap><a href="#cidr.host">cidr:host</a>&nbsp;()</td>
  207. <td class="summary">
  208. Derive host address of CIDR instance.</td>
  209. </tr>
  210. <tr>
  211. <td class="name" nowrap><a href="#cidr.mask">cidr:mask</a>&nbsp;(mask)</td>
  212. <td class="summary">
  213. Derive netmask of CIDR instance.</td>
  214. </tr>
  215. <tr>
  216. <td class="name" nowrap><a href="#cidr.broadcast">cidr:broadcast</a>&nbsp;(mask)</td>
  217. <td class="summary">
  218. Derive broadcast address of CIDR instance.</td>
  219. </tr>
  220. <tr>
  221. <td class="name" nowrap><a href="#cidr.mapped4">cidr:mapped4</a>&nbsp;()</td>
  222. <td class="summary">
  223. Derive mapped IPv4 address of CIDR instance.</td>
  224. </tr>
  225. <tr>
  226. <td class="name" nowrap><a href="#cidr.contains">cidr:contains</a>&nbsp;(addr)</td>
  227. <td class="summary">
  228. Test whether CIDR contains given range.</td>
  229. </tr>
  230. <tr>
  231. <td class="name" nowrap><a href="#cidr.add">cidr:add</a>&nbsp;(amount, inplace)</td>
  232. <td class="summary">
  233. Add given amount to CIDR instance.</td>
  234. </tr>
  235. <tr>
  236. <td class="name" nowrap><a href="#cidr.sub">cidr:sub</a>&nbsp;(amount, inplace)</td>
  237. <td class="summary">
  238. Subtract given amount from CIDR instance.</td>
  239. </tr>
  240. <tr>
  241. <td class="name" nowrap><a href="#cidr.minhost">cidr:minhost</a>&nbsp;()</td>
  242. <td class="summary">
  243. Calculate the lowest possible host address within this CIDR instance.</td>
  244. </tr>
  245. <tr>
  246. <td class="name" nowrap><a href="#cidr.maxhost">cidr:maxhost</a>&nbsp;()</td>
  247. <td class="summary">
  248. Calculate the highest possible host address within this CIDR instance.</td>
  249. </tr>
  250. <tr>
  251. <td class="name" nowrap><a href="#cidr.string">cidr:string</a>&nbsp;()</td>
  252. <td class="summary">
  253. Convert CIDR instance into string representation.</td>
  254. </tr>
  255. </table>
  256. <br/>
  257. <br/>
  258. <h2><a name="functions"></a>Functions</h2>
  259. <dl class="function">
  260. <dt><a name="cidr.is4"></a><strong>cidr:is4</strong>&nbsp;()</dt>
  261. <dd>
  262. Checks whether the CIDR instance is an IPv4 address range
  263. <h3>Return value:</h3>
  264. <code>true</code> if the CIDR is an IPv4 range, else <code>false</code>
  265. <h3>See also:</h3>
  266. <ul>
  267. <li><a href="#cidr.is6">
  268. cidr:is6
  269. </a>
  270. </ul>
  271. </dd>
  272. <dt><a name="cidr.is4rfc1918"></a><strong>cidr:is4rfc1918</strong>&nbsp;()</dt>
  273. <dd>
  274. Checks whether the CIDR instance is within the private RFC1918 address space
  275. <h3>Usage:</h3>
  276. <pre>local addr = luci.ip.new("192.168.45.2/24")
  277. if addr:is4rfc1918() then
  278. print("Is a private address")
  279. end</pre>
  280. <h3>Return value:</h3>
  281. <code>true</code> if the entire range of this CIDR lies within one of
  282. the ranges <code>10.0.0.0-10.255.255.255</code>,
  283. <code>172.16.0.0-172.31.0.0</code> or
  284. <code>192.168.0.0-192.168.255.255</code>, else <code>false</code>.
  285. </dd>
  286. <dt><a name="cidr.is4linklocal"></a><strong>cidr:is4linklocal</strong>&nbsp;()</dt>
  287. <dd>
  288. Checks whether the CIDR instance is an IPv4 link local (Zeroconf) address
  289. <h3>Usage:</h3>
  290. <pre>local addr = luci.ip.new("169.254.34.125")
  291. if addr:is4linklocal() then
  292. print("Is a zeroconf address")
  293. end</pre>
  294. <h3>Return value:</h3>
  295. <code>true</code> if the entire range of this CIDR lies within the range
  296. the range <code>169.254.0.0-169.254.255.255</code>, else <code>false</code>.
  297. </dd>
  298. <dt><a name="cidr.is6"></a><strong>cidr:is6</strong>&nbsp;()</dt>
  299. <dd>
  300. Checks whether the CIDR instance is an IPv6 address range
  301. <h3>Return value:</h3>
  302. <code>true</code> if the CIDR is an IPv6 range, else <code>false</code>
  303. <h3>See also:</h3>
  304. <ul>
  305. <li><a href="#cidr.is4">
  306. cidr:is4
  307. </a>
  308. </ul>
  309. </dd>
  310. <dt><a name="cidr.is6linklocal"></a><strong>cidr:is6linklocal</strong>&nbsp;()</dt>
  311. <dd>
  312. Checks whether the CIDR instance is an IPv6 link local address
  313. <h3>Usage:</h3>
  314. <pre>local addr = luci.ip.new("fe92:53a:3216:af01:221:63ff:fe75:aa17/64")
  315. if addr:is6linklocal() then
  316. print("Is a linklocal address")
  317. end</pre>
  318. <h3>Return value:</h3>
  319. <code>true</code> if the entire range of this CIDR lies within the range
  320. the <code>fe80::/10</code> range, else <code>false</code>.
  321. </dd>
  322. <dt><a name="cidr.is6mapped4"></a><strong>cidr:is6mapped4</strong>&nbsp;()</dt>
  323. <dd>
  324. Checks whether the CIDR instance is an IPv6 mapped IPv4 address
  325. <h3>Usage:</h3>
  326. <pre>local addr = luci.ip.new("::ffff:192.168.1.1")
  327. if addr:is6mapped4() then
  328. print("Is a mapped IPv4 address")
  329. end</pre>
  330. <h3>Return value:</h3>
  331. <code>true</code> if the address is an IPv6 mapped IPv4 address in the
  332. form <code>::ffff:1.2.3.4</code>.
  333. </dd>
  334. <dt><a name="cidr.lower"></a><strong>cidr:lower</strong>&nbsp;(addr)</dt>
  335. <dd>
  336. Checks whether this CIDR instance is lower than the given argument.
  337. The comparisation follows these rules:
  338. <ul><li>An IPv4 address is always lower than an IPv6 address</li>
  339. <li>Prefix sizes are ignored</li></ul>
  340. <h3>Parameters</h3>
  341. <ul>
  342. <li>
  343. addr: A <code>luci.ip.cidr</code> instance or a string convertable by
  344. <code>luci.ip.new()</code> to compare against.
  345. </li>
  346. </ul>
  347. <h3>Usage:</h3>
  348. <pre>local addr = luci.ip.new("192.168.1.1")
  349. print(addr:lower(addr)) -- false
  350. print(addr:lower("10.10.10.10/24")) -- false
  351. print(addr:lower(luci.ip.new("::1"))) -- true
  352. print(addr:lower(luci.ip.new("192.168.200.1"))) -- true</pre>
  353. <h3>Return value:</h3>
  354. <code>true</code> if this CIDR is lower than the given address,
  355. else <code>false</code>.
  356. <h3>See also:</h3>
  357. <ul>
  358. <li><a href="#cidr.higher">
  359. cidr:higher
  360. </a>
  361. <li><a href="#cidr.equal">
  362. cidr:equal
  363. </a>
  364. </ul>
  365. </dd>
  366. <dt><a name="cidr.higher"></a><strong>cidr:higher</strong>&nbsp;(addr)</dt>
  367. <dd>
  368. Checks whether this CIDR instance is higher than the given argument.
  369. The comparisation follows these rules:
  370. <ul><li>An IPv4 address is always lower than an IPv6 address</li>
  371. <li>Prefix sizes are ignored</li></ul>
  372. <h3>Parameters</h3>
  373. <ul>
  374. <li>
  375. addr: A <code>luci.ip.cidr</code> instance or a string convertable by
  376. <code>luci.ip.new()</code> to compare against.
  377. </li>
  378. </ul>
  379. <h3>Usage:</h3>
  380. <pre>local addr = luci.ip.new("192.168.1.1")
  381. print(addr:higher(addr)) -- false
  382. print(addr:higher("10.10.10.10/24")) -- true
  383. print(addr:higher(luci.ip.new("::1"))) -- false
  384. print(addr:higher(luci.ip.new("192.168.200.1"))) -- false</pre>
  385. <h3>Return value:</h3>
  386. <code>true</code> if this CIDR is higher than the given address,
  387. else <code>false</code>.
  388. <h3>See also:</h3>
  389. <ul>
  390. <li><a href="#cidr.lower">
  391. cidr:lower
  392. </a>
  393. <li><a href="#cidr.equal">
  394. cidr:equal
  395. </a>
  396. </ul>
  397. </dd>
  398. <dt><a name="cidr.equal"></a><strong>cidr:equal</strong>&nbsp;(addr)</dt>
  399. <dd>
  400. Checks whether this CIDR instance is equal to the given argument.
  401. <h3>Parameters</h3>
  402. <ul>
  403. <li>
  404. addr: A <code>luci.ip.cidr</code> instance or a string convertable by
  405. <code>luci.ip.new()</code> to compare against.
  406. </li>
  407. </ul>
  408. <h3>Usage:</h3>
  409. <pre>local addr = luci.ip.new("192.168.1.1")
  410. print(addr:equal(addr)) -- true
  411. print(addr:equal("192.168.1.1")) -- true
  412. print(addr:equal(luci.ip.new("::1"))) -- false
  413. local addr6 = luci.ip.new("::1")
  414. print(addr6:equal("0:0:0:0:0:0:0:1/64")) -- true
  415. print(addr6:equal(luci.ip.new("fe80::221:63ff:fe75:aa17"))) -- false</pre>
  416. <h3>Return value:</h3>
  417. <code>true</code> if this CIDR is equal to the given address,
  418. else <code>false</code>.
  419. <h3>See also:</h3>
  420. <ul>
  421. <li><a href="#cidr.lower">
  422. cidr:lower
  423. </a>
  424. <li><a href="#cidr.higher">
  425. cidr:higher
  426. </a>
  427. </ul>
  428. </dd>
  429. <dt><a name="cidr.prefix"></a><strong>cidr:prefix</strong>&nbsp;(mask)</dt>
  430. <dd>
  431. Get or set prefix size of CIDR instance.
  432. If the optional mask parameter is given, the prefix size of this CIDR is altered
  433. else the current prefix size is returned.
  434. <h3>Parameters</h3>
  435. <ul>
  436. <li>
  437. mask: Either a number containing the number of bits (<code>0..32</code>
  438. for IPv4, <code>0..128</code> for IPv6) or a string containing a valid
  439. netmask (optional)
  440. </li>
  441. </ul>
  442. <h3>Usage:</h3>
  443. <pre>local range = luci.ip.new("192.168.1.1/255.255.255.0")
  444. print(range:prefix()) -- 24
  445. range:prefix(16)
  446. print(range:prefix()) -- 16
  447. range:prefix("255.255.255.255")
  448. print(range:prefix()) -- 32</pre>
  449. <h3>Return value:</h3>
  450. Bit count of the current prefix size
  451. </dd>
  452. <dt><a name="cidr.network"></a><strong>cidr:network</strong>&nbsp;(mask)</dt>
  453. <dd>
  454. Derive network address of CIDR instance.
  455. Returns a new CIDR instance representing the network address of this instance
  456. with all host parts masked out. The used prefix size can be overridden by the
  457. optional mask parameter.
  458. <h3>Parameters</h3>
  459. <ul>
  460. <li>
  461. mask: Either a number containing the number of bits (<code>0..32</code>
  462. for IPv4, <code>0..128</code> for IPv6) or a string containing a valid
  463. netmask (optional)
  464. </li>
  465. </ul>
  466. <h3>Usage:</h3>
  467. <pre>local range = luci.ip.new("192.168.62.243/255.255.0.0")
  468. print(range:network()) -- "192.168.0.0"
  469. print(range:network(24)) -- "192.168.62.0"
  470. print(range:network("255.255.255.0")) -- "192.168.62.0"
  471. local range6 = luci.ip.new("fd9b:62b3:9cc5:0:221:63ff:fe75:aa17/64")
  472. print(range6:network()) -- "fd9b:62b3:9cc5::"</pre>
  473. <h3>Return value:</h3>
  474. CIDR instance representing the network address
  475. </dd>
  476. <dt><a name="cidr.host"></a><strong>cidr:host</strong>&nbsp;()</dt>
  477. <dd>
  478. Derive host address of CIDR instance.
  479. This function essentially constructs a copy of this CIDR with the prefix size
  480. set to <code>32</code> for IPv4 and <code>128</code> for IPv6.
  481. <h3>Usage:</h3>
  482. <pre>local range = luci.ip.new("172.19.37.45/16")
  483. print(range) -- "172.19.37.45/16"
  484. print(range:host()) -- "172.19.37.45"</pre>
  485. <h3>Return value:</h3>
  486. CIDR instance representing the host address
  487. </dd>
  488. <dt><a name="cidr.mask"></a><strong>cidr:mask</strong>&nbsp;(mask)</dt>
  489. <dd>
  490. Derive netmask of CIDR instance.
  491. Constructs a CIDR instance representing the netmask of this instance. The used
  492. prefix size can be overridden by the optional mask parameter.
  493. <h3>Parameters</h3>
  494. <ul>
  495. <li>
  496. mask: Either a number containing the number of bits (<code>0..32</code>
  497. for IPv4, <code>0..128</code> for IPv6) or a string containing a valid
  498. netmask (optional)
  499. </li>
  500. </ul>
  501. <h3>Usage:</h3>
  502. <pre>local range = luci.ip.new("172.19.37.45/16")
  503. print(range:mask()) -- "255.255.0.0"
  504. print(range:mask(24)) -- "255.255.255.0"
  505. print(range:mask("255.0.0.0")) -- "255.0.0.0"</pre>
  506. <h3>Return value:</h3>
  507. CIDR instance representing the netmask
  508. </dd>
  509. <dt><a name="cidr.broadcast"></a><strong>cidr:broadcast</strong>&nbsp;(mask)</dt>
  510. <dd>
  511. Derive broadcast address of CIDR instance.
  512. Constructs a CIDR instance representing the broadcast address of this instance.
  513. The used prefix size can be overridden by the optional mask parameter.
  514. This function has no effect on IPv6 instances, it will return nothing in this
  515. case.
  516. <h3>Parameters</h3>
  517. <ul>
  518. <li>
  519. mask: Either a number containing the number of bits (<code>0..32</code>
  520. for IPv4, <code>0..128</code> for IPv6) or a string containing a valid
  521. netmask (optional)
  522. </li>
  523. </ul>
  524. <h3>Usage:</h3>
  525. <pre>local range = luci.ip.new("172.19.37.45/16")
  526. print(range:broadcast()) -- "172.19.255.255"
  527. print(range:broadcast(24)) -- "172.19.37.255"
  528. print(range:broadcast("255.0.0.0")) -- "172.255.255.255"</pre>
  529. <h3>Return value:</h3>
  530. Return a new CIDR instance representing the broadcast address if this
  531. instance is an IPv4 range, else return nothing.
  532. </dd>
  533. <dt><a name="cidr.mapped4"></a><strong>cidr:mapped4</strong>&nbsp;()</dt>
  534. <dd>
  535. Derive mapped IPv4 address of CIDR instance.
  536. Constructs a CIDR instance representing the IPv4 address of the IPv6 mapped
  537. IPv4 address in this instance.
  538. This function has no effect on IPv4 instances or IPv6 instances which are not a
  539. mapped address, it will return nothing in this case.
  540. <h3>Usage:</h3>
  541. <pre>local addr = luci.ip.new("::ffff:172.16.19.1")
  542. print(addr:mapped4()) -- "172.16.19.1"</pre>
  543. <h3>Return value:</h3>
  544. Return a new CIDR instance representing the IPv4 address if this
  545. instance is an IPv6 mapped IPv4 address, else return nothing.
  546. </dd>
  547. <dt><a name="cidr.contains"></a><strong>cidr:contains</strong>&nbsp;(addr)</dt>
  548. <dd>
  549. Test whether CIDR contains given range.
  550. <h3>Parameters</h3>
  551. <ul>
  552. <li>
  553. addr: A <code>luci.ip.cidr</code> instance or a string convertable by
  554. <code>luci.ip.new()</code> to test.
  555. </li>
  556. </ul>
  557. <h3>Usage:</h3>
  558. <pre>local range = luci.ip.new("10.24.0.0/255.255.0.0")
  559. print(range:contains("10.24.5.1")) -- true
  560. print(range:contains("::1")) -- false
  561. print(range:contains("10.0.0.0/8")) -- false
  562. local range6 = luci.ip.new("fe80::/10")
  563. print(range6:contains("fe80::221:63f:fe75:aa17/64")) -- true
  564. print(range6:contains("fd9b:6b3:c5:0:221:63f:fe75:aa17/64")) -- false</pre>
  565. <h3>Return value:</h3>
  566. <code>true</code> if this instance fully contains the given address else
  567. <code>false</code>.
  568. </dd>
  569. <dt><a name="cidr.add"></a><strong>cidr:add</strong>&nbsp;(amount, inplace)</dt>
  570. <dd>
  571. Add given amount to CIDR instance. If the result would overflow the maximum
  572. address space, the result is set to the highest possible address.
  573. <h3>Parameters</h3>
  574. <ul>
  575. <li>
  576. amount: A numeric value between 0 and 0xFFFFFFFF, a
  577. <code>luci.ip.cidr</code> instance or a string convertable by
  578. <code>luci.ip.new()</code>.
  579. </li>
  580. <li>
  581. inplace: If <code>true</code>, modify this instance instead of returning
  582. a new derived CIDR instance.
  583. </li>
  584. </ul>
  585. <h3>Usage:</h3>
  586. <pre>local addr = luci.ip.new("192.168.1.1/24")
  587. print(addr:add(250)) -- "192.168.1.251/24"
  588. print(addr:add("0.0.99.0")) -- "192.168.100.1/24"
  589. addr:add(256, true) -- true
  590. print(addr) -- "192.168.2.1/24
  591. addr:add("255.0.0.0", true) -- false (overflow)
  592. print(addr) -- "255.255.255.255/24
  593. local addr6 = luci.ip.new("fe80::221:63f:fe75:aa17/64")
  594. print(addr6:add(256)) -- "fe80::221:63f:fe75:ab17/64"
  595. print(addr6:add("::ffff:0")) -- "fe80::221:640:fe74:aa17/64"
  596. addr:add(256, true) -- true
  597. print(addr) -- "fe80::221:63f:fe75:ab17/64
  598. addr:add("ffff::", true) -- false (overflow)
  599. print(addr) -- "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/64"</pre>
  600. <h3>Return value:</h3>
  601. <ul>
  602. <li>When adding inplace: Return <code>true</code> if the addition succeeded
  603. or <code>false</code> when the addition overflowed.</li>
  604. <li>When deriving new CIDR: Return new instance representing the value of
  605. this instance plus the added amount or the highest possible address if
  606. the addition overflowed the available address space.</li></ul>
  607. </dd>
  608. <dt><a name="cidr.sub"></a><strong>cidr:sub</strong>&nbsp;(amount, inplace)</dt>
  609. <dd>
  610. Subtract given amount from CIDR instance. If the result would under, the lowest
  611. possible address is returned.
  612. <h3>Parameters</h3>
  613. <ul>
  614. <li>
  615. amount: A numeric value between 0 and 0xFFFFFFFF, a
  616. <code>luci.ip.cidr</code> instance or a string convertable by
  617. <code>luci.ip.new()</code>.
  618. </li>
  619. <li>
  620. inplace: If <code>true</code>, modify this instance instead of returning
  621. a new derived CIDR instance.
  622. </li>
  623. </ul>
  624. <h3>Usage:</h3>
  625. <pre>local addr = luci.ip.new("192.168.1.1/24")
  626. print(addr:sub(256)) -- "192.168.0.1/24"
  627. print(addr:sub("0.168.0.0")) -- "192.0.1.1/24"
  628. addr:sub(256, true) -- true
  629. print(addr) -- "192.168.0.1/24
  630. addr:sub("255.0.0.0", true) -- false (underflow)
  631. print(addr) -- "0.0.0.0/24
  632. local addr6 = luci.ip.new("fe80::221:63f:fe75:aa17/64")
  633. print(addr6:sub(256)) -- "fe80::221:63f:fe75:a917/64"
  634. print(addr6:sub("::ffff:0")) -- "fe80::221:63e:fe76:aa17/64"
  635. addr:sub(256, true) -- true
  636. print(addr) -- "fe80::221:63f:fe75:a917/64"
  637. addr:sub("ffff::", true) -- false (underflow)
  638. print(addr) -- "::/64"</pre>
  639. <h3>Return value:</h3>
  640. <ul>
  641. <li>When subtracting inplace: Return <code>true</code> if the subtraction
  642. succeeded or <code>false</code> when the subtraction underflowed.</li>
  643. <li>When deriving new CIDR: Return new instance representing the value of
  644. this instance minus the subtracted amount or the lowest address if
  645. the subtraction underflowed.</li></ul>
  646. </dd>
  647. <dt><a name="cidr.minhost"></a><strong>cidr:minhost</strong>&nbsp;()</dt>
  648. <dd>
  649. Calculate the lowest possible host address within this CIDR instance.
  650. <h3>Usage:</h3>
  651. <pre>local addr = luci.ip.new("192.168.123.56/24")
  652. print(addr:minhost()) -- "192.168.123.1"
  653. local addr6 = luci.ip.new("fd9b:62b3:9cc5:0:221:63ff:fe75:aa17/64")
  654. print(addr6:minhost()) -- "fd9b:62b3:9cc5::1"</pre>
  655. <h3>Return value:</h3>
  656. Returns a new CIDR instance representing the lowest host address
  657. within this range.
  658. </dd>
  659. <dt><a name="cidr.maxhost"></a><strong>cidr:maxhost</strong>&nbsp;()</dt>
  660. <dd>
  661. Calculate the highest possible host address within this CIDR instance.
  662. <h3>Usage:</h3>
  663. <pre>local addr = luci.ip.new("192.168.123.56/24")
  664. print(addr:maxhost()) -- "192.168.123.254" (.255 is broadcast)
  665. local addr6 = luci.ip.new("fd9b:62b3:9cc5:0:221:63ff:fe75:aa17/64")
  666. print(addr6:maxhost()) -- "fd9b:62b3:9cc5:0:ffff:ffff:ffff:ffff"</pre>
  667. <h3>Return value:</h3>
  668. Returns a new CIDR instance representing the highest host address
  669. within this range.
  670. </dd>
  671. <dt><a name="cidr.string"></a><strong>cidr:string</strong>&nbsp;()</dt>
  672. <dd>
  673. Convert CIDR instance into string representation.
  674. If the prefix size of instance is less than 32 for IPv4 or 128 for IPv6, the
  675. address is returned in the form "address/prefix" otherwise just "address".
  676. It is usually not required to call this function directly as CIDR objects
  677. define it as __tostring function in the associated metatable.
  678. <h3>Return value:</h3>
  679. Returns a string representing the range or address of this CIDR instance
  680. </dd>
  681. </dl>
  682. </div> <!-- id="content" -->
  683. </div> <!-- id="main" -->
  684. <div id="about">
  685. <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>
  686. </div> <!-- id="about" -->
  687. </div> <!-- id="container" -->
  688. </body>
  689. </html>