Debian Codegraph @

src/modules/htable/README

raw
1 HTable Module
2
3 Elena-Ramona Modroiu
4
5 asipto.com
6 <[email protected]>
7
8 Edited by
9
10 Elena-Ramona Modroiu
11
12 <[email protected]>
13
14 Alex Balashov
15
16 <[email protected]>
17
18 Ovidiu Sas
19
20 <[email protected]>
21
22 Tyler Moore
23
24 <[email protected]>
25
26 Copyright © 2008-2011 http://www.asipto.com
27
28 Copyright © 2023-2025 Tyler Moore, dOpenSource
29 __________________________________________________________________
30
31 Table of Contents
32
33 1. Admin Guide
34
35 1. Overview
36 2. Dependencies
37
38 2.1. Kamailio Modules
39 2.2. External Libraries or Applications
40 2.3. Loading from database
41
42 3. Parameters
43
44 3.1. htable (str)
45 3.2. db_url (str)
46 3.3. key_name_column (str)
47 3.4. key_type_column (str)
48 3.5. value_type_column (str)
49 3.6. key_value_column (str)
50 3.7. expires_column (str)
51 3.8. array_size_suffix (str)
52 3.9. fetch_rows (integer)
53 3.10. timer_interval (integer)
54 3.11. db_expires (integer)
55 3.12. enable_dmq (integer)
56 3.13. dmq_init_sync (integer)
57 3.14. timer_procs (integer)
58 3.15. event_callback (str)
59 3.16. event_callback_mode (int)
60
61 4. Functions
62
63 4.1. sht_print()
64 4.2. sht_rm(htname, itname)
65 4.3. sht_rm_name_re(htable=>regexp)
66 4.4. sht_rm_value_re(htable=>regexp)
67 4.5. sht_rm_name(htable, op, val)
68 4.6. sht_rm_value(htable, op, val)
69 4.7. sht_setxs(htname, itname, itval, exval)
70 4.8. sht_setxi(htname, itname, itval, exval)
71 4.9. sht_reset(htable)
72 4.10. sht_lock(htable=>key)
73 4.11. sht_unlock(htable=>key)
74 4.12. sht_iterator_start(iname, hname)
75 4.13. sht_iterator_end(iname)
76 4.14. sht_iterator_next(iname)
77 4.15. sht_iterator_rm(iname)
78 4.16. sht_iterator_sets(iname, sval)
79 4.17. sht_iterator_seti(iname, ival)
80 4.18. sht_iterator_setex(iname, exval)
81 4.19. sht_match_name(htable, op, mval)
82 4.20. sht_has_name(htable, op, mval)
83 4.21. sht_match_str_value(htable, op, mval)
84 4.22. sht_has_str_value(htable, op, mval)
85 4.23. sht_is_null(htname, itname)
86 4.24. sht_inc(htname, itname)
87 4.25. sht_dec(htname, itname)
88
89 5. Exported pseudo-variables
90 6. RPC Commands
91
92 6.1. htable.get htable key
93 6.2. htable.delete htable key
94 6.3. htable.sets htable key value
95 6.4. htable.seti htable key value
96 6.5. htable.setex htable key expire
97 6.6. htable.setxs htable key value expire
98 6.7. htable.setxi htable key value expire
99 6.8. htable.dump htable
100 6.9. htable.reload htable
101 6.10. htable.store htable
102 6.11. htable.flush htable
103 6.12. htable.listTables
104 6.13. htable.stats
105 6.14. htable.dmqsync [htable]
106 6.15. htable.dmqresync htable
107
108 7. Event routes
109
110 7.1. htable:mod-init
111 7.2. htable:expired:<table>
112
113 List of Examples
114
115 1.1. Accessing $sht(htname=>key)
116 1.2. Dictionary attack limitation
117 1.3. Storing array values
118 1.4. Set htable parameter
119 1.5. Set db_url parameter
120 1.6. Set key_name_column parameter
121 1.7. Set key_type_column parameter
122 1.8. Set value_type_column parameter
123 1.9. Set key_value_column parameter
124 1.10. Set expires_column parameter
125 1.11. Set array_size_suffix parameter
126 1.12. Set fetch_rows parameter
127 1.13. Set timer_interval parameter
128 1.14. Set db_expires parameter
129 1.15. Set enable_dmq parameter
130 1.16. Set dmq_init_sync parameter
131 1.17. Set timer_procs parameter
132 1.18. Set event_callback parameter
133 1.19. Set event_callback_mode parameter
134 1.20. sht_print usage
135 1.21. sht_rm usage
136 1.22. sht_rm_name_re usage
137 1.23. sht_rm_value_re usage
138 1.24. sht_rm_name usage
139 1.25. sht_rm_value usage
140 1.26. sht_setxs usage
141 1.27. sht_setxi usage
142 1.28. sht_reset usage
143 1.29. sht_lock usage
144 1.30. sht_unlock usage
145 1.31. sht_iterator_start usage
146 1.32. sht_iterator_end usage
147 1.33. sht_iterator_next usage
148 1.34. sht_iterator_rm usage
149 1.35. sht_iterator_sets usage
150 1.36. sht_iterator_seti usage
151 1.37. sht_iterator_setex usage
152 1.38. sht_match_name usage
153 1.39. sht_match_name usage
154 1.40. sht_is_null usage
155 1.41. sht_inc usage
156 1.42. sht_dec usage
157
158 Chapter 1. Admin Guide
159
160 Table of Contents
161
162 1. Overview
163 2. Dependencies
164
165 2.1. Kamailio Modules
166 2.2. External Libraries or Applications
167 2.3. Loading from database
168
169 3. Parameters
170
171 3.1. htable (str)
172 3.2. db_url (str)
173 3.3. key_name_column (str)
174 3.4. key_type_column (str)
175 3.5. value_type_column (str)
176 3.6. key_value_column (str)
177 3.7. expires_column (str)
178 3.8. array_size_suffix (str)
179 3.9. fetch_rows (integer)
180 3.10. timer_interval (integer)
181 3.11. db_expires (integer)
182 3.12. enable_dmq (integer)
183 3.13. dmq_init_sync (integer)
184 3.14. timer_procs (integer)
185 3.15. event_callback (str)
186 3.16. event_callback_mode (int)
187
188 4. Functions
189
190 4.1. sht_print()
191 4.2. sht_rm(htname, itname)
192 4.3. sht_rm_name_re(htable=>regexp)
193 4.4. sht_rm_value_re(htable=>regexp)
194 4.5. sht_rm_name(htable, op, val)
195 4.6. sht_rm_value(htable, op, val)
196 4.7. sht_setxs(htname, itname, itval, exval)
197 4.8. sht_setxi(htname, itname, itval, exval)
198 4.9. sht_reset(htable)
199 4.10. sht_lock(htable=>key)
200 4.11. sht_unlock(htable=>key)
201 4.12. sht_iterator_start(iname, hname)
202 4.13. sht_iterator_end(iname)
203 4.14. sht_iterator_next(iname)
204 4.15. sht_iterator_rm(iname)
205 4.16. sht_iterator_sets(iname, sval)
206 4.17. sht_iterator_seti(iname, ival)
207 4.18. sht_iterator_setex(iname, exval)
208 4.19. sht_match_name(htable, op, mval)
209 4.20. sht_has_name(htable, op, mval)
210 4.21. sht_match_str_value(htable, op, mval)
211 4.22. sht_has_str_value(htable, op, mval)
212 4.23. sht_is_null(htname, itname)
213 4.24. sht_inc(htname, itname)
214 4.25. sht_dec(htname, itname)
215
216 5. Exported pseudo-variables
217 6. RPC Commands
218
219 6.1. htable.get htable key
220 6.2. htable.delete htable key
221 6.3. htable.sets htable key value
222 6.4. htable.seti htable key value
223 6.5. htable.setex htable key expire
224 6.6. htable.setxs htable key value expire
225 6.7. htable.setxi htable key value expire
226 6.8. htable.dump htable
227 6.9. htable.reload htable
228 6.10. htable.store htable
229 6.11. htable.flush htable
230 6.12. htable.listTables
231 6.13. htable.stats
232 6.14. htable.dmqsync [htable]
233 6.15. htable.dmqresync htable
234
235 7. Event routes
236
237 7.1. htable:mod-init
238 7.2. htable:expired:<table>
239
240 1. Overview
241
242 The module adds a hash table container to the configuration language.
243 The hash table is stored in shared memory and the access to it can be
244 done via pseudo-variables: $sht(htname=>name). The module supports
245 definition of many hash tables and can load values at startup from a
246 database table.
247
248 A typical use case for the SIP server is to implement a cache system in
249 configuration file - if a value is not found in hash table, load it
250 from database and store it in hash table so next time the access to it
251 is very fast. In the definition of the table you can define the default
252 expiration time of cached items. The expiration time can be adjusted
253 per item via assignment operation at runtime.
254
255 Replication between multiple servers is performed automatically (if
256 enabled) via the DMQ module.
257
258 You can read more about hash tables at:
259 http://en.wikipedia.org/wiki/Hash_table.
260
261 The “name” can be a static string or can include pseudo- variables that
262 will be replaced at runtime.
263
264 Example 1.1. Accessing $sht(htname=>key)
265 ...
266 modparam("htable", "htable", "a=>size=8;")
267 ...
268 $sht(a=>test) = 1;
269 $sht(a=>$ci::srcip) = $si;
270 ...
271
272 The next example shows a way to protect against dictionary attacks. If
273 someone fails to authenticate 3 times, it is forbidden for 15 minutes.
274 Authentication against database is expensive as it does a select on the
275 “subscriber” table. By disabling the DB auth for 15 minutes, resources
276 on the server are saved and time to discover the password is increased
277 substantially. Additional alerting can be done by writing a message to
278 syslog or sending email, etc.
279
280 To implement the logic, two hash table variables are used: one counting
281 the failed authentications per user and one for storing the time of the
282 last authentication attempt. To ensure a unique name per user, the hash
283 table uses a combination of authentication username and text
284 “::auth_count” and “::last_auth”.
285
286 Example 1.2. Dictionary attack limitation
287 ...
288 modparam("htable", "htable", "a=>size=8;")
289 ...
290 if(is_present_hf("Authorization"))
291 {
292 if($sht(a=>$au::auth_count)==3)
293 {
294 $var(exp) = $Ts - 900;
295 if($sht(a=>$au::last_auth) > $var(exp))
296 {
297 sl_send_reply("403", "Try later");
298 exit;
299 } else {
300 $sht(a=>$au::auth_count) = 0;
301 }
302 }
303 if(!www_authenticate("$td", "subscriber"))
304 {
305 switch ($retcode) {
306 case -1:
307 sl_send_reply("403", "Forbidden");
308 exit;
309 case -2:
310 if($sht(a=>$au::auth_count) == $null)
311 $sht(a=>$au::auth_count) = 0;
312 $sht(a=>$au::auth_count) = $sht(a=>$au::auth_count) + 1;
313 if($sht(a=>$au::auth_count) == 3)
314 xlog("auth failed 3rd time - src ip: $si\n");
315 $sht(a=>$au::last_auth) = $Ts;
316 break;
317 }
318 www_challenge("$td"/*realm*/,"0"/*qop*/);
319 exit;
320 }
321 $sht(a=>$au::auth_count) = 0;
322 } else {
323 www_challenge("$td","0");
324 exit;
325 }
326 ...
327
328 The module also provides a way to store multiple values for a single
329 key. This is emulated by storing individual keys as 'key_name[n]',
330 where n is incremented for each key. The total number of keys is stored
331 in a dedicated key, by default: 'key_name::size'.
332
333 The array is built when the table is loaded in memory and afterwards
334 all the keys are treated as individual keys. If a particular entry in
335 the array is deleted, it is the administrator's responsibility to
336 update the size of the array and any other elements (if required).
337
338 Example 1.3. Storing array values
339 # Example of dbtext with multiple keys
340 $ cat /usr/local/etc/kamailio/dbtext/htable
341 1:key:1:0:value3:0
342 2:key:1:0:value2:0
343 3:key:1:0:value1:0
344
345 # The array key will be loaded in memory in the following format:
346 $ kamctl rpc htable.dump htable
347 {
348 entry: 35
349 size: 1
350 slot: {
351 item: {
352 name: key[0]
353 value: value1
354 }
355 }
356 }
357 {
358 entry: 50
359 size: 1
360 slot: {
361 item: {
362 name: key::size
363 value: 3
364 }
365 }
366 }
367 {
368 entry: 67
369 size: 1
370 slot: {
371 item: {
372 name: key[1]
373 value: value2
374 }
375 }
376 }
377 {
378 entry: 227
379 size: 1
380 slot: {
381 item: {
382 name: key[2]
383 value: value3
384 }
385 }
386 }
387
388 # Now let's delete a particular entry in the array: key[0].
389 $ kamctl rpc htable.delete htable key[0]
390
391 # The array key will look like this after a key was deleted:
392 $ kamctl rpc htable.dump htable
393 {
394 entry: 50
395 size: 1
396 slot: {
397 item: {
398 name: key::size
399 value: 3
400 }
401 }
402 }
403 {
404 entry: 67
405 size: 1
406 slot: {
407 item: {
408 name: key[1]
409 value: value2
410 }
411 }
412 }
413 {
414 entry: 227
415 size: 1
416 slot: {
417 item: {
418 name: key[2]
419 value: value3
420 }
421 }
422 }
423
424 2. Dependencies
425
426 2.1. Kamailio Modules
427 2.2. External Libraries or Applications
428 2.3. Loading from database
429
430 2.1. Kamailio Modules
431
432 The following modules must be loaded before this module:
433 * If DMQ replication is enabled, the DMQ module must be loaded
434 first..
435
436 2.2. External Libraries or Applications
437
438 The following libraries or applications must be installed before
439 running Kamailio with this module loaded:
440 * None.
441
442 2.3. Loading from database
443
444 The module is able to load values in a hash table at startup upon
445 providing a DB URL and table name.
446
447 The structure of the table must contain:
448 * key name - string containing the name of the key.
449 * key type - the type of the key
450 + 0 - simple key - the key is added as 'key_name'.
451 + 1 - array key - the key is added as 'key_name[n]' - n is
452 incremented for each key with this name to build an array in
453 hash table. In addition, an additional key is built to hold
454 the total number of key in the array, by default
455 key_name::size (see array_size_suffix parameter).
456 * value type - the type of the key value
457 + 0 - value is string.
458 + 1 - value is integer.
459 * key value - string containing the value of the key.
460
461 3. Parameters
462
463 3.1. htable (str)
464 3.2. db_url (str)
465 3.3. key_name_column (str)
466 3.4. key_type_column (str)
467 3.5. value_type_column (str)
468 3.6. key_value_column (str)
469 3.7. expires_column (str)
470 3.8. array_size_suffix (str)
471 3.9. fetch_rows (integer)
472 3.10. timer_interval (integer)
473 3.11. db_expires (integer)
474 3.12. enable_dmq (integer)
475 3.13. dmq_init_sync (integer)
476 3.14. timer_procs (integer)
477 3.15. event_callback (str)
478 3.16. event_callback_mode (int)
479
480 3.1. htable (str)
481
482 The definition of a hash table. The value of the parameter may have the
483 following format:
484 * "htname=>size=_number_;autoexpire=_number_;dbtable=_string_"
485
486 The parameter can be set multiple times to get more hash tables in same
487 configuration file.
488 * htname - string specifying the name of the hash table. This string
489 is used by $sht(...) to refer to the hash table.
490 * size - number to control how many slots (buckets) to create for the
491 hash table. Larger value means more slots with higher probability
492 for less collisions. The actual number slots (or buckets) created
493 for the table is 2^size. The possible range for this value is from
494 2 to 31, smaller or larger values will be increased to 3 (8 slots)
495 or decreased to 14 (16384 slots). Note that each slot can store
496 more than one item, when there are collisions of hash ids computed
497 for keys. The items in the same slot are stored in a linked list.
498 In other words, the size is not setting a limit of how many items
499 can be stored in a hash table, as long as there is enough free
500 shared memory, new items can be added.
501 * autoexpire -time in seconds to delete an item from a hash table if
502 no update was done to it. If it is missing or set to 0, the items
503 won't expire.
504 * dbtable - name of database to be loaded at startup in hash table.
505 If empty or missing, no data will be loaded.
506 * cols - the column names of the database table. They must be
507 enclosed in quotes in order to form a valid SIP parameter value and
508 be separated by comma. The first column corresponds to key_name.
509 When specified, there must be at least two columns. If this
510 attribute is not specified, then the global module parameters for
511 column names are used. If more than one value columns are
512 specified, the hash table will pack the column values in a comma
513 separated string, which will be associated with the key (string
514 transformation {s.select,...} can be used in configuration file to
515 extract a specific column value). When cols attribute is present,
516 writing back to database table is disabled.
517 * dbmode - if set to 1, the content of hash table is written to
518 database table when the SIP server is stopped (i.e., ensure
519 persistency over restarts). Default value is 0 (no write back to db
520 table).
521 * initval - the integer value to be returned instead of $null when a
522 requested key is not set.
523 * updateexpire - if set to 1 (default), the time until expiration of
524 an item is reset when that item is updated. Certain uses of htable
525 may dictate that updates should not reset the expiration timeout,
526 however, in which case this attribute can be set to 0.
527 * dmqreplicate - if set to 1, any actions (set, update, delete etc.)
528 performed upon entries in this table will be replicated to other
529 nodes (htable peers). Please note, module parameter “enable_dmq”
530 must also be set in order for this to apply (see below). Default is
531 0 (no replication).
532 * coldelim - the character delimeter to use when packing the htable.
533 When set, this parameter changes the column delimeter between
534 columns in a multiple column hash table. This can be useful when
535 loading JSON data types into a hash table as they conflict with the
536 default “,” delimeter, allowing these values to be parsed in the
537 routing config. See the example below on parsing a JSON array from
538 an htable with “;” as the column delimeter.
539 ...
540 modparam("htable", "htable", "customer=>size=8;dbtable=customer;cols='dids,descr
541 iption';coldelim=';'")
542 ...
543 $avp(customer) = $sht(customer=>1);
544 $var(customer_dids) = $(avp(customer){s.select,0,;});
545 $var(customer_desc) = $(avp(customer){s.select,1,;});
546 ...
547 * colnull - the character to use when packing a NULL value into the
548 htable from the database. This parameter can be set to the empty
549 string or a single character. This can be used to simplify checking
550 a single column in a row for emptiness, in the routing config. The
551 example below shows how one would do that.
552 ...
553 modparam("htable", "htable", "customer=>size=8;dbtable=customer;cols='name,descr
554 iption';colnull=''")
555 ...
556 $avp(customer) = $sht(customer=>1);
557 $var(customer_name) = $(avp(customer){s.select,0,;});
558 if (!strempty($var(customer_name))) {
559 ...
560 }
561
562 Default value is NULL.
563
564 Example 1.4. Set htable parameter
565 ...
566 modparam("htable", "htable", "a=>size=4;autoexpire=7200;dbtable=htable_a;")
567 modparam("htable", "htable", "b=>size=5;")
568 modparam("htable", "htable", "c=>size=4;autoexpire=7200;initval=1;dmqreplicate=1
569 ;")
570 ...
571
572 3.2. db_url (str)
573
574 The URL to connect to database for loading values in hash table at
575 start up.
576
577 Default value is NULL (do not connect).
578
579 Example 1.5. Set db_url parameter
580 ...
581 modparam("htable", "db_url", "mysql://kamailio:kamailiorw@localhost/kamailio")
582 ...
583
584 3.3. key_name_column (str)
585
586 The name of the column containing the hash table key name.
587
588 Default value is 'key_name'.
589
590 Example 1.6. Set key_name_column parameter
591 ...
592 modparam("htable", "key_name_column", "kname")
593 ...
594
595 3.4. key_type_column (str)
596
597 The name of the column containing the hash table key type.
598
599 Default value is 'key_type'.
600
601 Example 1.7. Set key_type_column parameter
602 ...
603 modparam("htable", "key_type_column", "ktype")
604 ...
605
606 3.5. value_type_column (str)
607
608 The name of the column containing the hash table value type.
609
610 Default value is 'value_type'.
611
612 Example 1.8. Set value_type_column parameter
613 ...
614 modparam("htable", "value_type_column", "vtype")
615 ...
616
617 3.6. key_value_column (str)
618
619 The name of the column containing hash table key value.
620
621 Default value is 'key_value'.
622
623 Example 1.9. Set key_value_column parameter
624 ...
625 modparam("htable", "key_value_column", "kvalue")
626 ...
627
628 3.7. expires_column (str)
629
630 The name of the column containing the expires value.
631
632 Default value is 'expires'.
633
634 Example 1.10. Set expires_column parameter
635 ...
636 modparam("htable", "expires_column", "expiry")
637 ...
638
639 3.8. array_size_suffix (str)
640
641 The suffix to be added to store the number of items in an array (see
642 key type).
643
644 Default value is '::size'.
645
646 Example 1.11. Set array_size_suffix parameter
647 ...
648 modparam("htable", "array_size_suffix", "-count")
649 ...
650
651 3.9. fetch_rows (integer)
652
653 How many rows to fetch at once from database.
654
655 Default value is 100.
656
657 Example 1.12. Set fetch_rows parameter
658 ...
659 modparam("htable", "fetch_rows", 1000)
660 ...
661
662 3.10. timer_interval (integer)
663
664 Interval in seconds to check for expired htable values.
665
666 Default value is 20.
667
668 Example 1.13. Set timer_interval parameter
669 ...
670 modparam("htable", "timer_interval", 10)
671 ...
672
673 3.11. db_expires (integer)
674
675 If set to 1, the module loads/saves the value for expire of the items
676 in hash table from/to database. It applies only to hash tables that
677 have the auto-expires attribute defined. If set to 0, only the key name
678 and the value are loaded, the expires for each item being set to 0.
679
680 Note that the module is not reloading automatically the items from
681 database when they expire, the reloading can be done only via RPC
682 command.
683
684 Default value is 0.
685
686 Example 1.14. Set db_expires parameter
687 ...
688 modparam("htable", "db_expires", 1)
689 ...
690
691 3.12. enable_dmq (integer)
692
693 If set to 1, will enable DMQ replication of actions performed upon
694 entries in all tables having "dmqreplicate" parameter set. Any update
695 action performed via pseudo-variables and RPC commands will be repeated
696 on all other nodes. Therefore, it is important to ensure the table
697 definition (size, autoexpire etc.) is identical across all instances.
698
699 Important: If this parameter is enabled, the DMQ module must be loaded
700 first - otherwise, startup will fail.
701
702 Currently, values are not replicated on load from DB as it is expected
703 that in these cases, all servers will load their values from the same
704 DB.
705
706 Default value is 0.
707
708 Example 1.15. Set enable_dmq parameter
709 ...
710 modparam("htable", "enable_dmq", 1)
711 ...
712
713 3.13. dmq_init_sync (integer)
714
715 If set to 1, will request synchronization from other nodes at startup.
716 It applies to all tables having the "dmqreplicate" parameter set. As
717 above, it is important to ensure the definition (size, autoexpire etc.)
718 of replicated tables is identical across all instances.
719
720 Default value is 0.
721
722 Example 1.16. Set dmq_init_sync parameter
723 ...
724 modparam("htable", "dmq_init_sync", 1)
725 ...
726
727 3.14. timer_procs (integer)
728
729 If set to 1 or greater, the module will create its own timer processes
730 to scan for expired items in hash tables. If set to zero, it will use
731 the core timer for this task. Set it to 1 if you store a lot of items
732 with autoexpire property.
733
734 Default value is 0.
735
736 Example 1.17. Set timer_procs parameter
737 ...
738 modparam("htable", "timer_procs", 4)
739 ...
740
741 3.15. event_callback (str)
742
743 The name of the function in the kemi configuration file (embedded
744 scripting language such as Lua, Python, ...) to be executed instead of
745 event_route[...] blocks.
746
747 The function receives a string parameter with the name of the event,
748 the values can be: 'htable:mod-init', 'htable:expired:htname' ('htname'
749 being the name of hash table).
750
751 Default value is 'empty' (no function is executed for events).
752
753 Example 1.18. Set event_callback parameter
754 ...
755 modparam("htable", "event_callback", "ksr_htable_event")
756 ...
757 -- event callback function implemented in Lua
758 function ksr_htable_event(evname)
759 KSR.info("===== htable module triggered event: " .. evname .. "\n");
760 return 1;
761 end
762 ...
763
764 3.16. event_callback_mode (int)
765
766 Control when event_route[htable:init] is executed: 0 - after all
767 modules were initialized; 1 - in first worker process.
768
769 Set it to 1 if used in a KEMI script or when needing to use database
770 (e.g., via sqlops) inside event_route[htable:init].
771
772 Default value is 0.
773
774 Example 1.19. Set event_callback_mode parameter
775 ...
776 modparam("htable", "event_callback_mode", 1)
777 ...
778
779 4. Functions
780
781 4.1. sht_print()
782 4.2. sht_rm(htname, itname)
783 4.3. sht_rm_name_re(htable=>regexp)
784 4.4. sht_rm_value_re(htable=>regexp)
785 4.5. sht_rm_name(htable, op, val)
786 4.6. sht_rm_value(htable, op, val)
787 4.7. sht_setxs(htname, itname, itval, exval)
788 4.8. sht_setxi(htname, itname, itval, exval)
789 4.9. sht_reset(htable)
790 4.10. sht_lock(htable=>key)
791 4.11. sht_unlock(htable=>key)
792 4.12. sht_iterator_start(iname, hname)
793 4.13. sht_iterator_end(iname)
794 4.14. sht_iterator_next(iname)
795 4.15. sht_iterator_rm(iname)
796 4.16. sht_iterator_sets(iname, sval)
797 4.17. sht_iterator_seti(iname, ival)
798 4.18. sht_iterator_setex(iname, exval)
799 4.19. sht_match_name(htable, op, mval)
800 4.20. sht_has_name(htable, op, mval)
801 4.21. sht_match_str_value(htable, op, mval)
802 4.22. sht_has_str_value(htable, op, mval)
803 4.23. sht_is_null(htname, itname)
804 4.24. sht_inc(htname, itname)
805 4.25. sht_dec(htname, itname)
806
807 4.1. sht_print()
808
809 Dump content of hash table to L_ERR log level. Intended for debug
810 purposes.
811
812 This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
813 ONREPLY_ROUTE, BRANCH_ROUTE.
814
815 Example 1.20. sht_print usage
816 ...
817 sht_print();
818 ...
819
820 4.2. sht_rm(htname, itname)
821
822 Delete the item with the name 'itname' from hash table 'htname'. This
823 API function is equivalent to '$sht(htname=>itname) = $null'.
824
825 This function can be used from ANY_ROUTE.
826
827 Example 1.21. sht_rm usage
828 ...
829 sht_rm("ha", "test"");
830 ...
831
832 4.3. sht_rm_name_re(htable=>regexp)
833
834 Delete all entries in the htable that match the name against regular
835 expression.
836
837 This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
838 ONREPLY_ROUTE, BRANCH_ROUTE.
839
840 Example 1.22. sht_rm_name_re usage
841 ...
842 sht_rm_name_re("ha=>.*");
843 ...
844
845 4.4. sht_rm_value_re(htable=>regexp)
846
847 Delete all entries in the htable that match the value against regular
848 expression.
849
850 This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
851 ONREPLY_ROUTE, BRANCH_ROUTE.
852
853 Example 1.23. sht_rm_value_re usage
854 ...
855 sht_rm_value_re("ha=>.*");
856 ...
857
858 4.5. sht_rm_name(htable, op, val)
859
860 Delete all entries in the htable that match the name against the val
861 parameter.
862
863 The op parameter can be:
864 * re - match the val parameter as regular expression.
865 * sw - match the val parameter as 'starts with'.
866 * ew - match the val parameter as 'ends with'.
867 * in - match the val parameter as 'includes'.
868
869 All parameters can be static strings or contain variables.
870
871 This function can be used from ANY_ROUTE.
872
873 Example 1.24. sht_rm_name usage
874 ...
875 sht_rm_name("ha", "re", ".*");
876 ...
877
878 4.6. sht_rm_value(htable, op, val)
879
880 Delete all entries in the htable that match the value against the val
881 parameter.
882
883 The op parameter can be:
884 * re - match the val parameter as regular expression.
885 * sw - match the val parameter as 'starts with'.
886 * ew - match the val parameter as 'ends with'.
887 * in - match the val parameter as 'includes'.
888
889 All parameters can be static strings or contain variables.
890
891 This function can be used from ANY_ROUTE.
892
893 Example 1.25. sht_rm_value usage
894 ...
895 sht_rm_value("ha", "re", ".*");
896 ...
897
898 4.7. sht_setxs(htname, itname, itval, exval)
899
900 Set the item with the name 'itname' from hash table 'htname' to string
901 value 'itval' and expire property to 'exval'.
902
903 The parameters can be with variables.
904
905 This function can be used from ANY_ROUTE.
906
907 Example 1.26. sht_setxs usage
908 ...
909 sht_setxs("ha", "test", "abc", "10");
910 ...
911
912 4.8. sht_setxi(htname, itname, itval, exval)
913
914 Set the item with the name 'itname' from hash table 'htname' to integer
915 value 'itval' and expire property to 'exval'.
916
917 The parameters can be with variables.
918
919 This function can be used from ANY_ROUTE.
920
921 Example 1.27. sht_setxi usage
922 ...
923 sht_setxs("ha", "test", "100", "10");
924 ...
925
926 4.9. sht_reset(htable)
927
928 Delete all entries in the htable. The name of the hash table can be a
929 dynamic string with variables.
930
931 This function can be used from ANY_ROUTE.
932
933 Example 1.28. sht_reset usage
934 ...
935 sht_reset("ha$var(x)");
936 ...
937
938 4.10. sht_lock(htable=>key)
939
940 Lock the slot in htable corresponding to the key item. Note that the
941 locking is re-entrant for the process, therefore the lock and unlock
942 should be done by the same process.
943
944 This function can be used from ANY_ROUTE.
945
946 Example 1.29. sht_lock usage
947 ...
948 sht_lock("ha=>test");
949 ...
950
951 4.11. sht_unlock(htable=>key)
952
953 Unlock the slot in htable corresponding to the key item. Note that the
954 locking is re-entrant for the process, therefore the lock and unlock
955 should be done by the same process.
956
957 This function can be used from ANY_ROUTE.
958
959 Example 1.30. sht_unlock usage
960 ...
961 sht_lock("ha=>test");
962 $sht(ha=>test) = $sht(ha=>test) + 10;
963 sht_unlock("ha=>test");
964 ...
965
966 4.12. sht_iterator_start(iname, hname)
967
968 Start an iterator for hash table named by the value of parameter hname.
969 The parameter iname is used to identify the iterator. There can be up
970 to 4 iterators at the same time, with different name.
971
972 Both parameters can be dynamic strings with variables.
973
974 IMPORTANT: the slot of the hash table is left locked when retrieving in
975 item. Therefore be sure you do not update the content of the hash table
976 in between sht_iterator_start() and sht_iterator_end(), because it may
977 end up in dead lock.
978
979 This function can be used from ANY_ROUTE.
980
981 Example 1.31. sht_iterator_start usage
982 ...
983 sht_iterator_start("i1", "h1");
984 ...
985
986 4.13. sht_iterator_end(iname)
987
988 Close the iterator identified by iname parameter and release the hash
989 table slot acquired by the iterator. The iname value must be the same
990 used for sht_iterator_start().
991
992 The parameter can be dynamic string with variables.
993
994 This function can be used from ANY_ROUTE.
995
996 Example 1.32. sht_iterator_end usage
997 ...
998 sht_iterator_end("i1");
999 ...
1000
1001 4.14. sht_iterator_next(iname)
1002
1003 Move the iterator to the next item in hash table. It must be called
1004 also after sht_iterator_start() to get the first item in the hash
1005 table. Items are returned as they are found in the hash table slot,
1006 starting with the first slot.
1007
1008 The return code is false when there is no (more) item in the hash
1009 table.
1010
1011 The item name and value are accessible via variables: $shtitkey(iname)
1012 and $shtitval(iname).
1013
1014 The parameter can be dynamic string with variables.
1015
1016 This function can be used from ANY_ROUTE.
1017
1018 Example 1.33. sht_iterator_next usage
1019 ...
1020 sht_iterator_start("i1", "h1");
1021 while(sht_iterator_next("i1")) {
1022 xlog("h1[$shtitkey(i1)] is: $shtitval(i1)\n");
1023 }
1024 sht_iterator_end("i1");
1025 ...
1026
1027 4.15. sht_iterator_rm(iname)
1028
1029 Remove the current item in the iterator and move the iterator to the
1030 next one.
1031
1032 The return code is 1 (true) if the item was removed and next item
1033 exists; -2 (false) if the item was removed and there is no next item
1034 (end of items); other negative value (false) can be returned on error
1035 (e.g., iterator or item not found).
1036
1037 The parameter can be dynamic string with variables.
1038
1039 This function can be used from ANY_ROUTE.
1040
1041 Example 1.34. sht_iterator_rm usage
1042 ...
1043 sht_iterator_start("i1", "h1");
1044 while(sht_iterator_next("i1")) {
1045 while($shtitkey(i1) =~ "xyz" and sht_iterator_rm("i1")) {
1046 xdbg("item removed\n");
1047 }
1048 }
1049 sht_iterator_end("i1");
1050 ...
1051
1052 4.16. sht_iterator_sets(iname, sval)
1053
1054 Set the value of the current item to the string in the sval.
1055
1056 The parameters can be dynamic strings with variables.
1057
1058 This function can be used from ANY_ROUTE.
1059
1060 Example 1.35. sht_iterator_sets usage
1061 ...
1062 sht_iterator_start("i1", "h1");
1063 sht_iterator_next("i1");
1064 sht_iterator_sets("i1", "$ci");
1065 sht_iterator_end("i1");
1066 ...
1067
1068 4.17. sht_iterator_seti(iname, ival)
1069
1070 Set the value of the current item to the integer in the ival.
1071
1072 The parameters can be dynamic strings or integers with variables.
1073
1074 This function can be used from ANY_ROUTE.
1075
1076 Example 1.36. sht_iterator_seti usage
1077 ...
1078 sht_iterator_start("i1", "h1");
1079 sht_iterator_next("i1");
1080 sht_iterator_seti("i1", "20");
1081 sht_iterator_end("i1");
1082 ...
1083
1084 4.18. sht_iterator_setex(iname, exval)
1085
1086 Set the expire of the current item to the integer in the exval.
1087
1088 The parameters can be dynamic strings or integers with variables.
1089
1090 This function can be used from ANY_ROUTE.
1091
1092 Example 1.37. sht_iterator_setex usage
1093 ...
1094 sht_iterator_start("i1", "h1");
1095 sht_iterator_next("i1");
1096 sht_iterator_setex("i1", "120");
1097 sht_iterator_end("i1");
1098 ...
1099
1100 4.19. sht_match_name(htable, op, mval)
1101
1102 Return greater than 0 (true) if the htable has an item that matches the
1103 name against the mval parameter.
1104
1105 The op parameter can be:
1106 * eq - match the val parameter as string equal expression.
1107 * ne - match the val parameter as string not-equal expression.
1108 * re - match the val parameter as regular expression.
1109 * sw - match the val parameter as 'starts with' expression.
1110
1111 All parameters can be static strings or contain variables.
1112
1113 This function can be used from ANY_ROUTE.
1114
1115 Example 1.38. sht_match_name usage
1116 ...
1117 if(sht_match_name("ha", "eq", "alice")) {
1118 ...
1119 }
1120 ...
1121
1122 4.20. sht_has_name(htable, op, mval)
1123
1124 Alias for sht_match_name().
1125
1126 4.21. sht_match_str_value(htable, op, mval)
1127
1128 Return greater than 0 (true) if the htable has an item that matches the
1129 string value against the mval parameter.
1130
1131 The op parameter can be:
1132 * eq - match the val parameter as string equal expression.
1133 * ne - match the val parameter as string not-equal expression.
1134 * re - match the val parameter as regular expression.
1135 * sw - match the val parameter as 'starts with' expression.
1136
1137 All parameters can be static strings or contain variables.
1138
1139 This function can be used from ANY_ROUTE.
1140
1141 Example 1.39. sht_match_name usage
1142 ...
1143 if(sht_match_str_value("ha", "eq", "alice")) {
1144 ...
1145 }
1146 ...
1147
1148 4.22. sht_has_str_value(htable, op, mval)
1149
1150 Alias for sht_match_str_value().
1151
1152 4.23. sht_is_null(htname, itname)
1153
1154 Check if the item with the name 'itname' from hash table 'htname'
1155 exists. This API function is equivalent to '($sht(htname=>itname) ==
1156 $null)'.
1157
1158 This function returns:
1159 * 3 - if parameters could not be parsed
1160 * 2 - if hash table is null
1161 * 1 - if the value is null
1162 * -1 - if hash table default is non null
1163 * -2 - if the value is not null
1164
1165 All parameters can be static strings or contain variables.
1166
1167 This function can be used from ANY_ROUTE.
1168
1169 Example 1.40. sht_is_null usage
1170 ...
1171 if (sht_is_null("my_ht", "my_key")) {
1172 xlog("L_ERR", "could not find my_key in my_ht\n");
1173 }
1174 ...
1175
1176 4.24. sht_inc(htname, itname)
1177
1178 Atomically increment the value of 'itname' in hash table 'htname'. This
1179 API function is similar to '$shtinc(htname=>itname)', without returning
1180 the value.
1181
1182 This function returns 1 on successful increment or -1 otherwise.
1183
1184 All parameters can be static strings or contain variables.
1185
1186 This function can be used from ANY_ROUTE.
1187
1188 Example 1.41. sht_inc usage
1189 ...
1190 if (!sht_inc("my_ht", "my_key")) {
1191 xlog("L_ERR", "could not increment my_key\n");
1192 }
1193 ...
1194
1195 4.25. sht_dec(htname, itname)
1196
1197 Atomically decrement the value of 'itname' in hash table 'htname'. This
1198 API function is similar to '$sht_dec(htname=>itname)', without
1199 returning the value.
1200
1201 This function returns 1 on successful decrement or -1 otherwise.
1202
1203 All parameters can be static strings or contain variables.
1204
1205 This function can be used from ANY_ROUTE.
1206
1207 Example 1.42. sht_dec usage
1208 ...
1209 if (!sht_dec("my_ht", "my_key")) {
1210 xlog("L_ERR", "could not decrement my_key\n");
1211 }
1212 ...
1213
1214 5. Exported pseudo-variables
1215
1216 * $sht(htable=>key)
1217 * $shtex(htable=>key)
1218 * $shtcn(htable=>key)
1219 * $shtcv(htable=>key)
1220 * $shtinc(htable=>key)
1221 * $shtdec(htable=>key)
1222 * $shtitkey(iname)
1223 * $shtitval(iname)
1224 * $shtrecord(attribute)
1225
1226 Exported pseudo-variables are documented at
1227 https://www.kamailio.org/wikidocs/.
1228
1229 6. RPC Commands
1230
1231 6.1. htable.get htable key
1232 6.2. htable.delete htable key
1233 6.3. htable.sets htable key value
1234 6.4. htable.seti htable key value
1235 6.5. htable.setex htable key expire
1236 6.6. htable.setxs htable key value expire
1237 6.7. htable.setxi htable key value expire
1238 6.8. htable.dump htable
1239 6.9. htable.reload htable
1240 6.10. htable.store htable
1241 6.11. htable.flush htable
1242 6.12. htable.listTables
1243 6.13. htable.stats
1244 6.14. htable.dmqsync [htable]
1245 6.15. htable.dmqresync htable
1246
1247 6.1. htable.get htable key
1248
1249 Lists one value in a hash table
1250
1251 Name: htable.get
1252
1253 Parameters:
1254 * htable : Name of the hash table to dump
1255 * key : Key name of the hash table value to dump. Note: if key name
1256 is an integer value, it should be prefixed with s:
1257
1258 Example:
1259 ...
1260 # Dump $sht(students=>alice)
1261 kamctl rpc htable.get students alice
1262
1263 # Dump $sht(students=>15)
1264 kamctl rpc htable.get students s:15
1265
1266 # Dump first entry in array key course $sht(students=>course[0])
1267 kamctl rpc htable.get students course[0]
1268 ...
1269
1270 6.2. htable.delete htable key
1271
1272 Delete one value in a hash table
1273
1274 Name: htable.delete
1275
1276 Parameters:
1277 * htable : Name of the hash table to delete
1278 * key : Key name of the hash table value to delete. Note: if key name
1279 is an integer value, it should be prefixed with s:
1280
1281 Example:
1282 ...
1283 # Delete $sht(students=>alice)
1284 kamctl rpc htable.delete students alice
1285
1286 # Delete $sht(students=>15)
1287 kamctl rpc htable.delete students s:15
1288
1289 # Delete first entry in array key course $sht(students=>course[0])
1290 kamctl rpc htable.delete students course[0]
1291 ...
1292
1293 6.3. htable.sets htable key value
1294
1295 Set an item in hash table to string value.
1296
1297 Name: htable.sets
1298
1299 Parameters:
1300 * htable : Name of the hash table
1301 * key : Key name in the hash table. Note: if key name is an integer
1302 value, it should be prefixed with s:
1303 * Value : String value for the item
1304
1305 Example:
1306 ...
1307 # Set $sht(test=>x) as string
1308 kamctl rpc htable.sets test x abc
1309
1310 # Set $sht(test=>5) as string
1311 kamctl rpc htable.sets test s:5 abc
1312
1313 # Set firsti entry in array key x $sht(test=>x[0]) as string
1314 kamctl rpc htable.sets test x[0] abc
1315 ...
1316
1317 6.4. htable.seti htable key value
1318
1319 Set an item in hash table to integer value.
1320
1321 Name: htable.seti
1322
1323 Parameters:
1324 * htable : Name of the hash table
1325 * key : Key name in the hash table. Note: if key name is an integer
1326 value, it should be prefixed with s:
1327 * Value : Integer value for the item
1328
1329 Example:
1330 ...
1331 # Set $sht(test=>x) as integer
1332 kamctl rpc htable.seti test x 123
1333
1334 # Set $sht(test=>5) as integer
1335 kamctl rpc htable.seti test s:5 123
1336
1337 # Set first entry in array key x $sht(test=>x[0]) as integer
1338 kamctl rpc htable.seti test x[0] 123
1339 ...
1340
1341 6.5. htable.setex htable key expire
1342
1343 Set the expire for an item in hash table.
1344
1345 Name: htable.setex
1346
1347 Parameters:
1348 * htable : name of the hash table
1349 * key : key name in the hash table. Note: if key name is an integer
1350 value, it should be prefixed with s:
1351 * expire : integer value for the expire (seconds)
1352
1353 Example:
1354 ...
1355 # set expire for $sht(test=>x) to 120 secs
1356 kamctl rpc htable.setex test x 120
1357
1358 # set expire for $sht(test=>10) to 120 secs
1359 kamctl rpc htable.setex test s:10 120
1360 ...
1361
1362 6.6. htable.setxs htable key value expire
1363
1364 Set the string value and expire for an item in hash table.
1365
1366 Name: htable.setxs
1367
1368 Parameters:
1369 * htable : name of the hash table
1370 * key : key name in the hash table. Note: if key name is an integer
1371 value, it should be prefixed with s:
1372 * value : string value for the item
1373 * expire : integer value for the expire (seconds)
1374
1375 Example:
1376 ...
1377 # set value to 'abc' and expire for $sht(test=>x) to 120 secs
1378 kamctl rpc htable.setxs test x abc 120
1379
1380 # set value to 'abc' and expire for $sht(test=>10) to 120 secs
1381 kamctl rpc htable.setxs test s:10 abc 120
1382 ...
1383
1384 6.7. htable.setxi htable key value expire
1385
1386 Set the integer value and expire for an item in hash table.
1387
1388 Name: htable.setxi
1389
1390 Parameters:
1391 * htable : name of the hash table
1392 * key : key name in the hash table. Note: if key name is an integer
1393 value, it should be prefixed with s:
1394 * value : integer value for the item
1395 * expire : integer value for the expire (seconds)
1396
1397 Example:
1398 ...
1399 # set value to 10 and expire for $sht(test=>x) to 120 secs
1400 kamctl rpc htable.setxi test x 10 120
1401
1402 # set value to 10 and expire for $sht(test=>5) to 120 secs
1403 kamctl rpc htable.setxi test s:5 10 120
1404 ...
1405
1406 6.8. htable.dump htable
1407
1408 Lists all the values in a hash table
1409
1410 Name: htable.dump
1411
1412 Parameters:
1413 * htable : Name of the hash table to dump
1414
1415 Example:
1416 ...
1417 kamctl rpc htable.dump ipban
1418 ...
1419
1420 6.9. htable.reload htable
1421
1422 Reload hash table from database.
1423
1424 Name: htable.reload
1425
1426 Parameters:
1427 * htable : Name of the hash table to reload
1428
1429 Example:
1430 ...
1431 kamctl rpc htable.reload ipban
1432 ...
1433
1434 6.10. htable.store htable
1435
1436 Store hash table to database.
1437
1438 Name: htable.store
1439
1440 Parameters:
1441 * htable : Name of the hash table to store
1442
1443 Example:
1444 ...
1445 kamctl rpc htable.store ipban
1446 ...
1447
1448 6.11. htable.flush htable
1449
1450 Empty the hash table
1451
1452 Name: htable.flush
1453
1454 Parameters:
1455 * htable : Name of the hash table to flush
1456
1457 Example:
1458 ...
1459 kamctl rpc htable.flush ipban
1460 ...
1461
1462 6.12. htable.listTables
1463
1464 Lists all defined tables
1465
1466 Name: htable.listTables
1467
1468 Parameters:
1469 * None
1470
1471 Example:
1472 ...
1473 kamctl rpc htable.listTables
1474 ...
1475
1476 6.13. htable.stats
1477
1478 Get statistics for hash tables - name, number of slots, number of
1479 items, max number of items per slot, min number of items per slot.
1480
1481 Name: htable.stats
1482
1483 Parameters:
1484 * None
1485
1486 Example:
1487 ...
1488 kamctl rpc htable.stats
1489 ...
1490
1491 6.14. htable.dmqsync [htable]
1492
1493 Perform a DMQ sync action.
1494
1495 Name: htable.dmqsync
1496
1497 Parameters:
1498 * htable: name of the hash table to perform dmq sync for. If not
1499 provided, sync is performed for all hash tables that have dmq sync
1500 enabled.
1501
1502 Example:
1503 ...
1504 kamctl rpc htable.dmqsync
1505 kamctl rpc htable.dmqsync ipban
1506 ...
1507
1508 6.15. htable.dmqresync htable
1509
1510 Perform a resync action (flush and dmq sync).
1511
1512 Name: htable.dmqresync
1513
1514 Parameters:
1515 * htable: name of the hash table to perform resync for.
1516
1517 Example:
1518 ...
1519 kamctl rpc htable.dmqresync ipban
1520 ...
1521
1522 7. Event routes
1523
1524 7.1. htable:mod-init
1525 7.2. htable:expired:<table>
1526
1527 7.1. htable:mod-init
1528
1529 When defined, the module calls event_route[htable:mod-init] after all
1530 modules have been initialized. A typical use case is to initialise
1531 items in hash tables. The event route is executed only once, after core
1532 and module initialization, but before Kamailio forks any child
1533 processes.
1534
1535 Note: do not expect to use functions from all other modules here, even
1536 if they are loaded before the htable module, because many of them
1537 initialize their runtime structures inside child init callbacks, which
1538 are executed after this moment, when forking child processes. For
1539 example, sqlops cannot be used, connections to database are initialized
1540 in child init. Even more, it is recommended not to use functions from
1541 other modules, because it can mess up what they created in mod init
1542 callback and expect in child init callback. It should be ok to use
1543 functions from htable module or assignment operations.
1544 ...
1545 event_route[htable:mod-init] {
1546 $sht(a=>x) = 1;
1547 }
1548 ...
1549
1550 7.2. htable:expired:<table>
1551
1552 When defined, the module calls event_route[htable:expired:<table>] when
1553 an entry in the given table expires. In this event route, the key and
1554 value of the expired record are available with the $shtrecord(key) and
1555 $shtrecord(value) pseudo-variables.
1556 ...
1557
1558 event_route[htable:expired:mytable] {
1559 xlog("mytable record expired $shtrecord(key) => $shtrecord(value)\n");
1560 }
1561 ...