Debian Codegraph @

src/modules/db_text/README

raw
1 DBTEXT Module
2
3 Daniel-Constantin Mierla
4
5 <[email protected]>
6
7 Edited by
8
9 Ovidiu Sas
10
11 <[email protected]>
12
13 Daniel-Constantin Mierla
14
15 <[email protected]>
16
17 Olle E. Johansson
18
19 <[email protected]>
20
21 Copyright © 2003, 2004 FhG FOKUS
22 __________________________________________________________________
23
24 Table of Contents
25
26 1. Admin Guide
27
28 1. Overview
29
30 1.1. Design of dbtext engine
31 1.2. Internal format of a dbtext table
32
33 2. Known limitations
34 3. Dependencies
35
36 3.1. Kamailio modules
37 3.2. External libraries or applications
38
39 4. Parameters
40
41 4.1. db_mode (integer)
42 4.2. db_delim (string)
43 4.3. default_connection (string)
44 4.4. emptystring (integer)
45 4.5. file_buffer_size (integer)
46 4.6. max_result_rows (integer)
47
48 5. Exported RPC Functions
49
50 5.1. db_text.dump
51 5.2. db_text.query
52
53 6. Installation and Running
54
55 6.1. Using db_text with a basic Kamailio configuration
56
57 2. Developer Guide
58
59 List of Examples
60
61 1.1. Sample of a dbtext table
62 1.2. Minimal Kamailio location dbtext table definition
63 1.3. Minimal Kamailio subscriber dbtext table example
64 1.4. Set db_mode parameter
65 1.5. Set db_mode parameter
66 1.6. Set default_connection parameter
67 1.7. Set emptystring parameter
68 1.8. Set file_buffer_size parameter
69 1.9. Set max_result_rows parameter
70 1.10. Load the dbtext module
71 1.11. Definition of 'subscriber' table (one line)
72 1.12. Definition of 'location' and 'aliases' tables (one line)
73 1.13. Definition of 'version' table and sample records
74 1.14. Configuration file
75
76 Chapter 1. Admin Guide
77
78 Table of Contents
79
80 1. Overview
81
82 1.1. Design of dbtext engine
83 1.2. Internal format of a dbtext table
84
85 2. Known limitations
86 3. Dependencies
87
88 3.1. Kamailio modules
89 3.2. External libraries or applications
90
91 4. Parameters
92
93 4.1. db_mode (integer)
94 4.2. db_delim (string)
95 4.3. default_connection (string)
96 4.4. emptystring (integer)
97 4.5. file_buffer_size (integer)
98 4.6. max_result_rows (integer)
99
100 5. Exported RPC Functions
101
102 5.1. db_text.dump
103 5.2. db_text.query
104
105 6. Installation and Running
106
107 6.1. Using db_text with a basic Kamailio configuration
108
109 1. Overview
110
111 1.1. Design of dbtext engine
112 1.2. Internal format of a dbtext table
113
114 The module implements a simplified database engine based on text files.
115 It can be used by Kamailio DB interface instead of other database
116 module (like MySQL).
117
118 The module is meant for use in demos or small devices that do not
119 support other DB modules. It keeps everything in memory and if you deal
120 with large amount of data you may run out of memory quickly. Also, it
121 does not implement all standard database facilities (like order by), it
122 includes minimal functionality to work properly (who knows ?!?) with
123 Kamailio.
124
125 NOTE: the timestamp is printed in an integer value from time_t
126 structure. If you use it in a system that cannot do this conversion, it
127 will fail (support for such situation is in to-do list).
128
129 NOTE: even when db_text is in non-caching mode, the module does not
130 write back to hard drive after changes. In this mode, the module checks
131 if the corresponding file on disk has changed, and reloads it. The
132 write to disk happens at Kamailio shut down. If db_text is in caching
133 mode, many "reload" functions in various modules will not work.
134
135 1.1. Design of dbtext engine
136
137 The dbtext database system architecture:
138 * A database is represented by a directory in the local file system.
139 NOTE: when you use dbtext in Kamailio, the database URL for modules
140 must be the path to the directory where the table-files are
141 located, prefixed by “text://”, e.g., “text:///var/dbtext/ser”. If
142 there is no “/” after “text://” then “CFG_DIR/” is inserted at the
143 beginning of the database path. So, either you provide an absolute
144 path to database directory or a relative one to “CFG_DIR”
145 directory.
146 Do not forget that all databases in Kamailio needs the “version”
147 table.
148 * A table is represented by a text file inside database directory.
149
150 1.2. Internal format of a dbtext table
151
152 The first line is the definition of the columns. Each column must be
153 declared in the following format:
154 * the name of column must not include white spaces.
155 * the format of a column definition is: name(type,attr).
156 * between two column definitions must be a white space, e.g.,
157 “first_name(str) last_name(str)”.
158 * the type of a column can be:
159 + int - integer numbers.
160 + double - real numbers with two decimals.
161 + str - strings with maximum size of 4KB.
162 * a column can have one of the attributes:
163 + auto - only for 'int' columns, the maximum value in that
164 column is incremented and stored in this field if it is not
165 provided in queries.
166 + null - accept null values in column fields.
167 + if no attribute is set, the fields of the column cannot have
168 null value.
169 * each other line is a row with data. The line ends with “\n”.
170 * the fields are separated by “:”.
171 * no value between two ':' (or between ':' and start/end of a row)
172 means “null” value. If the parameter "emptystring" is enabled,
173 db_text sets a NULL string to an empty string value.
174 * next characters must be escaped in strings: “\n”, “\r”, “\t”, “:”.
175 * 0 -- the zero value must be escaped too.
176
177 Example 1.1. Sample of a dbtext table
178 ...
179 id(int,auto) name(str) flag(double) desc(str,null)
180 1:nick:0.34:a\tgood\: friend
181 2:cole:-3.75:colleague
182 3:bob:2.50:
183 ...
184
185 Example 1.2. Minimal Kamailio location dbtext table definition
186 ...
187 username(str) contact(str) expires(int) q(double) callid(str) cseq(int)
188 ...
189
190 Example 1.3. Minimal Kamailio subscriber dbtext table example
191 ...
192 username(str) password(str) ha1(str) domain(str) ha1b(str)
193 suser:supasswd:xxx:alpha.org:xxx
194 ...
195
196 2. Known limitations
197
198 This database interface does not support data insertion with default
199 values. All such values specified in the database template are ignored.
200 So it is advisable to specify all data for a column at insertion
201 operations.
202
203 3. Dependencies
204
205 3.1. Kamailio modules
206 3.2. External libraries or applications
207
208 3.1. Kamailio modules
209
210 These modules must be loaded before this module:
211 * none.
212
213 3.2. External libraries or applications
214
215 These libraries or applications must be installed before running
216 Kamailio with this module:
217 * none.
218
219 4. Parameters
220
221 4.1. db_mode (integer)
222 4.2. db_delim (string)
223 4.3. default_connection (string)
224 4.4. emptystring (integer)
225 4.5. file_buffer_size (integer)
226 4.6. max_result_rows (integer)
227
228 4.1. db_mode (integer)
229
230 Set caching mode (0) or non-caching mode (1). In caching mode, data is
231 loaded at startup. In non-caching mode, the module checks every time a
232 table is requested whether the corresponding file on disk has changed,
233 and if yes, will re-load the table from file.
234
235 Default value is “0”.
236
237 Example 1.4. Set db_mode parameter
238 ...
239 modparam("db_text", "db_mode", 1)
240 ...
241
242 4.2. db_delim (string)
243
244 Set the delimiter inside the db_text file.
245
246 Default value is “:”.
247
248 Example 1.5. Set db_mode parameter
249 ...
250 modparam("db_text", "db_delim", "|")
251 ...
252
253 4.3. default_connection (string)
254
255 connection for use with rpc query command.
256
257 Default value is “none” (off).
258
259 Example 1.6. Set default_connection parameter
260 ...
261 modparam("db_text", "default_connection", "text:///var/db/ka
262 mailio/dbtext")
263 ...
264
265 4.4. emptystring (integer)
266
267 db_text by default handles an empty string as a NULL value. Some
268 modules, like the dialplan module, does not accept NULL strings. If you
269 enable emptystring an empty string will not be NULL, but an empty
270 string.
271
272 Default value is “0” (off).
273
274 Example 1.7. Set emptystring parameter
275 ...
276 modparam("db_text", "emptystring", 1)
277 ...
278
279 4.5. file_buffer_size (integer)
280
281 size of the buffer used to read the text file. Some presence tables
282 have columns with large content.
283
284 Default value is “16384”.
285
286 Example 1.8. Set file_buffer_size parameter
287 ...
288 modparam("db_text", "file_buffer_size", 8192)
289 ...
290
291 4.6. max_result_rows (integer)
292
293 number of rows to read from the text file.
294
295 Default value is “100000”.
296
297 Example 1.9. Set max_result_rows parameter
298 ...
299 modparam("db_text", "max_result_rows", 1000000)
300 ...
301
302 5. Exported RPC Functions
303
304 5.1. db_text.dump
305 5.2. db_text.query
306
307 5.1. db_text.dump
308
309 Write back to hard drive all modified tables.
310
311 Name: db_text.dump
312
313 Parameters: none
314
315 RPC Command Format:
316 kamctl rpc db_text.dump
317
318 5.2. db_text.query
319
320 run sql command
321
322 Name: db_text.query
323
324 Parameters: sqlcmd
325
326 RPC Command Format:
327 kamctl rpc db_text.query 'select * from location where username="xxx
328 "'
329
330 6. Installation and Running
331
332 6.1. Using db_text with a basic Kamailio configuration
333
334 Compile the module and load it instead of mysql or other DB modules.
335
336 REMINDER: when you use text in Kamailio, the database URL for modules
337 must be the path to the directory where the table-files are located,
338 prefixed by “text://”, e.g., “text:///var/dbtext/ser”. If there is no
339 “/” after “text://” then “CFG_DIR/” is inserted at the beginning of the
340 database path. So, either you provide an absolute path to database
341 directory or a relative one to “CFG_DIR” directory.
342
343 Example 1.10. Load the dbtext module
344 ...
345 loadmodule "/path/to/kamailio/modules_k/db_text.so"
346 ...
347 modparam("module_name", "db_url", "text:///path/to/dbtext/database")
348 ...
349
350 6.1. Using db_text with a basic Kamailio configuration
351
352 Here are definitions for the most important tables as well as a basic
353 configuration file to use db_text with Kamailio. The table structures
354 may change in time and you will have to adjust these examples. Check
355 the source code directory “utils/kamctl/dbtext/kamailio” for current
356 definitions.
357
358 You have to populate the table 'subscriber' by hand with user profiles
359 in order to have authentication. To use with the given configuration
360 file, the table files must be placed in the '/tmp/serdb' directory.
361
362 Example 1.11. Definition of 'subscriber' table (one line)
363 ...
364 username(str) domain(str) password(str) first_name(str) last_name(str) phone(str
365 ) email_address(str) datetime_created(int) datetime_modified(int) confirmation(s
366 tr) flag(str) sendnotification(str) greeting(str) ha1(str) ha1b(str) perms(str)
367 allow_find(str) timezone(str,null) rpid(str,null)
368 ...
369
370 Example 1.12. Definition of 'location' and 'aliases' tables (one line)
371 ...
372 username(str) domain(str,null) contact(str,null) received(str) expires(int,null)
373 q(double,null) callid(str,null) cseq(int,null) last_modified(str) flags(int) us
374 er_agent(str) socket(str)
375 ...
376
377 Example 1.13. Definition of 'version' table and sample records
378 ...
379 table_name(str) table_version(int)
380 subscriber:3
381 location:6
382 aliases:6
383 ...
384
385 Example 1.14. Configuration file
386 ...
387 #
388 #
389 # simple quick-start config script with dbtext
390 #
391
392 # ----------- global configuration parameters ------------------------
393
394 #debug=3 # debug level (cmd line: -ddd)
395 #fork=yes
396 #log_stderror=no # (cmd line: -E)
397
398 check_via=no # (cmd. line: -v)
399 dns=no # (cmd. line: -r)
400 rev_dns=no # (cmd. line: -R)
401 children=4
402
403 listen=10.100.100.1
404 port=5060
405
406 # ------------------ module loading ----------------------------------
407
408 # use dbtext database
409 loadmodule "modules/dbtext/dbtext.so"
410
411 loadmodule "modules/sl/sl.so"
412 loadmodule "modules/tm/tm.so"
413 loadmodule "modules/rr/rr.so"
414 loadmodule "modules/maxfwd/maxfwd.so"
415 loadmodule "modules/usrloc/usrloc.so"
416 loadmodule "modules/registrar/registrar.so"
417 loadmodule "modules/textops/textops.so"
418 loadmodule "modules/jonrpcs/jsonrpcs.so"
419
420 # modules for digest authentication
421 loadmodule "modules/auth/auth.so"
422 loadmodule "modules/auth_db/auth_db.so"
423
424 # ----------------- setting module-specific parameters ---------------
425
426 # -- usrloc params --
427
428 # use dbtext database for persistent storage
429 modparam("usrloc", "db_mode", 2)
430 modparam("usrloc|auth_db", "db_url", "text:///tmp/kamailiodb")
431
432 # -- auth params --
433 #
434 modparam("auth_db", "calculate_ha1", 1)
435 modparam("auth_db", "password_column", "password")
436 modparam("auth_db", "user_column", "username")
437 modparam("auth_db", "domain_column", "domain")
438
439 # -- rr params --
440 # add value to ;lr param to make some broken UAs happy
441 modparam("rr", "enable_full_lr", 1)
442
443 # ------------------------- request routing logic -------------------
444
445 # main routing logic
446
447 route{
448 # initial sanity checks -- messages with
449 # max_forwards==0, or excessively long requests
450 if (!mf_process_maxfwd_header("10")) {
451 sl_send_reply("483","Too Many Hops");
452 exit;
453 };
454 if (msg:len >= max_len ) {
455 sl_send_reply("513", "Message too big");
456 exit;
457 };
458
459 # we record-route all messages -- to make sure that
460 # subsequent messages will go through our proxy; that's
461 # particularly good if upstream and downstream entities
462 # use different transport protocol
463 if (!method=="REGISTER") record_route();
464
465 # subsequent messages within a dialog should take the
466 # path determined by record-routing
467 if (loose_route()) {
468 # mark routing logic in request
469 append_hf("P-hint: rr-enforced\r\n");
470 route(1);
471 exit;
472 };
473
474 if (!uri==myself) {
475 # mark routing logic in request
476 append_hf("P-hint: outbound\r\n");
477 route(1);
478 exit;
479 };
480
481 # if the request is for other domain use UsrLoc
482 # (in case, it does not work, use the following command
483 # with proper names and addresses in it)
484 if (uri==myself) {
485 if (method=="REGISTER") {
486 # digest authentication
487 if (!www_authorize("", "subscriber")) {
488 www_challenge("", "0");
489 exit;
490 };
491
492 save("location");
493 exit;
494 };
495
496 lookup("aliases");
497 if (!uri==myself) {
498 append_hf("P-hint: outbound alias\r\n");
499 route(1);
500 exit;
501 };
502
503 # native SIP destinations are handled using our USRLOC DB
504 if (!lookup("location")) {
505 sl_send_reply("404", "Not Found");
506 exit;
507 };
508 };
509 append_hf("P-hint: usrloc applied\r\n");
510 route(1);
511 }
512
513 route[1]
514 {
515 # send it out now; use stateful forwarding as it works reliably
516 # even for UDP2TCP
517 if (!t_relay()) {
518 sl_reply_error();
519 };
520 }
521
522
523 ...
524
525 Chapter 2. Developer Guide
526
527 Once you have the module loaded, you can use the API specified by
528 Kamailio DB interface.