2
0

htable_admin.xml 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  1. <?xml version="1.0" encoding='ISO-8859-1'?>
  2. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
  4. <!-- Include general documentation entities -->
  5. <!ENTITY % docentities SYSTEM "../../../docbook/entities.xml">
  6. %docentities;
  7. ]>
  8. <!-- Module User's Guide -->
  9. <chapter>
  10. <title>&adminguide;</title>
  11. <section>
  12. <title>Overview</title>
  13. <para>
  14. The module adds a hash table container to the configuration language. The
  15. hash table is stored in shared memory and the access to it can be
  16. done via pseudo-variables: $sht(htname=&gt;name). The module supports
  17. definition of many hash tables and can load values at startup from
  18. a database table.
  19. </para>
  20. <para>
  21. A typical use case for the SIP server is to implement a cache system
  22. in configuration file - if a value is not found in hash table, load
  23. it from database and store it in hash table so next time the access to
  24. it is very fast. In the definition of the table you can define the
  25. default expiration time of cached items. The expiration time can
  26. be adjusted per item via assignment operation at runtime.
  27. </para>
  28. <para>
  29. Replication between multiple servers is performed automatically (if
  30. enabled) via the DMQ module.
  31. </para>
  32. <para>
  33. You can read more about hash tables at:
  34. http://en.wikipedia.org/wiki/Hash_table.
  35. </para>
  36. <para>
  37. The <quote>name</quote> can be a static string or can include pseudo-
  38. variables that will be replaced at runtime.
  39. </para>
  40. <example>
  41. <title>Accessing $sht(htname=&gt;key)</title>
  42. <programlisting format="linespecific">
  43. ...
  44. modparam("htable", "htable", "a=&gt;size=8;")
  45. ...
  46. $sht(a=&gt;test) = 1;
  47. $sht(a=&gt;$ci::srcip) = $si;
  48. ...
  49. </programlisting>
  50. </example>
  51. <para>
  52. Next example shows a way to protect against dictionary attacks. If
  53. someone fails to authenticate 3 times, it is forbidden for 15min.
  54. Authenticatiion against database is expensive as it does a select
  55. on the <quote>subscriberthe</quote> table. By disabling the DB auth for 15min,
  56. resources on the server are saved and time to discover the password is increased
  57. substantially. Additional alerting can be done by writing a message
  58. to syslog or sending email, etc.
  59. </para>
  60. <para>
  61. To implement the logic, two hash table variables are used: one counting
  62. the failed authentications per user and one for storing the time of
  63. last authentication attempt. To ensure a unique name per user, the
  64. hash table uses a combination of authentication username and text
  65. <quote>::auth_count</quote> and <quote>::last_auth</quote>.
  66. </para>
  67. <example>
  68. <title>Dictionary attack limitation</title>
  69. <programlisting format="linespecific">
  70. ...
  71. modparam("htable", "htable", "a=&gt;size=8;")
  72. ...
  73. if(is_present_hf("Authorization"))
  74. {
  75. if($sht(a=&gt;$au::auth_count)==3)
  76. {
  77. $var(exp) = $Ts - 900;
  78. if($sht(a=&gt;$au::last_auth) &gt; $var(exp))
  79. {
  80. sl_send_reply("403", "Try later");
  81. exit;
  82. } else {
  83. $sht(a=&gt;$au::auth_count) = 0;
  84. }
  85. }
  86. if(!www_authenticate("$td", "subscriber"))
  87. {
  88. switch ($retcode) {
  89. case -1:
  90. sl_send_reply("403", "Forbidden");
  91. exit;
  92. case -2:
  93. if($sht(a=&gt;$au::auth_count) == $null)
  94. $sht(a=&gt;$au::auth_count) = 0;
  95. $sht(a=&gt;$au::auth_count) = $sht(a=&gt;$au::auth_count) + 1;
  96. if($sht(a=&gt;$au::auth_count) == 3)
  97. xlog("auth failed 3rd time - src ip: $si\n");
  98. $sht(a=&gt;$au::last_auth) = $Ts;
  99. break;
  100. }
  101. www_challenge("$td"/*realm*/,"0"/*qop*/);
  102. exit;
  103. }
  104. $sht(a=&gt;$au::auth_count) = 0;
  105. } else {
  106. www_challenge("$td","0");
  107. exit;
  108. }
  109. ...
  110. </programlisting>
  111. </example>
  112. <para>
  113. The module also provides a way to store multiple values for a single key.
  114. This is emulated by storing individual keys as 'key_name[n]', where n is
  115. incremented for each key. The total number of keys is stored in a
  116. dedicated key, by default: 'key_name::size'.
  117. </para>
  118. <para>
  119. The array is built when the table is loaded in memory and afterwards all
  120. the keys are treated as individual keys.
  121. If a particular entry in the array is deleted, it is the administrator's
  122. responsibility to update the size of the array and any other elements
  123. (if required).
  124. </para>
  125. <example>
  126. <title>Storing array values</title>
  127. <programlisting format="linespecific">
  128. # Example of dbtext with multiple keys
  129. $ cat /usr/local/etc/kamailio/dbtext/htable
  130. 1:key:1:0:value3:0
  131. 2:key:1:0:value2:0
  132. 3:key:1:0:value1:0
  133. # The array key will be loaded in memory in the following format:
  134. $ kamcmd htable.dump htable
  135. {
  136. entry: 35
  137. size: 1
  138. slot: {
  139. item: {
  140. name: key[0]
  141. value: value1
  142. }
  143. }
  144. }
  145. {
  146. entry: 50
  147. size: 1
  148. slot: {
  149. item: {
  150. name: key::size
  151. value: 3
  152. }
  153. }
  154. }
  155. {
  156. entry: 67
  157. size: 1
  158. slot: {
  159. item: {
  160. name: key[1]
  161. value: value2
  162. }
  163. }
  164. }
  165. {
  166. entry: 227
  167. size: 1
  168. slot: {
  169. item: {
  170. name: key[2]
  171. value: value3
  172. }
  173. }
  174. }
  175. # Now let's delete a particular entry in the array: key[0].
  176. $ kamcmd htable.delete htable key[0]
  177. # The array key will look like this after a key was deleted:
  178. $ kamcmd htable.dump htable
  179. {
  180. entry: 50
  181. size: 1
  182. slot: {
  183. item: {
  184. name: key::size
  185. value: 3
  186. }
  187. }
  188. }
  189. {
  190. entry: 67
  191. size: 1
  192. slot: {
  193. item: {
  194. name: key[1]
  195. value: value2
  196. }
  197. }
  198. }
  199. {
  200. entry: 227
  201. size: 1
  202. slot: {
  203. item: {
  204. name: key[2]
  205. value: value3
  206. }
  207. }
  208. }
  209. </programlisting>
  210. </example>
  211. </section>
  212. <section>
  213. <title>Dependencies</title>
  214. <section>
  215. <title>&kamailio; Modules</title>
  216. <para>
  217. The following modules must be loaded before this module:
  218. <itemizedlist>
  219. <listitem>
  220. <para>
  221. <emphasis>If DMQ replication is enabled, the DMQ module must be loaded first.</emphasis>.
  222. </para>
  223. </listitem>
  224. </itemizedlist>
  225. </para>
  226. </section>
  227. <section>
  228. <title>External Libraries or Applications</title>
  229. <para>
  230. The following libraries or applications must be installed before running
  231. &kamailio; with this module loaded:
  232. <itemizedlist>
  233. <listitem>
  234. <para>
  235. <emphasis>None</emphasis>.
  236. </para>
  237. </listitem>
  238. </itemizedlist>
  239. </para>
  240. </section>
  241. <section>
  242. <title>Loading from database</title>
  243. <para>
  244. The module is able to load values in a hash table at startup upon
  245. providing a DB URL and table name.
  246. </para>
  247. <para>
  248. The structure of the table must contain:
  249. </para>
  250. <itemizedlist>
  251. <listitem>
  252. <para>
  253. <emphasis>key name</emphasis> - string containing the name of
  254. the key.
  255. </para>
  256. </listitem>
  257. <listitem>
  258. <para>
  259. <emphasis>key type</emphasis> - the type of the key
  260. </para>
  261. <itemizedlist>
  262. <listitem>
  263. <para>
  264. <emphasis>0</emphasis> - simple key - the key is added
  265. as 'key_name'.
  266. </para>
  267. </listitem>
  268. <listitem>
  269. <para>
  270. <emphasis>1</emphasis> - array key - the key is added
  271. as 'key_name[n]' - n is incremented for each key with this
  272. name to build an array in hash table. In addition, an additional
  273. key is built to hold the total number of key in the array,
  274. by default key_name::size (see array_size_suffix parameter).
  275. </para>
  276. </listitem>
  277. </itemizedlist>
  278. </listitem>
  279. <listitem>
  280. <para>
  281. <emphasis>value type</emphasis> - the type of the key value
  282. </para>
  283. <itemizedlist>
  284. <listitem>
  285. <para>
  286. <emphasis>0</emphasis> - value is string.
  287. </para>
  288. </listitem>
  289. <listitem>
  290. <para>
  291. <emphasis>1</emphasis> - value is integer.
  292. </para>
  293. </listitem>
  294. </itemizedlist>
  295. </listitem>
  296. <listitem>
  297. <para>
  298. <emphasis>key value</emphasis> - string containing the value of
  299. the key.
  300. </para>
  301. </listitem>
  302. </itemizedlist>
  303. </section>
  304. </section>
  305. <section>
  306. <title>Parameters</title>
  307. <section id="htable.p.htable">
  308. <title><varname>htable</varname> (str)</title>
  309. <para>
  310. The definition of a hash table. The value of the parameter must have the
  311. following format:
  312. </para>
  313. <itemizedlist>
  314. <listitem>
  315. <para>
  316. "htname=&gt;size=_number_;autoexpire=_number_;dbtable=_string_"
  317. </para>
  318. </listitem>
  319. </itemizedlist>
  320. <para>
  321. The parameter can be set multiple times to get more hash tables in
  322. same configuration file.
  323. </para>
  324. <itemizedlist>
  325. <listitem>
  326. <para>
  327. <emphasis>htname</emphasis> - string specifying the name of
  328. the hash table. This string is used by <emphasis>$sht(...)</emphasis>
  329. to refer to the hash table.
  330. </para>
  331. </listitem>
  332. <listitem>
  333. <para>
  334. <emphasis>size</emphasis> - number specifying the size of hash
  335. table. Larger value means less collisions. The number of entries
  336. (aka slots or buckets) in the table is 2^size. The possible range
  337. for this value is from 2 to 31, smaller or larger values will be
  338. increased to 3 (8 slots) or decreased to 14 (16384 slots).
  339. </para>
  340. </listitem>
  341. <listitem>
  342. <para>
  343. <emphasis>autoexpire</emphasis> -time in seconds to delete an item
  344. from hash table if no update was done to it. If is missing or set
  345. to 0, the items won't expire.
  346. </para>
  347. </listitem>
  348. <listitem>
  349. <para>
  350. <emphasis>dbtable</emphasis> - name of database to be loaded at
  351. startup in hash table. If empty or missing, no data will be loaded.
  352. </para>
  353. </listitem>
  354. <listitem>
  355. <para>
  356. <emphasis>dbmode</emphasis> - if set to 1, the content of hash
  357. table is written to database table when the SIP server is stopped
  358. (i.e., ensure persistency over restarts). Default value is 0 (no
  359. write back to db table).
  360. </para>
  361. </listitem>
  362. <listitem>
  363. <para>
  364. <emphasis>initval</emphasis> - the integer value to be returned
  365. instead of $null when a requested key is not set.
  366. </para>
  367. </listitem>
  368. <listitem>
  369. <para>
  370. <emphasis>updateexpire</emphasis> - if set to 1 (default), the time until
  371. expiration of an item is reset when that item is updated. Certain uses of
  372. htable may dictate that updates should not reset the expiration timeout, however,
  373. in which case this attribute can be set to 0.
  374. </para>
  375. </listitem>
  376. <listitem>
  377. <para>
  378. <emphasis>dmqreplicate</emphasis> - if set to 1, any actions (set, update, delete
  379. etc.) performed upon entries in this table will be replicated to other nodes (htable
  380. peers). Please note, module parameter <quote>enable_dmq</quote> must also be set in
  381. order for this to apply (see below). Default is 0 (no replication).
  382. </para>
  383. </listitem>
  384. </itemizedlist>
  385. <para>
  386. <emphasis>
  387. Default value is NULL.
  388. </emphasis>
  389. </para>
  390. <example>
  391. <title>Set <varname>hash_size</varname> parameter</title>
  392. <programlisting format="linespecific">
  393. ...
  394. modparam("htable", "htable", "a=&gt;size=4;autoexpire=7200;dbtable=htable_a;")
  395. modparam("htable", "htable", "b=&gt;size=5;")
  396. modparam("htable", "htable", "c=&gt;size=4;autoexpire=7200;initval=1;dmqreplicate=1;")
  397. ...
  398. </programlisting>
  399. </example>
  400. </section>
  401. <section id="htable.p.db_url">
  402. <title><varname>db_url</varname> (str)</title>
  403. <para>
  404. The <acronym>URL</acronym> to connect to database for loading values in hash
  405. table at start up.
  406. </para>
  407. <para>
  408. <emphasis>
  409. Default value is NULL (do not connect).
  410. </emphasis>
  411. </para>
  412. <example>
  413. <title>Set <varname>db_url</varname> parameter</title>
  414. <programlisting format="linespecific">
  415. ...
  416. modparam("htable", "db_url", "&defaultdb;")
  417. ...
  418. </programlisting>
  419. </example>
  420. </section>
  421. <section id="htable.p.key_name_column">
  422. <title><varname>key_name_column</varname> (str)</title>
  423. <para>
  424. The name of the column containing the hash table key name.
  425. </para>
  426. <para>
  427. <emphasis>
  428. Default value is 'key_name'.
  429. </emphasis>
  430. </para>
  431. <example>
  432. <title>Set <varname>key_name_column</varname> parameter</title>
  433. <programlisting format="linespecific">
  434. ...
  435. modparam("htable", "key_name_column", "kname")
  436. ...
  437. </programlisting>
  438. </example>
  439. </section>
  440. <section id="htable.p.key_type_column">
  441. <title><varname>key_type_column</varname> (str)</title>
  442. <para>
  443. The name of the column containing the hash table key type.
  444. </para>
  445. <para>
  446. <emphasis>
  447. Default value is 'key_type'.
  448. </emphasis>
  449. </para>
  450. <example>
  451. <title>Set <varname>key_type_column</varname> parameter</title>
  452. <programlisting format="linespecific">
  453. ...
  454. modparam("htable", "key_type_column", "ktype")
  455. ...
  456. </programlisting>
  457. </example>
  458. </section>
  459. <section id="htable.p.value_type_column">
  460. <title><varname>value_type_column</varname> (str)</title>
  461. <para>
  462. The name of the column containing the hash table value type.
  463. </para>
  464. <para>
  465. <emphasis>
  466. Default value is 'value_type'.
  467. </emphasis>
  468. </para>
  469. <example>
  470. <title>Set <varname>value_type_column</varname> parameter</title>
  471. <programlisting format="linespecific">
  472. ...
  473. modparam("htable", "value_type_column", "vtype")
  474. ...
  475. </programlisting>
  476. </example>
  477. </section>
  478. <section id="htable.p.key_value_column">
  479. <title><varname>key_value_column</varname> (str)</title>
  480. <para>
  481. The name of the column containing hash table key value.
  482. </para>
  483. <para>
  484. <emphasis>
  485. Default value is 'key_value'.
  486. </emphasis>
  487. </para>
  488. <example>
  489. <title>Set <varname>key_value_column</varname> parameter</title>
  490. <programlisting format="linespecific">
  491. ...
  492. modparam("htable", "key_value_column", "kvalue")
  493. ...
  494. </programlisting>
  495. </example>
  496. </section>
  497. <section id="htable.p.expires_column">
  498. <title><varname>expires_column</varname> (str)</title>
  499. <para>
  500. The name of the column containing the expires value.
  501. </para>
  502. <para>
  503. <emphasis>
  504. Default value is 'expires'.
  505. </emphasis>
  506. </para>
  507. <example>
  508. <title>Set <varname>expires_column</varname> parameter</title>
  509. <programlisting format="linespecific">
  510. ...
  511. modparam("htable", "expires", "expiry")
  512. ...
  513. </programlisting>
  514. </example>
  515. </section>
  516. <section id="htable.p.array_size_suffix">
  517. <title><varname>array_size_suffix</varname> (str)</title>
  518. <para>
  519. The suffix to be added to store the number of items in an
  520. array (see key type).
  521. </para>
  522. <para>
  523. <emphasis>
  524. Default value is '::size'.
  525. </emphasis>
  526. </para>
  527. <example>
  528. <title>Set <varname>array_size_suffix</varname> parameter</title>
  529. <programlisting format="linespecific">
  530. ...
  531. modparam("htable", "array_size_suffix", "-count")
  532. ...
  533. </programlisting>
  534. </example>
  535. </section>
  536. <section id="htable.p.fetch_rows">
  537. <title><varname>fetch_rows</varname> (integer)</title>
  538. <para>
  539. How many rows to fetch at once from database.
  540. </para>
  541. <para>
  542. <emphasis>
  543. Default value is 100.
  544. </emphasis>
  545. </para>
  546. <example>
  547. <title>Set <varname>fetch_rows</varname> parameter</title>
  548. <programlisting format="linespecific">
  549. ...
  550. modparam("htable", "fetch_rows", 1000)
  551. ...
  552. </programlisting>
  553. </example>
  554. </section>
  555. <section id="htable.p.timer_interval">
  556. <title><varname>timer_interval</varname> (integer)</title>
  557. <para>
  558. Interval in seconds to check for expired htable values.
  559. </para>
  560. <para>
  561. <emphasis>
  562. Default value is 20.
  563. </emphasis>
  564. </para>
  565. <example>
  566. <title>Set <varname>timer_interval</varname> parameter</title>
  567. <programlisting format="linespecific">
  568. ...
  569. modparam("htable", "timer_interval", 10)
  570. ...
  571. </programlisting>
  572. </example>
  573. </section>
  574. <section id="htable.p.timer_mode">
  575. <title><varname>timer_mode</varname> (integer)</title>
  576. <para>
  577. If set to 1, the module will start a new timer process. If set to 0
  578. will use the default timer process to check for expired htable
  579. values.
  580. </para>
  581. <para>
  582. <emphasis>
  583. Default value is 0.
  584. </emphasis>
  585. </para>
  586. <example>
  587. <title>Set <varname>timer_mode</varname> parameter</title>
  588. <programlisting format="linespecific">
  589. ...
  590. modparam("htable", "timer_mode", 1)
  591. ...
  592. </programlisting>
  593. </example>
  594. </section>
  595. <section id="htable.p.db_expires">
  596. <title><varname>db_expires</varname> (integer)</title>
  597. <para>
  598. If set to 1, the module will load/save the expires values of the items in
  599. hash table from/to database. It applies only to hash tables that
  600. have the auto-expires attribute defined.
  601. </para>
  602. <para>
  603. <emphasis>
  604. Default value is 0.
  605. </emphasis>
  606. </para>
  607. <example>
  608. <title>Set <varname>db_expires</varname> parameter</title>
  609. <programlisting format="linespecific">
  610. ...
  611. modparam("htable", "db_expires", 1)
  612. ...
  613. </programlisting>
  614. </example>
  615. </section>
  616. <section id="htable.p.enable_dmq">
  617. <title><varname>enable_dmq</varname> (integer)</title>
  618. <para>
  619. If set to 1, will enable DMQ replication of actions performed upon
  620. entries in all tables having "dmqreplicate" parameter set. Any update
  621. action performed via pseudo-variables, MI and RPC commands will be
  622. repeated on all other nodes. Therefore, it is important to ensure the
  623. table definition (size, autoexpire etc.) is identical across all instances.
  624. </para>
  625. <para>
  626. Currently, values are not replicated on load from DB as it is expected
  627. that in these cases, all servers will load their values from the same DB.
  628. </para>
  629. <para>
  630. <emphasis>
  631. Default value is 0.
  632. </emphasis>
  633. </para>
  634. <example>
  635. <title>Set <varname>enable_dmq</varname> parameter</title>
  636. <programlisting format="linespecific">
  637. ...
  638. modparam("htable", "enable_dmq", 1)
  639. ...
  640. </programlisting>
  641. </example>
  642. </section>
  643. </section>
  644. <section>
  645. <title>Functions</title>
  646. <section id="htable.f.sht_print">
  647. <title>
  648. <function moreinfo="none">sht_print()</function>
  649. </title>
  650. <para>
  651. Dump content of hash table to L_ERR log level. Intended for debug
  652. purposes.
  653. </para>
  654. <para>
  655. This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
  656. ONREPLY_ROUTE, BRANCH_ROUTE.
  657. </para>
  658. <example>
  659. <title><function>sht_print</function> usage</title>
  660. <programlisting format="linespecific">
  661. ...
  662. sht_print();
  663. ...
  664. </programlisting>
  665. </example>
  666. </section>
  667. <section id="htable.f.sht_rm_name_re">
  668. <title>
  669. <function moreinfo="none">sht_rm_name_re(htable=>regexp)</function>
  670. </title>
  671. <para>
  672. Delete all entries in the htable that match the name against
  673. regular expression.
  674. </para>
  675. <para>
  676. This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
  677. ONREPLY_ROUTE, BRANCH_ROUTE.
  678. </para>
  679. <example>
  680. <title><function>sht_rm_name_re</function> usage</title>
  681. <programlisting format="linespecific">
  682. ...
  683. sht_rm_name_re("ha=>.*");
  684. ...
  685. </programlisting>
  686. </example>
  687. </section>
  688. <section id="htable.f.sht_rm_value_re">
  689. <title>
  690. <function moreinfo="none">sht_rm_value_re(htable=>regexp)</function>
  691. </title>
  692. <para>
  693. Delete all entries in the htable that match the value against
  694. regular expression.
  695. </para>
  696. <para>
  697. This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
  698. ONREPLY_ROUTE, BRANCH_ROUTE.
  699. </para>
  700. <example>
  701. <title><function>sht_rm_value_re</function> usage</title>
  702. <programlisting format="linespecific">
  703. ...
  704. sht_rm_value_re("ha=>.*");
  705. ...
  706. </programlisting>
  707. </example>
  708. </section>
  709. <section id="htable.f.sht_reset">
  710. <title>
  711. <function moreinfo="none">sht_reset(htable)</function>
  712. </title>
  713. <para>
  714. Delete all entries in the htable. The name of the hash table
  715. can be a dynamic string with variables.
  716. </para>
  717. <para>
  718. This function can be used from ANY_ROUTE.
  719. </para>
  720. <example>
  721. <title><function>sht_reset</function> usage</title>
  722. <programlisting format="linespecific">
  723. ...
  724. sht_reset("ha$var(x)");
  725. ...
  726. </programlisting>
  727. </example>
  728. </section>
  729. <section id="htable.f.sht_lock">
  730. <title>
  731. <function moreinfo="none">sht_lock(htable=>key)</function>
  732. </title>
  733. <para>
  734. Lock the slot in htable corespoding to the key item. Note that
  735. the locking is re-entrant for the process, therefore the lock
  736. and unlock should be done by the same process.
  737. </para>
  738. <para>
  739. This function can be used from ANY_ROUTE.
  740. </para>
  741. <example>
  742. <title><function>sht_lock</function> usage</title>
  743. <programlisting format="linespecific">
  744. ...
  745. sht_lock("ha=>test");
  746. ...
  747. </programlisting>
  748. </example>
  749. </section>
  750. <section id="htable.f.sht_unlock">
  751. <title>
  752. <function moreinfo="none">sht_unlock(htable=>key)</function>
  753. </title>
  754. <para>
  755. Unlock the slot in htable corespoding to the key item. Note that
  756. the locking is re-entrant for the process, therefore the lock
  757. and unlock should be done by the same process.
  758. </para>
  759. <para>
  760. This function can be used from ANY_ROUTE.
  761. </para>
  762. <example>
  763. <title><function>sht_unlock</function> usage</title>
  764. <programlisting format="linespecific">
  765. ...
  766. sht_lock("ha=>test");
  767. $sht(ha=>test) = $sht(ha=>test) + 10;
  768. sht_unlock("ha=>test");
  769. ...
  770. </programlisting>
  771. </example>
  772. </section>
  773. <section id="htable.f.sht_iterator_start">
  774. <title>
  775. <function moreinfo="none">sht_iterator_start(iname, hname)</function>
  776. </title>
  777. <para>
  778. Start an iterator for hash table named by the value of parameter
  779. hname. The parameter iname is used to identify the iterator. There
  780. can be up to 4 iterators at the same time, with different name.
  781. </para>
  782. <para>
  783. Both parameters can be dynamic strings with variables.
  784. </para>
  785. <para>
  786. IMPORTANT: the slot of the hash table is left locked when
  787. retrieving in item. Therefore be sure you do not update the
  788. content of the hash table in between sht_iterator_start()
  789. and sht_iterator_end(), because it may end up in dead lock.
  790. </para>
  791. <para>
  792. This function can be used from ANY_ROUTE.
  793. </para>
  794. <example>
  795. <title><function>sht_iterator_start</function> usage</title>
  796. <programlisting format="linespecific">
  797. ...
  798. sht_iterator_start("i1", "h1");
  799. ...
  800. </programlisting>
  801. </example>
  802. </section>
  803. <section id="htable.f.sht_iterator_end">
  804. <title>
  805. <function moreinfo="none">sht_iterator_end(iname)</function>
  806. </title>
  807. <para>
  808. Close the iterator identified by iname parameter and release
  809. the hash table slot aquired by the iterator. The iname value
  810. must be the same used for sht_iterator_start().
  811. </para>
  812. <para>
  813. The parameter can be dynamic string with variables.
  814. </para>
  815. <para>
  816. This function can be used from ANY_ROUTE.
  817. </para>
  818. <example>
  819. <title><function>sht_iterator_end</function> usage</title>
  820. <programlisting format="linespecific">
  821. ...
  822. sht_iterator_end("i1");
  823. ...
  824. </programlisting>
  825. </example>
  826. </section>
  827. <section id="htable.f.sht_iterator_next">
  828. <title>
  829. <function moreinfo="none">sht_iterator_next(iname)</function>
  830. </title>
  831. <para>
  832. Move the iterator to the next item in hash table. It must
  833. be called also after sht_iterator_start() to get the first
  834. item in the hash table. Items are returned as they are found
  835. in the hash table slot, starting with the first slot.
  836. </para>
  837. <para>
  838. The return code is false when there is no (more) item in the
  839. hash table.
  840. </para>
  841. <para>
  842. The item name and value are accessible via variables:
  843. $shtitkey(iname) and $shtitval(iname).
  844. </para>
  845. <para>
  846. The parameter can be dynamic string with variables.
  847. </para>
  848. <para>
  849. This function can be used from ANY_ROUTE.
  850. </para>
  851. <example>
  852. <title><function>sht_iterator_next</function> usage</title>
  853. <programlisting format="linespecific">
  854. ...
  855. sht_iterator_start("i1", "h1");
  856. while(sht_iterator_next("i1")) {
  857. xlog("h1[$shtitkey(i1)] is: $shtitval(i1)\n");
  858. }
  859. sht_iterator_end("i1");
  860. ...
  861. </programlisting>
  862. </example>
  863. </section>
  864. </section>
  865. <section>
  866. <title>Exported pseudo-variables</title>
  867. <itemizedlist>
  868. <listitem><para>
  869. <emphasis>$sht(htable=>key)</emphasis>
  870. </para></listitem>
  871. <listitem><para>
  872. <emphasis>$shtex(htable=>key)</emphasis>
  873. </para></listitem>
  874. <listitem><para>
  875. <emphasis>$shtcn(htable=>key)</emphasis>
  876. </para></listitem>
  877. <listitem><para>
  878. <emphasis>$shtcv(htable=>key)</emphasis>
  879. </para></listitem>
  880. <listitem><para>
  881. <emphasis>$shtinc(htable=>key)</emphasis>
  882. </para></listitem>
  883. <listitem><para>
  884. <emphasis>$shtval(htable=>key)</emphasis>
  885. </para></listitem>
  886. <listitem><para>
  887. <emphasis>$shtrecord(attribute)</emphasis>
  888. </para></listitem>
  889. </itemizedlist>
  890. <para>
  891. Exported pseudo-variables are documented at &kamwikilink;.
  892. </para>
  893. </section>
  894. <section>
  895. <title>MI Commands</title>
  896. <section>
  897. <title>
  898. <function moreinfo="none">sht_reload</function>
  899. </title>
  900. <para>
  901. Reload a hash table from database.
  902. </para>
  903. <para>
  904. Name: <emphasis>sht_reload</emphasis>
  905. </para>
  906. <para>Parameters: <emphasis>_hash_table_name_</emphasis> - the name
  907. of hash table to reload.</para>
  908. <para>
  909. MI FIFO Command Format:
  910. </para>
  911. <programlisting format="linespecific">
  912. :sht_reload:_reply_fifo_file_
  913. _hash_table_name_
  914. _empty_line_
  915. </programlisting>
  916. </section>
  917. <section>
  918. <title>
  919. <function moreinfo="none">sht_dump</function>
  920. </title>
  921. <para>
  922. Dump content of a hash table via MI.
  923. </para>
  924. <para>
  925. Name: <emphasis>sht_dump</emphasis>
  926. </para>
  927. <para>Parameters: <emphasis>_hash_table_name_</emphasis> - the name
  928. of hash table to dump.</para>
  929. <para>
  930. MI FIFO Command Format:
  931. </para>
  932. <programlisting format="linespecific">
  933. :sht_dump:_reply_fifo_file_
  934. _hash_table_name_
  935. _empty_line_
  936. </programlisting>
  937. </section>
  938. <section>
  939. <title>
  940. <function moreinfo="none">sht_delete</function>
  941. </title>
  942. <para>
  943. Delete a key from a hash table via MI.
  944. </para>
  945. <para>
  946. Name: <emphasis>sht_delete</emphasis>
  947. </para>
  948. <para>Parameters:</para>
  949. <itemizedlist>
  950. <listitem><para><emphasis>_hash_table_name: </emphasis>The table name to delete the key from</para></listitem>
  951. <listitem><para><emphasis>_key_name: </emphasis>The key to delete from the htable</para></listitem>
  952. </itemizedlist>
  953. <para>
  954. MI FIFO Command Format:
  955. </para>
  956. <programlisting format="linespecific">
  957. :sht_delete:_reply_fifo_file_
  958. _hash_table_name_
  959. _key_name_
  960. _empty_line_
  961. </programlisting>
  962. <para>
  963. Example (note the quoting when executing it via FIFO):
  964. </para>
  965. <programlisting format="linespecific">
  966. kamctl fifo sht_delete auth '"[email protected]::last_auth"'
  967. </programlisting>
  968. </section>
  969. </section>
  970. <section>
  971. <title>Exported RPC Commands</title>
  972. <section>
  973. <title>
  974. <function moreinfo="none">htable.get htable key</function>
  975. </title>
  976. <para>
  977. Lists one value in a hash table
  978. </para>
  979. <para>
  980. Name: <emphasis>htable.get</emphasis>
  981. </para>
  982. <para>Parameters:</para>
  983. <itemizedlist>
  984. <listitem><para>htable : Name of the hash table to dump</para>
  985. </listitem>
  986. <listitem><para>key : Key name of the hash table value to dump</para>
  987. </listitem>
  988. </itemizedlist>
  989. <para>
  990. Example:
  991. </para>
  992. <programlisting format="linespecific">
  993. ...
  994. # Dump $sht(students=>daniel)
  995. kamcmd htable.get students daniel
  996. # Dump first entry in array key course $sht(students=>course[0])
  997. kamcmd htable.get students course[0]
  998. ...
  999. </programlisting>
  1000. </section>
  1001. <section>
  1002. <title>
  1003. <function moreinfo="none">htable.delete htable key</function>
  1004. </title>
  1005. <para>
  1006. Delete one value in a hash table
  1007. </para>
  1008. <para>
  1009. Name: <emphasis>htable.delete</emphasis>
  1010. </para>
  1011. <para>Parameters:</para>
  1012. <itemizedlist>
  1013. <listitem><para>htable : Name of the hash table to delete</para>
  1014. </listitem>
  1015. <listitem><para>key : Key name of the hash table value to delete</para>
  1016. </listitem>
  1017. </itemizedlist>
  1018. <para>
  1019. Example:
  1020. </para>
  1021. <programlisting format="linespecific">
  1022. ...
  1023. # Delete $sht(students=>anna)
  1024. kamcmd htable.delete students anna
  1025. # Delete first entry in array key course $sht(students=>course[0])
  1026. kamcmd htable.delete students course[0]
  1027. ...
  1028. </programlisting>
  1029. </section>
  1030. <section>
  1031. <title>
  1032. <function moreinfo="none">htable.sets htable key value</function>
  1033. </title>
  1034. <para>
  1035. Set an item in hash table to string value.
  1036. </para>
  1037. <para>
  1038. Name: <emphasis>htable.sets</emphasis>
  1039. </para>
  1040. <para>Parameters:</para>
  1041. <itemizedlist>
  1042. <listitem><para>htable : Name of the hash table</para>
  1043. </listitem>
  1044. <listitem><para>key : Key name in the hash table</para>
  1045. </listitem>
  1046. <listitem><para>Value : String value for the item</para>
  1047. </listitem>
  1048. </itemizedlist>
  1049. <para>
  1050. Example:
  1051. </para>
  1052. <programlisting format="linespecific">
  1053. ...
  1054. # Set $sht(test=>x) as string
  1055. kamcmd htable.sets test x abc
  1056. # Set firsti entry in array key x $sht(test=>x[0]) as string
  1057. kamcmd htable.sets test x[0] abc
  1058. ...
  1059. </programlisting>
  1060. </section>
  1061. <section>
  1062. <title>
  1063. <function moreinfo="none">htable.seti htable key value</function>
  1064. </title>
  1065. <para>
  1066. Set an item in hash table to integer value.
  1067. </para>
  1068. <para>
  1069. Name: <emphasis>htable.seti</emphasis>
  1070. </para>
  1071. <para>Parameters:</para>
  1072. <itemizedlist>
  1073. <listitem><para>htable : Name of the hash table</para>
  1074. </listitem>
  1075. <listitem><para>key : Key name in the hash table</para>
  1076. </listitem>
  1077. <listitem><para>Value : Integer value for the item</para>
  1078. </listitem>
  1079. </itemizedlist>
  1080. <para>
  1081. Example:
  1082. </para>
  1083. <programlisting format="linespecific">
  1084. ...
  1085. # Set $sht(test=>x) as integer
  1086. kamcmd htable.seti test x 123
  1087. # Set firsti entry in array key x $sht(test=>x[0]) as integer
  1088. kamcmd htable.sets test x[0] 123
  1089. ...
  1090. </programlisting>
  1091. </section>
  1092. <section>
  1093. <title>
  1094. <function moreinfo="none">htable.dump htable</function>
  1095. </title>
  1096. <para>
  1097. Lists all the values in a hash table
  1098. </para>
  1099. <para>
  1100. Name: <emphasis>dhtable.dump</emphasis>
  1101. </para>
  1102. <para>Parameters:</para>
  1103. <itemizedlist>
  1104. <listitem><para>htable : Name of the hash table to dump</para>
  1105. </listitem>
  1106. </itemizedlist>
  1107. <para>
  1108. Example:
  1109. </para>
  1110. <programlisting format="linespecific">
  1111. ...
  1112. kamcmd htable.dump ipban
  1113. ...
  1114. </programlisting>
  1115. </section>
  1116. <section>
  1117. <title>
  1118. <function moreinfo="none">htable.reload htable</function>
  1119. </title>
  1120. <para>
  1121. Reload hash table from database.
  1122. </para>
  1123. <para>
  1124. Name: <emphasis>dhtable.reload</emphasis>
  1125. </para>
  1126. <para>Parameters:</para>
  1127. <itemizedlist>
  1128. <listitem><para>htable : Name of the hash table to reload</para>
  1129. </listitem>
  1130. </itemizedlist>
  1131. <para>
  1132. Example:
  1133. </para>
  1134. <programlisting format="linespecific">
  1135. ...
  1136. kamcmd htable.reload ipban
  1137. ...
  1138. </programlisting>
  1139. </section>
  1140. <section>
  1141. <title>
  1142. <function moreinfo="none">htable.listTables</function>
  1143. </title>
  1144. <para>
  1145. Lists all defined tables
  1146. </para>
  1147. <para>
  1148. Name: <emphasis>htable.listTables</emphasis>
  1149. </para>
  1150. <para>Parameters:</para>
  1151. <itemizedlist>
  1152. <listitem><para>None</para>
  1153. </listitem>
  1154. </itemizedlist>
  1155. <para>
  1156. Example:
  1157. </para>
  1158. <programlisting format="linespecific">
  1159. ...
  1160. kamcmd htable.listTables
  1161. ...
  1162. </programlisting>
  1163. </section>
  1164. <section>
  1165. <title>
  1166. <function moreinfo="none">htable.stats</function>
  1167. </title>
  1168. <para>
  1169. Get statistics for hash tables - name, number of slots,
  1170. number of items, max number of items per slot, min number
  1171. of items per slot.
  1172. </para>
  1173. <para>
  1174. Name: <emphasis>htable.stats</emphasis>
  1175. </para>
  1176. <para>Parameters:</para>
  1177. <itemizedlist>
  1178. <listitem><para>None</para>
  1179. </listitem>
  1180. </itemizedlist>
  1181. <para>
  1182. Example:
  1183. </para>
  1184. <programlisting format="linespecific">
  1185. ...
  1186. kamcmd htable.stats
  1187. ...
  1188. </programlisting>
  1189. </section>
  1190. </section><!-- RPC commands -->
  1191. <section>
  1192. <title>Event routes</title>
  1193. <section>
  1194. <title>
  1195. <function moreinfo="none">htable:mod-init</function>
  1196. </title>
  1197. <para>
  1198. When defined, the module calls event_route[htable:mod-init] after
  1199. all modules have been initialized. A typical use case is to
  1200. initialise items in hash tables. The event route is executed only
  1201. once, after core and module initialization, but before &kamailio; forks any
  1202. child processes.
  1203. </para>
  1204. <programlisting format="linespecific">
  1205. ...
  1206. event_route[htable:mod-init] {
  1207. $sht(a=>x) = 1;
  1208. }
  1209. ...
  1210. </programlisting>
  1211. </section>
  1212. <section>
  1213. <title>
  1214. <function moreinfo="none">htable:expired:&lt;table&gt;</function>
  1215. </title>
  1216. <para>
  1217. When defined, the module calls event_route[htable:expired:&lt;table&gt;]
  1218. when an entry in the given table expires. In this event route, the key and value
  1219. of the expired record are available with the $shtrecord(key) and $shtrecord(value)
  1220. pseudo-variables.
  1221. </para>
  1222. <programlisting format="linespecific">
  1223. ...
  1224. event_route[htable:expired:mytable] {
  1225. xlog("mytable record expired $shtrecord(key) => $shtrecord(value)\n");
  1226. }
  1227. ...
  1228. </programlisting>
  1229. </section>
  1230. </section>
  1231. </chapter>