File: select_module.xml

package info (click to toggle)
kamailio 6.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 70,472 kB
  • sloc: ansic: 859,394; xml: 203,514; makefile: 9,688; sh: 9,105; sql: 8,571; yacc: 4,121; python: 3,086; perl: 2,955; java: 449; cpp: 289; javascript: 270; php: 258; ruby: 248; awk: 27
file content (223 lines) | stat: -rw-r--r-- 7,749 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" 
   "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">

<section id="select_module" xmlns:xi="http://www.w3.org/2001/XInclude">
    <sectioninfo>
	<revhistory>
	    <revision>
		<revnumber>$Revision$</revnumber>
		<date>$Date$</date>
	    </revision>
	</revhistory>
    </sectioninfo>
    
<section><title>Extend select framework with module related functions</title>
<para>
	If you want to extend functions which select framework can call with some
	dependent on module you have to follow next steps:
	<itemizedlist>
		<listitem><para>
			define working functions
			</para>
		</listitem>
		<listitem><para>
			create module's select parsing table
		</para>
		</listitem>
		<listitem><para>
			in module's mod_init call register_select_table
		</para>
		</listitem>
	</itemizedlist>
</para>
</section>
<section>
<title>Define your working functions</title>
<para>
	Working functions are the piece of code, which is called as when SER needs
	get result of select function (defined as @name.foo[2].bar[5]). The working copy
	has following definition:
</para>
<para><emphasis>
int <function>select_function_name</function>(str* res, struct select *s, struct sip_msg *msg)
</emphasis>
	Pointer to the string result workspace <emphasis>res</emphasis>, don't allocate
	memory for the string, but use static buffer. There is no way to free the
	allocated memory.
	Pointer to the parsed select structure <emphasis>s</emphasis>. Holds all names,
	numbers divided by the dot and square bracket notation. Use that if any of the
	part used CONSUME_NEXT_STR or CONSUME_NEXT_INT to get the value.
	Pointer to the processed message structure <emphasis>msg</emphasis>.
</para>
<para>
	<emphasis>FIXUP CALL:</emphasis>
	If you set FIXUP_CALL flag for the final
	function, the fixup call will be done immediately after function resolution.
	Such call is indicated with res==NULL &amp;&amp; msg==NULL. Such call can convert any of
	the select's parameter into internal data (can hold one pointer), if you do
	that, set param_type to SEL_PARAM_DATA.
</para>
<para>
	Result code of the function declares if the call was successful and if the result is
	valid string or empty string.
</para>
<itemizedlist>
	<listitem><para><emphasis>-1</emphasis> error</para></listitem>
	<listitem><para><emphasis>0</emphasis> success, res contains non-empty string</para></listitem>
	<listitem><para><emphasis>1</emphasis> success, result of select call is empty string (res can be left
	unchanged)</para></listitem>
</itemizedlist>
</section>
<section>
<title>Create module's select parsing table</title>
<para>Define static table of select_row_t type and initialize it directly.
</para>
<para>The table is representation of tree (or oriented graph if you want), where
first column represents current (up-to now) resolved function (starting with NULL),
next two columns define if next parameter is integer or particular string, next
column is new resolved function which will be tested in next round and the last
column is set of flags.
</para>
<programlisting><![CDATA[
static select_row_t test_sel[] = {
	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("test"), select_test, 0},
	{ select_test, SEL_PARAM_STR, STR_STATIC_INIT("value"), select_test_val, 0},
	{ select_test, SEL_PARAM_STR, STR_STATIC_INIT("echo"), select_test_echo, CONSUME_NEXT_STR},
	{ NULL, SEL_PARAM_INT, STR_NULL, NULL, 0}
};
]]>
</programlisting>
<para>
So in the previous example, the first line will accept syntax
<emphasis>@test</emphasis> and set the resolved function to select_test. In the
next round, all rows with the select_test in the first column will be used to
match, so the next two lines
are candidates to match depending on the next part of select syntax. If it
matches <emphasis>@test.value</emphasis>, the function is resolved to
select_test_val, if it matches <emphasis>@test.echo.anystring</emphasis>, it is
resolved into select_test_echo. Flag <emphasis>CONSUME_NEXT_STR</emphasis> will
accept any string at the 3rd position. As the <emphasis>OPTIONAL</emphasis> flag
is not present, it won't accept just the <emphasis>@test.echo</emphasis> syntax.
</para>
<para>
The table ends with the <emphasis>NULL</emphasis> in the 1st and 4th column,
other columns are not checked (the notation in the example suits well).
</para>
<para>
At the resolving time all function names must be already defined. For functions which are
not leaves, you can use macro <emphasis>ABSTRACT_F(name)</emphasis> to define empty
function, for working function you can use advance definition using
<emphasis>SELECT_F(name)</emphasis> macro.
</para>
</section>
<!--
TODO:
<section>
<title>Available flag options and their meaning</title>
<para>
<emphasis></emphasis>
</para>
</section>
-->
<section>
<title>Register the created table</title>
<para>
	In the module initialization function call <emphasis>register_select_table(table)</emphasis>
	where table is the parsing tree/table you have defined in previous step.
	This call ensures, that the table will become part of the parsing logic for
	all select framework calls defined in the script file or called by another
	module's parse_select.
</para>
<para>
	<emphasis>NOTE:</emphasis> The tables are inserted into the beginning of the
	list, so the core's table (and thus parseable names and definitions) can be
	overridden by module's function, if it is defined with the same name. To
	avoid such situation, the best practice is to start module's select with the
	module's name. E.g in our example code both select functions start
	with @test...
</para>
</section>
<section>
<title>Example - module defining select extension</title>
<para>Example module <emphasis>test</emphasis>, which defines two select function.
<itemizedlist>
	<listitem><para>
	<emphasis>@test.value</emphasis> - returns value passed as modules parameter
	"value"
	</para></listitem>
	<listitem><para>
	<emphasis>@test.echo.xxx</emphasis> - returns xxx regardless of what you put
	as xxx (shows CONSUME_NEXT_STR option)
	</para></listitem>
</itemizedlist>
</para>
<example id="test.c">
<title>test.c</title>
<programlisting><![CDATA[
#include <string.h>
#include "../../sr_module.h"
#include "../../str.h"
#include "../../dprint.h"
#include "../../select.h"

MODULE_VERSION

static cmd_export_t cmds[] = {
	{0, 0, 0, 0, 0}
};

static char* value=NULL;

static param_export_t params[] = {
	{"value", STR_PARAM, &value},
	{0, 0, 0}
};

int select_test_val(str* res, struct select* s, struct sip_msg* msg) {
	DBG("SELECT_TEST_VAL called test_val=%s\n", value);
	res->s=value;
	res->len=strlen(value);
	return 0;
}

int select_test_echo(str* res, struct select* s, struct sip_msg* msg) {
	DBG("SELECT_TEST_ECHO called\n");
	if (s->params[s->n-1].type==SEL_PARAM_STR) {
		*res=s->params[s->n-1].v.s;
		return 0;
	} else
		return -1;
}

ABSTRACT_F(select_test)
	
static select_row_t test_sel[] = {
	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("test"), select_test, 0},
	{ select_test, SEL_PARAM_STR, STR_STATIC_INIT("value"), select_test_val, 0},
	{ select_test, SEL_PARAM_STR, STR_STATIC_INIT("echo"), select_test_echo, CONSUME_NEXT_STR},
	{ NULL, SEL_PARAM_INT, STR_NULL, NULL, 0}
};
 
static int init(void) {
	register_select_table(test_sel);
	return 0;
};

struct module_exports exports = {
	"test", 
	cmds,           /* Exported commands */
	0,              /* RPC methods */
	params,         /* Exported parameters */
	init,           /* module initialization function */
	0,              /* response function*/
	0,              /* destroy function */
	0,              /* oncancel function */
	0               /* per-child init function */
};

]]>
</programlisting>
</example>
</section>
</section>